@magpie-community/opencode-opendesign-auth 0.1.12 → 0.1.14

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.
package/README.md CHANGED
@@ -127,6 +127,26 @@ of them is signed as that API takes the key — `x-api-key` on `/messages`,
127
127
  the model is declared on, so a model set to another API in magpie's model
128
128
  editor works there too.
129
129
 
130
+ ## Thinking blocks on Anthropic's messages
131
+
132
+ `amr-link` streams a thinking turn on `/v1/messages` as a
133
+ `redacted_thinking` block and then puts `signature_delta`s into it. Claude Code
134
+ reads a `redacted_thinking` block that receives deltas as a malformed stream:
135
+ it aborts the request and re-asks without streaming, which the routing log
136
+ shows as `499`/`canceled` — a third of OpenDesign's requests ended that way
137
+ (#1607). A `signature_delta` is legal on a standard `thinking` block, so the
138
+ plugin's own `fetch` renames amr-link's block to `thinking` as the stream
139
+ passes and leaves its `signature_delta`s alone: the id amr-link gave the block
140
+ is the one sent back on the next turn. The plain-text `thinking_delta`s are
141
+ left as they came.
142
+
143
+ Only amr-link's block is renamed. Anthropic's own `redacted_thinking` carries
144
+ `data` and nothing else; amr-link's also carries `thinking` and `signature`,
145
+ which is what tells them apart — every model is served on `/messages`,
146
+ Claude's included, and a real redacted block must keep its encrypted `data`.
147
+ The rewrite is streaming and per event, and touches only a streaming
148
+ `/messages` response.
149
+
130
150
  ## Where the sign-in is kept
131
151
 
132
152
  magpie's `plugin-auth.json` (mode 600), as an oauth account:
package/index.mjs CHANGED
@@ -42,6 +42,11 @@ const MODEL_WIRE = {
42
42
  "glm-5.3-flash": "anthropic",
43
43
  }
44
44
  const wireOf = (id) => WIRE[MODEL_WIRE[id]] ?? WIRE.chat
45
+ // alsoOf are the two APIs besides its own a model is served on (below),
46
+ // which magpie relays a client's request to as it was sent rather than
47
+ // translating it to the declared one (m.also; gaubeebangeel on X:
48
+ // OpenDesign's Anthropic request to a chat model went as chat completions)
49
+ const alsoOf = (id) => Object.values(WIRE).filter((w) => w !== wireOf(id))
45
50
  // The API a model is declared on is only its default: amr-link serves
46
51
  // every model on all three (keyblues, plugins#888: each of the fourteen
47
52
  // answered on chat completions and Responses at /v1 and on Anthropic's
@@ -71,8 +76,83 @@ const signFor = (key) => async (input, init = {}) => {
71
76
  }
72
77
  // the global fetch as it is when the request is sent: the host's own,
73
78
  // with its proxy and redirect rules, never one captured at load
74
- return globalThis.fetch(input, { ...init, headers: h })
79
+ const res = await globalThis.fetch(input, { ...init, headers: h })
80
+ if (!MESSAGES.test(path) || !res.ok || !res.body) return res
81
+ if (!(res.headers.get("content-type") ?? "").includes("text/event-stream")) return res
82
+ const out = new Headers(res.headers)
83
+ out.delete("content-length")
84
+ out.delete("content-encoding")
85
+ return new Response(plainThinkingSse(res.body), { status: res.status, statusText: res.statusText, headers: out })
75
86
  }
