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.
- package/LICENSE +14 -14
- package/README.md +906 -906
- package/bin/qwenproxy.js +5 -1
- package/package.json +77 -78
- package/src/api/error-classifier.ts +159 -159
- package/src/api/error-helpers.ts +118 -118
- package/src/api/models.ts +261 -261
- package/src/api/server.ts +860 -859
- package/src/cache/memory-cache.ts +385 -385
- package/src/clean-cache.ts +204 -204
- package/src/core/account-concurrency.ts +671 -671
- package/src/core/account-manager.ts +301 -297
- package/src/core/account-priority.ts +163 -163
- package/src/core/accounts.ts +186 -186
- package/src/core/config.ts +383 -383
- package/src/core/crypto-utils.ts +79 -79
- package/src/core/database.ts +276 -276
- package/src/core/errors.ts +118 -118
- package/src/core/logger.ts +269 -269
- package/src/core/memory-usage.ts +84 -84
- package/src/core/metrics.ts +291 -291
- package/src/core/model-alias.ts +77 -77
- package/src/core/model-registry.ts +544 -544
- package/src/core/mutex.ts +119 -119
- package/src/core/paths.ts +199 -199
- package/src/core/prompt-limits.ts +214 -214
- package/src/core/reasoning-effort.ts +102 -102
- package/src/core/stream-registry.ts +96 -96
- package/src/core/waf-isolation.ts +117 -117
- package/src/core/watchdog.ts +195 -195
- package/src/delete-chats.ts +23 -23
- package/src/index.ts +65 -64
- package/src/login.ts +147 -147
- package/src/reset-cooldowns.ts +11 -11
- package/src/routes/anthropic/index.ts +355 -355
- package/src/routes/anthropic/translate.ts +522 -522
- package/src/routes/anthropic/types.ts +154 -154
- package/src/routes/anthropic/validation.ts +144 -144
- package/src/routes/chat/account.ts +1817 -1817
- package/src/routes/chat/context.ts +241 -241
- package/src/routes/chat/errors.ts +85 -85
- package/src/routes/chat/helpers.ts +268 -268
- package/src/routes/chat/index.ts +618 -618
- package/src/routes/chat/media.ts +285 -285
- package/src/routes/chat/retry-policy.ts +754 -754
- package/src/routes/chat/stop.ts +98 -98
- package/src/routes/chat/streaming.ts +2710 -2710
- package/src/routes/chat/validation.ts +526 -526
- package/src/routes/chat.ts +2 -2
- package/src/routes/completions.ts +290 -290
- package/src/routes/images.ts +139 -139
- package/src/routes/responses/adapter.ts +503 -503
- package/src/routes/responses/index.ts +405 -405
- package/src/routes/responses/state.ts +230 -230
- package/src/routes/responses/streaming.ts +528 -528
- package/src/routes/responses/types.ts +285 -285
- package/src/routes/responses/validation.ts +202 -202
- package/src/routes/upload.ts +731 -731
- package/src/routes/videos.ts +214 -214
- package/src/services/auth-playwright.ts +173 -173
- package/src/services/captcha-coordinator.ts +161 -161
- package/src/services/captcha-solver.ts +553 -553
- package/src/services/chat-cleanup.ts +80 -80
- package/src/services/context-meter.ts +317 -317
- package/src/services/fingerprint.ts +242 -242
- package/src/services/human-behavior.ts +173 -173
- package/src/services/media-generation.ts +1748 -1748
- package/src/services/playwright.ts +2878 -2800
- package/src/services/qwen-chat-pool.ts +345 -345
- package/src/services/qwen-errors.ts +133 -133
- package/src/services/qwen-headers.ts +79 -79
- package/src/services/qwen-thread-state.ts +393 -393
- package/src/services/qwen-url.ts +19 -19
- package/src/services/qwen.ts +3126 -3126
- package/src/services/session-keeper.ts +88 -88
- package/src/services/token-estimation-metrics.ts +118 -118
- package/src/sync/claude-code.ts +75 -75
- package/src/sync/codex.ts +123 -123
- package/src/sync/index.ts +362 -362
- package/src/sync/omp.ts +105 -105
- package/src/sync/opencode.ts +214 -214
- package/src/sync/types.ts +53 -53
- package/src/sync/utils.ts +27 -27
- package/src/sync-clients.ts +189 -189
- package/src/tools/instructions.ts +137 -137
- package/src/tools/manifest.ts +81 -81
- package/src/tools/parser.ts +2989 -2989
- package/src/tools/toolcall-tags.ts +142 -142
- package/src/tui/app.ts +259 -264
- package/src/tui/index.ts +61 -61
- package/src/tui/markdown.ts +258 -258
- package/src/tui/proxy-client.ts +331 -326
- package/src/tui/screen.ts +294 -278
- package/src/tui/server-manager.ts +270 -270
- package/src/tui/theme.ts +432 -432
- package/src/tui/types.ts +33 -33
- package/src/tui/views/accounts-view.ts +656 -656
- package/src/tui/views/chat-view.ts +1018 -823
- package/src/tui/views/logs-view.ts +479 -413
- package/src/tui/views/status-view.ts +204 -204
- package/src/tui/views/storage-view.ts +304 -291
- package/src/tui/views/sync-view.ts +409 -409
- package/src/types/ali-oss.d.ts +32 -32
- package/src/update-cli.ts +121 -0
- package/src/utils/context-truncation.ts +84 -84
- package/src/utils/json.ts +380 -380
- package/src/utils/session-id.ts +37 -37
- package/src/utils/tool-call-guard.ts +84 -84
- package/src/utils/types.ts +109 -109
|
@@ -1,317 +1,317 @@
|
|
|
1
|
-
import { config } from "../core/config.ts";
|
|
2
|
-
import {
|
|
3
|
-
getModelCapabilities,
|
|
4
|
-
getModelContextWindow,
|
|
5
|
-
getModelContextWindowSource,
|
|
6
|
-
} from "../core/model-registry.ts";
|
|
7
|
-
import { estimateTokenCount } from "../utils/context-truncation.ts";
|
|
8
|
-
import type { PersonalizationEstimationInfo } from "./token-estimation-metrics.ts";
|
|
9
|
-
import type { Usage } from "../utils/types.ts";
|
|
10
|
-
|
|
11
|
-
export type ContextMeterMode = "full" | "delta" | "replay";
|
|
12
|
-
|
|
13
|
-
export interface ContextMeterOptions {
|
|
14
|
-
enabled: boolean;
|
|
15
|
-
windowTokens: number;
|
|
16
|
-
reportUsage: boolean;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface ContextMeterSnapshot {
|
|
20
|
-
enabled: true;
|
|
21
|
-
estimate: "heuristic";
|
|
22
|
-
model: string;
|
|
23
|
-
contextWindowTokens: number;
|
|
24
|
-
contextWindowSource: "configured" | "upstream" | "registry" | "default";
|
|
25
|
-
reservedOutputTokens: number;
|
|
26
|
-
usableInputTokens: number;
|
|
27
|
-
estimatedContextTokens: number;
|
|
28
|
-
estimatedRequestTokens: number;
|
|
29
|
-
estimatedContextPercent: number;
|
|
30
|
-
estimatedUsablePercent: number;
|
|
31
|
-
remainingContextTokens: number;
|
|
32
|
-
remainingUsableTokens: number;
|
|
33
|
-
fullPromptChars: number;
|
|
34
|
-
fullPromptBytes: number;
|
|
35
|
-
requestPromptChars: number;
|
|
36
|
-
requestPromptBytes: number;
|
|
37
|
-
qwenPayloadBytes: number | null;
|
|
38
|
-
qwenPayloadPromptChars: number | null;
|
|
39
|
-
qwenPayloadMessageCount: number | null;
|
|
40
|
-
messageCount: number | null;
|
|
41
|
-
fullMessageCount: number | null;
|
|
42
|
-
toolsCount: number;
|
|
43
|
-
filesCount: number;
|
|
44
|
-
personalizationBytes: number;
|
|
45
|
-
personalizationTokens: number;
|
|
46
|
-
mode: ContextMeterMode;
|
|
47
|
-
upstreamPromptTokens: number | null;
|
|
48
|
-
upstreamContextPercent: number | null;
|
|
49
|
-
upstreamRemainingContextTokens: number | null;
|
|
50
|
-
reportedPromptTokens: number | null;
|
|
51
|
-
measurementSource: "qwen" | "local_estimate" | "unavailable";
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface BuildContextMeterInput {
|
|
55
|
-
modelId: string;
|
|
56
|
-
accountId?: string;
|
|
57
|
-
requestPrompt: string;
|
|
58
|
-
fullPrompt: string;
|
|
59
|
-
mode: ContextMeterMode;
|
|
60
|
-
qwenPayloadBytes?: number;
|
|
61
|
-
qwenPayloadPromptChars?: number;
|
|
62
|
-
qwenPayloadMessageCount?: number;
|
|
63
|
-
messageCount?: number;
|
|
64
|
-
fullMessageCount?: number;
|
|
65
|
-
toolsCount?: number;
|
|
66
|
-
filesCount?: number;
|
|
67
|
-
activePersonalization?: PersonalizationEstimationInfo | null;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export interface MeteredUsage extends Usage {
|
|
71
|
-
context_meter: ContextMeterSnapshot;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function roundPercent(value: number): number {
|
|
75
|
-
return Number(value.toFixed(2));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function getReservedOutputTokens(
|
|
79
|
-
modelId: string,
|
|
80
|
-
accountId?: string,
|
|
81
|
-
): number {
|
|
82
|
-
const capabilities = getModelCapabilities(modelId, accountId);
|
|
83
|
-
return Math.max(
|
|
84
|
-
4_096,
|
|
85
|
-
capabilities.maxOutputTokens,
|
|
86
|
-
capabilities.maxThinkingTokens,
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export function getContextMeterOptions(): ContextMeterOptions {
|
|
91
|
-
return {
|
|
92
|
-
enabled: config.contextMeter.enabled,
|
|
93
|
-
windowTokens: config.contextMeter.windowTokens,
|
|
94
|
-
reportUsage: config.contextMeter.reportUsage,
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Build a request-time context estimate without serializing or retaining the
|
|
100
|
-
* prompt. `fullPrompt` is the complete history reconstructed from the client;
|
|
101
|
-
* `requestPrompt` is the delta or replay actually sent to Qwen.
|
|
102
|
-
*/
|
|
103
|
-
export function buildContextMeterSnapshot(
|
|
104
|
-
input: BuildContextMeterInput,
|
|
105
|
-
options: ContextMeterOptions = getContextMeterOptions(),
|
|
106
|
-
): ContextMeterSnapshot | null {
|
|
107
|
-
if (!options.enabled) return null;
|
|
108
|
-
|
|
109
|
-
const contextWindowTokens =
|
|
110
|
-
options.windowTokens > 0
|
|
111
|
-
? options.windowTokens
|
|
112
|
-
: getModelContextWindow(input.modelId, input.accountId);
|
|
113
|
-
const contextWindowSource =
|
|
114
|
-
options.windowTokens > 0
|
|
115
|
-
? "configured"
|
|
116
|
-
: getModelContextWindowSource(input.modelId, input.accountId);
|
|
117
|
-
const reservedOutputTokens = getReservedOutputTokens(
|
|
118
|
-
input.modelId,
|
|
119
|
-
input.accountId,
|
|
120
|
-
);
|
|
121
|
-
const usableInputTokens = Math.max(
|
|
122
|
-
1,
|
|
123
|
-
contextWindowTokens - reservedOutputTokens,
|
|
124
|
-
);
|
|
125
|
-
const estimatedContextTokens = estimateTokenCount(input.fullPrompt);
|
|
126
|
-
const estimatedRequestTokens = estimateTokenCount(input.requestPrompt);
|
|
127
|
-
const personalization = input.activePersonalization ?? null;
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
enabled: true,
|
|
131
|
-
estimate: "heuristic",
|
|
132
|
-
model: input.modelId,
|
|
133
|
-
contextWindowTokens,
|
|
134
|
-
contextWindowSource,
|
|
135
|
-
reservedOutputTokens,
|
|
136
|
-
usableInputTokens,
|
|
137
|
-
estimatedContextTokens,
|
|
138
|
-
estimatedRequestTokens,
|
|
139
|
-
estimatedContextPercent: roundPercent(
|
|
140
|
-
(estimatedContextTokens / contextWindowTokens) * 100,
|
|
141
|
-
),
|
|
142
|
-
estimatedUsablePercent: roundPercent(
|
|
143
|
-
(estimatedContextTokens / usableInputTokens) * 100,
|
|
144
|
-
),
|
|
145
|
-
remainingContextTokens: Math.max(
|
|
146
|
-
0,
|
|
147
|
-
contextWindowTokens - estimatedContextTokens,
|
|
148
|
-
),
|
|
149
|
-
remainingUsableTokens: Math.max(
|
|
150
|
-
0,
|
|
151
|
-
usableInputTokens - estimatedContextTokens,
|
|
152
|
-
),
|
|
153
|
-
fullPromptChars: input.fullPrompt.length,
|
|
154
|
-
fullPromptBytes: Buffer.byteLength(input.fullPrompt, "utf8"),
|
|
155
|
-
requestPromptChars: input.requestPrompt.length,
|
|
156
|
-
requestPromptBytes: Buffer.byteLength(input.requestPrompt, "utf8"),
|
|
157
|
-
qwenPayloadBytes: input.qwenPayloadBytes ?? null,
|
|
158
|
-
qwenPayloadPromptChars: input.qwenPayloadPromptChars ?? null,
|
|
159
|
-
qwenPayloadMessageCount: input.qwenPayloadMessageCount ?? null,
|
|
160
|
-
messageCount: input.messageCount ?? null,
|
|
161
|
-
fullMessageCount: input.fullMessageCount ?? null,
|
|
162
|
-
toolsCount: input.toolsCount ?? 0,
|
|
163
|
-
filesCount: input.filesCount ?? 0,
|
|
164
|
-
personalizationBytes: personalization?.bytes ?? 0,
|
|
165
|
-
personalizationTokens: personalization?.estimatedTokens ?? 0,
|
|
166
|
-
mode: input.mode,
|
|
167
|
-
upstreamPromptTokens: null,
|
|
168
|
-
upstreamContextPercent: null,
|
|
169
|
-
upstreamRemainingContextTokens: null,
|
|
170
|
-
reportedPromptTokens: null,
|
|
171
|
-
measurementSource: "local_estimate",
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Add the diagnostic extension without changing standard OpenAI usage by
|
|
177
|
-
* default. If reportUsage is enabled, prompt_tokens uses Qwen's measured
|
|
178
|
-
* input_tokens when available and falls back to the local estimate otherwise.
|
|
179
|
-
*/
|
|
180
|
-
export function enrichUsageWithContextMeter(
|
|
181
|
-
usage: Usage,
|
|
182
|
-
snapshot: ContextMeterSnapshot | null | undefined,
|
|
183
|
-
options: ContextMeterOptions = getContextMeterOptions(),
|
|
184
|
-
): Usage | MeteredUsage {
|
|
185
|
-
if (!snapshot || !options.enabled) return usage;
|
|
186
|
-
|
|
187
|
-
const upstreamPromptTokens =
|
|
188
|
-
Number.isFinite(usage.prompt_tokens) && usage.prompt_tokens > 0
|
|
189
|
-
? usage.prompt_tokens
|
|
190
|
-
: null;
|
|
191
|
-
const reportedPromptTokens = options.reportUsage
|
|
192
|
-
? (upstreamPromptTokens ?? snapshot.estimatedContextTokens)
|
|
193
|
-
: usage.prompt_tokens;
|
|
194
|
-
const contextMeter: ContextMeterSnapshot = {
|
|
195
|
-
...snapshot,
|
|
196
|
-
upstreamPromptTokens,
|
|
197
|
-
upstreamContextPercent:
|
|
198
|
-
upstreamPromptTokens !== null
|
|
199
|
-
? roundPercent((upstreamPromptTokens / snapshot.contextWindowTokens) * 100)
|
|
200
|
-
: null,
|
|
201
|
-
upstreamRemainingContextTokens:
|
|
202
|
-
upstreamPromptTokens !== null
|
|
203
|
-
? Math.max(0, snapshot.contextWindowTokens - upstreamPromptTokens)
|
|
204
|
-
: null,
|
|
205
|
-
reportedPromptTokens,
|
|
206
|
-
measurementSource: upstreamPromptTokens !== null ? "qwen" : "local_estimate",
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
if (!options.reportUsage) {
|
|
210
|
-
return {
|
|
211
|
-
...usage,
|
|
212
|
-
context_meter: contextMeter,
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const completionTokens = Math.max(0, usage.completion_tokens || 0);
|
|
217
|
-
return {
|
|
218
|
-
...usage,
|
|
219
|
-
prompt_tokens: reportedPromptTokens,
|
|
220
|
-
total_tokens: reportedPromptTokens + completionTokens,
|
|
221
|
-
prompt_tokens_details: {
|
|
222
|
-
...(usage.prompt_tokens_details ?? { cached_tokens: 0 }),
|
|
223
|
-
text_tokens: reportedPromptTokens,
|
|
224
|
-
},
|
|
225
|
-
context_meter: contextMeter,
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export function contextMeterLogData(
|
|
230
|
-
snapshot: ContextMeterSnapshot,
|
|
231
|
-
): Record<string, unknown> {
|
|
232
|
-
return {
|
|
233
|
-
model: snapshot.model,
|
|
234
|
-
mode: snapshot.mode,
|
|
235
|
-
estimate: snapshot.estimate,
|
|
236
|
-
estimatedContextTokens: snapshot.estimatedContextTokens,
|
|
237
|
-
estimatedRequestTokens: snapshot.estimatedRequestTokens,
|
|
238
|
-
contextWindowTokens: snapshot.contextWindowTokens,
|
|
239
|
-
contextWindowSource: snapshot.contextWindowSource,
|
|
240
|
-
measurementSource: snapshot.measurementSource,
|
|
241
|
-
estimatedContextPercent: snapshot.estimatedContextPercent,
|
|
242
|
-
estimatedUsablePercent: snapshot.estimatedUsablePercent,
|
|
243
|
-
remainingContextTokens: snapshot.remainingContextTokens,
|
|
244
|
-
remainingUsableTokens: snapshot.remainingUsableTokens,
|
|
245
|
-
fullPromptChars: snapshot.fullPromptChars,
|
|
246
|
-
fullPromptBytes: snapshot.fullPromptBytes,
|
|
247
|
-
requestPromptChars: snapshot.requestPromptChars,
|
|
248
|
-
requestPromptBytes: snapshot.requestPromptBytes,
|
|
249
|
-
qwenPayloadBytes: snapshot.qwenPayloadBytes,
|
|
250
|
-
qwenPayloadPromptChars: snapshot.qwenPayloadPromptChars,
|
|
251
|
-
qwenPayloadMessageCount: snapshot.qwenPayloadMessageCount,
|
|
252
|
-
messageCount: snapshot.messageCount,
|
|
253
|
-
fullMessageCount: snapshot.fullMessageCount,
|
|
254
|
-
toolsCount: snapshot.toolsCount,
|
|
255
|
-
filesCount: snapshot.filesCount,
|
|
256
|
-
personalizationBytes: snapshot.personalizationBytes,
|
|
257
|
-
personalizationTokens: snapshot.personalizationTokens,
|
|
258
|
-
upstreamPromptTokens: snapshot.upstreamPromptTokens,
|
|
259
|
-
upstreamContextPercent: snapshot.upstreamContextPercent,
|
|
260
|
-
upstreamRemainingContextTokens: snapshot.upstreamRemainingContextTokens,
|
|
261
|
-
reportedPromptTokens: snapshot.reportedPromptTokens,
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
export function getContextMeterHeaders(
|
|
266
|
-
snapshot: ContextMeterSnapshot | null | undefined,
|
|
267
|
-
): Record<string, string> {
|
|
268
|
-
if (!snapshot) return {};
|
|
269
|
-
|
|
270
|
-
const headers: Record<string, string> = {
|
|
271
|
-
"X-QwenProxy-Context-Meter": "enabled",
|
|
272
|
-
"X-QwenProxy-Context-Model": snapshot.model,
|
|
273
|
-
"X-QwenProxy-Context-Mode": snapshot.mode,
|
|
274
|
-
"X-QwenProxy-Context-Window-Tokens": String(snapshot.contextWindowTokens),
|
|
275
|
-
"X-QwenProxy-Context-Window-Source": snapshot.contextWindowSource,
|
|
276
|
-
"X-QwenProxy-Context-Measurement": snapshot.measurementSource,
|
|
277
|
-
"X-QwenProxy-Context-Estimated-Tokens": String(
|
|
278
|
-
snapshot.estimatedContextTokens,
|
|
279
|
-
),
|
|
280
|
-
"X-QwenProxy-Context-Percent": String(snapshot.estimatedContextPercent),
|
|
281
|
-
"X-QwenProxy-Context-Remaining-Tokens": String(
|
|
282
|
-
snapshot.remainingContextTokens,
|
|
283
|
-
),
|
|
284
|
-
"X-QwenProxy-Context-Full-Prompt-Bytes": String(
|
|
285
|
-
snapshot.fullPromptBytes,
|
|
286
|
-
),
|
|
287
|
-
"X-QwenProxy-Context-Request-Prompt-Bytes": String(
|
|
288
|
-
snapshot.requestPromptBytes,
|
|
289
|
-
),
|
|
290
|
-
"X-QwenProxy-Context-Qwen-Payload-Bytes": String(
|
|
291
|
-
snapshot.qwenPayloadBytes ?? 0,
|
|
292
|
-
),
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
if (snapshot.upstreamPromptTokens !== null) {
|
|
296
|
-
headers["X-QwenProxy-Context-Upstream-Tokens"] = String(
|
|
297
|
-
snapshot.upstreamPromptTokens,
|
|
298
|
-
);
|
|
299
|
-
}
|
|
300
|
-
if (snapshot.upstreamContextPercent !== null) {
|
|
301
|
-
headers["X-QwenProxy-Context-Upstream-Percent"] = String(
|
|
302
|
-
snapshot.upstreamContextPercent,
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
if (snapshot.upstreamRemainingContextTokens !== null) {
|
|
306
|
-
headers["X-QwenProxy-Context-Upstream-Remaining-Tokens"] = String(
|
|
307
|
-
snapshot.upstreamRemainingContextTokens,
|
|
308
|
-
);
|
|
309
|
-
}
|
|
310
|
-
if (snapshot.reportedPromptTokens !== null) {
|
|
311
|
-
headers["X-QwenProxy-Context-Reported-Tokens"] = String(
|
|
312
|
-
snapshot.reportedPromptTokens,
|
|
313
|
-
);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return headers;
|
|
317
|
-
}
|
|
1
|
+
import { config } from "../core/config.ts";
|
|
2
|
+
import {
|
|
3
|
+
getModelCapabilities,
|
|
4
|
+
getModelContextWindow,
|
|
5
|
+
getModelContextWindowSource,
|
|
6
|
+
} from "../core/model-registry.ts";
|
|
7
|
+
import { estimateTokenCount } from "../utils/context-truncation.ts";
|
|
8
|
+
import type { PersonalizationEstimationInfo } from "./token-estimation-metrics.ts";
|
|
9
|
+
import type { Usage } from "../utils/types.ts";
|
|
10
|
+
|
|
11
|
+
export type ContextMeterMode = "full" | "delta" | "replay";
|
|
12
|
+
|
|
13
|
+
export interface ContextMeterOptions {
|
|
14
|
+
enabled: boolean;
|
|
15
|
+
windowTokens: number;
|
|
16
|
+
reportUsage: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ContextMeterSnapshot {
|
|
20
|
+
enabled: true;
|
|
21
|
+
estimate: "heuristic";
|
|
22
|
+
model: string;
|
|
23
|
+
contextWindowTokens: number;
|
|
24
|
+
contextWindowSource: "configured" | "upstream" | "registry" | "default";
|
|
25
|
+
reservedOutputTokens: number;
|
|
26
|
+
usableInputTokens: number;
|
|
27
|
+
estimatedContextTokens: number;
|
|
28
|
+
estimatedRequestTokens: number;
|
|
29
|
+
estimatedContextPercent: number;
|
|
30
|
+
estimatedUsablePercent: number;
|
|
31
|
+
remainingContextTokens: number;
|
|
32
|
+
remainingUsableTokens: number;
|
|
33
|
+
fullPromptChars: number;
|
|
34
|
+
fullPromptBytes: number;
|
|
35
|
+
requestPromptChars: number;
|
|
36
|
+
requestPromptBytes: number;
|
|
37
|
+
qwenPayloadBytes: number | null;
|
|
38
|
+
qwenPayloadPromptChars: number | null;
|
|
39
|
+
qwenPayloadMessageCount: number | null;
|
|
40
|
+
messageCount: number | null;
|
|
41
|
+
fullMessageCount: number | null;
|
|
42
|
+
toolsCount: number;
|
|
43
|
+
filesCount: number;
|
|
44
|
+
personalizationBytes: number;
|
|
45
|
+
personalizationTokens: number;
|
|
46
|
+
mode: ContextMeterMode;
|
|
47
|
+
upstreamPromptTokens: number | null;
|
|
48
|
+
upstreamContextPercent: number | null;
|
|
49
|
+
upstreamRemainingContextTokens: number | null;
|
|
50
|
+
reportedPromptTokens: number | null;
|
|
51
|
+
measurementSource: "qwen" | "local_estimate" | "unavailable";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface BuildContextMeterInput {
|
|
55
|
+
modelId: string;
|
|
56
|
+
accountId?: string;
|
|
57
|
+
requestPrompt: string;
|
|
58
|
+
fullPrompt: string;
|
|
59
|
+
mode: ContextMeterMode;
|
|
60
|
+
qwenPayloadBytes?: number;
|
|
61
|
+
qwenPayloadPromptChars?: number;
|
|
62
|
+
qwenPayloadMessageCount?: number;
|
|
63
|
+
messageCount?: number;
|
|
64
|
+
fullMessageCount?: number;
|
|
65
|
+
toolsCount?: number;
|
|
66
|
+
filesCount?: number;
|
|
67
|
+
activePersonalization?: PersonalizationEstimationInfo | null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface MeteredUsage extends Usage {
|
|
71
|
+
context_meter: ContextMeterSnapshot;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function roundPercent(value: number): number {
|
|
75
|
+
return Number(value.toFixed(2));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function getReservedOutputTokens(
|
|
79
|
+
modelId: string,
|
|
80
|
+
accountId?: string,
|
|
81
|
+
): number {
|
|
82
|
+
const capabilities = getModelCapabilities(modelId, accountId);
|
|
83
|
+
return Math.max(
|
|
84
|
+
4_096,
|
|
85
|
+
capabilities.maxOutputTokens,
|
|
86
|
+
capabilities.maxThinkingTokens,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function getContextMeterOptions(): ContextMeterOptions {
|
|
91
|
+
return {
|
|
92
|
+
enabled: config.contextMeter.enabled,
|
|
93
|
+
windowTokens: config.contextMeter.windowTokens,
|
|
94
|
+
reportUsage: config.contextMeter.reportUsage,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Build a request-time context estimate without serializing or retaining the
|
|
100
|
+
* prompt. `fullPrompt` is the complete history reconstructed from the client;
|
|
101
|
+
* `requestPrompt` is the delta or replay actually sent to Qwen.
|
|
102
|
+
*/
|
|
103
|
+
export function buildContextMeterSnapshot(
|
|
104
|
+
input: BuildContextMeterInput,
|
|
105
|
+
options: ContextMeterOptions = getContextMeterOptions(),
|
|
106
|
+
): ContextMeterSnapshot | null {
|
|
107
|
+
if (!options.enabled) return null;
|
|
108
|
+
|
|
109
|
+
const contextWindowTokens =
|
|
110
|
+
options.windowTokens > 0
|
|
111
|
+
? options.windowTokens
|
|
112
|
+
: getModelContextWindow(input.modelId, input.accountId);
|
|
113
|
+
const contextWindowSource =
|
|
114
|
+
options.windowTokens > 0
|
|
115
|
+
? "configured"
|
|
116
|
+
: getModelContextWindowSource(input.modelId, input.accountId);
|
|
117
|
+
const reservedOutputTokens = getReservedOutputTokens(
|
|
118
|
+
input.modelId,
|
|
119
|
+
input.accountId,
|
|
120
|
+
);
|
|
121
|
+
const usableInputTokens = Math.max(
|
|
122
|
+
1,
|
|
123
|
+
contextWindowTokens - reservedOutputTokens,
|
|
124
|
+
);
|
|
125
|
+
const estimatedContextTokens = estimateTokenCount(input.fullPrompt);
|
|
126
|
+
const estimatedRequestTokens = estimateTokenCount(input.requestPrompt);
|
|
127
|
+
const personalization = input.activePersonalization ?? null;
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
enabled: true,
|
|
131
|
+
estimate: "heuristic",
|
|
132
|
+
model: input.modelId,
|
|
133
|
+
contextWindowTokens,
|
|
134
|
+
contextWindowSource,
|
|
135
|
+
reservedOutputTokens,
|
|
136
|
+
usableInputTokens,
|
|
137
|
+
estimatedContextTokens,
|
|
138
|
+
estimatedRequestTokens,
|
|
139
|
+
estimatedContextPercent: roundPercent(
|
|
140
|
+
(estimatedContextTokens / contextWindowTokens) * 100,
|
|
141
|
+
),
|
|
142
|
+
estimatedUsablePercent: roundPercent(
|
|
143
|
+
(estimatedContextTokens / usableInputTokens) * 100,
|
|
144
|
+
),
|
|
145
|
+
remainingContextTokens: Math.max(
|
|
146
|
+
0,
|
|
147
|
+
contextWindowTokens - estimatedContextTokens,
|
|
148
|
+
),
|
|
149
|
+
remainingUsableTokens: Math.max(
|
|
150
|
+
0,
|
|
151
|
+
usableInputTokens - estimatedContextTokens,
|
|
152
|
+
),
|
|
153
|
+
fullPromptChars: input.fullPrompt.length,
|
|
154
|
+
fullPromptBytes: Buffer.byteLength(input.fullPrompt, "utf8"),
|
|
155
|
+
requestPromptChars: input.requestPrompt.length,
|
|
156
|
+
requestPromptBytes: Buffer.byteLength(input.requestPrompt, "utf8"),
|
|
157
|
+
qwenPayloadBytes: input.qwenPayloadBytes ?? null,
|
|
158
|
+
qwenPayloadPromptChars: input.qwenPayloadPromptChars ?? null,
|
|
159
|
+
qwenPayloadMessageCount: input.qwenPayloadMessageCount ?? null,
|
|
160
|
+
messageCount: input.messageCount ?? null,
|
|
161
|
+
fullMessageCount: input.fullMessageCount ?? null,
|
|
162
|
+
toolsCount: input.toolsCount ?? 0,
|
|
163
|
+
filesCount: input.filesCount ?? 0,
|
|
164
|
+
personalizationBytes: personalization?.bytes ?? 0,
|
|
165
|
+
personalizationTokens: personalization?.estimatedTokens ?? 0,
|
|
166
|
+
mode: input.mode,
|
|
167
|
+
upstreamPromptTokens: null,
|
|
168
|
+
upstreamContextPercent: null,
|
|
169
|
+
upstreamRemainingContextTokens: null,
|
|
170
|
+
reportedPromptTokens: null,
|
|
171
|
+
measurementSource: "local_estimate",
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Add the diagnostic extension without changing standard OpenAI usage by
|
|
177
|
+
* default. If reportUsage is enabled, prompt_tokens uses Qwen's measured
|
|
178
|
+
* input_tokens when available and falls back to the local estimate otherwise.
|
|
179
|
+
*/
|
|
180
|
+
export function enrichUsageWithContextMeter(
|
|
181
|
+
usage: Usage,
|
|
182
|
+
snapshot: ContextMeterSnapshot | null | undefined,
|
|
183
|
+
options: ContextMeterOptions = getContextMeterOptions(),
|
|
184
|
+
): Usage | MeteredUsage {
|
|
185
|
+
if (!snapshot || !options.enabled) return usage;
|
|
186
|
+
|
|
187
|
+
const upstreamPromptTokens =
|
|
188
|
+
Number.isFinite(usage.prompt_tokens) && usage.prompt_tokens > 0
|
|
189
|
+
? usage.prompt_tokens
|
|
190
|
+
: null;
|
|
191
|
+
const reportedPromptTokens = options.reportUsage
|
|
192
|
+
? (upstreamPromptTokens ?? snapshot.estimatedContextTokens)
|
|
193
|
+
: usage.prompt_tokens;
|
|
194
|
+
const contextMeter: ContextMeterSnapshot = {
|
|
195
|
+
...snapshot,
|
|
196
|
+
upstreamPromptTokens,
|
|
197
|
+
upstreamContextPercent:
|
|
198
|
+
upstreamPromptTokens !== null
|
|
199
|
+
? roundPercent((upstreamPromptTokens / snapshot.contextWindowTokens) * 100)
|
|
200
|
+
: null,
|
|
201
|
+
upstreamRemainingContextTokens:
|
|
202
|
+
upstreamPromptTokens !== null
|
|
203
|
+
? Math.max(0, snapshot.contextWindowTokens - upstreamPromptTokens)
|
|
204
|
+
: null,
|
|
205
|
+
reportedPromptTokens,
|
|
206
|
+
measurementSource: upstreamPromptTokens !== null ? "qwen" : "local_estimate",
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
if (!options.reportUsage) {
|
|
210
|
+
return {
|
|
211
|
+
...usage,
|
|
212
|
+
context_meter: contextMeter,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const completionTokens = Math.max(0, usage.completion_tokens || 0);
|
|
217
|
+
return {
|
|
218
|
+
...usage,
|
|
219
|
+
prompt_tokens: reportedPromptTokens,
|
|
220
|
+
total_tokens: reportedPromptTokens + completionTokens,
|
|
221
|
+
prompt_tokens_details: {
|
|
222
|
+
...(usage.prompt_tokens_details ?? { cached_tokens: 0 }),
|
|
223
|
+
text_tokens: reportedPromptTokens,
|
|
224
|
+
},
|
|
225
|
+
context_meter: contextMeter,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function contextMeterLogData(
|
|
230
|
+
snapshot: ContextMeterSnapshot,
|
|
231
|
+
): Record<string, unknown> {
|
|
232
|
+
return {
|
|
233
|
+
model: snapshot.model,
|
|
234
|
+
mode: snapshot.mode,
|
|
235
|
+
estimate: snapshot.estimate,
|
|
236
|
+
estimatedContextTokens: snapshot.estimatedContextTokens,
|
|
237
|
+
estimatedRequestTokens: snapshot.estimatedRequestTokens,
|
|
238
|
+
contextWindowTokens: snapshot.contextWindowTokens,
|
|
239
|
+
contextWindowSource: snapshot.contextWindowSource,
|
|
240
|
+
measurementSource: snapshot.measurementSource,
|
|
241
|
+
estimatedContextPercent: snapshot.estimatedContextPercent,
|
|
242
|
+
estimatedUsablePercent: snapshot.estimatedUsablePercent,
|
|
243
|
+
remainingContextTokens: snapshot.remainingContextTokens,
|
|
244
|
+
remainingUsableTokens: snapshot.remainingUsableTokens,
|
|
245
|
+
fullPromptChars: snapshot.fullPromptChars,
|
|
246
|
+
fullPromptBytes: snapshot.fullPromptBytes,
|
|
247
|
+
requestPromptChars: snapshot.requestPromptChars,
|
|
248
|
+
requestPromptBytes: snapshot.requestPromptBytes,
|
|
249
|
+
qwenPayloadBytes: snapshot.qwenPayloadBytes,
|
|
250
|
+
qwenPayloadPromptChars: snapshot.qwenPayloadPromptChars,
|
|
251
|
+
qwenPayloadMessageCount: snapshot.qwenPayloadMessageCount,
|
|
252
|
+
messageCount: snapshot.messageCount,
|
|
253
|
+
fullMessageCount: snapshot.fullMessageCount,
|
|
254
|
+
toolsCount: snapshot.toolsCount,
|
|
255
|
+
filesCount: snapshot.filesCount,
|
|
256
|
+
personalizationBytes: snapshot.personalizationBytes,
|
|
257
|
+
personalizationTokens: snapshot.personalizationTokens,
|
|
258
|
+
upstreamPromptTokens: snapshot.upstreamPromptTokens,
|
|
259
|
+
upstreamContextPercent: snapshot.upstreamContextPercent,
|
|
260
|
+
upstreamRemainingContextTokens: snapshot.upstreamRemainingContextTokens,
|
|
261
|
+
reportedPromptTokens: snapshot.reportedPromptTokens,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function getContextMeterHeaders(
|
|
266
|
+
snapshot: ContextMeterSnapshot | null | undefined,
|
|
267
|
+
): Record<string, string> {
|
|
268
|
+
if (!snapshot) return {};
|
|
269
|
+
|
|
270
|
+
const headers: Record<string, string> = {
|
|
271
|
+
"X-QwenProxy-Context-Meter": "enabled",
|
|
272
|
+
"X-QwenProxy-Context-Model": snapshot.model,
|
|
273
|
+
"X-QwenProxy-Context-Mode": snapshot.mode,
|
|
274
|
+
"X-QwenProxy-Context-Window-Tokens": String(snapshot.contextWindowTokens),
|
|
275
|
+
"X-QwenProxy-Context-Window-Source": snapshot.contextWindowSource,
|
|
276
|
+
"X-QwenProxy-Context-Measurement": snapshot.measurementSource,
|
|
277
|
+
"X-QwenProxy-Context-Estimated-Tokens": String(
|
|
278
|
+
snapshot.estimatedContextTokens,
|
|
279
|
+
),
|
|
280
|
+
"X-QwenProxy-Context-Percent": String(snapshot.estimatedContextPercent),
|
|
281
|
+
"X-QwenProxy-Context-Remaining-Tokens": String(
|
|
282
|
+
snapshot.remainingContextTokens,
|
|
283
|
+
),
|
|
284
|
+
"X-QwenProxy-Context-Full-Prompt-Bytes": String(
|
|
285
|
+
snapshot.fullPromptBytes,
|
|
286
|
+
),
|
|
287
|
+
"X-QwenProxy-Context-Request-Prompt-Bytes": String(
|
|
288
|
+
snapshot.requestPromptBytes,
|
|
289
|
+
),
|
|
290
|
+
"X-QwenProxy-Context-Qwen-Payload-Bytes": String(
|
|
291
|
+
snapshot.qwenPayloadBytes ?? 0,
|
|
292
|
+
),
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
if (snapshot.upstreamPromptTokens !== null) {
|
|
296
|
+
headers["X-QwenProxy-Context-Upstream-Tokens"] = String(
|
|
297
|
+
snapshot.upstreamPromptTokens,
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
if (snapshot.upstreamContextPercent !== null) {
|
|
301
|
+
headers["X-QwenProxy-Context-Upstream-Percent"] = String(
|
|
302
|
+
snapshot.upstreamContextPercent,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
if (snapshot.upstreamRemainingContextTokens !== null) {
|
|
306
|
+
headers["X-QwenProxy-Context-Upstream-Remaining-Tokens"] = String(
|
|
307
|
+
snapshot.upstreamRemainingContextTokens,
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
if (snapshot.reportedPromptTokens !== null) {
|
|
311
|
+
headers["X-QwenProxy-Context-Reported-Tokens"] = String(
|
|
312
|
+
snapshot.reportedPromptTokens,
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return headers;
|
|
317
|
+
}
|