mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 19:12:27 +08:00
reduce the use of any, part 4
This commit is contained in:
parent
80ea2c3eef
commit
07db19abd8
@ -41,8 +41,13 @@ export interface VectorSearchOptions {
|
|||||||
summarize?: boolean;
|
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 {
|
export class VectorSearchTool {
|
||||||
private contextService: any = null;
|
private contextService: IVectorContextService | null = null;
|
||||||
private maxResults: number = 5;
|
private maxResults: number = 5;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -52,7 +57,7 @@ export class VectorSearchTool {
|
|||||||
/**
|
/**
|
||||||
* Set the context service for performing vector searches
|
* Set the context service for performing vector searches
|
||||||
*/
|
*/
|
||||||
setContextService(contextService: any): void {
|
setContextService(contextService: IVectorContextService): void {
|
||||||
this.contextService = contextService;
|
this.contextService = contextService;
|
||||||
log.info('Context service set in VectorSearchTool');
|
log.info('Context service set in VectorSearchTool');
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,12 @@ import log from '../../log.js';
|
|||||||
import becca from '../../../becca/becca.js';
|
import becca from '../../../becca/becca.js';
|
||||||
import attributes from '../../attributes.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
|
* Definition of the attribute manager tool
|
||||||
*/
|
*/
|
||||||
@ -129,9 +135,10 @@ export class AttributeManagerTool implements ToolHandler {
|
|||||||
attributeValue: value,
|
attributeValue: value,
|
||||||
message: `Added attribute ${attributeName}=${value || ''} to note "${note.title}"`
|
message: `Added attribute ${attributeName}=${value || ''} to note "${note.title}"`
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
log.error(`Error adding attribute: ${error.message || String(error)}`);
|
const errorMessage = isError(error) ? error.message : String(error);
|
||||||
return `Error: ${error.message || String(error)}`;
|
log.error(`Error adding attribute: ${errorMessage}`);
|
||||||
|
return `Error: ${errorMessage}`;
|
||||||
}
|
}
|
||||||
} else if (action === 'remove') {
|
} else if (action === 'remove') {
|
||||||
// Remove an attribute
|
// Remove an attribute
|
||||||
@ -179,9 +186,10 @@ export class AttributeManagerTool implements ToolHandler {
|
|||||||
attributesRemoved: attributesToRemove.length,
|
attributesRemoved: attributesToRemove.length,
|
||||||
message: `Removed ${attributesToRemove.length} attribute(s) from note "${note.title}"`
|
message: `Removed ${attributesToRemove.length} attribute(s) from note "${note.title}"`
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
log.error(`Error removing attribute: ${error.message || String(error)}`);
|
const errorMessage = isError(error) ? error.message : String(error);
|
||||||
return `Error: ${error.message || String(error)}`;
|
log.error(`Error removing attribute: ${errorMessage}`);
|
||||||
|
return `Error: ${errorMessage}`;
|
||||||
}
|
}
|
||||||
} else if (action === 'update') {
|
} else if (action === 'update') {
|
||||||
// Update an attribute
|
// Update an attribute
|
||||||
@ -233,16 +241,18 @@ export class AttributeManagerTool implements ToolHandler {
|
|||||||
attributesUpdated: attributesToUpdate.length,
|
attributesUpdated: attributesToUpdate.length,
|
||||||
message: `Updated ${attributesToUpdate.length} attribute(s) on note "${note.title}"`
|
message: `Updated ${attributesToUpdate.length} attribute(s) on note "${note.title}"`
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
log.error(`Error updating attribute: ${error.message || String(error)}`);
|
const errorMessage = isError(error) ? error.message : String(error);
|
||||||
return `Error: ${error.message || String(error)}`;
|
log.error(`Error updating attribute: ${errorMessage}`);
|
||||||
|
return `Error: ${errorMessage}`;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return `Error: Unsupported action "${action}". Supported actions are: add, remove, update, list`;
|
return `Error: Unsupported action "${action}". Supported actions are: add, remove, update, list`;
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
log.error(`Error executing manage_attributes tool: ${error.message || String(error)}`);
|
const errorMessage = isError(error) ? error.message : String(error);
|
||||||
return `Error: ${error.message || String(error)}`;
|
log.error(`Error executing manage_attributes tool: ${errorMessage}`);
|
||||||
|
return `Error: ${errorMessage}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,25 @@ import type { Tool, ToolHandler } from './tool_interfaces.js';
|
|||||||
import log from '../../log.js';
|
import log from '../../log.js';
|
||||||
import becca from '../../../becca/becca.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
|
* 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`);
|
log.info(`Retrieved note content in ${duration}ms, content length: ${content?.length || 0} chars`);
|
||||||
|
|
||||||
// Prepare the response
|
// Prepare the response
|
||||||
const response: any = {
|
const response: NoteResponse = {
|
||||||
noteId: note.noteId,
|
noteId: note.noteId,
|
||||||
title: note.title,
|
title: note.title,
|
||||||
type: note.type,
|
type: note.type,
|
||||||
@ -77,13 +96,13 @@ export class ReadNoteTool implements ToolHandler {
|
|||||||
if (includeAttributes) {
|
if (includeAttributes) {
|
||||||
const attributes = note.getOwnedAttributes();
|
const attributes = note.getOwnedAttributes();
|
||||||
log.info(`Including ${attributes.length} attributes in response`);
|
log.info(`Including ${attributes.length} attributes in response`);
|
||||||
|
|
||||||
response.attributes = attributes.map(attr => ({
|
response.attributes = attributes.map(attr => ({
|
||||||
name: attr.name,
|
name: attr.name,
|
||||||
value: attr.value,
|
value: attr.value,
|
||||||
type: attr.type
|
type: attr.type
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if (attributes.length > 0) {
|
if (attributes.length > 0) {
|
||||||
// Log some example attributes
|
// Log some example attributes
|
||||||
attributes.slice(0, 3).forEach((attr, index) => {
|
attributes.slice(0, 3).forEach((attr, index) => {
|
||||||
@ -93,9 +112,10 @@ export class ReadNoteTool implements ToolHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
log.error(`Error executing read_note tool: ${error.message || String(error)}`);
|
const errorMessage = isError(error) ? error.message : String(error);
|
||||||
return `Error: ${error.message || String(error)}`;
|
log.error(`Error executing read_note tool: ${errorMessage}`);
|
||||||
|
return `Error: ${errorMessage}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,12 @@ import { CalendarIntegrationTool } from './calendar_integration_tool.js';
|
|||||||
import { NoteSummarizationTool } from './note_summarization_tool.js';
|
import { NoteSummarizationTool } from './note_summarization_tool.js';
|
||||||
import log from '../../log.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
|
* Initialize all tools for the LLM
|
||||||
*/
|
*/
|
||||||
@ -50,8 +56,9 @@ export async function initializeTools(): Promise<void> {
|
|||||||
const toolCount = toolRegistry.getAllTools().length;
|
const toolCount = toolRegistry.getAllTools().length;
|
||||||
const toolNames = toolRegistry.getAllTools().map(tool => tool.definition.function.name).join(', ');
|
const toolNames = toolRegistry.getAllTools().map(tool => tool.definition.function.name).join(', ');
|
||||||
log.info(`Successfully registered ${toolCount} LLM tools: ${toolNames}`);
|
log.info(`Successfully registered ${toolCount} LLM tools: ${toolNames}`);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
log.error(`Error initializing LLM tools: ${error.message || String(error)}`);
|
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
|
// Don't throw, just log the error to prevent breaking the pipeline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user