@magpie-community/opencode-zcode-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.
Files changed (3) hide show
  1. package/README.md +4 -1
  2. package/index.mjs +60 -21
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -120,7 +120,10 @@ minutes. When the config can't be read, the plugin falls back to this list:
120
120
  | GLM-5.2 | 1M | 128K | none, high, max |
121
121
  | GLM-5-Turbo | 200K | 64K | none, high |
122
122
 
123
- The Start Plan has all of these except GLM-5.3.
123
+ The Start Plan's config has all of these except GLM-5.3. An account
124
+ with no GLM Coding Plan also lists every model its gift plans serve under
125
+ the model's own name, as ZCode does (the Start Plan's GLM-5.3 bucket makes
126
+ GLM-5.3 a model), since all of its requests go to the gift anyway.
124
127
 
125
128
  A gift plan's models appear again as `<model>-Trial` entries, so a
126
129
  request can spend the gift's quota on purpose. A trial entry is listed
package/index.mjs CHANGED
@@ -19,7 +19,7 @@
19
19
  import { createDecipheriv, createHash } from "node:crypto"
20
20
  import { existsSync, readFileSync, readdirSync } from "node:fs"
21
21
  import { arch, homedir, release, userInfo } from "node:os"
22
- import { basename, join } from "node:path"
22
+ import { join } from "node:path"
23
23
  import PROMPT from "./prompt.mjs"
24
24
 
25
25
  const PROVIDER = "zcode"
@@ -1299,20 +1299,47 @@ function startHeaders(h, jwt) {
1299
1299
  h.set("x-zcode-trace-id", uuid())
1300
1300
  }
1301
1301
 
1302
+ // WORK_DIR and WORK_GIT are where magpie tells a plugin's fetch the agent
1303
+ // that asked works: the folder, percent-encoded, and "yes" or "no" for a
1304
+ // git repository, each left off when magpie doesn't know it (magpie's
1305
+ // provider.WorkDirHeader and WorkGitHeader). They are for the plugin, not
1306
+ // the vendor.
1307
+ const WORK_DIR = "x-magpie-work-dir"
1308
+ const WORK_GIT = "x-magpie-work-git"
1309
+
1310
+ // workPlace takes WORK_DIR and WORK_GIT off h: {dir?, git?}, what magpie
1311
+ // said of where the agent works.
1312
+ function workPlace(h) {
1313
+ const place = {}
1314
+ const dir = h.get(WORK_DIR)
1315
+ if (dir) {
1316
+ try {
1317
+ place.dir = decodeURIComponent(dir)
1318
+ } catch {}
1319
+ }
1320
+ const git = h.get(WORK_GIT)
1321
+ if (place.dir && (git === "yes" || git === "no")) place.git = git
1322
+ h.delete(WORK_DIR)
1323
+ h.delete(WORK_GIT)
1324
+ return place
1325
+ }
1326
+
1302
1327
  // environment is the prompt's environment section; provider and model
