@magpie-community/opencode-factory-auth 0.1.46 → 0.1.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.mjs +46 -3
  2. package/opencode2.mjs +25 -1
  3. package/package.json +1 -1
package/index.mjs CHANGED
@@ -4,6 +4,7 @@
4
4
  // built-in Factory account (internal/provider/factory*.go).
5
5
 
6
6
  import { STATUS_CODES } from "node:http"
7
+ import { createHash } from "node:crypto"
7
8
  import { readFileSync } from "node:fs"
8
9
  import { parseToolJSON, wrapAnthropicTools, unwrapAnthropicTools } from "./anthropic-tools.mjs"
9
10
 
@@ -119,6 +120,9 @@ const MODELS = [
119
120
  ["claude-fable-5.2", "Fable 5.2", ANTHROPIC, "anthropic", 867000, 128000, E5, true, 3.2],
120
121
  // Droid 1.7.1 adds Carbon behind Factory's Gemini4Carbon feature flag.
121
122
  ["gemini-4-carbon", "Gemini 4 Carbon", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true, 1.6],
123
+ // Droid 1.10.0 adds Hydrogen, the head of the Flash line, behind Factory's
124
+ // Gemini4Hydrogen feature flag (#1073).
125
+ ["gemini-4-hydrogen", "Gemini 4 Hydrogen", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true, 0.6],
122
126
  // The Fast Mode models come last: magpie shows a provider with no models
123
127
  // picked only the first 24 of its list, and placed beside their base
124
128
  // models in 0.1.27 they pushed GPT-5.3-Codex, Grok 4.7, Grok 4.6, GLM-5.3
@@ -1794,6 +1798,32 @@ function anthropicBody(body) {
1794
1798
  return changed ? JSON.stringify(request) : body
1795
1799
  }
1796
1800
 
1801
+ // CONVERSATION is the header the chat.headers hook below names the
1802
+ // conversation in, for the loader's fetch to read and take off: it never
1803
+ // reaches Factory.
1804
+ const CONVERSATION = "x-magpie-factory-conversation"
1805
+
1806
+ // sessionId is the x-session-id droid sends for a conversation: droid opens
1807
+ // one session per conversation, and a subagent's Task its own, so every
1808
+ // request of one conversation says the same id and no two conversations
1809
+ // share one. A UUID, as droid's are, made of a hash of the conversation
1810
+ // magpie names (its session, else its first message's hash) and the
1811
+ // subagent; fallback, this process's own, when magpie names none.
1812
+ // One id for every request of the process put every conversation, model
1813
+ // and subagent in one Factory session, which droid never does, and
1814
+ // Factory's Gemini route answered a Gemini 4 model in such a session now
1815
+ // and then with 400 "Invalid model ID in request body" (marovole on X).
1816
+ function sessionId(conversation, subagent, fallback) {
1817
+ conversation = String(conversation ?? "").trim().slice(0, 128)
1818
+ if (!conversation) return fallback
1819
+ subagent = String(subagent ?? "").trim().slice(0, 128)
1820
+ const b = createHash("sha256").update("factory-session\0" + conversation + "\0" + subagent).digest().subarray(0, 16)
1821
+ b[6] = (b[6] & 0x0f) | 0x40
1822
+ b[8] = (b[8] & 0x3f) | 0x80
1823
+ const x = b.toString("hex")
1824
+ return `${x.slice(0, 8)}-${x.slice(8, 12)}-${x.slice(12, 16)}-${x.slice(16, 20)}-${x.slice(20)}`
1825
+ }
1826
+
1797
1827
  export const FactoryAuthPlugin = async ({ client, magpie }) => {
1798
1828
  // standardOnly is the user's STANDARD_ONLY as magpie has it now, read at
1799
1829
  // each usage reading so a change needs no restart; false outside magpie
@@ -1812,7 +1842,7 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
1812
1842
  }
1813
1843
  return models
1814
1844
  }
1815
- // the session id this process's requests carry
1845
+ // the session id a request that names no conversation carries
1816
1846
  const session = crypto.randomUUID()
1817
1847
  // serializes checking, rotating and saving tokens: WorkOS rotates the
