qwenproxy-cli 1.0.0

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 -0
  2. package/README.md +907 -0
  3. package/bin/qwenproxy.js +141 -0
  4. package/package.json +78 -0
  5. package/src/api/error-classifier.ts +159 -0
  6. package/src/api/error-helpers.ts +118 -0
  7. package/src/api/models.ts +261 -0
  8. package/src/api/server.ts +859 -0
  9. package/src/cache/memory-cache.ts +385 -0
  10. package/src/clean-cache.ts +204 -0
  11. package/src/core/account-concurrency.ts +671 -0
  12. package/src/core/account-manager.ts +297 -0
  13. package/src/core/account-priority.ts +163 -0
  14. package/src/core/accounts.ts +186 -0
  15. package/src/core/config.ts +383 -0
  16. package/src/core/crypto-utils.ts +79 -0
  17. package/src/core/database.ts +276 -0
  18. package/src/core/errors.ts +118 -0
  19. package/src/core/logger.ts +269 -0
  20. package/src/core/memory-usage.ts +84 -0
  21. package/src/core/metrics.ts +291 -0
  22. package/src/core/model-alias.ts +77 -0
  23. package/src/core/model-registry.ts +544 -0
  24. package/src/core/mutex.ts +119 -0
  25. package/src/core/paths.ts +199 -0
  26. package/src/core/prompt-limits.ts +214 -0
  27. package/src/core/reasoning-effort.ts +102 -0
  28. package/src/core/stream-registry.ts +96 -0
  29. package/src/core/waf-isolation.ts +117 -0
  30. package/src/core/watchdog.ts +195 -0
  31. package/src/delete-chats.ts +23 -0
  32. package/src/index.ts +64 -0
  33. package/src/login.ts +147 -0
  34. package/src/reset-cooldowns.ts +11 -0
  35. package/src/routes/anthropic/index.ts +355 -0
  36. package/src/routes/anthropic/translate.ts +522 -0
  37. package/src/routes/anthropic/types.ts +154 -0
  38. package/src/routes/anthropic/validation.ts +144 -0
  39. package/src/routes/chat/account.ts +1817 -0
  40. package/src/routes/chat/context.ts +241 -0
  41. package/src/routes/chat/errors.ts +85 -0
  42. package/src/routes/chat/helpers.ts +268 -0
  43. package/src/routes/chat/index.ts +618 -0
  44. package/src/routes/chat/media.ts +285 -0
  45. package/src/routes/chat/retry-policy.ts +754 -0
  46. package/src/routes/chat/stop.ts +98 -0
  47. package/src/routes/chat/streaming.ts +2710 -0
  48. package/src/routes/chat/validation.ts +526 -0
  49. package/src/routes/chat.ts +2 -0
  50. package/src/routes/completions.ts +290 -0
  51. package/src/routes/images.ts +139 -0
  52. package/src/routes/responses/adapter.ts +503 -0
  53. package/src/routes/responses/index.ts +405 -0
  54. package/src/routes/responses/state.ts +230 -0
  55. package/src/routes/responses/streaming.ts +528 -0
  56. package/src/routes/responses/types.ts +285 -0
  57. package/src/routes/responses/validation.ts +202 -0
  58. package/src/routes/upload.ts +731 -0
  59. package/src/routes/videos.ts +214 -0
  60. package/src/services/auth-playwright.ts +173 -0
  61. package/src/services/captcha-coordinator.ts +161 -0
  62. package/src/services/captcha-solver.ts +553 -0
  63. package/src/services/chat-cleanup.ts +80 -0
  64. package/src/services/context-meter.ts +317 -0
  65. package/src/services/fingerprint.ts +242 -0
  66. package/src/services/human-behavior.ts +173 -0
  67. package/src/services/media-generation.ts +1748 -0
  68. package/src/services/playwright.ts +2800 -0
  69. package/src/services/qwen-chat-pool.ts +345 -0
  70. package/src/services/qwen-errors.ts +133 -0
  71. package/src/services/qwen-headers.ts +79 -0
  72. package/src/services/qwen-thread-state.ts +393 -0
  73. package/src/services/qwen-url.ts +19 -0
  74. package/src/services/qwen.ts +3126 -0
  75. package/src/services/session-keeper.ts +88 -0
  76. package/src/services/token-estimation-metrics.ts +118 -0
  77. package/src/sync/claude-code.ts +75 -0
  78. package/src/sync/codex.ts +123 -0
  79. package/src/sync/index.ts +362 -0
  80. package/src/sync/omp.ts +105 -0
  81. package/src/sync/opencode.ts +214 -0
  82. package/src/sync/types.ts +53 -0
  83. package/src/sync/utils.ts +27 -0
  84. package/src/sync-clients.ts +189 -0
  85. package/src/tools/instructions.ts +137 -0
  86. package/src/tools/manifest.ts +81 -0
  87. package/src/tools/parser.ts +2989 -0
  88. package/src/tools/toolcall-tags.ts +142 -0
  89. package/src/tools/types.ts +53 -0
  90. package/src/tui/app.ts +264 -0
  91. package/src/tui/index.ts +61 -0
  92. package/src/tui/markdown.ts +258 -0
  93. package/src/tui/proxy-client.ts +326 -0
  94. package/src/tui/screen.ts +278 -0
  95. package/src/tui/server-manager.ts +270 -0
  96. package/src/tui/theme.ts +432 -0
  97. package/src/tui/types.ts +33 -0
  98. package/src/tui/views/accounts-view.ts +656 -0
  99. package/src/tui/views/chat-view.ts +823 -0
  100. package/src/tui/views/logs-view.ts +413 -0
  101. package/src/tui/views/status-view.ts +204 -0
  102. package/src/tui/views/storage-view.ts +291 -0
  103. package/src/tui/views/sync-view.ts +409 -0
  104. package/src/types/ali-oss.d.ts +32 -0
  105. package/src/utils/context-truncation.ts +84 -0
  106. package/src/utils/json.ts +380 -0
  107. package/src/utils/session-id.ts +37 -0
  108. package/src/utils/tool-call-guard.ts +85 -0
  109. package/src/utils/types.ts +109 -0
