@bitkyc08/opencodex 2.95.0 → 2.97.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.
- package/bin/ocx.mjs +214 -19
- package/gui/dist/assets/{App-8yedBkhm.js → App-DuNhggE1.js} +18 -18
- package/gui/dist/assets/{Tray-erSMzxPG.js → Tray-CEw0y0jR.js} +1 -1
- package/gui/dist/assets/index-CynE_Rue.js +94 -0
- package/gui/dist/assets/{quota-summary-BK4VuQNc.js → quota-summary-VHFSlUuM.js} +1 -1
- package/gui/dist/index.html +1 -1
- package/package.json +1 -1
- package/src/adapters/command-code.ts +15 -16
- package/src/adapters/cursor/transport-retry.ts +2 -2
- package/src/adapters/devin/cloud-direct/chat.ts +15 -6
- package/src/adapters/kiro/stream.ts +63 -4
- package/src/adapters/kiro-constants.ts +15 -1
- package/src/adapters/kiro-events.ts +31 -1
- package/src/adapters/kiro-retry.ts +3 -3
- package/src/adapters/ollama-native.ts +21 -7
- package/src/adapters/openai-responses/canonical-forward.ts +50 -13
- package/src/adapters/openai-responses/empty-assistant.ts +15 -3
- package/src/adapters/openai-responses/passthrough.ts +8 -8
- package/src/adapters/openai-responses/reasoning.ts +9 -4
- package/src/adapters/openai-responses/request-strips.ts +1 -1
- package/src/adapters/openai-responses/transport.ts +17 -26
- package/src/adapters/physical-send.ts +3 -3
- package/src/adapters/qoder/adapter.ts +1 -1
- package/src/adapters/qoder/image-input.ts +5 -2
- package/src/adapters/zed-openai-stream.ts +2 -1
- package/src/bridge/errors.ts +11 -2
- package/src/bridge/response-json.ts +2 -1
- package/src/bridge/sse.ts +14 -6
- package/src/chat/failure-verdict.ts +21 -0
- package/src/chat/outbound.ts +139 -21
- package/src/claude/outbound.ts +49 -2
- package/src/cli/agent.ts +44 -15
- package/src/cli/capabilities-agents-routing.ts +9 -0
- package/src/cli/capabilities-base.ts +25 -3
- package/src/cli/capabilities-command.ts +12 -3
- package/src/cli/capability-contracts.ts +2 -0
- package/src/cli/codex-launch-auth.ts +30 -1
- package/src/cli/codex-launcher.ts +12 -5
- package/src/cli/codex-shim-autorestore.ts +5 -4
- package/src/cli/dispatch.ts +11 -176
- package/src/cli/ensure-desired-integrations.ts +4 -3
- package/src/cli/help.ts +2 -2
- package/src/cli/index.ts +9 -2
- package/src/cli/link.ts +1 -1
- package/src/cli/message-command.ts +2 -1
- package/src/cli/message-mcp-command.ts +29 -0
- package/src/cli/registry.ts +12 -4
- package/src/cli/root.ts +8 -0
- package/src/cli/sync-command.ts +243 -0
- package/src/cli/sync-fanout-process.ts +92 -0
- package/src/cli/sync-fanout.ts +150 -0
- package/src/cli/sync-flags.ts +10 -0
- package/src/cli/sync-link-status.ts +39 -0
- package/src/cli/sync-update-guard.ts +18 -0
- package/src/client/connect.ts +31 -14
- package/src/client/link-home.ts +3 -2
- package/src/client/machine-listener.ts +3 -0
- package/src/client/restore-ownership.ts +19 -1
- package/src/client/runtime-restart.ts +12 -4
- package/src/client/runtime.ts +40 -11
- package/src/client/stop-routing.ts +62 -0
- package/src/client/sync-runtime.ts +65 -0
- package/src/clients/config-export.ts +32 -3
- package/src/codex/account-store.ts +1 -0
- package/src/codex/auth-context-provider.ts +15 -0
- package/src/codex/auth-context.ts +59 -122
- package/src/codex/catalog/antigravity-discovery.ts +16 -5
- package/src/codex/catalog/provider-models.ts +6 -8
- package/src/codex/forward-transport-headers.ts +65 -36
- package/src/codex/inject/config-toml.ts +88 -0
- package/src/codex/inject/multi-agent-v2.ts +30 -4
- package/src/codex/inject/plan.ts +34 -7
- package/src/codex/inject-coordination.ts +11 -0
- package/src/codex/inject.ts +18 -7
- package/src/codex/installation-id.ts +72 -0
- package/src/codex/lineage.ts +13 -7
- package/src/codex/quota.ts +22 -6
- package/src/codex/request-owned-affinity-key.ts +11 -0
- package/src/codex/request-owned-affinity.ts +118 -0
- package/src/codex/request-owned-main-pin.ts +86 -0
- package/src/codex/role-capability.ts +52 -41
- package/src/codex/routing/active-account.ts +13 -5
- package/src/codex/routing/selection.ts +40 -1
- package/src/codex/routing/thread-affinity-store.ts +916 -0
- package/src/codex/routing/thread-affinity.ts +90 -12
- package/src/codex/toml-source-lines.ts +72 -13
- package/src/combos/failover.ts +6 -109
- package/src/combos/request.ts +2 -2
- package/src/combos/resolve.ts +29 -11
- package/src/config/process-state.ts +5 -0
- package/src/config/schema/leaf-validators.ts +1 -0
- package/src/generated/compatibility-version.json +496 -192
- package/src/grok/sync.ts +10 -1
- package/src/images/loop.ts +2 -1
- package/src/lib/client-error-body.ts +184 -0
- package/src/lib/client-retry-after.ts +66 -0
- package/src/lib/config-ownership.ts +4 -0
- package/src/lib/context-overflow-verdict.ts +113 -0
- package/src/lib/desktop-cli-handoff.d.mts +1 -1
- package/src/lib/desktop-cli-handoff.mjs +11 -1
- package/src/lib/empty-tool-catalog.ts +25 -3
- package/src/lib/error-destination.ts +47 -0
- package/src/lib/failure-detail.ts +66 -0
- package/src/lib/model-identity.ts +44 -0
- package/src/lib/private-sqlite-path.ts +124 -0
- package/src/lib/response-service-tier.ts +17 -0
- package/src/lib/retry-after.ts +3 -0
- package/src/lib/spend-ledger-owner.ts +43 -4
- package/src/lib/upstream-condition.ts +142 -0
- package/src/lib/upstream-retry.ts +14 -11
- package/src/lib/windows-owner-acl.ts +13 -6
- package/src/lib/windows-secret-acl.ts +2 -2
- package/src/link/remote-platform.ts +14 -0
- package/src/link/remote-windows.ts +51 -9
- package/src/link/ssh-argv.ts +63 -10
- package/src/messaging/mcp-queue-rpc.ts +102 -0
- package/src/messaging/mcp-queue-server.ts +42 -0
- package/src/messaging/mcp-queue-tools.ts +39 -0
- package/src/oauth/anthropic-identity.ts +2 -6
- package/src/oauth/generic-account-failover.ts +13 -11
- package/src/oauth/index.ts +41 -74
- package/src/oauth/oauth-errors.ts +67 -0
- package/src/oauth/store.ts +1 -1
- package/src/protocols/codecs/ollama.ts +197 -0
- package/src/protocols/encoders/adapter-events.ts +4 -0
- package/src/protocols/encoders/chat.ts +18 -10
- package/src/protocols/trace.ts +18 -0
- package/src/providers/anthropic-subscription.ts +25 -0
- package/src/providers/claude-cli-usage.ts +15 -67
- package/src/providers/fastwire.ts +3 -2
- package/src/providers/quota/anthropic-decision-windows.ts +5 -19
- package/src/providers/quota/anthropic-usage-windows.ts +65 -0
- package/src/providers/quota/vendor-probes-key.ts +60 -24
- package/src/providers/request-pacing.ts +3 -2
- package/src/responses/apply-patch-envelope.ts +45 -19
- package/src/responses/compaction-history-framing.ts +5 -1
- package/src/responses/freeform-wrapper-scan.ts +5 -3
- package/src/responses/parser-tools.ts +1 -0
- package/src/responses/progressive-freeform-input.ts +354 -45
- package/src/responses/schema.ts +5 -2
- package/src/routing/fallback-policy.ts +3 -1
- package/src/server/admission-model-scope.ts +17 -6
- package/src/server/admitted-http-turn.ts +56 -0
- package/src/server/audio-dictation.ts +3 -0
- package/src/server/audio-live.ts +8 -2
- package/src/server/audio-upstream.ts +2 -0
- package/src/server/auth-cors.ts +5 -0
- package/src/server/chat-completions.ts +45 -20
- package/src/server/chat-native-sse.ts +52 -7
- package/src/server/chat-native.ts +60 -35
- package/src/server/claude-bridge-pool-session.ts +59 -0
- package/src/server/claude-messages.ts +57 -27
- package/src/server/context-history.ts +14 -7
- package/src/server/images.ts +14 -5
- package/src/server/inbound-body-admission.ts +11 -4
- package/src/server/index/live-sideband.ts +16 -2
- package/src/server/index/ollama-routes.ts +89 -0
- package/src/server/index/serve-options.ts +22 -4
- package/src/server/index/websocket-handler.ts +5 -3
- package/src/server/index.ts +6 -36
- package/src/server/inference/client-encoder-delivery.ts +4 -2
- package/src/server/json-serialized-bytes.ts +44 -0
- package/src/server/live.ts +30 -5
- package/src/server/management/agent-settings-routes.ts +24 -21
- package/src/server/management/codex-cli-update-routes.ts +2 -1
- package/src/server/management/route-registry.ts +1 -1
- package/src/server/messages-native-scope.ts +2 -1
- package/src/server/messages-native-tool-guard.ts +1 -1
- package/src/server/messages-native.ts +12 -7
- package/src/server/messages-shared.ts +6 -2
- package/src/server/native-final-log.ts +93 -0
- package/src/server/ollama-inbound.ts +349 -0
- package/src/server/ollama-native-paths.ts +21 -0
- package/src/server/ollama-route-policy.ts +73 -0
- package/src/server/relay.ts +22 -3
- package/src/server/request-decompress.ts +4 -2
- package/src/server/request-log-failure-attribution.ts +22 -0
- package/src/server/request-log-failure-detail.ts +80 -0
- package/src/server/request-log-terminal-status.ts +4 -2
- package/src/server/request-log.ts +79 -42
- package/src/server/response-log-body.ts +6 -1
- package/src/server/responses/account-change-state.ts +17 -6
- package/src/server/responses/adapter-continuation.ts +9 -4
- package/src/server/responses/adapter-delivery.ts +5 -4
- package/src/server/responses/adapter-dispatch.ts +36 -30
- package/src/server/responses/anthropic-thinking-recovery.ts +3 -0
- package/src/server/responses/codex-auth-error.ts +22 -17
- package/src/server/responses/codex-ws-exchange.ts +11 -8
- package/src/server/responses/codex-ws-metadata.ts +94 -21
- package/src/server/responses/codex-ws-request.ts +3 -1
- package/src/server/responses/combo-required-tool-contract.ts +24 -12
- package/src/server/responses/compact.ts +89 -41
- package/src/server/responses/compaction-recovery.ts +4 -2
- package/src/server/responses/compaction-routing.ts +9 -4
- package/src/server/responses/core-auth.ts +32 -14
- package/src/server/responses/core-codex-account.ts +34 -12
- package/src/server/responses/core-combo-failure.ts +5 -1
- package/src/server/responses/core-combo-native.ts +2 -1
- package/src/server/responses/core-combo.ts +51 -33
- package/src/server/responses/core-errors.ts +22 -20
- package/src/server/responses/core-lifetime.ts +6 -3
- package/src/server/responses/core-normalize.ts +8 -1
- package/src/server/responses/core-options.ts +7 -0
- package/src/server/responses/core-pipeline.ts +4 -3
- package/src/server/responses/fetch-helpers.ts +7 -1
- package/src/server/responses/input-admission.ts +28 -2
- package/src/server/responses/native-injection.ts +1 -1
- package/src/server/responses/native-response-json.ts +13 -4
- package/src/server/responses/native-response-output.ts +1 -2
- package/src/server/responses/non-replayable-error.ts +2 -1
- package/src/server/responses/pacing-overload.ts +3 -2
- package/src/server/responses/passthrough-build-error.ts +36 -0
- package/src/server/responses/passthrough-delivery.ts +55 -14
- package/src/server/responses/passthrough-dispatch.ts +26 -39
- package/src/server/responses/passthrough-error.ts +9 -9
- package/src/server/responses/policy-fallback.ts +2 -1
- package/src/server/responses/request-prepare.ts +67 -48
- package/src/server/responses/request-sidecar-auth.ts +3 -1
- package/src/server/responses/request-transport.ts +35 -31
- package/src/server/responses/run-turn-execution.ts +6 -6
- package/src/server/responses/sidecar-execution.ts +16 -12
- package/src/server/responses/spend-storage-error.ts +2 -1
- package/src/server/responses/ws-upstream.ts +9 -0
- package/src/server/responses-custom-tool-repair.ts +14 -3
- package/src/server/responses-terminal-repair.ts +5 -2
- package/src/server/responses-undeclared-tool-guard.ts +39 -16
- package/src/server/safe-response-headers.ts +162 -13
- package/src/server/search.ts +13 -4
- package/src/server/turn-state-handles.ts +164 -0
- package/src/server/workflow-refusal.ts +2 -1
- package/src/server/ws-bridge.ts +5 -2
- package/src/service/ownership-mutation-lease.d.mts +11 -2
- package/src/service/ownership-mutation-lease.mjs +53 -11
- package/src/types/config.ts +2 -0
- package/src/update/index.ts +133 -25
- package/src/update/npm-install.d.mts +8 -0
- package/src/update/npm-install.mjs +20 -1
- package/src/update/orchestrated-admission.d.mts +4 -0
- package/src/update/orchestrated-admission.mjs +12 -0
- package/src/update/orchestrated-arguments.d.mts +12 -0
- package/src/update/orchestrated-arguments.mjs +64 -0
- package/src/update/orchestrated-child.d.mts +12 -0
- package/src/update/orchestrated-child.mjs +70 -0
- package/src/update/orchestrated-entry.ts +234 -0
- package/src/update/orchestrated-identity-probe-entry.ts +20 -0
- package/src/update/orchestrated-identity.ts +53 -0
- package/src/update/orchestrated-live-identity.ts +48 -0
- package/src/update/orchestrated-node-driver.mjs +99 -0
- package/src/update/orchestrated-outcome.d.mts +21 -0
- package/src/update/orchestrated-outcome.mjs +40 -0
- package/src/update/orchestrated-plan.ts +49 -0
- package/src/update/orchestrated-pnpm.d.mts +8 -0
- package/src/update/orchestrated-pnpm.mjs +33 -0
- package/src/update/orchestrated-session.d.mts +37 -0
- package/src/update/orchestrated-session.mjs +264 -0
- package/src/update/orchestrated-token.d.mts +10 -0
- package/src/update/orchestrated-token.mjs +21 -0
- package/src/update/orchestrated-token.ts +3 -0
- package/src/update/orchestrated-tree.d.mts +1 -0
- package/src/update/orchestrated-tree.mjs +14 -0
- package/src/update/pnpm-global-install.d.mts +6 -0
- package/src/update/pnpm-global-install.mjs +9 -2
- package/src/update/posix-install-supervisor-entry.mjs +6 -0
- package/src/update/posix-install-supervisor.d.mts +10 -0
- package/src/update/posix-install-supervisor.mjs +88 -0
- package/src/update/sync-update-mutex.d.mts +10 -0
- package/src/update/sync-update-mutex.mjs +164 -0
- package/src/update/sync-update-mutex.ts +1 -0
- package/src/update/test-home-guard.mjs +33 -0
- package/src/update/transactional-install.mjs +2 -2
- package/src/usage/error-detail.ts +225 -0
- package/src/usage/log.ts +60 -27
- package/src/vision/describe.ts +4 -7
- package/src/web-search/executor.ts +4 -7
- package/src/web-search/loop.ts +31 -5
- package/gui/dist/assets/index-D7DCm5eQ.js +0 -94
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { isProxyError, hasProxyErrorMessage } from "../../lib/client-error-body";
|
|
2
|
+
import type { FailureVerdictSink } from "../../chat/failure-verdict";
|
|
3
|
+
import { ownedChatErrorResponse } from "../../chat/outbound";
|
|
1
4
|
/**
|
|
2
5
|
* AdapterEvent -> OpenAI Chat Completions, without the internal Responses SSE (PF-09).
|
|
3
6
|
*
|
|
@@ -14,7 +17,6 @@ import type { TranslatorBudget } from "../../lib/translator-budget";
|
|
|
14
17
|
import { responsesUsage } from "../../bridge/internal";
|
|
15
18
|
import {
|
|
16
19
|
appendedUtf8Bytes,
|
|
17
|
-
chatCompletionsErrorResponse,
|
|
18
20
|
chatCompletionsFailedResponse,
|
|
19
21
|
chatCompletionsIncompleteOutcome,
|
|
20
22
|
chatCompletionsStreamErrorPayload,
|
|
@@ -40,9 +42,11 @@ type Rec = Record<string, unknown>;
|
|
|
40
42
|
export interface ChatCompletionEncodeOptions extends AdapterEventEncodeOptions {
|
|
41
43
|
/** The model string the client asked for; every chunk carries it. */
|
|
42
44
|
model: string;
|
|
45
|
+
failureVerdictSink?: FailureVerdictSink;
|
|
46
|
+
proxyErrorContext?: object;
|
|
43
47
|
}
|
|
44
48
|
|
|
45
|
-
function createChatCompletionWriter(sink: ClientFrameSink, model: string): ClientWireWriter {
|
|
49
|
+
function createChatCompletionWriter(sink: ClientFrameSink, model: string, failureVerdictSink?: FailureVerdictSink, proxyErrorContext?: object): ClientWireWriter {
|
|
46
50
|
const id = completionId();
|
|
47
51
|
const created = Math.floor(Date.now() / 1000);
|
|
48
52
|
let started = false;
|
|
@@ -149,10 +153,11 @@ function createChatCompletionWriter(sink: ClientFrameSink, model: string): Clien
|
|
|
149
153
|
terminated = true;
|
|
150
154
|
failed = true;
|
|
151
155
|
// A real error event, then an abnormal close without [DONE].
|
|
152
|
-
const { payload, bounded } = chatCompletionsStreamErrorPayload(message, details);
|
|
156
|
+
const { payload, bounded, verdict } = chatCompletionsStreamErrorPayload(message, details);
|
|
153
157
|
try {
|
|
154
158
|
if (bounded) sink.emitBounded(dataFrame(payload));
|
|
155
159
|
else sink.emit(dataFrame(payload), { terminal: true });
|
|
160
|
+
if (failureVerdictSink) { failureVerdictSink.verdict = verdict; failureVerdictSink.contract = details?.contract; failureVerdictSink.proxyAuthored = details?.proxyAuthored; }
|
|
156
161
|
} catch {
|
|
157
162
|
/* the converter drops an unadmittable error frame the same way */
|
|
158
163
|
}
|
|
@@ -236,12 +241,13 @@ function createChatCompletionWriter(sink: ClientFrameSink, model: string): Clien
|
|
|
236
241
|
if ("finishReason" in outcome) {
|
|
237
242
|
finish(outcome.finishReason, terminal.usageOnWire ? responsesUsage(terminal.usage) : null);
|
|
238
243
|
} else {
|
|
239
|
-
fail(outcome.
|
|
244
|
+
fail(outcome.originalFailMessage, { contract: "stream_incomplete",
|
|
245
|
+
proxyAuthored: isProxyError(terminal) });
|
|
240
246
|
}
|
|
241
247
|
return;
|
|
242
248
|
}
|
|
243
249
|
const failure = chatCompletionsFailedResponse((terminal.error ?? {}) as unknown as Rec);
|
|
244
|
-
fail(failure.message, failure.details);
|
|
250
|
+
fail(failure.message, { ...failure.details, proxyAuthored: hasProxyErrorMessage(proxyErrorContext, failure.message, failure.details) });
|
|
245
251
|
},
|
|
246
252
|
overflow() {
|
|
247
253
|
fail("upstream translation buffer exceeded the safe limit", {
|
|
@@ -259,7 +265,7 @@ export function encodeChatCompletionSse(
|
|
|
259
265
|
events: AsyncIterable<AdapterEvent>,
|
|
260
266
|
options: ChatCompletionEncodeOptions,
|
|
261
267
|
): ReadableStream<Uint8Array> {
|
|
262
|
-
return encodeAdapterEventStream(events, sink => createChatCompletionWriter(sink, options.model), options);
|
|
268
|
+
return encodeAdapterEventStream(events, sink => createChatCompletionWriter(sink, options.model, options.failureVerdictSink, options.proxyErrorContext), options);
|
|
263
269
|
}
|
|
264
270
|
|
|
265
271
|
/**
|
|
@@ -271,18 +277,19 @@ export async function collectChatCompletionResponse(
|
|
|
271
277
|
stream: ReadableStream<Uint8Array>,
|
|
272
278
|
model: string,
|
|
273
279
|
translatorBudget: TranslatorBudget,
|
|
280
|
+
failureVerdictSink?: FailureVerdictSink,
|
|
274
281
|
): Promise<Response> {
|
|
275
282
|
try {
|
|
276
|
-
const completion = await collectChatCompletion(stream, model, translatorBudget);
|
|
283
|
+
const completion = await collectChatCompletion(stream, model, translatorBudget, failureVerdictSink);
|
|
277
284
|
return new Response(JSON.stringify(completion), {
|
|
278
285
|
status: 200,
|
|
279
286
|
headers: { "Content-Type": "application/json" },
|
|
280
287
|
});
|
|
281
288
|
} catch (err) {
|
|
282
289
|
if (isChatCompletionsStreamError(err)) {
|
|
283
|
-
return
|
|
290
|
+
return ownedChatErrorResponse(err.status, err.message, err.type, err.code, failureVerdictSink?.contract, failureVerdictSink?.proxyAuthored);
|
|
284
291
|
}
|
|
285
|
-
return
|
|
292
|
+
return ownedChatErrorResponse(502, err instanceof Error ? err.message : String(err), "server_error");
|
|
286
293
|
}
|
|
287
294
|
}
|
|
288
295
|
|
|
@@ -291,5 +298,6 @@ export function foldChatCompletion(
|
|
|
291
298
|
events: AsyncIterable<AdapterEvent>,
|
|
292
299
|
options: ChatCompletionEncodeOptions,
|
|
293
300
|
): Promise<Response> {
|
|
294
|
-
|
|
301
|
+
const failureVerdictSink: FailureVerdictSink = {};
|
|
302
|
+
return collectChatCompletionResponse(encodeChatCompletionSse(events, { ...options, failureVerdictSink }), options.model, options.translatorBudget, failureVerdictSink);
|
|
295
303
|
}
|
package/src/protocols/trace.ts
CHANGED
|
@@ -65,6 +65,18 @@ export interface ProtocolTraceAttempt {
|
|
|
65
65
|
const requestMarks = new WeakMap<object, EntryMark | BlockedMark>();
|
|
66
66
|
const attemptMarks = new WeakMap<object, AttemptMark>();
|
|
67
67
|
const shadowInputs = new WeakMap<object, ProtocolPlanInput>();
|
|
68
|
+
const suppressedContexts = new WeakSet<object>();
|
|
69
|
+
|
|
70
|
+
/** Suppress diagnostics for an internal protocol bridge while preserving request behavior. */
|
|
71
|
+
export function suppressProtocolTrace(logCtx: object): void {
|
|
72
|
+
suppressedContexts.add(logCtx);
|
|
73
|
+
requestMarks.delete(logCtx);
|
|
74
|
+
shadowInputs.delete(logCtx);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function protocolTraceSuppressed(logCtx: object): boolean {
|
|
78
|
+
return suppressedContexts.has(logCtx);
|
|
79
|
+
}
|
|
68
80
|
|
|
69
81
|
function boundedReasons(codes: Iterable<ProtocolReasonCode>): ProtocolReasonCode[] {
|
|
70
82
|
const out: ProtocolReasonCode[] = [];
|
|
@@ -95,6 +107,7 @@ export function markProtocolEntry(
|
|
|
95
107
|
logCtx: object,
|
|
96
108
|
mark: { inbound: Protocol; lane: ProtocolLane; reasonCodes?: Iterable<ProtocolReasonCode>; features?: ProtocolFeatureSource },
|
|
97
109
|
): void {
|
|
110
|
+
if (protocolTraceSuppressed(logCtx)) return;
|
|
98
111
|
try {
|
|
99
112
|
requestMarks.set(logCtx, {
|
|
100
113
|
kind: "entry",
|
|
@@ -110,6 +123,7 @@ export function markProtocolEntry(
|
|
|
110
123
|
|
|
111
124
|
/** Features the request's entry or blocked mark recorded, for the shadow plan input. */
|
|
112
125
|
export function protocolMarkFeatures(logCtx: object): readonly ProtocolFeature[] | undefined {
|
|
126
|
+
if (protocolTraceSuppressed(logCtx)) return undefined;
|
|
113
127
|
try {
|
|
114
128
|
const mark = requestMarks.get(logCtx);
|
|
115
129
|
return mark ? [...mark.features] : undefined;
|
|
@@ -123,6 +137,7 @@ export function protocolMarkFeatures(logCtx: object): readonly ProtocolFeature[]
|
|
|
123
137
|
* fixed vocabulary and the provider and model names the server already exposes, nothing else.
|
|
124
138
|
*/
|
|
125
139
|
export function markProtocolShadowPlanInput(logCtx: object, input: ProtocolPlanInput): void {
|
|
140
|
+
if (protocolTraceSuppressed(logCtx)) return;
|
|
126
141
|
try {
|
|
127
142
|
shadowInputs.set(logCtx, input);
|
|
128
143
|
} catch {
|
|
@@ -135,6 +150,7 @@ export function markProtocolBlocked(
|
|
|
135
150
|
logCtx: object,
|
|
136
151
|
mark: { inbound: Protocol; reasonCodes: Iterable<ProtocolReasonCode>; features?: ProtocolFeatureSource },
|
|
137
152
|
): void {
|
|
153
|
+
if (protocolTraceSuppressed(logCtx)) return;
|
|
138
154
|
try {
|
|
139
155
|
requestMarks.set(logCtx, {
|
|
140
156
|
kind: "blocked",
|
|
@@ -152,6 +168,7 @@ export function markProtocolBlocked(
|
|
|
152
168
|
* No-op without an entry mark, and a blocked mark is never reopened.
|
|
153
169
|
*/
|
|
154
170
|
export function addProtocolEntryReason(logCtx: object, code: ProtocolReasonCode): void {
|
|
171
|
+
if (protocolTraceSuppressed(logCtx)) return;
|
|
155
172
|
try {
|
|
156
173
|
const mark = requestMarks.get(logCtx);
|
|
157
174
|
if (mark?.kind !== "entry") return;
|
|
@@ -246,6 +263,7 @@ export function protocolTraceForRequest(
|
|
|
246
263
|
logCtx: ProtocolTraceContext,
|
|
247
264
|
attempts: readonly ProtocolTraceAttempt[] | undefined,
|
|
248
265
|
): ProtocolTraceV1 | undefined {
|
|
266
|
+
if (protocolTraceSuppressed(logCtx)) return undefined;
|
|
249
267
|
const trace = observedTrace(logCtx, attempts);
|
|
250
268
|
return trace ? withShadowPlan(logCtx, trace) : undefined;
|
|
251
269
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Display metadata only: subscription names never prove identity, entitlement or capacity. */
|
|
2
|
+
import { normalizeOAuthPlan } from "../oauth/plan";
|
|
3
|
+
|
|
4
|
+
const knownPlans = ["pro", "max_5x", "max_20x", "team", "enterprise", "ultra"] as const;
|
|
5
|
+
|
|
6
|
+
/** Canonicalize known aliases only; retain an unfamiliar validated tier verbatim. */
|
|
7
|
+
export function normalizeAnthropicPlan(value: unknown): string | undefined {
|
|
8
|
+
const raw = normalizeOAuthPlan(value === "Ultra" ? "ultra" : value);
|
|
9
|
+
if (!raw) return undefined;
|
|
10
|
+
const short = raw.replace(/^(?:default_)?claude_/, "");
|
|
11
|
+
return knownPlans.some(plan => plan === short) ? short : raw;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function anthropicSubscriptionLabel(value: unknown): string | undefined {
|
|
15
|
+
const plan = normalizeAnthropicPlan(value);
|
|
16
|
+
switch (plan) {
|
|
17
|
+
case "pro": return "Pro";
|
|
18
|
+
case "max_5x": return "Max 5x";
|
|
19
|
+
case "ultra":
|
|
20
|
+
case "max_20x": return "Ultra";
|
|
21
|
+
case "team": return "Team";
|
|
22
|
+
case "enterprise": return "Enterprise";
|
|
23
|
+
default: return plan;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { markAnthropicFamilyEnumeration } from "./quota/anthropic-family-headers";
|
|
2
2
|
import { rawAnthropicUsageWindows } from "./quota/anthropic-decision-windows";
|
|
3
|
+
import { anthropicUsageBuckets } from "./quota/anthropic-usage-windows";
|
|
3
4
|
import { bindRawDecisionWindows } from "./quota-decision-publication";
|
|
4
5
|
import { decisionQuotaSinkActive } from "../lib/decision-quota-sink";
|
|
5
6
|
import { CLAUDE_CLI_USER_AGENT } from "./claude-cli-identity";
|
|
@@ -8,43 +9,6 @@ import type { ProviderQuota, ProviderQuotaWindow, QuotaFailureCode } from "./quo
|
|
|
8
9
|
import { httpQuotaFailure } from "./quota/failure-codes";
|
|
9
10
|
const hasQuotaRows = (q: ProviderQuota) => q.fiveHourPercent !== undefined || q.weeklyPercent !== undefined || Boolean(q.customWindows?.length);
|
|
10
11
|
|
|
11
|
-
function parseClaudeBucket(value: unknown): { percent?: number; resetAt?: number } | null {
|
|
12
|
-
const rec = asRecord(value);
|
|
13
|
-
if (!rec) return null;
|
|
14
|
-
const percent = normalizePercent(rec.utilization);
|
|
15
|
-
const resetAt = normalizeResetAt(rec.resets_at);
|
|
16
|
-
if (percent === undefined && resetAt === undefined) return null;
|
|
17
|
-
return { percent, resetAt };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const TERMINAL_CONTROL_CHARACTERS = /[\u0000-\u001f\u007f-\u009f]/gu;
|
|
21
|
-
|
|
22
|
-
function parseClaudeLimit(value: unknown): ProviderQuotaWindow | null {
|
|
23
|
-
const rec = asRecord(value);
|
|
24
|
-
if (!rec) return null;
|
|
25
|
-
const percent = normalizePercent(rec.percent);
|
|
26
|
-
if (percent === undefined) return null;
|
|
27
|
-
const scope = asRecord(rec.scope);
|
|
28
|
-
const model = asRecord(scope?.model);
|
|
29
|
-
const rawLabel = String(model?.display_name ?? "")
|
|
30
|
-
.replace(TERMINAL_CONTROL_CHARACTERS, "")
|
|
31
|
-
.trim();
|
|
32
|
-
if (!rawLabel) return null;
|
|
33
|
-
const lowerLabel = rawLabel.toLowerCase();
|
|
34
|
-
const label = lowerLabel.includes("fable") ? "Fable"
|
|
35
|
-
: lowerLabel.includes("opus") ? "Opus"
|
|
36
|
-
: lowerLabel.includes("sonnet") ? "Sonnet"
|
|
37
|
-
: null;
|
|
38
|
-
// An unrecognized display_name is never published as a quota label: stripping
|
|
39
|
-
// control characters still leaves attacker-chosen residue on the quota line.
|
|
40
|
-
if (label === null) return null;
|
|
41
|
-
const resetAt = normalizeResetAt(rec.resets_at);
|
|
42
|
-
// Model scope is proven structurally here, not guessed from text: the caller admits only
|
|
43
|
-
// `kind: "weekly_scoped"`, and a limit without a recognized `scope.model.display_name` has
|
|
44
|
-
// already returned null above. Routing keys on `scope`, never on `label`.
|
|
45
|
-
return { label, scope: "model", percent, ...(resetAt !== undefined ? { resetAt } : {}) };
|
|
46
|
-
}
|
|
47
|
-
|
|
48
12
|
/** Read the Anthropic OAuth usage endpoint for an access token and return the parsed quota, or null on a non-OK or malformed response. */
|
|
49
13
|
export async function readAnthropicUsageQuota(accessToken: string): Promise<ProviderQuota | null> {
|
|
50
14
|
return (await probeAnthropicUsageQuota(accessToken)).quota;
|
|
@@ -73,44 +37,28 @@ export async function probeAnthropicUsageQuota(accessToken: string): Promise<Ant
|
|
|
73
37
|
}
|
|
74
38
|
const body = asRecord(await readQuotaJson(response));
|
|
75
39
|
if (!body) return { quota: null, failure: "response_unusable" };
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
const fable = parseClaudeBucket(body.seven_day_fable);
|
|
79
|
-
const opus = parseClaudeBucket(body.seven_day_opus);
|
|
80
|
-
const sonnet = parseClaudeBucket(body.seven_day_sonnet);
|
|
40
|
+
const { buckets, familyEnumeration } = anthropicUsageBuckets(body);
|
|
41
|
+
const shared: Partial<ProviderQuota> = {};
|
|
81
42
|
const customWindows: ProviderQuotaWindow[] = [];
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const limit = parseClaudeLimit(rawLimit);
|
|
93
|
-
if (!limit || knownLabels.has(limit.label.toLowerCase())) continue;
|
|
94
|
-
knownLabels.add(limit.label.toLowerCase());
|
|
95
|
-
customWindows.push(limit);
|
|
43
|
+
for (const { window, row, percentField } of buckets) {
|
|
44
|
+
const percent = normalizePercent(row[percentField]);
|
|
45
|
+
const resetAt = normalizeResetAt(row.resets_at);
|
|
46
|
+
if (window === "5h" || window === "weekly") {
|
|
47
|
+
if (percent !== undefined) shared[window === "5h" ? "fiveHourPercent" : "weeklyPercent"] = percent;
|
|
48
|
+
if (resetAt !== undefined) shared[window === "5h" ? "fiveHourResetAt" : "weeklyResetAt"] = resetAt;
|
|
49
|
+
} else if (percent !== undefined) {
|
|
50
|
+
const label = { fable: "Fable", opus: "Opus", sonnet: "Sonnet" }[window];
|
|
51
|
+
customWindows.push({ label, scope: "model", percent, ...(resetAt !== undefined ? { resetAt } : {}) });
|
|
52
|
+
}
|
|
96
53
|
}
|
|
97
54
|
const quota: ProviderQuota = {
|
|
98
|
-
|
|
99
|
-
// rows: report it in the canonical fields so the dashboard renders it with the standard
|
|
100
|
-
// "5-hour limit" label and ordering instead of as a generic extra window.
|
|
101
|
-
...(fiveHour?.percent !== undefined ? { fiveHourPercent: fiveHour.percent } : {}),
|
|
102
|
-
...(fiveHour?.resetAt !== undefined ? { fiveHourResetAt: fiveHour.resetAt } : {}),
|
|
103
|
-
...(sevenDay?.percent !== undefined ? { weeklyPercent: sevenDay.percent } : {}),
|
|
104
|
-
...(sevenDay?.resetAt !== undefined ? { weeklyResetAt: sevenDay.resetAt } : {}),
|
|
55
|
+
...shared,
|
|
105
56
|
...(customWindows.length > 0 ? { customWindows } : {}),
|
|
106
57
|
updatedAt: Date.now(),
|
|
107
58
|
};
|
|
108
59
|
if (decisionQuotaSinkActive()) bindRawDecisionWindows(quota, rawAnthropicUsageWindows(body, quota.updatedAt));
|
|
109
60
|
// Empty / schema-changed payloads must not cache as "success with no bars".
|
|
110
61
|
if (!hasQuotaRows(quota)) return { quota: null, failure: "response_unusable" };
|
|
111
|
-
return { quota: markAnthropicFamilyEnumeration(quota,
|
|
112
|
-
const kind = asRecord(raw)?.kind;
|
|
113
|
-
return kind === "session" || kind === "weekly_all" || kind === "weekly_scoped" && parseClaudeLimit(raw) !== null;
|
|
114
|
-
})) };
|
|
62
|
+
return { quota: markAnthropicFamilyEnumeration(quota, familyEnumeration) };
|
|
115
63
|
}
|
|
116
64
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { boundedServiceTier } from "../lib/response-service-tier";
|
|
1
2
|
import type {
|
|
2
3
|
AttemptTierOutcome,
|
|
3
4
|
FastWire,
|
|
@@ -361,7 +362,7 @@ export function createAdapterTierMetadata(
|
|
|
361
362
|
return {
|
|
362
363
|
outcome,
|
|
363
364
|
observeResponseServiceTier(value: unknown) {
|
|
364
|
-
const sanitized =
|
|
365
|
+
const sanitized = boundedServiceTier(value);
|
|
365
366
|
if (sanitized) outcome.responseServiceTier = sanitized;
|
|
366
367
|
},
|
|
367
368
|
markResponseUnparseable() {},
|
|
@@ -413,7 +414,7 @@ export function createAdapterTierMetadata(
|
|
|
413
414
|
}
|
|
414
415
|
return;
|
|
415
416
|
}
|
|
416
|
-
const sanitized =
|
|
417
|
+
const sanitized = boundedServiceTier(value);
|
|
417
418
|
if (sanitized) outcome.responseServiceTier = sanitized;
|
|
418
419
|
if (!responseCanConfirmFast) return;
|
|
419
420
|
if (canonicalFromWire(context.fastWire, value) === "priority") {
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
/** Raw advisory projection; compatibility display clamping/rounding remains unchanged. */
|
|
2
2
|
import type { DecisionQuotaWindow } from "../quota-decision-snapshot";
|
|
3
3
|
import { normalizeResetAt, toFiniteNumber } from "../quota-wire";
|
|
4
|
-
|
|
5
|
-
const record = (value: unknown): Record<string, unknown> | undefined =>
|
|
6
|
-
value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
|
|
4
|
+
import { anthropicUsageBuckets } from "./anthropic-usage-windows";
|
|
7
5
|
/** Normalize a reset value; an explicit invalid reset becomes NaN so it cannot read as an undated valid measurement. */
|
|
8
6
|
function reset(value: unknown): number | undefined {
|
|
9
7
|
// Explicit invalid reset must not become an apparently undated valid measurement.
|
|
@@ -12,23 +10,11 @@ function reset(value: unknown): number | undefined {
|
|
|
12
10
|
/** Extract raw decision windows (5h, weekly and per-model families) from an Anthropic usage body, keeping invalid resets distinct from display parsing. */
|
|
13
11
|
export function rawAnthropicUsageWindows(body: Record<string, unknown>, observedAt: number): DecisionQuotaWindow[] {
|
|
14
12
|
const rows: DecisionQuotaWindow[] = [];
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
if (percent === undefined) return;
|
|
20
|
-
const resetAt = reset(row?.resets_at);
|
|
13
|
+
for (const { window, row, percentField } of anthropicUsageBuckets(body).buckets) {
|
|
14
|
+
const percent = toFiniteNumber(row[percentField]);
|
|
15
|
+
if (percent === undefined) continue;
|
|
16
|
+
const resetAt = reset(row.resets_at);
|
|
21
17
|
rows.push({ window, percent, observedAt, ...(resetAt !== undefined ? { resetAt } : {}) });
|
|
22
|
-
};
|
|
23
|
-
for (const [window, field] of [["5h", "five_hour"], ["weekly", "seven_day"], ["fable", "seven_day_fable"], ["opus", "seven_day_opus"], ["sonnet", "seven_day_sonnet"]] as const) add(window, body[field], "utilization");
|
|
24
|
-
for (const limit of Array.isArray(body.limits) ? body.limits : []) {
|
|
25
|
-
const row = record(limit);
|
|
26
|
-
if (row?.kind !== "weekly_scoped") continue;
|
|
27
|
-
const display = record(record(row.scope)?.model)?.display_name;
|
|
28
|
-
if (typeof display !== "string") continue;
|
|
29
|
-
const label = display.replace(/[\u0000-\u001f\u007f-\u009f]/gu, "").toLowerCase();
|
|
30
|
-
const family = (["fable", "opus", "sonnet"] as const).find(family => label.includes(family));
|
|
31
|
-
if (family) add(family, row, "percent");
|
|
32
18
|
}
|
|
33
19
|
return rows;
|
|
34
20
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** One source selection for display and advisory usage; neither projection invents absent limits. */
|
|
2
|
+
import { asRecord, toFiniteNumber } from "../quota-wire";
|
|
3
|
+
|
|
4
|
+
type UsageWindow = "5h" | "weekly" | "fable" | "opus" | "sonnet";
|
|
5
|
+
export interface AnthropicUsageBucket {
|
|
6
|
+
window: UsageWindow;
|
|
7
|
+
row: Record<string, unknown>;
|
|
8
|
+
percentField: "percent" | "utilization";
|
|
9
|
+
}
|
|
10
|
+
const legacyFields = { "5h": "five_hour", weekly: "seven_day", fable: "seven_day_fable",
|
|
11
|
+
opus: "seven_day_opus", sonnet: "seven_day_sonnet" } as const;
|
|
12
|
+
|
|
13
|
+
/** Only these producer-proven model families may constrain routing. Unknown labels stay private. */
|
|
14
|
+
function scopedFamily(row: Record<string, unknown>): "fable" | "opus" | "sonnet" | undefined {
|
|
15
|
+
const display = asRecord(asRecord(row.scope)?.model)?.display_name;
|
|
16
|
+
if (typeof display !== "string") return undefined;
|
|
17
|
+
const label = display.replace(/[\u0000-\u001f\u007f-\u009f]/gu, "").toLowerCase();
|
|
18
|
+
return (["fable", "opus", "sonnet"] as const).find(family => label.includes(family));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Recognized rows supersede legacy duplicates even when skipped or malformed; duplicates
|
|
23
|
+
* and malformed shared measurements make family enumeration incomplete. Shared finite
|
|
24
|
+
* measurements apply regardless of the binding flag, though an unknown flag also makes
|
|
25
|
+
* enumeration incomplete. A null legacy five_hour requires an explicitly active session.
|
|
26
|
+
* Scoped rows retain active-only behavior; unknown kinds/families cannot clear refusals.
|
|
27
|
+
*/
|
|
28
|
+
export function anthropicUsageBuckets(body: Record<string, unknown>): {
|
|
29
|
+
buckets: AnthropicUsageBucket[]; familyEnumeration: boolean;
|
|
30
|
+
} {
|
|
31
|
+
const selected = new Map<UsageWindow, AnthropicUsageBucket>();
|
|
32
|
+
const represented = new Set<UsageWindow>();
|
|
33
|
+
const limits = Array.isArray(body.limits) ? body.limits : [];
|
|
34
|
+
let familyEnumeration = Array.isArray(body.limits);
|
|
35
|
+
for (const raw of limits) {
|
|
36
|
+
const row = asRecord(raw);
|
|
37
|
+
const window = row?.kind === "session" ? "5h" : row?.kind === "weekly_all" ? "weekly"
|
|
38
|
+
: row?.kind === "weekly_scoped" ? scopedFamily(row) : undefined;
|
|
39
|
+
if (!row || !window) { familyEnumeration = false; continue; }
|
|
40
|
+
if (represented.has(window)) { familyEnumeration = false; continue; }
|
|
41
|
+
represented.add(window);
|
|
42
|
+
if (window === "5h" || window === "weekly") {
|
|
43
|
+
const flag = row.is_active;
|
|
44
|
+
if (flag !== undefined && typeof flag !== "boolean") familyEnumeration = false;
|
|
45
|
+
if (toFiniteNumber(row.percent) === undefined) { familyEnumeration = false; continue; }
|
|
46
|
+
if (window === "5h" && body.five_hour === null && flag !== true) continue;
|
|
47
|
+
selected.set(window, { window, row, percentField: "percent" });
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (row.is_active === false) continue;
|
|
51
|
+
if (row.is_active !== undefined && row.is_active !== true) { familyEnumeration = false; continue; }
|
|
52
|
+
if (toFiniteNumber(row.percent) === undefined) familyEnumeration = false;
|
|
53
|
+
selected.set(window, { window, row, percentField: "percent" });
|
|
54
|
+
}
|
|
55
|
+
for (const window of Object.keys(legacyFields) as UsageWindow[]) {
|
|
56
|
+
if (represented.has(window)) continue;
|
|
57
|
+
const row = asRecord(body[legacyFields[window]]);
|
|
58
|
+
if (row) selected.set(window, { window, row, percentField: "utilization" });
|
|
59
|
+
}
|
|
60
|
+
// Stable canonical order, independent of how upstream orders limits.
|
|
61
|
+
return { buckets: (Object.keys(legacyFields) as UsageWindow[]).flatMap(window => {
|
|
62
|
+
const bucket = selected.get(window);
|
|
63
|
+
return bucket ? [bucket] : [];
|
|
64
|
+
}), familyEnumeration };
|
|
65
|
+
}
|
|
@@ -1125,18 +1125,17 @@ export function isCanonicalCommandCodeBaseUrl(baseUrl: string): boolean {
|
|
|
1125
1125
|
return normalized === COMMAND_CODE_BASE_URL || normalized === `${COMMAND_CODE_BASE_URL}/provider/v1`;
|
|
1126
1126
|
}
|
|
1127
1127
|
|
|
1128
|
-
/**
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
// { usage: null, data: { usage: {...} } }
|
|
1128
|
+
/**
|
|
1129
|
+
* Pick the envelope carrying the legacy windows (weekly, 5h, total credits). The monthly
|
|
1130
|
+
* `usages` container is located separately, so its presence never hides a nested window.
|
|
1131
|
+
*/
|
|
1132
|
+
function kimiLegacyWindowBody(root: Record<string, unknown>): Record<string, unknown> {
|
|
1133
|
+
const nested = asRecord(root.data);
|
|
1134
|
+
if (!nested) return root;
|
|
1135
|
+
// A null outer field is a placeholder, not data — { usage: null, data: { usage: {...} } } unwraps.
|
|
1136
1136
|
const usable = (field: unknown): boolean => field !== undefined && field !== null;
|
|
1137
|
-
const
|
|
1138
|
-
|
|
1139
|
-
return !outerHasUsage && nestedHasUsage ? nested : body;
|
|
1137
|
+
const hasLegacy = (body: Record<string, unknown>): boolean => usable(body.usage) || usable(body.limits) || usable(body.totalQuota);
|
|
1138
|
+
return !hasLegacy(root) && hasLegacy(nested) ? nested : root;
|
|
1140
1139
|
}
|
|
1141
1140
|
|
|
1142
1141
|
function kimiLimitLabel(item: Record<string, unknown>, detail: Record<string, unknown>): string {
|
|
@@ -1146,21 +1145,43 @@ function kimiLimitLabel(item: Record<string, unknown>, detail: Record<string, un
|
|
|
1146
1145
|
.toLowerCase();
|
|
1147
1146
|
}
|
|
1148
1147
|
|
|
1148
|
+
/** Highest percentage a window with a positive remainder may report; 100 means exhausted. */
|
|
1149
|
+
const KIMI_BELOW_EXHAUSTED_PERCENT = 99.999;
|
|
1150
|
+
|
|
1151
|
+
/**
|
|
1152
|
+
* Counter-derived percentage, or undefined when the counters cannot be trusted. Any
|
|
1153
|
+
* exhaustion signal (used at or past the limit, or remaining at or below zero) wins first,
|
|
1154
|
+
* even beside an impossible companion counter: dropping it would let an over-quota account
|
|
1155
|
+
* look available. Otherwise a negative used or a remaining above the limit is impossible.
|
|
1156
|
+
* A positive remainder never rounds up to exhaustion.
|
|
1157
|
+
*/
|
|
1158
|
+
function kimiCounterPercent(row: Record<string, unknown>): number | undefined {
|
|
1159
|
+
const limit = toFiniteNumber(row.limit);
|
|
1160
|
+
if (limit === undefined || limit <= 0) return undefined;
|
|
1161
|
+
const used = toFiniteNumber(row.used);
|
|
1162
|
+
const remaining = toFiniteNumber(row.remaining);
|
|
1163
|
+
if ((used !== undefined && used >= limit) || (remaining !== undefined && remaining <= 0)) return 100;
|
|
1164
|
+
if ((used !== undefined && used < 0) || (remaining !== undefined && remaining > limit)) return undefined;
|
|
1165
|
+
if (used !== undefined) return (used / limit) * 100;
|
|
1166
|
+
if (remaining === undefined) return undefined;
|
|
1167
|
+
return Math.min(((limit - remaining) / limit) * 100, KIMI_BELOW_EXHAUSTED_PERCENT);
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
/** Parse consumed usage, preferring valid counters, then a valid [0, 1] ratio, then direct percentages. */
|
|
1149
1171
|
function parseKimiQuotaRow(value: unknown, resetFallback?: Record<string, unknown>): { percent: number; resetAt?: number } | null {
|
|
1150
1172
|
const row = asRecord(value);
|
|
1151
1173
|
if (!row) return null;
|
|
1152
1174
|
const resetAt = quotaResetAt(row) ?? (resetFallback ? quotaResetAt(resetFallback) : undefined);
|
|
1153
|
-
const
|
|
1154
|
-
if (
|
|
1155
|
-
|
|
1156
|
-
if (
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
}
|
|
1175
|
+
const counterPercent = kimiCounterPercent(row);
|
|
1176
|
+
if (counterPercent !== undefined) {
|
|
1177
|
+
const percent = normalizePercent(counterPercent);
|
|
1178
|
+
if (percent !== undefined) return { percent, ...(resetAt !== undefined ? { resetAt } : {}) };
|
|
1179
|
+
}
|
|
1180
|
+
// Newer Kimi responses expose utilization as a fraction in [0, 1].
|
|
1181
|
+
const ratio = toFiniteNumber(row.used_ratio ?? row.usedRatio);
|
|
1182
|
+
if (ratio !== undefined && ratio >= 0 && ratio <= 1) {
|
|
1183
|
+
const percent = normalizePercent(ratio * 100);
|
|
1184
|
+
if (percent !== undefined) return { percent, ...(resetAt !== undefined ? { resetAt } : {}) };
|
|
1164
1185
|
}
|
|
1165
1186
|
// Some payloads expose utilisation directly when limit/used arithmetic is absent.
|
|
1166
1187
|
const direct = normalizePercent(row.utilization ?? row.percent ?? row.usedPercent ?? row.used_percent);
|
|
@@ -1181,11 +1202,22 @@ function isKimiWeeklyLimit(item: Record<string, unknown>, detail: Record<string,
|
|
|
1181
1202
|
return /weekly|7\s*(?:d|day)/.test(kimiLimitLabel(item, detail));
|
|
1182
1203
|
}
|
|
1183
1204
|
|
|
1205
|
+
/** Keep legacy windows separate and use the first parseable monthly row across supported envelopes. */
|
|
1184
1206
|
function parseKimiQuotaPayload(value: unknown): ProviderQuota | null {
|
|
1185
|
-
const
|
|
1186
|
-
if (!
|
|
1207
|
+
const root = asRecord(value);
|
|
1208
|
+
if (!root) return null;
|
|
1209
|
+
const body = kimiLegacyWindowBody(root);
|
|
1187
1210
|
let weekly = parseKimiQuotaRow(body.usage);
|
|
1188
1211
|
const total = parseKimiQuotaRow(body.totalQuota);
|
|
1212
|
+
const nested = asRecord(root.data);
|
|
1213
|
+
let monthly: { percent: number; resetAt?: number } | null = null;
|
|
1214
|
+
for (const container of [root, nested, asRecord(root.quota), asRecord(nested?.quota)]) {
|
|
1215
|
+
const usages = asRecord(container?.usages);
|
|
1216
|
+
if (!usages) continue;
|
|
1217
|
+
monthly = parseKimiQuotaRow(usages.limit_month_total ?? usages.monthTotal)
|
|
1218
|
+
?? parseKimiQuotaRow(usages.limit_month_code ?? usages.monthCode);
|
|
1219
|
+
if (monthly) break;
|
|
1220
|
+
}
|
|
1189
1221
|
let fiveHour: { percent: number; resetAt?: number } | null = null;
|
|
1190
1222
|
if (Array.isArray(body.limits)) {
|
|
1191
1223
|
for (const rawItem of body.limits) {
|
|
@@ -1211,6 +1243,10 @@ function parseKimiQuotaPayload(value: unknown): ProviderQuota | null {
|
|
|
1211
1243
|
weeklyPercent: weekly.percent,
|
|
1212
1244
|
...(weekly.resetAt !== undefined ? { weeklyResetAt: weekly.resetAt } : {}),
|
|
1213
1245
|
} : {}),
|
|
1246
|
+
...(monthly ? {
|
|
1247
|
+
monthlyPercent: monthly.percent,
|
|
1248
|
+
...(monthly.resetAt !== undefined ? { monthlyResetAt: monthly.resetAt } : {}),
|
|
1249
|
+
} : {}),
|
|
1214
1250
|
...(total ? { customWindows: [{ label: "Total subscription credits", percent: total.percent, ...(total.resetAt !== undefined ? { resetAt: total.resetAt } : {}) }] } : {}),
|
|
1215
1251
|
updatedAt: Date.now(),
|
|
1216
1252
|
};
|
|
@@ -3,6 +3,7 @@ import { createHash } from "node:crypto";
|
|
|
3
3
|
import type { GenerationContext } from "../lib/state-store-sweeper";
|
|
4
4
|
import { carryReplayRefusal, isNonReplayableResponse, markResponseNonReplayable } from "../lib/upstream-retry";
|
|
5
5
|
import { redactSecretString } from "../lib/redact";
|
|
6
|
+
import { errorDestinationText, providerErrorDestinationLabel } from "../lib/error-destination";
|
|
6
7
|
|
|
7
8
|
export const REQUEST_PACING_MAX_QUEUE_DEPTH = 256;
|
|
8
9
|
export const REQUEST_PACING_MAX_QUEUE_AGE_MS = 60_000;
|
|
@@ -49,8 +50,8 @@ export class RequestPacingQueueOverloadError extends Error {
|
|
|
49
50
|
public readonly retryAfterSeconds: number,
|
|
50
51
|
) {
|
|
51
52
|
super(reason === "keys_exhausted" ? "No eligible API key remains; wait for the provider cooldown before retrying" : reason === "queue_full"
|
|
52
|
-
? `request pacing queue for provider '${providerName}' is full`
|
|
53
|
-
: `request pacing queue for provider '${providerName}' exceeded the maximum queued age`);
|
|
53
|
+
? `request pacing queue for provider '${errorDestinationText(providerErrorDestinationLabel(providerName))}' is full`
|
|
54
|
+
: `request pacing queue for provider '${errorDestinationText(providerErrorDestinationLabel(providerName))}' exceeded the maximum queued age`);
|
|
54
55
|
this.name = "RequestPacingQueueOverloadError";
|
|
55
56
|
}
|
|
56
57
|
}
|