@magpie-community/opencode-zcode-auth 0.1.15 → 0.1.17

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 +5 -1
  2. package/index.mjs +31 -8
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -31,7 +31,11 @@ over Anthropic's Messages API. Provider id: `zcode`.
31
31
  - Uses the account the ZCode app on this computer is signed in to. For
32
32
  every request it reads ZCode's own credentials (`~/.zcode/v2`), so it
33
33
  follows ZCode as it signs in again, renews its session or switches to a
34
- team's plan.
34
+ team's plan, but only while ZCode is signed in to that same account.
35
+ Once ZCode signs out, or signs in to another account, the sign-in uses
36
+ what it saved when it was added; when that session has run out, it
37
+ asks to be signed in again. It never sends one account's requests with
38
+ another's credentials: to use both, add the app's sign-in once for each.
35
39
  - **GLM Coding Plan API key**
36
40
  - Paste a key (`<id>.<secret>`) and say which site it is from.
37
41
 
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 { join } from "node:path"
22
+ import { isAbsolute, join } from "node:path"
23
23
  import PROMPT from "./prompt.mjs"
24
24
 
25
25
  const PROVIDER = "zcode"
@@ -933,8 +933,28 @@ async function authorize(site, log) {
933
933
  // team's plan. An account signed in "as ZCode" reads them for every
934
934
  // request, so it follows ZCode's own sign-in, its renewed session and its
935
935
  // switches between plans, as magpie's built-in did (zcode.go zcodeOwn).
936
+ //
937
+ // ZCode 3.14 can move its data (数据存储路径, "data storage path"): it copies
938
+ // ~/.zcode/v2 to <folder>/.zcode/v2, all but setting.json, and from its next
939
+ // start reads credentials.json and runtime/provider from there. The choice
940
+ // stays in the home folder's setting.json as dataBaseDir, which ZCode's main
941
+ // process reads before anything else (readBootstrapDataBaseDirFromDisk);
942
+ // setting.json itself, and the team plan it names, stay in the home folder.
943
+
944
+ // settingDir is where ZCode keeps setting.json, moved or not.
945
+ const settingDir = () => join(homedir(), ".zcode", "v2")
946
+
947
+ // dataIn is the folder ZCode keeps its .zcode data in: the home folder,
948
+ // unless dataBaseDir moved it. A value that isn't an absolute path is
949
+ // ignored, as magpie's built-in does (internal/appdir ZCodeDataIn).
950
+ function dataIn() {
951
+ const d = readJSON(join(settingDir(), "setting.json"))?.dataBaseDir
952
+ const t = typeof d === "string" ? d.trim() : ""
953
+ return t && isAbsolute(t) ? t : homedir()
954
+ }
936
955
 
937
- const zcodeDir = () => join(homedir(), ".zcode", "v2")
956
+ // zcodeDir is where ZCode keeps credentials.json and runtime/provider.
957
+ const zcodeDir = () => join(dataIn(), ".zcode", "v2")
938
958
 
939
959
  // secret is the key ZCode encrypts its credentials with.
940
960
  function secret() {
@@ -974,7 +994,7 @@ function readJSON(path) {
974
994
 
975
995
  // ownTeam is the team plan ZCode switched to, when it did.
976
996
  function ownTeam(store, key) {
977
- const set = readJSON(join(zcodeDir(), "setting.json"))
997
+ const set = readJSON(join(settingDir(), "setting.json"))
978
998
  if (!set) return null
979
999
  const families = set.providerFamilyDomain === "zai" || set.providerFamilyDomain === "bigmodel" ? [set.providerFamilyDomain] : ["zai", "bigmodel"]
980
1000
  for (const fam of families) {
@@ -1044,7 +1064,7 @@ function ownSignIn() {
1044
1064
  function asZCode() {
1045
1065
  return {
1046
1066
  url: "",
1047
- instructions: "Uses the account the ZCode app is signed in to, and follows it.",
1067
+ instructions: "Uses the account the ZCode app is signed in to, and follows it while ZCode stays signed in to that account.",
1048
1068
  method: "auto",
1049
1069
  async callback() {
1050
1070
  const own = ownSignIn()
@@ -1064,10 +1084,13 @@ function stateOf(auth) {
1064
1084
  s = JSON.parse(auth.refresh)
1065
1085
  } catch {}
1066
1086
  if (s.source === "zcode") {
1067
- // ZCode's own, read now: what it was when this was signed in, if
1068
- // ZCode has since signed out
1087
+ // ZCode's own, read now, while ZCode is signed in to this same
1088
+ // account. Signed out, or signed in to another account since, it is
1089
+ // what it was when this was signed in: another account's key would
1090
+ // send this account's requests as that one, and an expired session
1091
+ // says sign in again (EXPIRED) rather than borrowing another's.
1069
1092
  const own = ownSignIn()
1070
- if (own) {
1093
+ if (own && own.user === String(auth.accountId ?? "")) {
1071
1094
  const { user, ...now } = own
1072
1095
  s = { source: "zcode", device: s.device, plan: s.plan, ...now }
1073
1096
  }
@@ -1136,7 +1159,7 @@ function modelsOf(cfg, plan) {
1136
1159
  // localConfig is the latest provider config an installed ZCode has.
1137
1160
  function localConfig() {
1138
1161
  const files = []
1139
- const rt = join(homedir(), ".zcode", "v2", "runtime", "provider")
1162
+ const rt = join(zcodeDir(), "runtime", "provider")
1140
1163
  const walk = (dir, depth) => {
1141
1164
  let es = []
1142
1165
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
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",