Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
reduce the use of any, part 4
perf3ct07db19abd857
diff --git a/src/services/llm/context_extractors/vector_search_tool.ts b/src/services/llm/context_extractors/vector_search_tool.tsindex 252c837573..03318e3f9e 100644--- a/src/services/llm/context_extractors/vector_search_tool.ts+++ b/src/services/llm/context_extractors/vector_search_tool.ts@@ -41,8 +41,13 @@ export interface VectorSearchOptions {summarize?: boolean;}+// Define a type for the context service+export interface IVectorContextService {+ findRelevantNotes?: (query: string, contextNoteId: string | null, options: Record<string, unknown>) => Promise<unknown[]>;+}+export class VectorSearchTool {- private contextService: any = null;+ private contextService: IVectorContextService | null = null;private maxResults: number = 5;constructor() {@@ -52,7 +57,7 @@ export class VectorSearchTool {/*** Set the context service for performing vector searches*/- setContextService(contextService: any): void {+ setContextService(contextService: IVectorContextService): void {this.contextService = contextService;log.info('Context service set in VectorSearchTool');}diff --git a/src/services/llm/tools/attribute_manager_tool.ts b/src/services/llm/tools/attribute_manager_tool.tsindex 4739626c10..5618b47edd 100644--- a/src/services/llm/tools/attribute_manager_tool.ts+++ b/src/services/llm/tools/attribute_manager_tool.ts@@ -9,6 +9,12 @@ import log from '../../log.js';import becca from '../../../becca/becca.js';import attributes from '../../attributes.js';+// Define a custom error type guard+function isError(error: unknown): error is Error {+ return error instanceof Error || (typeof error === 'object' &&+ error !== null && 'message' in error);+}+/*** Definition of the attribute manager tool*/@@ -129,9 +135,10 @@ export class AttributeManagerTool implements ToolHandler {attributeValue: value,message: `Added attribute ${attributeName}=${value || ''} to note "${note.title}"`};- } catch (error: any) {- log.error(`Error adding attribute: ${error.message || String(error)}`);- return `Error: ${error.message || String(error)}`;+ } catch (error: unknown) {+ const errorMessage = isError(error) ? error.message : String(error);+ log.error(`Error adding attribute: ${errorMessage}`);+ return `Error: ${errorMessage}`;}} else if (action === 'remove') {// Remove an attribute@@ -179,9 +186,10 @@ export class AttributeManagerTool implements ToolHandler {attributesRemoved: attributesToRemove.length,message: `Removed ${attributesToRemove.length} attribute(s) from note "${note.title}"`};- } catch (error: any) {- log.error(`Error removing attribute: ${error.message || String(error)}`);- return `Error: ${error.message || String(error)}`;+ } catch (error: unknown) {+ const errorMessage = isError(error) ? error.message : String(error);+ log.error(`Error removing attribute: ${errorMessage}`);+ return `Error: ${errorMessage}`;}} else if (action === 'update') {// Update an attribute@@ -233,16 +241,18 @@ export class AttributeManagerTool implements ToolHandler {attributesUpdated: attributesToUpdate.length,message: `Updated ${attributesToUpdate.length} attribute(s) on note "${note.title}"`};- } catch (error: any) {- log.error(`Error updating attribute: ${error.message || String(error)}`);- return `Error: ${error.message || String(error)}`;+ } catch (error: unknown) {+ const errorMessage = isError(error) ? error.message : String(error);+ log.error(`Error updating attribute: ${errorMessage}`);+ return `Error: ${errorMessage}`;}} else {return `Error: Unsupported action "${action}". Supported actions are: add, remove, update, list`;}- } catch (error: any) {- log.error(`Error executing manage_attributes tool: ${error.message || String(error)}`);- return `Error: ${error.message || String(error)}`;+ } catch (error: unknown) {+ const errorMessage = isError(error) ? error.message : String(error);+ log.error(`Error executing manage_attributes tool: ${errorMessage}`);+ return `Error: ${errorMessage}`;}}}diff --git a/src/services/llm/tools/read_note_tool.ts b/src/services/llm/tools/read_note_tool.tsindex a4de7ec3ab..d128d550a4 100644--- a/src/services/llm/tools/read_note_tool.ts+++ b/src/services/llm/tools/read_note_tool.ts@@ -8,6 +8,25 @@ import type { Tool, ToolHandler } from './tool_interfaces.js';import log from '../../log.js';import becca from '../../../becca/becca.js';+// Define type for note response+interface NoteResponse {+ noteId: string;+ title: string;+ type: string;+ content: string | Buffer;+ attributes?: Array<{+ name: string;+ value: string;+ type: string;+ }>;+}++// Error type guard+function isError(error: unknown): error is Error {+ return error instanceof Error || (typeof error === 'object' &&+ error !== null && 'message' in error);+}+/*** Definition of the read note tool*/@@ -66,7 +85,7 @@ export class ReadNoteTool implements ToolHandler {log.info(`Retrieved note content in ${duration}ms, content length: ${content?.length || 0} chars`);// Prepare the response- const response: any = {+ const response: NoteResponse = {noteId: note.noteId,title: note.title,type: note.type,@@ -77,13 +96,13 @@ export class ReadNoteTool implements ToolHandler {if (includeAttributes) {const attributes = note.getOwnedAttributes();log.info(`Including ${attributes.length} attributes in response`);-+response.attributes = attributes.map(attr => ({name: attr.name,value: attr.value,type: attr.type}));-+if (attributes.length > 0) {// Log some example attributesattributes.slice(0, 3).forEach((attr, index) => {@@ -93,9 +112,10 @@ export class ReadNoteTool implements ToolHandler {}return response;- } catch (error: any) {- log.error(`Error executing read_note tool: ${error.message || String(error)}`);- return `Error: ${error.message || String(error)}`;+ } catch (error: unknown) {+ const errorMessage = isError(error) ? error.message : String(error);+ log.error(`Error executing read_note tool: ${errorMessage}`);+ return `Error: ${errorMessage}`;}}}diff --git a/src/services/llm/tools/tool_initializer.ts b/src/services/llm/tools/tool_initializer.tsindex 1d2b7e56a1..e8ceca3eee 100644--- a/src/services/llm/tools/tool_initializer.ts+++ b/src/services/llm/tools/tool_initializer.ts@@ -19,6 +19,12 @@ import { CalendarIntegrationTool } from './calendar_integration_tool.js';import { NoteSummarizationTool } from './note_summarization_tool.js';import log from '../../log.js';+// Error type guard+function isError(error: unknown): error is Error {+ return error instanceof Error || (typeof error === 'object' &&+ error !== null && 'message' in error);+}+/*** Initialize all tools for the LLM*/@@ -50,8 +56,9 @@ export async function initializeTools(): Promise<void> {const toolCount = toolRegistry.getAllTools().length;const toolNames = toolRegistry.getAllTools().map(tool => tool.definition.function.name).join(', ');log.info(`Successfully registered ${toolCount} LLM tools: ${toolNames}`);- } catch (error: any) {- log.error(`Error initializing LLM tools: ${error.message || String(error)}`);+ } catch (error: unknown) {+ const errorMessage = isError(error) ? error.message : String(error);+ log.error(`Error initializing LLM tools: ${errorMessage}`);// Don't throw, just log the error to prevent breaking the pipeline}}