refactor: DiviWriteAbility base and shared DiviErrors (H4)
The five Divi write tools extend DiviWriteAbility (final permission and execute, change() per tool, shared output schema). DiviErrors holds the unknown-node, position and compose-error mapping; DiviContentComposer uses the same mapping (ledger T9). ToolNamingTest skips abstract classes. 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:
@@ -4,9 +4,7 @@ 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 LIT\MCP\Integrations\Divi\Compose\ColumnWidths;
|
||||
use WP_Error;
|
||||
@@ -19,12 +17,8 @@ use WP_Error;
|
||||
* 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
|
||||
final class DiviUpdateAbility extends DiviWriteAbility
|
||||
{
|
||||
public function __construct(private DiviWriter $writer)
|
||||
{
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return 'lit/divi-update';
|
||||
@@ -77,35 +71,13 @@ final class DiviUpdateAbility implements Ability
|
||||
];
|
||||
}
|
||||
|
||||
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
|
||||
protected function change(DiviPage $page, array $input, DiviComposition $composition): DiviChange|WP_Error
|
||||
{
|
||||
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.', [
|
||||
@@ -113,9 +85,10 @@ final class DiviUpdateAbility implements Ability
|
||||
]);
|
||||
}
|
||||
|
||||
$composer = $this->writer->composition($page)->composer();
|
||||
$composer = $composition->composer();
|
||||
$errors = [];
|
||||
$invalidPaths = false;
|
||||
/** @var list<string> $invalidPaths */
|
||||
$invalidPaths = [];
|
||||
/** @var array<string, int> $seen id => position of its first change */
|
||||
$seen = [];
|
||||
/** @var list<array{0: Block, 1: array<string, mixed>, 2: string}> $updates */
|
||||
@@ -150,7 +123,7 @@ final class DiviUpdateAbility implements Ability
|
||||
|
||||
$result = $composer->update($block, $change, $field);
|
||||
$errors += $result['errors'];
|
||||
$invalidPaths = $invalidPaths || $result['invalid_paths'] !== [];
|
||||
array_push($invalidPaths, ...$result['invalid_paths']);
|
||||
$updates[] = [$block, $result['attrs'], $field];
|
||||
}
|
||||
|
||||
@@ -159,9 +132,10 @@ final class DiviUpdateAbility implements Ability
|
||||
}
|
||||
|
||||
if ($errors !== []) {
|
||||
return $invalidPaths
|
||||
? Errors::invalidAttrPath($errors)
|
||||
: Errors::invalidInput('Änderungen sind ungültig; nichts wurde gespeichert.', $errors);
|
||||
return DiviErrors::composed(
|
||||
['errors' => $errors, 'invalid_paths' => $invalidPaths],
|
||||
'Änderungen sind ungültig; nichts wurde gespeichert.'
|
||||
);
|
||||
}
|
||||
|
||||
$updates = array_values(array_filter(
|
||||
@@ -175,6 +149,6 @@ final class DiviUpdateAbility implements Ability
|
||||
$change->changed($block, 'updated');
|
||||
}
|
||||
|
||||
return $this->writer->save($this->name(), $page, $change, $input);
|
||||
return $change;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user