feat: lit/divi-insert and lit/divi-update

All-or-nothing at ability level (S39), duplicate ids refused (S14),
set and attrs on the same path refused (S38), invalid_attr_path error.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
2026-09-26 15:15:37 +02:00
co-authored by Claude Opus 5.5
parent b8cb01812e
commit 110beb7698
11 changed files with 711 additions and 5 deletions
@@ -0,0 +1,175 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Integrations\Divi\Pages;
use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\Errors;
use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\Integrations\Divi\BlockTree\Block;
use WP_Error;
/**
* lit/divi-update: sets or removes values of existing nodes – short-form fields ("set"), full
* form ("attrs", null removes a path or breakpoint), preset and admin label (spec §5.7). Several
* nodes per call, all or nothing (ruling S39): every change is validated first and all errors
* come back together; only then are the attributes Composer::update() computed set. A node may
* appear once per call (ruling S14); set and attrs of one change must not reach the same path
* (ruling S38, checked by Composer::update()).
*/
final class DiviUpdateAbility implements Ability
{
public function __construct(private DiviWriter $writer)
{
}
public function name(): string
{
return 'lit/divi-update';
}
public function label(): string
{
return 'Divi-Knoten ändern';
}
public function description(): string
{
return 'Ändert vorhandene Divi-Knoten: changes = Liste von {id, set: {Kurzform-Felder}, attrs: {Attributpfad: '
. '{Breakpoint: Wert}}, preset, label}. null entfernt einen Wert. Nicht genannte Einstellungen bleiben '
. 'unverändert. Jeder Knoten nur einmal pro Aufruf; denselben Pfad nicht zugleich über set und attrs. '
. 'tree_rev aus lit-divi-outline ist Pflicht; alles oder nichts.';
}
public function inputSchema(): array
{
return [
'type' => 'object',
'properties' => array_merge(DiviWriter::inputProperties(), [
'changes' => [
'type' => 'array',
'minItems' => 1,
'maxItems' => 50,
'items' => [
'type' => 'object',
'properties' => [
'id' => ['type' => 'string'],
'set' => ['type' => 'object', 'description' => 'Kurzform-Felder, z. B. {"text": "Neu"}.'],
'attrs' => [
'type' => 'object',
'description' => 'Vollform: Attributpfad → Breakpoint-Werte.',
],
'preset' => ['type' => ['string', 'null'], 'description' => 'Preset-Name; null entfernt.'],
'label' => [
'type' => ['string', 'null'],
'description' => 'Admin-Label im Divi-Builder; null entfernt.',
],
],
'required' => ['id'],
'additionalProperties' => false,
],
],
]),
'required' => ['post_id', 'tree_rev', 'changes'],
'additionalProperties' => false,
];
}
public function outputSchema(): ?array
{
return ToolResult::schema(['tree_rev' => ['type' => 'string']], ['tree_rev']);
}
public function annotations(): array
{
return ['readonly' => false, 'destructive' => false, 'idempotent' => true];
}
public function isDirect(): bool
{
return true;
}
public function permission(array $input): bool|WP_Error
{
$page = $this->writer->page($input);
return $page instanceof WP_Error ? $page : true;
}
public function execute(array $input): mixed
{
$page = $this->writer->page($input);
if ($page instanceof WP_Error) {
return $page;
}
$changes = is_array($input['changes'] ?? null) ? array_values($input['changes']) : [];
if ($changes === []) {
return Errors::invalidInput('Keine Änderungen angegeben; nichts wurde gespeichert.', [
'changes' => 'mindestens eine Änderung',
]);
}
$composer = $this->writer->composition($page)->composer();
$errors = [];
$invalidPaths = false;
/** @var array<string, int> $seen id => position of its first change */
$seen = [];
/** @var list<array{0: Block, 1: array<string, mixed>}> $updates */
$updates = [];
foreach ($changes as $position => $change) {
$field = sprintf('changes[%d]', $position);
if (! is_array($change)) {
$errors[$field] = 'Objekt mit id erwartet';
continue;
}
$id = is_string($change['id'] ?? null) ? $change['id'] : '';
if ($id !== '' && isset($seen[$id])) {
// Ruling S14: a second change of the same node would overwrite the first silently.
$errors[$field . '.id'] = sprintf(
'Knoten "%s" steht schon in changes[%d]; alle Änderungen eines Knotens in einer Änderung bündeln',
$id,
$seen[$id]
);
continue;
}
$block = $id === '' ? null : $page->index->find($id);
if ($id !== '') {
$seen[$id] = $position;
}
if ($block === null) {
$errors[$field . '.id'] = sprintf('Knoten "%s" gibt es nicht (lit-divi-outline neu lesen)', $id);
continue;
}
$result = $composer->update($block, $change, $field);
$errors += $result['errors'];
$invalidPaths = $invalidPaths || $result['invalid_paths'] !== [];
$updates[] = [$block, $result['attrs']];
}
if ($errors !== []) {
return $invalidPaths
? Errors::invalidAttrPath($errors)
: Errors::invalidInput('Änderungen sind ungültig; nichts wurde gespeichert.', $errors);
}
$updates = array_values(array_filter(
$updates,
static fn (array $update): bool => $update[1] !== $update[0]->attrs()
));
$change = new DiviChange(sprintf('%d Divi-Knoten geändert', count($updates)));
foreach ($updates as [$block, $attrs]) {
// Composer::update() output: every client value already passed the choke point.
$block->setAttrs($attrs);
$change->changed($block, 'updated');
}
return $this->writer->save($this->name(), $page, $change, $input);
}
}