-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_client.ts
More file actions
57 lines (47 loc) · 1.54 KB
/
agent_client.ts
File metadata and controls
57 lines (47 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Minimal zinc-cli contract client for AI agents (TypeScript).
*
* Usage:
* export ZINC_WALLET_PASSWORD='...'
* npx tsx examples/agent_client.ts
*/
import { spawnSync } from "node:child_process";
type Json = Record<string, unknown>;
class ZincCliError extends Error {
envelope: Json;
constructor(envelope: Json) {
const error = (envelope.error ?? {}) as Json;
super(
`zinc-cli error type=${String(error.type)} exit_code=${String(
error.exit_code
)}: ${String(error.message)}`
);
this.envelope = envelope;
}
}
function runZinc(args: string[], correlationId?: string): Json {
const cmd = ["--agent", ...(correlationId ? ["--correlation-id", correlationId] : []), ...args];
const result = spawnSync("zinc-cli", cmd, { encoding: "utf-8" });
const stdout = (result.stdout ?? "").trim();
if (!stdout) {
throw new Error(`zinc-cli returned empty stdout (stderr=${(result.stderr ?? "").trim()})`);
}
const envelope = JSON.parse(stdout) as Json;
if (envelope.ok !== true) {
throw new ZincCliError(envelope);
}
return envelope;
}
function main(): void {
const cid = "ts-agent-demo-001";
const info = runZinc(["wallet", "info"], cid);
console.log(
`profile=${String(info.profile)} network=${String(info.network)} scheme=${String(info.scheme)}`
);
runZinc(["sync", "chain"], cid);
runZinc(["sync", "ordinals"], cid);
const balance = runZinc(["balance"], cid);
const total = ((balance.total ?? {}) as Json).confirmed;
console.log(`confirmed_sats=${String(total)}`);
}
main();