-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.js
More file actions
335 lines (277 loc) · 9.85 KB
/
tools.js
File metadata and controls
335 lines (277 loc) · 9.85 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const TOOL_INJECTION_SENTINEL = '<!-- proxy-tools-injected -->';
export function injectToolsIntoSystem(messages, tools) {
if (!tools || tools.length === 0) {
return normalizeMessages(messages);
}
const toolDescriptions = tools
.map(t => {
let desc = `- ${t.function.name}: ${t.function.description}`;
if (t.function.parameters) {
const params = t.function.parameters;
const required = params.required || [];
if (params.properties) {
const paramDocs = Object.entries(params.properties).map(([name, schema]) => {
const req = required.includes(name) ? ' (required)' : ' (optional)';
return ` - ${name}${req}: ${schema.description || schema.type || 'string'}`;
});
desc += '\n Parameters:\n' + paramDocs.join('\n');
}
}
return desc;
})
.join('\n');
const toolInstruction = `You have access to the following tools:
${toolDescriptions}
IMPORTANT: Only use the tools listed above. Do not invent or use tools that are not in this list.
When you need to use a tool, format your response like this:
TOOL_CALL: <tool_name>
ARGUMENTS: <json_arguments>
For example:
TOOL_CALL: bash
ARGUMENTS: {"command": "ls -la", "description": "List files in current directory"}
You MUST include ALL required parameters in the ARGUMENTS JSON. Do not omit any required parameter.
Only make one tool call at a time. Wait for the result before making another tool call.
When you receive a tool result, analyze it and provide a helpful response to the user. If you need more information, make another tool call. If you have enough information, respond directly to the user's query.`;
const result = normalizeMessages(messages);
const systemIndex = result.findIndex(m => m.role === 'system');
if (systemIndex !== -1) {
if (result[systemIndex].content.includes(TOOL_INJECTION_SENTINEL)) {
return result;
}
result[systemIndex] = {
role: 'system',
content: `${result[systemIndex].content}\n\n${toolInstruction}\n${TOOL_INJECTION_SENTINEL}`
};
} else {
result.unshift({ role: 'system', content: `${toolInstruction}\n${TOOL_INJECTION_SENTINEL}` });
}
return result;
}
function normalizeMessages(messages) {
return messages.map(msg => {
const normalized = { role: msg.role };
if (msg.role === 'assistant' && msg.tool_calls) {
normalized.content = msg.tool_calls.map(tc =>
`TOOL_CALL: ${tc.function.name}\nARGUMENTS: ${tc.function.arguments}`
).join('\n\n');
} else if (typeof msg.content === 'string') {
normalized.content = msg.content;
} else if (Array.isArray(msg.content)) {
const textParts = msg.content.filter(c => c.type === 'text' && !c.text.includes('<system-reminder>')).map(c => c.text);
normalized.content = textParts.join('\n');
} else if (msg.content) {
normalized.content = msg.content;
}
return normalized;
});
}
export function parseToolCall(responseText) {
if (!responseText || typeof responseText !== 'string') {
return null;
}
const parsers = [
{ name: 'Minimax XML', parser: parseMinimaxXML },
{ name: 'Claude XML', parser: parseClaudeXML },
{ name: 'OpenAI Native', parser: parseOpenAIToolCalls },
{ name: 'Text Format', parser: parseTextFormat },
];
const triedParsers = [];
for (const { name, parser } of parsers) {
try {
const toolCalls = parser(responseText);
if (toolCalls && toolCalls.length > 0) {
console.log(`[Tool Parsing] Successfully parsed tool calls using ${name}`);
return toolCalls;
}
triedParsers.push(name);
} catch (error) {
console.warn(`[Tool Parsing] ${name} parser failed: ${error.message}`);
triedParsers.push(name + ' (failed)');
}
}
console.warn(`[Tool Parsing] No tool calls detected. Tried parsers: ${triedParsers.join(', ')}`);
console.debug(`[Tool Parsing] Response snippet: ${responseText.substring(0, 200)}...`);
return null;
}
function parseMinimaxXML(responseText) {
const minimaxMatch = responseText.match(/<minimax:tool_call>(.*?)<\/minimax:tool_call>/s);
if (!minimaxMatch) return [];
const toolCalls = [];
const invokeMatches = minimaxMatch[1].matchAll(/<invoke\s+([^>]+)>(.*?)<\/invoke>/gs);
for (const invokeMatch of invokeMatches) {
const attrs = invokeMatch[1];
const innerContent = invokeMatch[2];
let toolName = '';
const args = {};
const attrPairs = attrs.matchAll(/(\w+):\s*"([^"]*)"/g);
for (const attrPair of attrPairs) {
const [, attrName, attrValue] = attrPair;
if (!toolName) {
toolName = extractToolName(attrName);
}
const mappedName = mapToolAttribute(attrName);
if (mappedName) {
args[mappedName] = attrValue;
}
}
const paramMatches = innerContent.matchAll(/<parameter\s+name="([^"]+)">([^<]*)<\/parameter>/g);
for (const paramMatch of paramMatches) {
args[paramMatch[1]] = paramMatch[2].trim();
}
if (toolName && Object.keys(args).length > 0) {
toolCalls.push(createToolCallObject(toolName, args, toolCalls.length));
}
}
return toolCalls;
}
function parseClaudeXML(responseText) {
const toolCalls = [];
const functionCallMatches = responseText.matchAll(/<invoke\s+name="([^"]+)">\s*<parameter_list>\s*(.*?)\s*<\/parameter_list>\s*<\/invoke>/gs);
for (const match of functionCallMatches) {
const [, toolName, parametersXml] = match;
const args = {};
const paramMatches = parametersXml.matchAll(/<parameter\s+name="([^"]+)">(.*?)<\/parameter>/gs);
for (const paramMatch of paramMatches) {
const [, paramName, paramValue] = paramMatch;
args[paramName] = paramValue.trim();
}
if (toolName && Object.keys(args).length > 0) {
toolCalls.push(createToolCallObject(toolName, args, toolCalls.length));
}
}
return toolCalls;
}
function parseOpenAIToolCalls(responseText) {
const toolCalls = [];
try {
const toolCallMatch = responseText.match(/"tool_calls"\s*:\s*\[(.*?)\]/s);
if (!toolCallMatch) return [];
const toolCallArray = `[${toolCallMatch[1]}]`;
const parsedToolCalls = JSON.parse(toolCallArray);
for (const toolCall of parsedToolCalls) {
if (toolCall.function && toolCall.function.name) {
toolCalls.push({
id: toolCall.id || createToolCallId(toolCalls.length),
type: toolCall.type || 'function',
function: {
name: toolCall.function.name,
arguments: toolCall.function.arguments || JSON.stringify(toolCall.function.parameters || {}),
},
});
}
}
} catch (error) {
return [];
}
return toolCalls;
}
function parseTextFormat(responseText) {
const toolCalls = [];
let currentIndex = 0;
const patterns = [
/TOOL_CALL:\s*(\w+).*?ARGUMENTS:\s*\{/is,
/TOOL_CALL:\s*(\w+)ARGUMENTS:\s*\{/is,
];
while (currentIndex < responseText.length) {
let toolCallMatch = null;
for (const pattern of patterns) {
toolCallMatch = responseText.substring(currentIndex).match(pattern);
if (toolCallMatch) break;
}
if (!toolCallMatch) break;
const toolName = toolCallMatch[1];
const fullMatchText = toolCallMatch[0];
const matchStartIndex = currentIndex + toolCallMatch.index;
const argsStartIndex = matchStartIndex + fullMatchText.lastIndexOf('{');
let braceCount = 0;
let argsEndIndex = argsStartIndex;
let foundClosingBrace = false;
for (let i = argsStartIndex; i < responseText.length; i++) {
const char = responseText[i];
if (char === '{') {
braceCount++;
} else if (char === '}') {
braceCount--;
if (braceCount === 0) {
argsEndIndex = i + 1;
foundClosingBrace = true;
break;
}
}
}
if (!foundClosingBrace) {
console.warn('[Tool Parsing] Incomplete JSON for tool call:', toolName);
break;
}
const argsString = responseText.substring(argsStartIndex, argsEndIndex);
try {
const args = JSON.parse(argsString);
toolCalls.push(createToolCallObject(toolName, args, toolCalls.length));
currentIndex = argsEndIndex;
} catch (error) {
console.error('Failed to parse tool call arguments:', error);
console.debug('[Tool Parsing] Failed to parse:', argsString.substring(0, 200));
break;
}
}
return toolCalls;
}
function mapToolAttribute(attrName) {
const mappings = {
'readfilePath': 'filePath',
'bashcommand': 'command',
};
return mappings[attrName] || attrName;
}
function extractToolName(attrName) {
if (attrName === 'bash') return 'bash';
if (attrName === 'read') return 'read';
if (attrName === 'readfilePath') return 'read';
if (attrName === 'bashcommand') return 'bash';
return attrName;
}
function createToolCallObject(toolName, args, index) {
return {
id: createToolCallId(index),
type: 'function',
function: {
name: toolName,
arguments: JSON.stringify(args),
},
};
}
function createToolCallId(index) {
return `call_${Date.now()}_${Math.random().toString(36).substring(2, 11)}_${index}`;
}
export function formatToolCallResponse(toolCalls) {
if (!toolCalls || toolCalls.length === 0) {
return null;
}
return {
id: `chatcmpl-${Date.now()}`,
object: 'chat.completion',
created: Math.floor(Date.now() / 1000),
model: 'doai-proxy',
choices: [{
index: 0,
message: {
role: 'assistant',
tool_calls: toolCalls,
content: null,
},
finish_reason: 'tool_calls',
}],
usage: {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0,
},
};
}
export function formatToolResultMessage(toolCallId, result) {
return {
role: 'tool',
tool_call_id: toolCallId,
content: typeof result === 'string' ? result : JSON.stringify(result),
};
}