@ww_nero/mini-cli 1.0.82 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ww_nero/mini-cli",
3
- "version": "1.0.82",
3
+ "version": "1.0.85",
4
4
  "description": "极简的 AI 命令行助手",
5
5
  "bin": {
6
6
  "mini": "bin/mini.js"
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) {
package/src/request.js CHANGED
@@ -159,20 +159,27 @@ const processStreamResponse = (response, options = {}) => {
159
159
  }
160
160
 
161
161
  if (includeToolCalls && Array.isArray(delta.tool_calls)) {
162
- delta.tool_calls.forEach((call, index) => {
163
- if (!toolCalls[index]) {
164
- toolCalls[index] = {
162
+ delta.tool_calls.forEach((call) => {
163
+ const idx = typeof call.index === 'number' ? call.index : toolCalls.length;
164
+ if (!toolCalls[idx]) {
165
+ toolCalls[idx] = {
165
166
  id: call.id || '',
166
167
  type: call.type || 'function',
167
168
  function: {
168
- name: (call.function && call.function.name) || '',
169
+ name: '',
169
170
  arguments: ''
170
171
  }
171
172
  };
172
173
  }
173
174
 
175
+ if (call.id) {
176
+ toolCalls[idx].id = call.id;
177
+ }
178
+ if (call.function && call.function.name) {
179
+ toolCalls[idx].function.name += call.function.name;
180
+ }
174
181
  if (call.function && typeof call.function.arguments === 'string') {
175
- toolCalls[index].function.arguments += call.function.arguments;
182
+ toolCalls[idx].function.arguments += call.function.arguments;
176
183
  }
177
184
  });
178
185
  }