1818
1848
  // refresh token, so two refreshes would spend it twice
@@ -2028,6 +2058,15 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
2028
2058
  }
2029
2059
 
2030
2060
  return {
2061
+ // the conversation a request is of, as magpie tells the hook (its
2062
+ // session, else its first message's hash), for the loader's fetch to
2063
+ // name its x-session-id by (sessionId). Every plugin's hook sees every
2064
+ // provider's requests, so it goes on Factory's alone.
2065
+ "chat.headers": async (input, output) => {
2066
+ const id = input?.model?.providerID ?? input?.provider?.info?.id
2067
+ if (id !== PROVIDER || !input?.sessionID || !output?.headers) return
2068
+ output.headers[CONVERSATION] = String(input.sessionID).slice(0, 128)
2069
+ },
2031
2070
  // magpie's list for this provider: the same models configModels gives
2032
2071
  // OpenCode, each with the token multiplier droid shows it by, which
2033
2072
  // magpie shows beside the model as its rate
@@ -2131,6 +2170,10 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
2131
2170
  const to = llmBase(c)
2132
2171
  if (to !== API && url.startsWith(API)) url = to + url.slice(API.length)
2133
2172
  const h = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined))
2173
+ // the conversation the hook named, else the session magpie or
2174
+ // OpenCode 2 (which has no chat.headers) names on the request
2175
+ const conversation = h.get(CONVERSATION) || h.get("x-magpie-session") || h.get("x-opencode-session")
2176
+ h.delete(CONVERSATION)
2134
2177
  h.delete("x-api-key")
2135
2178
  // the AI SDK's Google package's, with OpenCode's placeholder key
2136
2179
  h.delete("x-goog-api-key")
@@ -2145,7 +2188,7 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
2145
2188
  const model = byId.get(bodyModel(body))
2146
2189
  const upstream = model ? model.upstream : path.includes("/llm/o/") ? "openai" : "anthropic"
2147
2190
  h.set("x-api-provider", upstream)
2148
- h.set("x-session-id", session)
2191
+ h.set("x-session-id", sessionId(conversation, h.get("x-magpie-subagent"), session))
2149
2192
  h.set("x-assistant-message-id", crypto.randomUUID())
2150
2193
  h.set("x-provider-routing-source", "registry_default")
2151
2194
  if (upstream === "openai") h.set("OpenAI-Platform", "org-bHuLtG1fGmYk5YaOihAAXFBw")
@@ -2228,4 +2271,4 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
2228
2271
  }
2229
2272
 
2230
2273
  // for tests
2231
- export const _internal = { STANDARD_ONLY, contentRefused, explain, fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT, MODELS, PROMOTIONS, rateOf, configModels, providerModel, providerModels }
2274
+ export const _internal = { CONVERSATION, sessionId, STANDARD_ONLY, contentRefused, explain, fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT, MODELS, PROMOTIONS, rateOf, configModels, providerModel, providerModels }
package/opencode2.mjs CHANGED
@@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) {
575
575
  }
576
576
  } catch {}
577
577
  })()
578
- void refresh()
578
+ // A plugin still reading its list in the background when the first read
579
+ // returned (agy-cli: `agy models` and a session probe take seconds, and
580
+ // the first read gets its static list) says so through
581
+ // provider.warming(): the list is read once more when that ends, once,
582
+ // and no later than a minute on.
583
+ const warmed = async () => {
584
+ let w
585
+ try {
586
+ w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null
587
+ } catch {
588
+ return
589
+ }
590
+ if (!w || typeof w.then !== "function") return
591
+ let timer
592
+ await Promise.race([
593
+ Promise.resolve(w).catch(() => {}),
594
+ new Promise((r) => {
595
+ timer = setTimeout(r, 60_000)
596
+ timer.unref?.()
597
+ }),
598
+ ])
599
+ clearTimeout(timer)
600
+ if (live.on) await refresh()
601
+ }
602
+ void refresh().then(warmed, () => {})
579
603
  }
580
604
 
581
605
  export async function setup(ctx, mod = v1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.46",
3
+ "version": "0.1.48",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",