@@ -0,0 +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
+ }
@@ -0,0 +1,119 @@
1
+ import { logger } from "./logger.js";
2
+
3
+ /**
4
+ * Maximum time a mutex can be held before it's considered leaked and
5
+ * force-released. Overridable via MUTEX_MAX_HOLD_MS for tests.
6
+ */
7
+ const MAX_HOLD_MS = (() => {
8
+ const parsed = parseInt(process.env.MUTEX_MAX_HOLD_MS ?? "", 10);
9
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : 120_000;
10
+ })();
11
+
12
+ export class Mutex {
13
+ private queue: Array<{ waiter: () => void; enqueuedAt: number; key: string }> = [];
14
+ private locked = false;
15
+ private lockedAt = 0;
16
+ private lockedByKey = "";
17
+
18
+ constructor(
19
+ public readonly name: string = "unnamed",
20
+ private readonly maxHoldMs: number = MAX_HOLD_MS,
21
+ ) {}
22
+
23
+ async acquire(timeoutMs = 300_000, key = ""): Promise<() => void> {
24
+ // Stale detection: force-release if held beyond the hold limit
25
+ // (leaked lock). The chat lock uses a longer hold so a legitimate
26
+ // long generation (2-3 min with huge contexts) is not force-released
27
+ // mid-stream; the page/init locks keep the shorter safety net so a
28
+ // stuck browser op releases the account back to the pool faster.
29
+ const holdLimitMs = this.maxHoldMs;
30
+ if (this.locked && Date.now() - this.lockedAt > holdLimitMs) {
31
+ const heldFor = Date.now() - this.lockedAt;
32
+ logger.warn(
33
+ `[Mutex:${this.name}] Force-releasing stale lock | heldBy=${this.lockedByKey} | heldFor=${heldFor}ms | limit=${holdLimitMs}ms`,
34
+ );
35
+ this.locked = false;
36
+ this.lockedAt = 0;
37
+ this.lockedByKey = "";
38
+ }
39
+
40
+ if (!this.locked) {
41
+ this.locked = true;
42
+ this.lockedAt = Date.now();
43
+ this.lockedByKey = key;
44
+ return this.createRelease();
45
+ }
46
+
47
+ const enqueuedAt = Date.now();
48
+ const logKey = key || "anon";
49
+ if (logger.isLevelEnabled("debug")) {
50
+ logger.debug(`[Mutex:${this.name}] enqueue key=${logKey} queue=${this.queue.length + 1} heldBy=${this.lockedByKey || "unknown"} heldFor=${Date.now() - this.lockedAt}ms`);
51
+ }
52
+
53
+ return new Promise<() => void>((resolve, reject) => {
54
+ const waiter = () => {
55
+ clearTimeout(timer);
56
+ this.lockedByKey = logKey;
57
+ this.lockedAt = Date.now();
58
+ resolve(this.createRelease());
59
+ };
60
+ const timer = setTimeout(() => {
61
+ const index = this.queue.findIndex((e) => e.waiter === waiter);
62
+ if (index !== -1) this.queue.splice(index, 1);
63
+ const heldFor = Date.now() - this.lockedAt;
64
+ logger.warn(`[Mutex:${this.name}] TIMEOUT key=${logKey} waited=${timeoutMs}ms heldBy=${this.lockedByKey || "unknown"} heldFor=${heldFor}ms queueLeft=${this.queue.length}`);
65
+ reject(new Error(`Mutex[${this.name}] acquire timeout after ${timeoutMs}ms (held by ${this.lockedByKey || "unknown"} for ${heldFor}ms)`));
66
+ }, timeoutMs);
67
+ this.queue.push({ waiter, enqueuedAt, key: logKey });
68
+ });
69
+ }
70
+
71
+ async withLock<T>(fn: () => Promise<T> | T, timeoutMs?: number): Promise<T> {
72
+ const release = await this.acquire(timeoutMs);
73
+ try {
74
+ return await fn();
75
+ } finally {
76
+ release();
77
+ }
78
+ }
79
+
80
+ private createRelease(): () => void {
81
+ let released = false;
82
+ return () => {
83
+ if (released) return;
84
+ released = true;
85
+ this.release();
86
+ };
87
+ }
88
+
89
+ private release(): void {
90
+ const next = this.queue.shift();
91
+ if (next) {
92
+ const waitTime = Date.now() - next.enqueuedAt;
93
+ if (waitTime > 1_000 && logger.isLevelEnabled("debug")) {
94
+ logger.debug(`[Mutex:${this.name}] dequeued key=${next.key} waited=${waitTime}ms`);
95
+ }
96
+ next.waiter();
97
+ return;
98
+ }
99
+
100
+ this.locked = false;
101
+ this.lockedAt = 0;
102
+ this.lockedByKey = "";
103
+ }
104
+
105
+ /** Returns true if the mutex is not locked and has no waiting queue. */
106
+ isIdle(): boolean {
107
+ return !this.locked && this.queue.length === 0;
108
+ }
109
+
110
+ /** Returns diagnostic info about the current lock state. */
111
+ state(): { locked: boolean; heldBy: string; heldForMs: number; queueLength: number } {
112
+ return {
113
+ locked: this.locked,
114
+ heldBy: this.lockedByKey,
115
+ heldForMs: this.locked ? Date.now() - this.lockedAt : 0,
116
+ queueLength: this.queue.length,
117
+ };
118
+ }
119
+ }