@magpie-community/opencode-factory-auth 0.1.25 → 0.1.27

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/README.md +29 -6
  2. package/index.mjs +239 -12
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -63,19 +63,42 @@ Each model is served on the one API droid uses for it:
63
63
 
64
64
  | API | Models |
65
65
  |---|---|
66
- | Anthropic Messages (`/api/llm/a`) | Fable 5.1, Fable 5, Opus 5.5, Opus 5, Opus 4.8, Sonnet 5.5, Sonnet 5, Sonnet 4.6, Haiku 5.5, Haiku 4.5, MiniMax M2.7 |
67
- | OpenAI Responses (`/api/llm/o/v1`) | GPT-6.1 Sol, GPT-6 Sol/Astra/Luna, GPT-5.6 Sol/Terra/Luna, GPT-5.5, GPT-5.4, GPT-5.3-Codex, Grok 4.7, Grok 4.6 |
66
+ | Anthropic Messages (`/api/llm/a`) | Fable 5.1, Fable 5, Opus 5.5, Opus 5, Opus 4.8 (each also in Fast Mode), Sonnet 5.5, Sonnet 5, Sonnet 4.6, Haiku 5.5, Haiku 4.5, MiniMax M2.7 |
67
+ | OpenAI Responses (`/api/llm/o/v1`) | GPT-6.1 Sol, GPT-6 Sol/Astra/Luna, GPT-5.6 Sol/Terra/Luna, GPT-5.5, GPT-5.4, GPT-5.3-Codex (each also in Fast Mode), Grok 4.7, Grok 4.6 |
68
68
  | Chat completions (`/api/llm/o/v1`) | GLM-5.3, GLM-5.3-Flash, GLM-5.2, Kimi K3, DeepSeek V4.1 Flash, Qwen3.8 Max, MiniMax M3, Mistral Large 4, Mistral Medium 3.5, Nemotron 3 Ultra |
69
+ | Gemini generate (`/api/llm/g/v1/generate`) | Gemini 4 Argon, Gemini 3.1 Pro, Gemini 3.8/3.7/3.6/3.5 Flash, Gemini 3 Flash |
69
70
 
70
71
  Reasoning efforts are the variants droid offers for each model. DeepSeek V4.1
71
72
  Flash, Kimi K3 and GLM-5.2 have `none`, droid's "off": they stop thinking.
72
73
  Reasoning is mandatory on GLM-5.3 and GLM-5.3-Flash (Fireworks turns `none`
73
74
  away), so `low` is their least.
74
75
 
