feat(llm): reduce the use of "any" in the tool calling stage

This commit is contained in:
perf3ct 2025-05-29 21:07:12 +00:00
parent 511d2c5e96
commit ba59d6b3c1
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232

View File

@ -67,8 +67,9 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
// Tools are already initialized in the AIServiceManager constructor // Tools are already initialized in the AIServiceManager constructor
// No need to initialize them again // No need to initialize them again
log.info(`After recovery initialization: ${toolRegistry.getAllTools().length} tools available`); log.info(`After recovery initialization: ${toolRegistry.getAllTools().length} tools available`);
} catch (error: any) { } catch (error: unknown) {
log.error(`Failed to initialize tools in recovery step: ${error.message}`); const errorMessage = error instanceof Error ? error.message : String(error);
log.error(`Failed to initialize tools in recovery step: ${errorMessage}`);
} }
} }
@ -263,9 +264,10 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
callbackResult.catch((e: Error) => log.error(`Error sending tool execution complete event: ${e.message}`)); callbackResult.catch((e: Error) => log.error(`Error sending tool execution complete event: ${e.message}`));
} }
} }
} catch (execError: any) { } catch (execError: unknown) {
const executionTime = Date.now() - executionStart; const executionTime = Date.now() - executionStart;
log.error(`================ TOOL EXECUTION FAILED in ${executionTime}ms: ${execError.message} ================`); const errorMessage = execError instanceof Error ? execError.message : String(execError);
log.error(`================ TOOL EXECUTION FAILED in ${executionTime}ms: ${errorMessage} ================`);
// Record this failed tool execution if there's a sessionId available // Record this failed tool execution if there's a sessionId available
if (input.options?.sessionId) { if (input.options?.sessionId) {
@ -276,7 +278,7 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
toolCall.id || `tool-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`, toolCall.id || `tool-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`,
args, args,
"", // No result for failed execution "", // No result for failed execution
execError.message || String(execError) errorMessage
); );
} catch (storageError) { } catch (storageError) {
log.error(`Failed to record tool execution error in chat storage: ${storageError}`); log.error(`Failed to record tool execution error in chat storage: ${storageError}`);
@ -291,7 +293,7 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
name: toolCall.function.name, name: toolCall.function.name,
arguments: {} as Record<string, unknown> arguments: {} as Record<string, unknown>
}, },
error: execError.message || String(execError), error: errorMessage,
type: 'error' as const type: 'error' as const
}; };
@ -322,19 +324,24 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
name: toolCall.function.name, name: toolCall.function.name,
result result
}; };
} catch (error: any) { } catch (error: unknown) {
log.error(`Error executing tool ${toolCall.function.name}: ${error.message || String(error)}`); const errorMessage = error instanceof Error ? error.message : String(error);
log.error(`Error executing tool ${toolCall.function.name}: ${errorMessage}`);
// Emit tool error event if not already handled in the try/catch above // Emit tool error event if not already handled in the try/catch above
// and if streaming is enabled // and if streaming is enabled
if (streamCallback && error.name !== "ExecutionError") { // Need to check if error is an object with a name property of type string
const isExecutionError = typeof error === 'object' && error !== null &&
'name' in error && (error as { name: unknown }).name === "ExecutionError";
if (streamCallback && !isExecutionError) {
const toolExecutionData = { const toolExecutionData = {
action: 'error', action: 'error',
tool: { tool: {
name: toolCall.function.name, name: toolCall.function.name,
arguments: {} as Record<string, unknown> arguments: {} as Record<string, unknown>
}, },
error: error.message || String(error), error: errorMessage,
type: 'error' as const type: 'error' as const
}; };
@ -353,7 +360,7 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
return { return {
toolCallId: toolCall.id, toolCallId: toolCall.id,
name: toolCall.function.name, name: toolCall.function.name,
result: `Error: ${error.message || String(error)}` result: `Error: ${errorMessage}`
}; };
} }
})); }));
@ -498,8 +505,9 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
// Unknown dependency type // Unknown dependency type
log.error(`Unknown dependency type: ${dependencyType}`); log.error(`Unknown dependency type: ${dependencyType}`);
return null; return null;
} catch (error: any) { } catch (error: unknown) {
log.error(`Error getting or creating dependency '${dependencyType}': ${error.message}`); const errorMessage = error instanceof Error ? error.message : String(error);
log.error(`Error getting or creating dependency '${dependencyType}': ${errorMessage}`);
return null; return null;
} }
} }
@ -549,8 +557,9 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
return false; return false;
} }
log.info('Successfully initialized vectorSearchTool'); log.info('Successfully initialized vectorSearchTool');
} catch (initError: any) { } catch (initError: unknown) {
log.error(`Failed to initialize agent tools: ${initError.message}`); const errorMessage = initError instanceof Error ? initError.message : String(initError);
log.error(`Failed to initialize agent tools: ${errorMessage}`);
return false; return false;
} }
} }
@ -559,8 +568,9 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
log.error(`Tool '${toolName}' dependency vectorSearchTool is missing searchNotes method`); log.error(`Tool '${toolName}' dependency vectorSearchTool is missing searchNotes method`);
return false; return false;
} }
} catch (error: any) { } catch (error: unknown) {
log.error(`Error validating dependencies for tool '${toolName}': ${error.message}`); const errorMessage = error instanceof Error ? error.message : String(error);
log.error(`Error validating dependencies for tool '${toolName}': ${errorMessage}`);
return false; return false;
} }
} }
@ -568,8 +578,9 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
// Add additional tool-specific validations here // Add additional tool-specific validations here
return true; return true;
} catch (error: any) { } catch (error: unknown) {
log.error(`Error validating tool before execution: ${error.message}`); const errorMessage = error instanceof Error ? error.message : String(error);
log.error(`Error validating tool before execution: ${errorMessage}`);
return false; return false;
} }
} }
@ -666,8 +677,9 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
} else { } else {
log.error(`Vector search tool not available after initialization`); log.error(`Vector search tool not available after initialization`);
} }
} catch (error: any) { } catch (error: unknown) {
log.error(`Failed to preload vector search tool: ${error.message}`); const errorMessage = error instanceof Error ? error.message : String(error);
log.error(`Failed to preload vector search tool: ${errorMessage}`);
} }
} }
} }