qwenproxy-cli 1.0.0 → 1.0.2

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.
Files changed (109) hide show
  1. package/LICENSE +14 -14
  2. package/README.md +906 -906
  3. package/bin/qwenproxy.js +5 -1
  4. package/package.json +77 -78
  5. package/src/api/error-classifier.ts +159 -159
  6. package/src/api/error-helpers.ts +118 -118
  7. package/src/api/models.ts +261 -261
  8. package/src/api/server.ts +860 -859
  9. package/src/cache/memory-cache.ts +385 -385
  10. package/src/clean-cache.ts +204 -204
  11. package/src/core/account-concurrency.ts +671 -671
  12. package/src/core/account-manager.ts +301 -297
  13. package/src/core/account-priority.ts +163 -163
  14. package/src/core/accounts.ts +186 -186
  15. package/src/core/config.ts +383 -383
  16. package/src/core/crypto-utils.ts +79 -79
  17. package/src/core/database.ts +276 -276
  18. package/src/core/errors.ts +118 -118
  19. package/src/core/logger.ts +269 -269
  20. package/src/core/memory-usage.ts +84 -84
  21. package/src/core/metrics.ts +291 -291
  22. package/src/core/model-alias.ts +77 -77
  23. package/src/core/model-registry.ts +544 -544
  24. package/src/core/mutex.ts +119 -119
  25. package/src/core/paths.ts +199 -199
  26. package/src/core/prompt-limits.ts +214 -214
  27. package/src/core/reasoning-effort.ts +102 -102
  28. package/src/core/stream-registry.ts +96 -96
  29. package/src/core/waf-isolation.ts +117 -117
  30. package/src/core/watchdog.ts +195 -195
  31. package/src/delete-chats.ts +23 -23
  32. package/src/index.ts +65 -64
  33. package/src/login.ts +147 -147
  34. package/src/reset-cooldowns.ts +11 -11
  35. package/src/routes/anthropic/index.ts +355 -355
  36. package/src/routes/anthropic/translate.ts +522 -522
  37. package/src/routes/anthropic/types.ts +154 -154
  38. package/src/routes/anthropic/validation.ts +144 -144
  39. package/src/routes/chat/account.ts +1817 -1817
  40. package/src/routes/chat/context.ts +241 -241
  41. package/src/routes/chat/errors.ts +85 -85
  42. package/src/routes/chat/helpers.ts +268 -268
  43. package/src/routes/chat/index.ts +618 -618
  44. package/src/routes/chat/media.ts +285 -285
  45. package/src/routes/chat/retry-policy.ts +754 -754
  46. package/src/routes/chat/stop.ts +98 -98
  47. package/src/routes/chat/streaming.ts +2710 -2710
  48. package/src/routes/chat/validation.ts +526 -526
  49. package/src/routes/chat.ts +2 -2
  50. package/src/routes/completions.ts +290 -290
  51. package/src/routes/images.ts +139 -139
  52. package/src/routes/responses/adapter.ts +503 -503
  53. package/src/routes/responses/index.ts +405 -405
  54. package/src/routes/responses/state.ts +230 -230
  55. package/src/routes/responses/streaming.ts +528 -528
  56. package/src/routes/responses/types.ts +285 -285
  57. package/src/routes/responses/validation.ts +202 -202
  58. package/src/routes/upload.ts +731 -731
  59. package/src/routes/videos.ts +214 -214
  60. package/src/services/auth-playwright.ts +173 -173
  61. package/src/services/captcha-coordinator.ts +161 -161
  62. package/src/services/captcha-solver.ts +553 -553
  63. package/src/services/chat-cleanup.ts +80 -80
  64. package/src/services/context-meter.ts +317 -317
  65. package/src/services/fingerprint.ts +242 -242
  66. package/src/services/human-behavior.ts +173 -173
  67. package/src/services/media-generation.ts +1748 -1748
  68. package/src/services/playwright.ts +2878 -2800
  69. package/src/services/qwen-chat-pool.ts +345 -345
  70. package/src/services/qwen-errors.ts +133 -133
  71. package/src/services/qwen-headers.ts +79 -79
  72. package/src/services/qwen-thread-state.ts +393 -393
  73. package/src/services/qwen-url.ts +19 -19
  74. package/src/services/qwen.ts +3126 -3126
  75. package/src/services/session-keeper.ts +88 -88
  76. package/src/services/token-estimation-metrics.ts +118 -118
  77. package/src/sync/claude-code.ts +75 -75
  78. package/src/sync/codex.ts +123 -123
  79. package/src/sync/index.ts +362 -362
  80. package/src/sync/omp.ts +105 -105
  81. package/src/sync/opencode.ts +214 -214
  82. package/src/sync/types.ts +53 -53
  83. package/src/sync/utils.ts +27 -27
  84. package/src/sync-clients.ts +189 -189
  85. package/src/tools/instructions.ts +137 -137
  86. package/src/tools/manifest.ts +81 -81
  87. package/src/tools/parser.ts +2989 -2989
  88. package/src/tools/toolcall-tags.ts +142 -142
  89. package/src/tui/app.ts +259 -264
  90. package/src/tui/index.ts +61 -61
  91. package/src/tui/markdown.ts +258 -258
  92. package/src/tui/proxy-client.ts +331 -326
  93. package/src/tui/screen.ts +294 -278
  94. package/src/tui/server-manager.ts +270 -270
  95. package/src/tui/theme.ts +432 -432
  96. package/src/tui/types.ts +33 -33
  97. package/src/tui/views/accounts-view.ts +656 -656
  98. package/src/tui/views/chat-view.ts +1018 -823
  99. package/src/tui/views/logs-view.ts +479 -413
  100. package/src/tui/views/status-view.ts +204 -204
  101. package/src/tui/views/storage-view.ts +304 -291
  102. package/src/tui/views/sync-view.ts +409 -409
  103. package/src/types/ali-oss.d.ts +32 -32
  104. package/src/update-cli.ts +121 -0
  105. package/src/utils/context-truncation.ts +84 -84
  106. package/src/utils/json.ts +380 -380
  107. package/src/utils/session-id.ts +37 -37
  108. package/src/utils/tool-call-guard.ts +84 -84
  109. package/src/utils/types.ts +109 -109
