@apa-network/agent-sdk 0.2.0-beta.10 → 0.2.0-beta.13

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/README.md CHANGED
@@ -123,6 +123,8 @@ Default path:
123
123
  ./credentials.json
124
124
  ```
125
125
 
126
+ You should not create this file manually. `apa-bot register` writes it automatically.
127
+
126
128
  Format (single credential only):
127
129
 
128
130
  ```json
package/bin/apa-bot.js CHANGED
@@ -1,7 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
  import("../dist/cli.js")
3
- .then((mod) => mod.runCLI())
3
+ .then((mod) => mod.runCLIEntrypoint())
4
4
  .catch((err) => {
5
- console.error(err instanceof Error ? err.message : String(err));
5
+ const payload = {
6
+ type: "error",
7
+ error: "cli_bootstrap_error",
8
+ message: err instanceof Error ? err.message : String(err)
9
+ };
10
+ process.stdout.write(`${JSON.stringify(payload)}\n`);
6
11
  process.exit(1);
7
12
  });
package/dist/cli.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export declare function runCLI(argv?: string[]): Promise<void>;
2
+ export declare function runCLIEntrypoint(argv?: string[]): Promise<void>;
package/dist/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { APAHttpClient } from "./http/client.js";
2
2
  import { resolveApiBase, requireArg } from "./utils/config.js";
3
- import { loadCredential, saveCredential } from "./loop/credentials.js";
3
+ import { defaultCredentialPath, loadCredential, saveCredential } from "./loop/credentials.js";
4
4
  import { loadDecisionState, saveDecisionState } from "./loop/decision_state.js";
5
5
  import { TurnTracker } from "./loop/state.js";
6
6
  import { buildCredentialFromRegisterResult } from "./commands/register.js";
@@ -65,7 +65,8 @@ Config priority: CLI args > env (API_BASE) > defaults.`);
65
65
  async function requireApiKey(apiBase) {
66
66
  const cached = await loadCredential(apiBase, undefined);
67
67
  if (!cached?.api_key) {
68
- throw new Error("api_key_not_found (run apa-bot register)");
68
+ const path = defaultCredentialPath();
69
+ throw new Error(`api_key_not_found for api_base=${apiBase} (run apa-bot register). path=${path}`);
69
70
  }
70
71
  return cached.api_key;
71
72
  }
@@ -82,6 +83,16 @@ function claimCodeFromUrl(raw) {
82
83
  function emit(message) {
83
84
  process.stdout.write(`${JSON.stringify(message)}\n`);
84
85
  }
86
+ function toErrorPayload(err) {
87
+ const apiErr = err;
88
+ const code = apiErr?.code || "cli_error";
89
+ const message = err instanceof Error ? err.message : String(err);
90
+ return {
91
+ type: "error",
92
+ error: code,
93
+ message
94
+ };
95
+ }
85
96
  function readLegalActions(state) {
86
97
  const raw = state["legal_actions"];
87
98
  if (!Array.isArray(raw)) {
@@ -591,3 +602,12 @@ export async function runCLI(argv = process.argv.slice(2)) {
591
602
  throw new Error(`unknown command: ${command}`);
592
603
  }
593
604
  }
605
+ export async function runCLIEntrypoint(argv = process.argv.slice(2)) {
606
+ try {
607
+ await runCLI(argv);
608
+ }
609
+ catch (err) {
610
+ emit(toErrorPayload(err));
611
+ process.exitCode = 1;
612
+ }
613
+ }
@@ -3,9 +3,13 @@ export function buildCredentialFromRegisterResult(result, apiBase, fallbackName)
3
3
  if (!obj) {
4
4
  return null;
5
5
  }
6
- const agentID = typeof obj.agent_id === "string" ? obj.agent_id : "";
7
- const apiKey = typeof obj.api_key === "string" ? obj.api_key : "";
8
- const agentName = typeof obj.name === "string" && obj.name.trim() ? obj.name : fallbackName;
6
+ const agent = (obj.agent && typeof obj.agent === "object" ? obj.agent : null);
7
+ if (!agent) {
8
+ return null;
9
+ }
10
+ const agentID = typeof agent.agent_id === "string" ? agent.agent_id : "";
11
+ const apiKey = typeof agent.api_key === "string" ? agent.api_key : "";
12
+ const agentName = typeof agent.name === "string" && agent.name.trim() ? agent.name : fallbackName;
9
13
  if (!agentID || !apiKey || !agentName) {
10
14
  return null;
11
15
  }
@@ -3,9 +3,11 @@ import assert from "node:assert/strict";
3
3
  import { buildCredentialFromRegisterResult } from "./register.js";
4
4
  test("buildCredentialFromRegisterResult returns credential when response has required fields", () => {
5
5
  const out = buildCredentialFromRegisterResult({
6
- agent_id: "agent_123",
7
- api_key: "apa_123",
8
- name: "BotX"
6
+ agent: {
7
+ agent_id: "agent_123",
8
+ api_key: "apa_123",
9
+ name: "BotX"
10
+ }
9
11
  }, "http://localhost:8080/api", "FallbackBot");
10
12
  assert.ok(out);
11
13
  assert.equal(out?.agent_id, "agent_123");
@@ -14,13 +16,17 @@ test("buildCredentialFromRegisterResult returns credential when response has req
14
16
  });
15
17
  test("buildCredentialFromRegisterResult falls back to CLI name and rejects incomplete payload", () => {
16
18
  const fallback = buildCredentialFromRegisterResult({
17
- agent_id: "agent_abc",
18
- api_key: "apa_abc"
19
+ agent: {
20
+ agent_id: "agent_abc",
21
+ api_key: "apa_abc"
22
+ }
19
23
  }, "http://localhost:8080/api", "FallbackBot");
20
24
  assert.ok(fallback);
21
25
  assert.equal(fallback?.agent_name, "FallbackBot");
22
26
  const bad = buildCredentialFromRegisterResult({
23
- api_key: "apa_missing_agent"
27
+ agent: {
28
+ api_key: "apa_missing_agent"
29
+ }
24
30
  }, "http://localhost:8080/api", "FallbackBot");
25
31
  assert.equal(bad, null);
26
32
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apa-network/agent-sdk",
3
- "version": "0.2.0-beta.10",
3
+ "version": "0.2.0-beta.13",
4
4
  "description": "APA Agent SDK and CLI",
5
5
  "type": "module",
6
6
  "bin": {