claude-use 14.0.4 → 14.1.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/dist/cli.cjs +447 -114
- package/dist/index.cjs +59 -13
- package/dist/index.mjs +59 -13
- package/dist/types/index.d.ts +400 -383
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -28517,7 +28517,7 @@ function isRateLimitHeader(name) {
|
|
|
28517
28517
|
const lower = name.toLowerCase();
|
|
28518
28518
|
return lower === "retry-after" || lower.startsWith("anthropic-ratelimit-") || lower.startsWith("x-ratelimit-") || lower.startsWith("ratelimit") || lower.startsWith("x-codex-");
|
|
28519
28519
|
}
|
|
28520
|
-
var USAGE_SCHEMA_VERSION, VersionSchema, InstantSchema, CLIENT_SESSION_ID_PATTERN, TokenCountSchema, TokenUsageSchema, LIMIT_KINDS, LimitClassificationSchema, UsageRecordSchema, QuotaWindowSchema, ExtraUsageSpendSchema, UnifiedRateLimitSchema, RateLimitStateSchema, LimitEventSchema, ProviderQuotaWindowSchema, ProviderAccountSchema, ProviderQuotaSchema, ProviderUsageStateSchema, AccountMetadataSchema, UsageSnapshotSchema;
|
|
28520
|
+
var USAGE_SCHEMA_VERSION, VersionSchema, InstantSchema, CLIENT_SESSION_ID_PATTERN, TokenCountSchema, TokenUsageSchema, LIMIT_KINDS, LimitClassificationSchema, REQUEST_CLASSES, RequestClassSchema, UsageRecordSchema, QuotaWindowSchema, ExtraUsageSpendSchema, UnifiedRateLimitSchema, RateLimitStateSchema, LimitEventSchema, ProviderQuotaWindowSchema, ProviderAccountSchema, ProviderQuotaSchema, ProviderUsageStateSchema, AccountMetadataSchema, UsageSnapshotSchema;
|
|
28521
28521
|
var init_schema2 = __esm({
|
|
28522
28522
|
"src/usage/schema.ts"() {
|
|
28523
28523
|
"use strict";
|
|
@@ -28545,6 +28545,8 @@ var init_schema2 = __esm({
|
|
|
28545
28545
|
/** The signals the classification rests on, each `<name>=<value>` from a response header or the error body's `type` or `code`. Never the error message. */
|
|
28546
28546
|
evidence: external_exports.array(external_exports.string())
|
|
28547
28547
|
});
|
|
28548
|
+
REQUEST_CLASSES = ["main", "subagent", "workflow", "compaction", "auxiliary", "side_reply", "unknown"];
|
|
28549
|
+
RequestClassSchema = external_exports.enum(REQUEST_CLASSES);
|
|
28548
28550
|
UsageRecordSchema = external_exports.strictObject({
|
|
28549
28551
|
schemaVersion: VersionSchema,
|
|
28550
28552
|
/** When the front door received the request. */
|
|
@@ -28568,8 +28570,10 @@ var init_schema2 = __esm({
|
|
|
28568
28570
|
endpoint: external_exports.string(),
|
|
28569
28571
|
/** The model the response named. */
|
|
28570
28572
|
model: external_exports.string().optional(),
|
|
28571
|
-
/** The model the
|
|
28573
|
+
/** The model name the request asked for, as its body carried it before any routing layer rewrote it: compared with `model`, it tells a choice or tier mapping apart from a name served unchanged. Present when the response served a different model than the name asked for, and when the response names no model (an error response included) and the door had read the request's body; absent when the response confirms the name. */
|
|
28572
28574
|
requestedModel: external_exports.string().optional(),
|
|
28575
|
+
/** The request's class, as Claude Code stated it in its `x-claude-code-request-class` header: `main` for the conversation's own loop, otherwise `subagent`, `workflow`, `compaction`, `auxiliary` (a hook judge, a title) or `side_reply`; `unknown` when the header carried anything else, so a client-chosen string never reaches the log. `subagent` as well for a request that states no class but carries Claude Code's agent id header, which it sends on every non-main agent request. Absent when the client sent neither. Claude Code derives it from the request's query source, which it does not send, so the query source itself is never recorded. */
|
|
28576
|
+
requestClass: RequestClassSchema.optional(),
|
|
28573
28577
|
/** The distinct providers the choice could have landed on, present only when there was more than one, so a reader can tell a choice that crossed providers from one that stayed inside a single provider's lineup. */
|
|
28574
28578
|
candidateProviders: external_exports.array(external_exports.string()).optional(),
|
|
28575
28579
|
status: external_exports.number().int(),
|
|
@@ -31457,6 +31461,9 @@ function readCatalogueStatus(fs38, paths, pid, generation, name) {
|
|
|
31457
31461
|
return void 0;
|
|
31458
31462
|
}
|
|
31459
31463
|
}
|
|
31464
|
+
function launchKeyOf(launch) {
|
|
31465
|
+
return `${String(launch.pid)}-${String(launch.startedAt)}`;
|
|
31466
|
+
}
|
|
31460
31467
|
function writeFrontDoorCodexHome(fs38, frontdoorDir, pid, codexHome2, generation) {
|
|
31461
31468
|
const directory = import_node_path7.default.join(frontdoorDir, "codex-homes");
|
|
31462
31469
|
fs38.mkdirp(directory);
|
|
@@ -31577,8 +31584,33 @@ function pruneDeadFrontDoorDirectRecords(fs38, recordsDir, records, liveness) {
|
|
|
31577
31584
|
function recordOf(details) {
|
|
31578
31585
|
return details === void 0 ? {} : { sessionId: details.sessionId, ...details.identity === void 0 ? {} : { identity: details.identity }, ...details.models === void 0 ? {} : { models: details.models } };
|
|
31579
31586
|
}
|
|
31587
|
+
function launchRouteStore(fs38, sessionsDir, log) {
|
|
31588
|
+
const failing = /* @__PURE__ */ new Set();
|
|
31589
|
+
return {
|
|
31590
|
+
read: (launch) => readLaunchRoute(fs38, sessionsDir, launch),
|
|
31591
|
+
write: (launch, route) => {
|
|
31592
|
+
writeLaunchRoute(fs38, sessionsDir, launch, route);
|
|
31593
|
+
},
|
|
31594
|
+
// Best effort and the one place that owns its failure: the identity only saves a read, so a stat that cannot be made (no permission, an I/O error, a path that is not a directory) is no identity, never a failed request.
|
|
31595
|
+
identity: (launch) => {
|
|
31596
|
+
const key = launchKeyOf(launch);
|
|
31597
|
+
try {
|
|
31598
|
+
const stat2 = fs38.lstat(import_node_path7.default.join(sessionsDir, launchRouteName(launch.pid)));
|
|
31599
|
+
failing.delete(key);
|
|
31600
|
+
return stat2 === void 0 ? void 0 : `${String(stat2.mtimeMs)}:${String(stat2.sizeBytes)}`;
|
|
31601
|
+
} catch (error63) {
|
|
31602
|
+
if (!failing.has(key)) {
|
|
31603
|
+
failing.add(key);
|
|
31604
|
+
log(`front door: could not stat where launch ${String(launch.pid)}'s main loop went, so a route another door writes may go unseen until its next main-loop request: ${error63 instanceof Error ? error63.message : String(error63)}`);
|
|
31605
|
+
}
|
|
31606
|
+
return void 0;
|
|
31607
|
+
}
|
|
31608
|
+
}
|
|
31609
|
+
};
|
|
31610
|
+
}
|
|
31580
31611
|
function writeFrontDoorSession(fs38, sessionsDir, session) {
|
|
31581
31612
|
fs38.mkdirPrivate(sessionsDir);
|
|
31613
|
+
fs38.removeRecursive(import_node_path7.default.join(sessionsDir, launchRouteName(session.pid)));
|
|
31582
31614
|
fs38.writeFilePrivate(import_node_path7.default.join(sessionsDir, sessionRecordName(session.pid)), `${JSON.stringify(session, null, 2)}
|
|
31583
31615
|
`);
|
|
31584
31616
|
}
|
|
@@ -31586,7 +31618,40 @@ function sessionRecordName(pid) {
|
|
|
31586
31618
|
return `${String(pid)}.json`;
|
|
31587
31619
|
}
|
|
31588
31620
|
function isSessionRecordName(name) {
|
|
31589
|
-
return name
|
|
31621
|
+
return /^[1-9]\d*\.json$/.test(name);
|
|
31622
|
+
}
|
|
31623
|
+
function launchRouteName(pid) {
|
|
31624
|
+
return `${String(pid)}.route.json`;
|
|
31625
|
+
}
|
|
31626
|
+
function writeLaunchRoute(fs38, sessionsDir, launch, route) {
|
|
31627
|
+
if (readFrontDoorSessionRecord(fs38, sessionsDir, sessionRecordName(launch.pid))?.startedAt !== launch.startedAt) {
|
|
31628
|
+
return;
|
|
31629
|
+
}
|
|
31630
|
+
fs38.mkdirPrivate(sessionsDir);
|
|
31631
|
+
fs38.writeFilePrivate(import_node_path7.default.join(sessionsDir, launchRouteName(launch.pid)), `${JSON.stringify({ version: LAUNCH_ROUTE_FILE_VERSION, startedAt: launch.startedAt, route })}
|
|
31632
|
+
`);
|
|
31633
|
+
}
|
|
31634
|
+
function readLaunchRoute(fs38, sessionsDir, launch) {
|
|
31635
|
+
const name = launchRouteName(launch.pid);
|
|
31636
|
+
const text5 = fs38.readFileUtf8(import_node_path7.default.join(sessionsDir, name));
|
|
31637
|
+
if (text5 === void 0) {
|
|
31638
|
+
return void 0;
|
|
31639
|
+
}
|
|
31640
|
+
let json2;
|
|
31641
|
+
try {
|
|
31642
|
+
json2 = JSON.parse(text5);
|
|
31643
|
+
} catch {
|
|
31644
|
+
throw new Error(`${name} is not JSON`);
|
|
31645
|
+
}
|
|
31646
|
+
const version2 = typeof json2 === "object" && json2 !== null && "version" in json2 ? json2.version : void 0;
|
|
31647
|
+
if (typeof version2 === "number" && version2 > LAUNCH_ROUTE_FILE_VERSION) {
|
|
31648
|
+
throw new Error(`${name} is version ${String(version2)}, written by a newer door than this one reads (version ${String(LAUNCH_ROUTE_FILE_VERSION)})`);
|
|
31649
|
+
}
|
|
31650
|
+
const parsed = LaunchRouteFileSchema.safeParse(json2);
|
|
31651
|
+
if (!parsed.success) {
|
|
31652
|
+
throw new Error(`${name} is not a route file this door reads`);
|
|
31653
|
+
}
|
|
31654
|
+
return parsed.data.startedAt === launch.startedAt ? parsed.data.route : void 0;
|
|
31590
31655
|
}
|
|
31591
31656
|
function summaryOf(session) {
|
|
31592
31657
|
return { pid: session.pid, startedAt: session.startedAt, ...session.supervisorPid === void 0 ? {} : { supervisorPid: session.supervisorPid } };
|
|
@@ -31609,8 +31674,13 @@ function listFrontDoorSessions(fs38, sessionsDir) {
|
|
|
31609
31674
|
}
|
|
31610
31675
|
return sessions.sort((a10, b) => a10.pid - b.pid);
|
|
31611
31676
|
}
|
|
31677
|
+
function routeFilePid(name) {
|
|
31678
|
+
const match = /^([1-9]\d*)\.route\.json$/.exec(name);
|
|
31679
|
+
return match?.[1] === void 0 ? void 0 : Number(match[1]);
|
|
31680
|
+
}
|
|
31612
31681
|
function removeFrontDoorSession(fs38, sessionsDir, pid) {
|
|
31613
31682
|
fs38.removeRecursive(import_node_path7.default.join(sessionsDir, sessionRecordName(pid)));
|
|
31683
|
+
fs38.removeRecursive(import_node_path7.default.join(sessionsDir, launchRouteName(pid)));
|
|
31614
31684
|
}
|
|
31615
31685
|
function pruneDeadFrontDoorSessions(fs38, sessionsDir, listed, isRunning) {
|
|
31616
31686
|
const removed = [];
|
|
@@ -31620,9 +31690,16 @@ function pruneDeadFrontDoorSessions(fs38, sessionsDir, listed, isRunning) {
|
|
|
31620
31690
|
removed.push(session.pid);
|
|
31621
31691
|
}
|
|
31622
31692
|
}
|
|
31693
|
+
const names = fs38.readdir(sessionsDir);
|
|
31694
|
+
for (const name of names) {
|
|
31695
|
+
const pid = routeFilePid(name);
|
|
31696
|
+
if (pid !== void 0 && !names.includes(sessionRecordName(pid))) {
|
|
31697
|
+
fs38.removeRecursive(import_node_path7.default.join(sessionsDir, name));
|
|
31698
|
+
}
|
|
31699
|
+
}
|
|
31623
31700
|
return removed;
|
|
31624
31701
|
}
|
|
31625
|
-
var import_node_path7, FRONT_DOOR_PROTOCOL, FRONT_DOOR_HANDOVER_PROTOCOL, FrontDoorSessionSchema, FrontDoorStateSchema, FrontDoorDirectRecordSchema, RECORD_TEMPORARY_FILE;
|
|
31702
|
+
var import_node_path7, FRONT_DOOR_PROTOCOL, FRONT_DOOR_HANDOVER_PROTOCOL, FrontDoorSessionSchema, MainLoopRouteSchema, FrontDoorStateSchema, FrontDoorDirectRecordSchema, RECORD_TEMPORARY_FILE, LAUNCH_ROUTE_FILE_VERSION, LaunchRouteFileSchema;
|
|
31626
31703
|
var init_state = __esm({
|
|
31627
31704
|
"src/frontdoor/state.ts"() {
|
|
31628
31705
|
"use strict";
|
|
@@ -31634,6 +31711,7 @@ var init_state = __esm({
|
|
|
31634
31711
|
FRONT_DOOR_PROTOCOL = 8;
|
|
31635
31712
|
FRONT_DOOR_HANDOVER_PROTOCOL = 6;
|
|
31636
31713
|
FrontDoorSessionSchema = external_exports.strictObject({ pid: external_exports.number().int().positive(), startedAt: external_exports.number(), token: external_exports.string().min(1), supervisorPid: external_exports.number().int().positive().optional(), sessionId: external_exports.string().min(1).optional(), identity: external_exports.string().min(1).optional(), models: ModelsConfigSchema.optional() });
|
|
31714
|
+
MainLoopRouteSchema = external_exports.union([external_exports.strictObject({ kind: external_exports.literal("anthropic") }), external_exports.strictObject({ kind: external_exports.literal("provider"), provider: external_exports.string().min(1) })]);
|
|
31637
31715
|
FrontDoorStateSchema = external_exports.strictObject({
|
|
31638
31716
|
/**
|
|
31639
31717
|
* The launcher-to-door wire protocol this supervisor speaks, written once it is serving; absent in state written by a release that predates the field, which `ensureFrontDoor` reads as protocol 0. A launcher replaces a door whose protocol is lower than its own (see `FRONT_DOOR_PROTOCOL`).
|
|
@@ -31666,6 +31744,8 @@ var init_state = __esm({
|
|
|
31666
31744
|
});
|
|
31667
31745
|
FrontDoorDirectRecordSchema = external_exports.strictObject({ directPort: external_exports.number().int().positive().max(MAX_TCP_PORT) });
|
|
31668
31746
|
RECORD_TEMPORARY_FILE = /^[1-9]\d*\.json\.([1-9]\d*)\.tmp$/;
|
|
31747
|
+
LAUNCH_ROUTE_FILE_VERSION = 1;
|
|
31748
|
+
LaunchRouteFileSchema = external_exports.strictObject({ version: external_exports.literal(LAUNCH_ROUTE_FILE_VERSION), startedAt: external_exports.number(), route: MainLoopRouteSchema });
|
|
31669
31749
|
}
|
|
31670
31750
|
});
|
|
31671
31751
|
|
|
@@ -37099,6 +37179,7 @@ function buildRecord(event, context) {
|
|
|
37099
37179
|
...scan?.model === void 0 ? {} : { model: scan.model },
|
|
37100
37180
|
...requestedModel === void 0 ? {} : { requestedModel },
|
|
37101
37181
|
...choice !== void 0 && choice.providers.length > 1 ? { candidateProviders: [...choice.providers] } : {},
|
|
37182
|
+
...event.requestClass === void 0 ? {} : { requestClass: event.requestClass },
|
|
37102
37183
|
status: event.status,
|
|
37103
37184
|
latencyMs: Math.max(0, Math.round(context.headAt - event.receivedAt)),
|
|
37104
37185
|
durationMs: Math.max(0, Math.round(context.endedAt - event.receivedAt)),
|
|
@@ -39598,7 +39679,7 @@ function mergeModels(current, layer) {
|
|
|
39598
39679
|
}
|
|
39599
39680
|
if (layer.tiers !== void 0) {
|
|
39600
39681
|
const tiers = {};
|
|
39601
|
-
for (const tier of
|
|
39682
|
+
for (const tier of CLAUDE_TIERS) {
|
|
39602
39683
|
const ref = layer.tiers[tier];
|
|
39603
39684
|
const inherited = current?.tiers?.[tier];
|
|
39604
39685
|
if (ref !== null && ref !== void 0) {
|
|
@@ -46606,6 +46687,25 @@ var init_catalogueCredential = __esm({
|
|
|
46606
46687
|
}
|
|
46607
46688
|
});
|
|
46608
46689
|
|
|
46690
|
+
// src/models/tiers.ts
|
|
46691
|
+
function claudeTierOf(model) {
|
|
46692
|
+
const lower = model.toLowerCase();
|
|
46693
|
+
return CLAUDE_TIERS.find((tier) => lower.includes(tier));
|
|
46694
|
+
}
|
|
46695
|
+
var TIER_ENV_VARIABLES;
|
|
46696
|
+
var init_tiers = __esm({
|
|
46697
|
+
"src/models/tiers.ts"() {
|
|
46698
|
+
"use strict";
|
|
46699
|
+
init_schema();
|
|
46700
|
+
TIER_ENV_VARIABLES = Object.freeze({
|
|
46701
|
+
fable: "ANTHROPIC_DEFAULT_FABLE_MODEL",
|
|
46702
|
+
opus: "ANTHROPIC_DEFAULT_OPUS_MODEL",
|
|
46703
|
+
sonnet: "ANTHROPIC_DEFAULT_SONNET_MODEL",
|
|
46704
|
+
haiku: "ANTHROPIC_DEFAULT_HAIKU_MODEL"
|
|
46705
|
+
});
|
|
46706
|
+
}
|
|
46707
|
+
});
|
|
46708
|
+
|
|
46609
46709
|
// src/selection/engine.ts
|
|
46610
46710
|
function compareMaybe(left, right, preferHigh) {
|
|
46611
46711
|
if (left === void 0 && right === void 0) {
|
|
@@ -47457,7 +47557,7 @@ function resolveName(requested, config2) {
|
|
|
47457
47557
|
if (isBackendModelName(stripped)) {
|
|
47458
47558
|
return { candidates: [stripped], tier: void 0 };
|
|
47459
47559
|
}
|
|
47460
|
-
const tier =
|
|
47560
|
+
const tier = claudeTierOf(stripped);
|
|
47461
47561
|
return tier === void 0 ? { candidates: [config2.defaultModel], tier: void 0 } : { candidates: config2.models[tier], tier };
|
|
47462
47562
|
}
|
|
47463
47563
|
function candidateStages(requested, config2) {
|
|
@@ -47692,6 +47792,7 @@ var init_translate = __esm({
|
|
|
47692
47792
|
"use strict";
|
|
47693
47793
|
import_node_crypto18 = require("node:crypto");
|
|
47694
47794
|
init_identity2();
|
|
47795
|
+
init_tiers();
|
|
47695
47796
|
init_engine();
|
|
47696
47797
|
init_quota();
|
|
47697
47798
|
init_signatures();
|
|
@@ -47738,8 +47839,7 @@ function mapModel(requested, config2) {
|
|
|
47738
47839
|
if (requested.startsWith("gpt-")) {
|
|
47739
47840
|
return requested;
|
|
47740
47841
|
}
|
|
47741
|
-
const
|
|
47742
|
-
const tier = CLAUDE_TIERS.find((candidate) => lower.includes(candidate));
|
|
47842
|
+
const tier = claudeTierOf(requested);
|
|
47743
47843
|
return tier === void 0 ? config2.defaultModel : config2.models[tier];
|
|
47744
47844
|
}
|
|
47745
47845
|
function isRequestableEffort(value3) {
|
|
@@ -47888,6 +47988,7 @@ var init_translate2 = __esm({
|
|
|
47888
47988
|
"src/codex/translate.ts"() {
|
|
47889
47989
|
"use strict";
|
|
47890
47990
|
import_node_crypto19 = require("node:crypto");
|
|
47991
|
+
init_tiers();
|
|
47891
47992
|
init_schema();
|
|
47892
47993
|
init_anthropic();
|
|
47893
47994
|
REQUESTABLE_EFFORTS = ["low", "medium", "high"];
|
|
@@ -48017,7 +48118,7 @@ function safeExpand(name, models) {
|
|
|
48017
48118
|
function modelPickerSettingsFromRows(options, replaceBuiltInOptions) {
|
|
48018
48119
|
return options.length === 0 ? void 0 : JSON.stringify({ modelPicker: { options, replaceBuiltInOptions } });
|
|
48019
48120
|
}
|
|
48020
|
-
var TIER_BEHAVES_AS
|
|
48121
|
+
var TIER_BEHAVES_AS;
|
|
48021
48122
|
var init_modelPicker = __esm({
|
|
48022
48123
|
"src/launcher/modelPicker.ts"() {
|
|
48023
48124
|
"use strict";
|
|
@@ -48028,18 +48129,13 @@ var init_modelPicker = __esm({
|
|
|
48028
48129
|
init_identity2();
|
|
48029
48130
|
init_families();
|
|
48030
48131
|
init_modelsStore();
|
|
48132
|
+
init_tiers();
|
|
48031
48133
|
TIER_BEHAVES_AS = Object.freeze({
|
|
48032
48134
|
fable: "claude-fable-5-1",
|
|
48033
48135
|
opus: "claude-opus-5-5",
|
|
48034
48136
|
sonnet: "claude-sonnet-5-5",
|
|
48035
48137
|
haiku: "claude-haiku-5-5"
|
|
48036
48138
|
});
|
|
48037
|
-
TIER_ENV_VARIABLES = Object.freeze({
|
|
48038
|
-
fable: "ANTHROPIC_DEFAULT_FABLE_MODEL",
|
|
48039
|
-
opus: "ANTHROPIC_DEFAULT_OPUS_MODEL",
|
|
48040
|
-
sonnet: "ANTHROPIC_DEFAULT_SONNET_MODEL",
|
|
48041
|
-
haiku: "ANTHROPIC_DEFAULT_HAIKU_MODEL"
|
|
48042
|
-
});
|
|
48043
48139
|
}
|
|
48044
48140
|
});
|
|
48045
48141
|
|
|
@@ -74444,7 +74540,7 @@ var init_package = __esm({
|
|
|
74444
74540
|
"package.json"() {
|
|
74445
74541
|
package_default = {
|
|
74446
74542
|
name: "claude-use",
|
|
74447
|
-
version: "14.0
|
|
74543
|
+
version: "14.1.0",
|
|
74448
74544
|
description: "A profile manager and launcher for Claude Code that lets one person run multiple logins from one machine while controlling what gets shared between them.",
|
|
74449
74545
|
license: "MIT",
|
|
74450
74546
|
author: "Joseph Mearman <joseph@mearman.co.uk>",
|
|
@@ -75776,6 +75872,7 @@ function buildEnv(params) {
|
|
|
75776
75872
|
launcherHeaders
|
|
75777
75873
|
);
|
|
75778
75874
|
inject("NODE_EXTRA_CA_CERTS", params.frontdoor.trustBundlePath);
|
|
75875
|
+
inject("CLAUDE_CODE_GATEWAY_HINT_HEADERS", "1");
|
|
75779
75876
|
inject("HTTPS_PROXY", connectProxyUrl(params.frontdoor.connectPort, params.frontdoor.sessionToken));
|
|
75780
75877
|
const bypassed = [...LOOPBACK_HOSTS, ...params.proxyBypass ?? []];
|
|
75781
75878
|
for (const list2 of ["NO_PROXY", "no_proxy"]) {
|
|
@@ -131452,7 +131549,6 @@ function showSession(fs38, configDirOf, identities, query, onlyIdentity) {
|
|
|
131452
131549
|
}
|
|
131453
131550
|
|
|
131454
131551
|
// src/frontdoor/commands.ts
|
|
131455
|
-
init_identityStore();
|
|
131456
131552
|
init_realPorts();
|
|
131457
131553
|
|
|
131458
131554
|
// src/headroom/keeper.ts
|
|
@@ -132404,14 +132500,15 @@ init_route();
|
|
|
132404
132500
|
function singleValue(value3) {
|
|
132405
132501
|
return typeof value3 === "string" ? value3 : void 0;
|
|
132406
132502
|
}
|
|
132407
|
-
function admitLaunch(isLiveToken, identityOfToken) {
|
|
132503
|
+
function admitLaunch(isLiveToken, identityOfToken, launchOfToken) {
|
|
132408
132504
|
return (request7) => {
|
|
132409
132505
|
const token = singleValue(request7.headers[AUTH_HEADER]);
|
|
132410
132506
|
if (token === void 0 || !isLiveToken(token)) {
|
|
132411
132507
|
return { ok: false, message: "agent-shim front door: this request carries no capability from a live agent-shim launch" };
|
|
132412
132508
|
}
|
|
132413
132509
|
const identity = identityOfToken(token);
|
|
132414
|
-
|
|
132510
|
+
const launch = launchOfToken(token);
|
|
132511
|
+
return { ok: true, headers: { ...request7.forwardable }, ...identity === void 0 ? {} : { identity }, ...launch === void 0 ? {} : { launch } };
|
|
132415
132512
|
};
|
|
132416
132513
|
}
|
|
132417
132514
|
function admitHop(hopSecret, custody) {
|
|
@@ -132434,7 +132531,7 @@ function createDoorPipelines(deps) {
|
|
|
132434
132531
|
resolveRoute: deps.resolveRoute,
|
|
132435
132532
|
// The response middleware hook point, where usage tracking registers. The direct listener registers none, because its responses are consumed by headroom, not by the client: the entry the client sees is where observation belongs, and observing both would record every hopped request twice.
|
|
132436
132533
|
responseObservers: deps.responseObservers,
|
|
132437
|
-
admit: admitLaunch(deps.isLiveToken, deps.identityOfToken),
|
|
132534
|
+
admit: admitLaunch(deps.isLiveToken, deps.identityOfToken, deps.launchOfToken),
|
|
132438
132535
|
headroom: { headroomSocket: deps.headroomSocket, hopSecret: deps.hopSecret, custody: deps.custody, log: deps.log },
|
|
132439
132536
|
headroomDecision: deps.headroomDecision,
|
|
132440
132537
|
canonicalProvider: deps.canonicalProvider,
|
|
@@ -145236,12 +145333,14 @@ function createLiveSessionRegistry(ports) {
|
|
|
145236
145333
|
let tokens = /* @__PURE__ */ new Set();
|
|
145237
145334
|
let byToken = /* @__PURE__ */ new Map();
|
|
145238
145335
|
let bySessionId = /* @__PURE__ */ new Map();
|
|
145336
|
+
let launches = /* @__PURE__ */ new Set();
|
|
145239
145337
|
const recordNames = () => fs38.readdir(sessionsDir).filter(isSessionRecordName);
|
|
145240
145338
|
const rebuildIndexes = () => {
|
|
145241
145339
|
const records = [...known.values()].flatMap(({ record: record3 }) => record3 === void 0 ? [] : [record3]);
|
|
145242
145340
|
tokens = new Set(records.map((record3) => record3.token));
|
|
145243
145341
|
byToken = new Map(records.map((record3) => [record3.token, record3]));
|
|
145244
145342
|
bySessionId = new Map(records.flatMap((record3) => record3.sessionId === void 0 ? [] : [[record3.sessionId, record3]]));
|
|
145343
|
+
launches = new Set(records.map(launchKeyOf));
|
|
145245
145344
|
};
|
|
145246
145345
|
const load = (name, stat2) => {
|
|
145247
145346
|
known.set(name, { stat: { mtimeMs: stat2.mtimeMs, sizeBytes: stat2.sizeBytes }, record: readFrontDoorSessionRecord(fs38, sessionsDir, name) });
|
|
@@ -145283,6 +145382,11 @@ function createLiveSessionRegistry(ports) {
|
|
|
145283
145382
|
const held = bySessionId.get(sessionId) ?? (reconcile() ? bySessionId.get(sessionId) : void 0);
|
|
145284
145383
|
return held?.models;
|
|
145285
145384
|
},
|
|
145385
|
+
launchOfToken: (token) => {
|
|
145386
|
+
const held = byToken.get(token) ?? (reconcile() ? byToken.get(token) : void 0);
|
|
145387
|
+
return held?.sessionId === void 0 ? void 0 : { pid: held.pid, startedAt: held.startedAt };
|
|
145388
|
+
},
|
|
145389
|
+
holdsLaunch: (launch) => launches.has(launchKeyOf(launch)) || reconcile() && launches.has(launchKeyOf(launch)),
|
|
145286
145390
|
rescan: () => {
|
|
145287
145391
|
known.clear();
|
|
145288
145392
|
for (const name of recordNames()) {
|
|
@@ -145786,6 +145890,271 @@ async function hopOnce(request7, response, target, body, mayRetry) {
|
|
|
145786
145890
|
});
|
|
145787
145891
|
}
|
|
145788
145892
|
|
|
145893
|
+
// src/frontdoor/mainLoopRoute.ts
|
|
145894
|
+
var import_node_stream3 = require("node:stream");
|
|
145895
|
+
init_http();
|
|
145896
|
+
init_schema2();
|
|
145897
|
+
init_route();
|
|
145898
|
+
|
|
145899
|
+
// src/frontdoor/requestScan.ts
|
|
145900
|
+
var import_node_stream2 = require("node:stream");
|
|
145901
|
+
var import_node_string_decoder = require("node:string_decoder");
|
|
145902
|
+
var BYTES_PER_KIB2 = 1024;
|
|
145903
|
+
var SCAN_KIB = 64;
|
|
145904
|
+
var REQUEST_SCAN_CAP_BYTES = SCAN_KIB * BYTES_PER_KIB2;
|
|
145905
|
+
async function scanRequestHead(request7, wholeBody = false) {
|
|
145906
|
+
const body = request7.body;
|
|
145907
|
+
if (body.readableEnded) {
|
|
145908
|
+
throw new Error("the request body was already read to its end, so its head cannot be scanned again");
|
|
145909
|
+
}
|
|
145910
|
+
const head = [];
|
|
145911
|
+
let seen = 0;
|
|
145912
|
+
let text5 = "";
|
|
145913
|
+
const decoder = new import_node_string_decoder.StringDecoder("utf8");
|
|
145914
|
+
let decided = false;
|
|
145915
|
+
let ended = false;
|
|
145916
|
+
let error63;
|
|
145917
|
+
const replay = new import_node_stream2.PassThrough();
|
|
145918
|
+
replay.on("error", () => void 0);
|
|
145919
|
+
const modelIn = () => /"model"\s*:\s*"((?:[^"\\]|\\.)+)"/.exec(text5)?.[1];
|
|
145920
|
+
const settled = new Promise((resolve3) => {
|
|
145921
|
+
const settle2 = () => {
|
|
145922
|
+
const model = modelIn();
|
|
145923
|
+
const hasImage = /"type"\s*:\s*"image"/.test(text5);
|
|
145924
|
+
for (const chunk2 of head) {
|
|
145925
|
+
replay.write(chunk2);
|
|
145926
|
+
}
|
|
145927
|
+
const maxTokens = /"max_tokens"\s*:\s*([0-9]+)/.exec(text5)?.[1];
|
|
145928
|
+
resolve3({
|
|
145929
|
+
...model === void 0 ? {} : { model },
|
|
145930
|
+
headBytes: seen,
|
|
145931
|
+
hasImage,
|
|
145932
|
+
toolsPresent: /"tools"\s*:\s*\[\s*\{/.test(text5),
|
|
145933
|
+
thinking: /"thinking"\s*:\s*\{\s*"type"\s*:\s*"enabled"/.test(text5),
|
|
145934
|
+
...maxTokens === void 0 ? {} : { maxTokens: Number(maxTokens) },
|
|
145935
|
+
isCountTokens: request7.url.includes("/v1/messages/count_tokens"),
|
|
145936
|
+
...wholeBody ? { whole: true, text: text5 } : {},
|
|
145937
|
+
replayed: { ...request7, body: replay }
|
|
145938
|
+
});
|
|
145939
|
+
};
|
|
145940
|
+
body.on("data", (chunk2) => {
|
|
145941
|
+
if (decided) {
|
|
145942
|
+
if (!replay.write(chunk2)) {
|
|
145943
|
+
body.pause();
|
|
145944
|
+
replay.once("drain", () => {
|
|
145945
|
+
body.resume();
|
|
145946
|
+
});
|
|
145947
|
+
}
|
|
145948
|
+
return;
|
|
145949
|
+
}
|
|
145950
|
+
head.push(chunk2);
|
|
145951
|
+
seen += chunk2.length;
|
|
145952
|
+
text5 += decoder.write(chunk2);
|
|
145953
|
+
if (!wholeBody && (modelIn() !== void 0 || seen >= REQUEST_SCAN_CAP_BYTES)) {
|
|
145954
|
+
decided = true;
|
|
145955
|
+
settle2();
|
|
145956
|
+
}
|
|
145957
|
+
});
|
|
145958
|
+
body.on("end", () => {
|
|
145959
|
+
ended = true;
|
|
145960
|
+
text5 += decoder.end();
|
|
145961
|
+
if (!decided) {
|
|
145962
|
+
decided = true;
|
|
145963
|
+
settle2();
|
|
145964
|
+
}
|
|
145965
|
+
replay.end();
|
|
145966
|
+
});
|
|
145967
|
+
body.on("error", (cause) => {
|
|
145968
|
+
error63 = cause;
|
|
145969
|
+
if (!decided) {
|
|
145970
|
+
decided = true;
|
|
145971
|
+
settle2();
|
|
145972
|
+
}
|
|
145973
|
+
replay.destroy(cause);
|
|
145974
|
+
});
|
|
145975
|
+
body.on("close", () => {
|
|
145976
|
+
if (!decided && !ended && error63 === void 0) {
|
|
145977
|
+
decided = true;
|
|
145978
|
+
settle2();
|
|
145979
|
+
}
|
|
145980
|
+
});
|
|
145981
|
+
});
|
|
145982
|
+
return await settled;
|
|
145983
|
+
}
|
|
145984
|
+
|
|
145985
|
+
// src/frontdoor/mainLoopRoute.ts
|
|
145986
|
+
init_state();
|
|
145987
|
+
var REQUEST_CLASS_HEADER = "x-claude-code-request-class";
|
|
145988
|
+
var AGENT_ID_HEADER = "x-claude-code-agent-id";
|
|
145989
|
+
var MAIN_REQUEST_CLASS = "main";
|
|
145990
|
+
function requestClassOf(headers) {
|
|
145991
|
+
const agentId = internalHeaderValue(headers, AGENT_ID_HEADER);
|
|
145992
|
+
const hasAgentId = agentId !== void 0 && agentId !== "";
|
|
145993
|
+
const value3 = internalHeaderValue(headers, REQUEST_CLASS_HEADER);
|
|
145994
|
+
if (value3 === void 0) {
|
|
145995
|
+
return hasAgentId ? "subagent" : void 0;
|
|
145996
|
+
}
|
|
145997
|
+
const parsed = RequestClassSchema.safeParse(value3);
|
|
145998
|
+
const stated = parsed.success ? parsed.data : "unknown";
|
|
145999
|
+
return hasAgentId && stated === MAIN_REQUEST_CLASS ? "subagent" : stated;
|
|
146000
|
+
}
|
|
146001
|
+
function sameRoute(a10, b) {
|
|
146002
|
+
return a10.kind === b.kind && (a10.kind !== "provider" || b.kind === "provider" && a10.provider === b.provider);
|
|
146003
|
+
}
|
|
146004
|
+
function createMainLoopRouteRecord(ports) {
|
|
146005
|
+
const routes = /* @__PURE__ */ new Map();
|
|
146006
|
+
const failed = /* @__PURE__ */ new Set();
|
|
146007
|
+
const dropEnded = (except) => {
|
|
146008
|
+
for (const [held, entry] of routes) {
|
|
146009
|
+
if (held !== except && !ports.holdsLaunch(entry.launch)) {
|
|
146010
|
+
routes.delete(held);
|
|
146011
|
+
failed.delete(held);
|
|
146012
|
+
}
|
|
146013
|
+
}
|
|
146014
|
+
};
|
|
146015
|
+
const persist = (launch, key, route, persisted) => {
|
|
146016
|
+
if (persisted !== void 0 && sameRoute(persisted, route)) {
|
|
146017
|
+
return persisted;
|
|
146018
|
+
}
|
|
146019
|
+
try {
|
|
146020
|
+
ports.stored.write(launch, route);
|
|
146021
|
+
failed.delete(key);
|
|
146022
|
+
return route;
|
|
146023
|
+
} catch (error63) {
|
|
146024
|
+
if (!failed.has(key)) {
|
|
146025
|
+
failed.add(key);
|
|
146026
|
+
ports.log(`front door: could not record where launch ${String(launch.pid)}'s main loop went, so a door restart would send its helpers to the route last stored: ${error63 instanceof Error ? error63.message : String(error63)}`);
|
|
146027
|
+
}
|
|
146028
|
+
return persisted;
|
|
146029
|
+
}
|
|
146030
|
+
};
|
|
146031
|
+
const load = (launch) => {
|
|
146032
|
+
const identity = ports.stored.identity(launch);
|
|
146033
|
+
let stored2;
|
|
146034
|
+
try {
|
|
146035
|
+
stored2 = ports.stored.read(launch);
|
|
146036
|
+
} catch (error63) {
|
|
146037
|
+
ports.log(`front door: could not read where launch ${String(launch.pid)}'s main loop went (${error63 instanceof Error ? error63.message : String(error63)}), so its helpers go the route the launch was launched on until its next main-loop request`);
|
|
146038
|
+
}
|
|
146039
|
+
return { launch, route: stored2, persisted: stored2, identity };
|
|
146040
|
+
};
|
|
146041
|
+
return {
|
|
146042
|
+
record: (launch, route) => {
|
|
146043
|
+
const key = launchKeyOf(launch);
|
|
146044
|
+
dropEnded(key);
|
|
146045
|
+
const before = routes.get(key);
|
|
146046
|
+
const persisted = persist(launch, key, route, before?.persisted);
|
|
146047
|
+
const written = persisted !== void 0 && sameRoute(persisted, route) && (before?.persisted === void 0 || !sameRoute(before.persisted, route));
|
|
146048
|
+
routes.set(key, { launch, route, persisted, identity: written ? ports.stored.identity(launch) : before?.identity });
|
|
146049
|
+
},
|
|
146050
|
+
routeOf: (launch) => {
|
|
146051
|
+
const key = launchKeyOf(launch);
|
|
146052
|
+
const entry = routes.get(key);
|
|
146053
|
+
if (entry === void 0) {
|
|
146054
|
+
dropEnded(key);
|
|
146055
|
+
const loaded = load(launch);
|
|
146056
|
+
routes.set(key, loaded);
|
|
146057
|
+
return loaded.route;
|
|
146058
|
+
}
|
|
146059
|
+
if (entry.route !== void 0 && (entry.persisted === void 0 || !sameRoute(entry.persisted, entry.route))) {
|
|
146060
|
+
const persisted = persist(launch, key, entry.route, entry.persisted);
|
|
146061
|
+
const written = persisted !== void 0 && sameRoute(persisted, entry.route);
|
|
146062
|
+
routes.set(key, { ...entry, persisted, identity: written ? ports.stored.identity(launch) : entry.identity });
|
|
146063
|
+
return entry.route;
|
|
146064
|
+
}
|
|
146065
|
+
const current = ports.stored.identity(launch);
|
|
146066
|
+
if (current !== void 0 && current !== entry.identity) {
|
|
146067
|
+
const loaded = load(launch);
|
|
146068
|
+
routes.set(key, loaded);
|
|
146069
|
+
return loaded.route;
|
|
146070
|
+
}
|
|
146071
|
+
return entry.route;
|
|
146072
|
+
}
|
|
146073
|
+
};
|
|
146074
|
+
}
|
|
146075
|
+
function isInferencePath(path101) {
|
|
146076
|
+
return path101 === "/v1/messages" || path101.startsWith("/v1/messages/");
|
|
146077
|
+
}
|
|
146078
|
+
function servedProviderOf(url2) {
|
|
146079
|
+
return parseProviderPath(new URL(url2, "http://127.0.0.1").pathname)?.provider;
|
|
146080
|
+
}
|
|
146081
|
+
function followingMainLoop(resolve3, deps) {
|
|
146082
|
+
return async (request7) => {
|
|
146083
|
+
const { launch, provider } = request7.session;
|
|
146084
|
+
const path101 = new URL(request7.url, "http://127.0.0.1").pathname;
|
|
146085
|
+
if (launch === void 0 || provider !== void 0 || !isInferencePath(path101)) {
|
|
146086
|
+
return await resolve3(request7);
|
|
146087
|
+
}
|
|
146088
|
+
const requestClass = requestClassOf(request7.headers);
|
|
146089
|
+
if (requestClass === void 0 || requestClass === MAIN_REQUEST_CLASS) {
|
|
146090
|
+
const resolution2 = await resolve3(request7);
|
|
146091
|
+
if (resolution2.ok) {
|
|
146092
|
+
const served = servedProviderOf(resolution2.request?.url ?? request7.url);
|
|
146093
|
+
deps.record.record(launch, served === void 0 ? { kind: "anthropic" } : { kind: "provider", provider: served });
|
|
146094
|
+
}
|
|
146095
|
+
return resolution2;
|
|
146096
|
+
}
|
|
146097
|
+
const route = deps.record.routeOf(launch);
|
|
146098
|
+
if (route === void 0 || route.kind === "anthropic") {
|
|
146099
|
+
return await resolve3(request7);
|
|
146100
|
+
}
|
|
146101
|
+
const scan = await scanRequestHead(request7);
|
|
146102
|
+
const requested = scan.model;
|
|
146103
|
+
const choice = requested === void 0 ? { kind: "as-is" } : deps.helperModel(route.provider, request7.session, requested);
|
|
146104
|
+
if (choice.kind === "refused") {
|
|
146105
|
+
return { ok: false, status: HTTP_STATUS.badRequest, message: choice.message };
|
|
146106
|
+
}
|
|
146107
|
+
const replayed = scan.replayed ?? request7;
|
|
146108
|
+
const body = choice.kind === "as-is" || requested === void 0 ? replayed : withModelInHead(replayed, scan.headBytes, requested, choice.model);
|
|
146109
|
+
const helper = { ...body, url: `${PROVIDER_PATH_PREFIX}${encodeURIComponent(route.provider)}${request7.url}` };
|
|
146110
|
+
const resolution = await resolve3(helper);
|
|
146111
|
+
if (!resolution.ok) {
|
|
146112
|
+
return resolution;
|
|
146113
|
+
}
|
|
146114
|
+
if (resolution.request === void 0) {
|
|
146115
|
+
return { ok: false, status: HTTP_STATUS.internalServerError, message: "the provider resolver served a helper request without saying which request it served" };
|
|
146116
|
+
}
|
|
146117
|
+
return { ...resolution, request: resolution.request, ...requested === void 0 ? {} : { requestedModel: requested } };
|
|
146118
|
+
};
|
|
146119
|
+
}
|
|
146120
|
+
function asBytes(value3) {
|
|
146121
|
+
return Buffer.from(value3, "utf8").toString("latin1");
|
|
146122
|
+
}
|
|
146123
|
+
function withModelInHead(request7, headBytes, from, to2) {
|
|
146124
|
+
const pattern = new RegExp('"model"\\s*:\\s*"' + asBytes(from).replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + '"');
|
|
146125
|
+
const rewriteHead = (head) => Buffer.from(head.toString("latin1").replace(pattern, () => `"model":${asBytes(JSON.stringify(to2))}`), "latin1");
|
|
146126
|
+
async function* rewritten() {
|
|
146127
|
+
let held = Buffer.alloc(0);
|
|
146128
|
+
let headDone = false;
|
|
146129
|
+
const chunks = request7.body;
|
|
146130
|
+
for await (const piece of chunks) {
|
|
146131
|
+
if (!Buffer.isBuffer(piece)) {
|
|
146132
|
+
throw new TypeError("a request body chunk is not a Buffer");
|
|
146133
|
+
}
|
|
146134
|
+
if (headDone) {
|
|
146135
|
+
yield piece;
|
|
146136
|
+
continue;
|
|
146137
|
+
}
|
|
146138
|
+
held = Buffer.concat([held, piece]);
|
|
146139
|
+
if (held.length >= headBytes) {
|
|
146140
|
+
headDone = true;
|
|
146141
|
+
yield rewriteHead(held.subarray(0, headBytes));
|
|
146142
|
+
yield held.subarray(headBytes);
|
|
146143
|
+
}
|
|
146144
|
+
}
|
|
146145
|
+
if (!headDone) {
|
|
146146
|
+
yield rewriteHead(held);
|
|
146147
|
+
}
|
|
146148
|
+
}
|
|
146149
|
+
const headers = Object.fromEntries(Object.entries(request7.headers).filter(([name]) => name.toLowerCase() !== "content-length"));
|
|
146150
|
+
return { ...request7, headers, body: import_node_stream3.Readable.from(rewritten()) };
|
|
146151
|
+
}
|
|
146152
|
+
function withModel(request7, text5, from, to2) {
|
|
146153
|
+
const rewritten = text5.replace(new RegExp('"model"\\s*:\\s*"' + from.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + '"'), () => `"model":${JSON.stringify(to2)}`);
|
|
146154
|
+
const headers = Object.fromEntries(Object.entries(request7.headers).filter(([name]) => name.toLowerCase() !== "content-length"));
|
|
146155
|
+
return { ...request7, headers, body: import_node_stream3.Readable.from([Buffer.from(rewritten, "utf8")]) };
|
|
146156
|
+
}
|
|
146157
|
+
|
|
145789
146158
|
// src/frontdoor/pipeline.ts
|
|
145790
146159
|
init_route();
|
|
145791
146160
|
function errorTypeFor(status) {
|
|
@@ -145865,7 +146234,7 @@ function createRoutedResponse(response, context) {
|
|
|
145865
146234
|
start: (status, headers) => {
|
|
145866
146235
|
for (const observe of deps.responseObservers) {
|
|
145867
146236
|
try {
|
|
145868
|
-
const body = observe({ session, route, method: request7.method, path: request7.path, servedPath: context.servedPath, receivedAt: request7.receivedAt, viaHeadroom: request7.viaHeadroom, lineage: request7.thread.lineage, modelChoice: request7.modelChoice.value, requestedModel: request7.requestedModel.value, clientSessionId: request7.clientSessionId, status, headers });
|
|
146237
|
+
const body = observe({ session, route, method: request7.method, path: request7.path, servedPath: context.servedPath, receivedAt: request7.receivedAt, viaHeadroom: request7.viaHeadroom, lineage: request7.thread.lineage, modelChoice: request7.modelChoice.value, requestedModel: request7.requestedModel.value, clientSessionId: request7.clientSessionId, requestClass: request7.requestClass, status, headers });
|
|
145869
146238
|
if (body !== void 0) {
|
|
145870
146239
|
bodyObservers.push(body);
|
|
145871
146240
|
}
|
|
@@ -145910,7 +146279,7 @@ function createRoutedResponse(response, context) {
|
|
|
145910
146279
|
}
|
|
145911
146280
|
async function serveRouted(incoming, deps) {
|
|
145912
146281
|
const request7 = { ...incoming, url: withCanonicalProviderTarget(incoming.url, deps.canonicalProvider) };
|
|
145913
|
-
const observed = { method: request7.method, path: pathOf(request7.url), receivedAt: deps.now(), viaHeadroom: false, thread: { lineage: void 0 }, modelChoice: { value: void 0 }, requestedModel: { value: void 0 }, clientSessionId: clientSessionIdOf(incoming.headers) };
|
|
146282
|
+
const observed = { method: request7.method, path: pathOf(request7.url), receivedAt: deps.now(), viaHeadroom: false, thread: { lineage: void 0 }, modelChoice: { value: void 0 }, requestedModel: { value: void 0 }, clientSessionId: clientSessionIdOf(incoming.headers), requestClass: requestClassOf(incoming.headers) };
|
|
145914
146283
|
const identity = identifyRequest(request7.headers);
|
|
145915
146284
|
const identified = { ...identity, session: { ...identity.session, provider: identity.session.provider === void 0 ? void 0 : deps.canonicalProvider(identity.session.provider) } };
|
|
145916
146285
|
const admission = deps.admit({ url: request7.url, headers: request7.headers, forwardable: identified.forwardableHeaders });
|
|
@@ -145927,7 +146296,11 @@ async function serveRouted(incoming, deps) {
|
|
|
145927
146296
|
headers: admission.headers,
|
|
145928
146297
|
body: request7.body,
|
|
145929
146298
|
signal: request7.signal,
|
|
145930
|
-
session:
|
|
146299
|
+
session: {
|
|
146300
|
+
...identified.session,
|
|
146301
|
+
...identified.session.identity === void 0 && admission.identity !== void 0 ? { identity: admission.identity } : {},
|
|
146302
|
+
...admission.launch === void 0 ? {} : { launch: admission.launch }
|
|
146303
|
+
}
|
|
145931
146304
|
};
|
|
145932
146305
|
const resolution = await deps.resolveRoute(routed);
|
|
145933
146306
|
if (!resolution.ok) {
|
|
@@ -145941,6 +146314,9 @@ async function serveRouted(incoming, deps) {
|
|
|
145941
146314
|
if (resolution.modelChoice !== void 0) {
|
|
145942
146315
|
observed.modelChoice.value = resolution.modelChoice;
|
|
145943
146316
|
}
|
|
146317
|
+
if (resolution.requestedModel !== void 0) {
|
|
146318
|
+
observed.requestedModel.value = resolution.requestedModel;
|
|
146319
|
+
}
|
|
145944
146320
|
const wantsHeadroom = headroom !== void 0 && await deps.headroomDecision.decide(identified.session.sessionId, identified.session.headroom, deps.now(), async () => await headroomReady(headroom, request7.signal));
|
|
145945
146321
|
const viaHeadroom = wantsHeadroom && resolution.route.headroomEligible && deps.headroom !== void 0;
|
|
145946
146322
|
const response = createRoutedResponse(request7.response, { deps, session: identified.session, route: resolution.route.name, request: { ...observed, viaHeadroom }, servedPath: pathOf(resolution.request?.url ?? request7.url) });
|
|
@@ -147651,88 +148027,7 @@ init_engine();
|
|
|
147651
148027
|
init_families();
|
|
147652
148028
|
init_identity2();
|
|
147653
148029
|
init_route();
|
|
147654
|
-
|
|
147655
|
-
|
|
147656
|
-
// src/frontdoor/requestScan.ts
|
|
147657
|
-
var import_node_stream2 = require("node:stream");
|
|
147658
|
-
var BYTES_PER_KIB2 = 1024;
|
|
147659
|
-
var SCAN_KIB = 64;
|
|
147660
|
-
var REQUEST_SCAN_CAP_BYTES = SCAN_KIB * BYTES_PER_KIB2;
|
|
147661
|
-
async function scanRequestHead(request7, wholeBody = false) {
|
|
147662
|
-
const body = request7.body;
|
|
147663
|
-
const head = [];
|
|
147664
|
-
let seen = 0;
|
|
147665
|
-
let text5 = "";
|
|
147666
|
-
let decided = false;
|
|
147667
|
-
let ended = false;
|
|
147668
|
-
let error63;
|
|
147669
|
-
const replay = new import_node_stream2.PassThrough();
|
|
147670
|
-
replay.on("error", () => void 0);
|
|
147671
|
-
const modelIn = () => /"model"\s*:\s*"([^"]+)"/.exec(text5)?.[1];
|
|
147672
|
-
const settled = new Promise((resolve3) => {
|
|
147673
|
-
const settle2 = () => {
|
|
147674
|
-
const model = modelIn();
|
|
147675
|
-
const hasImage = /"type"\s*:\s*"image"/.test(text5);
|
|
147676
|
-
for (const chunk2 of head) {
|
|
147677
|
-
replay.write(chunk2);
|
|
147678
|
-
}
|
|
147679
|
-
const maxTokens = /"max_tokens"\s*:\s*([0-9]+)/.exec(text5)?.[1];
|
|
147680
|
-
resolve3({
|
|
147681
|
-
...model === void 0 ? {} : { model },
|
|
147682
|
-
hasImage,
|
|
147683
|
-
toolsPresent: /"tools"\s*:\s*\[\s*\{/.test(text5),
|
|
147684
|
-
thinking: /"thinking"\s*:\s*\{\s*"type"\s*:\s*"enabled"/.test(text5),
|
|
147685
|
-
...maxTokens === void 0 ? {} : { maxTokens: Number(maxTokens) },
|
|
147686
|
-
isCountTokens: request7.url.includes("/v1/messages/count_tokens"),
|
|
147687
|
-
...wholeBody ? { whole: true, text: text5 } : {},
|
|
147688
|
-
replayed: { ...request7, body: replay }
|
|
147689
|
-
});
|
|
147690
|
-
};
|
|
147691
|
-
body.on("data", (chunk2) => {
|
|
147692
|
-
if (decided) {
|
|
147693
|
-
if (!replay.write(chunk2)) {
|
|
147694
|
-
body.pause();
|
|
147695
|
-
replay.once("drain", () => {
|
|
147696
|
-
body.resume();
|
|
147697
|
-
});
|
|
147698
|
-
}
|
|
147699
|
-
return;
|
|
147700
|
-
}
|
|
147701
|
-
head.push(chunk2);
|
|
147702
|
-
seen += chunk2.length;
|
|
147703
|
-
text5 += chunk2.toString("utf8");
|
|
147704
|
-
if (!wholeBody && (modelIn() !== void 0 || seen >= REQUEST_SCAN_CAP_BYTES)) {
|
|
147705
|
-
decided = true;
|
|
147706
|
-
settle2();
|
|
147707
|
-
}
|
|
147708
|
-
});
|
|
147709
|
-
body.on("end", () => {
|
|
147710
|
-
ended = true;
|
|
147711
|
-
if (!decided) {
|
|
147712
|
-
decided = true;
|
|
147713
|
-
settle2();
|
|
147714
|
-
}
|
|
147715
|
-
replay.end();
|
|
147716
|
-
});
|
|
147717
|
-
body.on("error", (cause) => {
|
|
147718
|
-
error63 = cause;
|
|
147719
|
-
if (!decided) {
|
|
147720
|
-
decided = true;
|
|
147721
|
-
settle2();
|
|
147722
|
-
}
|
|
147723
|
-
replay.destroy(cause);
|
|
147724
|
-
});
|
|
147725
|
-
body.on("close", () => {
|
|
147726
|
-
if (!decided && !ended && error63 === void 0) {
|
|
147727
|
-
decided = true;
|
|
147728
|
-
settle2();
|
|
147729
|
-
}
|
|
147730
|
-
});
|
|
147731
|
-
});
|
|
147732
|
-
return await settled;
|
|
147733
|
-
}
|
|
147734
|
-
|
|
147735
|
-
// src/frontdoor/providerRoute.ts
|
|
148030
|
+
init_tiers();
|
|
147736
148031
|
var COULD_NOT_CONNECT_LOG_INTERVAL_MS = 6e4;
|
|
147737
148032
|
var isRcSelfHostPath = (path101) => path101 === "/v1/code/sessions" || path101.startsWith("/v1/code/sessions/") || path101 === "/v1/sessions" || path101.startsWith("/v1/sessions/");
|
|
147738
148033
|
function scopeSessionProviderPath(request7) {
|
|
@@ -147996,7 +148291,7 @@ function createProviderRouteResolver(deps) {
|
|
|
147996
148291
|
await route.serve({ ...request7, url: rewritten.url, headers: rewritten.headers, body: rewritten.body }, response);
|
|
147997
148292
|
}
|
|
147998
148293
|
});
|
|
147999
|
-
|
|
148294
|
+
const resolveByName = async (request7, noteRequestedModel) => {
|
|
148000
148295
|
const scopedRequest = scopeSessionProviderPath(request7);
|
|
148001
148296
|
const serveScoped = (resolution) => scopedRequest === request7 ? resolution : { ok: true, route: servingRewritten(resolution.route, scopedRequest) };
|
|
148002
148297
|
let current = scopedRequest;
|
|
@@ -148024,6 +148319,7 @@ function createProviderRouteResolver(deps) {
|
|
|
148024
148319
|
const resolvesNames = !nameResolved && !isRcSelfHostPath(path101);
|
|
148025
148320
|
if (resolvesNames) {
|
|
148026
148321
|
const scan2 = await scanRequestHead(current, true);
|
|
148322
|
+
noteRequestedModel(scan2.model);
|
|
148027
148323
|
if (scan2.replayed === void 0) {
|
|
148028
148324
|
const resolution = resolve3(current);
|
|
148029
148325
|
return resolution.ok && replay !== void 0 ? { ok: true, route: servingRewritten(resolution.route, { ...replay, url: current.url }), request: { ...replay, url: current.url } } : resolution.ok ? serveScoped(resolution) : resolution;
|
|
@@ -148054,9 +148350,7 @@ function createProviderRouteResolver(deps) {
|
|
|
148054
148350
|
return resolution2.ok ? { ok: true, route: servingRewritten(resolution2.route, { ...replay, url: current.url }), request: { ...replay, url: current.url }, ...modelChoice === void 0 ? {} : { modelChoice } } : resolution2;
|
|
148055
148351
|
}
|
|
148056
148352
|
if (requested !== void 0 && routed.model !== requested) {
|
|
148057
|
-
|
|
148058
|
-
const headers = Object.fromEntries(Object.entries(replay.headers).filter(([name]) => name.toLowerCase() !== "content-length"));
|
|
148059
|
-
replay = { ...replay, headers, body: import_node_stream3.Readable.from([Buffer.from(rewritten, "utf8")]) };
|
|
148353
|
+
replay = withModel(replay, scan2.text ?? "", requested, routed.model);
|
|
148060
148354
|
}
|
|
148061
148355
|
if (routed.provider !== providerName) {
|
|
148062
148356
|
const scopedPrefix = providerName === void 0 ? void 0 : `${PROVIDER_PATH_PREFIX}${encodeURIComponent(providerName)}`;
|
|
@@ -148083,6 +148377,7 @@ function createProviderRouteResolver(deps) {
|
|
|
148083
148377
|
})
|
|
148084
148378
|
);
|
|
148085
148379
|
const scan = await scanRequestHead(current, whole);
|
|
148380
|
+
noteRequestedModel(scan.model);
|
|
148086
148381
|
if (scan.replayed === void 0) {
|
|
148087
148382
|
const resolution = resolve3(current);
|
|
148088
148383
|
return resolution.ok && replay !== void 0 ? { ok: true, route: servingRewritten(resolution.route, { ...replay, url: current.url }), request: { ...replay, url: current.url } } : resolution.ok ? serveScoped(resolution) : resolution;
|
|
@@ -148110,15 +148405,48 @@ function createProviderRouteResolver(deps) {
|
|
|
148110
148405
|
return resolution.ok ? { ok: true, route: servingRewritten(resolution.route, { ...replay, url: current.url }), request: { ...replay, url: current.url }, ...modelChoice === void 0 ? {} : { modelChoice } } : resolution;
|
|
148111
148406
|
}
|
|
148112
148407
|
if (matched.model !== void 0 && scan.text !== void 0 && scan.model !== void 0) {
|
|
148113
|
-
|
|
148114
|
-
const headers = Object.fromEntries(Object.entries(replay.headers).filter(([name]) => name.toLowerCase() !== "content-length"));
|
|
148115
|
-
replay = { ...replay, headers, body: import_node_stream3.Readable.from([Buffer.from(rewritten, "utf8")]) };
|
|
148408
|
+
replay = withModel(replay, scan.text, scan.model, matched.model);
|
|
148116
148409
|
}
|
|
148117
148410
|
const rest = scoped === void 0 ? path101 : path101.slice(`${PROVIDER_PATH_PREFIX}${encodeURIComponent(scoped.provider)}`.length);
|
|
148118
148411
|
const query = current.url.includes("?") ? current.url.slice(current.url.indexOf("?")) : "";
|
|
148119
148412
|
current = { ...current, url: `${PROVIDER_PATH_PREFIX}${encodeURIComponent(matched.provider)}${rest}${query}`, body: replay.body, headers: replay.headers };
|
|
148120
148413
|
}
|
|
148121
148414
|
};
|
|
148415
|
+
const helperModel = (provider, session, requested) => {
|
|
148416
|
+
const asIs = { kind: "as-is" };
|
|
148417
|
+
const tier = requested.includes("/") ? void 0 : claudeTierOf(requested);
|
|
148418
|
+
if (tier === void 0) {
|
|
148419
|
+
return asIs;
|
|
148420
|
+
}
|
|
148421
|
+
let definition;
|
|
148422
|
+
try {
|
|
148423
|
+
definition = loadProvider(deps.providersDir, provider, deps.fs);
|
|
148424
|
+
} catch {
|
|
148425
|
+
return asIs;
|
|
148426
|
+
}
|
|
148427
|
+
if (definition === void 0) {
|
|
148428
|
+
return asIs;
|
|
148429
|
+
}
|
|
148430
|
+
if (isHttpProvider(definition)) {
|
|
148431
|
+
const model = definition.env?.[TIER_ENV_VARIABLES[tier]];
|
|
148432
|
+
return model === void 0 || model === "" ? asIs : { kind: "mapped", model: withoutContextMarker(model) };
|
|
148433
|
+
}
|
|
148434
|
+
if (!isRouterProvider(definition)) {
|
|
148435
|
+
return asIs;
|
|
148436
|
+
}
|
|
148437
|
+
const pointer = deps.models?.(session)?.tiers?.[tier];
|
|
148438
|
+
return pointer === void 0 ? { kind: "refused", message: `provider "${provider}" routes by model name and the models block has no ${tier} tier pointer, so this ${tier}-tier request of the session cannot be routed; set models.tiers.${tier}` } : { kind: "mapped", model: pointer };
|
|
148439
|
+
};
|
|
148440
|
+
return followingMainLoop(
|
|
148441
|
+
async (request7) => {
|
|
148442
|
+
let requestedModel;
|
|
148443
|
+
const resolution = await resolveByName(request7, (model) => {
|
|
148444
|
+
requestedModel ??= model;
|
|
148445
|
+
});
|
|
148446
|
+
return resolution.ok && requestedModel !== void 0 ? { ...resolution, requestedModel } : resolution;
|
|
148447
|
+
},
|
|
148448
|
+
{ record: deps.mainLoopRoutes, helperModel }
|
|
148449
|
+
);
|
|
148122
148450
|
}
|
|
148123
148451
|
function providerCredentialHeaders(credential) {
|
|
148124
148452
|
return credential.target === "apiKey" ? { "x-api-key": credential.token } : { authorization: `Bearer ${credential.token}` };
|
|
@@ -152399,7 +152727,8 @@ function realFrontDoorSupervisorPorts(paths, respawnGeneration, lifecycle, track
|
|
|
152399
152727
|
accountMoves: async (sessionId, to2, model) => await requestAccountMove({ fs: realFarmFs, paths, runningAmong: realRunningAmong, idOf: (name) => identityIdOf(paths, name), now: () => Date.now() }, sessionId, to2, model),
|
|
152400
152728
|
...rcSelfHostSurface === void 0 ? {} : { rcSelfHostRoute: rcSelfHostSurface.route },
|
|
152401
152729
|
log,
|
|
152402
|
-
now: () => Date.now()
|
|
152730
|
+
now: () => Date.now(),
|
|
152731
|
+
mainLoopRoutes: createMainLoopRouteRecord({ holdsLaunch: sessionRegistry.holdsLaunch, stored: launchRouteStore(realFarmFs, paths.frontdoorSessionsDir, log), log })
|
|
152403
152732
|
}),
|
|
152404
152733
|
rcTracker,
|
|
152405
152734
|
rcHub
|
|
@@ -152554,6 +152883,7 @@ function realFrontDoorSupervisorPorts(paths, respawnGeneration, lifecycle, track
|
|
|
152554
152883
|
resolveRoute,
|
|
152555
152884
|
isLiveToken,
|
|
152556
152885
|
identityOfToken: sessionRegistry.identityOf,
|
|
152886
|
+
launchOfToken: sessionRegistry.launchOfToken,
|
|
152557
152887
|
canonicalProvider,
|
|
152558
152888
|
responseObservers: [usageMiddleware],
|
|
152559
152889
|
now: () => Date.now(),
|
|
@@ -153042,6 +153372,9 @@ var realHeadroomGet = async (target, requestPath, timeoutMs) => {
|
|
|
153042
153372
|
|
|
153043
153373
|
// src/statusline/enrich.ts
|
|
153044
153374
|
init_quota();
|
|
153375
|
+
function lastMainLoopRecord(records, sessionId) {
|
|
153376
|
+
return records.filter((record3) => record3.sessionId === sessionId && (record3.requestClass === void 0 || record3.requestClass === "main")).at(-1);
|
|
153377
|
+
}
|
|
153045
153378
|
var FULL_PERCENT2 = 100;
|
|
153046
153379
|
var MS_PER_SECOND18 = 1e3;
|
|
153047
153380
|
function identityOf(env) {
|
|
@@ -157617,7 +157950,7 @@ function registerStatuslineCommand(program2, deps) {
|
|
|
157617
157950
|
let modelChoice;
|
|
157618
157951
|
if (sessionHeader !== void 0) {
|
|
157619
157952
|
try {
|
|
157620
|
-
const last = readUsageLog(realFarmFs, deps.paths.usageLogDir, { sinceMs: nowMs - ROUTED_TARGET_LOOKBACK_MS }).records
|
|
157953
|
+
const last = lastMainLoopRecord(readUsageLog(realFarmFs, deps.paths.usageLogDir, { sinceMs: nowMs - ROUTED_TARGET_LOOKBACK_MS }).records, sessionHeader);
|
|
157621
157954
|
if (last?.model !== void 0) {
|
|
157622
157955
|
if (last.provider !== providerName) {
|
|
157623
157956
|
routedTarget = `${last.provider}/${last.model}`;
|