@@ -1,503 +1,503 @@
1
- import crypto from "crypto";
2
- import { mapClientModelToQwen } from "../../core/model-alias.ts";
3
- import { normalizeReasoningEffort, applyEffortToModel } from "../../core/reasoning-effort.ts";
4
- import type {
5
- ResponsesRequest,
6
- ResponsesResponse,
7
- ResponsesContentPart,
8
- ResponsesOutputMessage,
9
- ResponsesOutputFunctionCall,
10
- ResponsesOutputReasoning,
11
- ResponsesUsage,
12
- ResponsesFunctionTool,
13
- } from "./types.ts";
14
-
15
- // OpenAI Chat Completions types (internal)
16
- interface ChatMessage {
17
- role: "system" | "user" | "assistant" | "tool";
18
- content: string | null;
19
- reasoning_content?: string;
20
- tool_calls?: ChatToolCall[];
21
- tool_call_id?: string;
22
- }
23
-
24
- interface ChatToolCall {
25
- id: string;
26
- type: "function";
27
- function: { name: string; arguments: string };
28
- }
29
-
30
- interface ChatRequest {
31
- model: string;
32
- messages: ChatMessage[];
33
- tools?: Array<{
34
- type: "function";
35
- function: {
36
- name: string;
37
- description?: string;
38
- parameters?: Record<string, unknown>;
39
- strict?: boolean;
40
- };
41
- }>;
42
- tool_choice?: string | object;
43
- stream?: boolean;
44
- temperature?: number;
45
- top_p?: number;
46
- max_tokens?: number;
47
- max_completion_tokens?: number;
48
- parallel_tool_calls?: boolean;
49
- response_format?: Record<string, unknown>;
50
- reasoning_effort?: string;
51
- }
52
-
53
- interface ChatChoice {
54
- index: number;
55
- message: ChatMessage;
56
- finish_reason: "stop" | "tool_calls" | "length" | "content_filter" | null;
57
- }
58
-
59
- interface ChatResponse {
60
- id: string;
61
- object: "chat.completion";
62
- created: number;
63
- model: string;
64
- choices: ChatChoice[];
65
- usage: {
66
- prompt_tokens: number;
67
- completion_tokens: number;
68
- total_tokens: number;
69
- context_meter?: ResponsesUsage["context_meter"];
70
- };
71
- }
72
-
73
- // ============ ID generators ============
74
-
75
- export function generateResponseId(): string {
76
- return `resp_${crypto.randomBytes(16).toString("hex")}`;
77
- }
78
-
79
- export function generateMessageId(): string {
80
- return `msg_${crypto.randomBytes(16).toString("hex")}`;
81
- }
82
-
83
- export function generateCallId(): string {
84
- return `call_${crypto.randomBytes(12).toString("hex")}`;
85
- }
86
-
87
- // ============ Request conversion ============
88
-
89
- /**
90
- * Extract text from a content field that can be string or array of parts.
91
- */
92
- function extractText(content?: string | ResponsesContentPart[]): string {
93
- if (!content) return "";
94
- if (typeof content === "string") return content;
95
- return content
96
- .filter(
97
- (p) =>
98
- p.type === "input_text" ||
99
- p.type === "output_text" ||
100
- p.type === "text",
101
- )
102
- .map((p) => p.text || "")
103
- .join("\n");
104
- }
105
-
106
- /**
107
- * Extract multimodal content parts (images/files) for chat completions.
108
- */
109
- function extractMultimodalParts(
110
- content: ResponsesContentPart[],
111
- ): Array<{ type: string; image_url?: { url: string; detail?: string }; file_url?: { url: string; filename?: string } }> {
112
- const parts: Array<{ type: string; image_url?: { url: string; detail?: string }; file_url?: { url: string; filename?: string } }> = [];
113
- for (const p of content) {
114
- if (p.type === "input_image" && p.image_url) {
115
- parts.push({
116
- type: "image_url",
117
- image_url: { url: p.image_url, detail: p.detail || "auto" },
118
- });
119
- } else if (p.type === "input_file" && p.file) {
120
- const fileData = p.file.file_data || p.file.file_id || "";
121
- if (fileData) {
122
- parts.push({
123
- type: "file_url",
124
- file_url: { url: fileData, filename: p.file.filename },
125
- });
126
- }
127
- }
128
- }
129
- return parts;
130
- }
131
-
132
- /**
133
- * Convert Responses API request to OpenAI Chat Completions format.
134
- */
135
- type ResponsesRequestInput = {
136
- input: string | unknown[];
137
- } & Omit<ResponsesRequest, "input">;
138
-
139
- export function responsesToChatCompletions(
140
- req: ResponsesRequestInput,
141
- historyMessages: ChatMessage[] = [],
142
- ): ChatRequest {
143
- const messages: ChatMessage[] = [...historyMessages];
144
-
145
- // Instructions → system message (prepended)
146
- if (req.instructions) {
147
- messages.unshift({ role: "system", content: req.instructions });
148
- }
149
-
150
- // Convert input to messages
151
- if (typeof req.input === "string") {
152
- messages.push({ role: "user", content: req.input });
153
- } else if (Array.isArray(req.input)) {
154
- for (const raw of req.input) {
155
- const msg = raw as Record<string, unknown>;
156
-
157
- // Handle function_call_output (tool results)
158
- if (msg.type === "function_call_output") {
159
- messages.push({
160
- role: "tool",
161
- content: (msg.output as string) ?? extractText(msg.content as any),
162
- tool_call_id: msg.call_id as string,
163
- });
164
- continue;
165
- }
166
-
167
- // Handle function_call (assistant tool calls from history)
168
- if (msg.type === "function_call") {
169
- const callId = (msg.call_id as string) || generateCallId();
170
- const name = (msg.name as string) || "unknown";
171
- const args = (msg.arguments as string) || "{}";
172
-
173
- messages.push({
174
- role: "assistant",
175
- content: null,
176
- tool_calls: [
177
- {
178
- id: callId,
179
- type: "function",
180
- function: { name, arguments: args },
181
- },
182
- ],
183
- });
184
- continue;
185
- }
186
-
187
- // Skip items without role (unknown types like reasoning)
188
- if (!("role" in msg)) continue;
189
-
190
- const msgRole = msg.role as string;
191
- const rawContent = msg.content as string | ResponsesContentPart[] | undefined;
192
- const content = extractText(rawContent);
193
-
194
- // Handle multimodal content (images/files)
195
- if (Array.isArray(rawContent)) {
196
- const multimodalParts = extractMultimodalParts(rawContent);
197
- if (multimodalParts.length > 0) {
198
- // Build content array with text + multimodal parts
199
- const contentArray: Array<{ type: string; text?: string; image_url?: any; file_url?: any }> = [];
200
- if (content) {
201
- contentArray.push({ type: "text", text: content });
202
- }
203
- contentArray.push(...multimodalParts);
204
- messages.push({ role: msgRole as any, content: contentArray as any });
205
- continue;
206
- }
207
- }
208
-
209
- if (msgRole === "system" || msgRole === "developer") {
210
- messages.push({ role: "system", content });
211
- } else {
212
- messages.push({ role: msgRole as any, content });
213
- }
214
- }
215
- }
216
-
217
- // Convert tools — only function tools are sent to Qwen
218
- // Built-in tools (web_search, shell, etc.) are silently dropped
219
- let tools: ChatRequest["tools"];
220
- if (req.tools && req.tools.length > 0) {
221
- const functionTools = req.tools.filter(
222
- (t): t is ResponsesFunctionTool => t.type === "function",
223
- );
224
- if (functionTools.length > 0) {
225
- tools = functionTools.map((t) => ({
226
- type: "function" as const,
227
- function: {
228
- name: t.name,
229
- description: t.description,
230
- parameters: t.parameters,
231
- strict: t.strict,
232
- },
233
- }));
234
- }
235
- }
236
-
237
- // Convert tool_choice
238
- let toolChoice: ChatRequest["tool_choice"];
239
- if (req.tool_choice != null) {
240
- if (typeof req.tool_choice === "string") {
241
- toolChoice = req.tool_choice;
242
- } else {
243
- const name = req.tool_choice.name ?? req.tool_choice.function?.name;
244
- if (name) {
245
- toolChoice = {
246
- type: "function",
247
- function: { name },
248
- };
249
- }
250
- }
251
- }
252
-
253
- const chatReq: ChatRequest = {
254
- model: mapClientModelToQwen(req.model),
255
- messages,
256
- stream: req.stream ?? false,
257
- };
258
-
259
- // Apply reasoning effort to model selection
260
- const rawEffort = req.reasoning?.effort ?? (req as any).reasoning_effort;
261
- const normalizedEffort = normalizeReasoningEffort(rawEffort);
262
- if (normalizedEffort) {
263
- chatReq.reasoning_effort = normalizedEffort;
264
- chatReq.model = applyEffortToModel(chatReq.model, normalizedEffort);
265
- }
266
-
267
- if (tools) chatReq.tools = tools;
268
- if (toolChoice !== undefined) chatReq.tool_choice = toolChoice;
269
- if (req.temperature !== undefined) chatReq.temperature = req.temperature;
270
- if (req.top_p !== undefined) chatReq.top_p = req.top_p;
271
- if (req.max_output_tokens !== undefined)
272
- chatReq.max_completion_tokens = req.max_output_tokens;
273
- if (req.parallel_tool_calls !== undefined)
274
- chatReq.parallel_tool_calls = req.parallel_tool_calls;
275
-
276
- // Structured outputs (Responses `text.format` → chat `response_format`): the
277
- // chat pipeline enforces it at the prompt level (see
278
- // buildResponseFormatInstruction in chat/validation.ts).
279
- const textFormat = req.text?.format;
280
- if (textFormat && textFormat.type !== "text") {
281
- if (textFormat.type === "json_schema") {
282
- chatReq.response_format = {
283
- type: "json_schema",
284
- json_schema: {
285
- ...(textFormat.name ? { name: textFormat.name } : {}),
286
- ...(textFormat.description
287
- ? { description: textFormat.description }
288
- : {}),
289
- ...(textFormat.schema ? { schema: textFormat.schema } : {}),
290
- ...(textFormat.strict !== undefined
291
- ? { strict: textFormat.strict }
292
- : {}),
293
- },
294
- };
295
- } else {
296
- chatReq.response_format = { type: "json_object" };
297
- }
298
- }
299
-
300
- return chatReq;
301
- }
302
-
303
- // ============ Response conversion ============
304
-
305
- /**
306
- * Convert OpenAI Chat Completions response to Responses API format.
307
- */
308
- export function chatCompletionsToResponses(
309
- chatRes: ChatResponse,
310
- requestModel: string,
311
- originalRequest: ResponsesRequestInput,
312
- ): ResponsesResponse {
313
- const choice = chatRes.choices[0];
314
- const output: (
315
- | ResponsesOutputMessage
316
- | ResponsesOutputFunctionCall
317
- | ResponsesOutputReasoning
318
- )[] = [];
319
-
320
- // Reasoning content → reasoning output item
321
- if ((choice.message as any).reasoning_content) {
322
- output.push({
323
- type: "reasoning",
324
- id: `rs_${crypto.randomBytes(16).toString("hex")}`,
325
- summary: [
326
- {
327
- type: "summary_text",
328
- text: (choice.message as any).reasoning_content,
329
- },
330
- ],
331
- });
332
- }
333
-
334
- // Text content → message output item
335
- if (choice.message.content) {
336
- const msgId = generateMessageId();
337
- output.push({
338
- type: "message",
339
- id: msgId,
340
- role: "assistant",
341
- status: "completed",
342
- content: [
343
- {
344
- type: "output_text",
345
- text: choice.message.content,
346
- annotations: [],
347
- },
348
- ],
349
- });
350
- }
351
-
352
- // Tool calls → function_call output items
353
- if (choice.message.tool_calls) {
354
- for (const tc of choice.message.tool_calls) {
355
- output.push({
356
- type: "function_call",
357
- id: `fc_${crypto.randomBytes(12).toString("hex")}`,
358
- call_id: tc.id,
359
- name: tc.function.name,
360
- arguments: tc.function.arguments,
361
- status: "completed",
362
- });
363
- }
364
- }
365
-
366
- // Build usage — always include details (Grok/serde requires them)
367
- const usage: ResponsesUsage = {
368
- input_tokens: chatRes.usage.prompt_tokens,
369
- output_tokens: chatRes.usage.completion_tokens,
370
- total_tokens: chatRes.usage.total_tokens,
371
- input_tokens_details: {
372
- cached_tokens: (chatRes.usage as any).prompt_tokens_details?.cached_tokens ?? 0,
373
- },
374
- output_tokens_details: {
375
- reasoning_tokens: (chatRes.usage as any).completion_tokens_details?.reasoning_tokens ?? 0,
376
- },
377
- ...(chatRes.usage.context_meter
378
- ? { context_meter: chatRes.usage.context_meter }
379
- : {}),
380
- };
381
-
382
- return {
383
- id: generateResponseId(),
384
- object: "response",
385
- created_at: chatRes.created,
386
- model: requestModel,
387
- status: "completed",
388
- output,
389
- usage,
390
- parallel_tool_calls: originalRequest.parallel_tool_calls,
391
- tool_choice: originalRequest.tool_choice ?? undefined,
392
- tools: originalRequest.tools ?? undefined,
393
- temperature: originalRequest.temperature,
394
- top_p: originalRequest.top_p,
395
- max_output_tokens: originalRequest.max_output_tokens,
396
- previous_response_id: originalRequest.previous_response_id || null,
397
- last_response_id: originalRequest.previous_response_id || null,
398
- metadata: originalRequest.metadata,
399
- user: originalRequest.user,
400
- error: null,
401
- incomplete_details: null,
402
- };
403
- }
404
-
405
- /**
406
- * Build a minimal "in-progress" response for streaming initial event.
407
- */
408
- export function buildInProgressResponse(
409
- responseId: string,
410
- requestModel: string,
411
- originalRequest: ResponsesRequestInput,
412
- ): ResponsesResponse {
413
- return {
414
- id: responseId,
415
- object: "response",
416
- created_at: Math.floor(Date.now() / 1000),
417
- model: requestModel,
418
- status: "in_progress",
419
- output: [],
420
- usage: {
421
- input_tokens: 0,
422
- output_tokens: 0,
423
- total_tokens: 0,
424
- input_tokens_details: { cached_tokens: 0 },
425
- output_tokens_details: { reasoning_tokens: 0 },
426
- },
427
- parallel_tool_calls: originalRequest.parallel_tool_calls,
428
- tool_choice: originalRequest.tool_choice ?? undefined,
429
- tools: originalRequest.tools ?? undefined,
430
- temperature: originalRequest.temperature,
431
- top_p: originalRequest.top_p,
432
- max_output_tokens: originalRequest.max_output_tokens,
433
- previous_response_id: originalRequest.previous_response_id || null,
434
- last_response_id: originalRequest.previous_response_id || null,
435
- metadata: originalRequest.metadata,
436
- user: originalRequest.user,
437
- error: null,
438
- incomplete_details: null,
439
- };
440
- }
441
-
442
- /**
443
- * Finalize an in-progress response for the completed event.
444
- */
445
- export function finalizeResponse(
446
- inProgress: ResponsesResponse,
447
- output: (
448
- | ResponsesOutputMessage
449
- | ResponsesOutputFunctionCall
450
- | ResponsesOutputReasoning
451
- )[],
452
- usage: ResponsesUsage,
453
- ): ResponsesResponse {
454
- return {
455
- ...inProgress,
456
- status: "completed",
457
- output,
458
- usage,
459
- };
460
- }
461
-
462
- export type ChatHistoryMessage = ChatMessage;
463
-
464
- /**
465
- * Convert a Responses API output array into Chat Completions history messages.
466
- */
467
- export function responsesOutputToChatMessages(
468
- output: (
469
- | ResponsesOutputMessage
470
- | ResponsesOutputFunctionCall
471
- | ResponsesOutputReasoning
472
- )[],
473
- ): ChatMessage[] {
474
- const messages: ChatMessage[] = [];
475
- const toolCalls: ChatToolCall[] = [];
476
- const textParts: string[] = [];
477
-
478
- for (const item of output) {
479
- if (item.type === "message") {
480
- for (const part of item.content) {
481
- if (part.type === "output_text" && part.text) {
482
- textParts.push(part.text);
483
- }
484
- }
485
- } else if (item.type === "function_call") {
486
- toolCalls.push({
487
- id: item.call_id,
488
- type: "function",
489
- function: { name: item.name, arguments: item.arguments || "{}" },
490
- });
491
- }
492
- }
493
-
494
- if (textParts.length > 0 || toolCalls.length > 0) {
495
- messages.push({
496
- role: "assistant",
497
- content: textParts.length > 0 ? textParts.join("\n") : null,
498
- ...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),
499
- });
500
- }
501
-
502
- return messages;
503
- }
1
+ import crypto from "crypto";
2
+ import { mapClientModelToQwen } from "../../core/model-alias.ts";
3
+ import { normalizeReasoningEffort, applyEffortToModel } from "../../core/reasoning-effort.ts";
4
+ import type {
5
+ ResponsesRequest,
6
+ ResponsesResponse,
7
+ ResponsesContentPart,
8
+ ResponsesOutputMessage,
9
+ ResponsesOutputFunctionCall,
10
+ ResponsesOutputReasoning,
11
+ ResponsesUsage,
12
+ ResponsesFunctionTool,
13
+ } from "./types.ts";
14
+
15
+ // OpenAI Chat Completions types (internal)
16
+ interface ChatMessage {
17
+ role: "system" | "user" | "assistant" | "tool";
18
+ content: string | null;
19
+ reasoning_content?: string;
20
+ tool_calls?: ChatToolCall[];
21
+ tool_call_id?: string;
22
+ }
23
+
24
+ interface ChatToolCall {
25
+ id: string;
26
+ type: "function";
27
+ function: { name: string; arguments: string };
28
+ }
29
+
30
+ interface ChatRequest {
31
+ model: string;
32
+ messages: ChatMessage[];
33
+ tools?: Array<{
34
+ type: "function";
35
+ function: {
36
+ name: string;
37
+ description?: string;
38
+ parameters?: Record<string, unknown>;
39
+ strict?: boolean;
40
+ };
41
+ }>;
42
+ tool_choice?: string | object;
43
+ stream?: boolean;
44
+ temperature?: number;
45
+ top_p?: number;
46
+ max_tokens?: number;
47
+ max_completion_tokens?: number;
48
+ parallel_tool_calls?: boolean;
49
+ response_format?: Record<string, unknown>;
50
+ reasoning_effort?: string;
51
+ }
52
+
53
+ interface ChatChoice {
54
+ index: number;
55
+ message: ChatMessage;
56
+ finish_reason: "stop" | "tool_calls" | "length" | "content_filter" | null;
57
+ }
58
+
59
+ interface ChatResponse {
60
+ id: string;
61
+ object: "chat.completion";
62
+ created: number;
63
+ model: string;
64
+ choices: ChatChoice[];
65
+ usage: {
66
+ prompt_tokens: number;
67
+ completion_tokens: number;
68
+ total_tokens: number;
69
+ context_meter?: ResponsesUsage["context_meter"];
70
+ };
71
+ }
72
+
73
+ // ============ ID generators ============
74
+
75
+ export function generateResponseId(): string {
76
+ return `resp_${crypto.randomBytes(16).toString("hex")}`;
77
+ }
78
+
79
+ export function generateMessageId(): string {
80
+ return `msg_${crypto.randomBytes(16).toString("hex")}`;
81
+ }
82
+
83
+ export function generateCallId(): string {
84
+ return `call_${crypto.randomBytes(12).toString("hex")}`;
85
+ }
86
+
87
+ // ============ Request conversion ============
88
+
89
+ /**
90
+ * Extract text from a content field that can be string or array of parts.
91
+ */
92
+ function extractText(content?: string | ResponsesContentPart[]): string {
93
+ if (!content) return "";
94
+ if (typeof content === "string") return content;
95
+ return content
96
+ .filter(
97
+ (p) =>
98
+ p.type === "input_text" ||
99
+ p.type === "output_text" ||
100
+ p.type === "text",
101
+ )
102
+ .map((p) => p.text || "")
103
+ .join("\n");
104
+ }
105
+
106
+ /**
107
+ * Extract multimodal content parts (images/files) for chat completions.
108
+ */
109
+ function extractMultimodalParts(
110
+ content: ResponsesContentPart[],
111
+ ): Array<{ type: string; image_url?: { url: string; detail?: string }; file_url?: { url: string; filename?: string } }> {
112
+ const parts: Array<{ type: string; image_url?: { url: string; detail?: string }; file_url?: { url: string; filename?: string } }> = [];
113
+ for (const p of content) {
114
+ if (p.type === "input_image" && p.image_url) {
115
+ parts.push({
116
+ type: "image_url",
117
+ image_url: { url: p.image_url, detail: p.detail || "auto" },
118
+ });
119
+ } else if (p.type === "input_file" && p.file) {
120
+ const fileData = p.file.file_data || p.file.file_id || "";
121
+ if (fileData) {
122
+ parts.push({
123
+ type: "file_url",
124
+ file_url: { url: fileData, filename: p.file.filename },
125
+ });
126
+ }
127
+ }
128
+ }
129
+ return parts;
130
+ }
131
+
132
+ /**
133
+ * Convert Responses API request to OpenAI Chat Completions format.
134
+ */
135
+ type ResponsesRequestInput = {
136
+ input: string | unknown[];
137
+ } & Omit<ResponsesRequest, "input">;
138
+
139
+ export function responsesToChatCompletions(
140
+ req: ResponsesRequestInput,
141
+ historyMessages: ChatMessage[] = [],
142
+ ): ChatRequest {
143
+ const messages: ChatMessage[] = [...historyMessages];
144
+
145
+ // Instructions → system message (prepended)
146
+ if (req.instructions) {
147
+ messages.unshift({ role: "system", content: req.instructions });
148
+ }
149
+
150
+ // Convert input to messages
151
+ if (typeof req.input === "string") {
152
+ messages.push({ role: "user", content: req.input });
153
+ } else if (Array.isArray(req.input)) {
154
+ for (const raw of req.input) {
155
+ const msg = raw as Record<string, unknown>;
156
+
157
+ // Handle function_call_output (tool results)
158
+ if (msg.type === "function_call_output") {
159
+ messages.push({
160
+ role: "tool",
161
+ content: (msg.output as string) ?? extractText(msg.content as any),
162
+ tool_call_id: msg.call_id as string,
163
+ });
164
+ continue;
165
+ }
166
+
167
+ // Handle function_call (assistant tool calls from history)
168
+ if (msg.type === "function_call") {
169
+ const callId = (msg.call_id as string) || generateCallId();
170
+ const name = (msg.name as string) || "unknown";
171
+ const args = (msg.arguments as string) || "{}";
172
+
173
+ messages.push({
174
+ role: "assistant",
175
+ content: null,
176
+ tool_calls: [
177
+ {
178
+ id: callId,
179
+ type: "function",
180
+ function: { name, arguments: args },
181
+ },
182
+ ],
183
+ });
184
+ continue;
185
+ }
186
+
187
+ // Skip items without role (unknown types like reasoning)
188
+ if (!("role" in msg)) continue;
189
+
190
+ const msgRole = msg.role as string;
191
+ const rawContent = msg.content as string | ResponsesContentPart[] | undefined;
192
+ const content = extractText(rawContent);
193
+
194
+ // Handle multimodal content (images/files)
195
+ if (Array.isArray(rawContent)) {
196
+ const multimodalParts = extractMultimodalParts(rawContent);
197
+ if (multimodalParts.length > 0) {
198
+ // Build content array with text + multimodal parts
199
+ const contentArray: Array<{ type: string; text?: string; image_url?: any; file_url?: any }> = [];
200
+ if (content) {
201
+ contentArray.push({ type: "text", text: content });
202
+ }
203
+ contentArray.push(...multimodalParts);
204
+ messages.push({ role: msgRole as any, content: contentArray as any });
205
+ continue;
206
+ }
207
+ }
208
+
209
+ if (msgRole === "system" || msgRole === "developer") {
210
+ messages.push({ role: "system", content });
211
+ } else {
212
+ messages.push({ role: msgRole as any, content });
213
+ }
214
+ }
215
+ }
216
+
217
+ // Convert tools — only function tools are sent to Qwen
218
+ // Built-in tools (web_search, shell, etc.) are silently dropped
219
+ let tools: ChatRequest["tools"];
220
+ if (req.tools && req.tools.length > 0) {
221
+ const functionTools = req.tools.filter(
222
+ (t): t is ResponsesFunctionTool => t.type === "function",
223
+ );
224
+ if (functionTools.length > 0) {
225
+ tools = functionTools.map((t) => ({
226
+ type: "function" as const,
227
+ function: {
228
+ name: t.name,
229
+ description: t.description,
230
+ parameters: t.parameters,
231
+ strict: t.strict,
232
+ },
233
+ }));
234
+ }
235
+ }
236
+
237
+ // Convert tool_choice
238
+ let toolChoice: ChatRequest["tool_choice"];
239
+ if (req.tool_choice != null) {
240
+ if (typeof req.tool_choice === "string") {
241
+ toolChoice = req.tool_choice;
242
+ } else {
243
+ const name = req.tool_choice.name ?? req.tool_choice.function?.name;
244
+ if (name) {
245
+ toolChoice = {
246
+ type: "function",
247
+ function: { name },
248
+ };
249
+ }
250
+ }
251
+ }
252
+
253
+ const chatReq: ChatRequest = {
254
+ model: mapClientModelToQwen(req.model),
255
+ messages,
256
+ stream: req.stream ?? false,
257
+ };
258
+
259
+ // Apply reasoning effort to model selection
260
+ const rawEffort = req.reasoning?.effort ?? (req as any).reasoning_effort;
261
+ const normalizedEffort = normalizeReasoningEffort(rawEffort);
262
+ if (normalizedEffort) {
263
+ chatReq.reasoning_effort = normalizedEffort;
264
+ chatReq.model = applyEffortToModel(chatReq.model, normalizedEffort);
265
+ }
266
+
267
+ if (tools) chatReq.tools = tools;
268
+ if (toolChoice !== undefined) chatReq.tool_choice = toolChoice;
269
+ if (req.temperature !== undefined) chatReq.temperature = req.temperature;
270
+ if (req.top_p !== undefined) chatReq.top_p = req.top_p;
271
+ if (req.max_output_tokens !== undefined)
272
+ chatReq.max_completion_tokens = req.max_output_tokens;
273
+ if (req.parallel_tool_calls !== undefined)
274
+ chatReq.parallel_tool_calls = req.parallel_tool_calls;
275
+
276
+ // Structured outputs (Responses `text.format` → chat `response_format`): the
277
+ // chat pipeline enforces it at the prompt level (see
278
+ // buildResponseFormatInstruction in chat/validation.ts).
279
+ const textFormat = req.text?.format;
280
+ if (textFormat && textFormat.type !== "text") {
281
+ if (textFormat.type === "json_schema") {
282
+ chatReq.response_format = {
283
+ type: "json_schema",
284
+ json_schema: {
285
+ ...(textFormat.name ? { name: textFormat.name } : {}),
286
+ ...(textFormat.description
287
+ ? { description: textFormat.description }
288
+ : {}),
289
+ ...(textFormat.schema ? { schema: textFormat.schema } : {}),
290
+ ...(textFormat.strict !== undefined
291
+ ? { strict: textFormat.strict }
292
+ : {}),
293
+ },
294
+ };
295
+ } else {
296
+ chatReq.response_format = { type: "json_object" };
297
+ }
298
+ }
299
+
300
+ return chatReq;
301
+ }
302
+
303
+ // ============ Response conversion ============
304
+
305
+ /**
306
+ * Convert OpenAI Chat Completions response to Responses API format.
307
+ */
308
+ export function chatCompletionsToResponses(
309
+ chatRes: ChatResponse,
310
+ requestModel: string,
311
+ originalRequest: ResponsesRequestInput,
312
+ ): ResponsesResponse {
313
+ const choice = chatRes.choices[0];
314
+ const output: (
315
+ | ResponsesOutputMessage
316
+ | ResponsesOutputFunctionCall
317
+ | ResponsesOutputReasoning
318
+ )[] = [];
319
+
320
+ // Reasoning content → reasoning output item
321
+ if ((choice.message as any).reasoning_content) {
322
+ output.push({
323
+ type: "reasoning",
324
+ id: `rs_${crypto.randomBytes(16).toString("hex")}`,
325
+ summary: [
326
+ {
327
+ type: "summary_text",
328
+ text: (choice.message as any).reasoning_content,
329
+ },
330
+ ],
331
+ });
332
+ }
333
+
334
+ // Text content → message output item
335
+ if (choice.message.content) {
336
+ const msgId = generateMessageId();
337
+ output.push({
338
+ type: "message",
339
+ id: msgId,
340
+ role: "assistant",
341
+ status: "completed",
342
+ content: [
343
+ {
344
+ type: "output_text",
345
+ text: choice.message.content,
346
+ annotations: [],
347
+ },
348
+ ],
349
+ });
350
+ }
351
+
352
+ // Tool calls → function_call output items
353
+ if (choice.message.tool_calls) {
354
+ for (const tc of choice.message.tool_calls) {
355
+ output.push({
356
+ type: "function_call",
357
+ id: `fc_${crypto.randomBytes(12).toString("hex")}`,
358
+ call_id: tc.id,
359
+ name: tc.function.name,
360
+ arguments: tc.function.arguments,
361
+ status: "completed",
362
+ });
363
+ }
364
+ }
365
+
366
+ // Build usage — always include details (Grok/serde requires them)
367
+ const usage: ResponsesUsage = {
368
+ input_tokens: chatRes.usage.prompt_tokens,
369
+ output_tokens: chatRes.usage.completion_tokens,
370
+ total_tokens: chatRes.usage.total_tokens,
371
+ input_tokens_details: {
372
+ cached_tokens: (chatRes.usage as any).prompt_tokens_details?.cached_tokens ?? 0,
373
+ },
374
+ output_tokens_details: {
375
+ reasoning_tokens: (chatRes.usage as any).completion_tokens_details?.reasoning_tokens ?? 0,
376
+ },
377
+ ...(chatRes.usage.context_meter
378
+ ? { context_meter: chatRes.usage.context_meter }
379
+ : {}),
380
+ };
381
+
382
+ return {
383
+ id: generateResponseId(),
384
+ object: "response",
385
+ created_at: chatRes.created,
386
+ model: requestModel,
387
+ status: "completed",
388
+ output,
389
+ usage,
390
+ parallel_tool_calls: originalRequest.parallel_tool_calls,
391
+ tool_choice: originalRequest.tool_choice ?? undefined,
392
+ tools: originalRequest.tools ?? undefined,
393
+ temperature: originalRequest.temperature,
394
+ top_p: originalRequest.top_p,
395
+ max_output_tokens: originalRequest.max_output_tokens,
396
+ previous_response_id: originalRequest.previous_response_id || null,
397
+ last_response_id: originalRequest.previous_response_id || null,
398
+ metadata: originalRequest.metadata,
399
+ user: originalRequest.user,
400
+ error: null,
401
+ incomplete_details: null,
402
+ };
403
+ }
404
+
405
+ /**
406
+ * Build a minimal "in-progress" response for streaming initial event.
407
+ */
408
+ export function buildInProgressResponse(
409
+ responseId: string,
410
+ requestModel: string,
411
+ originalRequest: ResponsesRequestInput,
412
+ ): ResponsesResponse {
413
+ return {
414
+ id: responseId,
415
+ object: "response",
416
+ created_at: Math.floor(Date.now() / 1000),
417
+ model: requestModel,
418
+ status: "in_progress",
419
+ output: [],
420
+ usage: {
421
+ input_tokens: 0,
422
+ output_tokens: 0,
423
+ total_tokens: 0,
424
+ input_tokens_details: { cached_tokens: 0 },
425
+ output_tokens_details: { reasoning_tokens: 0 },
426
+ },
427
+ parallel_tool_calls: originalRequest.parallel_tool_calls,
428
+ tool_choice: originalRequest.tool_choice ?? undefined,
429
+ tools: originalRequest.tools ?? undefined,
430
+ temperature: originalRequest.temperature,
431
+ top_p: originalRequest.top_p,
432
+ max_output_tokens: originalRequest.max_output_tokens,
433
+ previous_response_id: originalRequest.previous_response_id || null,
434
+ last_response_id: originalRequest.previous_response_id || null,
435
+ metadata: originalRequest.metadata,
436
+ user: originalRequest.user,
437
+ error: null,
438
+ incomplete_details: null,
439
+ };
440
+ }
441
+
442
+ /**
443
+ * Finalize an in-progress response for the completed event.
444
+ */
445
+ export function finalizeResponse(
446
+ inProgress: ResponsesResponse,
447
+ output: (
448
+ | ResponsesOutputMessage
449
+ | ResponsesOutputFunctionCall
450
+ | ResponsesOutputReasoning
451
+ )[],
452
+ usage: ResponsesUsage,
453
+ ): ResponsesResponse {
454
+ return {
455
+ ...inProgress,
456
+ status: "completed",
457
+ output,
458
+ usage,
459
+ };
460
+ }
461
+
462
+ export type ChatHistoryMessage = ChatMessage;
463
+
464
+ /**
465
+ * Convert a Responses API output array into Chat Completions history messages.
466
+ */
467
+ export function responsesOutputToChatMessages(
468
+ output: (
469
+ | ResponsesOutputMessage
470
+ | ResponsesOutputFunctionCall
471
+ | ResponsesOutputReasoning
472
+ )[],
473
+ ): ChatMessage[] {
474
+ const messages: ChatMessage[] = [];
475
+ const toolCalls: ChatToolCall[] = [];
476
+ const textParts: string[] = [];
477
+
478
+ for (const item of output) {
479
+ if (item.type === "message") {
480
+ for (const part of item.content) {
481
+ if (part.type === "output_text" && part.text) {
482
+ textParts.push(part.text);
483
+ }
484
+ }
485
+ } else if (item.type === "function_call") {
486
+ toolCalls.push({
487
+ id: item.call_id,
488
+ type: "function",
489
+ function: { name: item.name, arguments: item.arguments || "{}" },
490
+ });
491
+ }
492
+ }
493
+
494
+ if (textParts.length > 0 || toolCalls.length > 0) {
495
+ messages.push({
496
+ role: "assistant",
497
+ content: textParts.length > 0 ? textParts.join("\n") : null,
498
+ ...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),
499
+ });
500
+ }
501
+
502
+ return messages;
503
+ }