@magpie-community/opencode-factory-auth 0.1.47 → 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 (2) hide show
  1. package/index.mjs +43 -3
  2. 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
 
@@ -1797,6 +1798,32 @@ function anthropicBody(body) {
1797
1798
  return changed ? JSON.stringify(request) : body
1798
1799
  }
1799
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
+
1800
1827
  export const FactoryAuthPlugin = async ({ client, magpie }) => {
1801
1828
  // standardOnly is the user's STANDARD_ONLY as magpie has it now, read at
1802
1829
  // each usage reading so a change needs no restart; false outside magpie
@@ -1815,7 +1842,7 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
1815
1842
  }
1816
1843
  return models
1817
1844
  }
1818
- // the session id this process's requests carry
1845
+ // the session id a request that names no conversation carries
1819
1846
  const session = crypto.randomUUID()
1820
1847
  // serializes checking, rotating and saving tokens: WorkOS rotates the
1821
1848
  // refresh token, so two refreshes would spend it twice
@@ -2031,6 +2058,15 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
2031
2058
  }
2032
2059
 
2033
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
+ },
2034
2070
  // magpie's list for this provider: the same models configModels gives
2035
2071
  // OpenCode, each with the token multiplier droid shows it by, which
2036
2072
  // magpie shows beside the model as its rate
@@ -2134,6 +2170,10 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
2134
2170
  const to = llmBase(c)
2135
2171
  if (to !== API && url.startsWith(API)) url = to + url.slice(API.length)
2136
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)
2137
2177
  h.delete("x-api-key")
2138
2178
  // the AI SDK's Google package's, with OpenCode's placeholder key
2139
2179
  h.delete("x-goog-api-key")
@@ -2148,7 +2188,7 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
2148
2188
  const model = byId.get(bodyModel(body))
2149
2189
  const upstream = model ? model.upstream : path.includes("/llm/o/") ? "openai" : "anthropic"
2150
2190
  h.set("x-api-provider", upstream)
2151
- h.set("x-session-id", session)
2191
+ h.set("x-session-id", sessionId(conversation, h.get("x-magpie-subagent"), session))
2152
2192
  h.set("x-assistant-message-id", crypto.randomUUID())
2153
2193
  h.set("x-provider-routing-source", "registry_default")
2154
2194
  if (upstream === "openai") h.set("OpenAI-Platform", "org-bHuLtG1fGmYk5YaOihAAXFBw")
@@ -2231,4 +2271,4 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
2231
2271
  }
2232
2272
 
2233
2273
  // for tests
2234
- 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.47",
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",