@magpie-community/opencode-opendesign-auth 0.1.13 → 0.1.15

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
@@ -76,8 +76,83 @@ const signFor = (key) => async (input, init = {}) => {
76
76
  }
77
77
  // the global fetch as it is when the request is sent: the host's own,
78
78
  // with its proxy and redirect rules, never one captured at load
79
- 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 })
80
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
+
81
156
  // the provider's own default: a model this plugin doesn't name is asked on
82
157
  // chat completions, where ten of the eleven thought
83
158
  const NPM = WIRE.chat
@@ -605,4 +680,4 @@ export const OpenDesignAuthPlugin = async () => {
605
680
 
606
681
  // _internal is what the tests read through; magpie calls every exported
607
682
  // function as a plugin, so only the plugin itself is exported bare.
608
- 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
@@ -507,6 +507,14 @@ async function setupPlugin(ctx, fn, live) {
507
507
  // the account the list was last read for: a credential.updated that
508
508
  // leaves it as it was reads nothing again
509
509
  let readFor = null
510
+ // the last list each account's own read gave, by the account it was read
511
+ // for: a read that fails (a workspace config answering 503) keeps the
512
+ // list that same account last read rather than the bundled one, which
513
+ // brought back models its workspace hides (plugins#1642). Another
514
+ // account's list is never used, a read that gave an empty list is kept
515
+ // as empty, and the bundled list stands in only for an account whose
516
+ // list was never read.
517
+ const lists = new Map()
510
518
  const stateOf = async () => {
511
519
  const c = await ctx.integration.connection.active(authID).catch(() => undefined)
512
520
  const a = c ? await getAuth(authID) : undefined
@@ -523,7 +531,8 @@ async function setupPlugin(ctx, fn, live) {
523
531
  const s = source.get(authID)
524
532
  const connection = await ctx.integration.connection.active(authID).catch(() => undefined)
525
533
  const a = connection ? await getAuth(authID) : undefined
526
- readFor = connKey(connection) + "\0" + (a ? fingerprint(a) : "")
534
+ const readingFor = connKey(connection) + "\0" + (a ? fingerprint(a) : "")
535
+ readFor = readingFor
527
536
  if (!a || listID !== authID || typeof hooks.provider?.models !== "function") {
528
537
  if (s.connection || s.models) {
529
538
  s.connection = undefined
@@ -544,7 +553,12 @@ async function setupPlugin(ctx, fn, live) {
544
553
  return
545
554
  }
546
555
  s.connection = connection
547
- s.models = got && typeof got === "object" ? Object.entries(got).map(([k, m]) => modelOf(authID, s.prov, k, record(m))) : null
556
+ if (got && typeof got === "object") {
557
+ s.models = Object.entries(got).map(([k, m]) => modelOf(authID, s.prov, k, record(m)))
558
+ lists.delete(readingFor)
559
+ lists.set(readingFor, s.models)
560
+ if (lists.size > 32) lists.delete(lists.keys().next().value)
561
+ } else s.models = lists.get(readingFor) ?? null
548
562
  await ctx.provider.reload()
549
563
  await ctx.model?.reload?.()
550
564
  } finally {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-opendesign-auth",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
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",