1303
- // name the powered-by line, left out with no model.
1304
- function environment(provider, model) {
1328
+ // name the powered-by line, left out with no model. The plugin runs in
1329
+ // magpie, not in the agent's folder: the working directory and whether it
1330
+ // is a git repository are the agent's, as magpie tells them (place), and
1331
+ // each is left out when it isn't told rather than said as magpie's own
1332
+ // (#86). The agent's shell isn't known here either, so it isn't said; the
1333
+ // platform and OS version are this computer's, as ZCode's X-Os-* headers
1334
+ // say them.
1335
+ function environment(provider, model, place = {}) {
1305
1336
  const e = PROMPT.environment
1306
- const sh = process.env.SHELL || process.env.ComSpec
1307
- const lines = [
1308
- e.heading,
1309
- e.invokedLine,
1310
- `- ${e.cwdLabel}: ${process.cwd()}`,
1311
- `- ${e.gitLabel}: ${e.gitNo}`,
1312
- `- ${e.platformLabel}: ${process.platform}`,
1313
- `- ${e.shellLabel}: ${sh ? basename(sh) : "unknown"}`,
1314
- `- ${e.osVersionLabel}: ${osVersion()}`,
1315
- ]
1337
+ const lines = [e.heading, e.invokedLine]
1338
+ if (place.dir) {
1339
+ lines.push(`- ${e.cwdLabel}: ${place.dir}`)
1340
+ if (place.git) lines.push(`- ${e.gitLabel}: ${place.git === "yes" ? "yes" : e.gitNo}`)
1341
+ }
1342
+ lines.push(`- ${e.platformLabel}: ${process.platform}`, `- ${e.osVersionLabel}: ${osVersion()}`)
1316
1343
  if (model) lines.push(e.poweredByLine.replace("{provider}", provider).replace("{model}", model))
1317
1344
  return lines.join("\n")
1318
1345
  }
@@ -1335,9 +1362,10 @@ function contextPrefix() {
1335
1362
  // system blocks, then the agent's own system text uncached; the context
1336
1363
  // prefix as the first turn; no cache mark in the turns but on the last
1337
1364
  // block of the last; none on the tools; metadata.user_id naming the
1338
- // sign-in's device. site names the powered-by line's provider. A body that
1339
- // isn't a Messages request goes as it is.
1340
- function dress(text, site, device) {
1365
+ // sign-in's device. site names the powered-by line's provider, place where
1366
+ // the agent works (workPlace). A body that isn't a Messages request goes as
1367
+ // it is.
1368
+ function dress(text, site, device, place) {
1341
1369
  if (typeof text !== "string" || !text.includes("messages")) return text
1342
1370
  let b
1343
1371
  try {
@@ -1358,7 +1386,7 @@ function dress(text, site, device) {
1358
1386
  b.system = [
1359
1387
  cached(PROMPT.cliPrefix),
1360
1388
  cached(PROMPT.stableSections.join("\n\n")),
1361
- cached("\n\n" + [PROMPT.beforeEnvironment, environment(provider, model), PROMPT.afterEnvironment].join("\n\n")),
1389
+ cached("\n\n" + [PROMPT.beforeEnvironment, environment(provider, model, place), PROMPT.afterEnvironment].join("\n\n")),
1362
1390
  ...own,
1363
1391
  ]
1364
1392
  b.messages = [contextPrefix(), ...b.messages]
@@ -1421,12 +1449,22 @@ export async function ZCodeAuthPlugin({ client }) {
1421
1449
  if (fell) ms = start ? START_MODELS : MODELS
1422
1450
  const url = s.base + "/v1"
1423
1451
  const out = Object.fromEntries(ms.map((m) => [m.id, model(m, url)]))
1424
- const details = [...ms, ...modelsOf(cfg, planID(START_BASE)), ...START_MODELS]
1452
+ const details = [...ms, ...modelsOf(cfg, planID(START_BASE)), ...modelsOf(cfg, planID(ZAI_BASE)), ...MODELS]
1453
+ const about = (raw) => details.find((m) => m.id.toLowerCase() === raw.toLowerCase()) ?? { efforts: [] }
1454
+ const has = new Set(Object.keys(out).map((id) => id.toLowerCase()))
1425
1455
  for (const raw of giftNames(gift)) {
1456
+ // a gift-only account's models are its gift's, by their own
1457
+ // names, as ZCode lists them (its Start Plan's GLM-5.3, which the
1458
+ // plan's config leaves out): every request of it goes to the gift
1459
+ // anyway, and the card counts them by that name too (#1261)
1460
+ if (start && !has.has(raw.toLowerCase())) {
1461
+ const plain = about(raw).id ?? raw
1462
+ has.add(raw.toLowerCase())
1463
+ out[plain] = model({ ...about(raw), id: plain }, url)
1464
+ }
1426
1465
  const id = giftID(raw)
1427
1466
  if (out[id]) continue
1428
- const m = details.find((m) => m.id.toLowerCase() === raw.toLowerCase()) ?? { efforts: [] }
1429
- out[id] = model({ ...m, id }, url)
1467
+ out[id] = model({ ...about(raw), id }, url)
1430
1468
  }
1431
1469
  if (fell) out[Symbol.for("magpie.fellBack")] = true
1432
1470
  return out
@@ -1488,6 +1526,7 @@ export async function ZCodeAuthPlugin({ client }) {
1488
1526
  }
1489
1527
  }
1490
1528
  let h = new Headers(opts.headers)
1529
+ const place = workPlace(h)
1491
1530
  if (start) {
1492
1531
  if (jwtExpired(s.jwt)) throw new Error(EXPIRED)
1493
1532
  if (!s.jwt) throw new Error("ZCode's sign-in has no key; sign in again")
@@ -1496,7 +1535,7 @@ export async function ZCodeAuthPlugin({ client }) {
1496
1535
  startHeaders(h, s.jwt)
1497
1536
  if (opts.body != null) {
1498
1537
  const text = orig ?? (typeof opts.body === "string" ? opts.body : await new Response(opts.body).text())
1499
- opts.body = dress(text, s.site, s.device)
1538
+ opts.body = dress(text, s.site, s.device, place)
1500
1539
  delete opts.duplex
1501
1540
  h.delete("content-length")
1502
1541
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",