@magpie-community/opencode-zcode-auth 0.1.14 → 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 (2) hide show
  1. package/index.mjs +47 -18
  2. package/package.json +1 -1
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]
@@ -1498,6 +1526,7 @@ export async function ZCodeAuthPlugin({ client }) {
1498
1526
  }
1499
1527
  }
1500
1528
  let h = new Headers(opts.headers)
1529
+ const place = workPlace(h)
1501
1530
  if (start) {
1502
1531
  if (jwtExpired(s.jwt)) throw new Error(EXPIRED)
1503
1532
  if (!s.jwt) throw new Error("ZCode's sign-in has no key; sign in again")
@@ -1506,7 +1535,7 @@ export async function ZCodeAuthPlugin({ client }) {
1506
1535
  startHeaders(h, s.jwt)
1507
1536
  if (opts.body != null) {
1508
1537
  const text = orig ?? (typeof opts.body === "string" ? opts.body : await new Response(opts.body).text())
1509
- opts.body = dress(text, s.site, s.device)
1538
+ opts.body = dress(text, s.site, s.device, place)
1510
1539
  delete opts.duplex
1511
1540
  h.delete("content-length")
1512
1541
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.14",
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",