agent/php/Mcp/Tools/Agent/Brain/BrainRecall.php
Snider bb88604045 feat(core): wire Core framework into agentic + monitor subsystems
Phase 2 of Core DI migration:
- Add *core.Core field + SetCore() to PrepSubsystem and monitor.Subsystem
- Register agentic/monitor/brain as Core services with lifecycle hooks
- Mark SetCompletionNotifier and SetNotifier as deprecated (removed in Phase 3)
- Fix monitor test to match actual event names
- initServices() now wires Core refs before legacy callbacks

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-24 14:44:53 +00:00

119 lines
4.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Core\Mod\Agentic\Mcp\Tools\Agent\Brain;
use Core\Mcp\Dependencies\ToolDependency;
use Core\Mod\Agentic\Actions\Brain\RecallKnowledge;
use Core\Mod\Agentic\Mcp\Tools\Agent\AgentTool;
use Core\Mod\Agentic\Models\BrainMemory;
/**
* Semantic search across the shared OpenBrain knowledge store.
*
* Uses vector similarity to find memories relevant to a natural
* language query, with optional filtering by project, type, agent,
* or minimum confidence.
*/
class BrainRecall extends AgentTool
{
protected string $category = 'brain';
protected array $scopes = ['read'];
public function dependencies(): array
{
return [
ToolDependency::contextExists('workspace_id', 'Workspace context required to recall memories'),
];
}
public function name(): string
{
return 'brain_recall';
}
public function description(): string
{
return 'Semantic search across the shared OpenBrain knowledge store. Returns memories ranked by similarity to your query, with optional filtering.';
}
public function inputSchema(): array
{
return [
'type' => 'object',
'properties' => [
'query' => [
'type' => 'string',
'description' => 'Natural language search query (max 2,000 characters)',
'maxLength' => 2000,
],
'top_k' => [
'type' => 'integer',
'description' => 'Number of results to return (default: 5, max: 20)',
'minimum' => 1,
'maximum' => 20,
'default' => 5,
],
'filter' => [
'type' => 'object',
'description' => 'Optional filters to narrow results',
'properties' => [
'project' => [
'type' => 'string',
'description' => 'Filter by project scope',
],
'type' => [
'oneOf' => [
['type' => 'string', 'enum' => BrainMemory::VALID_TYPES],
[
'type' => 'array',
'items' => ['type' => 'string', 'enum' => BrainMemory::VALID_TYPES],
],
],
'description' => 'Filter by memory type (single or array)',
],
'agent_id' => [
'type' => 'string',
'description' => 'Filter by originating agent',
],
'min_confidence' => [
'type' => 'number',
'description' => 'Minimum confidence threshold (0.0-1.0)',
'minimum' => 0.0,
'maximum' => 1.0,
],
],
],
],
'required' => ['query'],
];
}
public function handle(array $args, array $context = []): array
{
$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');
}
$query = $args['query'] ?? '';
$topK = $this->optionalInt($args, 'top_k', 5, 1, 20);
$filter = $this->optional($args, 'filter', []);
if (! is_array($filter)) {
return $this->error('filter must be an object');
}
return $this->withCircuitBreaker('brain', function () use ($query, $workspaceId, $filter, $topK) {
$result = RecallKnowledge::run($query, (int) $workspaceId, $filter, $topK);
return $this->success([
'count' => $result['count'],
'memories' => $result['memories'],
'scores' => $result['scores'],
]);
}, fn () => $this->error('Brain service temporarily unavailable. Recall failed.', 'service_unavailable'));
}
}