MCPcopy Create free account
hub / github.com/Hello-Application-XH/HelloGML / injectToolsPrompt

Function injectToolsPrompt

src/chat.ts:62–98  ·  view source on GitHub ↗
(messages: any[], tools: any[])

Source from the content-addressed store, hash-verified

60// ==================== Tool Calling Helpers ====================
61
62function 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}
67Description: ${fn.description || ""}
68Parameters: ${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
72STRICT RULES:
731. If a tool is needed, output EXACTLY this format (nothing else):
74{"tool_calls":[{"name":"TOOL_NAME","arguments":{"param":"value"}}]}
75
762. Do NOT wrap the JSON in markdown code blocks (no \`\`\`json).
773. Do NOT add any explanation before or after the JSON.
784. If no tool is needed, respond normally with plain text.
79
80Available tools:
81${toolsDesc}
82
83Examples:
84User: What is the weather in Beijing?
85Assistant: {"tool_calls":[{"name":"get_weather","arguments":{"location":"Beijing"}}]}
86
87User: Hello
88Assistant: 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
100function parseToolCalls(content: string): { tool_calls: any[] | null; text: string } {
101 if (!content || !content.trim()) return { tool_calls: null, text: content };

Callers 2

createCompletionFunction · 0.85
createCompletionStreamFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected