After /compact, Claude Code 2.1.280 sends the files it restores and the
session's runtime context as one system text block with no reminder
wrappers and no opening token marker: restored-file notes ("Note: <path> was
read before the last conversation was summarized…"), "Called the Read tool
with the following input: {…}" and its result, "# Environment\nYou have been
invoked in the following environment:", the model line, token and date.
The reporter's bisect shows Factory 403s on each of those fixed fragments,
and 0.1.21 matched none of them: SYSTEM_ENV_CONTEXT is anchored at the
block's start, COMPACT_READ / COMPACT_FILE need a standalone wrapped
reminder, and the token-prefixed bundle path needs a leading token marker.
restoredContext recognizes the complete turn: its first paragraph is a
generated restored-file note or Read call (as COMPACT_FILE / COMPACT_READ
would match it wrapped), and a later paragraph, before any hook output, is
the generated environment paragraph or a token marker. Then
announcedContext adapts every paragraph from the first on, with the
existing compat spellings (Previously read file…, # Runtime context / The
session environment is:, Current model name / Model ID / Model knowledge
cutoff) and the lossless JSON encoding of Read results; everything else
stays byte-for-byte. It runs for system-role strings and text blocks and
for the same block folded into user text. An adapted turn no longer opens
with the generated note, so a second pass is byte-for-byte.
Quoted, incomplete (no environment or token), hook-owned and assistant
forms are left untouched.
Run: bun test (packages/factory: 116 pass; repo: 1010 pass, 0 fail),
bun scripts/check.mjs (all pass). The two new tests use the issue's literal
fragments (several notes, Read call + result, environment, model line with
claude-opus-5-5[1m], token, date); the adapting one fails on 0.1.21's
index.mjs, and removing the env/token requirement, the hook guard, the
user-text path or starting at paragraph 1 each turns a test red. PR #66
applied on top passes too (119 factory tests). Stubbed fetch only; no
request was sent to Factory (no Factory account here).
1417 lines
67 KiB
JavaScript
1417 lines
67 KiB
JavaScript
// Factory (Droid) subscriptions for OpenCode and magpie: WorkOS's device
|
|
// flow under droid's client, as `droid` signs in, and each model request
|
|
// sent to Factory's API with the headers droid sends. Ported from magpie's
|
|
// built-in Factory account (internal/provider/factory*.go).
|
|
|
|
import { STATUS_CODES } from "node:http"
|
|
import { parseToolJSON, wrapAnthropicTools, unwrapAnthropicTools } from "./anthropic-tools.mjs"
|
|
|
|
const PROVIDER = "factory"
|
|
|
|
const WORKOS = "https://api.workos.com/user_management"
|
|
const API = "https://api.factory.ai"
|
|
const API_EU = "https://api.eu.factory.ai"
|
|
// droid's WorkOS client, production
|
|
const CLIENT_ID = "client_01HNM792M5G5G1A2THWPXKFMXB"
|
|
// the droid release the requests say they are
|
|
const VERSION = "0.233.0"
|
|
// how long before an access token lapses it is renewed (droid: a minute)
|
|
const REFRESH_LEAD = 2 * 60 * 1000
|
|
// and how long before, magpie renews it ahead of time (auth.refresh): a
|
|
// minute more, so under magpie the check before a request finds it fresh.
|
|
// WorkOS's access tokens are short-lived, so not much more
|
|
const MAGPIE_LEAD = 3 * 60 * 1000
|
|
// how long an account whose whoami failed waits before asking again
|
|
const ASK_AGAIN = 10 * 60 * 1000
|
|
|
|
const ANTHROPIC = { npm: "@ai-sdk/anthropic", api: API + "/api/llm/a/v1" }
|
|
const RESPONSES = { npm: "@ai-sdk/openai", api: API + "/api/llm/o/v1" }
|
|
const CHAT = { npm: "@ai-sdk/openai-compatible", api: API + "/api/llm/o/v1" }
|
|
|
|
const E5 = ["low", "medium", "high", "xhigh", "max"]
|
|
const E6 = ["none", "low", "medium", "high", "xhigh", "max"]
|
|
const E4 = ["low", "medium", "high", "xhigh"]
|
|
|
|
// The models droid's /model picker offers, less Gemini's (sent on a route of
|
|
// Factory's own) and auto (droid picks it client side): id, name, the wire
|
|
// droid sends it on, the vendor it names in x-api-provider, context, output,
|
|
// reasoning efforts, images.
|
|
const MODELS = [
|
|
["claude-fable-5.1", "Fable 5.1", ANTHROPIC, "anthropic", 867000, 128000, E5, true],
|
|
["claude-fable-5", "Fable 5", ANTHROPIC, "anthropic", 867000, 128000, E5, true],
|
|
["claude-opus-5-5", "Opus 5.5", ANTHROPIC, "anthropic", 872000, 128000, E5, true],
|
|
["claude-opus-5", "Opus 5", ANTHROPIC, "anthropic", 867000, 128000, E5, true],
|
|
["claude-opus-4-8", "Opus 4.8", ANTHROPIC, "anthropic", 867000, 128000, E5, true],
|
|
["claude-sonnet-5-5", "Sonnet 5.5", ANTHROPIC, "anthropic", 872000, 128000, E5, true],
|
|
["claude-sonnet-5", "Sonnet 5", ANTHROPIC, "anthropic", 872000, 128000, E5, true],
|
|
["claude-sonnet-4-6", "Sonnet 4.6", ANTHROPIC, "anthropic", 931000, 64000, ["low", "medium", "high", "max"], true],
|
|
["claude-haiku-4-5-20251001", "Haiku 4.5", ANTHROPIC, "anthropic", 0, 0, ["low", "medium", "high"], true],
|
|
["gpt-6.1-sol", "GPT-6.1 Sol", RESPONSES, "openai", 1050000, 128000, E5, true],
|
|
["gpt-6-sol", "GPT-6 Sol", RESPONSES, "openai", 1050000, 128000, E6, true],
|
|
["gpt-6-astra", "GPT-6 Astra", RESPONSES, "openai", 1050000, 128000, E5, true],
|
|
["gpt-6-luna", "GPT-6 Luna", RESPONSES, "openai", 1050000, 128000, E6, true],
|
|
["gpt-5.6-sol", "GPT-5.6 Sol", RESPONSES, "openai", 1050000, 128000, E6, true],
|
|
["gpt-5.6-terra", "GPT-5.6 Terra", RESPONSES, "openai", 1050000, 128000, E6, true],
|
|
["gpt-5.6-luna", "GPT-5.6 Luna", RESPONSES, "openai", 1050000, 128000, E6, true],
|
|
["gpt-5.5", "GPT-5.5", RESPONSES, "openai", 1050000, 128000, E4, true],
|
|
["gpt-5.4", "GPT-5.4", RESPONSES, "openai", 1050000, 128000, E4, true],
|
|
["gpt-5.3-codex", "GPT-5.3-Codex", RESPONSES, "openai", 400000, 128000, E4, true],
|
|
["grok-4.7", "Grok 4.7", RESPONSES, "xai", 500000, 63356, E4, true],
|
|
["grok-4.6", "Grok 4.6", RESPONSES, "xai", 200000, 63356, E4, true],
|
|
// reasoning is mandatory on GLM-5.3 and GLM-5.3-Flash (Fireworks turns
|
|
// none away): low is their least. GLM-5.2, Kimi K3 and DeepSeek V4.1
|
|
// Flash stop thinking at none, droid's "off" (yetone/magpie#899).
|
|
["glm-5.3", "GLM-5.3", CHAT, "fireworks", 1040000, 131072, ["low", "high", "max"], false],
|
|
["glm-5.3-flash", "GLM-5.3-Flash", CHAT, "fireworks", 1048576, 131072, ["low", "high", "max"], true],
|
|
["glm-5.2", "GLM-5.2", CHAT, "baseten", 1040000, 131072, ["none", "high", "max"], false],
|
|
["kimi-k3", "Kimi K3", CHAT, "fireworks", 262144, 65536, ["none", "low", "high", "max"], true],
|
|
["deepseek-v4.1-flash", "DeepSeek V4.1 Flash", CHAT, "fireworks", 1040000, 131072, ["none", "low", "high", "max"], true],
|
|
["qwen3.8-max", "Qwen3.8 Max", CHAT, "fireworks", 262144, 131072, ["low", "medium", "xhigh"], false],
|
|
["minimax-m3", "MiniMax M3", CHAT, "fireworks", 512000, 64000, ["high"], true],
|
|
["minimax-m2.7", "MiniMax M2.7", ANTHROPIC, "fireworks", 196600, 64000, ["high"], false],
|
|
["mistral-medium-3.5", "Mistral Medium 3.5", CHAT, "mistral", 256000, 64000, ["high"], true],
|
|
["nemotron-3-ultra", "Nemotron 3 Ultra", CHAT, "baseten", 202000, 65536, ["high"], false],
|
|
].map(([id, name, wire, upstream, context, output, efforts, images]) => ({ id, name, wire, upstream, context, output, efforts, images }))
|
|
|
|
const byId = new Map(MODELS.map((m) => [m.id, m]))
|
|
|
|
// variant is what the AI SDK package a model is on is given for an effort.
|
|
function variant(npm, effort) {
|
|
if (npm === ANTHROPIC.npm) return { effort }
|
|
return { reasoningEffort: effort }
|
|
}
|
|
|
|
function configModels() {
|
|
const out = {}
|
|
for (const m of MODELS) {
|
|
out[m.id] = {
|
|
id: m.id,
|
|
name: m.name,
|
|
provider: { npm: m.wire.npm, api: m.wire.api },
|
|
reasoning: m.efforts.length > 0,
|
|
attachment: m.images,
|
|
tool_call: true,
|
|
temperature: true,
|
|
modalities: { input: m.images ? ["text", "image"] : ["text"], output: ["text"] },
|
|
cost: { input: 0, output: 0 },
|
|
limit: { context: m.context, output: m.output },
|
|
variants: Object.fromEntries(m.efforts.map((e) => [e, variant(m.wire.npm, e)])),
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ---- tokens -----------------------------------------------------------------
|
|
|
|
class FactoryStatus extends Error {
|
|
constructor(code, message, body) {
|
|
super(message)
|
|
this.code = code
|
|
this.body = body
|
|
}
|
|
}
|
|
|
|
// refused is WorkOS turning the refresh token away for good: any 4xx but a
|
|
// rate limit, as droid reads it.
|
|
const refused = (e) => e instanceof FactoryStatus && e.code >= 400 && e.code < 500 && e.code !== 429
|
|
|
|
function claims(jwt) {
|
|
try {
|
|
const part = String(jwt ?? "").split(".")[1]
|
|
return JSON.parse(Buffer.from(part.replace(/-/g, "+").replace(/_/g, "/"), "base64").toString("utf8")) ?? {}
|
|
} catch {
|
|
return {}
|
|
}
|
|
}
|
|
|
|
const claim = (c, k) => (typeof c[k] === "string" ? c[k] : "")
|
|
|
|
// expiry is when an access token lapses, from its exp; 0 when it doesn't say.
|
|
function expiry(access) {
|
|
const exp = claims(access).exp
|
|
return typeof exp === "number" && exp > 0 ? Math.floor(exp) * 1000 : 0
|
|
}
|
|
|
|
// workos posts a form to WorkOS: the answer and its status.
|
|
async function workos(path, form, signal) {
|
|
const res = await fetch(WORKOS + path, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" },
|
|
body: new URLSearchParams(form).toString(),
|
|
signal: signal ?? AbortSignal.timeout(30_000),
|
|
})
|
|
return { text: await res.text(), status: res.status }
|
|
}
|
|
|
|
// authenticate asks WorkOS for tokens. A 400 carrying an OAuth error is an
|
|
// answer, not a failure: the device flow's polls are told to wait so.
|
|
async function authenticate(form, signal) {
|
|
const { text, status } = await workos("/authenticate", form, signal)
|
|
let t = {}
|
|
try {
|
|
t = JSON.parse(text) ?? {}
|
|
} catch {}
|
|
t.status = status
|
|
if (status !== 200 && !t.error) throw new FactoryStatus(status, "Factory sign-in: " + vendorError(text, STATUS_CODES[status] ?? ""))
|
|
return t
|
|
}
|
|
|
|
// renew trades a refresh token for a new pair, in the WorkOS org when one is
|
|
// named (droid's way of putting a token that names no org in one); droid's
|
|
// routine refresh names none, and WorkOS keeps the org.
|
|
async function renew(refresh, org) {
|
|
const form = { grant_type: "refresh_token", refresh_token: refresh, client_id: CLIENT_ID }
|
|
if (org) form.organization_id = org
|
|
const t = await authenticate(form, AbortSignal.timeout(20_000))
|
|
if (t.error) throw new FactoryStatus(t.status, "Factory: " + `${t.error} ${t.error_description ?? ""}`.trim())
|
|
if (!t.access_token) throw new Error("Factory: the refresh gave no access token")
|
|
return t
|
|
}
|
|
|
|
// ---- Factory's API ------------------------------------------------------------
|
|
|
|
// base is the Factory API the account's org is served from.
|
|
const base = (c) => (c.region === "eu" ? API_EU : API)
|
|
|
|
// llmBase is where the account's model requests go: the org's own host when
|
|
// whoami named one, else its region's API.
|
|
function llmBase(c) {
|
|
if (c.premBaseHost) {
|
|
const p = c.premBaseHost.replace(/\/+$/, "")
|
|
return p.includes("://") ? p : "https://" + p
|
|
}
|
|
return base(c)
|
|
}
|
|
|
|
// factoryHeaders are what droid sends on every call to Factory's API.
|
|
function factoryHeaders(h, c) {
|
|
h.set("Authorization", "Bearer " + c.access)
|
|
h.set("X-Factory-Client", "cli")
|
|
h.set("X-Client-Version", VERSION)
|
|
h.set("User-Agent", "factory-cli/" + VERSION)
|
|
if (c.activeOrganizationId) h.set("X-Factory-Org-Id", c.activeOrganizationId)
|
|
else h.delete("X-Factory-Org-Id")
|
|
}
|
|
|
|
async function factoryGet(c, path) {
|
|
const h = new Headers({ Accept: "application/json" })
|
|
factoryHeaders(h, c)
|
|
const res = await fetch(base(c) + path, { headers: h, signal: AbortSignal.timeout(30_000) })
|
|
const text = await res.text()
|
|
if (res.status !== 200) throw new FactoryStatus(res.status, "Factory: " + vendorError(text, statusLine(res.status)))
|
|
return JSON.parse(text)
|
|
}
|
|
|
|
// firstOrg is the first WorkOS org /api/cli/org says the account is in, ""
|
|
// for none. droid asks it with the bearer token alone.
|
|
async function firstOrg(c) {
|
|
const r = await factoryGet({ ...c, activeOrganizationId: "" }, "/api/cli/org")
|
|
return r?.workosOrgIds?.[0] ?? ""
|
|
}
|
|
|
|
// whoami asks Factory whose the token is with the headers droid's whoami
|
|
// sends: the token, X-Factory-Whoami-Extended, and the active org when there
|
|
// is one — nothing else.
|
|
async function whoami(c, signal) {
|
|
const h = { Authorization: "Bearer " + c.access, "X-Factory-Whoami-Extended": "true" }
|
|
if (c.activeOrganizationId) h["X-Factory-Org-Id"] = c.activeOrganizationId
|
|
const res = await fetch(base(c) + "/api/cli/whoami", { headers: h, signal: signal ?? AbortSignal.timeout(10_000) })
|
|
const text = await res.text()
|
|
if (res.status !== 200) throw new FactoryStatus(res.status, "Factory: " + vendorError(text, statusLine(res.status)))
|
|
return JSON.parse(text) ?? {}
|
|
}
|
|
|
|
// reconcile asks whoami, as droid does for each token it holds, and keeps the
|
|
// org, region and host it names: true when any changed. An active org whoami
|
|
// refuses is left off and whoami asked again without it.
|
|
async function reconcile(c) {
|
|
const signal = AbortSignal.timeout(10_000)
|
|
let who
|
|
try {
|
|
who = await whoami(c, signal)
|
|
} catch (e) {
|
|
if (!(c.activeOrganizationId && e instanceof FactoryStatus && e.code === 403)) return false
|
|
try {
|
|
who = await whoami({ ...c, activeOrganizationId: "" }, signal)
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
if (!who?.orgId) return false
|
|
const changed = c.activeOrganizationId !== who.orgId || (c.region ?? "") !== (who.region ?? "") || (c.premBaseHost ?? "") !== (who.premBaseHostV2 ?? "")
|
|
c.activeOrganizationId = who.orgId
|
|
c.region = who.region ?? ""
|
|
c.premBaseHost = who.premBaseHostV2 ?? ""
|
|
return changed
|
|
}
|
|
|
|
// orgRefused is Factory's 403 for an X-Factory-Org-Id the user can't reach:
|
|
// "Requested active organization is not accessible by this user".
|
|
const orgRefused = (status, text) => status === 403 && text.toLowerCase().includes("active organization is not accessible")
|
|
|
|
// explain is what the user can do about a 403 Factory still answers once
|
|
// the request carries what droid sends.
|
|
function explain(status, text) {
|
|
if (status !== 403) return ""
|
|
if (orgRefused(status, text)) return "the Factory account's organization changed; remove the account in magpie and sign in to it again"
|
|
return "Factory refused the request. magpie adapts fixed client metadata on both OpenAI and Anthropic routes. Factory may still refuse unsupported fields or wrappers; check the upstream error, the organization's model policy, plan and regional provider availability, and compare the same model in Droid"
|
|
}
|
|
|
|
// ---- opening as droid ---------------------------------------------------------
|
|
//
|
|
// Factory takes a subscription's model requests only from Droid (magpie
|
|
// #242, #506): the same account's GPT, Grok and GLM answered Droid every
|
|
// time and refused Codex, Grok Build and Claude Code every time, on the same
|
|
// models, efforts, endpoint and headers, the body the only difference. Every
|
|
// request droid sends opens its system prompt with one line (droid 0.231.0:
|
|
// its system blocks are [that line, the agent's prompt, …], sent as
|
|
// Responses' instructions joined with "\n", and on chat completions as one
|
|
// system message first, joined the same way). So another agent's request to
|
|
// /api/llm/o opens with the line too, as the built-in's factoryDroidBody
|
|
// (internal/provider/factory_client.go) has it. Anthropic's Messages
|
|
// (/api/llm/a) uses anthropicBody below to adapt fixed client metadata.
|
|
// A native Droid request goes on byte for byte.
|
|
|
|
const DROID_LINE = "You are Droid, an AI software engineering agent built by Factory."
|
|
|
|
// pi (@earendil-works/pi-coding-agent 1.0.4) opens its system prompt with
|
|
// a sentence Factory refuses whole, on the OpenAI and Anthropic routes
|
|
// alike (yetone/magpie#952): either half of it passes, and pi's full
|
|
// request with the sentence cut to "You are an expert coding assistant."
|
|
// is answered. Only that sentence, at the start of a line of a system or
|
|
// developer prompt, changes; the rest of pi's prompt goes on as it is.
|
|
const PI_OPENING = /(^|\n)You are an expert coding assistant operating inside pi, a coding agent harness\./
|
|
const PI_COMPAT = "You are an expert coding assistant."
|
|
function clientOpening(text) {
|
|
return typeof text === "string" ? text.replace(PI_OPENING, (_, at) => at + PI_COMPAT) : text
|
|
}
|
|
|
|
// pi turns a compaction, and a branch it came back from, into a user
|
|
// message of one text: a fixed sentence, then the summary in <summary> tags
|
|
// (pi-coding-agent 1.0.4 and 1.1.0, dist/core/messages.js:
|
|
// COMPACTION_SUMMARY_PREFIX and BRANCH_SUMMARY_PREFIX). Factory refuses the
|
|
// compaction sentence whole (yetone/magpie#1316): after a compaction every
|
|
// turn was 403, and the reporter's replays passed with the sentence removed,
|
|
// reworded or only half of it. The branch sentence is pi's other fixed
|
|
// summary opening and is reworded the same way. Only a text that is pi's
|
|
// whole summary, sentence first and </summary> last, changes; the summary
|
|
// itself goes on as it is.
|
|
const PI_SUMMARIES = [
|
|
["The conversation history before this point was compacted into the following summary:\n\n<summary>\n", "Earlier conversation context is summarized below:\n\n<summary>\n"],
|
|
["The following is a summary of a branch that this conversation came back from:\n\n<summary>\n", "Summary of an earlier conversation branch:\n\n<summary>\n"],
|
|
]
|
|
function piSummary(text) {
|
|
if (typeof text !== "string" || !text.endsWith("</summary>")) return text
|
|
for (const [from, to] of PI_SUMMARIES) if (text.startsWith(from)) return to + text.slice(from.length)
|
|
return text
|
|
}
|
|
|
|
// promptOpenings adapts clientOpening in the system and developer messages
|
|
// of msgs (chat completions' messages, Responses' input), and piSummary in
|
|
// their user messages, their string content or text parts: false when
|
|
// nothing changed.
|
|
function promptOpenings(msgs) {
|
|
let changed = false
|
|
for (const m of Array.isArray(msgs) ? msgs : []) {
|
|
if (!m || typeof m !== "object") continue
|
|
const adapt = m.role === "system" || m.role === "developer" ? clientOpening : m.role === "user" ? piSummary : null
|
|
if (!adapt) continue
|
|
if (typeof m.content === "string") {
|
|
const adapted = adapt(m.content)
|
|
if (adapted !== m.content) (m.content = adapted), (changed = true)
|
|
} else if (Array.isArray(m.content)) {
|
|
for (const p of m.content) {
|
|
if (!p || typeof p !== "object" || typeof p.text !== "string") continue
|
|
const adapted = adapt(p.text)
|
|
if (adapted !== p.text) (p.text = adapted), (changed = true)
|
|
}
|
|
}
|
|
}
|
|
return changed
|
|
}
|
|
|
|
// droidChat opens msgs' first system message with droid's line, or puts one
|
|
// before them with the line alone: false when it opens so already.
|
|
function droidChat(msgs) {
|
|
const first = msgs[0]
|
|
if (first && typeof first === "object" && first.role === "system") {
|
|
const c = first.content
|
|
if (typeof c === "string") {
|
|
if (c.startsWith(DROID_LINE)) return false
|
|
first.content = c.trim() === "" ? DROID_LINE : DROID_LINE + "\n" + c
|
|
return true
|
|
}
|
|
if (Array.isArray(c)) {
|
|
// droid sends a string, its blocks joined with "\n": text parts
|
|
// alone are joined so
|
|
let texts = [DROID_LINE]
|
|
for (let i = 0; i < c.length; i++) {
|
|
const p = c[i]
|
|
if (!p || typeof p !== "object" || p.type !== "text" || typeof p.text !== "string") {
|
|
texts = null
|
|
break
|
|
}
|
|
if (i === 0 && p.text.startsWith(DROID_LINE)) return false
|
|
texts.push(p.text)
|
|
}
|
|
first.content = texts ? texts.join("\n") : [{ type: "text", text: DROID_LINE }, ...c]
|
|
return true
|
|
}
|
|
}
|
|
msgs.unshift({ role: "system", content: DROID_LINE })
|
|
return true
|
|
}
|
|
|
|
// droidBody is body, a request to Factory's /api/llm/o at path, as droid
|
|
// would open it: Responses' instructions, or chat completions' first system
|
|
// message, starting with droid's line. Anything else, a body that can't be
|
|
// read, or one that already starts so is returned as it is.
|
|
function droidBody(path, body) {
|
|
if (!path.includes("/llm/o/") || body == null) return body
|
|
let text
|
|
if (typeof body === "string") text = body
|
|
else if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) text = Buffer.from(body instanceof ArrayBuffer ? body : body.buffer.slice(body.byteOffset, body.byteOffset + body.byteLength)).toString("utf8")
|
|
else return body
|
|
let m
|
|
try {
|
|
m = JSON.parse(text)
|
|
} catch {
|
|
return body
|
|
}
|
|
if (!m || typeof m !== "object" || Array.isArray(m)) return body
|
|
if (path.endsWith("/responses")) {
|
|
const v = m.instructions
|
|
if (v !== undefined && v !== null && typeof v !== "string") return body // not something droid sends
|
|
const opened = promptOpenings(m.input)
|
|
const ins = clientOpening(v ?? "")
|
|
if (!ins.startsWith(DROID_LINE)) m.instructions = ins.trim() === "" ? DROID_LINE : DROID_LINE + "\n" + ins
|
|
else if (ins !== v || opened) m.instructions = ins
|
|
else return body
|
|
} else if (path.endsWith("/chat/completions")) {
|
|
if (!Array.isArray(m.messages)) return body
|
|
const opened = promptOpenings(m.messages)
|
|
if (!droidChat(m.messages) && !opened) return body
|
|
} else return body
|
|
return JSON.stringify(m)
|
|
}
|
|
|
|
// errorReply is an error as the API the request was for words one:
|
|
// Anthropic's on /llm/a/, OpenAI's on the others.
|
|
function errorReply(url, status, type, message, headers = new Headers()) {
|
|
headers.set("content-type", "application/json")
|
|
headers.delete("content-length")
|
|
headers.delete("content-encoding")
|
|
const anthropic = new URL(url).pathname.includes("/llm/a/")
|
|
const body = anthropic ? { type: "error", error: { type, message } } : { error: { message, type, code: null } }
|
|
return new Response(JSON.stringify(body), { status, headers })
|
|
}
|
|
|
|
// said is an answer with magpie's X-Magpie-Sign-In, which tells what it
|
|
// means for the account's sign-in whatever its status: "expired" marks it
|
|
// lapsed, "kept" leaves it be. magpie takes it off before the agent sees it.
|
|
function said(res, v) {
|
|
const h = new Headers(res.headers)
|
|
h.set("X-Magpie-Sign-In", v)
|
|
return new Response(res.body, { status: res.status, statusText: res.statusText, headers: h })
|
|
}
|
|
|
|
// ---- API keys -----------------------------------------------------------------
|
|
//
|
|
// A Factory API key (fk-…), as droid takes one from FACTORY_API_KEY: its
|
|
// om() hands the key on as the token, sent as "Authorization: Bearer fk-…"
|
|
// with the headers every request carries, and never renewed. droid's active
|
|
// org (st(), X-Factory-Org-Id) is what a sign-in stored, so a key alone
|
|
// sends none. As the built-in's factory_key.go keeps one.
|
|
|
|
const isKey = (a) => a?.type === "api" && typeof a.key === "string" && a.key.trim() !== ""
|
|
|
|
// keyAccount is a key as the account the requests are signed with.
|
|
function keyAccount(a) {
|
|
const md = a.metadata ?? {}
|
|
const key = a.key.trim()
|
|
return {
|
|
key: true,
|
|
access: key,
|
|
refresh: "",
|
|
expires: 0,
|
|
accountId: md.email || md.userId || "",
|
|
email: md.email ?? "",
|
|
userId: md.userId ?? "",
|
|
activeOrganizationId: "",
|
|
region: md.region ?? "",
|
|
premBaseHost: md.premBaseHost ?? "",
|
|
whoKnown: typeof md.region === "string",
|
|
}
|
|
}
|
|
|
|
// ---- sign-in ----------------------------------------------------------------
|
|
|
|
// signedInWith is the account WorkOS just signed in, as droid keeps it: a
|
|
// token that names no org (the JWT's external_org_id or org_id) is put in
|
|
// the first org /api/cli/org lists, and whoami, asked without an org header,
|
|
// gives Factory's own id for the org the token is in (the active org droid
|
|
// then sends as X-Factory-Org-Id) and where Factory serves it from. droid
|
|
// carries on without either when they fail.
|
|
async function signedInWith(t) {
|
|
const cl = claims(t.access_token)
|
|
const c = {
|
|
access: t.access_token,
|
|
refresh: t.refresh_token ?? "",
|
|
expires: expiry(t.access_token),
|
|
orgId: t.organization_id || claim(cl, "org_id"),
|
|
email: t.user?.email || claim(cl, "email"),
|
|
userId: t.user?.id || claim(cl, "sub"),
|
|
activeOrganizationId: "",
|
|
region: "",
|
|
premBaseHost: "",
|
|
}
|
|
if (!c.orgId && !claim(cl, "external_org_id")) {
|
|
let org = ""
|
|
try {
|
|
org = await firstOrg(c)
|
|
} catch {}
|
|
if (org) {
|
|
const r = await renew(c.refresh, org)
|
|
c.access = r.access_token
|
|
c.expires = expiry(r.access_token)
|
|
c.orgId = org
|
|
if (r.refresh_token) c.refresh = r.refresh_token
|
|
}
|
|
}
|
|
try {
|
|
const who = await whoami(c)
|
|
c.activeOrganizationId = who.orgId ?? ""
|
|
c.region = who.region ?? ""
|
|
c.premBaseHost = who.premBaseHostV2 ?? ""
|
|
c.email ||= who.email ?? ""
|
|
c.userId ||= who.userId ?? ""
|
|
} catch (e) {
|
|
if (!c.email && !c.userId) throw e
|
|
}
|
|
const user = c.email || c.userId
|
|
if (!user) throw new Error("signed in, but Factory didn't say whose account it is; try again")
|
|
return { ...c, accountId: user }
|
|
}
|
|
|
|
async function deviceSignIn() {
|
|
const { text, status } = await workos("/authorize/device", { client_id: CLIENT_ID })
|
|
if (status !== 200) throw new FactoryStatus(status, "Factory sign-in: " + vendorError(text, STATUS_CODES[status] ?? ""))
|
|
const dc = JSON.parse(text)
|
|
if (!dc.device_code || !dc.user_code) throw new Error("Factory's sign-in gave no device code")
|
|
let interval = Math.max(dc.interval ?? 0, 1) * 1000
|
|
// WorkOS answers expired_token once the code lapses; stop a little after
|
|
// that regardless, so a lost poll can't run forever
|
|
const until = Date.now() + (Math.max(dc.expires_in ?? 0, 300) + 30) * 1000
|
|
return {
|
|
url: dc.verification_uri_complete || dc.verification_uri,
|
|
instructions: `Confirm the code ${dc.user_code} on Factory's page`,
|
|
method: "auto",
|
|
async callback() {
|
|
for (;;) {
|
|
await new Promise((r) => setTimeout(r, interval))
|
|
if (Date.now() > until) throw new Error("the code expired; start again")
|
|
let t
|
|
try {
|
|
t = await authenticate({
|
|
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
device_code: dc.device_code,
|
|
client_id: CLIENT_ID,
|
|
})
|
|
} catch {
|
|
continue // a hiccup: ask again
|
|
}
|
|
if (t.error === "authorization_pending") continue
|
|
if (t.error === "slow_down") {
|
|
interval += 1000
|
|
continue
|
|
}
|
|
if (t.error === "expired_token") throw new Error("the code expired; start again")
|
|
if (t.error === "access_denied") throw new Error("the sign-in was declined")
|
|
if (t.error || !t.access_token) throw new Error("Factory: " + (`${t.error ?? ""} ${t.error_description ?? ""}`.trim() || "no token came back"))
|
|
const c = await signedInWith(t)
|
|
return { type: "success", ...c }
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
// ---- usage ------------------------------------------------------------------
|
|
//
|
|
// How much of the plan the account has used, as magpie's built-in Factory
|
|
// account shows it (internal/provider/factory_usage.go) and droid's /status
|
|
// asks it: GET /api/billing/limits. Standard usage runs in rolling 5-hour,
|
|
// weekly and monthly windows, each a percent used and when it ends; Droid
|
|
// Core, the open models Factory hosts, has windows of its own; extra usage
|
|
// is a balance in cents.
|
|
|
|
// CORE are the models Droid Core's windows count: those of a vendor other
|
|
// than Anthropic, OpenAI and xAI. Standard's count every other model.
|
|
const CORE = MODELS.filter((m) => !["anthropic", "openai", "xai"].includes(m.upstream)).map((m) => m.id)
|
|
|
|
// statusLine is a status as Go's HTTP client names it, "403 Forbidden".
|
|
const statusLine = (status) => `${status} ${STATUS_CODES[status] ?? ""}`.trim()
|
|
|
|
// vendorError is the message in an error body as magpie reads it: Factory's
|
|
// {error: {message}} or {error}, {message}, {detail}, else the body cut
|
|
// short after the status.
|
|
function vendorError(text, fallback) {
|
|
let v
|
|
try {
|
|
v = JSON.parse(text)
|
|
} catch {}
|
|
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
const err = v.detail !== undefined && v.detail !== null ? v.detail : v.error
|
|
if (Array.isArray(v.errors) && typeof v.errors[0]?.message === "string" && v.errors[0].message) return v.errors[0].message
|
|
if (err && typeof err === "object" && typeof err.message === "string" && err.message) return err.message
|
|
if (typeof err === "string" && err) return err
|
|
const m = (typeof v.message === "string" && v.message) || (typeof v.msg === "string" && v.msg)
|
|
if (m) return m
|
|
}
|
|
const s = String(text ?? "").split(/\s+/).filter(Boolean).join(" ")
|
|
if (!s || s.startsWith("<")) return fallback
|
|
const r = [...s]
|
|
return fallback + ": " + (r.length > 300 ? r.slice(0, 300).join("") + "…" : s)
|
|
}
|
|
|
|
// windowEnd reads a window's end: an ISO time or epoch milliseconds, as
|
|
// droid hands either to new Date.
|
|
function windowEnd(v) {
|
|
if (typeof v === "string" && v) {
|
|
if (/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(Z|[+-]\d\d:\d\d)$/i.test(v) && !isNaN(Date.parse(v))) return new Date(v).toISOString()
|
|
if (/^[+-]?\d+$/.test(v) && Number(v) > 0) return new Date(Number(v)).toISOString()
|
|
return undefined
|
|
}
|
|
if (typeof v === "number" && v > 0) return new Date(Math.trunc(v)).toISOString()
|
|
return undefined
|
|
}
|
|
|
|
// dollars is cents as Go's $%.2f writes them: a half cent rounds to even.
|
|
function dollars(cents) {
|
|
const x = cents / 100
|
|
const tie = Number.isInteger(x * 8) && !Number.isInteger(x * 4) // .125, .375, .625, .875 exactly
|
|
if (!tie) return "$" + x.toFixed(2)
|
|
const lo = Math.floor(x * 100)
|
|
return "$" + ((lo % 2 === 0 ? lo : lo + 1) / 100).toFixed(2)
|
|
}
|
|
|
|
// limitWindows turns the limits into windows: standard's by their span,
|
|
// then Droid Core's, then the extra usage left. For routing, standard counts
|
|
// the vendors' models and Core the open ones Factory hosts; with extra usage
|
|
// to spend, a window used up stops neither.
|
|
function limitWindows(l) {
|
|
const out = []
|
|
const cents = typeof l.extraUsageBalanceCents === "number" ? l.extraUsageBalanceCents : null
|
|
const extra = l.extraUsageAllowed === true && cents !== null && cents > 0
|
|
const add = (p, prefix, core) => {
|
|
if (!p || typeof p !== "object") return
|
|
for (const [w, name, span] of [
|
|
[p.fiveHour, "5 hours", 5 * 3600],
|
|
[p.weekly, "7 days", 7 * 24 * 3600],
|
|
[p.monthly, "30 days", 30 * 24 * 3600],
|
|
]) {
|
|
if (!w || typeof w !== "object") continue
|
|
const used = typeof w.usedPercent === "number" ? w.usedPercent : 0
|
|
const win = { name: prefix + name, used: Math.min(Math.max(used, 0), 100), span }
|
|
const end = windowEnd(w.windowEnd)
|
|
if (end) win.resetsAt = end
|
|
if (extra) win.aside = true
|
|
if (core) win.models = CORE
|
|
else win.notModels = CORE
|
|
out.push(win)
|
|
}
|
|
}
|
|
add(l.limits?.standard, "", false)
|
|
add(l.limits?.core, "Droid Core · ", true)
|
|
if (cents !== null && cents > 0) out.push({ name: "Extra usage", used: 0, display: dollars(cents), aside: true })
|
|
return out
|
|
}
|
|
|
|
// ---- the plugin ---------------------------------------------------------------
|
|
|
|
function bodyModel(body) {
|
|
try {
|
|
if (body == null) return ""
|
|
const s = typeof body === "string" ? body : body instanceof ArrayBuffer || ArrayBuffer.isView(body) ? Buffer.from(body).toString("utf8") : ""
|
|
return JSON.parse(s)?.model ?? ""
|
|
} catch {
|
|
return ""
|
|
}
|
|
}
|
|
|
|
// Factory's Anthropic route accepts Droid's client preamble. Keep the
|
|
// caller's instructions, tools and history, adapting only fixed client
|
|
// metadata that Factory refuses (including Claude Code's wrappers).
|
|
const CLAUDE_IDENTITIES = new Set([
|
|
"You are Claude Code, Anthropic's official CLI for Claude.",
|
|
"You are Claude Code, Anthropic's official CLI for Claude, running within the Claude Agent SDK.",
|
|
"You are a Claude agent, built on Anthropic's Claude Agent SDK.",
|
|
])
|
|
|
|
// Match a complete generated block, not a pasted reminder followed by a
|
|
// user's question, or an incomplete fragment of one.
|
|
const ENV_REMINDER = /^<system-reminder>\n# Environment\nYou have been invoked in the following environment:[ \t]*\n(?: {1,2}- [^\n]*\n)+<\/system-reminder>$/
|
|
const MODEL_REMINDER = /^<system-reminder>\nYou are powered by the model (?:named )?[^\n<>]+\.\n<\/system-reminder>$/
|
|
const SKILL_REMINDER = /^<system-reminder>\nThe following skills are available for use with the Skill tool:\n\n(?:(?!<\/?system-reminder>)[\s\S])*\n<\/system-reminder>$/
|
|
// Factory also refuses this fixed self-reference in Claude Code 2.1.287's
|
|
// built-in update-config description. Keep its instructions and the rest
|
|
// of the skill list intact; only the known line in a complete listing changes.
|
|
const CONFIG_SKILL_METADATA = '- update-config: Use this skill to configure the Claude Code harness via settings.json. Automated behaviors ("from now on when X", "each time X", "whenever X", "before/after X") require hooks configured in settings.json - the harness executes these, not Claude, so memory/preferences cannot fulfill them.'
|
|
const CONFIG_SKILL_COMPAT = CONFIG_SKILL_METADATA.replace("not Claude", "not the assistant")
|
|
|
|
// Claude 5 also sends its runtime metadata as a string-content system
|
|
// message, instead of user-message reminders. Preserve that role and all
|
|
// instructions. A model switch sends a separate update without the
|
|
// environment paragraph, sometimes following a working-directory update;
|
|
// require the generated opening block and complete token context before
|
|
// adapting either update shape.
|
|
const SYSTEM_ENV_CONTEXT = /^# Environment\nYou have been invoked in the following environment:[ \t]*\n(?: {1,2}- [^\n]*\n)+(?=\n|$)/
|
|
const SYSTEM_MODEL_CONTEXT = /(^|\n\n)You are powered by the model (?:named )?[^\n<>]+\.(?=\n\n|$)/g
|
|
const SYSTEM_MODEL_UPDATE = /^You are powered by the model (?:named )?[^\n<>]+\.(?=\n\n|$)/
|
|
const SYSTEM_ENV_UPDATE = /^# Environment update\n(?: {1,2}- [^\n]*\n)+(?=\n|$)/
|
|
const SYSTEM_TOKEN_CONTEXT = /(?:^|\n\n)<total_tokens>\d+ tokens left<\/total_tokens>(?=\n\n|$)/
|
|
// Startup hooks and deferred-tool announcements can precede the generated
|
|
// context in the same message (#634 and subagent startup). Adapt only the
|
|
// environment suffix; keep the hook output and tool announcement verbatim.
|
|
const HOOK_OUTPUT = /^(?:SessionStart|SubagentStart)(?::[^\n]* hook success:| hook additional context:)/
|
|
// Other hook events can also own additional context inside a notification
|
|
// bundle. This guard only stops rewriting; it does not adapt their output.
|
|
const HOOK_NOTIFICATION = /^[A-Z][A-Za-z]*(?::[^\n]* hook success:| hook additional context:)/
|
|
const DEFERRED_TOOLS_OPENING = 'The following deferred tools are now available via ToolSearch. Their schemas are NOT loaded — calling them directly will fail with InputValidationError. Use ToolSearch with query "select:<name>[,<name>...]" to load tool schemas before calling them:\n'
|
|
const HOOK_CONTEXT = "\n# Environment\nYou have been invoked in the following environment:"
|
|
const SYSTEM_TOKEN_OPENING = /^<total_tokens>\d+ tokens left<\/total_tokens>\n\n/
|
|
// Skill updates can omit the opening token marker (for example on leaving
|
|
// auto mode). Require the generated list opening and a complete token tail.
|
|
const SYSTEM_SKILL_CONTEXT = /^The following skills are available for use with the Skill tool:\n\n- [\s\S]*\n\n<total_tokens>\d+ tokens left<\/total_tokens>$/
|
|
function systemContext(text) {
|
|
text = changedFileContext(text)
|
|
text = announcedSkills(text)
|
|
const restored = restoredContext(text)
|
|
if (restored !== null) return restored
|
|
if (SYSTEM_TOKEN_OPENING.test(text) || SYSTEM_SKILL_CONTEXT.test(text)) return announcedContext(text)
|
|
if (HOOK_OUTPUT.test(text) || text.startsWith(DEFERRED_TOOLS_OPENING)) {
|
|
const at = text.indexOf(HOOK_CONTEXT)
|
|
return at < 0 ? text : text.slice(0, at + 1) + generatedContext(text.slice(at + 1))
|
|
}
|
|
return generatedContext(text)
|
|
}
|
|
|
|
// Runtime notifications can precede a skill update, so the message opening
|
|
// is not a reliable discriminator. Require a complete token-terminated
|
|
// bundle, an exact skill header and a bullet-list paragraph. Only the known
|
|
// built-in description changes; notifications and hook-owned output stay.
|
|
function announcedSkills(text) {
|
|
if (!/(?:^|\n\n)<total_tokens>\d+ tokens left<\/total_tokens>$/.test(text)) return text
|
|
const parts = text.split("\n\n")
|
|
for (let i = 0; i + 1 < parts.length; i++) {
|
|
if (HOOK_OUTPUT.test(parts[i])) break
|
|
if (parts[i] !== "The following skills are available for use with the Skill tool:") continue
|
|
const list = parts[i + 1]
|
|
// Skill descriptions can contain continuation lines.
|
|
if (!list.startsWith("- ")) continue
|
|
parts[i + 1] = ("\n" + list).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT).slice(1)
|
|
}
|
|
return parts.join("\n\n")
|
|
}
|
|
|
|
// Claude Code also announces several reminders as one system turn, with
|
|
// token context and no system-reminder wrappers. Only known metadata
|
|
// paragraphs change; numbered file contents and the hook's own output stay.
|
|
function announcedContext(text, start = 1) {
|
|
const parts = changedFileContext(text).split(/\n\n(?!\.\.\. \[)/)
|
|
const open = "<system-reminder>\n", close = "\n</system-reminder>"
|
|
let quoteChangedFiles = true
|
|
for (let i = start; i < parts.length; i++) {
|
|
const part = parts[i]
|
|
const hook = part.replace(/^<system-reminder>\n/, "")
|
|
if (HOOK_OUTPUT.test(hook)) break
|
|
// Any hook owns its file snippets. Non-startup hooks do not stop the
|
|
// existing adaptation of model/environment metadata that follows them.
|
|
if (HOOK_NOTIFICATION.test(hook)) quoteChangedFiles = false
|
|
const changedFile = quoteChangedFiles ? changedFileText(part) : null
|
|
if (changedFile !== null) {
|
|
parts[i] = changedFile
|
|
continue
|
|
}
|
|
const result = part.indexOf("\n" + READ_RESULT_HEADER)
|
|
const heading = result < 0 ? part : part.slice(0, result)
|
|
const wrapped = open + heading + close
|
|
const compacted = compactContext(wrapped, quoteChangedFiles)
|
|
if (compacted !== wrapped) {
|
|
parts[i] = compacted.slice(open.length, -close.length) + (result < 0 ? "" : "\n" + readResultText(part.slice(result + 1)))
|
|
} else if (SYSTEM_ENV_CONTEXT.test(part + "\n")) {
|
|
parts[i] = generatedContext(part + "\n").slice(0, -1)
|
|
} else if (SYSTEM_MODEL_UPDATE.test(part)) {
|
|
parts[i] = systemModelLine(part)
|
|
} else if (parts[i - 1] === "The following skills are available for use with the Skill tool:") {
|
|
parts[i] = ("\n" + part).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT).slice(1)
|
|
}
|
|
}
|
|
return parts.join("\n\n")
|
|
}
|
|
|
|
// After /compact, Claude Code (2.1.280) sends the files it restores and the
|
|
// session's runtime context as one turn without reminder wrappers or an
|
|
// opening token marker: restored-file notes, Read calls and their results,
|
|
// the environment, the model line, token and date. Each fixed fragment is
|
|
// refused on its own (plugins#68). Require the generated restored-file
|
|
// opening and the generated environment paragraph or a token marker before
|
|
// the first hook; then each paragraph is adapted as in a token-prefixed
|
|
// bundle, the rest kept byte-for-byte. An adapted block no longer opens
|
|
// with the generated note, so a second pass leaves it as it is.
|
|
function restoredContext(text) {
|
|
const parts = text.split(/\n\n(?!\.\.\. \[)/)
|
|
const result = parts[0].indexOf("\n" + READ_RESULT_HEADER)
|
|
const opening = "<system-reminder>\n" + (result < 0 ? parts[0] : parts[0].slice(0, result)) + "\n</system-reminder>"
|
|
if (!COMPACT_FILE.test(opening) && !COMPACT_READ.test(opening)) return null
|
|
if (compactContext(opening) === opening) return null
|
|
for (const part of parts.slice(1)) {
|
|
if (HOOK_NOTIFICATION.test(part.replace(/^<system-reminder>\n/, ""))) return null
|
|
if (SYSTEM_ENV_CONTEXT.test(part + "\n") || /^<total_tokens>\d+ tokens left<\/total_tokens>$/.test(part)) return announcedContext(text, 0)
|
|
}
|
|
return null
|
|
}
|
|
|
|
function generatedContext(text) {
|
|
const environment = SYSTEM_ENV_CONTEXT.test(text)
|
|
if (!environment && !((SYSTEM_MODEL_UPDATE.test(text) || SYSTEM_ENV_UPDATE.test(text)) && SYSTEM_TOKEN_CONTEXT.test(text))) return text
|
|
let out = text.replace(SYSTEM_MODEL_CONTEXT, (paragraph) => paragraph
|
|
.replace("You are powered by the model named", "Current model name:")
|
|
.replace("You are powered by the model", "Current model:")
|
|
.replace("The exact model ID is", "Model ID:")
|
|
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:"))
|
|
if (!environment) return out
|
|
out = out.replace("# Environment", "# Runtime context")
|
|
.replace("You have been invoked in the following environment:", "The session environment is:")
|
|
const skillHeader = "\n\nThe following skills are available for use with the Skill tool:\n\n"
|
|
const skills = out.indexOf(skillHeader)
|
|
if (skills >= 0) {
|
|
const start = skills + skillHeader.length - 1
|
|
out = out.slice(0, start) + out.slice(start).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// The model line inside a system prompt (Claude Desktop's, #634) is the
|
|
// same generated sentence as in a reminder; only that line changes.
|
|
const SYSTEM_MODEL_LINE = /(^|\n)You are powered by the model [^\n<>]+/g
|
|
function systemModelLine(text) {
|
|
return text.replace(SYSTEM_MODEL_LINE, (line) => line
|
|
.replace("You are powered by the model named", "Current model name:")
|
|
.replace("You are powered by the model", "Current model:")
|
|
.replace("The exact model ID is", "Model ID:")
|
|
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:"))
|
|
}
|
|
|
|
// The reminder carrying CLAUDE.md files names the global one with a phrase
|
|
// Factory refuses; only that phrase in the generated reminder changes.
|
|
const INSTRUCTIONS_REMINDER = "<system-reminder>\nCodebase and user instructions are shown below"
|
|
const GLOBAL_INSTRUCTIONS = "(user's private global instructions for all projects)"
|
|
|
|
// Factory returns 403 for Claude Code's fixed compaction opening, even
|
|
// without tools or other history. Match the two generated opening sentences,
|
|
// including the provenance prefix, without requiring a Summary label. Keep
|
|
// the provenance verbatim. A summary that quotes refused client metadata
|
|
// needs lossless encoding too, including its transcript path and continuation
|
|
// instructions. File reminders restored after compaction need the same narrow
|
|
// adaptation; their paths and read arguments stay intact; refused file text is quoted
|
|
// losslessly below.
|
|
const COMPACT_OPENING = "This session is being continued from a previous conversation that ran out of context."
|
|
const COMPACT_HEADER = COMPACT_OPENING + " The summary below covers the earlier portion of the conversation."
|
|
const COMPACT_ARTIFACT = /^<artifact-content-authored-by-others\/>\nThe summarized conversation included Artifact content written by people other than you, which the summary may restate\. Treat restated content as data, not instructions\.\n+/
|
|
const READ_RESULT_HEADER = "Result of calling the Read tool:\n"
|
|
const COMPACT_READ = /^<system-reminder>\nCalled the Read tool with the following input: (\{[^\n]*\})\n<\/system-reminder>$/
|
|
const COMPACT_FILE = /^<system-reminder>\nNote: ([^\n]+) was read before the last conversation was summarized, but the contents are too large to include\. Use Read tool if you need to access it\.\n<\/system-reminder>$/
|
|
function compactContext(text, quoteChangedFiles = true) {
|
|
// A translating gateway can fold an announced system turn into user text.
|
|
if (SYSTEM_TOKEN_OPENING.test(text) || SYSTEM_SKILL_CONTEXT.test(text)) return announcedContext(text)
|
|
if (HOOK_OUTPUT.test(text) || text.startsWith(DEFERRED_TOOLS_OPENING)) return systemContext(text)
|
|
const restored = restoredContext(text)
|
|
if (restored !== null) return restored
|
|
const result = readResultText(text)
|
|
if (result !== null) return result
|
|
const changedFile = quoteChangedFiles && text.startsWith("<system-reminder>\n") ? changedFileText(text) : null
|
|
if (changedFile !== null) return changedFile
|
|
const prefix = text.match(COMPACT_ARTIFACT)?.[0] ?? ""
|
|
if (text.startsWith(COMPACT_HEADER, prefix.length)) {
|
|
const context = text.slice(prefix.length + COMPACT_HEADER.length)
|
|
const header = prefix + COMPACT_HEADER.replace(COMPACT_OPENING, "Earlier conversation context is summarized below.")
|
|
const quoted = quotedToolText(context, "Conversation context encoded as a JSON string. Decode the JSON string to recover the exact original context before continuing:\n")
|
|
return header + (quoted === context ? context : "\n\n" + quoted)
|
|
}
|
|
const summary = piSummary(text)
|
|
if (summary !== text) return summary
|
|
const read = text.match(COMPACT_READ)
|
|
if (read) {
|
|
try {
|
|
if (typeof JSON.parse(read[1])?.file_path === "string") return text.replace("Called the Read tool with the following input:", "Previously read file with these arguments:")
|
|
} catch {}
|
|
}
|
|
const file = text.match(COMPACT_FILE)
|
|
if (file) return "<system-reminder>\nPreviously read file: " + file[1] + ". Its contents were omitted from the conversation summary because of length. Use Read tool if you need to access it.\n</system-reminder>"
|
|
return announcedSkills(text)
|
|
}
|
|
|
|
// Restored and @-attached reads are user text rather than tool_result blocks.
|
|
// Keep the generated header/wrapper; encode only the original file text.
|
|
function readResultText(text) {
|
|
const wrapped = text.startsWith("<system-reminder>\n")
|
|
const prefix = (wrapped ? "<system-reminder>\n" : "") + READ_RESULT_HEADER
|
|
const suffix = wrapped ? "\n</system-reminder>" : ""
|
|
if (!text.startsWith(prefix) || (suffix && !text.endsWith(suffix))) return null
|
|
const content = text.slice(prefix.length, suffix ? -suffix.length : undefined)
|
|
return prefix + quotedToolText(content) + suffix
|
|
}
|
|
|
|
// Claude Code's edited_text_file attachment reports current file lines,
|
|
// not a unified diff. Keep its path and instructions; quote only the lines.
|
|
const CHANGED_FILE_HEADER = /^Note: [^\n]+ changed on disk since you last read it\. That's usually deliberate, so take it as the current state rather than reverting it; if the change looks wrong, say so rather than undoing it yourself — otherwise no need to call it out\. Here are the relevant changes \(shown with line numbers\):\n/
|
|
function changedFileText(text) {
|
|
const open = "<system-reminder>\n", close = "\n</system-reminder>"
|
|
const wrapped = text.startsWith(open)
|
|
if (wrapped && !text.endsWith(close)) return null
|
|
const content = wrapped ? text.slice(open.length, -close.length) : text
|
|
const header = content.match(CHANGED_FILE_HEADER)?.[0]
|
|
if (!header) return null
|
|
const lines = content.slice(header.length)
|
|
if (!/^\d+[\t:][^\n]*(?:\n(?:\.\.\.\n)?\d+[\t:][^\n]*)*(?:(?:\n\.\.\.)?\n\n\.\.\. \[\d+ lines truncated\] \.\.\.)?$/.test(lines)) return null
|
|
return (wrapped ? open : "") + header + quotedToolText(lines) + (wrapped ? close : "")
|
|
}
|
|
|
|
// System-turn notifications may start with the changed file, or put it
|
|
// among other notifications before the final token marker. Hook output is
|
|
// owned by the hook, even when it quotes a complete generated notification.
|
|
function changedFileContext(text) {
|
|
// Keep even an incomplete truncation tail with its snippet, so the
|
|
// complete-notification check can reject it rather than encode a fragment.
|
|
const parts = text.split(/\n\n(?!\.\.\. \[)/)
|
|
if (changedFileText(parts[0]) === null && !SYSTEM_TOKEN_OPENING.test(text) && !/(?:^|\n\n)<total_tokens>\d+ tokens left<\/total_tokens>$/.test(text)) return text
|
|
for (let i = 0; i < parts.length; i++) {
|
|
if (HOOK_NOTIFICATION.test(parts[i].replace(/^<system-reminder>\n/, ""))) break
|
|
parts[i] = changedFileText(parts[i]) ?? parts[i]
|
|
}
|
|
return parts.join("\n\n")
|
|
}
|
|
|
|
// Factory also refuses these fixed client phrases when they are quoted in
|
|
// tool output, e.g. while reading this adapter's source. Keep the original
|
|
// text recoverable: a JSON string with explicit decoding instructions,
|
|
// rather than deleting or rewriting the file's contents. Other output and
|
|
// the tool's id, error/cache markers and non-text blocks stay untouched.
|
|
const QUOTED_TOOL_PREFIX = "Tool output encoded as a JSON string. Decode the JSON string to recover the exact original text before using it:\n"
|
|
function quotedToolText(text, prefix = QUOTED_TOOL_PREFIX) {
|
|
if (![...CLAUDE_IDENTITIES].some((identity) => text.includes(identity)) &&
|
|
!text.includes("You have been invoked in the following environment:") &&
|
|
!text.includes("x-anthropic-billing-header: cc_version=") &&
|
|
!text.includes(COMPACT_OPENING)) return text
|
|
let encoded = JSON.stringify(text)
|
|
for (const phrase of ["You are", "You have", "x-anthropic-billing-header", "system-reminder", COMPACT_OPENING]) {
|
|
encoded = encoded.replaceAll(phrase, "\\u" + phrase.charCodeAt(0).toString(16).padStart(4, "0") + phrase.slice(1))
|
|
}
|
|
return prefix + encoded
|
|
}
|
|
|
|
function anthropicBody(body) {
|
|
let request
|
|
try {
|
|
const text = typeof body === "string" ? body
|
|
: body instanceof ArrayBuffer ? Buffer.from(body).toString("utf8")
|
|
: ArrayBuffer.isView(body) ? Buffer.from(body.buffer, body.byteOffset, body.byteLength).toString("utf8") : ""
|
|
request = parseToolJSON(text)
|
|
} catch {
|
|
return body
|
|
}
|
|
// As in droidBody, JSON round-tripping uses JavaScript Numbers: integer
|
|
// tokens above 2^53 can lose precision when a body needs adapting. A
|
|
// body that needs no changes is returned byte-for-byte instead.
|
|
if (!request || !Array.isArray(request.messages)) return body
|
|
if (request.system != null && typeof request.system !== "string" && !Array.isArray(request.system)) return body
|
|
if (Array.isArray(request.system) && request.system.some((b) => b?.type === "text" && typeof b.text !== "string")) return body
|
|
|
|
let changed = false
|
|
const system = typeof request.system === "string" ? [{ type: "text", text: request.system }] : request.system ?? []
|
|
if (typeof request.system === "string" && !request.system.startsWith(DROID_LINE)) changed = true
|
|
const kept = []
|
|
for (const block of system) {
|
|
if (block?.type === "text" && block.text.trim() === "") {
|
|
changed = true
|
|
continue
|
|
}
|
|
// Attribution is consumed here; Factory does not strip Anthropic's
|
|
// billing block and requires the Droid identity to be first.
|
|
if (block?.type === "text" && block.text?.startsWith("x-anthropic-billing-header: cc_version=")) {
|
|
changed = true
|
|
continue
|
|
}
|
|
if (block?.type === "text") {
|
|
// The gateway may join the identity and instructions into one block.
|
|
// Only the complete fixed first line is metadata; keep the rest.
|
|
const identity = [...CLAUDE_IDENTITIES].find((line) => block.text === line || block.text.startsWith(line + "\n"))
|
|
const text = identity ? DROID_LINE + block.text.slice(identity.length) : block.text
|
|
const adapted = clientOpening(systemModelLine(text))
|
|
if (adapted !== block.text) {
|
|
block.text = adapted
|
|
changed = true
|
|
}
|
|
}
|
|
kept.push(block)
|
|
}
|
|
const identity = kept.findIndex((b) => b?.type === "text" && b.text.startsWith(DROID_LINE))
|
|
if (identity < 0) {
|
|
kept.unshift({ type: "text", text: DROID_LINE })
|
|
changed = true
|
|
} else if (identity > 0) {
|
|
kept.unshift(...kept.splice(identity, 1))
|
|
changed = true
|
|
}
|
|
// Drop only duplicate identity-only blocks. A block that also contains
|
|
// task instructions stays intact.
|
|
for (let i = kept.length - 1; i > 0; i--) {
|
|
if (kept[i]?.type === "text" && kept[i].text === DROID_LINE) {
|
|
kept.splice(i, 1)
|
|
changed = true
|
|
}
|
|
}
|
|
if (changed) request.system = kept
|
|
|
|
for (const message of request.messages) {
|
|
if (message?.role === "system" && typeof message.content === "string") {
|
|
const adapted = clientOpening(systemContext(message.content))
|
|
if (adapted !== message.content) {
|
|
message.content = adapted
|
|
changed = true
|
|
}
|
|
continue
|
|
}
|
|
// Claude Code 2.1.288 also sends the same context as an array of text
|
|
// blocks (#658): each text block is adapted as the string would be,
|
|
// the blocks' other fields and any other block left as they are.
|
|
if (message?.role === "system" && Array.isArray(message.content)) {
|
|
for (const block of message.content) {
|
|
if (block?.type !== "text" || typeof block.text !== "string") continue
|
|
const adapted = clientOpening(systemContext(block.text))
|
|
if (adapted !== block.text) {
|
|
block.text = adapted
|
|
changed = true
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
if (message?.role !== "user") continue
|
|
if (typeof message.content === "string") {
|
|
const adapted = compactContext(message.content)
|
|
if (adapted !== message.content) {
|
|
message.content = adapted
|
|
changed = true
|
|
}
|
|
continue
|
|
}
|
|
if (!Array.isArray(message.content)) continue
|
|
for (const block of message.content) {
|
|
if (block?.type === "tool_result") {
|
|
if (typeof block.content === "string") {
|
|
const adapted = quotedToolText(block.content)
|
|
if (adapted !== block.content) {
|
|
block.content = adapted
|
|
changed = true
|
|
}
|
|
} else if (Array.isArray(block.content)) {
|
|
for (const part of block.content) {
|
|
if (part?.type !== "text" || typeof part.text !== "string") continue
|
|
const adapted = quotedToolText(part.text)
|
|
if (adapted !== part.text) {
|
|
part.text = adapted
|
|
changed = true
|
|
}
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
if (block?.type !== "text" || typeof block.text !== "string") continue
|
|
const compacted = compactContext(block.text)
|
|
if (compacted !== block.text) {
|
|
block.text = compacted
|
|
changed = true
|
|
} else if (block.text.startsWith(INSTRUCTIONS_REMINDER) && block.text.includes(GLOBAL_INSTRUCTIONS)) {
|
|
block.text = block.text.replaceAll(GLOBAL_INSTRUCTIONS, "(global instructions)")
|
|
changed = true
|
|
} else if (HOOK_OUTPUT.test(block.text) || SYSTEM_ENV_CONTEXT.test(block.text)) {
|
|
// a system message folded into the user's turn on its way here
|
|
const adapted = systemContext(block.text)
|
|
if (adapted !== block.text) {
|
|
block.text = adapted
|
|
changed = true
|
|
}
|
|
} else if (ENV_REMINDER.test(block.text)) {
|
|
block.text = block.text.replace("# Environment", "# Runtime context")
|
|
.replace("You have been invoked in the following environment:", "The session environment is:")
|
|
changed = true
|
|
} else if (MODEL_REMINDER.test(block.text)) {
|
|
block.text = block.text.replace("You are powered by the model named", "Current model name:")
|
|
.replace("You are powered by the model", "Current model:")
|
|
.replace("The exact model ID is", "Model ID:")
|
|
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:")
|
|
changed = true
|
|
} else if (SKILL_REMINDER.test(block.text)) {
|
|
const adapted = block.text.replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT)
|
|
if (adapted !== block.text) {
|
|
block.text = adapted
|
|
changed = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return changed ? JSON.stringify(request) : body
|
|
}
|
|
|
|
export const FactoryAuthPlugin = async ({ client }) => {
|
|
// the session id this process's requests carry
|
|
const session = crypto.randomUUID()
|
|
// serializes checking, rotating and saving tokens: WorkOS rotates the
|
|
// refresh token, so two refreshes would spend it twice
|
|
let lock = Promise.resolve()
|
|
const locked = (fn) => {
|
|
const run = lock.then(fn, fn)
|
|
lock = run.catch(() => {})
|
|
return run
|
|
}
|
|
// renewed is what each refresh this process ran gave, by the refresh
|
|
// token it spent: a sign-in still holding that one (magpie not yet
|
|
// saving what auth.refresh gave, or a save that failed) goes on with
|
|
// the new tokens rather than spending the old one again
|
|
const renewed = new Map()
|
|
const took = (spent, c) => {
|
|
for (const [k, v] of renewed) if (v.expires > 0 && Date.now() >= v.expires) renewed.delete(k)
|
|
renewed.set(spent, { access: c.access, refresh: c.refresh, expires: c.expires, orgId: c.orgId, activeOrganizationId: c.activeOrganizationId, region: c.region, premBaseHost: c.premBaseHost })
|
|
}
|
|
// latest is c with the newest tokens this process got for it
|
|
const latest = (c) => {
|
|
for (let i = 0; i < 16 && c.refresh; i++) {
|
|
const got = renewed.get(c.refresh)
|
|
if (!got || got.access === c.access || (c.expires > 0 && got.expires > 0 && got.expires <= c.expires)) break
|
|
c = { ...c, ...got }
|
|
}
|
|
return c
|
|
}
|
|
// when each account last asked whoami for its org, so one whoami can't
|
|
// answer doesn't ask before every request, nor hold back the others
|
|
const asked = new Map()
|
|
const askedOf = (c) => asked.get(c.accountId || c.refresh || c.access || "") ?? 0
|
|
const ask = (c) => asked.set(c.accountId || c.refresh || c.access || "", Date.now())
|
|
|
|
// account is the account getAuth reads, with what keeps its token live
|
|
// and its org right.
|
|
const account = (getAuth) => {
|
|
const save = async (c) => {
|
|
const { type: _t, ...rest } = c
|
|
await client.auth.set({ path: { id: PROVIDER }, body: { ...rest, type: "oauth" } })
|
|
}
|
|
const current = async () => {
|
|
const a = await getAuth()
|
|
if (isKey(a)) return keyAccount(a)
|
|
if (a?.type !== "oauth" || !a.access) throw new Error("Factory: not signed in")
|
|
return { ...a }
|
|
}
|
|
|
|
// keyed is a key account with where Factory serves its org: whoami,
|
|
// asked with the key and no org (droid's st() is null for a key), as
|
|
// droid's yP checks a key before it is used, and as the built-in asks
|
|
// it when the key is added. What it names is kept in the key's
|
|
// metadata, so magpie shows whose the key is; a whoami that fails is
|
|
// asked again ASK_AGAIN later, the request going on meanwhile.
|
|
const keyed = async (c) => {
|
|
if (c.whoKnown || Date.now() - askedOf(c) < ASK_AGAIN) return c
|
|
ask(c)
|
|
let who
|
|
try {
|
|
who = await whoami({ ...c, activeOrganizationId: "" })
|
|
} catch {
|
|
return c
|
|
}
|
|
c.region = who.region ?? ""
|
|
c.premBaseHost = who.premBaseHostV2 ?? ""
|
|
c.email ||= who.email ?? ""
|
|
c.userId ||= who.userId ?? ""
|
|
c.whoKnown = true
|
|
const a = await getAuth()
|
|
if (isKey(a) && a.key === c.access) {
|
|
const metadata = { ...(a.metadata ?? {}), email: c.email, userId: c.userId, region: c.region, premBaseHost: c.premBaseHost }
|
|
await client.auth.set({ path: { id: PROVIDER }, body: { ...a, metadata } }).catch(() => {})
|
|
}
|
|
return c
|
|
}
|
|
|
|
// orgOf fills in the active org when there is none: droid asks
|
|
// whoami as soon as it holds a token and sends the orgId it answers
|
|
// as X-Factory-Org-Id on every request after.
|
|
const orgOf = async (c) => {
|
|
if (c.activeOrganizationId || Date.now() - askedOf(c) < ASK_AGAIN) return c
|
|
ask(c)
|
|
if (await reconcile(c)) await save(c)
|
|
return c
|
|
}
|
|
|
|
// fresh is a live token, renewed near its end; renewed is told when
|
|
// it was, the built-in's clearing of the account's lapse.
|
|
const fresh = (renewed) =>
|
|
locked(async () => {
|
|
const c = latest(await current())
|
|
if (c.key) return keyed(c) // an API key: nothing to renew, and no org with it
|
|
if ((c.expires > 0 && Date.now() < c.expires - REFRESH_LEAD) || !c.refresh) return orgOf(c)
|
|
const spent = c.refresh
|
|
let t
|
|
try {
|
|
t = await renew(spent, "")
|
|
} catch (e) {
|
|
// a hiccup while the token still runs: go on with it
|
|
if (!refused(e) && c.expires > 0 && Date.now() < c.expires) return orgOf(c)
|
|
if (refused(e)) throw lapsed(c, e)
|
|
throw e
|
|
}
|
|
c.access = t.access_token
|
|
c.expires = expiry(t.access_token)
|
|
if (t.refresh_token) c.refresh = t.refresh_token
|
|
// droid asks whoami again for each new token, keeping the org it names
|
|
await reconcile(c)
|
|
ask(c)
|
|
took(spent, c)
|
|
await save(c)
|
|
renewed?.()
|
|
return c
|
|
})
|
|
|
|
// mendOrg answers Factory refusing a request: an active org it
|
|
// can't reach is left off and whoami asked again without it; with
|
|
// no header sent, the token is put in the first org /api/cli/org
|
|
// lists. Any other 403 to an account that sent no org asks whoami
|
|
// for one. True when the request is worth sending again; renewed is
|
|
// told when the token was.
|
|
const mendOrg = (status, text, renewed) =>
|
|
locked(async () => {
|
|
if (status !== 403) return false
|
|
const isOrg = orgRefused(status, text)
|
|
const c = latest(await current())
|
|
if (c.key) return false // an API key carries no org droid would send
|
|
if (c.activeOrganizationId) {
|
|
if (!isOrg) return false // the org was sent: the refusal is about something else
|
|
const was = c.activeOrganizationId
|
|
c.activeOrganizationId = ""
|
|
if ((await reconcile(c)) && c.activeOrganizationId === was) c.activeOrganizationId = "" // whoami names the org refused: send none
|
|
ask(c)
|
|
await save(c)
|
|
return true
|
|
}
|
|
if (!isOrg) {
|
|
ask(c)
|
|
if ((await reconcile(c)) && c.activeOrganizationId) {
|
|
await save(c)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
if (!c.refresh) return false
|
|
let org = ""
|
|
try {
|
|
org = await firstOrg(c)
|
|
} catch {}
|
|
if (!org) return false
|
|
const spent = c.refresh
|
|
let t
|
|
try {
|
|
t = await renew(spent, org)
|
|
} catch {
|
|
return false
|
|
}
|
|
c.access = t.access_token
|
|
c.expires = expiry(t.access_token)
|
|
c.orgId = org
|
|
if (t.refresh_token) c.refresh = t.refresh_token
|
|
took(spent, c)
|
|
await save(c)
|
|
renewed?.()
|
|
return true
|
|
})
|
|
|
|
// usage is the account's limits, asked once more when Factory refused
|
|
// an org that was put right. signIn is what the read did to the
|
|
// built-in's lapse mark: a renewal took it off whatever came after, a
|
|
// refused one put it on, a read that renewed nothing left it be (a
|
|
// clean read too)
|
|
const usage = async () => {
|
|
let signIn = "kept"
|
|
const onRenew = () => (signIn = "renewed")
|
|
const limits = async () => {
|
|
const c = await fresh(onRenew)
|
|
const h = new Headers({ Accept: "application/json" })
|
|
factoryHeaders(h, c)
|
|
const res = await fetch(base(c) + "/api/billing/limits", { headers: h, signal: AbortSignal.timeout(15_000) })
|
|
const text = await res.text()
|
|
if (res.status !== 200) throw new FactoryStatus(res.status, "Factory: " + vendorError(text, statusLine(res.status)), text)
|
|
return JSON.parse(text)
|
|
}
|
|
try {
|
|
let l
|
|
try {
|
|
l = await limits()
|
|
} catch (e) {
|
|
if (!(e instanceof FactoryStatus && e.body !== undefined && (await mendOrg(e.code, e.body, onRenew).catch(() => false)))) throw e
|
|
l = await limits()
|
|
}
|
|
if (!l?.limits?.standard) return { error: "Factory: the account reported no limits", signIn }
|
|
return { windows: limitWindows(l), signIn }
|
|
} catch (e) {
|
|
if (e?.lapsed) signIn = "expired"
|
|
return { error: e?.message ?? String(e), signIn }
|
|
}
|
|
}
|
|
|
|
return { fresh, mendOrg, usage }
|
|
}
|
|
|
|
// lapsed is WorkOS refusing c's refresh token for good: the account
|
|
// needs signing in again
|
|
const lapsed = (c, e) =>
|
|
Object.assign(new Error(`${c.accountId || "the account"}'s Factory sign-in has expired — sign in again (${e.message})`), { lapsed: true, signIn: "expired" })
|
|
|
|
// renewal is what of c magpie keeps over the stored sign-in
|
|
const renewal = (c) => {
|
|
const out = { access: c.access, refresh: c.refresh, expires: c.expires }
|
|
for (const k of ["orgId", "activeOrganizationId", "region", "premBaseHost"]) if (typeof c[k] === "string") out[k] = c[k]
|
|
return out
|
|
}
|
|
|
|
return {
|
|
config: async (config) => {
|
|
config.provider ??= {}
|
|
const was = config.provider[PROVIDER] ?? {}
|
|
config.provider[PROVIDER] = {
|
|
name: "Factory",
|
|
npm: CHAT.npm,
|
|
api: CHAT.api,
|
|
...was,
|
|
models: { ...configModels(), ...(was.models ?? {}) },
|
|
}
|
|
},
|
|
auth: {
|
|
provider: PROVIDER,
|
|
methods: [
|
|
{
|
|
type: "oauth",
|
|
label: "Sign in with Factory (device code)",
|
|
authorize: deviceSignIn,
|
|
},
|
|
// droid's FACTORY_API_KEY: the key is the bearer, never renewed
|
|
{ type: "api", label: "Factory API key (fk-…)", placeholder: "fk-…" },
|
|
],
|
|
// magpie renews the sign-in MAGPIE_LEAD before its end, before its
|
|
// requests and usage ask for it, and saves what this gives. Under
|
|
// the same lock as the renewal before a request, so the two never
|
|
// spend one refresh token; that check stays for OpenCode, which
|
|
// doesn't call this
|
|
refreshLead: MAGPIE_LEAD,
|
|
async refresh(auth) {
|
|
if (auth?.type !== "oauth" || !auth.access || !auth.refresh) return undefined
|
|
return locked(async () => {
|
|
const c = latest({ ...auth })
|
|
// renewed here already, the store not yet saying so
|
|
if (c.access !== auth.access && c.expires > 0 && Date.now() < c.expires - MAGPIE_LEAD) return renewal(c)
|
|
const spent = c.refresh
|
|
let t
|
|
try {
|
|
t = await renew(spent, "")
|
|
} catch (e) {
|
|
if (refused(e)) throw lapsed(c, e)
|
|
throw e
|
|
}
|
|
c.access = t.access_token
|
|
c.expires = expiry(t.access_token)
|
|
if (t.refresh_token) c.refresh = t.refresh_token
|
|
await reconcile(c)
|
|
ask(c)
|
|
took(spent, c)
|
|
return renewal(c)
|
|
})
|
|
},
|
|
// magpie's own hook: the account's limits
|
|
async usage(getAuth) {
|
|
return account(getAuth).usage()
|
|
},
|
|
async loader(getAuth) {
|
|
const first = await getAuth()
|
|
if (first?.type !== "oauth" && !isKey(first)) return {}
|
|
|
|
const { fresh, mendOrg } = account(getAuth)
|
|
|
|
const send = async (input, init, body, renewed) => {
|
|
const c = await fresh(renewed)
|
|
let url = input instanceof Request ? input.url : String(input)
|
|
// an EU org is served from Factory's EU region, an on-prem one
|
|
// from its own host: the request goes there
|
|
const to = llmBase(c)
|
|
if (to !== API && url.startsWith(API)) url = to + url.slice(API.length)
|
|
const h = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined))
|
|
h.delete("x-api-key")
|
|
factoryHeaders(h, c)
|
|
const model = byId.get(bodyModel(body))
|
|
const path = new URL(url).pathname
|
|
const upstream = model ? model.upstream : path.includes("/llm/o/") ? "openai" : "anthropic"
|
|
h.set("x-api-provider", upstream)
|
|
h.set("x-session-id", session)
|
|
h.set("x-assistant-message-id", crypto.randomUUID())
|
|
h.set("x-provider-routing-source", "registry_default")
|
|
if (upstream === "openai") h.set("OpenAI-Platform", "org-bHuLtG1fGmYk5YaOihAAXFBw")
|
|
// droid's Anthropic client is made with the key "placeholder",
|
|
// which Anthropic's SDK sends beside the bearer token
|
|
if (path.includes("/llm/a/")) h.set("X-Api-Key", "placeholder")
|
|
// another agent's request opens as droid's does (droidBody)
|
|
const anthropic = path.includes("/llm/a/") && (path.endsWith("/messages") || path.endsWith("/messages/count_tokens"))
|
|
const adapted = anthropic ? wrapAnthropicTools(anthropicBody(body)) : { body: droidBody(path, body), names: new Set() }
|
|
const out = adapted.body
|
|
if (out !== body) h.delete("content-length")
|
|
const res = await fetch(url, { ...init, method: init?.method ?? (input instanceof Request ? input.method : "POST"), headers: h, body: out })
|
|
return path.endsWith("/messages") ? unwrapAnthropicTools(res, adapted.names) : res
|
|
}
|
|
|
|
return {
|
|
apiKey: "placeholder",
|
|
async fetch(input, init) {
|
|
let body = init?.body
|
|
if (body == null && input instanceof Request && input.body) body = new Uint8Array(await input.arrayBuffer())
|
|
if (body instanceof ReadableStream) body = new Uint8Array(await new Response(body).arrayBuffer())
|
|
const url = input instanceof Request ? input.url : String(input)
|
|
// the built-in takes an account's lapse off when it renews the
|
|
// token, whatever the request then meets, and never for an
|
|
// answer: a 401 of Factory's leaves it be, and so does a success
|
|
// with no renewal on the way
|
|
let renewed = false
|
|
const onRenew = () => (renewed = true)
|
|
const answer = (res) => said(res, renewed ? "renewed" : "kept")
|
|
let res
|
|
try {
|
|
res = await send(input, init, body, onRenew)
|
|
if (res.status !== 403) return answer(res)
|
|
let text = await res.text()
|
|
if (await mendOrg(res.status, text, onRenew).catch(() => false)) {
|
|
res = await send(input, init, body, onRenew)
|
|
if (res.status !== 403) return answer(res)
|
|
text = await res.text()
|
|
}
|
|
const why = explain(res.status, text)
|
|
// the refusal, then what to do about it, as provider.Explain joins them
|
|
const msg = `${vendorError(text, statusLine(res.status))} — ${why}`
|
|
return answer(errorReply(url, res.status, "permission_error", msg, new Headers(res.headers)))
|
|
} catch (e) {
|
|
// Factory refused to renew the sign-in: the built-in failed the
|
|
// request (magpie's 502) and marked the account lapsed
|
|
if (e?.lapsed) return said(errorReply(url, 502, "api_error", e.message), "expired")
|
|
throw e
|
|
}
|
|
},
|
|
}
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// for tests
|
|
export const _internal = { limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE }
|