75
- The list is not included:
76
-
77
- - Gemini, which Factory sends on a route of its own.
78
- - auto, which droid picks on the client side.
76
+ Gemini's requests go to Factory's generate route with the model in the
77
+ body, droid's line opening the system instruction. Gemini 4 Argon is behind
78
+ a Factory feature flag in droid 0.241.1, so an account it isn't on for may
79
+ be refused it.
80
+
81
+ A Fast Mode model (`claude-opus-5-5-fast`, `gpt-5.5-fast`, …) is its base
82
+ model served faster, for more of the plan's credits. It goes out as droid
83
+ sends it: its own id, with `speed: "fast"` and the `fast-mode-2026-02-01`
84
+ beta on Anthropic's API, at `service_tier: "priority"` on OpenAI's. A request
85
+ that asks the base model for fast (Claude Code's `speed: "fast"`, a
86
+ Responses request's `service_tier: "priority"`) is sent to its Fast Mode
87
+ model, as droid's own fast toggle does: asked of the base model, Factory
88
+ answered at standard speed (#119). Opus 4.6/4.7 Fast, GPT-5.4 Mini Fast, GPT-5.6 Sol Fast (unreleased)
89
+ and GLM-5.2 Fast (behind a feature flag) are not included.
90
+
91
+ auto is not included: droid picks it on the client side.
92
+
93
+ ## Requests too large
94
+
95
+ Factory refuses a request body over its size limit with 413 "Request
96
+ Entity Too Large". The limit is on bytes, not on the model's context, and
97
+ images reach it first. On a 413 the plugin sends the request once more with
98
+ every image and file but the newest replaced by a line saying it was left
99
+ out, each tool result kept beside its call, as droid does. If that is still
100
+ too large, or there was nothing to leave out, the error says it is
101
+ Factory's byte limit: compact the conversation or start a new one.
79
102
 
80
103
  ## Use
81
104
 
package/index.mjs CHANGED
@@ -27,24 +27,38 @@ const ASK_AGAIN = 10 * 60 * 1000
27
27
  const ANTHROPIC = { npm: "@ai-sdk/anthropic", api: API + "/api/llm/a/v1" }
28
28
  const RESPONSES = { npm: "@ai-sdk/openai", api: API + "/api/llm/o/v1" }
29
29
  const CHAT = { npm: "@ai-sdk/openai-compatible", api: API + "/api/llm/o/v1" }
30
+ // Gemini's go to Factory's /api/llm/g/v1/generate, the model in the body
31
+ // (geminiBody); the AI SDK's Google package asks for :streamGenerateContent
32
+ // under models/, which send() takes there
33
+ const GEMINI = { npm: "@ai-sdk/google", api: API + "/api/llm/g/v1" }
30
34
 
31
35
  const E5 = ["low", "medium", "high", "xhigh", "max"]
32
36
  const E6 = ["none", "low", "medium", "high", "xhigh", "max"]
33
37
  const E4 = ["low", "medium", "high", "xhigh"]
34
38
 
35
- // The models droid's /model picker offers, less Gemini's (sent on a route of
36
- // Factory's own) and auto (droid picks it client side): id, name, the wire
37
- // droid sends it on, the vendor it names in x-api-provider, context, output,
38
- // reasoning efforts, images. Haiku 5.5 and Mistral Large 4 are droid
39
- // 0.240.0's (models.test.mjs reads its registry); GPT-6 Astra Ultra Fast,
40
- // new there too, stays out: droid offers it only after the user accepts
41
- // Factory's high-cost terms for it (explicitOptInRequirement).
39
+ // The models droid's /model picker offers, less auto (droid picks it client
40
+ // side): id, name, the wire droid sends it on, the vendor it names in
41
+ // x-api-provider, context, output, reasoning efforts, images. Haiku 5.5 and
42
+ // Mistral Large 4 are droid 0.240.0's (models.test.mjs reads its registry);
43
+ // GPT-6 Astra Ultra Fast, new there too, stays out: droid offers it only
44
+ // after the user accepts Factory's high-cost terms for it
45
+ // (explicitOptInRequirement). Gemini's are droid 0.241.1's, as magpie's
46
+ // built-in has them (yetone/magpie#2080); Gemini 4 Argon is behind a
47
+ // feature flag of Factory's there, so an account it isn't on for may be
48
+ // refused it. The Fast Mode models are droid 0.240.0's (fastBody; testdata/
49
+ // droid-0.240.0-fast.txt), at their base model's limits and efforts as
50
+ // droid has them. Left out: Opus 4.6/4.7 Fast and GPT-5.4 Mini Fast (their
51
+ // base models aren't offered), GPT-5.6 Sol Fast (unreleased:!0) and
52
+ // GLM-5.2 Fast (behind a feature flag, on a Baseten model id of its own).
42
53
  const MODELS = [
43
54
  ["claude-fable-5.1", "Fable 5.1", ANTHROPIC, "anthropic", 867000, 128000, E5, true],
44
55
  ["claude-fable-5", "Fable 5", ANTHROPIC, "anthropic", 867000, 128000, E5, true],
45
56
  ["claude-opus-5-5", "Opus 5.5", ANTHROPIC, "anthropic", 872000, 128000, E5, true],
57
+ ["claude-opus-5-5-fast", "Opus 5.5 Fast Mode", ANTHROPIC, "anthropic", 872000, 128000, E5, true],
46
58
  ["claude-opus-5", "Opus 5", ANTHROPIC, "anthropic", 867000, 128000, E5, true],
59
+ ["claude-opus-5-fast", "Opus 5 Fast Mode", ANTHROPIC, "anthropic", 867000, 128000, E5, true],
47
60
  ["claude-opus-4-8", "Opus 4.8", ANTHROPIC, "anthropic", 867000, 128000, E5, true],
61
+ ["claude-opus-4-8-fast", "Opus 4.8 Fast Mode", ANTHROPIC, "anthropic", 867000, 128000, E5, true],
48
62
  ["claude-sonnet-5-5", "Sonnet 5.5", ANTHROPIC, "anthropic", 872000, 128000, E5, true],
49
63
  ["claude-sonnet-5", "Sonnet 5", ANTHROPIC, "anthropic", 872000, 128000, E5, true],
50
64
  ["claude-sonnet-4-6", "Sonnet 4.6", ANTHROPIC, "anthropic", 931000, 64000, ["low", "medium", "high", "max"], true],
@@ -58,8 +72,11 @@ const MODELS = [
58
72
  ["gpt-5.6-terra", "GPT-5.6 Terra", RESPONSES, "openai", 1050000, 128000, E6, true],
59
73
  ["gpt-5.6-luna", "GPT-5.6 Luna", RESPONSES, "openai", 1050000, 128000, E6, true],
60
74
  ["gpt-5.5", "GPT-5.5", RESPONSES, "openai", 1050000, 128000, E4, true],
75
+ ["gpt-5.5-fast", "GPT-5.5 Fast Mode", RESPONSES, "openai", 1050000, 128000, E4, true],
61
76
  ["gpt-5.4", "GPT-5.4", RESPONSES, "openai", 1050000, 128000, E4, true],
77
+ ["gpt-5.4-fast", "GPT-5.4 Fast Mode", RESPONSES, "openai", 1050000, 128000, E4, true],
62
78
  ["gpt-5.3-codex", "GPT-5.3-Codex", RESPONSES, "openai", 400000, 128000, E4, true],
79
+ ["gpt-5.3-codex-fast", "GPT-5.3-Codex Fast Mode", RESPONSES, "openai", 400000, 128000, E4, true],
63
80
  ["grok-4.7", "Grok 4.7", RESPONSES, "xai", 500000, 63356, E4, true],
64
81
  ["grok-4.6", "Grok 4.6", RESPONSES, "xai", 200000, 63356, E4, true],
65
82
  // reasoning is mandatory on GLM-5.3 and GLM-5.3-Flash (Fireworks turns
@@ -76,13 +93,61 @@ const MODELS = [
76
93
  ["mistral-large-4", "Mistral Large 4", CHAT, "mistral", 524288, 64000, ["high"], true],
77
94
  ["mistral-medium-3.5", "Mistral Medium 3.5", CHAT, "mistral", 256000, 64000, ["high"], true],
78
95
  ["nemotron-3-ultra", "Nemotron 3 Ultra", CHAT, "baseten", 202000, 65536, ["high"], false],
96
+ ["gemini-4-argon", "Gemini 4 Argon", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true],
97
+ ["gemini-3.1-pro-preview", "Gemini 3.1 Pro", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true],
98
+ ["gemini-3.8-flash", "Gemini 3.8 Flash", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true],
99
+ ["gemini-3.7-flash", "Gemini 3.7 Flash", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true],
100
+ ["gemini-3.6-flash", "Gemini 3.6 Flash", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true],
101
+ ["gemini-3.5-flash", "Gemini 3.5 Flash", GEMINI, "google", 1000000, 65536, ["minimal", "low", "medium", "high"], true],
102
+ ["gemini-3-flash-preview", "Gemini 3 Flash", GEMINI, "google", 1000000, 65536, ["minimal", "low", "medium", "high"], true],
79
103
  ].map(([id, name, wire, upstream, context, output, efforts, images]) => ({ id, name, wire, upstream, context, output, efforts, images }))
80
104
 
81
105
  const byId = new Map(MODELS.map((m) => [m.id, m]))
82
106
 
107
+ // A Fast Mode model is its base model served faster for more credits.
108
+ // droid sends the Fast Mode id itself as the model; on Anthropic's wire with
109
+ // speed "fast" and the fast-mode beta (anthropicFastMode), on OpenAI's with
110
+ // service_tier "priority" (its apiRequest). FAST_OF is a base model's Fast
111
+ // Mode id.
112
+ const FAST_BETA = "fast-mode-2026-02-01"
113
+ const FAST_OF = new Map(MODELS.filter((m) => m.id.endsWith("-fast")).map((m) => [m.id.slice(0, -"-fast".length), m.id]))
114
+
115
+ // fastBody sends a Fast Mode model as droid does. A request that asks its
116
+ // base model for fast (Claude Code's speed "fast", or service_tier
117
+ // "priority" on Responses) goes to the base model's Fast Mode model, as
118
+ // droid's own fast toggle switches to it: asked of the base model, Factory
119
+ // answered at standard speed, fast asked and standard served with nothing
120
+ // said (#119). Beta is
121
+ // whether the request needs the fast-mode beta.
122
+ function fastBody(path, body) {
123
+ const anthropic = path.includes("/llm/a/") && path.endsWith("/messages")
124
+ const responses = path.includes("/llm/o/") && path.endsWith("/responses")
125
+ if (!anthropic && !responses) return { body, beta: false }
126
+ const text = bodyText(body)
127
+ if (text == null) return { body, beta: false }
128
+ let m
129
+ try {
130
+ m = JSON.parse(text)
131
+ } catch {
132
+ return { body, beta: false }
133
+ }
134
+ if (!m || typeof m !== "object" || Array.isArray(m) || typeof m.model !== "string") return { body, beta: false }
135
+ const asked = anthropic ? m.speed === "fast" : m.service_tier === "priority"
136
+ const model = asked && FAST_OF.has(m.model) ? FAST_OF.get(m.model) : m.model
137
+ const fast = byId.get(model)
138
+ if (!fast || !model.endsWith("-fast") || fast.wire !== (anthropic ? ANTHROPIC : RESPONSES)) return { body, beta: false }
139
+ if (anthropic && model === m.model && m.speed === "fast") return { body, beta: true }
140
+ if (responses && model === m.model && m.service_tier === "priority") return { body, beta: false }
141
+ m.model = model
142
+ if (anthropic) m.speed = "fast"
143
+ else m.service_tier = "priority"
144
+ return { body: JSON.stringify(m), beta: anthropic }
145
+ }
146
+
83
147
  // variant is what the AI SDK package a model is on is given for an effort.
84
148
  function variant(npm, effort) {
85
149
  if (npm === ANTHROPIC.npm) return { effort }
150
+ if (npm === GEMINI.npm) return { thinkingConfig: { includeThoughts: true, thinkingLevel: effort } }
86
151
  return { reasoningEffort: effort }
87
152
  }
88
153
 
@@ -400,6 +465,136 @@ function droidBody(path, body) {
400
465
  return JSON.stringify(m)
401
466
  }
402
467
 
468
+ // bodyText is a request body as text, or null for one that isn't.
469
+ function bodyText(body) {
470
+ if (typeof body === "string") return body
471
+ if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) return Buffer.from(body instanceof ArrayBuffer ? body : body.buffer.slice(body.byteOffset, body.byteOffset + body.byteLength)).toString("utf8")
472
+ return null
473
+ }
474
+
475
+ // GEMINI_CALL is the AI SDK Google package's request path, the model in it.
476
+ const GEMINI_CALL = /\/models\/([^/:]+):(?:stream)?generateContent$/i
477
+
478
+ // geminiBody is body, a Gemini generateContent request for model, as
479
+ // Factory's /api/llm/g/v1/generate takes it, as magpie's built-in sends it
480
+ // (buildGemini, factoryDroidGoogle): the model in the body, the system
481
+ // instruction without a role and opening with droid's line, as one text part
482
+ // of droid's line and the instruction's text parts joined with "\n", then
483
+ // its other parts. A body that can't be read goes on as it is.
484
+ function geminiBody(model, body) {
485
+ const text = bodyText(body)
486
+ if (text == null) return body
487
+ let m
488
+ try {
489
+ m = JSON.parse(text)
490
+ } catch {
491
+ return body
492
+ }
493
+ if (!m || typeof m !== "object" || Array.isArray(m)) return body
494
+ m.model = model
495
+ delete m.stream
496
+ let si = m.systemInstruction
497
+ if (typeof si === "string") si = { parts: [{ text: si }] }
498
+ if (!si || typeof si !== "object") si = { parts: [] }
499
+ delete si.role
500
+ const parts = Array.isArray(si.parts) ? si.parts : []
501
+ if (!(typeof parts[0]?.text === "string" && parts[0].text.startsWith(DROID_LINE))) {
502
+ const texts = []
503
+ const rest = []
504
+ for (const p of parts) {
505
+ if (p && typeof p === "object" && typeof p.text === "string" && Object.keys(p).length === 1) texts.push(p.text)
506
+ else rest.push(p)
507
+ }
508
+ const joined = texts.join("\n")
509
+ si.parts = [{ text: joined.trim() === "" ? DROID_LINE : DROID_LINE + "\n" + joined }, ...rest]
510
+ }
511
+ m.systemInstruction = si
512
+ return JSON.stringify(m)
513
+ }
514
+
515
+ // ---- request size -------------------------------------------------------------
516
+ //
517
+ // Factory answers a request body over its size limit with 413 "Request
518
+ // Entity Too Large": a limit on bytes, not on the model's context, which
519
+ // the images an agent's session gathers reach long before its tokens do
520
+ // (yetone/magpie#2082: nine screenshots, ~4.5 MB of data URLs, on Opus
521
+ // 5.5). droid 0.239.0 leaves older images and PDFs out of such a request.
522
+ // The plugin does the same once, on a 413: every image and file but the
523
+ // newest KEEP_ATTACHMENTS becomes a line saying it was left out, in place,
524
+ // so each tool result stays beside its call. Failing that, the 413 says
525
+ // what it is and what to do.
526
+
527
+ const KEEP_ATTACHMENTS = 1
528
+ const LEFT_OUT = "[An earlier image or file was left out of this request: it was over Factory's request size limit.]"
529
+ // what magpie's gateway adds to a 413 (internal/gateway/gateway.go,
530
+ // writeError), said here too for OpenCode; the gateway, finding it, adds
531
+ // it no second time
532
+ const TOO_LARGE_HINT = "This is a request-body byte limit, not the model's context: compact the conversation (/compact in Codex or Claude Code), or start a new one with a short summary; fewer images or smaller tool results also help."
533
+
534
+ // dialectOf is which API's body a request to path carries, as attachments
535
+ // are found in it, or "" for one that has none to leave out.
536
+ function dialectOf(path) {
537
+ if (path.includes("/llm/a/") && path.endsWith("/messages")) return "anthropic"
538
+ if (path.endsWith("/responses")) return "responses"
539
+ if (path.endsWith("/chat/completions")) return "chat"
540
+ if (GEMINI_CALL.test(path) || path.endsWith("/llm/g/v1/generate")) return "gemini"
541
+ return ""
542
+ }
543
+
544
+ const DIALECTS = {
545
+ anthropic: { list: "messages", is: (p) => p.type === "image" || p.type === "document", text: (p) => ({ type: "text", text: LEFT_OUT, ...(p.cache_control ? { cache_control: p.cache_control } : {}) }) },
546
+ responses: { list: "input", is: (p) => p.type === "input_image" || p.type === "input_file", text: () => ({ type: "input_text", text: LEFT_OUT }) },
547
+ chat: { list: "messages", is: (p) => p.type === "image_url" || p.type === "file", text: () => ({ type: "text", text: LEFT_OUT }) },
548
+ gemini: { list: "contents", is: (p) => !!(p.inlineData || p.inline_data || p.fileData || p.file_data), text: () => ({ text: LEFT_OUT }) },
549
+ }
550
+
551
+ // leaveOutAttachments is body, a request to path, with every image and file
552
+ // but the newest KEEP_ATTACHMENTS left out, and how many were: null when
553
+ // there are none to leave out or the body can't be read.
554
+ function leaveOutAttachments(path, body) {
555
+ const d = DIALECTS[dialectOf(path)]
556
+ const text = d && bodyText(body)
557
+ if (!text) return null
558
+ let m
559
+ try {
560
+ m = JSON.parse(text)
561
+ } catch {
562
+ return null
563
+ }
564
+ if (!Array.isArray(m?.[d.list])) return null
565
+ // in the order they were sent: the array each is in, its place, and
566
+ // whether the array is a Gemini function response's parts, which take
567
+ // inline data alone
568
+ const found = []
569
+ const walk = (v, key) => {
570
+ if (Array.isArray(v)) {
571
+ v.forEach((p, i) => {
572
+ if (p && typeof p === "object" && !Array.isArray(p) && d.is(p)) found.push({ list: v, i, drop: key === "functionResponse" })
573
+ else walk(p, key)
574
+ })
575
+ } else if (v && typeof v === "object") {
576
+ for (const [k, x] of Object.entries(v)) walk(x, k === "functionResponse" ? k : key)
577
+ }
578
+ }
579
+ walk(m[d.list], "")
580
+ const out = found.slice(0, Math.max(found.length - KEEP_ATTACHMENTS, 0))
581
+ if (out.length === 0) return null
582
+ // from the last, so a part dropped doesn't move the places before it
583
+ for (const { list, i, drop } of out.reverse()) {
584
+ if (drop) list.splice(i, 1)
585
+ else list[i] = d.text(list[i])
586
+ }
587
+ return { body: JSON.stringify(m), left: out.length }
588
+ }
589
+
590
+ // tooLarge is what a 413 of Factory's says to the agent: Factory's own
591
+ // words, that the limit is on bytes, what the plugin left out, and the hint.
592
+ function tooLarge(text, left) {
593
+ const said = vendorError(text, "413")
594
+ const tried = left > 0 ? ` It was still too large with ${left} older image${left === 1 ? "" : "s"} or file${left === 1 ? "" : "s"} left out.` : ""
595
+ return `Factory refused the request as too large (${said}): it limits the size of a request's body in bytes.${tried} ${TOO_LARGE_HINT}`
596
+ }
597
+
403
598
  // errorReply is an error as the API the request was for words one:
404
599
  // Anthropic's on /llm/a/, OpenAI's on the others.
405
600
  function errorReply(url, status, type, message, headers = new Headers()) {
@@ -550,8 +745,8 @@ async function deviceSignIn() {
550
745
  // is a balance in cents.
551
746
 
552
747
  // CORE are the models Droid Core's windows count: those of a vendor other
553
- // than Anthropic, OpenAI and xAI. Standard's count every other model.
554
- const CORE = MODELS.filter((m) => !["anthropic", "openai", "xai"].includes(m.upstream)).map((m) => m.id)
748
+ // than Anthropic, OpenAI, xAI and Google. Standard's count every other model.
749
+ const CORE = MODELS.filter((m) => !["anthropic", "openai", "xai", "google"].includes(m.upstream)).map((m) => m.id)
555
750
 
556
751
  // statusLine is a status as Go's HTTP client names it, "403 Forbidden".
557
752
  const statusLine = (status) => `${status} ${STATUS_CODES[status] ?? ""}`.trim()
@@ -1432,15 +1627,32 @@ export const FactoryAuthPlugin = async ({ client }) => {
1432
1627
  const send = async (input, init, body, renewed) => {
1433
1628
  const c = await fresh(renewed)
1434
1629
  let url = input instanceof Request ? input.url : String(input)
1630
+ // Gemini's: Factory's generate route, the model in the body
1631
+ const call = GEMINI_CALL.exec(new URL(url).pathname)
1632
+ if (call) {
1633
+ const u = new URL(url)
1634
+ u.pathname = u.pathname.replace(GEMINI_CALL, "/generate")
1635
+ u.search = ""
1636
+ url = u.toString()
1637
+ body = geminiBody(decodeURIComponent(call[1]), body)
1638
+ }
1435
1639
  // an EU org is served from Factory's EU region, an on-prem one
1436
1640
  // from its own host: the request goes there
1437
1641
  const to = llmBase(c)
1438
1642
  if (to !== API && url.startsWith(API)) url = to + url.slice(API.length)
1439
1643
  const h = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined))
1440
1644
  h.delete("x-api-key")
1645
+ // the AI SDK's Google package's, with OpenCode's placeholder key
1646
+ h.delete("x-goog-api-key")
1441
1647
  factoryHeaders(h, c)
1442
- const model = byId.get(bodyModel(body))
1443
1648
  const path = new URL(url).pathname
1649
+ const sped = fastBody(path, body)
1650
+ body = sped.body
1651
+ if (sped.beta) {
1652
+ const betas = (h.get("anthropic-beta") ?? "").split(",").map((b) => b.trim()).filter(Boolean)
1653
+ if (!betas.includes(FAST_BETA)) h.set("anthropic-beta", [...betas, FAST_BETA].join(","))
1654
+ }
1655
+ const model = byId.get(bodyModel(body))
1444
1656
  const upstream = model ? model.upstream : path.includes("/llm/o/") ? "openai" : "anthropic"
1445
1657
  h.set("x-api-provider", upstream)
1446
1658
  h.set("x-session-id", session)
@@ -1454,7 +1666,8 @@ export const FactoryAuthPlugin = async ({ client }) => {
1454
1666
  const anthropic = path.includes("/llm/a/") && (path.endsWith("/messages") || path.endsWith("/messages/count_tokens"))
1455
1667
  const adapted = anthropic ? wrapAnthropicTools(anthropicBody(body)) : { body: droidBody(path, body), names: new Set() }
1456
1668
  const out = adapted.body
1457
- if (out !== body) h.delete("content-length")
1669
+ // the caller's length is of the body it sent, not of this one
1670
+ if (out !== init?.body) h.delete("content-length")
1458
1671
  const res = await fetch(url, { ...init, method: init?.method ?? (input instanceof Request ? input.method : "POST"), headers: h, body: out })
1459
1672
  return path.endsWith("/messages") ? unwrapAnthropicTools(res, adapted.names) : res
1460
1673
  }
@@ -1473,9 +1686,23 @@ export const FactoryAuthPlugin = async ({ client }) => {
1473
1686
  let renewed = false
1474
1687
  const onRenew = () => (renewed = true)
1475
1688
  const answer = (res) => said(res, renewed ? "renewed" : "kept")
1689
+ // a 413: once more with the older images left out, then the
1690
+ // refusal said for what it is (leaveOutAttachments)
1691
+ const smaller = async (res) => {
1692
+ const path = new URL(url).pathname
1693
+ const less = leaveOutAttachments(path, body)
1694
+ if (less) {
1695
+ await res.body?.cancel()
1696
+ res = await send(input, init, less.body, onRenew)
1697
+ if (res.status !== 413) return res
1698
+ }
1699
+ const anthropic = path.includes("/llm/a/")
1700
+ return errorReply(url, 413, anthropic ? "request_too_large" : "invalid_request_error", tooLarge(await res.text(), less?.left ?? 0), new Headers(res.headers))
1701
+ }
1476
1702
  let res
1477
1703
  try {
1478
1704
  res = await send(input, init, body, onRenew)
1705
+ if (res.status === 413) return answer(await smaller(res))
1479
1706
  if (res.status !== 403) return answer(res)
1480
1707
  let text = await res.text()
1481
1708
  if (await mendOrg(res.status, text, onRenew).catch(() => false)) {
@@ -1501,4 +1728,4 @@ export const FactoryAuthPlugin = async ({ client }) => {
1501
1728
  }
1502
1729
 
1503
1730
  // for tests
1504
- export const _internal = { limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE }
1731
+ export const _internal = { fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",