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,173 @@
1
+ import type { Page } from "patchright";
2
+
3
+ export const sleep = (ms: number): Promise<void> =>
4
+ new Promise((resolve) => setTimeout(resolve, ms));
5
+
6
+ export function humanDelay(
7
+ minMs: number,
8
+ maxMs: number,
9
+ rng: () => number = Math.random,
10
+ ): number {
11
+ if (maxMs <= minMs) return minMs;
12
+ const midpoint = (minMs + maxMs) / 2;
13
+ const jitter = (rng() - 0.5) * (maxMs - minMs);
14
+ return Math.round(Math.max(minMs, Math.min(maxMs, midpoint + jitter)));
15
+ }
16
+
17
+ /**
18
+ * Box-Muller gaussian sample (mean 0, sigma 1). Uniform jitter is itself a bot
19
+ * signature: real pointer noise clusters around zero, so the Baxia slider
20
+ * scorer treats a flat distribution as synthetic.
21
+ */
22
+ export function gaussianNoise(rng: () => number = Math.random): number {
23
+ let u = 0;
24
+ let v = 0;
25
+ while (u === 0) u = rng();
26
+ while (v === 0) v = rng();
27
+ return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
28
+ }
29
+
30
+ /**
31
+ * Log-normal delay: most steps are quick, a few are long — the heavy tail real
32
+ * hands produce. Linear/uniform sleeps read as a metronome to the scorer.
33
+ */
34
+ export function logNormalDelay(
35
+ medianMs: number,
36
+ sigma: number,
37
+ rng: () => number = Math.random,
38
+ ): number {
39
+ return Math.max(1, Math.round(medianMs * Math.exp(sigma * gaussianNoise(rng))));
40
+ }
41
+
42
+ /** One pointer sample of a slider drag. */
43
+ export interface DragSample {
44
+ x: number;
45
+ y: number;
46
+ /** Delay to wait BEFORE emitting this sample. */
47
+ delayMs: number;
48
+ }
49
+
50
+ /**
51
+ * Pure trajectory generator for a human slider drag.
52
+ *
53
+ * Baxia scores the *shape* of the drag, not just its endpoints, so the path
54
+ * must reproduce the three things a real hand does that a linear ramp does not:
55
+ * an acceleration ramp, an overshoot past the target followed by a correction
56
+ * phase, and dwell pauses mid-path.
57
+ */
58
+ export function buildDragTrajectory(
59
+ startX: number,
60
+ startY: number,
61
+ endX: number,
62
+ endY: number,
63
+ rng: () => number = Math.random,
64
+ ): DragSample[] {
65
+ const distance = Math.hypot(endX - startX, endY - startY);
66
+ const steps = Math.max(30, Math.min(50, Math.round(distance / 6)));
67
+ const samples: DragSample[] = [];
68
+
69
+ // Overshoot target: real hands decelerate late and pass the mark, then
70
+ // correct. ~12% of the remaining distance, clamped so it stays on-page.
71
+ const overshootX = endX + Math.max(4, distance * 0.12);
72
+ const correctionSteps = Math.max(4, Math.round(steps * 0.2));
73
+
74
+ for (let step = 1; step <= steps; step++) {
75
+ const progress = step / steps;
76
+ // Cubic ease-in-out with an acceleration ramp: slow start, fast middle.
77
+ const eased =
78
+ progress < 0.5
79
+ ? 4 * progress * progress * progress
80
+ : 1 - Math.pow(-2 * progress + 2, 3) / 2;
81
+ const ramp = 0.7 + Math.min(0.6, progress * 1.3);
82
+
83
+ const targetX = startX + (overshootX - startX) * eased;
84
+ const jitterY = gaussianNoise(rng) * 1.2;
85
+
86
+ // Micro-pauses: humans hesitate at roughly a third and two thirds of a
87
+ // deliberate drag.
88
+ const nearPause =
89
+ Math.abs(progress - 0.3) < 1 / steps || Math.abs(progress - 0.65) < 1 / steps;
90
+ const delayMs = nearPause
91
+ ? logNormalDelay(90, 0.5, rng)
92
+ : logNormalDelay(14 / ramp, 0.45, rng);
93
+
94
+ samples.push({
95
+ x: targetX,
96
+ y: startY + (endY - startY) * eased + jitterY,
97
+ delayMs,
98
+ });
99
+ }
100
+
101
+ // Correction phase: ease the pointer back from the overshoot onto the target.
102
+ for (let step = 1; step <= correctionSteps; step++) {
103
+ const progress = step / correctionSteps;
104
+ const eased = 1 - Math.pow(1 - progress, 2);
105
+ samples.push({
106
+ x: overshootX + (endX - overshootX) * eased,
107
+ y: endY + gaussianNoise(rng) * 0.8,
108
+ delayMs: logNormalDelay(22, 0.4, rng),
109
+ });
110
+ }
111
+
112
+ // Land exactly on the target so the slider registers the release in-range.
113
+ samples.push({ x: endX, y: endY, delayMs: logNormalDelay(18, 0.3, rng) });
114
+ return samples;
115
+ }
116
+
117
+ export async function humanDrag(
118
+ page: Page,
119
+ startX: number,
120
+ startY: number,
121
+ endX: number,
122
+ endY: number,
123
+ ): Promise<void> {
124
+ // Approach with its own path, then dwell before pressing: a pointer that
125
+ // teleports onto the handle and clicks instantly is the classic automation
126
+ // signature.
127
+ await page.mouse.move(startX, startY, { steps: 8 });
128
+ await sleep(logNormalDelay(160, 0.5));
129
+ await page.mouse.down();
130
+ await sleep(logNormalDelay(120, 0.5));
131
+
132
+ try {
133
+ for (const sample of buildDragTrajectory(startX, startY, endX, endY)) {
134
+ await sleep(sample.delayMs);
135
+ await page.mouse.move(sample.x, sample.y, { steps: 1 });
136
+ }
137
+ // Dwell before release — humans verify the handle is in place.
138
+ await sleep(logNormalDelay(220, 0.5));
139
+ } finally {
140
+ await page.mouse.up();
141
+ }
142
+ }
143
+
144
+ export async function subtlePageActivity(page: Page): Promise<void> {
145
+ if (page.isClosed()) return;
146
+
147
+ const viewport = page.viewportSize();
148
+ if (!viewport) return;
149
+
150
+ const x = Math.floor(viewport.width * (0.25 + Math.random() * 0.5));
151
+ const y = Math.floor(viewport.height * (0.25 + Math.random() * 0.5));
152
+ await page.mouse.move(x, y, { steps: 6 + Math.floor(Math.random() * 8) });
153
+
154
+ if (Math.random() < 0.35) {
155
+ await page.mouse.wheel(0, Math.random() < 0.5 ? 60 : -60).catch(() => {});
156
+ }
157
+
158
+ await page
159
+ .evaluate(() => {
160
+ try {
161
+ const target = document.querySelector(
162
+ '[data-testid="sidebar"], .sidebar, nav, aside, main',
163
+ );
164
+ if (target) {
165
+ target.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true }));
166
+ target.dispatchEvent(new MouseEvent("mouseleave", { bubbles: true }));
167
+ }
168
+ } catch {
169
+ // Best-effort keep-alive only.
170
+ }
171
+ })
172
+ .catch(() => {});
173
+ }