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,544 +1,544 @@
1
- const defaultContextWindow = 1_048_576;
2
- const defaultMaxOutputTokens = 65536;
3
- const defaultMaxThinkingTokens = 16384;
4
- export const MAX_PAYLOAD_SIZE = 50 * 1024 * 1024;
5
-
6
- /**
7
- * Model metadata exposed by Qwen's live `/api/models` catalog.
8
- *
9
- * The registry deliberately has no model-name table. Qwen can add, remove or
10
- * change models without requiring a QwenProxy release. The values below are
11
- * conservative fallbacks used only until the selected account's catalog has
12
- * been synchronized.
13
- */
14
- export interface ModelCapabilities {
15
- maxOutputTokens: number;
16
- maxThinkingTokens: number;
17
- supportsThinking: boolean;
18
- supportsVision: boolean;
19
- canSkipThinking: boolean;
20
- supportsDocument: boolean;
21
- supportsAudio: boolean;
22
- supportsVideo: boolean;
23
- supportsCitations: boolean;
24
- supportsCodeExecution: boolean;
25
- supportsStructuredOutputs: boolean;
26
- modalities: string[];
27
- chatTypes: string[];
28
- mcp: string[];
29
- isActive: boolean;
30
- }
31
-
32
- export interface RegisteredModelMetadata {
33
- id: string;
34
- contextWindow?: number;
35
- capabilities: ModelCapabilities;
36
- raw: Record<string, unknown>;
37
- }
38
-
39
- type JsonRecord = Record<string, unknown>;
40
-
41
- const defaultCapabilities: ModelCapabilities = {
42
- maxOutputTokens: defaultMaxOutputTokens,
43
- maxThinkingTokens: defaultMaxThinkingTokens,
44
- supportsThinking: false,
45
- supportsVision: false,
46
- canSkipThinking: false,
47
- supportsDocument: false,
48
- supportsAudio: false,
49
- supportsVideo: false,
50
- supportsCitations: false,
51
- supportsCodeExecution: false,
52
- supportsStructuredOutputs: false,
53
- modalities: ["text"],
54
- chatTypes: [],
55
- mcp: [],
56
- isActive: true,
57
- };
58
-
59
- interface RegistryEntry {
60
- contextWindow?: number;
61
- capabilities: ModelCapabilities;
62
- raw: JsonRecord;
63
- }
64
-
65
- // The Qwen catalog is account-scoped. Never allow one account's metadata to
66
- // overwrite another account's context window or capabilities.
67
- const modelRegistryByAccount = new Map<string, Map<string, RegistryEntry>>();
68
-
69
- function accountKey(accountId?: string): string {
70
- return accountId || "global";
71
- }
72
-
73
- export function getBaseModelId(modelId: string): string {
74
- // `-fast` is the only public variant. Keep legacy suffixes normalized for
75
- // account metadata lookups and old clients, without publishing them.
76
- // Check `-no-thinking` before `-thinking` (the former ends with the latter).
77
- if (modelId.endsWith("-no-thinking")) return modelId.slice(0, -12);
78
- if (modelId.endsWith("-thinking")) return modelId.slice(0, -9);
79
- if (modelId.endsWith("-fast")) return modelId.slice(0, -5);
80
- return modelId;
81
- }
82
-
83
- function getAccountRegistry(accountId?: string): Map<string, RegistryEntry> {
84
- const key = accountKey(accountId);
85
- let registry = modelRegistryByAccount.get(key);
86
- if (!registry) {
87
- registry = new Map<string, RegistryEntry>();
88
- modelRegistryByAccount.set(key, registry);
89
- }
90
- return registry;
91
- }
92
-
93
- function getEntry(modelId: string, accountId?: string): RegistryEntry | undefined {
94
- return modelRegistryByAccount.get(accountKey(accountId))?.get(
95
- getBaseModelId(modelId),
96
- );
97
- }
98
-
99
- function getOrCreateEntry(modelId: string, accountId?: string): RegistryEntry {
100
- const registry = getAccountRegistry(accountId);
101
- const baseId = getBaseModelId(modelId);
102
- const existing = registry.get(baseId);
103
- if (existing) return existing;
104
-
105
- const entry: RegistryEntry = {
106
- capabilities: cloneCapabilities(defaultCapabilities),
107
- raw: { id: baseId },
108
- };
109
- registry.set(baseId, entry);
110
- return entry;
111
- }
112
-
113
- function cloneCapabilities(capabilities: ModelCapabilities): ModelCapabilities {
114
- return {
115
- ...capabilities,
116
- modalities: [...capabilities.modalities],
117
- chatTypes: [...capabilities.chatTypes],
118
- mcp: [...capabilities.mcp],
119
- };
120
- }
121
-
122
- function asRecord(value: unknown): JsonRecord {
123
- return value && typeof value === "object" && !Array.isArray(value)
124
- ? (value as JsonRecord)
125
- : {};
126
- }
127
-
128
- function finitePositiveNumber(value: unknown): number | undefined {
129
- if (typeof value === "number") {
130
- return Number.isFinite(value) && value > 0 ? value : undefined;
131
- }
132
- if (typeof value === "string" && value.trim()) {
133
- const parsed = Number(value);
134
- return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
135
- }
136
- return undefined;
137
- }
138
-
139
- function firstPositiveNumber(...values: unknown[]): number | undefined {
140
- for (const value of values) {
141
- const number = finitePositiveNumber(value);
142
- if (number !== undefined) return number;
143
- }
144
- return undefined;
145
- }
146
-
147
- function booleanValue(...values: unknown[]): boolean | undefined {
148
- for (const value of values) {
149
- if (typeof value === "boolean") return value;
150
- }
151
- return undefined;
152
- }
153
-
154
- function positiveFlag(...values: unknown[]): boolean | undefined {
155
- for (const value of values) {
156
- if (typeof value === "number" && Number.isFinite(value)) {
157
- return value > 0;
158
- }
159
- if (typeof value === "boolean") return value;
160
- }
161
- return undefined;
162
- }
163
-
164
- function normalizeStringList(...values: unknown[]): string[] | undefined {
165
- const result: string[] = [];
166
- let sawList = false;
167
-
168
- const append = (value: unknown) => {
169
- if (Array.isArray(value)) {
170
- sawList = true;
171
- for (const item of value) append(item);
172
- return;
173
- }
174
- if (typeof value === "string") {
175
- sawList = true;
176
- for (const item of value.split(",")) {
177
- const normalized = item.trim().toLowerCase();
178
- if (normalized && !result.includes(normalized)) result.push(normalized);
179
- }
180
- }
181
- };
182
-
183
- for (const value of values) append(value);
184
- return sawList ? result : undefined;
185
- }
186
-
187
- function metadataRecords(model: JsonRecord): {
188
- info: JsonRecord;
189
- metadata: JsonRecord;
190
- upstreamCapabilities: JsonRecord;
191
- abilities: JsonRecord;
192
- thinkSkip: JsonRecord;
193
- } {
194
- const info = asRecord(model.info);
195
- const metadata = {
196
- ...asRecord(model.metadata),
197
- ...asRecord(model.meta),
198
- ...asRecord(info.meta),
199
- };
200
- const upstreamCapabilities = {
201
- ...asRecord(metadata.capabilities),
202
- ...asRecord(info.capabilities),
203
- ...asRecord(model.capabilities),
204
- };
205
- const abilities = {
206
- ...asRecord(metadata.abilities),
207
- ...asRecord(model.abilities),
208
- };
209
- const thinkSkip = {
210
- ...asRecord(metadata.think_skip),
211
- ...asRecord(model.think_skip),
212
- };
213
-
214
- return { info, metadata, upstreamCapabilities, abilities, thinkSkip };
215
- }
216
-
217
- function deriveCapabilities(
218
- model: JsonRecord,
219
- existing: ModelCapabilities | undefined,
220
- ): { capabilities: ModelCapabilities; contextWindow?: number } {
221
- const { info, metadata, upstreamCapabilities, abilities, thinkSkip } =
222
- metadataRecords(model);
223
- const fallback = existing ?? defaultCapabilities;
224
-
225
- const modalities =
226
- normalizeStringList(
227
- model.modalities,
228
- model.modality,
229
- metadata.modalities,
230
- metadata.modality,
231
- upstreamCapabilities.modalities,
232
- ) ?? [...fallback.modalities];
233
- const chatTypes =
234
- normalizeStringList(
235
- model.chat_types,
236
- model.chat_type,
237
- metadata.chat_types,
238
- metadata.chat_type,
239
- ) ?? [...fallback.chatTypes];
240
- const mcp =
241
- normalizeStringList(model.mcp, metadata.mcp) ?? [...fallback.mcp];
242
-
243
- const supportsThinking =
244
- booleanValue(
245
- model.supports_thinking,
246
- model.supportsThinking,
247
- upstreamCapabilities.thinking,
248
- upstreamCapabilities.supports_thinking,
249
- metadata.thinking,
250
- ) ??
251
- positiveFlag(abilities.thinking) ??
252
- fallback.supportsThinking;
253
-
254
- const supportsVision =
255
- booleanValue(
256
- model.supports_vision,
257
- model.supportsVision,
258
- upstreamCapabilities.vision,
259
- upstreamCapabilities.supports_vision,
260
- ) ??
261
- positiveFlag(abilities.vision) ??
262
- (modalities.includes("image") || fallback.supportsVision);
263
-
264
- const supportsDocument =
265
- booleanValue(
266
- model.supports_document,
267
- model.supportsDocument,
268
- upstreamCapabilities.document,
269
- upstreamCapabilities.pdf,
270
- upstreamCapabilities.pdf_input,
271
- ) ??
272
- positiveFlag(abilities.document) ??
273
- fallback.supportsDocument;
274
-
275
- const supportsAudio =
276
- booleanValue(
277
- model.supports_audio,
278
- model.supportsAudio,
279
- upstreamCapabilities.audio,
280
- ) ??
281
- (modalities.includes("audio") || fallback.supportsAudio);
282
-
283
- const supportsVideo =
284
- booleanValue(
285
- model.supports_video,
286
- model.supportsVideo,
287
- upstreamCapabilities.video,
288
- ) ??
289
- (modalities.includes("video") || fallback.supportsVideo);
290
-
291
- const supportsCitations =
292
- booleanValue(
293
- model.supports_citations,
294
- model.supportsCitations,
295
- model.citations,
296
- upstreamCapabilities.citations,
297
- ) ??
298
- positiveFlag(abilities.citations) ??
299
- fallback.supportsCitations;
300
-
301
- const supportsCodeExecution =
302
- booleanValue(
303
- model.supports_code_execution,
304
- model.supportsCodeExecution,
305
- upstreamCapabilities.code_execution,
306
- upstreamCapabilities.codeInterpreter,
307
- upstreamCapabilities.code_interpreter,
308
- ) ??
309
- positiveFlag(abilities.code_execution, abilities.code_interpreter) ??
310
- (mcp.some((item) =>
311
- ["code-interpreter", "code_interpreter", "code-execution"].includes(item),
312
- ) || fallback.supportsCodeExecution);
313
-
314
- const supportsStructuredOutputs =
315
- booleanValue(
316
- model.supports_structured_outputs,
317
- model.supportsStructuredOutputs,
318
- upstreamCapabilities.structured_outputs,
319
- upstreamCapabilities.structuredOutputs,
320
- ) ?? fallback.supportsStructuredOutputs;
321
-
322
- const maxOutputTokens =
323
- firstPositiveNumber(
324
- model.max_output_tokens,
325
- model.maxOutputTokens,
326
- model.max_tokens,
327
- metadata.max_output_tokens,
328
- metadata.maxOutputTokens,
329
- metadata.max_summary_generation_length,
330
- metadata.maxSummaryGenerationLength,
331
- metadata.max_generation_length,
332
- metadata.maxGenerationLength,
333
- upstreamCapabilities.max_output_tokens,
334
- upstreamCapabilities.maxOutputTokens,
335
- upstreamCapabilities.max_summary_generation_length,
336
- upstreamCapabilities.maxSummaryGenerationLength,
337
- upstreamCapabilities.max_generation_length,
338
- upstreamCapabilities.maxGenerationLength,
339
- ) ?? fallback.maxOutputTokens;
340
-
341
- const explicitThinkingTokens = firstPositiveNumber(
342
- model.max_thinking_tokens,
343
- model.maxThinkingTokens,
344
- metadata.max_thinking_tokens,
345
- metadata.maxThinkingTokens,
346
- metadata.max_thinking_generation_length,
347
- metadata.maxThinkingGenerationLength,
348
- upstreamCapabilities.max_thinking_tokens,
349
- upstreamCapabilities.maxThinkingTokens,
350
- upstreamCapabilities.max_thinking_generation_length,
351
- upstreamCapabilities.maxThinkingGenerationLength,
352
- );
353
- const maxThinkingTokens =
354
- explicitThinkingTokens ??
355
- (supportsThinking ? maxOutputTokens : 0) ??
356
- fallback.maxThinkingTokens;
357
-
358
- const canSkipThinking =
359
- supportsThinking &&
360
- (booleanValue(
361
- thinkSkip.enable,
362
- model.can_skip_thinking,
363
- model.canSkipThinking,
364
- upstreamCapabilities.can_skip_thinking,
365
- upstreamCapabilities.canSkipThinking,
366
- ) ??
367
- fallback.canSkipThinking);
368
-
369
- const isActive =
370
- booleanValue(model.is_active, model.isActive, info.is_active, metadata.is_active) ??
371
- fallback.isActive;
372
-
373
- const contextWindow = firstPositiveNumber(
374
- model.context_window,
375
- model.contextWindow,
376
- model.max_context_length,
377
- metadata.max_context_length,
378
- metadata.maxContextLength,
379
- metadata.context_window,
380
- metadata.contextWindow,
381
- upstreamCapabilities.max_context_length,
382
- upstreamCapabilities.context_window,
383
- );
384
-
385
- return {
386
- contextWindow,
387
- capabilities: {
388
- maxOutputTokens,
389
- maxThinkingTokens,
390
- supportsThinking,
391
- supportsVision,
392
- canSkipThinking,
393
- supportsDocument,
394
- supportsAudio,
395
- supportsVideo,
396
- supportsCitations,
397
- supportsCodeExecution,
398
- supportsStructuredOutputs,
399
- modalities,
400
- chatTypes,
401
- mcp,
402
- isActive,
403
- },
404
- };
405
- }
406
-
407
- export function setModelContextWindow(
408
- modelId: string,
409
- contextWindow: number,
410
- accountId?: string,
411
- ): void {
412
- if (!Number.isFinite(contextWindow) || contextWindow <= 0) return;
413
- const entry = getOrCreateEntry(modelId, accountId);
414
- entry.contextWindow = contextWindow;
415
- entry.raw.id = getBaseModelId(modelId);
416
- }
417
-
418
- export function getModelContextWindow(
419
- modelId: string,
420
- accountId?: string,
421
- ): number {
422
- return getEntry(modelId, accountId)?.contextWindow ?? defaultContextWindow;
423
- }
424
-
425
- export function getModelContextWindowSource(
426
- modelId: string,
427
- accountId?: string,
428
- ): "upstream" | "registry" | "default" {
429
- return getEntry(modelId, accountId)?.contextWindow !== undefined
430
- ? "upstream"
431
- : "default";
432
- }
433
-
434
- export function getModelCapabilities(
435
- modelId: string,
436
- accountId?: string,
437
- ): ModelCapabilities {
438
- const capabilities = getEntry(modelId, accountId)?.capabilities;
439
- return cloneCapabilities(capabilities ?? defaultCapabilities);
440
- }
441
-
442
- export function getModelMetadata(
443
- modelId: string,
444
- accountId?: string,
445
- ): RegisteredModelMetadata | undefined {
446
- const entry = getEntry(modelId, accountId);
447
- if (!entry) return undefined;
448
- return {
449
- id: getBaseModelId(modelId),
450
- contextWindow: entry.contextWindow,
451
- capabilities: cloneCapabilities(entry.capabilities),
452
- raw: { ...entry.raw },
453
- };
454
- }
455
-
456
- /** Update one model's live metadata without changing other accounts. */
457
- export function setModelCapabilities(
458
- modelId: string,
459
- capabilities: Partial<ModelCapabilities>,
460
- accountId?: string,
461
- ): void {
462
- const entry = getOrCreateEntry(modelId, accountId);
463
- const current = entry.capabilities;
464
- entry.capabilities = {
465
- ...current,
466
- ...capabilities,
467
- modalities: capabilities.modalities
468
- ? [...capabilities.modalities]
469
- : [...current.modalities],
470
- chatTypes: capabilities.chatTypes
471
- ? [...capabilities.chatTypes]
472
- : [...current.chatTypes],
473
- mcp: capabilities.mcp ? [...capabilities.mcp] : [...current.mcp],
474
- };
475
- }
476
-
477
- /**
478
- * Sync the context windows exposed directly by an upstream model list.
479
- * Prefer `syncModelMetadata` when the complete Qwen response is available.
480
- */
481
- export function syncModelContextWindows(
482
- models: Array<Record<string, unknown> & { id: string }>,
483
- accountId?: string,
484
- ): void {
485
- for (const model of models) {
486
- const { contextWindow } = deriveCapabilities(model, undefined);
487
- if (contextWindow !== undefined) {
488
- setModelContextWindow(model.id, contextWindow, accountId);
489
- }
490
- }
491
- }
492
-
493
- /**
494
- * Normalize and store the complete metadata returned by Qwen's `/api/models`.
495
- * The raw object is retained in the account-scoped registry so new metadata
496
- * fields remain available to future adapters instead of being discarded here.
497
- */
498
- export function syncModelMetadata(
499
- models: Array<Record<string, unknown> & { id: string }>,
500
- accountId?: string,
501
- ): void {
502
- const registry = getAccountRegistry(accountId);
503
-
504
- for (const model of models) {
505
- if (!model || typeof model.id !== "string" || !model.id.trim()) continue;
506
-
507
- const baseId = getBaseModelId(model.id);
508
- const existing = registry.get(baseId);
509
- const derived = deriveCapabilities(model, existing?.capabilities);
510
- const entry: RegistryEntry = {
511
- contextWindow: derived.contextWindow ?? existing?.contextWindow,
512
- capabilities: derived.capabilities,
513
- raw: { ...model },
514
- };
515
- registry.set(baseId, entry);
516
- }
517
- }
518
-
519
- /** Replace one account's complete live catalog after a successful upstream fetch. */
520
- export function replaceModelMetadata(
521
- models: Array<Record<string, unknown> & { id: string }>,
522
- accountId?: string,
523
- ): void {
524
- modelRegistryByAccount.delete(accountKey(accountId));
525
- syncModelMetadata(models, accountId);
526
- }
527
-
528
- /** Strip the public Fast suffix from a model ID. */
529
- export function stripFastSuffix(modelId: string): string {
530
- return modelId.replace(/-fast$/, "");
531
- }
532
-
533
- /**
534
- * Whether Qwen's metadata says a thinking-capable model cannot disable
535
- * thinking through its native `think_skip` flag. This is metadata only; the
536
- * public `-fast` variant is still exposed for every catalog model.
537
- */
538
- export function isAlwaysThinkingModel(
539
- modelId: string,
540
- accountId?: string,
541
- ): boolean {
542
- const capabilities = getModelCapabilities(modelId, accountId);
543
- return capabilities.supportsThinking && !capabilities.canSkipThinking;
544
- }
1
+ const defaultContextWindow = 1_048_576;
2
+ const defaultMaxOutputTokens = 65536;
3
+ const defaultMaxThinkingTokens = 16384;
4
+ export const MAX_PAYLOAD_SIZE = 50 * 1024 * 1024;
5
+
6
+ /**
7
+ * Model metadata exposed by Qwen's live `/api/models` catalog.
8
+ *
9
+ * The registry deliberately has no model-name table. Qwen can add, remove or
10
+ * change models without requiring a QwenProxy release. The values below are
11
+ * conservative fallbacks used only until the selected account's catalog has
12
+ * been synchronized.
13
+ */
14
+ export interface ModelCapabilities {
15
+ maxOutputTokens: number;
16
+ maxThinkingTokens: number;
17
+ supportsThinking: boolean;
18
+ supportsVision: boolean;
19
+ canSkipThinking: boolean;
20
+ supportsDocument: boolean;
21
+ supportsAudio: boolean;
22
+ supportsVideo: boolean;
23
+ supportsCitations: boolean;
24
+ supportsCodeExecution: boolean;
25
+ supportsStructuredOutputs: boolean;
26
+ modalities: string[];
27
+ chatTypes: string[];
28
+ mcp: string[];
29
+ isActive: boolean;
30
+ }
31
+
32
+ export interface RegisteredModelMetadata {
33
+ id: string;
34
+ contextWindow?: number;
35
+ capabilities: ModelCapabilities;
36
+ raw: Record<string, unknown>;
37
+ }
38
+
39
+ type JsonRecord = Record<string, unknown>;
40
+
41
+ const defaultCapabilities: ModelCapabilities = {
42
+ maxOutputTokens: defaultMaxOutputTokens,
43
+ maxThinkingTokens: defaultMaxThinkingTokens,
44
+ supportsThinking: false,
45
+ supportsVision: false,
46
+ canSkipThinking: false,
47
+ supportsDocument: false,
48
+ supportsAudio: false,
49
+ supportsVideo: false,
50
+ supportsCitations: false,
51
+ supportsCodeExecution: false,
52
+ supportsStructuredOutputs: false,
53
+ modalities: ["text"],
54
+ chatTypes: [],
55
+ mcp: [],
56
+ isActive: true,
57
+ };
58
+
59
+ interface RegistryEntry {
60
+ contextWindow?: number;
61
+ capabilities: ModelCapabilities;
62
+ raw: JsonRecord;
63
+ }
64
+
65
+ // The Qwen catalog is account-scoped. Never allow one account's metadata to
66
+ // overwrite another account's context window or capabilities.
67
+ const modelRegistryByAccount = new Map<string, Map<string, RegistryEntry>>();
68
+
69
+ function accountKey(accountId?: string): string {
70
+ return accountId || "global";
71
+ }
72
+
73
+ export function getBaseModelId(modelId: string): string {
74
+ // `-fast` is the only public variant. Keep legacy suffixes normalized for
75
+ // account metadata lookups and old clients, without publishing them.
76
+ // Check `-no-thinking` before `-thinking` (the former ends with the latter).
77
+ if (modelId.endsWith("-no-thinking")) return modelId.slice(0, -12);
78
+ if (modelId.endsWith("-thinking")) return modelId.slice(0, -9);
79
+ if (modelId.endsWith("-fast")) return modelId.slice(0, -5);
80
+ return modelId;
81
+ }
82
+
83
+ function getAccountRegistry(accountId?: string): Map<string, RegistryEntry> {
84
+ const key = accountKey(accountId);
85
+ let registry = modelRegistryByAccount.get(key);
86
+ if (!registry) {
87
+ registry = new Map<string, RegistryEntry>();
88
+ modelRegistryByAccount.set(key, registry);
89
+ }
90
+ return registry;
91
+ }
92
+
93
+ function getEntry(modelId: string, accountId?: string): RegistryEntry | undefined {
94
+ return modelRegistryByAccount.get(accountKey(accountId))?.get(
95
+ getBaseModelId(modelId),
96
+ );
97
+ }
98
+
99
+ function getOrCreateEntry(modelId: string, accountId?: string): RegistryEntry {
100
+ const registry = getAccountRegistry(accountId);
101
+ const baseId = getBaseModelId(modelId);
102
+ const existing = registry.get(baseId);
103
+ if (existing) return existing;
104
+
105
+ const entry: RegistryEntry = {
106
+ capabilities: cloneCapabilities(defaultCapabilities),
107
+ raw: { id: baseId },
108
+ };
109
+ registry.set(baseId, entry);
110
+ return entry;
111
+ }
112
+
113
+ function cloneCapabilities(capabilities: ModelCapabilities): ModelCapabilities {
114
+ return {
115
+ ...capabilities,
116
+ modalities: [...capabilities.modalities],
117
+ chatTypes: [...capabilities.chatTypes],
118
+ mcp: [...capabilities.mcp],
119
+ };
120
+ }
121
+
122
+ function asRecord(value: unknown): JsonRecord {
123
+ return value && typeof value === "object" && !Array.isArray(value)
124
+ ? (value as JsonRecord)
125
+ : {};
126
+ }
127
+
128
+ function finitePositiveNumber(value: unknown): number | undefined {
129
+ if (typeof value === "number") {
130
+ return Number.isFinite(value) && value > 0 ? value : undefined;
131
+ }
132
+ if (typeof value === "string" && value.trim()) {
133
+ const parsed = Number(value);
134
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
135
+ }
136
+ return undefined;
137
+ }
138
+
139
+ function firstPositiveNumber(...values: unknown[]): number | undefined {
140
+ for (const value of values) {
141
+ const number = finitePositiveNumber(value);
142
+ if (number !== undefined) return number;
143
+ }
144
+ return undefined;
145
+ }
146
+
147
+ function booleanValue(...values: unknown[]): boolean | undefined {
148
+ for (const value of values) {
149
+ if (typeof value === "boolean") return value;
150
+ }
151
+ return undefined;
152
+ }
153
+
154
+ function positiveFlag(...values: unknown[]): boolean | undefined {
155
+ for (const value of values) {
156
+ if (typeof value === "number" && Number.isFinite(value)) {
157
+ return value > 0;
158
+ }
159
+ if (typeof value === "boolean") return value;
160
+ }
161
+ return undefined;
162
+ }
163
+
164
+ function normalizeStringList(...values: unknown[]): string[] | undefined {
165
+ const result: string[] = [];
166
+ let sawList = false;
167
+
168
+ const append = (value: unknown) => {
169
+ if (Array.isArray(value)) {
170
+ sawList = true;
171
+ for (const item of value) append(item);
172
+ return;
173
+ }
174
+ if (typeof value === "string") {
175
+ sawList = true;
176
+ for (const item of value.split(",")) {
177
+ const normalized = item.trim().toLowerCase();
178
+ if (normalized && !result.includes(normalized)) result.push(normalized);
179
+ }
180
+ }
181
+ };
182
+
183
+ for (const value of values) append(value);
184
+ return sawList ? result : undefined;
185
+ }
186
+
187
+ function metadataRecords(model: JsonRecord): {
188
+ info: JsonRecord;
189
+ metadata: JsonRecord;
190
+ upstreamCapabilities: JsonRecord;
191
+ abilities: JsonRecord;
192
+ thinkSkip: JsonRecord;
193
+ } {
194
+ const info = asRecord(model.info);
195
+ const metadata = {
196
+ ...asRecord(model.metadata),
197
+ ...asRecord(model.meta),
198
+ ...asRecord(info.meta),
199
+ };
200
+ const upstreamCapabilities = {
201
+ ...asRecord(metadata.capabilities),
202
+ ...asRecord(info.capabilities),
203
+ ...asRecord(model.capabilities),
204
+ };
205
+ const abilities = {
206
+ ...asRecord(metadata.abilities),
207
+ ...asRecord(model.abilities),
208
+ };
209
+ const thinkSkip = {
210
+ ...asRecord(metadata.think_skip),
211
+ ...asRecord(model.think_skip),
212
+ };
213
+
214
+ return { info, metadata, upstreamCapabilities, abilities, thinkSkip };
215
+ }
216
+
217
+ function deriveCapabilities(
218
+ model: JsonRecord,
219
+ existing: ModelCapabilities | undefined,
220
+ ): { capabilities: ModelCapabilities; contextWindow?: number } {
221
+ const { info, metadata, upstreamCapabilities, abilities, thinkSkip } =
222
+ metadataRecords(model);
223
+ const fallback = existing ?? defaultCapabilities;
224
+
225
+ const modalities =
226
+ normalizeStringList(
227
+ model.modalities,
228
+ model.modality,
229
+ metadata.modalities,
230
+ metadata.modality,
231
+ upstreamCapabilities.modalities,
232
+ ) ?? [...fallback.modalities];
233
+ const chatTypes =
234
+ normalizeStringList(
235
+ model.chat_types,
236
+ model.chat_type,
237
+ metadata.chat_types,
238
+ metadata.chat_type,
239
+ ) ?? [...fallback.chatTypes];
240
+ const mcp =
241
+ normalizeStringList(model.mcp, metadata.mcp) ?? [...fallback.mcp];
242
+
243
+ const supportsThinking =
244
+ booleanValue(
245
+ model.supports_thinking,
246
+ model.supportsThinking,
247
+ upstreamCapabilities.thinking,
248
+ upstreamCapabilities.supports_thinking,
249
+ metadata.thinking,
250
+ ) ??
251
+ positiveFlag(abilities.thinking) ??
252
+ fallback.supportsThinking;
253
+
254
+ const supportsVision =
255
+ booleanValue(
256
+ model.supports_vision,
257
+ model.supportsVision,
258
+ upstreamCapabilities.vision,
259
+ upstreamCapabilities.supports_vision,
260
+ ) ??
261
+ positiveFlag(abilities.vision) ??
262
+ (modalities.includes("image") || fallback.supportsVision);
263
+
264
+ const supportsDocument =
265
+ booleanValue(
266
+ model.supports_document,
267
+ model.supportsDocument,
268
+ upstreamCapabilities.document,
269
+ upstreamCapabilities.pdf,
270
+ upstreamCapabilities.pdf_input,
271
+ ) ??
272
+ positiveFlag(abilities.document) ??
273
+ fallback.supportsDocument;
274
+
275
+ const supportsAudio =
276
+ booleanValue(
277
+ model.supports_audio,
278
+ model.supportsAudio,
279
+ upstreamCapabilities.audio,
280
+ ) ??
281
+ (modalities.includes("audio") || fallback.supportsAudio);
282
+
283
+ const supportsVideo =
284
+ booleanValue(
285
+ model.supports_video,
286
+ model.supportsVideo,
287
+ upstreamCapabilities.video,
288
+ ) ??
289
+ (modalities.includes("video") || fallback.supportsVideo);
290
+
291
+ const supportsCitations =
292
+ booleanValue(
293
+ model.supports_citations,
294
+ model.supportsCitations,
295
+ model.citations,
296
+ upstreamCapabilities.citations,
297
+ ) ??
298
+ positiveFlag(abilities.citations) ??
299
+ fallback.supportsCitations;
300
+
301
+ const supportsCodeExecution =
302
+ booleanValue(
303
+ model.supports_code_execution,
304
+ model.supportsCodeExecution,
305
+ upstreamCapabilities.code_execution,
306
+ upstreamCapabilities.codeInterpreter,
307
+ upstreamCapabilities.code_interpreter,
308
+ ) ??
309
+ positiveFlag(abilities.code_execution, abilities.code_interpreter) ??
310
+ (mcp.some((item) =>
311
+ ["code-interpreter", "code_interpreter", "code-execution"].includes(item),
312
+ ) || fallback.supportsCodeExecution);
313
+
314
+ const supportsStructuredOutputs =
315
+ booleanValue(
316
+ model.supports_structured_outputs,
317
+ model.supportsStructuredOutputs,
318
+ upstreamCapabilities.structured_outputs,
319
+ upstreamCapabilities.structuredOutputs,
320
+ ) ?? fallback.supportsStructuredOutputs;
321
+
322
+ const maxOutputTokens =
323
+ firstPositiveNumber(
324
+ model.max_output_tokens,
325
+ model.maxOutputTokens,
326
+ model.max_tokens,
327
+ metadata.max_output_tokens,
328
+ metadata.maxOutputTokens,
329
+ metadata.max_summary_generation_length,
330
+ metadata.maxSummaryGenerationLength,
331
+ metadata.max_generation_length,
332
+ metadata.maxGenerationLength,
333
+ upstreamCapabilities.max_output_tokens,
334
+ upstreamCapabilities.maxOutputTokens,
335
+ upstreamCapabilities.max_summary_generation_length,
336
+ upstreamCapabilities.maxSummaryGenerationLength,
337
+ upstreamCapabilities.max_generation_length,
338
+ upstreamCapabilities.maxGenerationLength,
339
+ ) ?? fallback.maxOutputTokens;
340
+
341
+ const explicitThinkingTokens = firstPositiveNumber(
342
+ model.max_thinking_tokens,
343
+ model.maxThinkingTokens,
344
+ metadata.max_thinking_tokens,
345
+ metadata.maxThinkingTokens,
346
+ metadata.max_thinking_generation_length,
347
+ metadata.maxThinkingGenerationLength,
348
+ upstreamCapabilities.max_thinking_tokens,
349
+ upstreamCapabilities.maxThinkingTokens,
350
+ upstreamCapabilities.max_thinking_generation_length,
351
+ upstreamCapabilities.maxThinkingGenerationLength,
352
+ );
353
+ const maxThinkingTokens =
354
+ explicitThinkingTokens ??
355
+ (supportsThinking ? maxOutputTokens : 0) ??
356
+ fallback.maxThinkingTokens;
357
+
358
+ const canSkipThinking =
359
+ supportsThinking &&
360
+ (booleanValue(
361
+ thinkSkip.enable,
362
+ model.can_skip_thinking,
363
+ model.canSkipThinking,
364
+ upstreamCapabilities.can_skip_thinking,
365
+ upstreamCapabilities.canSkipThinking,
366
+ ) ??
367
+ fallback.canSkipThinking);
368
+
369
+ const isActive =
370
+ booleanValue(model.is_active, model.isActive, info.is_active, metadata.is_active) ??
371
+ fallback.isActive;
372
+
373
+ const contextWindow = firstPositiveNumber(
374
+ model.context_window,
375
+ model.contextWindow,
376
+ model.max_context_length,
377
+ metadata.max_context_length,
378
+ metadata.maxContextLength,
379
+ metadata.context_window,
380
+ metadata.contextWindow,
381
+ upstreamCapabilities.max_context_length,
382
+ upstreamCapabilities.context_window,
383
+ );
384
+
385
+ return {
386
+ contextWindow,
387
+ capabilities: {
388
+ maxOutputTokens,
389
+ maxThinkingTokens,
390
+ supportsThinking,
391
+ supportsVision,
392
+ canSkipThinking,
393
+ supportsDocument,
394
+ supportsAudio,
395
+ supportsVideo,
396
+ supportsCitations,
397
+ supportsCodeExecution,
398
+ supportsStructuredOutputs,
399
+ modalities,
400
+ chatTypes,
401
+ mcp,
402
+ isActive,
403
+ },
404
+ };
405
+ }
406
+
407
+ export function setModelContextWindow(
408
+ modelId: string,
409
+ contextWindow: number,
410
+ accountId?: string,
411
+ ): void {
412
+ if (!Number.isFinite(contextWindow) || contextWindow <= 0) return;
413
+ const entry = getOrCreateEntry(modelId, accountId);
414
+ entry.contextWindow = contextWindow;
415
+ entry.raw.id = getBaseModelId(modelId);
416
+ }
417
+
418
+ export function getModelContextWindow(
419
+ modelId: string,
420
+ accountId?: string,
421
+ ): number {
422
+ return getEntry(modelId, accountId)?.contextWindow ?? defaultContextWindow;
423
+ }
424
+
425
+ export function getModelContextWindowSource(
426
+ modelId: string,
427
+ accountId?: string,
428
+ ): "upstream" | "registry" | "default" {
429
+ return getEntry(modelId, accountId)?.contextWindow !== undefined
430
+ ? "upstream"
431
+ : "default";
432
+ }
433
+
434
+ export function getModelCapabilities(
435
+ modelId: string,
436
+ accountId?: string,
437
+ ): ModelCapabilities {
438
+ const capabilities = getEntry(modelId, accountId)?.capabilities;
439
+ return cloneCapabilities(capabilities ?? defaultCapabilities);
440
+ }
441
+
442
+ export function getModelMetadata(
443
+ modelId: string,
444
+ accountId?: string,
445
+ ): RegisteredModelMetadata | undefined {
446
+ const entry = getEntry(modelId, accountId);
447
+ if (!entry) return undefined;
448
+ return {
449
+ id: getBaseModelId(modelId),
450
+ contextWindow: entry.contextWindow,
451
+ capabilities: cloneCapabilities(entry.capabilities),
452
+ raw: { ...entry.raw },
453
+ };
454
+ }
455
+
456
+ /** Update one model's live metadata without changing other accounts. */
457
+ export function setModelCapabilities(
458
+ modelId: string,
459
+ capabilities: Partial<ModelCapabilities>,
460
+ accountId?: string,
461
+ ): void {
462
+ const entry = getOrCreateEntry(modelId, accountId);
463
+ const current = entry.capabilities;
464
+ entry.capabilities = {
465
+ ...current,
466
+ ...capabilities,
467
+ modalities: capabilities.modalities
468
+ ? [...capabilities.modalities]
469
+ : [...current.modalities],
470
+ chatTypes: capabilities.chatTypes
471
+ ? [...capabilities.chatTypes]
472
+ : [...current.chatTypes],
473
+ mcp: capabilities.mcp ? [...capabilities.mcp] : [...current.mcp],
474
+ };
475
+ }
476
+
477
+ /**
478
+ * Sync the context windows exposed directly by an upstream model list.
479
+ * Prefer `syncModelMetadata` when the complete Qwen response is available.
480
+ */
481
+ export function syncModelContextWindows(
482
+ models: Array<Record<string, unknown> & { id: string }>,
483
+ accountId?: string,
484
+ ): void {
485
+ for (const model of models) {
486
+ const { contextWindow } = deriveCapabilities(model, undefined);
487
+ if (contextWindow !== undefined) {
488
+ setModelContextWindow(model.id, contextWindow, accountId);
489
+ }
490
+ }
491
+ }
492
+
493
+ /**
494
+ * Normalize and store the complete metadata returned by Qwen's `/api/models`.
495
+ * The raw object is retained in the account-scoped registry so new metadata
496
+ * fields remain available to future adapters instead of being discarded here.
497
+ */
498
+ export function syncModelMetadata(
499
+ models: Array<Record<string, unknown> & { id: string }>,
500
+ accountId?: string,
501
+ ): void {
502
+ const registry = getAccountRegistry(accountId);
503
+
504
+ for (const model of models) {
505
+ if (!model || typeof model.id !== "string" || !model.id.trim()) continue;
506
+
507
+ const baseId = getBaseModelId(model.id);
508
+ const existing = registry.get(baseId);
509
+ const derived = deriveCapabilities(model, existing?.capabilities);
510
+ const entry: RegistryEntry = {
511
+ contextWindow: derived.contextWindow ?? existing?.contextWindow,
512
+ capabilities: derived.capabilities,
513
+ raw: { ...model },
514
+ };
515
+ registry.set(baseId, entry);
516
+ }
517
+ }
518
+
519
+ /** Replace one account's complete live catalog after a successful upstream fetch. */
520
+ export function replaceModelMetadata(
521
+ models: Array<Record<string, unknown> & { id: string }>,
522
+ accountId?: string,
523
+ ): void {
524
+ modelRegistryByAccount.delete(accountKey(accountId));
525
+ syncModelMetadata(models, accountId);
526
+ }
527
+
528
+ /** Strip the public Fast suffix from a model ID. */
529
+ export function stripFastSuffix(modelId: string): string {
530
+ return modelId.replace(/-fast$/, "");
531
+ }
532
+
533
+ /**
534
+ * Whether Qwen's metadata says a thinking-capable model cannot disable
535
+ * thinking through its native `think_skip` flag. This is metadata only; the
536
+ * public `-fast` variant is still exposed for every catalog model.
537
+ */
538
+ export function isAlwaysThinkingModel(
539
+ modelId: string,
540
+ accountId?: string,
541
+ ): boolean {
542
+ const capabilities = getModelCapabilities(modelId, accountId);
543
+ return capabilities.supportsThinking && !capabilities.canSkipThinking;
544
+ }