qwenproxy-cli 1.0.0 → 1.0.1

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 (108) hide show
  1. package/LICENSE +14 -14
  2. package/README.md +906 -906
  3. package/bin/qwenproxy.js +5 -1
  4. package/package.json +78 -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 +2800 -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 +264 -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/utils/context-truncation.ts +84 -84
  105. package/src/utils/json.ts +380 -380
  106. package/src/utils/session-id.ts +37 -37
  107. package/src/utils/tool-call-guard.ts +84 -84
  108. package/src/utils/types.ts +109 -109
@@ -1,526 +1,526 @@
1
- import type { Context } from "hono";
2
- import type { OpenAIRequest, Message } from "../../utils/types.ts";
3
- import type { QwenFileEntry } from "../upload.ts";
4
- import { processImagesForQwen } from "../upload.ts";
5
- import { logger, isToolcallDebugEnabled } from "../../core/logger.js";
6
- import { config } from "../../core/config.ts";
7
- import { getBasicHeaders } from "../../services/auth-playwright.ts";
8
- import { buildToolInstructions } from "../../tools/instructions.ts";
9
- import {
10
- mapClientModelToQwen,
11
- stripThinkingSuffix,
12
- type ReasoningMode,
13
- } from "../../core/model-alias.ts";
14
-
15
- import {
16
- normalizeReasoningEffort,
17
- effortToReasoningMode,
18
- } from "../../core/reasoning-effort.ts";
19
-
20
- import { TOOL_CALL_OPEN, TOOL_CALL_CLOSE } from "../../tools/toolcall-tags.ts";
21
-
22
- export interface ParsedRequest {
23
- body: OpenAIRequest;
24
- isStream: boolean;
25
- conversationKey: string | null;
26
- hasExplicitConversationKey: boolean;
27
- systemPrompt: string;
28
- toolInstructions: string;
29
- prompt: string;
30
- currentPrompt: string;
31
- allFiles: QwenFileEntry[];
32
- currentFiles: QwenFileEntry[];
33
- shouldParseToolCalls: boolean;
34
- modelId: string;
35
- enableThinking: boolean;
36
- reasoningMode: ReasoningMode;
37
- messageCount: number;
38
- currentMessageCount: number;
39
- }
40
-
41
- export async function parseRequestBody(c: Context): Promise<ParsedRequest> {
42
- const body: OpenAIRequest = await c.req.json();
43
- logIncomingChatRequest(c, body);
44
- const isStream = body.stream ?? false;
45
- const conversationKey =
46
- typeof body.session_id === "string" && body.session_id.trim().length > 0
47
- ? body.session_id.trim()
48
- : typeof body.conversation_id === "string" &&
49
- body.conversation_id.trim().length > 0
50
- ? body.conversation_id.trim()
51
- : null;
52
-
53
- const messages = body.messages || [];
54
- let uploadHeaders: Record<string, string> | null = null;
55
-
56
- const {
57
- systemPromptParts,
58
- promptParts,
59
- currentPromptParts,
60
- allFiles,
61
- currentFiles,
62
- } = await buildPromptFromMessages(messages, uploadHeaders);
63
-
64
- const toolInstructions = injectToolInstructions(body);
65
- const shouldParseToolCalls = toolInstructions.length > 0;
66
-
67
- const systemPrompt = systemPromptParts.join("") + buildResponseFormatInstruction(body);
68
- const prompt = promptParts.join("");
69
- const currentPrompt = currentPromptParts.join("");
70
-
71
- // Thinking suffixes → base model + reasoning mode
72
- const { baseModel, enableThinking, reasoningMode } = stripThinkingSuffix(body.model);
73
- const modelId = mapClientModelToQwen(baseModel);
74
-
75
- // OpenAI `reasoning_effort` (none|minimal|low|medium|high|xhigh|max).
76
- // Precedence: an explicit model suffix wins — effort only acts on unsuffixed
77
- // models (reasoningMode "auto"). Absent/empty effort is a complete no-op.
78
- // - low/none/minimal → forces Fast (thinking off)
79
- // - medium → sets Auto (Qwen decides dynamically)
80
- // - high/max/xhigh → forces Thinking (thinking on)
81
- // The camelCase alias is accepted too: OpenCode-style configs overlay the
82
- // raw `reasoningEffort` setting into the request body.
83
- const rawEffort = body.reasoning_effort ?? body.reasoningEffort;
84
- const effortMode =
85
- reasoningMode === "auto"
86
- ? effortToReasoningMode(normalizeReasoningEffort(rawEffort))
87
- : undefined;
88
- const finalReasoningMode = effortMode ?? reasoningMode;
89
- const finalEnableThinking = effortMode === "fast" ? false : enableThinking;
90
-
91
- return {
92
- body,
93
- isStream,
94
- conversationKey,
95
- hasExplicitConversationKey: conversationKey !== null,
96
- systemPrompt,
97
- toolInstructions,
98
- prompt,
99
- currentPrompt,
100
- allFiles,
101
- currentFiles,
102
- shouldParseToolCalls,
103
- modelId,
104
- enableThinking: finalEnableThinking,
105
- reasoningMode: finalReasoningMode,
106
- messageCount: promptParts.length,
107
- currentMessageCount: currentPromptParts.length,
108
- };
109
- }
110
-
111
- async function buildPromptFromMessages(
112
- messages: Message[],
113
- uploadHeaders: Record<string, string> | null,
114
- ): Promise<{
115
- systemPromptParts: string[];
116
- promptParts: string[];
117
- currentPromptParts: string[];
118
- allFiles: QwenFileEntry[];
119
- currentFiles: QwenFileEntry[];
120
- }> {
121
- const promptParts: string[] = [];
122
- const currentPromptParts: string[] = [];
123
- const systemPromptParts: string[] = [];
124
- const toolCallNamesById = new Map<string, string>();
125
- const allFiles: QwenFileEntry[] = [];
126
- const currentFiles: QwenFileEntry[] = [];
127
- const currentStartIndex = getCurrentPromptStartIndex(messages);
128
-
129
- // Pre-build tool_call_id -> name mapping in O(n)
130
- for (const msg of messages) {
131
- if (
132
- msg.role === "assistant" &&
133
- msg.tool_calls &&
134
- Array.isArray(msg.tool_calls)
135
- ) {
136
- for (const tc of msg.tool_calls) {
137
- if (tc.id && tc.function?.name) {
138
- toolCallNamesById.set(tc.id, tc.function.name);
139
- }
140
- }
141
- }
142
- }
143
-
144
- for (let i = 0; i < messages.length; i++) {
145
- const msg = messages[i];
146
- let contentStr = "";
147
-
148
- if (Array.isArray(msg.content)) {
149
- const isCurrentMessage = i >= currentStartIndex;
150
- const imageParts = (msg.content as any[]).filter(
151
- (p: any) =>
152
- (p.type === "image_url" && p.image_url?.url) ||
153
- (p.type === "video_url" && p.video_url?.url) ||
154
- (p.type === "audio_url" && p.audio_url?.url) ||
155
- (p.type === "file_url" && p.file_url?.url),
156
- );
157
-
158
- if (imageParts.length > 0 && isCurrentMessage) {
159
- try {
160
- if (!uploadHeaders) {
161
- const { cookie, userAgent, bxV, bxUa, bxUmidtoken } =
162
- await getBasicHeaders();
163
- uploadHeaders = {
164
- cookie,
165
- "user-agent": userAgent,
166
- "bx-ua": bxUa,
167
- "bx-umidtoken": bxUmidtoken,
168
- "bx-v": bxV,
169
- };
170
- }
171
- const { text, files } = await processImagesForQwen(
172
- msg.content as any[],
173
- uploadHeaders,
174
- );
175
- contentStr = text;
176
- allFiles.push(...files);
177
- currentFiles.push(...files);
178
- } catch (err: unknown) {
179
- const errMsg = err instanceof Error ? err.message : "Unknown error";
180
- console.error("[Chat] Failed to process images:", errMsg);
181
- contentStr = (msg.content as any[])
182
- .filter((p: any) => p.type === "text")
183
- .map((p: any) => p.text)
184
- .join("\n");
185
- }
186
- } else {
187
- contentStr = (msg.content as any[])
188
- .filter((p: any) => p.type === "text")
189
- .map((p: any) => p.text)
190
- .join("\n");
191
- }
192
- } else if (typeof msg.content === "object" && msg.content !== null) {
193
- contentStr = JSON.stringify(msg.content);
194
- } else {
195
- contentStr = msg.content || "";
196
- }
197
-
198
- if (msg.role === "system") {
199
- systemPromptParts.push((contentStr || "") + "\n\n");
200
- } else if (msg.role === "user") {
201
- const segment = `User: ${contentStr || ""}\n\n`;
202
- promptParts.push(segment);
203
- if (i >= currentStartIndex) currentPromptParts.push(segment);
204
- } else if (msg.role === "assistant") {
205
- const assistantContentParts: string[] = [];
206
- const reasoning = (msg as any).reasoning_content;
207
- if (reasoning) {
208
- assistantContentParts.push(reasoning + "\n");
209
- }
210
- if (contentStr) {
211
- assistantContentParts.push(contentStr);
212
- }
213
- if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
214
- if (isToolcallDebugEnabled()) {
215
- logger.debug("[chat] processing assistant tool_calls in history", {
216
- messageIndex: i,
217
- toolCallsCount: msg.tool_calls.length,
218
- toolCallNames: msg.tool_calls.map((tc: any) => tc.function?.name),
219
- });
220
- }
221
- for (const tc of msg.tool_calls) {
222
- const args = tc.function?.arguments;
223
- let parsedArgs: any = {};
224
- if (typeof args === "string") {
225
- try {
226
- parsedArgs = JSON.parse(args);
227
- } catch (parseErr) {
228
- // Malformed JSON: preserve raw string for model visibility
229
- logger.warn("[chat] Failed to parse tool_call arguments", {
230
- toolCallId: tc.id,
231
- toolName: tc.function?.name,
232
- error: parseErr instanceof Error ? parseErr.message : "Unknown",
233
- rawArgs: args.substring(0, 200),
234
- });
235
- parsedArgs = { _raw: args };
236
- }
237
- } else if (args && typeof args === "object") {
238
- parsedArgs = args;
239
- }
240
- const payload = {
241
- name: tc.function?.name,
242
- arguments: parsedArgs,
243
- };
244
- const toolCallStr =
245
- "\n" +
246
- TOOL_CALL_OPEN +
247
- "\n" +
248
- JSON.stringify(payload) +
249
- "\n" +
250
- TOOL_CALL_CLOSE;
251
- assistantContentParts.push(
252
- assistantContentParts.length > 0 ? toolCallStr : toolCallStr.trim(),
253
- );
254
-
255
- if (isToolcallDebugEnabled()) {
256
- logger.debug("[chat] tool_call serialized to prompt", {
257
- toolName: tc.function?.name,
258
- toolCallId: tc.id,
259
- argsKeys: Object.keys(parsedArgs),
260
- });
261
- }
262
- }
263
- }
264
- const assistantContent = assistantContentParts.join("");
265
- const segment = `Assistant: ${assistantContent.trim()}\n\n`;
266
- promptParts.push(segment);
267
- if (i >= currentStartIndex) currentPromptParts.push(segment);
268
- } else if (msg.role === "tool" || msg.role === "function") {
269
- let toolName =
270
- msg.name ||
271
- (msg.tool_call_id
272
- ? toolCallNamesById.get(msg.tool_call_id)
273
- : undefined);
274
- if (isToolcallDebugEnabled()) {
275
- logger.debug("[chat] processing tool response in history", {
276
- messageIndex: i,
277
- toolName,
278
- toolCallId: msg.tool_call_id,
279
- contentLength: contentStr.length,
280
- contentPreview: contentStr.substring(0, 200),
281
- });
282
- }
283
- const segment = `Tool Response (${toolName || "tool"}): ${contentStr || ""}\n\n`;
284
- promptParts.push(segment);
285
- if (i >= currentStartIndex) currentPromptParts.push(segment);
286
- }
287
- }
288
-
289
- return {
290
- systemPromptParts,
291
- promptParts,
292
- currentPromptParts,
293
- allFiles,
294
- currentFiles,
295
- };
296
- }
297
-
298
- function previewText(value: unknown, max = 220): string {
299
- let text = "";
300
- if (typeof value === "string") {
301
- text = value;
302
- } else if (Array.isArray(value)) {
303
- text = value
304
- .map((part: any) => {
305
- if (part?.type === "text") return part.text || "";
306
- if (part?.type) return `[${part.type}]`;
307
- return JSON.stringify(part);
308
- })
309
- .join(" ");
310
- } else if (value !== null && value !== undefined) {
311
- text = JSON.stringify(value);
312
- }
313
-
314
- text = text.replace(/\s+/g, " ").trim();
315
- return text.length > max ? `${text.slice(0, max)}…` : text;
316
- }
317
-
318
- function contentLength(value: unknown): number {
319
- if (typeof value === "string") return value.length;
320
- if (Array.isArray(value)) return value.length;
321
- if (value !== null && value !== undefined)
322
- return JSON.stringify(value).length;
323
- return 0;
324
- }
325
-
326
- function logIncomingChatRequest(c: Context, body: OpenAIRequest): void {
327
- const messages = Array.isArray(body.messages) ? body.messages : [];
328
- const tools = Array.isArray((body as any).tools) ? (body as any).tools : [];
329
- const requestId = c.req.header("x-request-id") || null;
330
- const toolChoice = (body as any).tool_choice || null;
331
-
332
- // Full request debug
333
- if (process.env.REQUEST_DEBUG === "true") {
334
- const bodyStr = JSON.stringify(body);
335
- console.log(
336
- `[Request] Full body | ${bodyStr.length} chars | ${bodyStr.substring(0, 2000)}`,
337
- );
338
- if (tools.length > 0) {
339
- console.log(
340
- `[Request] Tools | ${tools.length} definitions | ${JSON.stringify(tools).length} chars`,
341
- );
342
- }
343
- // Log each message role and content preview
344
- messages.forEach((msg: any, i: number) => {
345
- const content =
346
- typeof msg.content === "string"
347
- ? msg.content
348
- : JSON.stringify(msg.content);
349
- const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0;
350
- console.log(
351
- `[Request] Message ${i} | role=${msg.role} | ${content.length} chars${hasToolCalls ? " | tool_calls=" + msg.tool_calls.length : ""}${msg.tool_call_id ? " | tool_call_id=" + msg.tool_call_id : ""} | preview=${content.substring(0, 100)}`,
352
- );
353
- });
354
- }
355
-
356
- // The debug payload below scans every message (regex previews + JSON
357
- // stringify), so skip building it unless it will actually be logged.
358
- if (!config.logging.chatRequests || !logger.isLevelEnabled("debug")) return;
359
-
360
- const last = messages[messages.length - 1];
361
- const firstUser = messages.find((msg) => msg.role === "user");
362
-
363
- logger.debug("[chat] request details", {
364
- requestId,
365
- userAgent: c.req.header("user-agent") || null,
366
- model: body.model,
367
- stream: body.stream ?? false,
368
- conversationId: body.conversation_id || null,
369
- sessionId: body.session_id || null,
370
- user: body.user || null,
371
- messagesCount: messages.length,
372
- toolsCount: tools.length,
373
- toolChoice,
374
- roles: messages.map((msg) => msg.role),
375
- firstUserPreview: firstUser ? previewText(firstUser.content) : null,
376
- lastRole: last?.role || null,
377
- lastPreview: last ? previewText(last.content) : null,
378
- messageShape: messages.map((msg, index) => ({
379
- index,
380
- role: msg.role,
381
- contentType: Array.isArray(msg.content) ? "array" : typeof msg.content,
382
- contentLength: contentLength(msg.content),
383
- hasToolCalls: Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0,
384
- toolCallCount: Array.isArray(msg.tool_calls) ? msg.tool_calls.length : 0,
385
- toolCallId: msg.tool_call_id || null,
386
- name: msg.name || null,
387
- preview: previewText(msg.content, 140),
388
- })),
389
- });
390
- }
391
-
392
- function getCurrentPromptStartIndex(messages: Message[]): number {
393
- if (messages.length === 0) return 0;
394
-
395
- let last = messages.length - 1;
396
- while (last >= 0 && messages[last].role === "system") last--;
397
- if (last < 0) return messages.length;
398
-
399
- const lastRole = messages[last].role;
400
- if (lastRole === "user") {
401
- // Check if there's a tool response right before this user message
402
- // If so, include it in the current prompt
403
- let toolStart = last - 1;
404
- while (
405
- toolStart >= 0 &&
406
- (messages[toolStart].role === "tool" ||
407
- messages[toolStart].role === "function")
408
- ) {
409
- toolStart--;
410
- }
411
- // If we found tool messages, also include the assistant message that made the tool calls
412
- if (toolStart < last - 1) {
413
- // toolStart points to the message before the first tool message
414
- // Check if it's an assistant message with tool_calls
415
- if (
416
- toolStart >= 0 &&
417
- messages[toolStart].role === "assistant" &&
418
- messages[toolStart].tool_calls &&
419
- messages[toolStart].tool_calls!.length > 0
420
- ) {
421
- return toolStart;
422
- }
423
- return toolStart + 1;
424
- }
425
- return last;
426
- }
427
-
428
- if (lastRole === "tool" || lastRole === "function") {
429
- let firstTrailingTool = last;
430
- while (
431
- firstTrailingTool - 1 >= 0 &&
432
- (messages[firstTrailingTool - 1].role === "tool" ||
433
- messages[firstTrailingTool - 1].role === "function")
434
- ) {
435
- firstTrailingTool--;
436
- }
437
- return firstTrailingTool;
438
- }
439
-
440
- return last;
441
- }
442
-
443
- // Structured outputs (doc §2.1 / checklist item 6): OpenAI's response_format
444
- // is not natively supported by the Qwen web API, so the schema/JSON-mode
445
- // constraint is enforced at the prompt level — the same approach as tools.
446
- // json_object demands a bare JSON object; json_schema embeds the schema with a
447
- // strict-mode note. Text mode (default) returns nothing and changes nothing.
448
- function buildResponseFormatInstruction(body: OpenAIRequest): string {
449
- const bodyAny = body as any;
450
- const rf = bodyAny?.response_format;
451
- if (!rf || typeof rf !== "object") return "";
452
-
453
- if (rf.type === "json_object") {
454
- return (
455
- "\n\n[OUTPUT FORMAT]\n" +
456
- "Respond with a single valid JSON object only. " +
457
- "Do not wrap it in markdown code fences and do not add any text " +
458
- "before or after the JSON object."
459
- );
460
- }
461
-
462
- if (rf.type === "json_schema" && rf.json_schema?.schema) {
463
- const { name, description, schema, strict } = rf.json_schema;
464
- const strictNote =
465
- strict === true
466
- ? "The output MUST strictly conform to the schema: every required property present and no extra properties.\n"
467
- : "The output MUST conform to the following JSON schema.\n";
468
- return (
469
- "\n\n[OUTPUT FORMAT]\n" +
470
- (name ? `Output schema name: ${name}\n` : "") +
471
- (description ? `Schema description: ${description}\n` : "") +
472
- strictNote +
473
- "Respond with a single JSON object that matches this schema. " +
474
- "Do not wrap it in markdown code fences and do not add any text " +
475
- "before or after the JSON object.\n\n" +
476
- `Schema:\n${JSON.stringify(schema, null, 2)}`
477
- );
478
- }
479
-
480
- return "";
481
- }
482
-
483
- function injectToolInstructions(body: OpenAIRequest): string {
484
- const bodyAny = body as any;
485
- const declaredTools = Array.isArray(bodyAny.tools) ? bodyAny.tools : [];
486
- const shouldParseToolCalls = declaredTools.length > 0;
487
-
488
- if (!shouldParseToolCalls) return "";
489
-
490
- if (isToolcallDebugEnabled()) {
491
- logger.debug("[chat] tools provided in request", {
492
- toolsCount: declaredTools.length,
493
- toolNames: declaredTools.map((t: any) =>
494
- t.type === "function" ? t.function?.name : t.name,
495
- ),
496
- toolChoice: bodyAny.tool_choice || "none",
497
- });
498
- }
499
-
500
- const formattedTools = declaredTools.map((t: any) => {
501
- if (t.type === "function") {
502
- return {
503
- name: t.function.name,
504
- description: t.function.description || "",
505
- parameters: t.function.parameters,
506
- };
507
- }
508
- return t;
509
- });
510
- const toolsJson = JSON.stringify(formattedTools, null, 2);
511
-
512
- const instructions = buildToolInstructions(toolsJson, bodyAny.tool_choice);
513
-
514
- if (
515
- isToolcallDebugEnabled() &&
516
- bodyAny.tool_choice &&
517
- typeof bodyAny.tool_choice === "object" &&
518
- bodyAny.tool_choice.function
519
- ) {
520
- logger.debug("[chat] forced tool_choice", {
521
- forcedTool: bodyAny.tool_choice.function.name,
522
- });
523
- }
524
-
525
- return instructions;
526
- }
1
+ import type { Context } from "hono";
2
+ import type { OpenAIRequest, Message } from "../../utils/types.ts";
3
+ import type { QwenFileEntry } from "../upload.ts";
4
+ import { processImagesForQwen } from "../upload.ts";
5
+ import { logger, isToolcallDebugEnabled } from "../../core/logger.js";
6
+ import { config } from "../../core/config.ts";
7
+ import { getBasicHeaders } from "../../services/auth-playwright.ts";
8
+ import { buildToolInstructions } from "../../tools/instructions.ts";
9
+ import {
10
+ mapClientModelToQwen,
11
+ stripThinkingSuffix,
12
+ type ReasoningMode,
13
+ } from "../../core/model-alias.ts";
14
+
15
+ import {
16
+ normalizeReasoningEffort,
17
+ effortToReasoningMode,
18
+ } from "../../core/reasoning-effort.ts";
19
+
20
+ import { TOOL_CALL_OPEN, TOOL_CALL_CLOSE } from "../../tools/toolcall-tags.ts";
21
+
22
+ export interface ParsedRequest {
23
+ body: OpenAIRequest;
24
+ isStream: boolean;
25
+ conversationKey: string | null;
26
+ hasExplicitConversationKey: boolean;
27
+ systemPrompt: string;
28
+ toolInstructions: string;
29
+ prompt: string;
30
+ currentPrompt: string;
31
+ allFiles: QwenFileEntry[];
32
+ currentFiles: QwenFileEntry[];
33
+ shouldParseToolCalls: boolean;
34
+ modelId: string;
35
+ enableThinking: boolean;
36
+ reasoningMode: ReasoningMode;
37
+ messageCount: number;
38
+ currentMessageCount: number;
39
+ }
40
+
41
+ export async function parseRequestBody(c: Context): Promise<ParsedRequest> {
42
+ const body: OpenAIRequest = await c.req.json();
43
+ logIncomingChatRequest(c, body);
44
+ const isStream = body.stream ?? false;
45
+ const conversationKey =
46
+ typeof body.session_id === "string" && body.session_id.trim().length > 0
47
+ ? body.session_id.trim()
48
+ : typeof body.conversation_id === "string" &&
49
+ body.conversation_id.trim().length > 0
50
+ ? body.conversation_id.trim()
51
+ : null;
52
+
53
+ const messages = body.messages || [];
54
+ let uploadHeaders: Record<string, string> | null = null;
55
+
56
+ const {
57
+ systemPromptParts,
58
+ promptParts,
59
+ currentPromptParts,
60
+ allFiles,
61
+ currentFiles,
62
+ } = await buildPromptFromMessages(messages, uploadHeaders);
63
+
64
+ const toolInstructions = injectToolInstructions(body);
65
+ const shouldParseToolCalls = toolInstructions.length > 0;
66
+
67
+ const systemPrompt = systemPromptParts.join("") + buildResponseFormatInstruction(body);
68
+ const prompt = promptParts.join("");
69
+ const currentPrompt = currentPromptParts.join("");
70
+
71
+ // Thinking suffixes → base model + reasoning mode
72
+ const { baseModel, enableThinking, reasoningMode } = stripThinkingSuffix(body.model);
73
+ const modelId = mapClientModelToQwen(baseModel);
74
+
75
+ // OpenAI `reasoning_effort` (none|minimal|low|medium|high|xhigh|max).
76
+ // Precedence: an explicit model suffix wins — effort only acts on unsuffixed
77
+ // models (reasoningMode "auto"). Absent/empty effort is a complete no-op.
78
+ // - low/none/minimal → forces Fast (thinking off)
79
+ // - medium → sets Auto (Qwen decides dynamically)
80
+ // - high/max/xhigh → forces Thinking (thinking on)
81
+ // The camelCase alias is accepted too: OpenCode-style configs overlay the
82
+ // raw `reasoningEffort` setting into the request body.
83
+ const rawEffort = body.reasoning_effort ?? body.reasoningEffort;
84
+ const effortMode =
85
+ reasoningMode === "auto"
86
+ ? effortToReasoningMode(normalizeReasoningEffort(rawEffort))
87
+ : undefined;
88
+ const finalReasoningMode = effortMode ?? reasoningMode;
89
+ const finalEnableThinking = effortMode === "fast" ? false : enableThinking;
90
+
91
+ return {
92
+ body,
93
+ isStream,
94
+ conversationKey,
95
+ hasExplicitConversationKey: conversationKey !== null,
96
+ systemPrompt,
97
+ toolInstructions,
98
+ prompt,
99
+ currentPrompt,
100
+ allFiles,
101
+ currentFiles,
102
+ shouldParseToolCalls,
103
+ modelId,
104
+ enableThinking: finalEnableThinking,
105
+ reasoningMode: finalReasoningMode,
106
+ messageCount: promptParts.length,
107
+ currentMessageCount: currentPromptParts.length,
108
+ };
109
+ }
110
+
111
+ async function buildPromptFromMessages(
112
+ messages: Message[],
113
+ uploadHeaders: Record<string, string> | null,
114
+ ): Promise<{
115
+ systemPromptParts: string[];
116
+ promptParts: string[];
117
+ currentPromptParts: string[];
118
+ allFiles: QwenFileEntry[];
119
+ currentFiles: QwenFileEntry[];
120
+ }> {
121
+ const promptParts: string[] = [];
122
+ const currentPromptParts: string[] = [];
123
+ const systemPromptParts: string[] = [];
124
+ const toolCallNamesById = new Map<string, string>();
125
+ const allFiles: QwenFileEntry[] = [];
126
+ const currentFiles: QwenFileEntry[] = [];
127
+ const currentStartIndex = getCurrentPromptStartIndex(messages);
128
+
129
+ // Pre-build tool_call_id -> name mapping in O(n)
130
+ for (const msg of messages) {
131
+ if (
132
+ msg.role === "assistant" &&
133
+ msg.tool_calls &&
134
+ Array.isArray(msg.tool_calls)
135
+ ) {
136
+ for (const tc of msg.tool_calls) {
137
+ if (tc.id && tc.function?.name) {
138
+ toolCallNamesById.set(tc.id, tc.function.name);
139
+ }
140
+ }
141
+ }
142
+ }
143
+
144
+ for (let i = 0; i < messages.length; i++) {
145
+ const msg = messages[i];
146
+ let contentStr = "";
147
+
148
+ if (Array.isArray(msg.content)) {
149
+ const isCurrentMessage = i >= currentStartIndex;
150
+ const imageParts = (msg.content as any[]).filter(
151
+ (p: any) =>
152
+ (p.type === "image_url" && p.image_url?.url) ||
153
+ (p.type === "video_url" && p.video_url?.url) ||
154
+ (p.type === "audio_url" && p.audio_url?.url) ||
155
+ (p.type === "file_url" && p.file_url?.url),
156
+ );
157
+
158
+ if (imageParts.length > 0 && isCurrentMessage) {
159
+ try {
160
+ if (!uploadHeaders) {
161
+ const { cookie, userAgent, bxV, bxUa, bxUmidtoken } =
162
+ await getBasicHeaders();
163
+ uploadHeaders = {
164
+ cookie,
165
+ "user-agent": userAgent,
166
+ "bx-ua": bxUa,
167
+ "bx-umidtoken": bxUmidtoken,
168
+ "bx-v": bxV,
169
+ };
170
+ }
171
+ const { text, files } = await processImagesForQwen(
172
+ msg.content as any[],
173
+ uploadHeaders,
174
+ );
175
+ contentStr = text;
176
+ allFiles.push(...files);
177
+ currentFiles.push(...files);
178
+ } catch (err: unknown) {
179
+ const errMsg = err instanceof Error ? err.message : "Unknown error";
180
+ console.error("[Chat] Failed to process images:", errMsg);
181
+ contentStr = (msg.content as any[])
182
+ .filter((p: any) => p.type === "text")
183
+ .map((p: any) => p.text)
184
+ .join("\n");
185
+ }
186
+ } else {
187
+ contentStr = (msg.content as any[])
188
+ .filter((p: any) => p.type === "text")
189
+ .map((p: any) => p.text)
190
+ .join("\n");
191
+ }
192
+ } else if (typeof msg.content === "object" && msg.content !== null) {
193
+ contentStr = JSON.stringify(msg.content);
194
+ } else {
195
+ contentStr = msg.content || "";
196
+ }
197
+
198
+ if (msg.role === "system") {
199
+ systemPromptParts.push((contentStr || "") + "\n\n");
200
+ } else if (msg.role === "user") {
201
+ const segment = `User: ${contentStr || ""}\n\n`;
202
+ promptParts.push(segment);
203
+ if (i >= currentStartIndex) currentPromptParts.push(segment);
204
+ } else if (msg.role === "assistant") {
205
+ const assistantContentParts: string[] = [];
206
+ const reasoning = (msg as any).reasoning_content;
207
+ if (reasoning) {
208
+ assistantContentParts.push(reasoning + "\n");
209
+ }
210
+ if (contentStr) {
211
+ assistantContentParts.push(contentStr);
212
+ }
213
+ if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
214
+ if (isToolcallDebugEnabled()) {
215
+ logger.debug("[chat] processing assistant tool_calls in history", {
216
+ messageIndex: i,
217
+ toolCallsCount: msg.tool_calls.length,
218
+ toolCallNames: msg.tool_calls.map((tc: any) => tc.function?.name),
219
+ });
220
+ }
221
+ for (const tc of msg.tool_calls) {
222
+ const args = tc.function?.arguments;
223
+ let parsedArgs: any = {};
224
+ if (typeof args === "string") {
225
+ try {
226
+ parsedArgs = JSON.parse(args);
227
+ } catch (parseErr) {
228
+ // Malformed JSON: preserve raw string for model visibility
229
+ logger.warn("[chat] Failed to parse tool_call arguments", {
230
+ toolCallId: tc.id,
231
+ toolName: tc.function?.name,
232
+ error: parseErr instanceof Error ? parseErr.message : "Unknown",
233
+ rawArgs: args.substring(0, 200),
234
+ });
235
+ parsedArgs = { _raw: args };
236
+ }
237
+ } else if (args && typeof args === "object") {
238
+ parsedArgs = args;
239
+ }
240
+ const payload = {
241
+ name: tc.function?.name,
242
+ arguments: parsedArgs,
243
+ };
244
+ const toolCallStr =
245
+ "\n" +
246
+ TOOL_CALL_OPEN +
247
+ "\n" +
248
+ JSON.stringify(payload) +
249
+ "\n" +
250
+ TOOL_CALL_CLOSE;
251
+ assistantContentParts.push(
252
+ assistantContentParts.length > 0 ? toolCallStr : toolCallStr.trim(),
253
+ );
254
+
255
+ if (isToolcallDebugEnabled()) {
256
+ logger.debug("[chat] tool_call serialized to prompt", {
257
+ toolName: tc.function?.name,
258
+ toolCallId: tc.id,
259
+ argsKeys: Object.keys(parsedArgs),
260
+ });
261
+ }
262
+ }
263
+ }
264
+ const assistantContent = assistantContentParts.join("");
265
+ const segment = `Assistant: ${assistantContent.trim()}\n\n`;
266
+ promptParts.push(segment);
267
+ if (i >= currentStartIndex) currentPromptParts.push(segment);
268
+ } else if (msg.role === "tool" || msg.role === "function") {
269
+ let toolName =
270
+ msg.name ||
271
+ (msg.tool_call_id
272
+ ? toolCallNamesById.get(msg.tool_call_id)
273
+ : undefined);
274
+ if (isToolcallDebugEnabled()) {
275
+ logger.debug("[chat] processing tool response in history", {
276
+ messageIndex: i,
277
+ toolName,
278
+ toolCallId: msg.tool_call_id,
279
+ contentLength: contentStr.length,
280
+ contentPreview: contentStr.substring(0, 200),
281
+ });
282
+ }
283
+ const segment = `Tool Response (${toolName || "tool"}): ${contentStr || ""}\n\n`;
284
+ promptParts.push(segment);
285
+ if (i >= currentStartIndex) currentPromptParts.push(segment);
286
+ }
287
+ }
288
+
289
+ return {
290
+ systemPromptParts,
291
+ promptParts,
292
+ currentPromptParts,
293
+ allFiles,
294
+ currentFiles,
295
+ };
296
+ }
297
+
298
+ function previewText(value: unknown, max = 220): string {
299
+ let text = "";
300
+ if (typeof value === "string") {
301
+ text = value;
302
+ } else if (Array.isArray(value)) {
303
+ text = value
304
+ .map((part: any) => {
305
+ if (part?.type === "text") return part.text || "";
306
+ if (part?.type) return `[${part.type}]`;
307
+ return JSON.stringify(part);
308
+ })
309
+ .join(" ");
310
+ } else if (value !== null && value !== undefined) {
311
+ text = JSON.stringify(value);
312
+ }
313
+
314
+ text = text.replace(/\s+/g, " ").trim();
315
+ return text.length > max ? `${text.slice(0, max)}…` : text;
316
+ }
317
+
318
+ function contentLength(value: unknown): number {
319
+ if (typeof value === "string") return value.length;
320
+ if (Array.isArray(value)) return value.length;
321
+ if (value !== null && value !== undefined)
322
+ return JSON.stringify(value).length;
323
+ return 0;
324
+ }
325
+
326
+ function logIncomingChatRequest(c: Context, body: OpenAIRequest): void {
327
+ const messages = Array.isArray(body.messages) ? body.messages : [];
328
+ const tools = Array.isArray((body as any).tools) ? (body as any).tools : [];
329
+ const requestId = c.req.header("x-request-id") || null;
330
+ const toolChoice = (body as any).tool_choice || null;
331
+
332
+ // Full request debug
333
+ if (process.env.REQUEST_DEBUG === "true") {
334
+ const bodyStr = JSON.stringify(body);
335
+ console.log(
336
+ `[Request] Full body | ${bodyStr.length} chars | ${bodyStr.substring(0, 2000)}`,
337
+ );
338
+ if (tools.length > 0) {
339
+ console.log(
340
+ `[Request] Tools | ${tools.length} definitions | ${JSON.stringify(tools).length} chars`,
341
+ );
342
+ }
343
+ // Log each message role and content preview
344
+ messages.forEach((msg: any, i: number) => {
345
+ const content =
346
+ typeof msg.content === "string"
347
+ ? msg.content
348
+ : JSON.stringify(msg.content);
349
+ const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0;
350
+ console.log(
351
+ `[Request] Message ${i} | role=${msg.role} | ${content.length} chars${hasToolCalls ? " | tool_calls=" + msg.tool_calls.length : ""}${msg.tool_call_id ? " | tool_call_id=" + msg.tool_call_id : ""} | preview=${content.substring(0, 100)}`,
352
+ );
353
+ });
354
+ }
355
+
356
+ // The debug payload below scans every message (regex previews + JSON
357
+ // stringify), so skip building it unless it will actually be logged.
358
+ if (!config.logging.chatRequests || !logger.isLevelEnabled("debug")) return;
359
+
360
+ const last = messages[messages.length - 1];
361
+ const firstUser = messages.find((msg) => msg.role === "user");
362
+
363
+ logger.debug("[chat] request details", {
364
+ requestId,
365
+ userAgent: c.req.header("user-agent") || null,
366
+ model: body.model,
367
+ stream: body.stream ?? false,
368
+ conversationId: body.conversation_id || null,
369
+ sessionId: body.session_id || null,
370
+ user: body.user || null,
371
+ messagesCount: messages.length,
372
+ toolsCount: tools.length,
373
+ toolChoice,
374
+ roles: messages.map((msg) => msg.role),
375
+ firstUserPreview: firstUser ? previewText(firstUser.content) : null,
376
+ lastRole: last?.role || null,
377
+ lastPreview: last ? previewText(last.content) : null,
378
+ messageShape: messages.map((msg, index) => ({
379
+ index,
380
+ role: msg.role,
381
+ contentType: Array.isArray(msg.content) ? "array" : typeof msg.content,
382
+ contentLength: contentLength(msg.content),
383
+ hasToolCalls: Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0,
384
+ toolCallCount: Array.isArray(msg.tool_calls) ? msg.tool_calls.length : 0,
385
+ toolCallId: msg.tool_call_id || null,
386
+ name: msg.name || null,
387
+ preview: previewText(msg.content, 140),
388
+ })),
389
+ });
390
+ }
391
+
392
+ function getCurrentPromptStartIndex(messages: Message[]): number {
393
+ if (messages.length === 0) return 0;
394
+
395
+ let last = messages.length - 1;
396
+ while (last >= 0 && messages[last].role === "system") last--;
397
+ if (last < 0) return messages.length;
398
+
399
+ const lastRole = messages[last].role;
400
+ if (lastRole === "user") {
401
+ // Check if there's a tool response right before this user message
402
+ // If so, include it in the current prompt
403
+ let toolStart = last - 1;
404
+ while (
405
+ toolStart >= 0 &&
406
+ (messages[toolStart].role === "tool" ||
407
+ messages[toolStart].role === "function")
408
+ ) {
409
+ toolStart--;
410
+ }
411
+ // If we found tool messages, also include the assistant message that made the tool calls
412
+ if (toolStart < last - 1) {
413
+ // toolStart points to the message before the first tool message
414
+ // Check if it's an assistant message with tool_calls
415
+ if (
416
+ toolStart >= 0 &&
417
+ messages[toolStart].role === "assistant" &&
418
+ messages[toolStart].tool_calls &&
419
+ messages[toolStart].tool_calls!.length > 0
420
+ ) {
421
+ return toolStart;
422
+ }
423
+ return toolStart + 1;
424
+ }
425
+ return last;
426
+ }
427
+
428
+ if (lastRole === "tool" || lastRole === "function") {
429
+ let firstTrailingTool = last;
430
+ while (
431
+ firstTrailingTool - 1 >= 0 &&
432
+ (messages[firstTrailingTool - 1].role === "tool" ||
433
+ messages[firstTrailingTool - 1].role === "function")
434
+ ) {
435
+ firstTrailingTool--;
436
+ }
437
+ return firstTrailingTool;
438
+ }
439
+
440
+ return last;
441
+ }
442
+
443
+ // Structured outputs (doc §2.1 / checklist item 6): OpenAI's response_format
444
+ // is not natively supported by the Qwen web API, so the schema/JSON-mode
445
+ // constraint is enforced at the prompt level — the same approach as tools.
446
+ // json_object demands a bare JSON object; json_schema embeds the schema with a
447
+ // strict-mode note. Text mode (default) returns nothing and changes nothing.
448
+ function buildResponseFormatInstruction(body: OpenAIRequest): string {
449
+ const bodyAny = body as any;
450
+ const rf = bodyAny?.response_format;
451
+ if (!rf || typeof rf !== "object") return "";
452
+
453
+ if (rf.type === "json_object") {
454
+ return (
455
+ "\n\n[OUTPUT FORMAT]\n" +
456
+ "Respond with a single valid JSON object only. " +
457
+ "Do not wrap it in markdown code fences and do not add any text " +
458
+ "before or after the JSON object."
459
+ );
460
+ }
461
+
462
+ if (rf.type === "json_schema" && rf.json_schema?.schema) {
463
+ const { name, description, schema, strict } = rf.json_schema;
464
+ const strictNote =
465
+ strict === true
466
+ ? "The output MUST strictly conform to the schema: every required property present and no extra properties.\n"
467
+ : "The output MUST conform to the following JSON schema.\n";
468
+ return (
469
+ "\n\n[OUTPUT FORMAT]\n" +
470
+ (name ? `Output schema name: ${name}\n` : "") +
471
+ (description ? `Schema description: ${description}\n` : "") +
472
+ strictNote +
473
+ "Respond with a single JSON object that matches this schema. " +
474
+ "Do not wrap it in markdown code fences and do not add any text " +
475
+ "before or after the JSON object.\n\n" +
476
+ `Schema:\n${JSON.stringify(schema, null, 2)}`
477
+ );
478
+ }
479
+
480
+ return "";
481
+ }
482
+
483
+ function injectToolInstructions(body: OpenAIRequest): string {
484
+ const bodyAny = body as any;
485
+ const declaredTools = Array.isArray(bodyAny.tools) ? bodyAny.tools : [];
486
+ const shouldParseToolCalls = declaredTools.length > 0;
487
+
488
+ if (!shouldParseToolCalls) return "";
489
+
490
+ if (isToolcallDebugEnabled()) {
491
+ logger.debug("[chat] tools provided in request", {
492
+ toolsCount: declaredTools.length,
493
+ toolNames: declaredTools.map((t: any) =>
494
+ t.type === "function" ? t.function?.name : t.name,
495
+ ),
496
+ toolChoice: bodyAny.tool_choice || "none",
497
+ });
498
+ }
499
+
500
+ const formattedTools = declaredTools.map((t: any) => {
501
+ if (t.type === "function") {
502
+ return {
503
+ name: t.function.name,
504
+ description: t.function.description || "",
505
+ parameters: t.function.parameters,
506
+ };
507
+ }
508
+ return t;
509
+ });
510
+ const toolsJson = JSON.stringify(formattedTools, null, 2);
511
+
512
+ const instructions = buildToolInstructions(toolsJson, bodyAny.tool_choice);
513
+
514
+ if (
515
+ isToolcallDebugEnabled() &&
516
+ bodyAny.tool_choice &&
517
+ typeof bodyAny.tool_choice === "object" &&
518
+ bodyAny.tool_choice.function
519
+ ) {
520
+ logger.debug("[chat] forced tool_choice", {
521
+ forcedTool: bodyAny.tool_choice.function.name,
522
+ });
523
+ }
524
+
525
+ return instructions;
526
+ }