UNPKG

@gork-labs/secondbrain-mcp

Version:

Second Brain MCP Server - Agent team orchestration with dynamic tool discovery

115 lines (114 loc) 3.93 kB
/** * OpenAI Native Function Calling Implementation * * This implementation uses OpenAI's built-in function calling capabilities to replace * the regex-based tool parsing system with reliable, structured function execution. * * Key Ben sessionLog.info('Executing sub-agent with OpenRouter native function calling', { chatmode: chatmodeName, availableTools: this.mcpTools.map(t => t.name), functionToolsCount: functionTools.length, maxIterations: this.maxIterations, model: config.model }); * 1. Structured function definitions with automatic validation * 2. Built-in JSON parsing and type safety * 3. Multiple simultaneous function calls support * 4. Automatic error handling and retry logic * 5. No regex parsing - uses OpenAI's proven approach * * Created: 2025-07-25T17:04:49+02:00 * Author: Staff Software Engineer - Gorka */ import { SubAgentResponse } from '../utils/types.js'; export interface MCPTool { name: string; description: string; inputSchema: any; serverId: string; serverName: string; safe: boolean; } /** * Enhanced sub-agent executor using OpenAI's native function calling * Replaces regex-based tool parsing with structured function execution */ export declare class OpenAIFunctionCallingExecutor { private openai; private mcpTools; private maxIterations; private toolNameMapping; constructor(apiKey: string, mcpTools: MCPTool[], maxIterations?: number); /** * Create mapping from VS Code tool names to MCP tool names * This enables backward compatibility with existing agent instructions */ private createToolNameMapping; /** * Map tool names and parameters from VS Code format to MCP format */ private mapToolCall; private createOpenAIFunctionTools; /** * Convert MCP input schema to OpenAI function parameters schema */ private convertMCPSchemaToOpenAI; /** * Execute sub-agent with native OpenAI function calling */ executeSubAgent(instructions: string, chatmodeName: string, sessionId: string, toolExecutor: (toolName: string, args: any) => Promise<{ success: boolean; content: any; error?: string; serverId?: string; }>): Promise<SubAgentResponse>; /** * Manual function call handling implementation * This provides full control over the execution flow */ executeSubAgentManual(instructions: string, chatmodeName: string, sessionId: string, toolExecutor: (toolName: string, args: any) => Promise<{ success: boolean; content: any; error?: string; serverId?: string; }>): Promise<{ result: SubAgentResponse; toolCallCount: number; }>; /** * Parse sub-agent response from final content using centralized parser */ private parseSubAgentResponse; } /** * Configuration for OpenAI function calling */ export declare class FunctionCallingConfig { /** * Get OpenAI API key from environment */ static getOpenAIApiKey(): string; /** * Check if chatmode should use OpenAI function calling * Optional chatmode filtering via environment variable */ static shouldUseOpenAIFunctionCalling(chatmode: string): boolean; } /** * Performance monitoring for OpenAI function calling */ export declare class FunctionCallingMetrics { private static metrics; static recordExecution(chatmode: string, duration: number, success: boolean, toolCallCount: number, errorType?: string): void; static getMetrics(chatmode?: string): any; static getPerformanceSummary(): { avgDuration: number; successRate: number; totalExecutions: number; last24Hours: { avgDuration: number; successRate: number; executions: number; }; }; static clearMetrics(chatmode?: string): void; }