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\BlockTree\NodeSummary;
|
||||
use WP_Error;
|
||||
@@ -17,12 +15,8 @@ use WP_Error;
|
||||
* nodes inside another node that is deleted anyway do not count twice. The revision in the
|
||||
* result allows undoing it (lit/revision-restore).
|
||||
*/
|
||||
final class DiviDeleteAbility implements Ability
|
||||
final class DiviDeleteAbility extends DiviWriteAbility
|
||||
{
|
||||
public function __construct(private DiviWriter $writer)
|
||||
{
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return 'lit/divi-delete';
|
||||
@@ -59,41 +53,19 @@ final class DiviDeleteAbility implements Ability
|
||||
];
|
||||
}
|
||||
|
||||
public function outputSchema(): ?array
|
||||
{
|
||||
return ToolResult::schema(['tree_rev' => ['type' => 'string']], ['tree_rev']);
|
||||
}
|
||||
|
||||
public function annotations(): array
|
||||
{
|
||||
return ['readonly' => false, 'destructive' => true, 'idempotent' => false];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/** @var array<string, Block> $nodes */
|
||||
$nodes = [];
|
||||
foreach (is_array($input['ids'] ?? null) ? $input['ids'] : [] as $id) {
|
||||
$node = is_string($id) ? $page->index->find($id) : null;
|
||||
if ($node === null) {
|
||||
return DiviPageLoader::unknownNode((string) $id, $page->post);
|
||||
return DiviErrors::unknownNode((string) $id, $page->post);
|
||||
}
|
||||
$nodes[$id] = $node;
|
||||
}
|
||||
@@ -115,14 +87,14 @@ final class DiviDeleteAbility implements Ability
|
||||
);
|
||||
}
|
||||
|
||||
$editor = $this->writer->composition($page)->editor();
|
||||
$editor = $composition->editor();
|
||||
$change = new DiviChange(sprintf('%d Divi-Knoten gelöscht', count($nodes)));
|
||||
foreach ($nodes as $id => $node) {
|
||||
$change->removed((string) $id, $node);
|
||||
$editor->remove($page->index, $node);
|
||||
}
|
||||
|
||||
return $this->writer->save($this->name(), $page, $change, $input);
|
||||
return $change;
|
||||
}
|
||||
|
||||
/** @param array<string, Block> $nodes */
|
||||
|
||||
Reference in New Issue
Block a user