Files
Trilium/src/services/llm/context_extractors/index.ts

140 lines
4.0 KiB
TypeScript
Raw Normal View History

2025-03-19 16:19:48 +00:00
/**
* Agent Tools Index
*
* This file exports all available agent tools for use by the LLM.
* Tools are prioritized in order of importance/impact.
*/
import { VectorSearchTool } from './vector_search_tool.js';
import { NoteNavigatorTool } from './note_navigator_tool.js';
import { QueryDecompositionTool } from './query_decomposition_tool.js';
import { ContextualThinkingTool } from './contextual_thinking_tool.js';
// Import services needed for initialization
2025-03-19 19:28:02 +00:00
import contextService from '../context_service.js';
2025-03-19 16:19:48 +00:00
import aiServiceManager from '../ai_service_manager.js';
import log from '../../log.js';
// Import interfaces
import type {
IAgentToolsManager,
LLMServiceInterface,
IVectorSearchTool,
INoteNavigatorTool,
IQueryDecompositionTool,
IContextualThinkingTool
} from '../interfaces/agent_tool_interfaces.js';
2025-03-19 16:19:48 +00:00
/**
* Manages all agent tools and provides a unified interface for the LLM agent
*/
export class AgentToolsManager implements IAgentToolsManager {
2025-03-19 16:19:48 +00:00
private vectorSearchTool: VectorSearchTool | null = null;
private noteNavigatorTool: NoteNavigatorTool | null = null;
private queryDecompositionTool: QueryDecompositionTool | null = null;
private contextualThinkingTool: ContextualThinkingTool | null = null;
private initialized = false;
constructor() {
// Initialize tools only when requested to avoid circular dependencies
}
async initialize(aiServiceManager: LLMServiceInterface): Promise<void> {
2025-03-19 16:19:48 +00:00
try {
if (this.initialized) {
return;
}
log.info("Initializing LLM agent tools...");
// Create tools
this.vectorSearchTool = new VectorSearchTool();
this.noteNavigatorTool = new NoteNavigatorTool();
this.queryDecompositionTool = new QueryDecompositionTool();
this.contextualThinkingTool = new ContextualThinkingTool();
2025-03-19 19:28:02 +00:00
// Set context service in the vector search tool
this.vectorSearchTool.setContextService(contextService);
2025-03-19 16:19:48 +00:00
this.initialized = true;
log.info("LLM agent tools initialized successfully");
2025-03-19 19:28:02 +00:00
} catch (error) {
log.error(`Failed to initialize agent tools: ${error}`);
throw error;
2025-03-19 16:19:48 +00:00
}
}
isInitialized(): boolean {
return this.initialized;
}
/**
* Get all available agent tools
* @returns Object containing all initialized tools
*/
getAllTools() {
if (!this.initialized) {
throw new Error("Agent tools not initialized. Call initialize() first.");
}
return {
vectorSearch: this.vectorSearchTool as IVectorSearchTool,
noteNavigator: this.noteNavigatorTool as INoteNavigatorTool,
queryDecomposition: this.queryDecompositionTool as IQueryDecompositionTool,
contextualThinking: this.contextualThinkingTool as IContextualThinkingTool
2025-03-19 16:19:48 +00:00
};
}
/**
* Get the vector search tool
*/
getVectorSearchTool(): IVectorSearchTool {
2025-03-19 16:19:48 +00:00
if (!this.initialized || !this.vectorSearchTool) {
throw new Error("Vector search tool not initialized");
}
return this.vectorSearchTool;
}
/**
* Get the note structure navigator tool
*/
getNoteNavigatorTool(): INoteNavigatorTool {
2025-03-19 16:19:48 +00:00
if (!this.initialized || !this.noteNavigatorTool) {
throw new Error("Note navigator tool not initialized");
}
return this.noteNavigatorTool;
}
/**
* Get the query decomposition tool
*/
getQueryDecompositionTool(): IQueryDecompositionTool {
2025-03-19 16:19:48 +00:00
if (!this.initialized || !this.queryDecompositionTool) {
throw new Error("Query decomposition tool not initialized");
}
return this.queryDecompositionTool;
}
/**
* Get the contextual thinking tool
*/
getContextualThinkingTool(): IContextualThinkingTool {
2025-03-19 16:19:48 +00:00
if (!this.initialized || !this.contextualThinkingTool) {
throw new Error("Contextual thinking tool not initialized");
}
return this.contextualThinkingTool;
}
}
// Export a singleton instance
const agentTools = new AgentToolsManager();
export default agentTools;
// Also export individual tool classes for direct use if needed
export {
VectorSearchTool,
NoteNavigatorTool,
QueryDecompositionTool,
ContextualThinkingTool
};