Public: anyone can read this; the crew can change it.
crew: a Claude account can use a self-hosted endpoint on the LAN over http, with its model #14396
Check · Review · Land · Live — It conflicts with its base.
claude-local-gateway → main 2 commits
This pull asks to reach lab--forge-trevato-apps.open-platform.sh. Its preview and check reach what main reaches; the new host opens when the pull lands.
12 files
changed +166
−27
- docs/MCP.md
- src/actions/model_grant.ts
- src/core/migration-pins/20261119120000-subscription-model.pin
- src/core/migrations/20261119120000-subscription-model.ts
- src/core/subscriptions.ts
- src/core/subscriptions_words.ts
- src/crew/runner.ts
- src/crew/runner_env.ts
- src/http/mcp/tools/subscription_add.ts
- test/api-base-v1.test.ts
- test/runner-env-confined.test.ts
- test/subscriptions.test.ts
docs/MCP.md +1 −1
| @@ -219,7 +219,7 @@ bytes in `tools/list`. After changing a tool run `bun scripts/mcp-table.ts`; | ||
| 219 | 219 | | `ssh_keys` | `list`, `add`, `remove` | Your SSH keys; add: key, remove: id | 250 | |
| 220 | 220 | | `steer` | — | Say a word to one crew run by its id | 224 | |
| 221 | 221 | | `stop` | — | Stop a crew run by its id from `now`; with no repo, the forge's own agent mid-answer | 227 | |
| 222 | | `subscription_add` | — | Admin: add a Claude login | 224 | | |
| 222 | | `subscription_add` | — | Admin: add a Claude login | 250 | | |
| 223 | 223 | | `subscription_budget` | — | Admin: set an account's lifetime USD budget; null clears | 231 | |
| 224 | 224 | | `subscription_delete` | — | Remove a subscription, with its grants | 174 | |
| 225 | 225 | | `subscription_headroom` | — | Admin: set the % of each window the crew leaves for people | 235 | |
src/actions/model_grant.ts +1
| @@ -27,6 +27,7 @@ export const ADD_MESSAGES = { | ||
| 27 | 27 | name_taken: "You already added your own subscription.", |
| 28 | 28 | invalid_token: SUBSCRIPTION_WORDS.invalid_token, |
| 29 | 29 | invalid_api_base: SUBSCRIPTION_WORDS.invalid_api_base, |
| 30 | invalid_model: SUBSCRIPTION_WORDS.invalid_model, | |
| 30 | 31 | not_allowed: "Only a person adds their own subscription.", |
| 31 | 32 | }; |
| 32 | 33 |
src/core/migration-pins/20261119120000-subscription-model.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 730585a0040aa36e64bbb02e0048106762fda16d7fe7e8ac4b562819c33da7ca |
src/core/migrations/20261119120000-subscription-model.ts added +5
src/core/subscriptions.ts +53 −14
| @@ -31,6 +31,7 @@ import { err, ok, type Result } from "../kernel/result.ts"; | ||
| 31 | 31 | import { open, seal } from "../kernel/secret.ts"; |
| 32 | 32 | import { accountEnv, envNameOf, runnerCredentialInEnv } from "../crew/runner_env.ts"; |
| 33 | 33 | import { limitValue } from "../settings/limits.ts"; |
| 34 | import { isPrivateHost } from "../actions/migrate.ts"; | |
| 34 | 35 | |
| 35 | 36 | export type SubscriptionKind = "oauth" | "api_key"; |
| 36 | 37 | |
| @@ -45,6 +46,12 @@ export interface Subscription { | ||
| 45 | 46 | * `ANTHROPIC_BASE_URL` (without a trailing `/v1`; the SDK adds that). |
| 46 | 47 | */ |
| 47 | 48 | readonly apiBase: string | null; |
| 49 | /** | |
| 50 | * The model a self-hosted Anthropic-compatible endpoint serves | |
| 51 | * (`gpt-oss:120b` behind Ollama or vLLM), or null: every run on the account | |
| 52 | * asks for it, whatever tier the crew chose (`crew/runner_env.ts`). | |
| 53 | */ | |
| 54 | readonly model?: string | null | undefined; | |
| 48 | 55 | /** Set while the provider's limit holds; the account is skipped until then. */ |
| 49 | 56 | readonly restingUntil: string | null; |
| 50 | 57 | /** The provider's own words for why, one line. */ |
| @@ -59,6 +66,7 @@ interface Row { | ||
| 59 | 66 | name: string; |
| 60 | 67 | kind: string; |
| 61 | 68 | api_base: string | null; |
| 69 | model: string | null; | |
| 62 | 70 | resting_until: string | null; |
| 63 | 71 | resting_said: string; |
| 64 | 72 | last_used_at: string | null; |
| @@ -79,7 +87,8 @@ export type SubscriptionError = | ||
| 79 | 87 | | "invalid_name" |
| 80 | 88 | | "name_taken" |
| 81 | 89 | | "invalid_token" |
| 82 | | "invalid_api_base"; | |
| 90 | | "invalid_api_base" | |
| 91 | | "invalid_model"; | |
| 83 | 92 | |
| 84 | 93 | /** How soon an account out of budget is looked at again. */ |
| 85 | 94 | const BUDGET_RECHECK_MS = 3_600_000; |
| @@ -181,6 +190,7 @@ const toSubscription = (r: Row): Subscription => ({ | ||
| 181 | 190 | name: r.name, |
| 182 | 191 | kind: r.kind === "oauth" ? "oauth" : "api_key", |
| 183 | 192 | apiBase: r.api_base && r.api_base.trim() !== "" ? r.api_base : null, |
| 193 | model: r.model && r.model.trim() !== "" ? r.model : null, | |
| 184 | 194 | restingUntil: r.resting_until, |
| 185 | 195 | restingSaid: r.resting_said, |
| 186 | 196 | lastUsedAt: r.last_used_at, |
| @@ -224,11 +234,25 @@ export function kindOfToken(token: string): SubscriptionKind | undefined { | ||
| 224 | 234 | return "api_key"; |
| 225 | 235 | } |
| 226 | 236 | |
| 237 | /** Names that only resolve inside a network: `.local` (mDNS), `.internal`, `.home.arpa`, `.lan`. */ | |
| 238 | const LAN_NAME = /\.(local|internal|lan|home\.arpa)$/i; | |
| 239 | ||
| 227 | 240 | /** |
| 228 | * An Anthropic-compatible base URL for Claude Code: https, no credentials, | |
| 229 | * no query or hash. Trailing slashes and a trailing `/v1` are stripped — the | |
| 230 | * Anthropic SDK appends `/v1/messages` itself (OpenRouter wants | |
| 231 | * `https://openrouter.ai/api`, not `…/api/v1`). | |
| 241 | * Whether a base URL may be plain http: its host is loopback, a private or | |
| 242 | * link-local address, or a name only a LAN resolves. A model on the same | |
| 243 | * network (Ollama, vLLM on a workstation) speaks http; a public host must not | |
| 244 | * see the token in clear. | |
| 245 | */ | |
| 246 | export function isLanHost(hostname: string): boolean { | |
| 247 | return isPrivateHost(hostname) || LAN_NAME.test(hostname); | |
| 248 | } | |
| 249 | ||
| 250 | /** | |
| 251 | * An Anthropic-compatible base URL for Claude Code: https, or http to a host | |
| 252 | * on this network (`isLanHost`); no credentials, no query or hash. Trailing | |
| 253 | * slashes and a trailing `/v1` are stripped — the Anthropic SDK appends | |
| 254 | * `/v1/messages` itself (OpenRouter wants `https://openrouter.ai/api`, not | |
| 255 | * `…/api/v1`). | |
| 232 | 256 | */ |
| 233 | 257 | export function parseApiBase(raw: string): string | undefined { |
| 234 | 258 | const trimmed = raw.trim(); |
| @@ -240,15 +264,21 @@ export function parseApiBase(raw: string): string | undefined { | ||
| 240 | 264 | } catch { |
| 241 | 265 | return undefined; |
| 242 | 266 | } |
| 243 | if (url.protocol !== "https:") return undefined; | |
| 267 | if (url.protocol !== "https:" && !(url.protocol === "http:" && isLanHost(url.hostname))) return undefined; | |
| 244 | 268 | if (url.username !== "" || url.password !== "") return undefined; |
| 245 | 269 | if (url.search !== "" || url.hash !== "") return undefined; |
| 246 | 270 | if (url.hostname === "") return undefined; |
| 247 | 271 | let path = url.pathname.replace(/\/+$/, ""); |
| 248 | 272 | if (path.endsWith("/v1")) path = path.slice(0, -3).replace(/\/+$/, ""); |
| 249 | 273 | return path === "" || path === "/" |
| 250 | ? `https://${url.host}` | |
| 251 | : `https://${url.host}${path}`; | |
| 274 | ? `${url.protocol}//${url.host}` | |
| 275 | : `${url.protocol}//${url.host}${path}`; | |
| 276 | } | |
| 277 | ||
| 278 | /** A model name as an endpoint takes it (`gpt-oss:120b`, `org/model-7b`), or undefined. */ | |
| 279 | export function parseModelName(raw: string): string | undefined { | |
| 280 | const name = raw.trim(); | |
| 281 | return /^[\w][\w.:/@+-]{0,199}$/.test(name) ? name : undefined; | |
| 252 | 282 | } |
| 253 | 283 | |
| 254 | 284 | /** |
| @@ -277,6 +307,8 @@ export function addSubscription( | ||
| 277 | 307 | token: string; |
| 278 | 308 | /** Anthropic-compatible gateway URL; empty/omitted = Anthropic. */ |
| 279 | 309 | apiBase?: string | undefined; |
| 310 | /** The model a self-hosted endpoint serves; only with an `apiBase`. */ | |
| 311 | model?: string | undefined; | |
| 280 | 312 | createdBy?: string | undefined; |
| 281 | 313 | }, |
| 282 | 314 | ): Result<Subscription, SubscriptionError> { |
| @@ -289,6 +321,9 @@ export function addSubscription( | ||
| 289 | 321 | if (parsed === undefined) return err("invalid_api_base"); |
| 290 | 322 | apiBase = parsed; |
| 291 | 323 | } |
| 324 | const rawModel = (input.model ?? "").trim(); | |
| 325 | const model = rawModel === "" ? null : (parseModelName(rawModel) ?? false); | |
| 326 | if (model === false || (model !== null && apiBase === null)) return err("invalid_model"); | |
| 292 | 327 | const token = normalizeToken(input.token); |
| 293 | 328 | const kind = kindOfToken(token); |
| 294 | 329 | if (kind === undefined) return err("invalid_token"); |
| @@ -297,8 +332,8 @@ export function addSubscription( | ||
| 297 | 332 | if (getSubscriptionByName(db, name) !== undefined) return err("name_taken"); |
| 298 | 333 | const subscriptionId = id(); |
| 299 | 334 | db.query( |
| 300 | `INSERT INTO subscription (id, name, kind, token_enc, api_base, created_by) | |
| 301 | VALUES (?1, ?2, ?3, ?4, ?5, ?6)`, | |
| 335 | `INSERT INTO subscription (id, name, kind, token_enc, api_base, created_by, model) | |
| 336 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)`, | |
| 302 | 337 | ).run( |
| 303 | 338 | subscriptionId, |
| 304 | 339 | name, |
| @@ -306,6 +341,7 @@ export function addSubscription( | ||
| 306 | 341 | seal(dataDir, token, aad(subscriptionId)), |
| 307 | 342 | apiBase, |
| 308 | 343 | input.createdBy ?? null, |
| 344 | model, | |
| 309 | 345 | ); |
| 310 | 346 | return ok(getSubscription(db, subscriptionId)!); |
| 311 | 347 | } |
| @@ -316,7 +352,7 @@ export function getSubscription( | ||
| 316 | 352 | ): Subscription | undefined { |
| 317 | 353 | const row = db |
| 318 | 354 | .query<Row, [string]>( |
| 319 | `SELECT id, name, kind, api_base, resting_until, resting_said, last_used_at, created_by, created_at | |
| 355 | `SELECT id, name, kind, api_base, model, resting_until, resting_said, last_used_at, created_by, created_at | |
| 320 | 356 | FROM subscription WHERE id = ?1`, |
| 321 | 357 | ) |
| 322 | 358 | .get(subscriptionId); |
| @@ -329,7 +365,7 @@ export function getSubscriptionByName( | ||
| 329 | 365 | ): Subscription | undefined { |
| 330 | 366 | const row = db |
| 331 | 367 | .query<Row, [string]>( |
| 332 | `SELECT id, name, kind, api_base, resting_until, resting_said, last_used_at, created_by, created_at | |
| 368 | `SELECT id, name, kind, api_base, model, resting_until, resting_said, last_used_at, created_by, created_at | |
| 333 | 369 | FROM subscription WHERE name = ?1 COLLATE NOCASE`, |
| 334 | 370 | ) |
| 335 | 371 | .get(name.trim()); |
| @@ -339,7 +375,7 @@ export function getSubscriptionByName( | ||
| 339 | 375 | export function listSubscriptions(db: Database): Subscription[] { |
| 340 | 376 | return db |
| 341 | 377 | .query<Row, []>( |
| 342 | `SELECT id, name, kind, api_base, resting_until, resting_said, last_used_at, created_by, created_at | |
| 378 | `SELECT id, name, kind, api_base, model, resting_until, resting_said, last_used_at, created_by, created_at | |
| 343 | 379 | FROM subscription WHERE id <> 'platform' ORDER BY name COLLATE NOCASE`, |
| 344 | 380 | ) |
| 345 | 381 | .all() |
| @@ -551,6 +587,8 @@ export type Pick = | ||
| 551 | 587 | readonly token: string; |
| 552 | 588 | /** Anthropic-compatible gateway URL, or null for Anthropic itself. */ |
| 553 | 589 | readonly apiBase: string | null; |
| 590 | /** The model a self-hosted endpoint serves, or null. */ | |
| 591 | readonly model?: string | null | undefined; | |
| 554 | 592 | /** On a budgeted account: the most this run may spend, reserved for it. */ |
| 555 | 593 | readonly capUsd?: number; |
| 556 | 594 | } |
| @@ -593,7 +631,7 @@ export function pickSubscription( | ||
| 593 | 631 | }, |
| 594 | 632 | [string, string, string, string] |
| 595 | 633 | >( |
| 596 | `SELECT g.scope_id, g.agent_id, s.id, s.name, s.kind, s.token_enc, s.api_base, s.resting_until, | |
| 634 | `SELECT g.scope_id, g.agent_id, s.id, s.name, s.kind, s.token_enc, s.api_base, s.model, s.resting_until, | |
| 597 | 635 | s.resting_said, s.last_used_at, s.created_by, s.created_at, s.budget_usd, |
| 598 | 636 | ${COMMITTED} AS spent_usd |
| 599 | 637 | FROM subscription_grant g JOIN subscription s ON s.id = g.subscription_id |
| @@ -676,6 +714,7 @@ export function pickSubscription( | ||
| 676 | 714 | envName: envNameOf(subscription.kind), |
| 677 | 715 | token: opened.value, |
| 678 | 716 | apiBase: subscription.apiBase, |
| 717 | model: subscription.model, | |
| 679 | 718 | ...(capUsd === undefined ? {} : { capUsd }), |
| 680 | 719 | }; |
| 681 | 720 | } |
src/core/subscriptions_words.ts +3 −1
| @@ -16,7 +16,9 @@ export const SUBSCRIPTION_WORDS = { | ||
| 16 | 16 | invalid_token: |
| 17 | 17 | "That token is empty or over 4096 characters once spaces, line breaks and a `Bearer ` prefix are dropped. One from `claude setup-token` begins sk-ant-oat; anything else is taken as an API key (sk-ant-api…, sk-ant-usr…, sk-or-v1-…, or a gateway's own with its base URL).", |
| 18 | 18 | invalid_api_base: |
| 19 | "The API base URL must be https, with no credentials, query, or hash. For OpenRouter use https://openrouter.ai/api (a Claude subscription token cannot use a gateway).", | |
| 19 | "The API base URL must be https (http only to a loopback, private-network or .local/.lan/.internal host), with no credentials, query, or hash. For OpenRouter use https://openrouter.ai/api (a Claude subscription token cannot use a gateway).", | |
| 20 | invalid_model: | |
| 21 | "A model name is letters, digits and . : / @ + - _ (like gpt-oss:120b), and goes with an API base URL: it names what that endpoint serves.", | |
| 20 | 22 | headroom_range: `Headroom is a whole percent from 0 to ${HEADROOM_MAX}.`, |
| 21 | 23 | not_found: "No such subscription.", |
| 22 | 24 | removed: |
src/crew/runner.ts +2 −1
| @@ -1595,7 +1595,8 @@ async function work( | ||
| 1595 | 1595 | cwd: dir, |
| 1596 | 1596 | prompt, |
| 1597 | 1597 | appendSystemPrompt: systemPrompt(brief), |
| 1598 | model: chosen.model || undefined, | |
| 1598 | // An account on a self-hosted endpoint serves one model, whatever tier was chosen. | |
| 1599 | model: (picked.kind === "ready" ? picked.model : undefined) || chosen.model || undefined, | |
| 1599 | 1600 | budgetUsd, |
| 1600 | 1601 | resume, |
| 1601 | 1602 | env, |
src/crew/runner_env.ts +45 −7
| @@ -24,18 +24,40 @@ export function hostOfApiBase(apiBase: string | null | undefined): string | unde | ||
| 24 | 24 | } |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | /** The tier names Claude Code maps to a model, each read from its own variable. */ | |
| 28 | const TIER_MODEL_NAMES = ["ANTHROPIC_DEFAULT_OPUS_MODEL", "ANTHROPIC_DEFAULT_SONNET_MODEL", "ANTHROPIC_DEFAULT_HAIKU_MODEL"] as const; | |
| 29 | ||
| 30 | /** | |
| 31 | * What points Claude Code at a self-hosted Anthropic-compatible endpoint | |
| 32 | * serving `model` (Ollama, vLLM): the token as a bearer (`ANTHROPIC_AUTH_TOKEN`, | |
| 33 | * the API key empty so it is not sent as one), every tier mapped to the one | |
| 34 | * model, and Anthropic's experimental beta headers off, which such an | |
| 35 | * endpoint refuses. | |
| 36 | */ | |
| 37 | function selfHostedEnv(base: string, token: string, model: string): Record<string, string> { | |
| 38 | return { | |
| 39 | ANTHROPIC_BASE_URL: base, | |
| 40 | ANTHROPIC_AUTH_TOKEN: token, | |
| 41 | ANTHROPIC_API_KEY: "", | |
| 42 | ...Object.fromEntries(TIER_MODEL_NAMES.map((n) => [n, model])), | |
| 43 | CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS: "1", | |
| 44 | }; | |
| 45 | } | |
| 46 | ||
| 27 | 47 | /** |
| 28 | 48 | * Variables for a picked account: the token under the variable Claude Code |
| 29 | 49 | * reads, plus the base URL (and OpenRouter's auth-token dance) when the |
| 30 | * account has a gateway. | |
| 50 | * account has a gateway, and the self-hosted set when it names a model. | |
| 31 | 51 | */ |
| 32 | 52 | export function accountEnv(a: { |
| 33 | 53 | readonly envName: string; |
| 34 | 54 | readonly token: string; |
| 35 | 55 | readonly apiBase?: string | null | undefined; |
| 56 | readonly model?: string | null | undefined; | |
| 36 | 57 | }): Record<string, string> { |
| 37 | 58 | const base = a.apiBase; |
| 38 | 59 | if (base === null || base === undefined || base === "") return { [a.envName]: a.token }; |
| 60 | if (a.model !== null && a.model !== undefined && a.model !== "") return selfHostedEnv(base, a.token, a.model); | |
| 39 | 61 | const host = hostOfApiBase(base) ?? ""; |
| 40 | 62 | if (host === "openrouter.ai" || host.endsWith(".openrouter.ai")) { |
| 41 | 63 | // AUTH_TOKEN carries the key; API_KEY must be empty so Claude Code does |
| @@ -51,7 +73,14 @@ export function accountEnv(a: { | ||
| 51 | 73 | */ |
| 52 | 74 | export function ownEnv(env: Env = process.env): Record<string, string> { |
| 53 | 75 | const own: Record<string, string> = {}; |
| 54 | for (const name of ["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN"]) { | |
| 76 | for (const name of [ | |
| 77 | "ANTHROPIC_API_KEY", | |
| 78 | "CLAUDE_CODE_OAUTH_TOKEN", | |
| 79 | "ANTHROPIC_BASE_URL", | |
| 80 | "ANTHROPIC_AUTH_TOKEN", | |
| 81 | ...TIER_MODEL_NAMES, | |
| 82 | "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS", | |
| 83 | ]) { | |
| 55 | 84 | const value = env[name]; |
| 56 | 85 | if (value !== undefined) own[name] = value; |
| 57 | 86 | } |
| @@ -84,7 +113,7 @@ export function namedRunner(env: Env): string | undefined { | ||
| 84 | 113 | |
| 85 | 114 | /** Whether the environment holds a model token or a signed-in runner config dir. */ |
| 86 | 115 | export function runnerCredentialInEnv(env: Env): boolean { |
| 87 | if (["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"].some((n) => (env[n] ?? "").trim() !== "")) return true; | |
| 116 | if (["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_AUTH_TOKEN"].some((n) => (env[n] ?? "").trim() !== "")) return true; | |
| 88 | 117 | return (runnerSettings(env).configDir ?? "").trim() !== ""; |
| 89 | 118 | } |
| 90 | 119 | |
| @@ -93,13 +122,22 @@ export function lentEnv(door: string, token: string): Record<string, string> { | ||
| 93 | 122 | return { ANTHROPIC_BASE_URL: door, ANTHROPIC_API_KEY: token }; |
| 94 | 123 | } |
| 95 | 124 | |
| 96 | /** A variable that holds an address, not a secret: left readable in a run's log. */ | |
| 97 | export const isGatewayAddress = (name: string): boolean => name === "ANTHROPIC_BASE_URL"; | |
| 125 | /** | |
| 126 | * A variable that holds an address or a setting, not a secret: left readable | |
| 127 | * in a run's log (a model name held as a secret would blank every mention of it). | |
| 128 | */ | |
| 129 | export const isGatewayAddress = (name: string): boolean => | |
| 130 | name === "ANTHROPIC_BASE_URL" || name === "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS" || (TIER_MODEL_NAMES as readonly string[]).includes(name); | |
| 98 | 131 | |
| 99 | /** The two variables that quiet Claude Code's background traffic. */ | |
| 100 | export const quietEnv = (): Record<string, string> => ({ | |
| 132 | /** | |
| 133 | * The two variables that quiet Claude Code's background traffic, and the | |
| 134 | * operator's extra CA bundle (`NODE_EXTRA_CA_CERTS`), so an endpoint behind a | |
| 135 | * private CA is trusted as the forge trusts it. | |
| 136 | */ | |
| 137 | export const quietEnv = (env: Env = process.env): Record<string, string> => ({ | |
| 101 | 138 | CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1", |
| 102 | 139 | DISABLE_TELEMETRY: "1", |
| 140 | ...((env["NODE_EXTRA_CA_CERTS"] ?? "") === "" ? {} : { NODE_EXTRA_CA_CERTS: env["NODE_EXTRA_CA_CERTS"]! }), | |
| 103 | 141 | }); |
| 104 | 142 | |
| 105 | 143 | /** Where Claude Code's sandbox keeps its temp files. */ |
src/http/mcp/tools/subscription_add.ts +3 −2
| @@ -4,11 +4,11 @@ import { good, bad, type Tool } from "../shared.ts"; | ||
| 4 | 4 | |
| 5 | 5 | export const tool: Tool = { |
| 6 | 6 | name: "subscription_add", |
| 7 | doc: "`{ name, token, apiBase? }` (forge admin): put a Claude account in the crew's pool, as the Subscriptions page does, with its refusals; answers `{ id, name, says }`; the token is never read back", | |
| 7 | doc: "`{ name, token, apiBase?, model? }` (forge admin): put a Claude account in the crew's pool, as the Subscriptions page does, with its refusals; `apiBase` is https, or http to a LAN or loopback host, and `model` names what a self-hosted endpoint there serves (`gpt-oss:120b`); answers `{ id, name, says }`; the token is never read back", | |
| 8 | 8 | description: "Admin: add a Claude login.", |
| 9 | 9 | inputSchema: { |
| 10 | 10 | type: "object", |
| 11 | properties: { name: { type: "string" }, token: { type: "string" }, apiBase: { type: "string" } }, | |
| 11 | properties: { name: { type: "string" }, token: { type: "string" }, apiBase: { type: "string" }, model: { type: "string" } }, | |
| 12 | 12 | required: ["name", "token"], |
| 13 | 13 | }, |
| 14 | 14 | writes: true, |
| @@ -21,6 +21,7 @@ export const tool: Tool = { | ||
| 21 | 21 | name, |
| 22 | 22 | token: str(args["token"]).trim(), |
| 23 | 23 | apiBase: str(args["apiBase"]).trim(), |
| 24 | model: str(args["model"]).trim(), | |
| 24 | 25 | createdBy: user.id, |
| 25 | 26 | }); |
| 26 | 27 | if (!added.ok) return bad(added.error, addRefusal(added.error, name)); |
test/api-base-v1.test.ts +11
| @@ -43,6 +43,17 @@ test("a base given with /v1 and trailing slashes is kept without them", () => { | ||
| 43 | 43 | expect(parseApiBase("http://x")).toBeUndefined(); |
| 44 | 44 | }); |
| 45 | 45 | |
| 46 | test("http is a base only on this network: loopback, private ranges and LAN names; a public host keeps https", () => { | |
| 47 | expect(parseApiBase("http://192.168.1.40:11434")).toBe("http://192.168.1.40:11434"); | |
| 48 | expect(parseApiBase("http://10.0.0.7:8000/v1")).toBe("http://10.0.0.7:8000"); | |
| 49 | expect(parseApiBase("http://127.0.0.1:11434/")).toBe("http://127.0.0.1:11434"); | |
| 50 | expect(parseApiBase("http://localhost:11434")).toBe("http://localhost:11434"); | |
| 51 | expect(parseApiBase("http://[::1]:8000")).toBe("http://[::1]:8000"); | |
| 52 | expect(parseApiBase("http://spark.local:11434")).toBe("http://spark.local:11434"); | |
| 53 | expect(parseApiBase("http://openrouter.ai/api")).toBeUndefined(); | |
| 54 | expect(parseApiBase("http://8.8.8.8:11434")).toBeUndefined(); | |
| 55 | }); | |
| 56 | ||
| 46 | 57 | test("the terminal screen shows the hint on one 80-column line", async () => { |
| 47 | 58 | const terminal = headlessTerminal(80, 40); |
| 48 | 59 | const { app: tui, session } = createForgeApp( |
test/runner-env-confined.test.ts +18 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | import { Glob } from "bun"; |
| 3 | import { accountEnv, runnerCredentialInEnv, lentEnv, oneShotEnv, ownEnv, upstreamOf } from "../src/crew/runner_env.ts"; | |
| 3 | import { accountEnv, isGatewayAddress, quietEnv, runnerCredentialInEnv, lentEnv, oneShotEnv, ownEnv, upstreamOf } from "../src/crew/runner_env.ts"; | |
| 4 | 4 | |
| 5 | 5 | test("only runner_env.ts names a vendor variable in src", async () => { |
| 6 | 6 | const re = /\b(ANTHROPIC_[A-Z_]+|CLAUDE_CODE_[A-Z_]+)\b/; |
| @@ -23,6 +23,23 @@ test("runner_env builds each environment", () => { | ||
| 23 | 23 | ANTHROPIC_AUTH_TOKEN: "t", |
| 24 | 24 | ANTHROPIC_API_KEY: "", |
| 25 | 25 | }); |
| 26 | // A self-hosted endpoint (Ollama, vLLM) serving one model: a bearer token, every tier that model, betas off. | |
| 27 | expect(accountEnv({ envName: "ANTHROPIC_API_KEY", token: "t", apiBase: "http://192.168.1.40:11434", model: "gpt-oss:120b" })).toEqual({ | |
| 28 | ANTHROPIC_BASE_URL: "http://192.168.1.40:11434", | |
| 29 | ANTHROPIC_AUTH_TOKEN: "t", | |
| 30 | ANTHROPIC_API_KEY: "", | |
| 31 | ANTHROPIC_DEFAULT_OPUS_MODEL: "gpt-oss:120b", | |
| 32 | ANTHROPIC_DEFAULT_SONNET_MODEL: "gpt-oss:120b", | |
| 33 | ANTHROPIC_DEFAULT_HAIKU_MODEL: "gpt-oss:120b", | |
| 34 | CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS: "1", | |
| 35 | }); | |
| 36 | expect(ownEnv({ ANTHROPIC_BASE_URL: "http://127.0.0.1:11434", ANTHROPIC_AUTH_TOKEN: "ollama", ANTHROPIC_DEFAULT_SONNET_MODEL: "gpt-oss:120b", CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS: "1", PATH: "/bin" })).toEqual({ | |
| 37 | ANTHROPIC_BASE_URL: "http://127.0.0.1:11434", ANTHROPIC_AUTH_TOKEN: "ollama", ANTHROPIC_DEFAULT_SONNET_MODEL: "gpt-oss:120b", CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS: "1", | |
| 38 | }); | |
| 39 | expect(runnerCredentialInEnv({ ANTHROPIC_AUTH_TOKEN: "ollama" })).toBe(true); | |
| 40 | expect(quietEnv({ NODE_EXTRA_CA_CERTS: "/etc/ca.pem" })["NODE_EXTRA_CA_CERTS"]).toBe("/etc/ca.pem"); | |
| 41 | expect(isGatewayAddress("ANTHROPIC_DEFAULT_OPUS_MODEL")).toBe(true); | |
| 42 | expect(isGatewayAddress("ANTHROPIC_AUTH_TOKEN")).toBe(false); | |
| 26 | 43 | expect(lentEnv("http://x", "k")).toEqual({ ANTHROPIC_BASE_URL: "http://x", ANTHROPIC_API_KEY: "k" }); |
| 27 | 44 | expect(ownEnv({ ANTHROPIC_API_KEY: "a", PATH: "/bin" })).toEqual({ ANTHROPIC_API_KEY: "a" }); |
| 28 | 45 | expect(oneShotEnv({ CLAUDE_CODE_OAUTH_TOKEN: "o", ANTHROPIC_BASE_URL: "u" })).toEqual({ CLAUDE_CODE_OAUTH_TOKEN: "o" }); |
test/subscriptions.test.ts +23
| @@ -117,6 +117,29 @@ describe("the pool", () => { | ||
| 117 | 117 | expect(raw.token_enc).not.toContain(OAT); |
| 118 | 118 | }); |
| 119 | 119 | |
| 120 | test("an account on a self-hosted endpoint on the LAN takes http and a model, and every run on it asks for that model", () => { | |
| 121 | const added = addSubscription(app.db, dataDir, { | |
| 122 | name: "spark", | |
| 123 | token: "ollama", | |
| 124 | apiBase: "http://192.168.1.40:11434", | |
| 125 | model: "gpt-oss:120b", | |
| 126 | }); | |
| 127 | if (!added.ok) throw new Error(added.error); | |
| 128 | expect(added.value).toMatchObject({ apiBase: "http://192.168.1.40:11434", model: "gpt-oss:120b" }); | |
| 129 | setGrants(app.db, "repo-spark", EVERYONE, [added.value.id]); | |
| 130 | const pick = pickSubscription(app.db, dataDir, { repoId: "repo-spark", ownerId: "org-spark", agentId: "bot-spark" }, new Date("2026-10-07T12:00:00Z")); | |
| 131 | if (pick.kind !== "ready") throw new Error(pick.kind); | |
| 132 | expect(pick.model).toBe("gpt-oss:120b"); | |
| 133 | expect(credentialEnvOf(pick)).toMatchObject({ | |
| 134 | ANTHROPIC_BASE_URL: "http://192.168.1.40:11434", | |
| 135 | ANTHROPIC_AUTH_TOKEN: "ollama", | |
| 136 | ANTHROPIC_DEFAULT_SONNET_MODEL: "gpt-oss:120b", | |
| 137 | }); | |
| 138 | expect(addSubscription(app.db, dataDir, { name: "public-http", token: "k", apiBase: "http://openrouter.ai/api" })).toEqual({ ok: false, error: "invalid_api_base" }); | |
| 139 | expect(addSubscription(app.db, dataDir, { name: "model-alone", token: "k", model: "gpt-oss:120b" })).toEqual({ ok: false, error: "invalid_model" }); | |
| 140 | expect(addSubscription(app.db, dataDir, { name: "bad-model", token: "k", apiBase: "http://127.0.0.1:8000", model: "a model; rm" })).toEqual({ ok: false, error: "invalid_model" }); | |
| 141 | }); | |
| 142 | ||
| 120 | 143 | test("seals an OpenRouter key with its gateway and injects Claude Code env", () => { |
| 121 | 144 | const added = addSubscription(app.db, dataDir, { |
| 122 | 145 | name: "openrouter", |
new above
offline