87
+
88
+ // amr-link opens a thinking turn on Anthropic's messages with a
89
+ // `redacted_thinking` block and then streams `signature_delta` into it,
90
+ // which Claude Code reads as a malformed stream: it aborts the request (the
91
+ // routing log's 499/canceled) and re-asks without streaming, so a third of
92
+ // OpenDesign's requests died that way. A `signature_delta` is legal on a
93
+ // `thinking` block, so amr-link's block is renamed to that; its signature,
94
+ // like the plain-text `thinking_delta`s, goes through untouched, so the id
95
+ // amr-link gave the block is the one sent back on the next turn.
96
+ //
97
+ // Only amr-link's block is renamed. Anthropic's own `redacted_thinking`
98
+ // carries `data` and nothing else; amr-link's also carries `thinking` and
99
+ // `signature`. Every model is served on /messages, Claude's included, and a
100
+ // real redacted block must keep its encrypted `data` — renaming it would
101
+ // hand the next turn an empty thinking block with no encrypted reasoning.
102
+ //
103
+ // The rewrite is per event and incremental: an event split across chunks is
104
+ // held until its blank-line separator arrives, and every event it doesn't
105
+ // touch passes through byte for byte.
106
+ const isAmrRedacted = (b) => b?.type === "redacted_thinking" && typeof b.thinking === "string"
107
+
108
+ function plainThinkingSse(source) {
109
+ const decoder = new TextDecoder()
110
+ const encoder = new TextEncoder()
111
+ let pending = ""
112
+
113
+ const emit = (block, sep, controller) => {
114
+ const lines = block.split("\n")
115
+ const at = lines.findIndex((l) => l.startsWith("data:"))
116
+ let ev
117
+ if (at !== -1) {
118
+ try {
119
+ ev = JSON.parse(lines[at].replace(/^data: ?/, "").replace(/\r$/, ""))
120
+ } catch {}
121
+ }
122
+ if (ev?.type === "content_block_start" && isAmrRedacted(ev.content_block)) {
123
+ ev.content_block = { type: "thinking", thinking: "", signature: "" }
124
+ // the line keeps its own terminator: a CRLF stream stays CRLF
125
+ lines[at] = `data: ${JSON.stringify(ev)}${lines[at].endsWith("\r") ? "\r" : ""}`
126
+ controller.enqueue(encoder.encode(lines.join("\n") + sep))
127
+ return
128
+ }
129
+ controller.enqueue(encoder.encode(block + sep))
130
+ }
131
+
132
+ const drain = (controller) => {
133
+ for (;;) {
134
+ const lf = pending.indexOf("\n\n")
135
+ const crlf = pending.indexOf("\r\n\r\n")
136
+ const [at, sep] = crlf !== -1 && (lf === -1 || crlf < lf) ? [crlf, "\r\n\r\n"] : lf !== -1 ? [lf, "\n\n"] : [-1, ""]
137
+ if (at === -1) return
138
+ emit(pending.slice(0, at), sep, controller)
139
+ pending = pending.slice(at + sep.length)
140
+ }
141
+ }
142
+
143
+ return source.pipeThrough(new TransformStream({
144
+ transform(chunk, controller) {
145
+ pending += decoder.decode(chunk, { stream: true })
146
+ drain(controller)
147
+ },
148
+ flush(controller) {
149
+ pending += decoder.decode()
150
+ drain(controller)
151
+ if (pending) controller.enqueue(encoder.encode(pending))
152
+ },
153
+ }))
154
+ }
155
+
76
156
  // the provider's own default: a model this plugin doesn't name is asked on
77
157
  // chat completions, where ten of the eleven thought
78
158
  const NPM = WIRE.chat
@@ -346,6 +426,7 @@ function modelOf(id, m) {
346
426
  // the API magpie asks this model on (WIRE above), per model
347
427
  api: { npm: wireOf(id) },
348
428
  provider: { npm: wireOf(id) },
429
+ also: alsoOf(id),
349
430
  ...(rate && { rate }),
350
431
  limit: {
351
432
  context: Number.isFinite(context) && context > 0 ? context : 200_000,
@@ -370,6 +451,7 @@ function servedOf(id, m) {
370
451
  id,
371
452
  name: c.name,
372
453
  api: { id, url: LINK, npm: wireOf(id) },
454
+ also: alsoOf(id),
373
455
  status: "active",
374
456
  headers: {},
375
457
  options: {},
@@ -528,7 +610,7 @@ export const OpenDesignAuthPlugin = async () => {
528
610
  // catalog's 232000 for glm-5.3-flash, which amr-link refuses (CAP)
529
611
  const out = Number(m.limit?.output)
530
612
  const limit = CAP[id] && !(out > 0 && out <= CAP[id]) ? { limit: { ...(m.limit ?? {}), output: CAP[id] } } : {}
531
- models[id] = { ...m, ...limit, api: { ...(m.api ?? {}), npm: wireOf(id) }, provider: { ...(m.provider ?? {}), npm: wireOf(id) }, ...(rate && { rate }) }
613
+ models[id] = { ...m, ...limit, api: { ...(m.api ?? {}), npm: wireOf(id) }, provider: { ...(m.provider ?? {}), npm: wireOf(id) }, also: alsoOf(id), ...(rate && { rate }) }
532
614
  }
533
615
  cfg.provider[ID] = {
534
616
  name: "Open Design",
@@ -598,4 +680,4 @@ export const OpenDesignAuthPlugin = async () => {
598
680
 
599
681
  // _internal is what the tests read through; magpie calls every exported
600
682
  // function as a plugin, so only the plugin itself is exported bare.
601
- export const _internal = { authOf, signFor, allowances, controlKeyOf, credits, modelsOf, money, rateOf, runtimeKeyOf, said, startDevice, thePlan, usedPercent, waitForDevice, windowName, when, workspaceOf }
683
+ export const _internal = { authOf, signFor, plainThinkingSse, allowances, controlKeyOf, credits, modelsOf, money, rateOf, runtimeKeyOf, said, startDevice, thePlan, usedPercent, waitForDevice, windowName, when, workspaceOf }
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-opendesign-auth",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "Open Design Cloud (Design Plan) in OpenCode and magpie: the account's device sign-in, its models, and the plan's 5-hour and 7-day allowance",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",