1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| async function processTurn(userMessage: string): Promise<string> { const messages = [{ role: 'user', content: userMessage }]; while (true) { const response = await llm.chatWithTools(messages, tools); if (!response.toolCalls) { return response.content; } for (const toolCall of response.toolCalls) { const tool = tools.find(t => t.name === toolCall.name); const result = await tool.execute(JSON.parse(toolCall.arguments)); messages.push({ role: 'assistant', content: '', tool_calls: [toolCall] }); messages.push({ role: 'tool', tool_call_id: toolCall.id, content: JSON.stringify(result) }); } } }
|