@askalf/dario 6.18.0 → 6.18.1
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/dist/codex-backend.d.ts +32 -1
- package/dist/codex-backend.js +47 -5
- package/dist/proxy.js +88 -50
- package/docs/two-plans.md +1 -1
- package/package.json +1 -1
package/dist/codex-backend.d.ts
CHANGED
|
@@ -282,7 +282,38 @@ export declare function createResponsesTranslator(model: string): {
|
|
|
282
282
|
export declare const CODEX_SUPPORTED_FIELDS: readonly string[];
|
|
283
283
|
/** Drop every field this backend does not accept. Pure; exported for tests. */
|
|
284
284
|
export declare function toCodexSupportedBody(body: Record<string, unknown>): Record<string, unknown>;
|
|
285
|
-
|
|
285
|
+
/** What a request adds to the seat's headers: its conversation and its model. */
|
|
286
|
+
export interface CodexRequestHeaderInput {
|
|
287
|
+
/** The `prompt_cache_key` the body goes out with; empty when none. */
|
|
288
|
+
cacheKey?: string;
|
|
289
|
+
/** The model the body names; empty when none. */
|
|
290
|
+
model?: string;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* A header value for a key the request supplies. Printable ASCII without
|
|
294
|
+
* spaces goes as it is (the Codex CLI's session ids are UUIDs); anything
|
|
295
|
+
* else is sent as a stable digest, so a key with a space, a quote or a
|
|
296
|
+
* non-ASCII character cannot break the request or smuggle a header.
|
|
297
|
+
*/
|
|
298
|
+
export declare function sessionHeaderValue(key: string): string;
|
|
299
|
+
/**
|
|
300
|
+
* The headers of one request to the ChatGPT backend: the seat's identity and,
|
|
301
|
+
* when the request is known, its conversation and model.
|
|
302
|
+
*
|
|
303
|
+
* The backend's prompt cache lives on individual machines and a request is
|
|
304
|
+
* routed to one by its `session-id` header: the Codex CLI sends its session
|
|
305
|
+
* id there on every turn (codex-rs: "ChatGPT derives cache affinity from the
|
|
306
|
+
* Responses session-id header") with the same value as its `prompt_cache_key`
|
|
307
|
+
* and a `thread-id`, and `x-codex-routing-hint: model=<model>`. Without the
|
|
308
|
+
* header a conversation's turns scatter across machines and read a cached
|
|
309
|
+
* prefix only when one happens to land where an earlier turn wrote it: with
|
|
310
|
+
* turns a few seconds apart and every input a strict extension of the last,
|
|
311
|
+
* a review loop through dario read its prefix on 8 of 23 turns, and what it
|
|
312
|
+
* read was a snapshot several turns old. So the key goes out in the header
|
|
313
|
+
* too, and the raw `/v1/responses` path sends it for the Codex CLI, whose
|
|
314
|
+
* own headers dario does not forward.
|
|
315
|
+
*/
|
|
316
|
+
export declare function buildCodexHeaders(creds: CodexAccountCredentials, request?: CodexRequestHeaderInput): Record<string, string>;
|
|
286
317
|
/**
|
|
287
318
|
* Serve a Responses-shape request (`POST /v1/responses`) from a stored Codex
|
|
288
319
|
* account with NO translation: the ChatGPT backend speaks this shape natively,
|
package/dist/codex-backend.js
CHANGED
|
@@ -796,7 +796,35 @@ export function toCodexSupportedBody(body) {
|
|
|
796
796
|
}
|
|
797
797
|
return out;
|
|
798
798
|
}
|
|
799
|
-
|
|
799
|
+
/**
|
|
800
|
+
* A header value for a key the request supplies. Printable ASCII without
|
|
801
|
+
* spaces goes as it is (the Codex CLI's session ids are UUIDs); anything
|
|
802
|
+
* else is sent as a stable digest, so a key with a space, a quote or a
|
|
803
|
+
* non-ASCII character cannot break the request or smuggle a header.
|
|
804
|
+
*/
|
|
805
|
+
export function sessionHeaderValue(key) {
|
|
806
|
+
if (/^[\x21-\x7e]{1,256}$/.test(key))
|
|
807
|
+
return key;
|
|
808
|
+
return createHash('sha256').update(key).digest('hex').slice(0, 32);
|
|
809
|
+
}
|
|
810
|
+
/**
|
|
811
|
+
* The headers of one request to the ChatGPT backend: the seat's identity and,
|
|
812
|
+
* when the request is known, its conversation and model.
|
|
813
|
+
*
|
|
814
|
+
* The backend's prompt cache lives on individual machines and a request is
|
|
815
|
+
* routed to one by its `session-id` header: the Codex CLI sends its session
|
|
816
|
+
* id there on every turn (codex-rs: "ChatGPT derives cache affinity from the
|
|
817
|
+
* Responses session-id header") with the same value as its `prompt_cache_key`
|
|
818
|
+
* and a `thread-id`, and `x-codex-routing-hint: model=<model>`. Without the
|
|
819
|
+
* header a conversation's turns scatter across machines and read a cached
|
|
820
|
+
* prefix only when one happens to land where an earlier turn wrote it: with
|
|
821
|
+
* turns a few seconds apart and every input a strict extension of the last,
|
|
822
|
+
* a review loop through dario read its prefix on 8 of 23 turns, and what it
|
|
823
|
+
* read was a snapshot several turns old. So the key goes out in the header
|
|
824
|
+
* too, and the raw `/v1/responses` path sends it for the Codex CLI, whose
|
|
825
|
+
* own headers dario does not forward.
|
|
826
|
+
*/
|
|
827
|
+
export function buildCodexHeaders(creds, request = {}) {
|
|
800
828
|
const headers = {
|
|
801
829
|
'Content-Type': 'application/json',
|
|
802
830
|
'Accept': 'text/event-stream',
|
|
@@ -807,6 +835,13 @@ export function buildCodexHeaders(creds) {
|
|
|
807
835
|
const accountId = extractChatGPTAccountId(creds.idToken);
|
|
808
836
|
if (accountId)
|
|
809
837
|
headers['ChatGPT-Account-ID'] = accountId;
|
|
838
|
+
if (request.cacheKey) {
|
|
839
|
+
const id = sessionHeaderValue(request.cacheKey);
|
|
840
|
+
headers['session-id'] = id;
|
|
841
|
+
headers['thread-id'] = id;
|
|
842
|
+
}
|
|
843
|
+
if (request.model && /^[\x21-\x7e]{1,128}$/.test(request.model))
|
|
844
|
+
headers['x-codex-routing-hint'] = `model=${request.model}`;
|
|
810
845
|
return headers;
|
|
811
846
|
}
|
|
812
847
|
/**
|
|
@@ -958,13 +993,17 @@ timing) {
|
|
|
958
993
|
if (verbose)
|
|
959
994
|
console.log(`[dario] → codex backend (responses passthrough): ${target} (model: ${model})`);
|
|
960
995
|
let activeCreds = creds;
|
|
961
|
-
|
|
996
|
+
// The client's own key as the session: the Codex CLI's headers do not
|
|
997
|
+
// pass through dario, so its cache affinity is rebuilt here from the key
|
|
998
|
+
// it sends in the body.
|
|
999
|
+
const requestHeaders = { cacheKey: typeof body.prompt_cache_key === 'string' ? body.prompt_cache_key : '', model };
|
|
1000
|
+
let upstream = await timedFetch(target, { method: 'POST', headers: buildCodexHeaders(activeCreds, requestHeaders), body: JSON.stringify(upstreamBody), signal: abort.signal });
|
|
962
1001
|
if (isCodexAuthFailure(upstream.status)) {
|
|
963
1002
|
await upstream.text().catch(() => ''); // release the rejected response before retrying
|
|
964
1003
|
const fresh = await refreshAfterCodexAuthFailure(activeCreds, verbose);
|
|
965
1004
|
if (fresh) {
|
|
966
1005
|
activeCreds = fresh;
|
|
967
|
-
upstream = await timedFetch(target, { method: 'POST', headers: buildCodexHeaders(activeCreds), body: JSON.stringify(upstreamBody), signal: abort.signal });
|
|
1006
|
+
upstream = await timedFetch(target, { method: 'POST', headers: buildCodexHeaders(activeCreds, requestHeaders), body: JSON.stringify(upstreamBody), signal: abort.signal });
|
|
968
1007
|
}
|
|
969
1008
|
}
|
|
970
1009
|
if (!upstream.ok || !upstream.body) {
|
|
@@ -1263,9 +1302,12 @@ timing) {
|
|
|
1263
1302
|
if (verbose)
|
|
1264
1303
|
console.log(`[dario] → codex backend: ${target} (model: ${model})`);
|
|
1265
1304
|
let activeCreds = creds;
|
|
1305
|
+
// The key the body goes out with, derived or the client's own, is the
|
|
1306
|
+
// session the backend routes the conversation by.
|
|
1307
|
+
const requestHeaders = { cacheKey: String(scrubbed.prompt_cache_key ?? ''), model: String(scrubbed.model ?? model) };
|
|
1266
1308
|
let upstream = await timedFetch(target, {
|
|
1267
1309
|
method: 'POST',
|
|
1268
|
-
headers: buildCodexHeaders(activeCreds),
|
|
1310
|
+
headers: buildCodexHeaders(activeCreds, requestHeaders),
|
|
1269
1311
|
body: JSON.stringify(scrubbed),
|
|
1270
1312
|
signal: abort.signal,
|
|
1271
1313
|
});
|
|
@@ -1278,7 +1320,7 @@ timing) {
|
|
|
1278
1320
|
activeCreds = fresh;
|
|
1279
1321
|
upstream = await timedFetch(target, {
|
|
1280
1322
|
method: 'POST',
|
|
1281
|
-
headers: buildCodexHeaders(activeCreds),
|
|
1323
|
+
headers: buildCodexHeaders(activeCreds, requestHeaders),
|
|
1282
1324
|
body: JSON.stringify(scrubbed),
|
|
1283
1325
|
signal: abort.signal,
|
|
1284
1326
|
});
|
package/dist/proxy.js
CHANGED
|
@@ -2470,6 +2470,60 @@ export async function startProxy(opts = {}) {
|
|
|
2470
2470
|
providerCooldowns.note('codex', d.retryAfterMs);
|
|
2471
2471
|
}).catch(() => { });
|
|
2472
2472
|
};
|
|
2473
|
+
/**
|
|
2474
|
+
* The /analytics row, request-log line and usage lines of one codex request.
|
|
2475
|
+
*
|
|
2476
|
+
* ONE function for BOTH routes that reach a seat: the codex route (a client
|
|
2477
|
+
* asking for a GPT model) and the pool-exhausted fallback below. The fallback
|
|
2478
|
+
* route passed no reporter until 6.18.1, so a request it served left no row
|
|
2479
|
+
* anywhere: /analytics, the request log and the ledger all showed a drained
|
|
2480
|
+
* Claude pool and nothing of the subscription that answered in its place.
|
|
2481
|
+
* The seat's served count and its decline state move here with the row, so a
|
|
2482
|
+
* route cannot have one without the others.
|
|
2483
|
+
*/
|
|
2484
|
+
const reportCodexOutcome = (o, ctx) => {
|
|
2485
|
+
codexRequestCounts.set(o.alias, (codexRequestCounts.get(o.alias) ?? 0) + 1);
|
|
2486
|
+
// A seat that actually SERVED is not rate-limited. Keyed on a 2xx, never on
|
|
2487
|
+
// forwardToCodex returning true: that means "I wrote a response", which is
|
|
2488
|
+
// equally true when it wrote the upstream 429.
|
|
2489
|
+
if (o.status >= 200 && o.status < 300)
|
|
2490
|
+
clearCodexDecline(o.alias);
|
|
2491
|
+
analytics.record({
|
|
2492
|
+
timestamp: Date.now(),
|
|
2493
|
+
consumer: ctx.consumer,
|
|
2494
|
+
account: o.alias,
|
|
2495
|
+
model: o.model || ctx.model || 'codex',
|
|
2496
|
+
inputTokens: o.inputTokens, outputTokens: o.outputTokens,
|
|
2497
|
+
// Anthropic convention, like every other row: inputTokens is
|
|
2498
|
+
// net of the cached prefix, which sits in cacheReadTokens.
|
|
2499
|
+
cacheReadTokens: o.cacheReadTokens, cacheCreateTokens: o.cacheCreateTokens, thinkingTokens: 0,
|
|
2500
|
+
// No Anthropic rate-limit headers on this path; the claim
|
|
2501
|
+
// names the engine and is subscription billing, so the
|
|
2502
|
+
// overage guard (#288) leaves it alone.
|
|
2503
|
+
claim: CODEX_CLAIM, util5h: 0, util7d: 0, overageUtil: 0,
|
|
2504
|
+
latencyMs: o.latencyMs, status: o.status, isStream: o.stream, isOpenAI: ctx.isOpenAI,
|
|
2505
|
+
continuation: continuationOf(ctx.codexGuard, ctx.requestDepth),
|
|
2506
|
+
timing: { queueMs: ctx.queueMs, pacingMs: 0, upstreamTtfbMs: o.upstreamTtfbMs, upstreamMs: o.upstreamMs, totalMs: Math.max(0, Date.now() - ctx.arrivedAt) },
|
|
2507
|
+
cache: o.cache,
|
|
2508
|
+
});
|
|
2509
|
+
writeLogLine(logFileStream, {
|
|
2510
|
+
ts: new Date().toISOString(), req: ctx.codexReq,
|
|
2511
|
+
method: ctx.method, path: ctx.urlPath, model: o.model || ctx.model || undefined,
|
|
2512
|
+
status: o.status, latency_ms: o.latencyMs, in_tokens: o.inputTokens, out_tokens: o.outputTokens,
|
|
2513
|
+
cache_read: o.cacheReadTokens, cache_create: o.cacheCreateTokens,
|
|
2514
|
+
claim: CODEX_CLAIM, bucket: 'subscription', account: o.alias, consumer: ctx.consumer, stream: o.stream,
|
|
2515
|
+
...(ctx.codexGuard?.outcome ? { continued: ctx.codexGuard.outcome, continued_by: ctx.codexGuard.continuedBy ?? undefined, continued_after: ctx.codexGuard.partialChars } : {}),
|
|
2516
|
+
...ctx.continuationLeg,
|
|
2517
|
+
...cacheLogFields(o.cache),
|
|
2518
|
+
});
|
|
2519
|
+
if (verbose)
|
|
2520
|
+
console.log(formatUsageLogLine(ctx.codexReq, {
|
|
2521
|
+
inputTokens: o.inputTokens, outputTokens: o.outputTokens,
|
|
2522
|
+
cacheReadTokens: o.cacheReadTokens, cacheCreateTokens: o.cacheCreateTokens,
|
|
2523
|
+
}, ctx.consumer));
|
|
2524
|
+
if (cacheTraceLines && o.cache)
|
|
2525
|
+
console.log(formatCacheTraceLine(ctx.codexReq, o.cache.key, o.cache, o, o.alias));
|
|
2526
|
+
};
|
|
2473
2527
|
/**
|
|
2474
2528
|
* Serve a pool-exhausted request from the ChatGPT subscription (v6.0.0).
|
|
2475
2529
|
*
|
|
@@ -2488,7 +2542,13 @@ export async function startProxy(opts = {}) {
|
|
|
2488
2542
|
*/
|
|
2489
2543
|
const tryCodexPoolFallback = async (req, res, body, fallbackModels, shape, why, attempted,
|
|
2490
2544
|
/** The request's queue wait and arrival, so the codex response carries the timing headers too. */
|
|
2491
|
-
timing
|
|
2545
|
+
timing,
|
|
2546
|
+
/**
|
|
2547
|
+
* Files the request the seat answered (reportCodexOutcome), under the
|
|
2548
|
+
* request number its console line carried. Required: a route that reaches
|
|
2549
|
+
* a seat without it serves GPT that nothing counts.
|
|
2550
|
+
*/
|
|
2551
|
+
onDone) => {
|
|
2492
2552
|
if (fallbackModels.length === 0)
|
|
2493
2553
|
return false;
|
|
2494
2554
|
// Never a second codex attempt in the same request, and never one while it
|
|
@@ -2571,13 +2631,18 @@ export async function startProxy(opts = {}) {
|
|
|
2571
2631
|
const fallbackBody = buildPoolFallbackBody(body, fallbackModel);
|
|
2572
2632
|
if (!fallbackBody)
|
|
2573
2633
|
return false;
|
|
2574
|
-
|
|
2634
|
+
const reqNo = requestCount;
|
|
2635
|
+
console.log(`[dario] #${reqNo} ${why} → codex account ${seat.alias} as ${fallbackModel}`);
|
|
2575
2636
|
requestCount++;
|
|
2576
2637
|
// Marked only once an attempt is actually being made. Marking it before
|
|
2577
2638
|
// the guards above would tell the rest of the request that codex had been
|
|
2578
2639
|
// tried when it had not, suppressing a later legitimate attempt.
|
|
2579
2640
|
attempted.add('codex');
|
|
2580
|
-
served = await forwardToCodex(req, res, fallbackBody, seat, corsOrigin, { ...SECURITY_HEADERS, 'x-dario-pool-fallback': fallbackModel }, upstreamTimeoutMs, verbose, shape, fetch, hasNextOption || peer !== null,
|
|
2641
|
+
served = await forwardToCodex(req, res, fallbackBody, seat, corsOrigin, { ...SECURITY_HEADERS, 'x-dario-pool-fallback': fallbackModel }, upstreamTimeoutMs, verbose, shape, fetch, hasNextOption || peer !== null,
|
|
2642
|
+
// The row files under the model the seat served as; the substituted
|
|
2643
|
+
// one when the seat named none. The client's own model is the Claude
|
|
2644
|
+
// one, which this request never reached.
|
|
2645
|
+
(o) => onDone({ ...o, model: o.model || fallbackModel }, reqNo), codexOnDecline,
|
|
2581
2646
|
// The mirror of the Claude side (dario#1161): an operator who writes
|
|
2582
2647
|
// `--pool-fallback=gpt-5.6-terra:high` is choosing the effort the
|
|
2583
2648
|
// failover runs at, so the entry's own suffix reaches the request rather
|
|
@@ -2619,6 +2684,8 @@ export async function startProxy(opts = {}) {
|
|
|
2619
2684
|
* this by construction, not by remembering to copy six lines correctly.
|
|
2620
2685
|
*/
|
|
2621
2686
|
const attemptPoolFallbackOn429 = async (req, res, body, isOpenAI, attempted, timing,
|
|
2687
|
+
/** Files a request the subscription served; see tryCodexPoolFallback. */
|
|
2688
|
+
onDone,
|
|
2622
2689
|
/**
|
|
2623
2690
|
* Re-arm THIS request on the credits rung (credits-rung.ts): returns true
|
|
2624
2691
|
* once the request is re-pointed at the key, and the caller re-enters its
|
|
@@ -2626,7 +2693,7 @@ export async function startProxy(opts = {}) {
|
|
|
2626
2693
|
*/
|
|
2627
2694
|
armCredits) => {
|
|
2628
2695
|
const chain = selectPoolFallbackForBody(body);
|
|
2629
|
-
if (await tryCodexPoolFallback(req, res, body, chain, isOpenAI ? 'openai' : 'anthropic', 'pool exhausted mid-flight (429, no peer)', attempted, timing)) {
|
|
2696
|
+
if (await tryCodexPoolFallback(req, res, body, chain, isOpenAI ? 'openai' : 'anthropic', 'pool exhausted mid-flight (429, no peer)', attempted, timing, onDone)) {
|
|
2630
2697
|
return 'served';
|
|
2631
2698
|
}
|
|
2632
2699
|
// The metered rungs, in the chain's own order. The credits rung is never
|
|
@@ -3708,6 +3775,14 @@ export async function startProxy(opts = {}) {
|
|
|
3708
3775
|
const continuationLeg = requestDepth > 0
|
|
3709
3776
|
? { continuation_depth: requestDepth, continuation_of: continuationOfRequest(req.headers[CONTINUATION_OF_HEADER]) }
|
|
3710
3777
|
: {};
|
|
3778
|
+
// The row of a request the pool-exhausted fallback handed to the
|
|
3779
|
+
// subscription, under the number the fallback's console line carried.
|
|
3780
|
+
// No model of its own to file under (the client asked for a Claude one)
|
|
3781
|
+
// and no midstream guard: that route does not resume a dead stream.
|
|
3782
|
+
const reportFallbackCodex = (o, reqNo) => reportCodexOutcome(o, {
|
|
3783
|
+
consumer, codexReq: reqNo, method: req.method ?? '', urlPath, model: '', isOpenAI,
|
|
3784
|
+
queueMs, arrivedAt, requestDepth, codexGuard: null, continuationLeg,
|
|
3785
|
+
});
|
|
3711
3786
|
/**
|
|
3712
3787
|
* First hop: the SAME model again, through the front door. The pool
|
|
3713
3788
|
* picks a seat (sticky binding keeps the prompt cache warm), and if the
|
|
@@ -4380,55 +4455,18 @@ export async function startProxy(opts = {}) {
|
|
|
4380
4455
|
// Reporting is one function for BOTH codex shapes below: the Responses
|
|
4381
4456
|
// passthrough and the translated Messages path record the same row, so a
|
|
4382
4457
|
// GPT request looks the same in /analytics whichever shape asked for it.
|
|
4458
|
+
// The row itself is reportCodexOutcome, shared with the pool-exhausted
|
|
4459
|
+
// fallback route.
|
|
4383
4460
|
//
|
|
4384
4461
|
// Before this hook a codex request left no trace: nothing in /analytics,
|
|
4385
4462
|
// nothing in the request log, no per-account count. The dock (and anyone
|
|
4386
4463
|
// reading /analytics) saw a proxy that served GPT all day and reported
|
|
4387
4464
|
// zero of it. A decline (the request handed to the Claude pool) reports
|
|
4388
4465
|
// nothing here; the Claude path records what it then serves.
|
|
4389
|
-
const codexOnDone = (o) => {
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
// response", which is equally true when it wrote the upstream 429.
|
|
4394
|
-
if (o.status >= 200 && o.status < 300)
|
|
4395
|
-
clearCodexDecline(o.alias);
|
|
4396
|
-
analytics.record({
|
|
4397
|
-
timestamp: Date.now(),
|
|
4398
|
-
consumer,
|
|
4399
|
-
account: o.alias,
|
|
4400
|
-
model: o.model || rawModel || 'codex',
|
|
4401
|
-
inputTokens: o.inputTokens, outputTokens: o.outputTokens,
|
|
4402
|
-
// Anthropic convention, like every other row: inputTokens is
|
|
4403
|
-
// net of the cached prefix, which sits in cacheReadTokens.
|
|
4404
|
-
cacheReadTokens: o.cacheReadTokens, cacheCreateTokens: o.cacheCreateTokens, thinkingTokens: 0,
|
|
4405
|
-
// No Anthropic rate-limit headers on this path; the claim
|
|
4406
|
-
// names the engine and is subscription billing, so the
|
|
4407
|
-
// overage guard (#288) leaves it alone.
|
|
4408
|
-
claim: CODEX_CLAIM, util5h: 0, util7d: 0, overageUtil: 0,
|
|
4409
|
-
latencyMs: o.latencyMs, status: o.status, isStream: o.stream, isOpenAI,
|
|
4410
|
-
continuation: continuationOf(codexGuard, requestDepth),
|
|
4411
|
-
timing: { queueMs, pacingMs: 0, upstreamTtfbMs: o.upstreamTtfbMs, upstreamMs: o.upstreamMs, totalMs: Math.max(0, Date.now() - arrivedAt) },
|
|
4412
|
-
cache: o.cache,
|
|
4413
|
-
});
|
|
4414
|
-
writeLogLine(logFileStream, {
|
|
4415
|
-
ts: new Date().toISOString(), req: codexReq,
|
|
4416
|
-
method: req.method ?? '', path: urlPath, model: o.model || rawModel || undefined,
|
|
4417
|
-
status: o.status, latency_ms: o.latencyMs, in_tokens: o.inputTokens, out_tokens: o.outputTokens,
|
|
4418
|
-
cache_read: o.cacheReadTokens, cache_create: o.cacheCreateTokens,
|
|
4419
|
-
claim: CODEX_CLAIM, bucket: 'subscription', account: o.alias, consumer, stream: o.stream,
|
|
4420
|
-
...(codexGuard?.outcome ? { continued: codexGuard.outcome, continued_by: codexGuard.continuedBy ?? undefined, continued_after: codexGuard.partialChars } : {}),
|
|
4421
|
-
...continuationLeg,
|
|
4422
|
-
...cacheLogFields(o.cache),
|
|
4423
|
-
});
|
|
4424
|
-
if (verbose)
|
|
4425
|
-
console.log(formatUsageLogLine(codexReq, {
|
|
4426
|
-
inputTokens: o.inputTokens, outputTokens: o.outputTokens,
|
|
4427
|
-
cacheReadTokens: o.cacheReadTokens, cacheCreateTokens: o.cacheCreateTokens,
|
|
4428
|
-
}, consumer));
|
|
4429
|
-
if (cacheTraceLines && o.cache)
|
|
4430
|
-
console.log(formatCacheTraceLine(codexReq, o.cache.key, o.cache, o, o.alias));
|
|
4431
|
-
};
|
|
4466
|
+
const codexOnDone = (o) => reportCodexOutcome(o, {
|
|
4467
|
+
consumer, codexReq, method: req.method ?? '', urlPath, model: rawModel, isOpenAI,
|
|
4468
|
+
queueMs, arrivedAt, requestDepth, codexGuard, continuationLeg,
|
|
4469
|
+
});
|
|
4432
4470
|
// Mid-flight seat failover. A 429 lands BEFORE any body is written —
|
|
4433
4471
|
// forwardToCodex only returns false on the decline path — so the same
|
|
4434
4472
|
// request can be handed to a healthy peer instead of failing. Without
|
|
@@ -4675,7 +4713,7 @@ export async function startProxy(opts = {}) {
|
|
|
4675
4713
|
if (!upstreamApiKey && !poolAccount) {
|
|
4676
4714
|
attemptedProviders.add('claude');
|
|
4677
4715
|
}
|
|
4678
|
-
if (!upstreamApiKey && !poolAccount && !pinnedAccount && await tryCodexPoolFallback(req, res, body, selectPoolFallbackForBody(body), isOpenAI ? 'openai' : 'anthropic', 'pool exhausted', attemptedProviders, { queueMs, arrivedAt })) {
|
|
4716
|
+
if (!upstreamApiKey && !poolAccount && !pinnedAccount && await tryCodexPoolFallback(req, res, body, selectPoolFallbackForBody(body), isOpenAI ? 'openai' : 'anthropic', 'pool exhausted', attemptedProviders, { queueMs, arrivedAt }, reportFallbackCodex)) {
|
|
4679
4717
|
return;
|
|
4680
4718
|
}
|
|
4681
4719
|
// `isOpenAI` is REQUIRED here and was not, before v6.0.0 — the selector's
|
|
@@ -5754,7 +5792,7 @@ export async function startProxy(opts = {}) {
|
|
|
5754
5792
|
if (!creditsRung)
|
|
5755
5793
|
providerCooldowns.note('claude', parseRetryAfterMs(upstream.headers.get('retry-after')));
|
|
5756
5794
|
if (!pinnedAccount) {
|
|
5757
|
-
const outcome = await attemptPoolFallbackOn429(req, res, body, isOpenAI, attemptedProviders, { queueMs, arrivedAt }, rearmOnCredits);
|
|
5795
|
+
const outcome = await attemptPoolFallbackOn429(req, res, body, isOpenAI, attemptedProviders, { queueMs, arrivedAt }, reportFallbackCodex, rearmOnCredits);
|
|
5758
5796
|
if (outcome === 'served')
|
|
5759
5797
|
return;
|
|
5760
5798
|
if (outcome === 'credits') {
|
|
@@ -5889,7 +5927,7 @@ export async function startProxy(opts = {}) {
|
|
|
5889
5927
|
if (!creditsRung)
|
|
5890
5928
|
providerCooldowns.note('claude', parseRetryAfterMs(upstream.headers.get('retry-after')));
|
|
5891
5929
|
if (!pinnedAccount) {
|
|
5892
|
-
const outcome = await attemptPoolFallbackOn429(req, res, body, isOpenAI, attemptedProviders, { queueMs, arrivedAt }, rearmOnCredits);
|
|
5930
|
+
const outcome = await attemptPoolFallbackOn429(req, res, body, isOpenAI, attemptedProviders, { queueMs, arrivedAt }, reportFallbackCodex, rearmOnCredits);
|
|
5893
5931
|
if (outcome === 'served')
|
|
5894
5932
|
return;
|
|
5895
5933
|
if (outcome === 'credits') {
|
package/docs/two-plans.md
CHANGED
|
@@ -28,7 +28,7 @@ curl localhost:3456/v1/messages -H 'content-type: application/json' \
|
|
|
28
28
|
|
|
29
29
|
Streaming, tool calls and tool-result round trips work on both shapes, and chat-shape `image_url` parts are carried as Responses `input_image` parts with `detail` preserved: dario translates chat/completions **or** Messages into the Responses API the subscription backend speaks, and translates the stream back into `chat.completion.chunk` or Anthropic message events. A `/v1/responses` request needs no translation: the backend speaks that shape, so the body goes through as the client wrote it (model resolved) and the stream comes back untouched, on the same seats and failover as the other shapes (see [Codex CLI](./integrations/codex-cli.md)). The Codex backend does not accept every chat field, so `response_format`, `stop`, `n`, `logprobs`, `stream_options` and the sampling parameters `temperature`, `top_p`, `max_tokens`, `max_completion_tokens` are intentionally lossy; with `--verbose`, dario reports each field that does not reach Codex once per process. Codex accounts live in `~/.dario/codex-accounts/`, separate from the Claude pool.
|
|
30
30
|
|
|
31
|
-
**Prompt caching:** the backend caches prompt prefixes of 1,024 tokens and up on its own; what dario adds is the `prompt_cache_key` that routes same-prefix requests to the cache that holds them, the way the Codex CLI does with its session id. A chat/completions client that sets its own key keeps it; an Anthropic-shape request gets one per Claude Code session (a hash of `metadata.user_id`, never the raw ids); anything else is keyed on its model, instructions and tool names, so repeated system prompts from any caller land together. Cached tokens come back as `prompt_tokens_details.cached_tokens` on chat/completions and as `cache_read_input_tokens` on `/v1/messages`, and show up in `/analytics` and the `-v` usage line like a Claude request's do. When the share is lower than the conversation shape would predict, the [cache trace](./analytics.md#the-cache-trace) shows what may explain it: turns close enough together that the write may not yet have been readable, a rewritten history, a changed system prompt or tool names, or a changed seat.
|
|
31
|
+
**Prompt caching:** the backend caches prompt prefixes of 1,024 tokens and up on its own; what dario adds is the `prompt_cache_key` that routes same-prefix requests to the cache that holds them, the way the Codex CLI does with its session id. A chat/completions client that sets its own key keeps it; an Anthropic-shape request gets one per Claude Code session (a hash of `metadata.user_id`, never the raw ids); anything else is keyed on its model, instructions and tool names, so repeated system prompts from any caller land together. The key also goes out as the `session-id` header (with `thread-id` and `x-codex-routing-hint: model=<model>`, as the Codex CLI sends them): the backend's cache lives on individual machines and that header is what routes a conversation's next turn to the machine holding its prefix; the body key alone does not. Cached tokens come back as `prompt_tokens_details.cached_tokens` on chat/completions and as `cache_read_input_tokens` on `/v1/messages`, and show up in `/analytics` and the `-v` usage line like a Claude request's do. When the share is lower than the conversation shape would predict, the [cache trace](./analytics.md#the-cache-trace) shows what may explain it: turns close enough together that the write may not yet have been readable, a rewritten history, a changed system prompt or tool names, or a changed seat.
|
|
32
32
|
|
|
33
33
|
## Failover between subscriptions
|
|
34
34
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@askalf/dario",
|
|
3
|
-
"version": "6.18.
|
|
3
|
+
"version": "6.18.1",
|
|
4
4
|
"description": "Use your Claude and ChatGPT subscriptions in Cursor, Cline, Aider, Claude Code and the Agent SDK at subscription pricing, not per-token API bills. One local Anthropic + OpenAI-compatible endpoint: either plan answers either wire shape, opt-in failover when one hits its limit, and a ledger of what each plan absorbed.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|