- BrainService: Ollama embeddings + Qdrant vector upsert/search/delete - brain_remember: store knowledge with type, tags, confidence, supersession - brain_recall: semantic search with filter by project/type/agent/confidence - brain_forget: workspace-scoped deletion from both stores - brain_list: MariaDB query with model scopes, no vector search - Config: brain.ollama_url, brain.qdrant_url, brain.collection - Boot: BrainService singleton + tool registration via AgentToolRegistry Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
104 lines
3.2 KiB
PHP
104 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);
|
|
|
|
// 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");
|
|
}
|
|
|
|
$agentId = $context['agent_id'] ?? $context['session_id'] ?? 'anonymous';
|
|
|
|
return $this->withCircuitBreaker('brain', function () use ($id, $memory, $reason, $agentId) {
|
|
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'));
|
|
}
|
|
}
|