claude-use 14.1.0 → 14.2.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 CHANGED
@@ -31586,8 +31586,15 @@ function recordOf(details) {
31586
31586
  }
31587
31587
  function launchRouteStore(fs38, sessionsDir, log) {
31588
31588
  const failing = /* @__PURE__ */ new Set();
31589
+ const badFacts = /* @__PURE__ */ new Set();
31589
31590
  return {
31590
- read: (launch) => readLaunchRoute(fs38, sessionsDir, launch),
31591
+ read: (launch) => readLaunchRoute(fs38, sessionsDir, launch, (message) => {
31592
+ const key = launchKeyOf(launch);
31593
+ if (!badFacts.has(key)) {
31594
+ badFacts.add(key);
31595
+ log(`front door: could not read the facts recorded for launch ${String(launch.pid)}'s main loop (${message}), so its route is used without them`);
31596
+ }
31597
+ }),
31591
31598
  write: (launch, route) => {
31592
31599
  writeLaunchRoute(fs38, sessionsDir, launch, route);
31593
31600
  },
@@ -31595,9 +31602,11 @@ function launchRouteStore(fs38, sessionsDir, log) {
31595
31602
  identity: (launch) => {
31596
31603
  const key = launchKeyOf(launch);
31597
31604
  try {
31598
- const stat2 = fs38.lstat(import_node_path7.default.join(sessionsDir, launchRouteName(launch.pid)));
31605
+ const route = fs38.lstat(import_node_path7.default.join(sessionsDir, launchRouteName(launch.pid)));
31606
+ const facts = fs38.lstat(import_node_path7.default.join(sessionsDir, launchFactsName(launch.pid)));
31599
31607
  failing.delete(key);
31600
- return stat2 === void 0 ? void 0 : `${String(stat2.mtimeMs)}:${String(stat2.sizeBytes)}`;
31608
+ const of2 = (stat2) => stat2 === void 0 ? "-" : `${String(stat2.mtimeMs)}:${String(stat2.sizeBytes)}`;
31609
+ return route === void 0 ? void 0 : `${of2(route)}|${of2(facts)}`;
31601
31610
  } catch (error63) {
31602
31611
  if (!failing.has(key)) {
31603
31612
  failing.add(key);
@@ -31611,6 +31620,7 @@ function launchRouteStore(fs38, sessionsDir, log) {
31611
31620
  function writeFrontDoorSession(fs38, sessionsDir, session) {
31612
31621
  fs38.mkdirPrivate(sessionsDir);
31613
31622
  fs38.removeRecursive(import_node_path7.default.join(sessionsDir, launchRouteName(session.pid)));
31623
+ fs38.removeRecursive(import_node_path7.default.join(sessionsDir, launchFactsName(session.pid)));
31614
31624
  fs38.writeFilePrivate(import_node_path7.default.join(sessionsDir, sessionRecordName(session.pid)), `${JSON.stringify(session, null, 2)}
31615
31625
  `);
31616
31626
  }
@@ -31623,15 +31633,31 @@ function isSessionRecordName(name) {
31623
31633
  function launchRouteName(pid) {
31624
31634
  return `${String(pid)}.route.json`;
31625
31635
  }
31626
- function writeLaunchRoute(fs38, sessionsDir, launch, route) {
31636
+ function launchFactsName(pid) {
31637
+ return `${String(pid)}.context.json`;
31638
+ }
31639
+ function writeLaunchRoute(fs38, sessionsDir, launch, context) {
31640
+ const { facts, route } = splitContext(context);
31627
31641
  if (readFrontDoorSessionRecord(fs38, sessionsDir, sessionRecordName(launch.pid))?.startedAt !== launch.startedAt) {
31628
31642
  return;
31629
31643
  }
31630
31644
  fs38.mkdirPrivate(sessionsDir);
31645
+ const factsFile = import_node_path7.default.join(sessionsDir, launchFactsName(launch.pid));
31646
+ if (Object.keys(facts).length === 0) {
31647
+ fs38.removeRecursive(factsFile);
31648
+ } else {
31649
+ fs38.writeFilePrivate(factsFile, `${JSON.stringify({ version: LAUNCH_FACTS_FILE_VERSION, startedAt: launch.startedAt, facts })}
31650
+ `);
31651
+ }
31631
31652
  fs38.writeFilePrivate(import_node_path7.default.join(sessionsDir, launchRouteName(launch.pid)), `${JSON.stringify({ version: LAUNCH_ROUTE_FILE_VERSION, startedAt: launch.startedAt, route })}
31632
31653
  `);
31633
31654
  }
31634
- function readLaunchRoute(fs38, sessionsDir, launch) {
31655
+ function splitContext(context) {
31656
+ const facts = ContextFactsSchema.parse({ ...context.credential === void 0 ? {} : { credential: context.credential }, ...context.requestedModel === void 0 ? {} : { requestedModel: context.requestedModel }, ...context.servedModel === void 0 ? {} : { servedModel: context.servedModel } });
31657
+ const route = LaunchRouteSchema.parse(context.kind === "provider" ? { kind: "provider", provider: context.provider } : { kind: "anthropic" });
31658
+ return { route, facts };
31659
+ }
31660
+ function readLaunchRoute(fs38, sessionsDir, launch, factsProblem) {
31635
31661
  const name = launchRouteName(launch.pid);
31636
31662
  const text5 = fs38.readFileUtf8(import_node_path7.default.join(sessionsDir, name));
31637
31663
  if (text5 === void 0) {
@@ -31651,7 +31677,30 @@ function readLaunchRoute(fs38, sessionsDir, launch) {
31651
31677
  if (!parsed.success) {
31652
31678
  throw new Error(`${name} is not a route file this door reads`);
31653
31679
  }
31654
- return parsed.data.startedAt === launch.startedAt ? parsed.data.route : void 0;
31680
+ if (parsed.data.startedAt !== launch.startedAt) {
31681
+ return void 0;
31682
+ }
31683
+ return { ...parsed.data.route, ...readLaunchFacts(fs38, sessionsDir, launch, factsProblem) };
31684
+ }
31685
+ function readLaunchFacts(fs38, sessionsDir, launch, factsProblem) {
31686
+ const name = launchFactsName(launch.pid);
31687
+ const text5 = fs38.readFileUtf8(import_node_path7.default.join(sessionsDir, name));
31688
+ if (text5 === void 0) {
31689
+ return {};
31690
+ }
31691
+ let json2;
31692
+ try {
31693
+ json2 = JSON.parse(text5);
31694
+ } catch {
31695
+ factsProblem(`${name} is not JSON`);
31696
+ return {};
31697
+ }
31698
+ const parsed = LaunchFactsFileSchema.safeParse(json2);
31699
+ if (!parsed.success) {
31700
+ factsProblem(`${name} is not a facts file this door reads`);
31701
+ return {};
31702
+ }
31703
+ return parsed.data.startedAt === launch.startedAt ? parsed.data.facts : {};
31655
31704
  }
31656
31705
  function summaryOf(session) {
31657
31706
  return { pid: session.pid, startedAt: session.startedAt, ...session.supervisorPid === void 0 ? {} : { supervisorPid: session.supervisorPid } };
@@ -31675,12 +31724,13 @@ function listFrontDoorSessions(fs38, sessionsDir) {
31675
31724
  return sessions.sort((a10, b) => a10.pid - b.pid);
31676
31725
  }
31677
31726
  function routeFilePid(name) {
31678
- const match = /^([1-9]\d*)\.route\.json$/.exec(name);
31727
+ const match = /^([1-9]\d*)\.(?:route|context)\.json$/.exec(name);
31679
31728
  return match?.[1] === void 0 ? void 0 : Number(match[1]);
31680
31729
  }
31681
31730
  function removeFrontDoorSession(fs38, sessionsDir, pid) {
31682
31731
  fs38.removeRecursive(import_node_path7.default.join(sessionsDir, sessionRecordName(pid)));
31683
31732
  fs38.removeRecursive(import_node_path7.default.join(sessionsDir, launchRouteName(pid)));
31733
+ fs38.removeRecursive(import_node_path7.default.join(sessionsDir, launchFactsName(pid)));
31684
31734
  }
31685
31735
  function pruneDeadFrontDoorSessions(fs38, sessionsDir, listed, isRunning) {
31686
31736
  const removed = [];
@@ -31699,7 +31749,7 @@ function pruneDeadFrontDoorSessions(fs38, sessionsDir, listed, isRunning) {
31699
31749
  }
31700
31750
  return removed;
31701
31751
  }
31702
- var import_node_path7, FRONT_DOOR_PROTOCOL, FRONT_DOOR_HANDOVER_PROTOCOL, FrontDoorSessionSchema, MainLoopRouteSchema, FrontDoorStateSchema, FrontDoorDirectRecordSchema, RECORD_TEMPORARY_FILE, LAUNCH_ROUTE_FILE_VERSION, LaunchRouteFileSchema;
31752
+ var import_node_path7, FRONT_DOOR_PROTOCOL, FRONT_DOOR_HANDOVER_PROTOCOL, FrontDoorSessionSchema, MAX_CONTEXT_TEXT_LENGTH, ContextText, LaunchRouteSchema, ContextFactsSchema, FrontDoorStateSchema, FrontDoorDirectRecordSchema, RECORD_TEMPORARY_FILE, LAUNCH_ROUTE_FILE_VERSION, LAUNCH_FACTS_FILE_VERSION, LaunchRouteFileSchema, LaunchFactsFileSchema;
31703
31753
  var init_state = __esm({
31704
31754
  "src/frontdoor/state.ts"() {
31705
31755
  "use strict";
@@ -31711,7 +31761,10 @@ var init_state = __esm({
31711
31761
  FRONT_DOOR_PROTOCOL = 8;
31712
31762
  FRONT_DOOR_HANDOVER_PROTOCOL = 6;
31713
31763
  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) })]);
31764
+ MAX_CONTEXT_TEXT_LENGTH = 256;
31765
+ ContextText = external_exports.string().min(1).max(MAX_CONTEXT_TEXT_LENGTH);
31766
+ LaunchRouteSchema = 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) })]);
31767
+ ContextFactsSchema = external_exports.strictObject({ credential: ContextText.optional(), requestedModel: ContextText.optional(), servedModel: ContextText.optional() });
31715
31768
  FrontDoorStateSchema = external_exports.strictObject({
31716
31769
  /**
31717
31770
  * 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`).
@@ -31745,7 +31798,9 @@ var init_state = __esm({
31745
31798
  FrontDoorDirectRecordSchema = external_exports.strictObject({ directPort: external_exports.number().int().positive().max(MAX_TCP_PORT) });
31746
31799
  RECORD_TEMPORARY_FILE = /^[1-9]\d*\.json\.([1-9]\d*)\.tmp$/;
31747
31800
  LAUNCH_ROUTE_FILE_VERSION = 1;
31748
- LaunchRouteFileSchema = external_exports.strictObject({ version: external_exports.literal(LAUNCH_ROUTE_FILE_VERSION), startedAt: external_exports.number(), route: MainLoopRouteSchema });
31801
+ LAUNCH_FACTS_FILE_VERSION = 1;
31802
+ LaunchRouteFileSchema = external_exports.strictObject({ version: external_exports.literal(LAUNCH_ROUTE_FILE_VERSION), startedAt: external_exports.number(), route: LaunchRouteSchema });
31803
+ LaunchFactsFileSchema = external_exports.strictObject({ version: external_exports.literal(LAUNCH_FACTS_FILE_VERSION), startedAt: external_exports.number(), facts: ContextFactsSchema });
31749
31804
  }
31750
31805
  });
31751
31806
 
@@ -46692,6 +46747,11 @@ function claudeTierOf(model) {
46692
46747
  const lower = model.toLowerCase();
46693
46748
  return CLAUDE_TIERS.find((tier) => lower.includes(tier));
46694
46749
  }
46750
+ function claudeNameTier(model) {
46751
+ const name = model.toLowerCase().replace(/\[1m\]$/, "");
46752
+ const parts = name.split("-");
46753
+ return parts[0] === "claude" ? CLAUDE_TIERS.find((tier) => parts.includes(tier)) : CLAUDE_TIERS.find((tier) => name === tier);
46754
+ }
46695
46755
  var TIER_ENV_VARIABLES;
46696
46756
  var init_tiers = __esm({
46697
46757
  "src/models/tiers.ts"() {
@@ -74540,7 +74600,7 @@ var init_package = __esm({
74540
74600
  "package.json"() {
74541
74601
  package_default = {
74542
74602
  name: "claude-use",
74543
- version: "14.1.0",
74603
+ version: "14.2.0",
74544
74604
  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.",
74545
74605
  license: "MIT",
74546
74606
  author: "Joseph Mearman <joseph@mearman.co.uk>",
@@ -145896,6 +145956,67 @@ init_http();
145896
145956
  init_schema2();
145897
145957
  init_route();
145898
145958
 
145959
+ // src/frontdoor/topLevelModel.ts
145960
+ var WHITESPACE = /* @__PURE__ */ new Set([" ", " ", "\n", "\r"]);
145961
+ function endOfString(text5, from) {
145962
+ for (let at2 = from + 1; at2 < text5.length; at2 += 1) {
145963
+ if (text5[at2] === "\\") {
145964
+ at2 += 1;
145965
+ } else if (text5[at2] === '"') {
145966
+ return at2 + 1;
145967
+ }
145968
+ }
145969
+ return void 0;
145970
+ }
145971
+ function skipWhitespace(text5, from) {
145972
+ let at2 = from;
145973
+ while (at2 < text5.length && WHITESPACE.has(text5[at2] ?? "")) {
145974
+ at2 += 1;
145975
+ }
145976
+ return at2;
145977
+ }
145978
+ function topLevelModel(text5) {
145979
+ let at2 = skipWhitespace(text5, 0);
145980
+ if (text5[at2] !== "{") {
145981
+ return void 0;
145982
+ }
145983
+ let depth = 0;
145984
+ while (at2 < text5.length) {
145985
+ const character = text5[at2];
145986
+ if (character === '"') {
145987
+ const end = endOfString(text5, at2);
145988
+ if (end === void 0) {
145989
+ return void 0;
145990
+ }
145991
+ const afterKey = skipWhitespace(text5, end);
145992
+ if (depth === 1 && text5[afterKey] === ":" && text5.slice(at2, end) === '"model"') {
145993
+ const valueStart = skipWhitespace(text5, afterKey + 1);
145994
+ if (text5[valueStart] === '"') {
145995
+ const valueEnd = endOfString(text5, valueStart);
145996
+ if (valueEnd === void 0) {
145997
+ return void 0;
145998
+ }
145999
+ const value3 = text5.slice(valueStart + 1, valueEnd - 1);
146000
+ return value3 === "" ? void 0 : { value: value3, start: valueStart, end: valueEnd };
146001
+ }
146002
+ return void 0;
146003
+ }
146004
+ at2 = end;
146005
+ continue;
146006
+ }
146007
+ if (character === "{" || character === "[") {
146008
+ depth += 1;
146009
+ } else if (character === "}" || character === "]") {
146010
+ depth -= 1;
146011
+ if (depth === 0) {
146012
+ return void 0;
146013
+ }
146014
+ }
146015
+ at2 += 1;
146016
+ }
146017
+ return void 0;
146018
+ }
146019
+
145899
146020
  // src/frontdoor/requestScan.ts
145900
146021
  var import_node_stream2 = require("node:stream");
145901
146022
  var import_node_string_decoder = require("node:string_decoder");
@@ -145916,7 +146037,7 @@ async function scanRequestHead(request7, wholeBody = false) {
145916
146037
  let error63;
145917
146038
  const replay = new import_node_stream2.PassThrough();
145918
146039
  replay.on("error", () => void 0);
145919
- const modelIn = () => /"model"\s*:\s*"((?:[^"\\]|\\.)+)"/.exec(text5)?.[1];
146040
+ const modelIn = () => topLevelModel(text5)?.value;
145920
146041
  const settled = new Promise((resolve3) => {
145921
146042
  const settle2 = () => {
145922
146043
  const model = modelIn();
@@ -145983,6 +146104,7 @@ async function scanRequestHead(request7, wholeBody = false) {
145983
146104
  }
145984
146105
 
145985
146106
  // src/frontdoor/mainLoopRoute.ts
146107
+ init_tiers();
145986
146108
  init_state();
145987
146109
  var REQUEST_CLASS_HEADER = "x-claude-code-request-class";
145988
146110
  var AGENT_ID_HEADER = "x-claude-code-agent-id";
@@ -145998,8 +146120,8 @@ function requestClassOf(headers) {
145998
146120
  const stated = parsed.success ? parsed.data : "unknown";
145999
146121
  return hasAgentId && stated === MAIN_REQUEST_CLASS ? "subagent" : stated;
146000
146122
  }
146001
- function sameRoute(a10, b) {
146002
- return a10.kind === b.kind && (a10.kind !== "provider" || b.kind === "provider" && a10.provider === b.provider);
146123
+ function sameContext(a10, b) {
146124
+ return a10.kind === b.kind && (a10.kind !== "provider" || b.kind === "provider" && a10.provider === b.provider) && a10.credential === b.credential && a10.requestedModel === b.requestedModel && a10.servedModel === b.servedModel;
146003
146125
  }
146004
146126
  function createMainLoopRouteRecord(ports) {
146005
146127
  const routes = /* @__PURE__ */ new Map();
@@ -146013,7 +146135,7 @@ function createMainLoopRouteRecord(ports) {
146013
146135
  }
146014
146136
  };
146015
146137
  const persist = (launch, key, route, persisted) => {
146016
- if (persisted !== void 0 && sameRoute(persisted, route)) {
146138
+ if (persisted !== void 0 && sameContext(persisted, route)) {
146017
146139
  return persisted;
146018
146140
  }
146019
146141
  try {
@@ -146044,7 +146166,7 @@ function createMainLoopRouteRecord(ports) {
146044
146166
  dropEnded(key);
146045
146167
  const before = routes.get(key);
146046
146168
  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));
146169
+ const written = persisted !== void 0 && sameContext(persisted, route) && (before?.persisted === void 0 || !sameContext(before.persisted, route));
146048
146170
  routes.set(key, { launch, route, persisted, identity: written ? ports.stored.identity(launch) : before?.identity });
146049
146171
  },
146050
146172
  routeOf: (launch) => {
@@ -146056,9 +146178,9 @@ function createMainLoopRouteRecord(ports) {
146056
146178
  routes.set(key, loaded);
146057
146179
  return loaded.route;
146058
146180
  }
146059
- if (entry.route !== void 0 && (entry.persisted === void 0 || !sameRoute(entry.persisted, entry.route))) {
146181
+ if (entry.route !== void 0 && (entry.persisted === void 0 || !sameContext(entry.persisted, entry.route))) {
146060
146182
  const persisted = persist(launch, key, entry.route, entry.persisted);
146061
- const written = persisted !== void 0 && sameRoute(persisted, entry.route);
146183
+ const written = persisted !== void 0 && sameContext(persisted, entry.route);
146062
146184
  routes.set(key, { ...entry, persisted, identity: written ? ports.stored.identity(launch) : entry.identity });
146063
146185
  return entry.route;
146064
146186
  }
@@ -146078,7 +146200,45 @@ function isInferencePath(path101) {
146078
146200
  function servedProviderOf(url2) {
146079
146201
  return parseProviderPath(new URL(url2, "http://127.0.0.1").pathname)?.provider;
146080
146202
  }
146203
+ var ANTHROPIC_PREFIX = "anthropic/";
146204
+ function isBareClaudeName(model) {
146205
+ return !model.includes("/") && claudeNameTier(model) !== void 0;
146206
+ }
146207
+ function boundedName(text5) {
146208
+ return text5 === void 0 || text5 === "" || text5.length > MAX_CONTEXT_TEXT_LENGTH ? void 0 : text5;
146209
+ }
146081
146210
  function followingMainLoop(resolve3, deps) {
146211
+ const contextOf = (route, credentialName, requested, served) => {
146212
+ const credential = boundedName(credentialName);
146213
+ const facts = { ...credential === void 0 ? {} : { credential }, ...boundedName(requested) === void 0 ? {} : { requestedModel: boundedName(requested) }, ...boundedName(served) === void 0 ? {} : { servedModel: boundedName(served) } };
146214
+ return { ...route, ...facts };
146215
+ };
146216
+ const toAnthropic = async (base, requested) => {
146217
+ const served = requested.slice(ANTHROPIC_PREFIX.length);
146218
+ const scan = await scanRequestHead(base);
146219
+ const rewritten = withModelInHead(scan.replayed ?? base, scan.headBytes, requested, served);
146220
+ const resolution = await resolve3({ ...rewritten, url: base.url });
146221
+ return { resolution: resolution.ok ? { ...resolution, requestedModel: requested } : resolution, served };
146222
+ };
146223
+ const toProvider = async (base, provider, session) => {
146224
+ const scan = base.body.readableEnded ? { model: void 0, headBytes: 0, replayed: void 0 } : await scanRequestHead(base);
146225
+ const requested = scan.model;
146226
+ const { model: choice, credential } = deps.providerMapping(provider, session, requested ?? "");
146227
+ if (choice.kind === "refused") {
146228
+ return { resolution: { ok: false, status: HTTP_STATUS.badRequest, message: choice.message }, requested, served: void 0, credential };
146229
+ }
146230
+ const replayed = scan.replayed ?? base;
146231
+ const body = choice.kind === "as-is" || requested === void 0 ? replayed : withModelInHead(replayed, scan.headBytes, requested, choice.model);
146232
+ const scoped = { ...body, url: `${PROVIDER_PATH_PREFIX}${encodeURIComponent(provider)}${base.url}` };
146233
+ const resolution = await resolve3(scoped);
146234
+ if (!resolution.ok) {
146235
+ return { resolution, requested, served: void 0, credential };
146236
+ }
146237
+ if (resolution.request === void 0) {
146238
+ return { resolution: { ok: false, status: HTTP_STATUS.internalServerError, message: "the provider resolver served a request without saying which request it served" }, requested, served: void 0, credential };
146239
+ }
146240
+ return { resolution: { ...resolution, request: resolution.request, ...requested === void 0 ? {} : { requestedModel: requested } }, requested, served: choice.kind === "mapped" ? choice.model : requested, credential };
146241
+ };
146082
146242
  return async (request7) => {
146083
146243
  const { launch, provider } = request7.session;
146084
146244
  const path101 = new URL(request7.url, "http://127.0.0.1").pathname;
@@ -146086,43 +146246,58 @@ function followingMainLoop(resolve3, deps) {
146086
146246
  return await resolve3(request7);
146087
146247
  }
146088
146248
  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 });
146249
+ const current = deps.record.routeOf(launch);
146250
+ if (requestClass !== void 0 && requestClass !== MAIN_REQUEST_CLASS) {
146251
+ const scan = await scanRequestHead(request7);
146252
+ const helper = scan.replayed ?? request7;
146253
+ if (scan.model?.startsWith(ANTHROPIC_PREFIX) === true && !deps.definesProvider("anthropic")) {
146254
+ return (await toAnthropic(helper, scan.model)).resolution;
146094
146255
  }
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 };
146256
+ if (current === void 0 || current.kind === "anthropic") {
146257
+ return await resolve3(helper);
146258
+ }
146259
+ return (await toProvider(helper, current.provider, request7.session)).resolution;
146106
146260
  }
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);
146261
+ const resolution = await resolve3(request7);
146111
146262
  if (!resolution.ok) {
146112
146263
  return resolution;
146113
146264
  }
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" };
146265
+ const requested = resolution.requestedModel;
146266
+ const served = servedProviderOf(resolution.request?.url ?? request7.url);
146267
+ if (served === void 0) {
146268
+ const base = resolution.request ?? request7;
146269
+ if (requested?.startsWith(ANTHROPIC_PREFIX) === true && !deps.definesProvider("anthropic")) {
146270
+ const placed = await toAnthropic(base, requested);
146271
+ if (placed.resolution.ok) {
146272
+ deps.record.record(launch, contextOf({ kind: "anthropic" }, deps.loginOf(request7.session), requested, placed.served));
146273
+ }
146274
+ return placed.resolution;
146275
+ }
146276
+ if (current?.kind === "provider") {
146277
+ const placed = await toProvider(base, current.provider, request7.session);
146278
+ if (placed.resolution.ok && requested !== void 0 && isBareClaudeName(requested)) {
146279
+ deps.record.record(launch, contextOf({ kind: "provider", provider: current.provider }, placed.credential, requested, placed.served));
146280
+ }
146281
+ return placed.resolution;
146282
+ }
146116
146283
  }
146117
- return { ...resolution, request: resolution.request, ...requested === void 0 ? {} : { requestedModel: requested } };
146284
+ const route = served === void 0 ? { kind: "anthropic" } : { kind: "provider", provider: served };
146285
+ const qualified = served !== void 0 && requested?.startsWith(`${served}/`) === true ? requested.slice(served.length + 1) : void 0;
146286
+ const servedModel = qualified ?? (served === void 0 ? requested : void 0);
146287
+ const credential = served === void 0 ? deps.loginOf(request7.session) : deps.providerMapping(served, request7.session, "").credential;
146288
+ deps.record.record(launch, contextOf(route, credential, requested, servedModel));
146289
+ return resolution;
146118
146290
  };
146119
146291
  }
146120
146292
  function asBytes(value3) {
146121
146293
  return Buffer.from(value3, "utf8").toString("latin1");
146122
146294
  }
146123
146295
  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");
146296
+ const rewriteHead = (head) => {
146297
+ const text5 = head.toString("latin1");
146298
+ const found = topLevelModel(text5);
146299
+ return found?.value === asBytes(from) ? Buffer.from(`${text5.slice(0, found.start)}${asBytes(JSON.stringify(to2))}${text5.slice(found.end)}`, "latin1") : head;
146300
+ };
146126
146301
  async function* rewritten() {
146127
146302
  let held = Buffer.alloc(0);
146128
146303
  let headDone = false;
@@ -146150,7 +146325,8 @@ function withModelInHead(request7, headBytes, from, to2) {
146150
146325
  return { ...request7, headers, body: import_node_stream3.Readable.from(rewritten()) };
146151
146326
  }
146152
146327
  function withModel(request7, text5, from, to2) {
146153
- const rewritten = text5.replace(new RegExp('"model"\\s*:\\s*"' + from.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + '"'), () => `"model":${JSON.stringify(to2)}`);
146328
+ const found = topLevelModel(text5);
146329
+ const rewritten = found?.value === from ? `${text5.slice(0, found.start)}${JSON.stringify(to2)}${text5.slice(found.end)}` : text5;
146154
146330
  const headers = Object.fromEntries(Object.entries(request7.headers).filter(([name]) => name.toLowerCase() !== "content-length"));
146155
146331
  return { ...request7, headers, body: import_node_stream3.Readable.from([Buffer.from(rewritten, "utf8")]) };
146156
146332
  }
@@ -148412,31 +148588,33 @@ function createProviderRouteResolver(deps) {
148412
148588
  current = { ...current, url: `${PROVIDER_PATH_PREFIX}${encodeURIComponent(matched.provider)}${rest}${query}`, body: replay.body, headers: replay.headers };
148413
148589
  }
148414
148590
  };
148415
- const helperModel = (provider, session, requested) => {
148591
+ const providerMapping = (provider, session, requested) => {
148416
148592
  const asIs = { kind: "as-is" };
148417
- const tier = requested.includes("/") ? void 0 : claudeTierOf(requested);
148418
- if (tier === void 0) {
148419
- return asIs;
148420
- }
148421
148593
  let definition;
148422
148594
  try {
148423
148595
  definition = loadProvider(deps.providersDir, provider, deps.fs);
148424
148596
  } catch {
148425
- return asIs;
148597
+ return { model: asIs, credential: void 0 };
148426
148598
  }
148427
148599
  if (definition === void 0) {
148428
- return asIs;
148600
+ return { model: asIs, credential: void 0 };
148601
+ }
148602
+ const credential = isRouterProvider(definition) ? void 0 : describeCredential(definition.credential);
148603
+ const tier = requested.includes("/") ? void 0 : claudeNameTier(requested);
148604
+ if (tier === void 0) {
148605
+ return { model: asIs, credential };
148429
148606
  }
148430
148607
  if (isHttpProvider(definition)) {
148431
148608
  const model = definition.env?.[TIER_ENV_VARIABLES[tier]];
148432
- return model === void 0 || model === "" ? asIs : { kind: "mapped", model: withoutContextMarker(model) };
148609
+ return { model: model === void 0 || model === "" ? asIs : { kind: "mapped", model: withoutContextMarker(model) }, credential };
148433
148610
  }
148434
148611
  if (!isRouterProvider(definition)) {
148435
- return asIs;
148612
+ return { model: asIs, credential };
148436
148613
  }
148437
148614
  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 };
148615
+ return { model: 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 }, credential };
148439
148616
  };
148617
+ const loginOf = (session) => session.identity === void 0 ? "the launch's login" : `identity ${session.identity} login`;
148440
148618
  return followingMainLoop(
148441
148619
  async (request7) => {
148442
148620
  let requestedModel;
@@ -148445,7 +148623,7 @@ function createProviderRouteResolver(deps) {
148445
148623
  });
148446
148624
  return resolution.ok && requestedModel !== void 0 ? { ...resolution, requestedModel } : resolution;
148447
148625
  },
148448
- { record: deps.mainLoopRoutes, helperModel }
148626
+ { record: deps.mainLoopRoutes, providerMapping, loginOf, definesProvider }
148449
148627
  );
148450
148628
  }
148451
148629
  function providerCredentialHeaders(credential) {
package/dist/index.cjs CHANGED
@@ -5246,6 +5246,7 @@ function recordOf(details) {
5246
5246
  function writeFrontDoorSession(fs17, sessionsDir, session) {
5247
5247
  fs17.mkdirPrivate(sessionsDir);
5248
5248
  fs17.removeRecursive(import_node_path13.default.join(sessionsDir, launchRouteName(session.pid)));
5249
+ fs17.removeRecursive(import_node_path13.default.join(sessionsDir, launchFactsName(session.pid)));
5249
5250
  fs17.writeFilePrivate(import_node_path13.default.join(sessionsDir, sessionRecordName(session.pid)), `${JSON.stringify(session, null, 2)}
5250
5251
  `);
5251
5252
  }
@@ -5258,6 +5259,9 @@ function isSessionRecordName(name) {
5258
5259
  function launchRouteName(pid) {
5259
5260
  return `${String(pid)}.route.json`;
5260
5261
  }
5262
+ function launchFactsName(pid) {
5263
+ return `${String(pid)}.context.json`;
5264
+ }
5261
5265
  function summaryOf(session) {
5262
5266
  return { pid: session.pid, startedAt: session.startedAt, ...session.supervisorPid === void 0 ? {} : { supervisorPid: session.supervisorPid } };
5263
5267
  }
@@ -5280,12 +5284,13 @@ function listFrontDoorSessions(fs17, sessionsDir) {
5280
5284
  return sessions.sort((a, b) => a.pid - b.pid);
5281
5285
  }
5282
5286
  function routeFilePid(name) {
5283
- const match = /^([1-9]\d*)\.route\.json$/.exec(name);
5287
+ const match = /^([1-9]\d*)\.(?:route|context)\.json$/.exec(name);
5284
5288
  return match?.[1] === void 0 ? void 0 : Number(match[1]);
5285
5289
  }
5286
5290
  function removeFrontDoorSession(fs17, sessionsDir, pid) {
5287
5291
  fs17.removeRecursive(import_node_path13.default.join(sessionsDir, sessionRecordName(pid)));
5288
5292
  fs17.removeRecursive(import_node_path13.default.join(sessionsDir, launchRouteName(pid)));
5293
+ fs17.removeRecursive(import_node_path13.default.join(sessionsDir, launchFactsName(pid)));
5289
5294
  }
5290
5295
  function pruneDeadFrontDoorSessions(fs17, sessionsDir, listed, isRunning) {
5291
5296
  const removed = [];
@@ -5304,7 +5309,7 @@ function pruneDeadFrontDoorSessions(fs17, sessionsDir, listed, isRunning) {
5304
5309
  }
5305
5310
  return removed;
5306
5311
  }
5307
- var import_node_path13, import_zod9, FRONT_DOOR_PROTOCOL, FRONT_DOOR_HANDOVER_PROTOCOL, FrontDoorSessionSchema, MainLoopRouteSchema, FrontDoorStateSchema, FrontDoorDirectRecordSchema, RECORD_TEMPORARY_FILE, LAUNCH_ROUTE_FILE_VERSION, LaunchRouteFileSchema;
5312
+ var import_node_path13, import_zod9, FRONT_DOOR_PROTOCOL, FRONT_DOOR_HANDOVER_PROTOCOL, FrontDoorSessionSchema, MAX_CONTEXT_TEXT_LENGTH, ContextText, LaunchRouteSchema, ContextFactsSchema, FrontDoorStateSchema, FrontDoorDirectRecordSchema, RECORD_TEMPORARY_FILE, LAUNCH_ROUTE_FILE_VERSION, LAUNCH_FACTS_FILE_VERSION, LaunchRouteFileSchema, LaunchFactsFileSchema;
5308
5313
  var init_state = __esm({
5309
5314
  "src/frontdoor/state.ts"() {
5310
5315
  "use strict";
@@ -5316,7 +5321,10 @@ var init_state = __esm({
5316
5321
  FRONT_DOOR_PROTOCOL = 8;
5317
5322
  FRONT_DOOR_HANDOVER_PROTOCOL = 6;
5318
5323
  FrontDoorSessionSchema = import_zod9.z.strictObject({ pid: import_zod9.z.number().int().positive(), startedAt: import_zod9.z.number(), token: import_zod9.z.string().min(1), supervisorPid: import_zod9.z.number().int().positive().optional(), sessionId: import_zod9.z.string().min(1).optional(), identity: import_zod9.z.string().min(1).optional(), models: ModelsConfigSchema.optional() });
5319
- MainLoopRouteSchema = import_zod9.z.union([import_zod9.z.strictObject({ kind: import_zod9.z.literal("anthropic") }), import_zod9.z.strictObject({ kind: import_zod9.z.literal("provider"), provider: import_zod9.z.string().min(1) })]);
5324
+ MAX_CONTEXT_TEXT_LENGTH = 256;
5325
+ ContextText = import_zod9.z.string().min(1).max(MAX_CONTEXT_TEXT_LENGTH);
5326
+ LaunchRouteSchema = import_zod9.z.union([import_zod9.z.strictObject({ kind: import_zod9.z.literal("anthropic") }), import_zod9.z.strictObject({ kind: import_zod9.z.literal("provider"), provider: import_zod9.z.string().min(1) })]);
5327
+ ContextFactsSchema = import_zod9.z.strictObject({ credential: ContextText.optional(), requestedModel: ContextText.optional(), servedModel: ContextText.optional() });
5320
5328
  FrontDoorStateSchema = import_zod9.z.strictObject({
5321
5329
  /**
5322
5330
  * 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`).
@@ -5350,7 +5358,9 @@ var init_state = __esm({
5350
5358
  FrontDoorDirectRecordSchema = import_zod9.z.strictObject({ directPort: import_zod9.z.number().int().positive().max(MAX_TCP_PORT) });
5351
5359
  RECORD_TEMPORARY_FILE = /^[1-9]\d*\.json\.([1-9]\d*)\.tmp$/;
5352
5360
  LAUNCH_ROUTE_FILE_VERSION = 1;
5353
- LaunchRouteFileSchema = import_zod9.z.strictObject({ version: import_zod9.z.literal(LAUNCH_ROUTE_FILE_VERSION), startedAt: import_zod9.z.number(), route: MainLoopRouteSchema });
5361
+ LAUNCH_FACTS_FILE_VERSION = 1;
5362
+ LaunchRouteFileSchema = import_zod9.z.strictObject({ version: import_zod9.z.literal(LAUNCH_ROUTE_FILE_VERSION), startedAt: import_zod9.z.number(), route: LaunchRouteSchema });
5363
+ LaunchFactsFileSchema = import_zod9.z.strictObject({ version: import_zod9.z.literal(LAUNCH_FACTS_FILE_VERSION), startedAt: import_zod9.z.number(), facts: ContextFactsSchema });
5354
5364
  }
5355
5365
  });
5356
5366
 
@@ -13902,7 +13912,7 @@ var init_package = __esm({
13902
13912
  "package.json"() {
13903
13913
  package_default = {
13904
13914
  name: "claude-use",
13905
- version: "14.1.0",
13915
+ version: "14.2.0",
13906
13916
  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.",
13907
13917
  license: "MIT",
13908
13918
  author: "Joseph Mearman <joseph@mearman.co.uk>",
package/dist/index.mjs CHANGED
@@ -5223,6 +5223,7 @@ function recordOf(details) {
5223
5223
  function writeFrontDoorSession(fs17, sessionsDir, session) {
5224
5224
  fs17.mkdirPrivate(sessionsDir);
5225
5225
  fs17.removeRecursive(path13.join(sessionsDir, launchRouteName(session.pid)));
5226
+ fs17.removeRecursive(path13.join(sessionsDir, launchFactsName(session.pid)));
5226
5227
  fs17.writeFilePrivate(path13.join(sessionsDir, sessionRecordName(session.pid)), `${JSON.stringify(session, null, 2)}
5227
5228
  `);
5228
5229
  }
@@ -5235,6 +5236,9 @@ function isSessionRecordName(name) {
5235
5236
  function launchRouteName(pid) {
5236
5237
  return `${String(pid)}.route.json`;
5237
5238
  }
5239
+ function launchFactsName(pid) {
5240
+ return `${String(pid)}.context.json`;
5241
+ }
5238
5242
  function summaryOf(session) {
5239
5243
  return { pid: session.pid, startedAt: session.startedAt, ...session.supervisorPid === void 0 ? {} : { supervisorPid: session.supervisorPid } };
5240
5244
  }
@@ -5257,12 +5261,13 @@ function listFrontDoorSessions(fs17, sessionsDir) {
5257
5261
  return sessions.sort((a, b) => a.pid - b.pid);
5258
5262
  }
5259
5263
  function routeFilePid(name) {
5260
- const match = /^([1-9]\d*)\.route\.json$/.exec(name);
5264
+ const match = /^([1-9]\d*)\.(?:route|context)\.json$/.exec(name);
5261
5265
  return match?.[1] === void 0 ? void 0 : Number(match[1]);
5262
5266
  }
5263
5267
  function removeFrontDoorSession(fs17, sessionsDir, pid) {
5264
5268
  fs17.removeRecursive(path13.join(sessionsDir, sessionRecordName(pid)));
5265
5269
  fs17.removeRecursive(path13.join(sessionsDir, launchRouteName(pid)));
5270
+ fs17.removeRecursive(path13.join(sessionsDir, launchFactsName(pid)));
5266
5271
  }
5267
5272
  function pruneDeadFrontDoorSessions(fs17, sessionsDir, listed, isRunning) {
5268
5273
  const removed = [];
@@ -5281,7 +5286,7 @@ function pruneDeadFrontDoorSessions(fs17, sessionsDir, listed, isRunning) {
5281
5286
  }
5282
5287
  return removed;
5283
5288
  }
5284
- var FRONT_DOOR_PROTOCOL, FRONT_DOOR_HANDOVER_PROTOCOL, FrontDoorSessionSchema, MainLoopRouteSchema, FrontDoorStateSchema, FrontDoorDirectRecordSchema, RECORD_TEMPORARY_FILE, LAUNCH_ROUTE_FILE_VERSION, LaunchRouteFileSchema;
5289
+ var FRONT_DOOR_PROTOCOL, FRONT_DOOR_HANDOVER_PROTOCOL, FrontDoorSessionSchema, MAX_CONTEXT_TEXT_LENGTH, ContextText, LaunchRouteSchema, ContextFactsSchema, FrontDoorStateSchema, FrontDoorDirectRecordSchema, RECORD_TEMPORARY_FILE, LAUNCH_ROUTE_FILE_VERSION, LAUNCH_FACTS_FILE_VERSION, LaunchRouteFileSchema, LaunchFactsFileSchema;
5285
5290
  var init_state = __esm({
5286
5291
  "src/frontdoor/state.ts"() {
5287
5292
  "use strict";
@@ -5291,7 +5296,10 @@ var init_state = __esm({
5291
5296
  FRONT_DOOR_PROTOCOL = 8;
5292
5297
  FRONT_DOOR_HANDOVER_PROTOCOL = 6;
5293
5298
  FrontDoorSessionSchema = z9.strictObject({ pid: z9.number().int().positive(), startedAt: z9.number(), token: z9.string().min(1), supervisorPid: z9.number().int().positive().optional(), sessionId: z9.string().min(1).optional(), identity: z9.string().min(1).optional(), models: ModelsConfigSchema.optional() });
5294
- MainLoopRouteSchema = z9.union([z9.strictObject({ kind: z9.literal("anthropic") }), z9.strictObject({ kind: z9.literal("provider"), provider: z9.string().min(1) })]);
5299
+ MAX_CONTEXT_TEXT_LENGTH = 256;
5300
+ ContextText = z9.string().min(1).max(MAX_CONTEXT_TEXT_LENGTH);
5301
+ LaunchRouteSchema = z9.union([z9.strictObject({ kind: z9.literal("anthropic") }), z9.strictObject({ kind: z9.literal("provider"), provider: z9.string().min(1) })]);
5302
+ ContextFactsSchema = z9.strictObject({ credential: ContextText.optional(), requestedModel: ContextText.optional(), servedModel: ContextText.optional() });
5295
5303
  FrontDoorStateSchema = z9.strictObject({
5296
5304
  /**
5297
5305
  * 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`).
@@ -5325,7 +5333,9 @@ var init_state = __esm({
5325
5333
  FrontDoorDirectRecordSchema = z9.strictObject({ directPort: z9.number().int().positive().max(MAX_TCP_PORT) });
5326
5334
  RECORD_TEMPORARY_FILE = /^[1-9]\d*\.json\.([1-9]\d*)\.tmp$/;
5327
5335
  LAUNCH_ROUTE_FILE_VERSION = 1;
5328
- LaunchRouteFileSchema = z9.strictObject({ version: z9.literal(LAUNCH_ROUTE_FILE_VERSION), startedAt: z9.number(), route: MainLoopRouteSchema });
5336
+ LAUNCH_FACTS_FILE_VERSION = 1;
5337
+ LaunchRouteFileSchema = z9.strictObject({ version: z9.literal(LAUNCH_ROUTE_FILE_VERSION), startedAt: z9.number(), route: LaunchRouteSchema });
5338
+ LaunchFactsFileSchema = z9.strictObject({ version: z9.literal(LAUNCH_FACTS_FILE_VERSION), startedAt: z9.number(), facts: ContextFactsSchema });
5329
5339
  }
5330
5340
  });
5331
5341
 
@@ -13875,7 +13885,7 @@ var init_package = __esm({
13875
13885
  "package.json"() {
13876
13886
  package_default = {
13877
13887
  name: "claude-use",
13878
- version: "14.1.0",
13888
+ version: "14.2.0",
13879
13889
  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.",
13880
13890
  license: "MIT",
13881
13891
  author: "Joseph Mearman <joseph@mearman.co.uk>",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-use",
3
- "version": "14.1.0",
3
+ "version": "14.2.0",
4
4
  "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.",
5
5
  "license": "MIT",
6
6
  "author": "Joseph Mearman <joseph@mearman.co.uk>",