@ww_nero/mini-cli 1.0.83 → 1.0.85
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/llm.js +32 -1
package/package.json
CHANGED
package/src/llm.js
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
const { makeRequestWithRetry, processStreamResponse } = require('./request');
|
|
2
2
|
|
|
3
|
+
const ensureValidArguments = (args) => {
|
|
4
|
+
if (typeof args !== 'string') return '{}';
|
|
5
|
+
try {
|
|
6
|
+
JSON.parse(args);
|
|
7
|
+
return args;
|
|
8
|
+
} catch (_) {
|
|
9
|
+
// 尝试截取第一个完整 JSON 对象
|
|
10
|
+
try {
|
|
11
|
+
const parts = args.split('}{');
|
|
12
|
+
if (parts.length > 1) {
|
|
13
|
+
const fixed = parts[0] + '}';
|
|
14
|
+
JSON.parse(fixed);
|
|
15
|
+
return fixed;
|
|
16
|
+
}
|
|
17
|
+
} catch (__) {
|
|
18
|
+
// ignore
|
|
19
|
+
}
|
|
20
|
+
return '{}';
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const sanitizeToolCalls = (toolCalls) => {
|
|
25
|
+
return toolCalls.map((call) => ({
|
|
26
|
+
...call,
|
|
27
|
+
function: {
|
|
28
|
+
...call.function,
|
|
29
|
+
arguments: ensureValidArguments(call.function?.arguments)
|
|
30
|
+
}
|
|
31
|
+
}));
|
|
32
|
+
};
|
|
33
|
+
|
|
3
34
|
const sanitizeMessages = (messages = []) => {
|
|
4
35
|
return messages.map((message = {}) => {
|
|
5
36
|
const role = message.role;
|
|
@@ -13,7 +44,7 @@ const sanitizeMessages = (messages = []) => {
|
|
|
13
44
|
}
|
|
14
45
|
|
|
15
46
|
if (role === 'assistant' && Array.isArray(message.tool_calls) && message.tool_calls.length > 0) {
|
|
16
|
-
sanitized.tool_calls = message.tool_calls;
|
|
47
|
+
sanitized.tool_calls = sanitizeToolCalls(message.tool_calls);
|
|
17
48
|
}
|
|
18
49
|
|
|
19
50
|
if (message.reasoning_content) {
|