mslxdff 0.1.103 → 0.1.104

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": "mslxdff",
3
- "version": "0.1.103",
3
+ "version": "0.1.104",
4
4
  "description": "测试项目,请勿使用。",
5
5
  "type": "module",
6
6
  "bin": {
@@ -12,9 +12,17 @@ export function chatToResponsesBody(chatBody) {
12
12
  const nonSystem = msgs.filter((m) => m.role !== "system");
13
13
  const inputParts = nonSystem.map((m) => {
14
14
  const c = m.content;
15
- if (typeof c === "string") return `${m.role}: ${c}`;
16
- if (Array.isArray(c)) return `${m.role}: ${c.map((x) => x.text || "").join("")}`;
17
- return `${m.role}: ${String(c || "")}`;
15
+ let base;
16
+ if (typeof c === "string") base = `${m.role}: ${c}`;
17
+ else if (Array.isArray(c)) base = `${m.role}: ${c.map((x) => x.text || x.content || "").join("")}`;
18
+ else base = `${m.role}: ${String(c || "")}`;
19
+ // 保留 tool_calls / tool 结果,避免多轮丢失
20
+ if (Array.isArray(m.tool_calls) && m.tool_calls.length) {
21
+ const tcStr = m.tool_calls.map((tc) => `${tc.function?.name || "tool"}(${tc.function?.arguments || ""})`).join("; ");
22
+ base += ` [tool_calls: ${tcStr}]`;
23
+ }
24
+ if (m.role === "tool" && m.tool_call_id) base += ` (call_id=${m.tool_call_id})`;
25
+ return base;
18
26
  });
19
27
  const input = inputParts.join("\n\n") || "hi";
20
28
  const out = { model: chatBody.model, input, stream: false };
@@ -25,25 +33,31 @@ export function chatToResponsesBody(chatBody) {
25
33
  if (!t || typeof t !== "object") return null;
26
34
  if (t.type === "function" && t.function && typeof t.function === "object") {
27
35
  const fn = t.function;
36
+ // 去掉 responses 不支持的 strict 等字段,parameters 原样透传
28
37
  const nt = { type: "function", name: fn.name, description: fn.description || undefined, parameters: fn.parameters || undefined };
29
38
  // 清理 undefined
30
39
  Object.keys(nt).forEach((k) => nt[k] === undefined && delete nt[k]);
31
40
  return nt.name ? nt : null;
32
41
  }
33
- // 已是平铺形态或未知形态,透传但确保 name 存在
34
- if (t.name) return t;
42
+ // 已是平铺形态或未知形态,透传但确保 name 存在,清理 strict
43
+ if (t.name) {
44
+ const { strict, ...rest } = t;
45
+ return rest;
46
+ }
35
47
  return null;
36
48
  }).filter(Boolean);
37
49
  if (mapped.length) out.tools = mapped;
38
50
  }
