This repository has been archived on 2026-03-09. You can view files and clone it, but cannot push or open issues or pull requests.
php-agentic/Mcp/Tools/Agent/Brain/BrainForget.php
Snider 2c6a095a0e fix(brain): address spec review findings
- Move BrainForget DB lookup inside circuit breaker for consistency
- Check ensureCollection() PUT response for Qdrant errors
- Wrap remember() in DB::transaction for atomicity
- Align migration confidence default to 0.8 (matches PHP default)

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-03 09:45:05 +00:00

103 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Core\Mod\Agentic\Mcp\Tools\Agent\Brain;
use Core\Mcp\Dependencies\ToolDependency;
use Core\Mod\Agentic\Mcp\Tools\Agent\AgentTool;
use Core\Mod\Agentic\Models\BrainMemory;
use Core\Mod\Agentic\Services\BrainService;
use Illuminate\Support\Facades\Log;
/**
* Remove a memory from the shared OpenBrain knowledge store.
*
* Deletes the memory from both MariaDB and Qdrant.
* Workspace-scoped: agents can only forget memories in their own workspace.
*/
class BrainForget extends AgentTool
{
protected string $category = 'brain';
protected array $scopes = ['write'];
public function dependencies(): array
{
return [
ToolDependency::contextExists('workspace_id', 'Workspace context required to forget memories'),
];
}
public function name(): string
{
return 'brain_forget';
}
public function description(): string
{
return 'Remove a memory from the shared OpenBrain knowledge store. Permanently deletes from both database and vector index.';
}
public function inputSchema(): array
{
return [
'type' => 'object',
'properties' => [
'id' => [
'type' => 'string',
'format' => 'uuid',
'description' => 'UUID of the memory to remove',
],
'reason' => [
'type' => 'string',
'description' => 'Optional reason for forgetting this memory',
'maxLength' => 500,
],
],
'required' => ['id'],
];
}
public function handle(array $args, array $context = []): array
{
try {
$id = $this->requireString($args, 'id');
} catch (\InvalidArgumentException $e) {
return $this->error($e->getMessage());
}
$workspaceId = $context['workspace_id'] ?? null;
if ($workspaceId === null) {
return $this->error('workspace_id is required. Ensure you have authenticated with a valid API key. See: https://host.uk.com/ai');
}
$reason = $this->optionalString($args, 'reason', null, 500);
$agentId = $context['agent_id'] ?? $context['session_id'] ?? 'anonymous';
return $this->withCircuitBreaker('brain', function () use ($id, $workspaceId, $reason, $agentId) {
// Verify memory exists and belongs to this workspace
$memory = BrainMemory::where('id', $id)
->where('workspace_id', $workspaceId)
->first();
if (! $memory) {
return $this->error("Memory '{$id}' not found in this workspace");
}
Log::info('OpenBrain: memory forgotten', [
'id' => $id,
'type' => $memory->type,
'agent_id' => $agentId,
'reason' => $reason,
]);
app(BrainService::class)->forget($id);
return $this->success([
'forgotten' => $id,
'type' => $memory->type,
]);
}, fn () => $this->error('Brain service temporarily unavailable. Memory could not be removed.', 'service_unavailable'));
}
}