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,37 +1,37 @@
1
- import { createHash } from "crypto";
2
- import type { Message } from "./types.js";
3
-
4
- /**
5
- * Derive a deterministic session ID from a caller-provided conversation key
6
- * plus the conversation's anchoring content.
7
- */
8
- export function deriveSessionId(
9
- messages: Message[],
10
- systemPrompt: string = "",
11
- conversationKey?: string,
12
- ): string {
13
- const firstUser = messages.find((m) => m.role === "user");
14
- const anchor = [
15
- conversationKey?.trim(),
16
- systemPrompt.trim(),
17
- extractTextContent(firstUser),
18
- ]
19
- .filter((part) => part && part.length > 0)
20
- .join("|");
21
- const hash = createHash("sha256").update(anchor).digest("hex").slice(0, 16);
22
- return `sess_${hash}`;
23
- }
24
-
25
- function extractTextContent(msg: Message | undefined): string {
26
- if (!msg || !msg.content) return "";
27
- if (typeof msg.content === "string") return msg.content;
28
-
29
- const content = msg.content as any;
30
- if (Array.isArray(content)) {
31
- return content
32
- .filter((p: any) => p?.type === "text")
33
- .map((p: any) => p.text || "")
34
- .join(" ");
35
- }
36
- return "";
37
- }
1
+ import { createHash } from "crypto";
2
+ import type { Message } from "./types.js";
3
+
4
+ /**
5
+ * Derive a deterministic session ID from a caller-provided conversation key
6
+ * plus the conversation's anchoring content.
7
+ */
8
+ export function deriveSessionId(
9
+ messages: Message[],
10
+ systemPrompt: string = "",
11
+ conversationKey?: string,
12
+ ): string {
13
+ const firstUser = messages.find((m) => m.role === "user");
14
+ const anchor = [
15
+ conversationKey?.trim(),
16
+ systemPrompt.trim(),
17
+ extractTextContent(firstUser),
18
+ ]
19
+ .filter((part) => part && part.length > 0)
20
+ .join("|");
21
+ const hash = createHash("sha256").update(anchor).digest("hex").slice(0, 16);
22
+ return `sess_${hash}`;
23
+ }
24
+
25
+ function extractTextContent(msg: Message | undefined): string {
26
+ if (!msg || !msg.content) return "";
27
+ if (typeof msg.content === "string") return msg.content;
28
+
29
+ const content = msg.content as any;
30
+ if (Array.isArray(content)) {
31
+ return content
32
+ .filter((p: any) => p?.type === "text")
33
+ .map((p: any) => p.text || "")
34
+ .join(" ");
35
+ }
36
+ return "";
37
+ }
@@ -1,85 +1,85 @@
1
- import type { Message } from "./types.ts";
2
-
3
- /**
4
- * Detects tool calls that were already executed (identical name + arguments)
5
- * earlier in the conversation and returns a short system reminder to be
6
- * appended to the prompt. This breaks the re-read/re-inspect loops where the
7
- * model keeps calling the same tool turn after turn instead of answering.
8
- *
9
- * The reminder is prompt-only and does not alter caching, session identity or
10
- * the upstream thread.
11
- *
12
- * @param messages - Full message history (assistant tool_calls + tool/function results).
13
- * @param threshold - Minimum number of identical executions before reminding.
14
- * @returns Reminder text to append to the prompt, or null when the history is clean.
15
- */
16
- export function buildRepeatedToolCallReminder(
17
- messages: Message[] | null | undefined,
18
- threshold = 2,
19
- ): string | null {
20
- if (!messages || messages.length === 0 || threshold < 2) return null;
21
-
22
- // Canonical key: tool name + normalized arguments (object key order ignored).
23
- const occurrences = new Map<string, number>();
24
- let lastToolCallTurnIndex = -1;
25
-
26
- for (let i = 0; i < messages.length; i++) {
27
- const calls = messages[i]?.tool_calls;
28
- if (!calls || calls.length === 0) continue;
29
- lastToolCallTurnIndex = i;
30
- for (const call of calls) {
31
- const key = toolCallKey(call.function?.name, call.function?.arguments);
32
- occurrences.set(key, (occurrences.get(key) ?? 0) + 1);
33
- }
34
- }
35
-
36
- if (lastToolCallTurnIndex < 0) return null;
37
-
38
- const lastCalls = messages[lastToolCallTurnIndex].tool_calls ?? [];
39
- const reminders: string[] = [];
40
-
41
- for (const call of lastCalls) {
42
- const key = toolCallKey(call.function?.name, call.function?.arguments);
43
- const count = occurrences.get(key) ?? 0;
44
- if (count >= threshold) {
45
- reminders.push(
46
- `[SYSTEM REMINDER] You have already executed the tool call "${call.function?.name}" ${count} time(s) in this conversation with exactly the same arguments, and its result is already in the history. Do NOT call it again: use the existing result and write the final answer now. If the earlier attempts were rejected, do not repeat them either; answer directly.`,
47
- );
48
- }
49
- }
50
-
51
- return reminders.length > 0
52
- ? reminders.join("\n\n")
53
- : null;
54
- }
55
-
56
- function toolCallKey(name: string | undefined, rawArgs: string | undefined): string {
57
- const toolName = typeof name === "string" ? name : "";
58
- const raw = typeof rawArgs === "string" ? rawArgs : "";
59
- let parsed: unknown = raw;
60
- try {
61
- parsed = JSON.parse(raw);
62
- } catch {
63
- // Unparseable payloads fall back to the raw string as the key.
64
- }
65
- return `${toolName}|${JSON.stringify(canonicalize(parsed))}`;
66
- }
67
-
68
- /**
69
- * Deep-normalizes JSON values so that argument objects with different key
70
- * orders or whitespace still produce the same canonical key.
71
- */
72
- function canonicalize(value: unknown): unknown {
73
- if (Array.isArray(value)) {
74
- return value.map(canonicalize);
75
- }
76
- if (value && typeof value === "object") {
77
- const record = value as Record<string, unknown>;
78
- const result: Record<string, unknown> = {};
79
- for (const key of Object.keys(record).sort()) {
80
- result[key] = canonicalize(record[key]);
81
- }
82
- return result;
83
- }
84
- return value;
1
+ import type { Message } from "./types.ts";
2
+
3
+ /**
4
+ * Detects tool calls that were already executed (identical name + arguments)
5
+ * earlier in the conversation and returns a short system reminder to be
6
+ * appended to the prompt. This breaks the re-read/re-inspect loops where the
7
+ * model keeps calling the same tool turn after turn instead of answering.
8
+ *
9
+ * The reminder is prompt-only and does not alter caching, session identity or
10
+ * the upstream thread.
11
+ *
12
+ * @param messages - Full message history (assistant tool_calls + tool/function results).
13
+ * @param threshold - Minimum number of identical executions before reminding.
14
+ * @returns Reminder text to append to the prompt, or null when the history is clean.
15
+ */
16
+ export function buildRepeatedToolCallReminder(
17
+ messages: Message[] | null | undefined,
18
+ threshold = 2,
19
+ ): string | null {
20
+ if (!messages || messages.length === 0 || threshold < 2) return null;
21
+
22
+ // Canonical key: tool name + normalized arguments (object key order ignored).
23
+ const occurrences = new Map<string, number>();
24
+ let lastToolCallTurnIndex = -1;
25
+
26
+ for (let i = 0; i < messages.length; i++) {
27
+ const calls = messages[i]?.tool_calls;
28
+ if (!calls || calls.length === 0) continue;
29
+ lastToolCallTurnIndex = i;
30
+ for (const call of calls) {
31
+ const key = toolCallKey(call.function?.name, call.function?.arguments);
32
+ occurrences.set(key, (occurrences.get(key) ?? 0) + 1);
33
+ }
34
+ }
35
+
36
+ if (lastToolCallTurnIndex < 0) return null;
37
+
38
+ const lastCalls = messages[lastToolCallTurnIndex].tool_calls ?? [];
39
+ const reminders: string[] = [];
40
+
41
+ for (const call of lastCalls) {
42
+ const key = toolCallKey(call.function?.name, call.function?.arguments);
43
+ const count = occurrences.get(key) ?? 0;
44
+ if (count >= threshold) {
45
+ reminders.push(
46
+ `[SYSTEM REMINDER] You have already executed the tool call "${call.function?.name}" ${count} time(s) in this conversation with exactly the same arguments, and its result is already in the history. Do NOT call it again: use the existing result and write the final answer now. If the earlier attempts were rejected, do not repeat them either; answer directly.`,
47
+ );
48
+ }
49
+ }
50
+
51
+ return reminders.length > 0
52
+ ? reminders.join("\n\n")
53
+ : null;
54
+ }
55
+
56
+ function toolCallKey(name: string | undefined, rawArgs: string | undefined): string {
57
+ const toolName = typeof name === "string" ? name : "";
58
+ const raw = typeof rawArgs === "string" ? rawArgs : "";
59
+ let parsed: unknown = raw;
60
+ try {
61
+ parsed = JSON.parse(raw);
62
+ } catch {
63
+ // Unparseable payloads fall back to the raw string as the key.
64
+ }
65
+ return `${toolName}|${JSON.stringify(canonicalize(parsed))}`;
66
+ }
67
+
68
+ /**
69
+ * Deep-normalizes JSON values so that argument objects with different key
70
+ * orders or whitespace still produce the same canonical key.
71
+ */
72
+ function canonicalize(value: unknown): unknown {
73
+ if (Array.isArray(value)) {
74
+ return value.map(canonicalize);
75
+ }
76
+ if (value && typeof value === "object") {
77
+ const record = value as Record<string, unknown>;
78
+ const result: Record<string, unknown> = {};
79
+ for (const key of Object.keys(record).sort()) {
80
+ result[key] = canonicalize(record[key]);
81
+ }
82
+ return result;
83
+ }
84
+ return value;
85
85
  }
@@ -1,109 +1,109 @@
1
- import type { JsonSchema, FunctionToolDefinition } from "../tools/types.ts";
2
- export type { JsonSchema, FunctionToolDefinition };
3
-
4
- /** Tool choice options */
5
- export type ToolChoice =
6
- | "auto"
7
- | "none"
8
- | "required"
9
- | {
10
- type: "function";
11
- function: { name: string };
12
- };
13
-
14
- // --- Message Types ---
15
-
16
- export interface ToolCallFunction {
17
- name: string;
18
- arguments: string;
19
- }
20
-
21
- export interface MessageToolCall {
22
- id: string;
23
- type: "function";
24
- function: ToolCallFunction;
25
- }
26
-
27
- export interface Message {
28
- role: string;
29
- content: string | null;
30
- /** Present on assistant messages that invoked tools */
31
- tool_calls?: MessageToolCall[];
32
- /** Present on tool/function response messages to link back to a call */
33
- tool_call_id?: string;
34
- /** Present on tool/function response messages */
35
- name?: string;
36
- /** Reasoning content for thinking models */
37
- reasoning_content?: string;
38
- }
39
-
40
- // --- Request Types ---
41
-
42
- export interface OpenAIRequest {
43
- model: string;
44
- messages: Message[];
45
- stream?: boolean;
46
- /** QwenProxy media extension used by image/video chat models. */
47
- size?: string;
48
- tools?: FunctionToolDefinition[];
49
- tool_choice?: ToolChoice;
50
- user?: string;
51
- session_id?: string;
52
- conversation_id?: string;
53
- /** OpenAI chat spec: none|minimal|low|medium|high|xhigh|max (reasoning models). */
54
- reasoning_effort?: string | null;
55
- /** camelCase alias overlaid by OpenCode-style configs. */
56
- reasoningEffort?: string | null;
57
- stream_options?: {
58
- include_usage?: boolean;
59
- };
60
- }
61
-
62
- // --- Response Types ---
63
-
64
- export interface ToolCall {
65
- index: number;
66
- id?: string;
67
- type: string;
68
- function: {
69
- name: string;
70
- arguments: string;
71
- };
72
- }
73
-
74
- export interface ChoiceDelta {
75
- role?: string;
76
- content?: string | null;
77
- reasoning_content?: string | null;
78
- tool_calls?: ToolCall[];
79
- }
80
-
81
- export interface Choice {
82
- index: number;
83
- delta?: ChoiceDelta;
84
- message?: ChoiceDelta;
85
- finish_reason: string | null;
86
- }
87
-
88
- export interface Usage {
89
- prompt_tokens: number;
90
- completion_tokens: number;
91
- total_tokens: number;
92
- prompt_tokens_details?: {
93
- cached_tokens: number;
94
- text_tokens?: number;
95
- };
96
- completion_tokens_details?: {
97
- reasoning_tokens?: number;
98
- text_tokens?: number;
99
- };
100
- }
101
-
102
- export interface ChatCompletionChunk {
103
- id: string;
104
- object: string;
105
- created: number;
106
- model: string;
107
- choices: Choice[];
108
- usage?: Usage;
109
- }
1
+ import type { JsonSchema, FunctionToolDefinition } from "../tools/types.ts";
2
+ export type { JsonSchema, FunctionToolDefinition };
3
+
4
+ /** Tool choice options */
5
+ export type ToolChoice =
6
+ | "auto"
7
+ | "none"
8
+ | "required"
9
+ | {
10
+ type: "function";
11
+ function: { name: string };
12
+ };
13
+
14
+ // --- Message Types ---
15
+
16
+ export interface ToolCallFunction {
17
+ name: string;
18
+ arguments: string;
19
+ }
20
+
21
+ export interface MessageToolCall {
22
+ id: string;
23
+ type: "function";
24
+ function: ToolCallFunction;
25
+ }
26
+
27
+ export interface Message {
28
+ role: string;
29
+ content: string | null;
30
+ /** Present on assistant messages that invoked tools */
31
+ tool_calls?: MessageToolCall[];
32
+ /** Present on tool/function response messages to link back to a call */
33
+ tool_call_id?: string;
34
+ /** Present on tool/function response messages */
35
+ name?: string;
36
+ /** Reasoning content for thinking models */
37
+ reasoning_content?: string;
38
+ }
39
+
40
+ // --- Request Types ---
41
+
42
+ export interface OpenAIRequest {
43
+ model: string;
44
+ messages: Message[];
45
+ stream?: boolean;
46
+ /** QwenProxy media extension used by image/video chat models. */
47
+ size?: string;
48
+ tools?: FunctionToolDefinition[];
49
+ tool_choice?: ToolChoice;
50
+ user?: string;
51
+ session_id?: string;
52
+ conversation_id?: string;
53
+ /** OpenAI chat spec: none|minimal|low|medium|high|xhigh|max (reasoning models). */
54
+ reasoning_effort?: string | null;
55
+ /** camelCase alias overlaid by OpenCode-style configs. */
56
+ reasoningEffort?: string | null;
57
+ stream_options?: {
58
+ include_usage?: boolean;
59
+ };
60
+ }
61
+
62
+ // --- Response Types ---
63
+
64
+ export interface ToolCall {
65
+ index: number;
66
+ id?: string;
67
+ type: string;
68
+ function: {
69
+ name: string;
70
+ arguments: string;
71
+ };
72
+ }
73
+
74
+ export interface ChoiceDelta {
75
+ role?: string;
76
+ content?: string | null;
77
+ reasoning_content?: string | null;
78
+ tool_calls?: ToolCall[];
79
+ }
80
+
81
+ export interface Choice {
82
+ index: number;
83
+ delta?: ChoiceDelta;
84
+ message?: ChoiceDelta;
85
+ finish_reason: string | null;
86
+ }
87
+
88
+ export interface Usage {
89
+ prompt_tokens: number;
90
+ completion_tokens: number;
91
+ total_tokens: number;
92
+ prompt_tokens_details?: {
93
+ cached_tokens: number;
94
+ text_tokens?: number;
95
+ };
96
+ completion_tokens_details?: {
97
+ reasoning_tokens?: number;
98
+ text_tokens?: number;
99
+ };
100
+ }
101
+
102
+ export interface ChatCompletionChunk {
103
+ id: string;
104
+ object: string;
105
+ created: number;
106
+ model: string;
107
+ choices: Choice[];
108
+ usage?: Usage;
109
+ }