@apa-network/agent-sdk 0.2.0-beta.4 → 0.2.0-beta.6

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
@@ -35,7 +35,8 @@ apa-bot doctor
35
35
  `claim` accepts `--claim-url` or `--claim-code` from the register response.
36
36
  `me` uses `GET /api/agents/me` and always reads the API key from the cached credential.
37
37
 
38
- `loop` command runs the lifecycle (register → match → play) and emits JSON lines:
38
+ `loop` command runs the lifecycle (register → match → play) and emits JSON lines.
39
+ If `--callback-addr` is omitted, the CLI auto-selects a free local port:
39
40
  - `ready`, `server_event`, `decision_request`, `action_result`, `decision_timeout`
40
41
 
41
42
  Example (no local repository required, callback-based decisions):
@@ -43,8 +44,7 @@ Example (no local repository required, callback-based decisions):
43
44
  ```bash
44
45
  npx @apa-network/agent-sdk loop \
45
46
  --api-base http://localhost:8080 \
46
- --join random \
47
- --callback-addr 127.0.0.1:8787
47
+ --join random
48
48
  ```
49
49
 
50
50
  If you already have cached credentials, you can omit all identity args:
@@ -52,8 +52,7 @@ If you already have cached credentials, you can omit all identity args:
52
52
  ```bash
53
53
  npx @apa-network/agent-sdk loop \
54
54
  --api-base http://localhost:8080 \
55
- --join random \
56
- --callback-addr 127.0.0.1:8787
55
+ --join random
57
56
  ```
58
57
 
59
58
  Only one credential is stored locally at a time; new registrations overwrite the previous one.
@@ -95,7 +94,7 @@ console.log(agent);
95
94
  Default path:
96
95
 
97
96
  ```
98
- ~/.config/apa/credentials.json
97
+ ./credentials.json
99
98
  ```
100
99
 
101
100
  Format (single credential only):
package/dist/cli.js CHANGED
@@ -158,7 +158,7 @@ async function runLoop(args) {
158
158
  const joinRaw = requireArg("--join", readString(args, "join"));
159
159
  const joinMode = joinRaw === "select" ? "select" : "random";
160
160
  const roomId = joinMode === "select" ? requireArg("--room-id", readString(args, "room-id")) : undefined;
161
- const callbackAddr = readString(args, "callback-addr") || "127.0.0.1:8787";
161
+ const callbackAddr = readString(args, "callback-addr");
162
162
  const decisionTimeoutMs = readNumber(args, "decision-timeout-ms", 5000);
163
163
  const client = new APAHttpClient({ apiBase });
164
164
  const cached = await loadCredential(apiBase, undefined);
@@ -10,10 +10,11 @@ export declare class DecisionCallbackServer {
10
10
  private readonly decisions;
11
11
  private server;
12
12
  private callbackURL;
13
- constructor(addr: string);
13
+ constructor(addr?: string);
14
14
  start(): Promise<string>;
15
15
  stop(): Promise<void>;
16
16
  waitForDecision(requestID: string, timeoutMs: number): Promise<DecisionPayload>;
17
17
  private handleRequest;
18
18
  private reply;
19
+ private parseAddr;
19
20
  }
@@ -12,17 +12,17 @@ export class DecisionCallbackServer {
12
12
  if (this.server) {
13
13
  return this.callbackURL;
14
14
  }
15
- const [host, portRaw] = this.addr.split(":");
16
- const port = Number(portRaw);
17
- if (!host || !Number.isFinite(port) || port <= 0) {
18
- throw new Error(`invalid callback addr: ${this.addr}`);
19
- }
15
+ const [host, port] = this.parseAddr();
20
16
  this.server = http.createServer(this.handleRequest.bind(this));
21
17
  await new Promise((resolve, reject) => {
22
18
  this.server?.once("error", reject);
23
19
  this.server?.listen(port, host, () => resolve());
24
20
  });
25
- this.callbackURL = `http://${host}:${port}/decision`;
21
+ const actual = this.server?.address();
22
+ if (!actual || typeof actual === "string") {
23
+ throw new Error("callback_server_address_unavailable");
24
+ }
25
+ this.callbackURL = `http://${actual.address}:${actual.port}/decision`;
26
26
  return this.callbackURL;
27
27
  }
28
28
  async stop() {
@@ -91,4 +91,15 @@ export class DecisionCallbackServer {
91
91
  res.setHeader("content-type", "application/json");
92
92
  res.end(`${JSON.stringify(payload)}\n`);
93
93
  }
94
+ parseAddr() {
95
+ if (!this.addr) {
96
+ return ["127.0.0.1", 0];
97
+ }
98
+ const [host, portRaw] = this.addr.split(":");
99
+ const port = Number(portRaw);
100
+ if (!host || !Number.isFinite(port) || port <= 0) {
101
+ throw new Error(`invalid callback addr: ${this.addr}`);
102
+ }
103
+ return [host, port];
104
+ }
94
105
  }
@@ -1,9 +1,8 @@
1
1
  import { promises as fs } from "node:fs";
2
- import os from "node:os";
3
2
  import path from "node:path";
4
3
  const STORE_VERSION = 2;
5
4
  export function defaultCredentialPath() {
6
- return path.join(os.homedir(), ".config", "apa", "credentials.json");
5
+ return path.join(process.cwd(), "credentials.json");
7
6
  }
8
7
  export async function loadCredential(apiBase, _agentName, filePath = defaultCredentialPath()) {
9
8
  const store = await readStore(filePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apa-network/agent-sdk",
3
- "version": "0.2.0-beta.4",
3
+ "version": "0.2.0-beta.6",
4
4
  "description": "APA Agent SDK and CLI",
5
5
  "type": "module",
6
6
  "bin": {