@pmate/cli 0.45.1 → 0.45.3

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/dist/index.js +45 -29
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -132,11 +132,16 @@ function saveLoginSession(session, requestedApp = DEFAULT_APP_ID) {
132
132
  }
133
133
  function readRequiredSession(options) {
134
134
  const { sessionPath, store } = loadSessionStoreWithPath();
135
- const resolvedApp = resolveReadApp(store, options);
135
+ const boundApp = process.env.PMATE_SESSION_APP?.trim();
136
+ if (process.env.PMATE_EXPECTED_ACCOUNT_ID && !process.env.PMATE_SESSION_FILE?.trim()) throw new Error("Agent CLI requires a dedicated session file");
137
+ if (boundApp && options?.app && options.app !== boundApp) throw new Error("Agent CLI application differs from its bound session");
138
+ const resolvedApp = boundApp ? assertValidAppRef(boundApp) : resolveReadApp(store, options);
136
139
  const session = store.sessions[resolvedApp];
137
140
  if (!session) {
138
141
  throw new Error(buildMissingSessionMessage(sessionPath, resolvedApp));
139
142
  }
143
+ if (process.env.PMATE_EXPECTED_ACCOUNT_ID && session.accountId !== process.env.PMATE_EXPECTED_ACCOUNT_ID) throw new Error("Agent CLI Account mismatch");
144
+ if (process.env.PMATE_EXPECTED_PROFILE_ID && session.profileId !== process.env.PMATE_EXPECTED_PROFILE_ID) throw new Error("Agent CLI Profile mismatch");
140
145
  const token = session.token?.trim();
141
146
  if (!token) {
142
147
  throw new Error(
@@ -8824,8 +8829,10 @@ var import_node_path36 = __toESM(require("path"));
8824
8829
  // src/managedSession.ts
8825
8830
  var import_node_fs = require("fs");
8826
8831
  var import_node_os = require("os");
8832
+ var import_node_crypto = require("crypto");
8827
8833
  var import_node_path = require("path");
8828
8834
  async function ensureManagedSession(env = process.env, fetcher = fetch) {
8835
+ if (env.PMATE_EXPECTED_ACCOUNT_ID && env.HEBO_MEMORY_TOKEN?.trim()) throw Error("Bound Agent CLI cannot use another delegated identity");
8829
8836
  if (env.HEBO_MEMORY_TOKEN?.trim()) {
8830
8837
  const token = env.HEBO_MEMORY_TOKEN.trim(), app = "pmate:factory";
8831
8838
  const base = (env.AUTH_API_BASE_URL ?? "https://auth-api-v2.pmate.chat").replace(/\/+$/, "");
@@ -8847,16 +8854,25 @@ async function ensureManagedSession(env = process.env, fetcher = fetch) {
8847
8854
  }
8848
8855
  if (env.PCS_AGENT_SESSION_MANAGED !== "1") return;
8849
8856
  if (!env.PMATE_SESSION_FILE) throw Error("Managed session file is required");
8850
- const state = JSON.parse((0, import_node_fs.readFileSync)(env.PMATE_SESSION_FILE, "utf8"));
8857
+ const source = env.PCS_AGENT_SESSION_SOURCE_FILE ?? env.PMATE_SESSION_FILE;
8858
+ const state = JSON.parse((0, import_node_fs.readFileSync)(source, "utf8"));
8851
8859
  if (!state.refreshKey) throw Error("Managed session refresh key is missing");
8852
- const port = Number(env.PCS_AGENT_CONTROL_PORT ?? 39399);
8860
+ const port = Number(env.PMATE_SESSION_REFRESH_PORT ?? env.PCS_AGENT_CONTROL_PORT ?? 39399);
8853
8861
  if (!Number.isInteger(port) || port < 1 || port > 65535) throw Error("Invalid controller port");
8854
- const r = await fetcher(`http://127.0.0.1:${port}/v2/session/refresh`, {
8862
+ const r = await fetcher(`http://127.0.0.1:${port}/${env.PMATE_SESSION_REFRESH_PORT ? "v1" : "v2"}/session/refresh`, {
8855
8863
  method: "POST",
8856
8864
  headers: { "x-agent-session-key": state.refreshKey },
8857
8865
  signal: AbortSignal.timeout(45e3)
8858
8866
  });
8859
8867
  if (!r.ok) throw Error(`Managed session renewal unavailable (${r.status}); retry later`);
8868
+ if (source !== env.PMATE_SESSION_FILE) {
8869
+ const raw = (0, import_node_fs.readFileSync)(source, "utf8"), current = JSON.parse(raw).sessions?.[env.PMATE_SESSION_APP ?? "pmate:factory"];
8870
+ if (env.PMATE_EXPECTED_ACCOUNT_ID && current?.accountId !== env.PMATE_EXPECTED_ACCOUNT_ID) throw Error("Renewed Agent CLI Account mismatch");
8871
+ if (env.PMATE_EXPECTED_PROFILE_ID && current?.profileId !== env.PMATE_EXPECTED_PROFILE_ID) throw Error("Renewed Agent CLI Profile mismatch");
8872
+ const temp = `${env.PMATE_SESSION_FILE}.${(0, import_node_crypto.randomUUID)()}.tmp`;
8873
+ (0, import_node_fs.writeFileSync)(temp, raw, { mode: 384, flag: "wx" });
8874
+ (0, import_node_fs.renameSync)(temp, env.PMATE_SESSION_FILE);
8875
+ }
8860
8876
  }
8861
8877
 
8862
8878
  // src/index.ts
@@ -9860,7 +9876,7 @@ var import_fc20230330 = __toESM(require("@alicloud/fc20230330"));
9860
9876
  var import_openapi_client = require("@alicloud/openapi-client");
9861
9877
  var import_sts20150401 = __toESM(require("@alicloud/sts20150401"));
9862
9878
  var import_ali_oss = __toESM(require("ali-oss"));
9863
- var import_node_crypto = require("crypto");
9879
+ var import_node_crypto2 = require("crypto");
9864
9880
  var import_node_fs5 = require("fs");
9865
9881
  var import_node_path7 = __toESM(require("path"));
9866
9882
 
@@ -10631,7 +10647,7 @@ var AliyunService = class {
10631
10647
  /** Compute MD5 for a file. */
10632
10648
  static getFileMd5(filePath) {
10633
10649
  return new Promise((resolve, reject) => {
10634
- const hash = (0, import_node_crypto.createHash)("md5");
10650
+ const hash = (0, import_node_crypto2.createHash)("md5");
10635
10651
  const stream = (0, import_node_fs5.createReadStream)(filePath);
10636
10652
  stream.on("data", (chunk) => {
10637
10653
  hash.update(chunk);
@@ -14530,7 +14546,7 @@ var QrcodeCommandHandler = class {
14530
14546
  };
14531
14547
 
14532
14548
  // src/commands/RandomCommandHandler.ts
14533
- var import_node_crypto2 = require("crypto");
14549
+ var import_node_crypto3 = require("crypto");
14534
14550
  var DEFAULT_TOKEN_LENGTH = 32;
14535
14551
  var DEFAULT_EASY_WORDS = 3;
14536
14552
  var DEFAULT_EASY_DIGITS = 2;
@@ -14696,7 +14712,7 @@ function randomString(length, charset) {
14696
14712
  const max = Math.floor(256 / charsetLength) * charsetLength;
14697
14713
  let result = "";
14698
14714
  while (result.length < length) {
14699
- const bytes = (0, import_node_crypto2.randomBytes)(Math.max(1, length - result.length));
14715
+ const bytes = (0, import_node_crypto3.randomBytes)(Math.max(1, length - result.length));
14700
14716
  for (const byte of bytes) {
14701
14717
  if (byte >= max) {
14702
14718
  continue;
@@ -14710,12 +14726,12 @@ function randomString(length, charset) {
14710
14726
  return result;
14711
14727
  }
14712
14728
  function randomWord() {
14713
- return EASY_WORDS[(0, import_node_crypto2.randomInt)(0, EASY_WORDS.length)];
14729
+ return EASY_WORDS[(0, import_node_crypto3.randomInt)(0, EASY_WORDS.length)];
14714
14730
  }
14715
14731
  function randomDigits(count) {
14716
14732
  let value = "";
14717
14733
  for (let i = 0; i < count; i += 1) {
14718
- value += (0, import_node_crypto2.randomInt)(0, 10).toString();
14734
+ value += (0, import_node_crypto3.randomInt)(0, 10).toString();
14719
14735
  }
14720
14736
  return value;
14721
14737
  }
@@ -15500,20 +15516,20 @@ var Im1BotClient = class {
15500
15516
  };
15501
15517
 
15502
15518
  // src/collectStore.ts
15503
- var import_node_crypto3 = require("crypto");
15519
+ var import_node_crypto4 = require("crypto");
15504
15520
  var import_node_fs12 = require("fs");
15505
15521
  var import_node_os7 = __toESM(require("os"));
15506
15522
  var import_node_path18 = __toESM(require("path"));
15507
15523
  function resolveCollectTarget(input) {
15508
15524
  const endpoint = input.endpoint ?? (input.source === "cc" ? "http://localhost:9223" : "http://localhost:9222");
15509
- const sourceKey = input.source ?? `endpoint-${(0, import_node_crypto3.createHash)("sha1").update(endpoint).digest("hex").slice(0, 8)}`;
15525
+ const sourceKey = input.source ?? `endpoint-${(0, import_node_crypto4.createHash)("sha1").update(endpoint).digest("hex").slice(0, 8)}`;
15510
15526
  const rootDir = import_node_path18.default.join(import_node_os7.default.homedir(), ".pmate", "collect", sourceKey);
15511
15527
  return { sourceKey, endpoint, rootDir };
15512
15528
  }
15513
15529
  function toThreadKey(name) {
15514
15530
  const normalized = name.trim().toLowerCase().replace(/[^a-z0-9\u4e00-\u9fff]+/g, "-").replace(/^-+|-+$/g, "");
15515
15531
  const base = normalized || "thread";
15516
- const suffix = (0, import_node_crypto3.createHash)("sha1").update(name).digest("hex").slice(0, 8);
15532
+ const suffix = (0, import_node_crypto4.createHash)("sha1").update(name).digest("hex").slice(0, 8);
15517
15533
  return `${base}-${suffix}`;
15518
15534
  }
15519
15535
  async function readStoredThread(rootDir, threadKey) {
@@ -18292,7 +18308,7 @@ var import_sharp2 = __toESM(require("sharp"));
18292
18308
 
18293
18309
  // src/desktopRelease.ts
18294
18310
  init_sessionStore();
18295
- var import_node_crypto4 = require("crypto");
18311
+ var import_node_crypto5 = require("crypto");
18296
18312
  var import_node_fs20 = require("fs");
18297
18313
  var import_node_path25 = __toESM(require("path"));
18298
18314
  async function uploadDesktopArtifacts(directory, app, version, platform, arch) {
@@ -18306,7 +18322,7 @@ async function uploadDesktopArtifacts(directory, app, version, platform, arch) {
18306
18322
  const artifacts = (0, import_node_fs20.readdirSync)(directory, { withFileTypes: true }).filter((entry) => entry.isFile() && (platform === "mac" ? /(?:\.(dmg|zip|blockmap)$|^latest-mac\.yml$)/i : /(?:\.(exe|zip|blockmap)$|^latest\.yml$)/i).test(entry.name)).sort((a, b) => a.name.localeCompare(b.name)).map((entry) => {
18307
18323
  const bytes = (0, import_node_fs20.readFileSync)(import_node_path25.default.join(directory, entry.name));
18308
18324
  const objectKey = prefix + entry.name;
18309
- return { name: entry.name, objectKey, downloadUrl: url(objectKey), sha256: (0, import_node_crypto4.createHash)("sha256").update(bytes).digest("hex"), size: bytes.length };
18325
+ return { name: entry.name, objectKey, downloadUrl: url(objectKey), sha256: (0, import_node_crypto5.createHash)("sha256").update(bytes).digest("hex"), size: bytes.length };
18310
18326
  });
18311
18327
  if (!artifacts.length) throw new Error("No desktop installers found to publish");
18312
18328
  const manifestKey = prefix + "release.json";
@@ -18510,7 +18526,7 @@ function currentRepositoryRoot(repository, cwd = process.cwd()) {
18510
18526
 
18511
18527
  // src/services/AndroidApkReleaseService.ts
18512
18528
  var import_node_child_process11 = require("child_process");
18513
- var import_node_crypto5 = require("crypto");
18529
+ var import_node_crypto6 = require("crypto");
18514
18530
  var import_node_fs22 = require("fs");
18515
18531
  var import_node_os11 = __toESM(require("os"));
18516
18532
  var import_node_path27 = __toESM(require("path"));
@@ -19011,7 +19027,7 @@ function listVersionedToolCandidates(parentDir, relativeToolPath) {
19011
19027
  }
19012
19028
  function sha256File(filePath) {
19013
19029
  return new Promise((resolve, reject) => {
19014
- const hash = (0, import_node_crypto5.createHash)("sha256");
19030
+ const hash = (0, import_node_crypto6.createHash)("sha256");
19015
19031
  const stream = (0, import_node_fs22.createReadStream)(filePath);
19016
19032
  stream.on("data", (chunk) => hash.update(chunk));
19017
19033
  stream.on("error", reject);
@@ -19030,7 +19046,7 @@ function capitalize(value) {
19030
19046
 
19031
19047
  // src/services/HarmonyReleaseService.ts
19032
19048
  var import_node_child_process12 = require("child_process");
19033
- var import_node_crypto6 = require("crypto");
19049
+ var import_node_crypto7 = require("crypto");
19034
19050
  var import_node_fs23 = require("fs");
19035
19051
  var import_node_os12 = __toESM(require("os"));
19036
19052
  var import_node_path28 = __toESM(require("path"));
@@ -19172,7 +19188,7 @@ function signingMaterial(values) {
19172
19188
  return bytes;
19173
19189
  };
19174
19190
  const certificate = decode2("HARMONY_CERTIFICATE_BASE64");
19175
- const fingerprint = new import_node_crypto6.X509Certificate(certificate).fingerprint256.replace(/:/g, "");
19191
+ const fingerprint = new import_node_crypto7.X509Certificate(certificate).fingerprint256.replace(/:/g, "");
19176
19192
  return { keystore: decode2("HARMONY_KEYSTORE_BASE64"), certificate, profile: decode2("HARMONY_PROFILE_BASE64"), fingerprint };
19177
19193
  }
19178
19194
  function assertHarmonyMetadata(raw, version) {
@@ -19182,7 +19198,7 @@ function assertHarmonyMetadata(raw, version) {
19182
19198
  var withHarmonyPublicationLock = async (bucket2, key, publish) => {
19183
19199
  if (!/^harmony\/[\w-]+\/[\w-]+\/\.publish-lock\.json$/.test(key)) throw new Error("Invalid HarmonyOS publication lock key");
19184
19200
  await AliyunService.assertUnversionedBucket(bucket2);
19185
- const owner = (0, import_node_crypto6.randomUUID)();
19201
+ const owner = (0, import_node_crypto7.randomUUID)();
19186
19202
  try {
19187
19203
  await AliyunService.uploadJsonObject(bucket2, key, { owner, createdAt: (/* @__PURE__ */ new Date()).toISOString() }, { "x-oss-forbid-overwrite": "true" });
19188
19204
  } catch {
@@ -19254,9 +19270,9 @@ var HarmonyReleaseService = class {
19254
19270
  const files = (0, import_node_fs23.readdirSync)(dist, { withFileTypes: true }).filter((e) => e.isFile() && /\.(app|hap)$/.test(e.name)).map((e) => import_node_path28.default.join(dist, e.name));
19255
19271
  if (files.length !== 2 || files.filter((f) => f.endsWith(".app")).length !== 1 || files.filter((f) => f.endsWith(".hap")).length !== 1) throw new Error("HarmonyOS build must produce exactly one signed APP and one signed HAP");
19256
19272
  const check = async (file, inspectMetadata) => {
19257
- const id = (0, import_node_crypto6.createHash)("sha256").update(file).digest("hex").slice(0, 16), cert = import_node_path28.default.join(temp, `${id}.cer`);
19273
+ const id = (0, import_node_crypto7.createHash)("sha256").update(file).digest("hex").slice(0, 16), cert = import_node_path28.default.join(temp, `${id}.cer`);
19258
19274
  await run(tools.java, ["-jar", tools.signer, "verify-app", "-inFile", file, "-outCertChain", cert, "-outProfile", import_node_path28.default.join(temp, `${id}.p7b`)], opts);
19259
- if (new import_node_crypto6.X509Certificate((0, import_node_fs23.readFileSync)(cert)).fingerprint256.replace(/:/g, "") !== material.fingerprint) throw new Error("HarmonyOS signer certificate does not match service certificate");
19275
+ if (new import_node_crypto7.X509Certificate((0, import_node_fs23.readFileSync)(cert)).fingerprint256.replace(/:/g, "") !== material.fingerprint) throw new Error("HarmonyOS signer certificate does not match service certificate");
19260
19276
  if (inspectMetadata) assertHarmonyMetadata(JSON.parse(await run("unzip", ["-p", file, "module.json"], { ...opts, capture: true })), version);
19261
19277
  };
19262
19278
  for (const file of files) {
@@ -19280,7 +19296,7 @@ var HarmonyReleaseService = class {
19280
19296
  if (!size) throw new Error("Empty HarmonyOS artifact");
19281
19297
  const target = import_node_path28.default.join(output, name);
19282
19298
  (0, import_node_fs23.cpSync)(file, target);
19283
- const hash = (0, import_node_crypto6.createHash)("sha256");
19299
+ const hash = (0, import_node_crypto7.createHash)("sha256");
19284
19300
  for await (const chunk of (0, import_node_fs23.createReadStream)(target)) hash.update(chunk);
19285
19301
  artifacts.push({ name, type: import_node_path28.default.extname(name).slice(1), objectKey: releasePrefix + name, downloadUrl: `${baseUrl}/${releasePrefix}${name}`, sha256: hash.digest("hex"), size });
19286
19302
  }
@@ -19311,7 +19327,7 @@ var HarmonyReleaseService = class {
19311
19327
 
19312
19328
  // src/services/IosBuildService.ts
19313
19329
  var import_node_child_process13 = require("child_process");
19314
- var import_node_crypto7 = require("crypto");
19330
+ var import_node_crypto8 = require("crypto");
19315
19331
  var import_node_fs24 = require("fs");
19316
19332
  var import_node_os13 = __toESM(require("os"));
19317
19333
  var import_node_path29 = __toESM(require("path"));
@@ -19374,7 +19390,7 @@ var IosBuildService = class {
19374
19390
  if (!bundleId || !/^\d+\.\d+(?:\.\d+)?$/.test(version ?? "") || !/^[A-Z0-9]{10}$/.test(team ?? "")) throw new Error("Missing or invalid bundle ID, MARKETING_VERSION or signing team; use --team if needed");
19375
19391
  if (service.mobile && "bundleId" in service.mobile && service.mobile.bundleId !== bundleId) throw new Error("Service bundle ID does not match application target");
19376
19392
  const currentBuild = iosBuildInteger(cfg.CURRENT_PROJECT_VERSION);
19377
- const key = (0, import_node_crypto7.createHash)("sha256").update(bundleId).digest("hex");
19393
+ const key = (0, import_node_crypto8.createHash)("sha256").update(bundleId).digest("hex");
19378
19394
  const statePath = import_node_path29.default.join(root, `${key}.json`);
19379
19395
  const lockPath = import_node_path29.default.join(root, `${key}.lock`);
19380
19396
  let lock;
@@ -22747,7 +22763,7 @@ var RoleCommandHandler = class {
22747
22763
 
22748
22764
  // src/commands/AccountCommandHandler.ts
22749
22765
  var import_node_fs28 = require("fs");
22750
- var import_node_crypto8 = require("crypto");
22766
+ var import_node_crypto9 = require("crypto");
22751
22767
  var import_node_path33 = __toESM(require("path"));
22752
22768
  init_sessionStore();
22753
22769
  var AccountCommandHandler = class {
@@ -23392,7 +23408,7 @@ var AccountCommandHandler = class {
23392
23408
  return process.env.HOME || process.env.USERPROFILE || process.cwd();
23393
23409
  }
23394
23410
  generateBotKeyFile(app, name) {
23395
- const { publicKey, privateKey } = (0, import_node_crypto8.generateKeyPairSync)("ed25519");
23411
+ const { publicKey, privateKey } = (0, import_node_crypto9.generateKeyPairSync)("ed25519");
23396
23412
  return {
23397
23413
  type: "pmate-bot-key",
23398
23414
  version: 1,
@@ -23514,12 +23530,12 @@ var AccountCommandHandler = class {
23514
23530
  });
23515
23531
  }
23516
23532
  signBotChallenge(challenge, privateKey) {
23517
- const key = (0, import_node_crypto8.createPrivateKey)({
23533
+ const key = (0, import_node_crypto9.createPrivateKey)({
23518
23534
  key: Buffer.from(privateKey, "base64url"),
23519
23535
  format: "der",
23520
23536
  type: "pkcs8"
23521
23537
  });
23522
- return (0, import_node_crypto8.sign)(
23538
+ return (0, import_node_crypto9.sign)(
23523
23539
  null,
23524
23540
  Buffer.from(JSON.stringify({
23525
23541
  id: challenge.id,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/cli",
3
- "version": "0.45.1",
3
+ "version": "0.45.3",
4
4
  "description": "CLI utilities for the pMate project",
5
5
  "license": "MIT",
6
6
  "bin": {