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

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
@@ -28,7 +28,7 @@ Open Design's cloud is two planes:
28
28
  | Plane | Host | Key | Used for |
29
29
  | --- | --- | --- | --- |
30
30
  | Control | `amr-api.open-design.ai` | `vela_ctrl_…` | plan, wallet balance, the Design Plan's allowance windows, the workspace |
31
- | Model | `amr-link.open-design.ai/v1` | `sk-amr-…` | `/models`, `/responses` (the OpenAI Responses API) |
31
+ | Model | `amr-link.open-design.ai/v1` | `sk-amr-…` | `/models`, `/chat/completions`, `/responses`, `/messages` |
32
32
 
33
33
  A sign-in is the same device authorization the Open Design CLI
34
34
  (`vela login`) runs:
@@ -120,6 +120,13 @@ for `claude-haiku-5.5` and `deepseek-v4-pro`, and Anthropic's messages
120
120
  (`@ai-sdk/anthropic`) for `glm-5.3-flash`. Every model reasons at `low`,
121
121
  `high` and `max`.
122
122
 
123
+ That API is each model's default, not the only one: amr-link serves every
124
+ model on all three (keyblues, plugins#888). A request for a model on another
125
+ of them is signed as that API takes the key — `x-api-key` on `/messages`,
126
+ `Authorization: Bearer` on `/chat/completions` and `/responses` — whatever
127
+ the model is declared on, so a model set to another API in magpie's model
128
+ editor works there too.
129
+
123
130
  ## Where the sign-in is kept
124
131
 
125
132
  magpie's `plugin-auth.json` (mode 600), as an oauth account:
package/index.mjs CHANGED
@@ -3,7 +3,8 @@
3
3
  // (amr-api.open-design.ai), which answers with two keys — a control key
4
4
  // (vela_ctrl_…) for the account's plan and its Design Plan allowance, and a
5
5
  // runtime key (sk-amr-…) that the model plane (amr-link.open-design.ai)
6
- // takes. Model requests are the OpenAI Responses API at the runtime key;
6
+ // takes. Model requests go there at the runtime key, on each model's own API
7
+ // (WIRE below) or any other of the three amr-link serves it on;
7
8
  // the model list, the plan and the 5-hour and 7-day allowance windows all
8
9
  // come from the control plane.
9
10
  //
@@ -41,6 +42,42 @@ const MODEL_WIRE = {
41
42
  "glm-5.3-flash": "anthropic",
42
43
  }
43
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))
50
+ // The API a model is declared on is only its default: amr-link serves
51
+ // every model on all three (keyblues, plugins#888: each of the fourteen
52
+ // answered on chat completions and Responses at /v1 and on Anthropic's
53
+ // messages at the bare host, which the AI SDK asks at /v1/messages). A
54
+ // request can reach the plugin on another API than the declared one — magpie
55
+ // sends it there when the model is set to be asked on it — and the key
56
+ // rides as that API takes it, not as the declared package sends it: the
57
+ // host signs with the declared package's header (x-api-key for
58
+ // @ai-sdk/anthropic, Bearer for the others), so a chat model asked on
59
+ // /messages went with a Bearer and glm-5.3-flash asked on /chat/completions
60
+ // with an x-api-key. signFor reads the API off the request's path.
61
+ const MESSAGES = /\/messages(\/count_tokens)?\/?$/
62
+ const signFor = (key) => async (input, init = {}) => {
63
+ const url = input instanceof Request ? input.url : String(input)
64
+ const h = new Headers(init.headers ?? (input instanceof Request ? input.headers : undefined))
65
+ let path = ""
66
+ try {
67
+ path = new URL(url).pathname
68
+ } catch {}
69
+ if (MESSAGES.test(path)) {
70
+ h.delete("authorization")
71
+ h.set("x-api-key", key)
72
+ if (!h.has("anthropic-version")) h.set("anthropic-version", "2023-06-01")
73
+ } else {
74
+ h.delete("x-api-key")
75
+ h.set("authorization", `Bearer ${key}`)
76
+ }
77
+ // the global fetch as it is when the request is sent: the host's own,
78
+ // with its proxy and redirect rules, never one captured at load
79
+ return globalThis.fetch(input, { ...init, headers: h })
80
+ }
44
81
  // the provider's own default: a model this plugin doesn't name is asked on
45
82
  // chat completions, where ten of the eleven thought
46
83
  const NPM = WIRE.chat
@@ -314,6 +351,7 @@ function modelOf(id, m) {
314
351
  // the API magpie asks this model on (WIRE above), per model
315
352
  api: { npm: wireOf(id) },
316
353
  provider: { npm: wireOf(id) },
354
+ also: alsoOf(id),
317
355
  ...(rate && { rate }),
318
356
  limit: {
319
357
  context: Number.isFinite(context) && context > 0 ? context : 200_000,
@@ -338,6 +376,7 @@ function servedOf(id, m) {
338
376
  id,
339
377
  name: c.name,
340
378
  api: { id, url: LINK, npm: wireOf(id) },
379
+ also: alsoOf(id),
341
380
  status: "active",
342
381
  headers: {},
343
382
  options: {},
@@ -496,7 +535,7 @@ export const OpenDesignAuthPlugin = async () => {
496
535
  // catalog's 232000 for glm-5.3-flash, which amr-link refuses (CAP)
497
536
  const out = Number(m.limit?.output)
498
537
  const limit = CAP[id] && !(out > 0 && out <= CAP[id]) ? { limit: { ...(m.limit ?? {}), output: CAP[id] } } : {}
499
- models[id] = { ...m, ...limit, api: { ...(m.api ?? {}), npm: wireOf(id) }, provider: { ...(m.provider ?? {}), npm: wireOf(id) }, ...(rate && { rate }) }
538
+ models[id] = { ...m, ...limit, api: { ...(m.api ?? {}), npm: wireOf(id) }, provider: { ...(m.provider ?? {}), npm: wireOf(id) }, also: alsoOf(id), ...(rate && { rate }) }
500
539
  }
501
540
  cfg.provider[ID] = {
502
541
  name: "Open Design",
@@ -528,7 +567,7 @@ export const OpenDesignAuthPlugin = async () => {
528
567
  loader: async (getAuth) => {
529
568
  const key = runtimeKeyOf(await getAuth().catch(() => null))
530
569
  if (!key) return {}
531
- return { baseURL: LINK, apiKey: key }
570
+ return { baseURL: LINK, apiKey: key, fetch: signFor(key) }
532
571
  },
533
572
  methods: [
534
573
  {
@@ -566,4 +605,4 @@ export const OpenDesignAuthPlugin = async () => {
566
605
 
567
606
  // _internal is what the tests read through; magpie calls every exported
568
607
  // function as a plugin, so only the plugin itself is exported bare.
569
- export const _internal = { authOf, allowances, controlKeyOf, credits, modelsOf, money, rateOf, runtimeKeyOf, said, startDevice, thePlan, usedPercent, waitForDevice, windowName, when, workspaceOf }
608
+ export const _internal = { authOf, signFor, 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.11",
3
+ "version": "0.1.13",
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",