(toolCalls: ToolCallResult[])
| 127 | } |
| 128 | |
| 129 | private async runTools(toolCalls: ToolCallResult[]): Promise<Message[]> { |
| 130 | const toolPromises = toolCalls.map(async (toolCall: ToolCallResult) => { |
| 131 | const toolName = toolCall.function.name; |
| 132 | const toolParameters = JSON.parse(toolCall.function.arguments); |
| 133 | const toolFunction = this.tools[toolName]; |
| 134 | |
| 135 | if (!toolFunction) { |
| 136 | throw new Error( |
| 137 | `Tool ${toolName} not found. If this is intentional, please set runTools to false to disable tool execution by default.`, |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | const toolResponse = await toolFunction(toolParameters); |
| 142 | |
| 143 | return { |
| 144 | tool_call_id: toolCall.id, |
| 145 | role: 'tool' as MessageRole, |
| 146 | name: toolName, |
| 147 | content: JSON.stringify(toolResponse), |
| 148 | }; |
| 149 | }); |
| 150 | |
| 151 | return Promise.all(toolPromises); |
| 152 | } |
| 153 | |
| 154 | private hasNoToolCalls(message: Message): boolean { |
| 155 | return !message.tool_calls || message.tool_calls.length === 0; |
no outgoing calls
no test coverage detected