(messages: any[], tools: any[])
| 60 | // ==================== Tool Calling Helpers ==================== |
| 61 | |
| 62 | function injectToolsPrompt(messages: any[], tools: any[]): any[] { |
| 63 | if (!tools || tools.length === 0) return messages; |
| 64 | const toolsDesc = tools.map((tool: any) => { |
| 65 | const fn = tool.function || tool; |
| 66 | return `### ${fn.name} |
| 67 | Description: ${fn.description || ""} |
| 68 | Parameters: ${JSON.stringify(fn.parameters || {}, null, 2)}`; |
| 69 | }).join("\n\n"); |
| 70 | const prompt = `You are an assistant with access to tools. When you need to use a tool, you MUST output ONLY a single JSON object with NO markdown, NO explanations, and NO extra text. |
| 71 | |
| 72 | STRICT RULES: |
| 73 | 1. If a tool is needed, output EXACTLY this format (nothing else): |
| 74 | {"tool_calls":[{"name":"TOOL_NAME","arguments":{"param":"value"}}]} |
| 75 | |
| 76 | 2. Do NOT wrap the JSON in markdown code blocks (no \`\`\`json). |
| 77 | 3. Do NOT add any explanation before or after the JSON. |
| 78 | 4. If no tool is needed, respond normally with plain text. |
| 79 | |
| 80 | Available tools: |
| 81 | ${toolsDesc} |
| 82 | |
| 83 | Examples: |
| 84 | User: What is the weather in Beijing? |
| 85 | Assistant: {"tool_calls":[{"name":"get_weather","arguments":{"location":"Beijing"}}]} |
| 86 | |
| 87 | User: Hello |
| 88 | Assistant: Hello! How can I help you today?`; |
| 89 | const newMessages = [...messages]; |
| 90 | const systemIdx = newMessages.findIndex((m: any) => m.role === "system"); |
| 91 | if (systemIdx >= 0) { |
| 92 | const original = newMessages[systemIdx].content || ""; |
| 93 | newMessages[systemIdx] = { ...newMessages[systemIdx], content: original + "\n\n" + prompt }; |
| 94 | } else { |
| 95 | newMessages.unshift({ role: "system", content: prompt }); |
| 96 | } |
| 97 | return newMessages; |
| 98 | } |
| 99 | |
| 100 | function parseToolCalls(content: string): { tool_calls: any[] | null; text: string } { |
| 101 | if (!content || !content.trim()) return { tool_calls: null, text: content }; |
no outgoing calls
no test coverage detected