@pmate/cli 0.24.17 → 0.24.19

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/dist/index.d.ts CHANGED
@@ -659,6 +659,7 @@ type AgentCommandArgs = {
659
659
  baseUrl?: string;
660
660
  hubBaseUrl?: string;
661
661
  from?: string;
662
+ profileId?: string;
662
663
  json: boolean;
663
664
  } | {
664
665
  type: Command.Agent;
package/dist/index.js CHANGED
@@ -107,6 +107,7 @@ function saveLoginSession(session, requestedApp = DEFAULT_APP_ID) {
107
107
  );
108
108
  }
109
109
  const accountId = session.session.identity?.accountId?.trim();
110
+ const profileId = session.profileId?.trim();
110
111
  const token = session.token?.trim();
111
112
  const issuedAt = session.session.issuedAt?.trim();
112
113
  const expiresAt = session.session.expiresAt?.trim();
@@ -116,6 +117,7 @@ function saveLoginSession(session, requestedApp = DEFAULT_APP_ID) {
116
117
  const nextSession = {
117
118
  token,
118
119
  accountId,
120
+ ...profileId ? { profileId } : {},
119
121
  issuedAt,
120
122
  expiresAt,
121
123
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
@@ -231,7 +233,7 @@ function isStoredSession(value) {
231
233
  return false;
232
234
  }
233
235
  const session = value;
234
- return typeof session.token === "string" && !!session.token.trim() && typeof session.accountId === "string" && !!session.accountId.trim() && typeof session.issuedAt === "string" && !!session.issuedAt.trim() && typeof session.expiresAt === "string" && !!session.expiresAt.trim() && typeof session.updatedAt === "string" && !!session.updatedAt.trim();
236
+ return typeof session.token === "string" && !!session.token.trim() && typeof session.accountId === "string" && !!session.accountId.trim() && (session.profileId === void 0 || typeof session.profileId === "string" && !!session.profileId.trim()) && typeof session.issuedAt === "string" && !!session.issuedAt.trim() && typeof session.expiresAt === "string" && !!session.expiresAt.trim() && typeof session.updatedAt === "string" && !!session.updatedAt.trim();
235
237
  }
236
238
  function resolveReadApp(store, options) {
237
239
  const requestedApp = options?.app?.trim();
@@ -283,6 +285,46 @@ var init_sessionStore = __esm({
283
285
  }
284
286
  });
285
287
 
288
+ // src/profileClient.ts
289
+ async function fetchDefaultProfileId(options) {
290
+ const token = options.token.trim();
291
+ const app = options.app.trim();
292
+ const accountId = options.accountId.trim();
293
+ if (!token || !app || !accountId) {
294
+ throw new Error("Missing token, app, or accountId for profile lookup.");
295
+ }
296
+ const url = new URL(`${resolveAuthApiBaseUrl(options.authApiBaseUrl)}/profiles`);
297
+ url.searchParams.set("app", app);
298
+ url.searchParams.set("account", accountId);
299
+ const response = await fetch(url, {
300
+ headers: {
301
+ Authorization: `Bearer ${token}`
302
+ }
303
+ });
304
+ if (!response.ok) {
305
+ const detail = await response.text();
306
+ throw new Error(
307
+ `Failed to resolve default profile (${response.status}): ${detail || response.statusText}`
308
+ );
309
+ }
310
+ const envelope = await response.json();
311
+ const profileId = envelope.data?.[0]?.id?.trim();
312
+ if (!profileId) {
313
+ throw new Error(`No profile found for app ${app} and account ${accountId}.`);
314
+ }
315
+ return profileId;
316
+ }
317
+ function resolveAuthApiBaseUrl(value) {
318
+ return (value || process.env.PMATE_AUTH_API_BASE_URL || process.env.AUTH_API_BASE_URL || DEFAULT_AUTH_API_BASE_URL).replace(/\/+$/, "");
319
+ }
320
+ var DEFAULT_AUTH_API_BASE_URL;
321
+ var init_profileClient = __esm({
322
+ "src/profileClient.ts"() {
323
+ "use strict";
324
+ DEFAULT_AUTH_API_BASE_URL = "https://auth-api-v2.pmate.chat";
325
+ }
326
+ });
327
+
286
328
  // node_modules/@pmate/meta/src/types/account.types.ts
287
329
  var init_account_types = __esm({
288
330
  "node_modules/@pmate/meta/src/types/account.types.ts"() {
@@ -8512,7 +8554,11 @@ var init_AgentClient = __esm({
8512
8554
  const timeoutMs = options?.timeoutMs ?? this.timeoutMs;
8513
8555
  await this.waitForConnection(timeoutMs);
8514
8556
  const token = options?.token ?? "";
8515
- await this.sendAuth(token, timeoutMs);
8557
+ const profileId = options?.profileId;
8558
+ if (!profileId) {
8559
+ throw new Error("AgentClient user auth requires profileId");
8560
+ }
8561
+ await this.sendAuth(token, timeoutMs, profileId);
8516
8562
  return true;
8517
8563
  }
8518
8564
  async prompt(request) {
@@ -8723,10 +8769,14 @@ var init_AgentClient = __esm({
8723
8769
  }
8724
8770
  if (event.type === "connected") {
8725
8771
  this.emit("connected" /* Connected */, event.data);
8772
+ if (!event.data) {
8773
+ this.rejectAuth(new Error("Agent websocket disconnected before auth ack"));
8774
+ }
8726
8775
  return;
8727
8776
  }
8728
8777
  if (event.type === "error") {
8729
8778
  this.emit("error" /* Error */, event.data);
8779
+ this.rejectAuth(new Error("Agent websocket error before auth ack"));
8730
8780
  }
8731
8781
  }
8732
8782
  handleIncomingMessage(message) {
@@ -8736,7 +8786,11 @@ var init_AgentClient = __esm({
8736
8786
  if (message.opcode === 3 /* ACK */) {
8737
8787
  const ack = message.body;
8738
8788
  const success = ack?.status === "ok";
8739
- this.resolveAuth(success);
8789
+ if (!success) {
8790
+ this.rejectAuth(new Error(ack?.message ?? ack?.code ?? "Auth failed"));
8791
+ return;
8792
+ }
8793
+ this.resolveAuth();
8740
8794
  return;
8741
8795
  }
8742
8796
  if (message.opcode === 2 /* AGENT */) {
@@ -8795,17 +8849,19 @@ var init_AgentClient = __esm({
8795
8849
  });
8796
8850
  });
8797
8851
  }
8798
- async sendAuth(token, timeoutMs) {
8852
+ async sendAuth(token, timeoutMs, profileId) {
8799
8853
  if (!this.ws || !this.from) {
8800
8854
  throw new Error("AgentClient not initialized");
8801
8855
  }
8802
- const msg = Msg.create(this.from, "@hub", 4 /* AUTH */, { token, as: "user" });
8856
+ const body = {
8857
+ token,
8858
+ as: "user",
8859
+ profileId
8860
+ };
8861
+ const msg = Msg.create(this.from, "@hub", 4 /* AUTH */, body);
8803
8862
  const waiter = this.waitForAuthAck(timeoutMs);
8804
8863
  await this.ws.sendMsg(msg);
8805
- const success = await waiter;
8806
- if (!success) {
8807
- throw new Error("Auth failed");
8808
- }
8864
+ await waiter;
8809
8865
  }
8810
8866
  waitForAuthAck(timeoutMs) {
8811
8867
  if (this.authPromise) {
@@ -8821,13 +8877,21 @@ var init_AgentClient = __esm({
8821
8877
  });
8822
8878
  return this.authPromise;
8823
8879
  }
8824
- resolveAuth(success) {
8880
+ resolveAuth() {
8825
8881
  if (!this.authResolve) {
8826
8882
  return;
8827
8883
  }
8828
8884
  const resolve = this.authResolve;
8829
8885
  this.clearAuthWaiter();
8830
- resolve(success);
8886
+ resolve();
8887
+ }
8888
+ rejectAuth(error) {
8889
+ if (!this.authReject) {
8890
+ return;
8891
+ }
8892
+ const reject = this.authReject;
8893
+ this.clearAuthWaiter();
8894
+ reject(error);
8831
8895
  }
8832
8896
  clearAuthWaiter() {
8833
8897
  if (this.authTimer) {
@@ -8995,11 +9059,6 @@ function resolveAgentApiBaseUrl(value) {
8995
9059
  function resolveHubBaseUrl(value) {
8996
9060
  return (value || process.env.PMATE_AGENT_HUB_BASE_URL || DEFAULT_AGENT_HUB_BASE_URL).replace(/\/+$/, "");
8997
9061
  }
8998
- function readSessionToken() {
8999
- return readRequiredSession({
9000
- preferredApp: DEFAULT_APP_ID
9001
- }).token;
9002
- }
9003
9062
  function unwrapAgentResult(agentId, value) {
9004
9063
  if (!value || typeof value !== "object") {
9005
9064
  if (value == null) {
@@ -9037,7 +9096,7 @@ function resolveMimeType(filePath) {
9037
9096
  return "application/octet-stream";
9038
9097
  }
9039
9098
  }
9040
- var import_node_fs7, import_node_path11, DEFAULT_AGENT_API_BASE_URL, DEFAULT_AGENT_HUB_BASE_URL, DEFAULT_NAMESPACE, DEFAULT_FROM, DEFAULT_AGENT_PROMPT_TIMEOUT_MS, AgentLlmClient;
9099
+ var import_node_fs7, import_node_path11, DEFAULT_AGENT_API_BASE_URL, DEFAULT_AGENT_HUB_BASE_URL, DEFAULT_NAMESPACE, DEFAULT_AGENT_PROMPT_TIMEOUT_MS, AgentLlmClient;
9041
9100
  var init_AgentLlmClient = __esm({
9042
9101
  "src/llm/AgentLlmClient.ts"() {
9043
9102
  "use strict";
@@ -9045,17 +9104,22 @@ var init_AgentLlmClient = __esm({
9045
9104
  import_node_path11 = __toESM(require("path"));
9046
9105
  init_dist2();
9047
9106
  init_sessionStore();
9107
+ init_profileClient();
9048
9108
  DEFAULT_AGENT_API_BASE_URL = "https://agent-api.pmate.chat";
9049
9109
  DEFAULT_AGENT_HUB_BASE_URL = "https://hub.pmate.chat";
9050
9110
  DEFAULT_NAMESPACE = "pmate-cli";
9051
- DEFAULT_FROM = "pmate-cli";
9052
9111
  DEFAULT_AGENT_PROMPT_TIMEOUT_MS = 12e4;
9053
9112
  AgentLlmClient = class {
9054
9113
  constructor(options) {
9055
- this.token = options?.token ?? readSessionToken();
9114
+ this.session = readRequiredSession({
9115
+ preferredApp: DEFAULT_APP_ID
9116
+ });
9117
+ this.token = options?.token ?? this.session.token;
9118
+ this.profileId = options?.profileId?.trim() || process.env.PMATE_PROFILE_ID || this.session.profileId;
9056
9119
  this.agentApiBaseUrl = resolveAgentApiBaseUrl(options?.agentApiBaseUrl);
9120
+ this.authApiBaseUrl = resolveAuthApiBaseUrl(options?.authApiBaseUrl);
9057
9121
  this.hubBaseUrl = resolveHubBaseUrl(options?.hubBaseUrl);
9058
- this.from = options?.from ?? DEFAULT_FROM;
9122
+ this.from = options?.from?.trim() || void 0;
9059
9123
  this.agentService = new AgentService({
9060
9124
  agentApiBaseUrl: this.agentApiBaseUrl,
9061
9125
  token: this.token
@@ -9072,8 +9136,10 @@ var init_AgentLlmClient = __esm({
9072
9136
  timeoutMs: DEFAULT_AGENT_PROMPT_TIMEOUT_MS
9073
9137
  });
9074
9138
  try {
9075
- await client.login(options?.from ?? this.from, {
9076
- token: this.token
9139
+ const profileId = await this.resolveProfileId();
9140
+ await client.login(this.resolveFrom(options?.from, profileId), {
9141
+ token: this.token,
9142
+ profileId
9077
9143
  });
9078
9144
  const result = await client.prompt({
9079
9145
  agentId,
@@ -9093,8 +9159,10 @@ var init_AgentLlmClient = __esm({
9093
9159
  minChunkSizeBytes: 0
9094
9160
  });
9095
9161
  try {
9096
- await client.login(options?.from ?? this.from, {
9097
- token: this.token
9162
+ const profileId = await this.resolveProfileId();
9163
+ await client.login(this.resolveFrom(options?.from, profileId), {
9164
+ token: this.token,
9165
+ profileId
9098
9166
  });
9099
9167
  const input = new AsyncStream();
9100
9168
  const stream = client.stream({
@@ -9111,6 +9179,21 @@ var init_AgentLlmClient = __esm({
9111
9179
  client.close();
9112
9180
  }
9113
9181
  }
9182
+ async resolveProfileId() {
9183
+ const profileId = this.profileId?.trim();
9184
+ if (profileId) {
9185
+ return profileId;
9186
+ }
9187
+ return fetchDefaultProfileId({
9188
+ token: this.token,
9189
+ app: DEFAULT_APP_ID,
9190
+ accountId: this.session.accountId,
9191
+ authApiBaseUrl: this.authApiBaseUrl
9192
+ });
9193
+ }
9194
+ resolveFrom(value, profileId) {
9195
+ return value?.trim() || this.from || profileId;
9196
+ }
9114
9197
  };
9115
9198
  }
9116
9199
  });
@@ -9556,8 +9639,9 @@ var ListCommandHandler = class {
9556
9639
  var import_node_child_process = require("child_process");
9557
9640
  var import_node_http = require("http");
9558
9641
  init_sessionStore();
9642
+ init_profileClient();
9559
9643
  var AUTH_UI_BASE_URL = "https://auth.pmate.chat";
9560
- var AUTH_API_BASE_URL = "https://auth-api-v2.pmate.chat";
9644
+ var AUTH_API_BASE_URL = DEFAULT_AUTH_API_BASE_URL;
9561
9645
  var CALLBACK_PATH = "/pmate-login-callback";
9562
9646
  var LOGIN_TIMEOUT_MS = 5 * 60 * 1e3;
9563
9647
  function buildHostedLoginUrl(redirectUrl, appId = DEFAULT_APP_ID) {
@@ -9588,7 +9672,12 @@ var LoginCommandHandler = class {
9588
9672
  try {
9589
9673
  const token = await callback.waitForToken;
9590
9674
  const loginResponse = await this.exchangeSession(token, appId);
9591
- savedSession = saveLoginSession(loginResponse, appId);
9675
+ const profileId = await fetchDefaultProfileId({
9676
+ token: loginResponse.token,
9677
+ app: appId,
9678
+ accountId: loginResponse.session.identity.accountId
9679
+ });
9680
+ savedSession = saveLoginSession({ ...loginResponse, profileId }, appId);
9592
9681
  } finally {
9593
9682
  await callback.close();
9594
9683
  }
@@ -16602,7 +16691,8 @@ var AgentCommandHandler = class {
16602
16691
  token,
16603
16692
  agentApiBaseUrl: baseUrl,
16604
16693
  hubBaseUrl: args.hubBaseUrl,
16605
- from: args.from
16694
+ from: args.from,
16695
+ profileId: args.profileId
16606
16696
  });
16607
16697
  if (args.filePath) {
16608
16698
  const filePayload = this.tryReadJsonObjectFile(args.filePath);
@@ -21085,7 +21175,7 @@ function parseAgentArgs(tokens) {
21085
21175
  const ref = rest.shift();
21086
21176
  if (!ref) {
21087
21177
  throw new Error(
21088
- "Usage: agent call <ns:objectId> [--payload <json>] [--file <path>] [--base-url <url>] [--hub-base-url <url>] [--from <id>] [--json]"
21178
+ "Usage: agent call <ns:objectId> [--payload <json>] [--file <path>] [--base-url <url>] [--hub-base-url <url>] [--from <id>] [--profile-id <id>] [--json]"
21089
21179
  );
21090
21180
  }
21091
21181
  const parsed = parseNamespacedRef(ref, { entityLabel: "agent ref" });
@@ -21093,6 +21183,7 @@ function parseAgentArgs(tokens) {
21093
21183
  let payload;
21094
21184
  let hubBaseUrl;
21095
21185
  let from;
21186
+ let profileId;
21096
21187
  while (rest.length > 0) {
21097
21188
  const token = rest.shift();
21098
21189
  switch (token) {
@@ -21111,6 +21202,9 @@ function parseAgentArgs(tokens) {
21111
21202
  case "--from":
21112
21203
  from = requireValue(rest, token);
21113
21204
  break;
21205
+ case "--profile-id":
21206
+ profileId = requireValue(rest, token);
21207
+ break;
21114
21208
  case "--json":
21115
21209
  json = true;
21116
21210
  break;
@@ -21128,6 +21222,7 @@ function parseAgentArgs(tokens) {
21128
21222
  baseUrl,
21129
21223
  hubBaseUrl,
21130
21224
  from,
21225
+ profileId,
21131
21226
  json
21132
21227
  };
21133
21228
  }
@@ -23842,7 +23937,7 @@ var HELP_TEXT = [
23842
23937
  ` ${import_chalk4.default.gray(" update <ns:objectId> --file <path> [--base-url <url>] [--json]")}`,
23843
23938
  ` ${import_chalk4.default.gray(" rename --from <id> --to <id> --ns <namespace> [--base-url <url>] [--json]")}`,
23844
23939
  ` ${import_chalk4.default.gray(" move --from <id> --ns <namespace> --to-ns <namespace> [--to <id>] [--base-url <url>] [--json]")}`,
23845
- ` ${import_chalk4.default.gray(" call <ns:objectId> [--payload <json>] [--file <path>] [--base-url <url>] [--hub-base-url <url>] [--from <id>] [--json]")}`,
23940
+ ` ${import_chalk4.default.gray(" call <ns:objectId> [--payload <json>] [--file <path>] [--base-url <url>] [--hub-base-url <url>] [--from <id>] [--profile-id <id>] [--json]")}`,
23846
23941
  ` ${import_chalk4.default.gray(" --file reads JSON payload files, or streams binary files such as .wav to ASR agents; --payload becomes stream params for binary files")}`,
23847
23942
  ` ${import_chalk4.default.gray(" delete <ns:objectId> [--base-url <url>] [--json]")}`,
23848
23943
  ` ${import_chalk4.default.gray(" list defaults to all namespaces; create defaults to namespace: pmate; default base URL: https://agent-api.pmate.chat; call uses pmate:factory session and default hub: https://hub.pmate.chat")}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/cli",
3
- "version": "0.24.17",
3
+ "version": "0.24.19",
4
4
  "description": "CLI utilities for the pMate project",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -56,7 +56,7 @@
56
56
  "yaml": "^2.8.2"
57
57
  },
58
58
  "devDependencies": {
59
- "@pmate/agent-sdk": "^1.1.5",
59
+ "@pmate/agent-sdk": "^1.1.11",
60
60
  "@pmate/meta": "^1.1.13",
61
61
  "@pmate/utils": "^1.1.3",
62
62
  "@types/node": "^25.0.3",