39
51
  if (chatBody.tool_choice) {
40
52
  const tc = chatBody.tool_choice;
41
- // chat: "auto" | {type:"auto"} | {type:"function", function:{name}} -> responses: "auto" | {type:"function", name}
42
- if (typeof tc === "string") out.tool_choice = tc;
43
- else if (tc && typeof tc === "object") {
44
- if (tc.type === "function" && tc.function?.name) out.tool_choice = { type: "function", name: tc.function.name };
45
- else if (tc.type) out.tool_choice = tc;
46
- else out.tool_choice = tc;
53
+ // responses 仅支持 "auto"(实测 required/named 400),一律归一为 auto
54
+ if (typeof tc === "string") {
55
+ out.tool_choice = tc === "auto" ? "auto" : "auto";
56
+ } else if (tc && typeof tc === "object") {
57
+ if (tc.type === "auto" || tc.type === "required") out.tool_choice = "auto";
58
+ else if (tc.type === "function") out.tool_choice = "auto";
59
+ else if (tc.type) out.tool_choice = "auto";
60
+ else out.tool_choice = "auto";
47
61
  }
48
62
  }
49
63
  if (chatBody.temperature != null) out.temperature = chatBody.temperature;
@@ -53,15 +67,22 @@ export function chatToResponsesBody(chatBody) {
53
67
 
54
68
  export function responsesToChatJson(respJson) {
55
69
  let text = "";
70
+ const toolCalls = [];
56
71
  for (const item of respJson.output || []) {
57
72
  if (item.type === "message" && item.role === "assistant") {
58
73
  for (const c of item.content || []) {
59
74
  if (c.type === "output_text") text += c.text || "";
60
75
  else if (c.type === "text") text += c.text || "";
61
76
  }
77
+ } else if (item.type === "function_call") {
78
+ toolCalls.push({
79
+ id: item.call_id || item.id || `call_${toolCalls.length}`,
80
+ type: "function",
81
+ function: { name: item.name || "", arguments: typeof item.arguments === "string" ? item.arguments : JSON.stringify(item.arguments || "") },
82
+ });
62
83
  }
63
84
  }
64
- if (!text) {
85
+ if (!text && toolCalls.length === 0) {
65
86
  for (const item of respJson.output || []) {
66
87
  if (item.type === "message") {
67
88
  const t = item.content?.[0]?.text;
@@ -69,12 +90,18 @@ export function responsesToChatJson(respJson) {
69
90
  }
70
91
  }
71
92
  }
93
+ const message = { role: "assistant", content: text };
94
+ if (toolCalls.length) {
95
+ message.tool_calls = toolCalls;
96
+ // 有 tool_calls 时 content 可为 "",finish_reason 应为 tool_calls
97
+ }
98
+ const finish = toolCalls.length ? "tool_calls" : (respJson.status === "completed" ? "stop" : "length");
72
99
  const chatJson = {
73
100
  id: respJson.id || `resp_${Date.now()}`,
74
101
  object: "chat.completion",
75
102
  created: Math.floor((respJson.created_at || Date.now() / 1000)),
76
103
  model: respJson.model,
77
- choices: [{ index: 0, finish_reason: respJson.status === "completed" ? "stop" : "length", message: { role: "assistant", content: text } }],
104
+ choices: [{ index: 0, finish_reason: finish, message }],
78
105
  usage: respJson.usage || { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
79
106
  };
80
107
  return chatJson;
@@ -102,6 +129,7 @@ export function reshapeResponsesSse(res, fallbackModel) {
102
129
  let respModel = fallbackModel || "";
103
130
  let created = Math.floor(Date.now() / 1000);
104
131
  let hasSentRole = false;
132
+ const toolMap = new Map(); // output_index -> {idx, id, name}
105
133
 
106
134
  function chatChunk(delta, finish) {
107
135
  const id = respId || `resp_${Date.now()}`;
@@ -155,37 +183,75 @@ export function reshapeResponsesSse(res, fallbackModel) {
155
183
  if (data.response?.created_at) created = Math.floor(data.response.created_at);
156
184
  if (data.response?.id && !respId) respId = data.response.id;
157
185
  // 关注 output_text.delta
158
- if (curEvent === "response.output_text.delta" || data.type === "response.output_text.delta") {
159
- const deltaText = data.delta || "";
160
- if (deltaText) {
161
- if (!hasSentRole) {
162
- hasSentRole = true;
163
- out += chatChunk({ role: "assistant" }, null);
164
- }
165
- out += chatChunk({ content: deltaText }, null);
166
- }
167
- } else if (curEvent === "response.completed" || data.type === "response.completed") {
168
- const usage = data.response?.usage || null;
169
- const finish = data.response?.status === "completed" ? "stop" : null;
170
- // 末帧带 usage
171
- const id = respId || `resp_${Date.now()}`;
172
- const payload = {
173
- id,
174
- object: "chat.completion.chunk",
175
- created,
176
- model: respModel,
177
- choices: [{ index: 0, delta: {}, finish_reason: finish }],
178
- usage: usage || undefined,
179
- };
180
- out += `data: ${JSON.stringify(payload)}\n\n`;
181
- } else if (data.type === "response.output_item.added" && data.item?.type === "message") {
182
- // message 开始,可发送 role
183
- if (!hasSentRole) {
184
- hasSentRole = true;
185
- out += chatChunk({ role: "assistant" }, null);
186
- }
187
- }
188
- // reasoning 加密块忽略
186
+ if (curEvent === "response.output_text.delta" || data.type === "response.output_text.delta") {
187
+ const deltaText = data.delta || "";
188
+ if (deltaText) {
189
+ if (!hasSentRole) {
190
+ hasSentRole = true;
191
+ out += chatChunk({ role: "assistant" }, null);
192
+ }
193
+ out += chatChunk({ content: deltaText }, null);
194
+ }
195
+ } else if (curEvent === "response.completed" || data.type === "response.completed") {
196
+ const usage = data.response?.usage || null;
197
+ // 若有 tool_calls,finish 应为 tool_calls
198
+ const hasTools = toolMap.size > 0;
199
+ const finish = hasTools ? "tool_calls" : (data.response?.status === "completed" ? "stop" : null);
200
+ // 末帧带 usage
201
+ const id = respId || `resp_${Date.now()}`;
202
+ const payload = {
203
+ id,
204
+ object: "chat.completion.chunk",
205
+ created,
206
+ model: respModel,
207
+ choices: [{ index: 0, delta: {}, finish_reason: finish }],
208
+ usage: usage || undefined,
209
+ };
210
+ out += `data: ${JSON.stringify(payload)}\n\n`;
211
+ } else if (data.type === "response.output_item.added" && data.item?.type === "message") {
212
+ // message 开始,可发送 role
213
+ if (!hasSentRole) {
214
+ hasSentRole = true;
215
+ out += chatChunk({ role: "assistant" }, null);
216
+ }
217
+ } else if (data.type === "response.output_item.added" && data.item?.type === "function_call") {
218
+ const outIdx = Number(data.output_index ?? 1);
219
+ const toolIdx = Math.max(0, outIdx - 1);
220
+ const callId = data.item?.call_id || data.item?.id || "";
221
+ const name = data.item?.name || "";
222
+ toolMap.set(outIdx, { idx: toolIdx, id: callId, name });
223
+ if (!hasSentRole) {
224
+ hasSentRole = true;
225
+ out += chatChunk({ role: "assistant" }, null);
226
+ }
227
+ const tc = { index: toolIdx, id: callId, type: "function", function: { name, arguments: "" } };
228
+ // 清理空字符串,避免 undefined
229
+ if (!callId) delete tc.id;
230
+ if (!name) delete tc.function.name;
231
+ out += chatChunk({ tool_calls: [tc] }, null);
232
+ } else if (data.type === "response.function_call_arguments.delta") {
233
+ const outIdx = Number(data.output_index ?? 1);
234
+ const entry = toolMap.get(outIdx) || { idx: Math.max(0, outIdx - 1) };
235
+ const deltaArgs = data.delta || "";
236
+ if (deltaArgs) {
237
+ if (!hasSentRole) {
238
+ hasSentRole = true;
239
+ out += chatChunk({ role: "assistant" }, null);
240
+ }
241
+ out += chatChunk({ tool_calls: [{ index: entry.idx, function: { arguments: deltaArgs } }] }, null);
242
+ }
243
+ } else if (data.type === "response.function_call_arguments.done") {
244
+ const outIdx = Number(data.output_index ?? 1);
245
+ const entry = toolMap.get(outIdx) || { idx: Math.max(0, outIdx - 1) };
246
+ const args = data.arguments || "";
247
+ if (args && !toolMap.get(outIdx)?._done) {
248
+ // done 可能带全量,若未通过 delta 发送过,补发
249
+ // 已通过 delta 流式发送则忽略,避免重复
250
+ }
251
+ } else if (data.type === "response.output_item.done" && data.item?.type === "function_call") {
252
+ // 可忽略,已通过 added+delta 完整
253
+ }
254
+ // reasoning 加密块忽略
189
255
  }
190
256
  if (out) controller.enqueue(encoder.encode(out));
191
257
  } catch {