@zerox1/client 0.4.2
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/AggregatorClient.d.ts +57 -0
- package/dist/AggregatorClient.d.ts.map +1 -0
- package/dist/AggregatorClient.js +141 -0
- package/dist/AggregatorClient.js.map +1 -0
- package/dist/HostedFleet.d.ts +232 -0
- package/dist/HostedFleet.d.ts.map +1 -0
- package/dist/HostedFleet.js +375 -0
- package/dist/HostedFleet.js.map +1 -0
- package/dist/NodeClient.d.ts +72 -0
- package/dist/NodeClient.d.ts.map +1 -0
- package/dist/NodeClient.js +151 -0
- package/dist/NodeClient.js.map +1 -0
- package/dist/codec.d.ts +5 -0
- package/dist/codec.d.ts.map +1 -0
- package/dist/codec.js +23 -0
- package/dist/codec.js.map +1 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +72 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +54 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AggregatorClient — HTTP + WebSocket client for the 0x01 aggregator REST API.
|
|
3
|
+
*
|
|
4
|
+
* The aggregator is a separate service from the node. It tracks reputation,
|
|
5
|
+
* agent discovery, and activity across the mesh. This client is read-mostly.
|
|
6
|
+
*
|
|
7
|
+
* Public aggregator: https://agg.0x01.world
|
|
8
|
+
*/
|
|
9
|
+
import type { AgentRecord, AgentProfile, ActivityEvent, NetworkStats, HostingNode, AgentsParams, ActivityParams } from './types.js';
|
|
10
|
+
export interface AggregatorClientOptions {
|
|
11
|
+
/** Aggregator base URL, e.g. "https://agg.0x01.world" */
|
|
12
|
+
url: string;
|
|
13
|
+
/** API key for gated read endpoints (reputation, leaderboard, etc.) */
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare class AggregatorClient {
|
|
17
|
+
private readonly baseUrl;
|
|
18
|
+
private readonly apiKey;
|
|
19
|
+
constructor(opts: AggregatorClientOptions);
|
|
20
|
+
private headers;
|
|
21
|
+
private get;
|
|
22
|
+
/** List agents on the mesh, optionally filtered by country or capabilities. */
|
|
23
|
+
agents(params?: AgentsParams): Promise<AgentRecord[]>;
|
|
24
|
+
/** Get full profile for a single agent (reputation, capabilities, recent disputes). */
|
|
25
|
+
agentProfile(agentId: string): Promise<AgentProfile>;
|
|
26
|
+
/** Reverse-lookup: get all agents registered to a wallet address. */
|
|
27
|
+
agentsByOwner(wallet: string): Promise<AgentRecord[]>;
|
|
28
|
+
/** Get the owner record for an agent (unclaimed / pending / claimed). */
|
|
29
|
+
agentOwner(agentId: string): Promise<unknown>;
|
|
30
|
+
/** Detailed reputation snapshot. Requires API key. */
|
|
31
|
+
reputation(agentId: string): Promise<unknown>;
|
|
32
|
+
/**
|
|
33
|
+
* Fetch recent activity events (JOIN, FEEDBACK, DISPUTE, VERDICT).
|
|
34
|
+
* Use `params.before` for cursor pagination (pass last seen `id`).
|
|
35
|
+
*/
|
|
36
|
+
activity(params?: ActivityParams): Promise<ActivityEvent[]>;
|
|
37
|
+
/**
|
|
38
|
+
* Subscribe to real-time activity events via WebSocket.
|
|
39
|
+
* Returns an unsubscribe function.
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* const stop = agg.watchActivity((event) => {
|
|
43
|
+
* console.log(event.event_type, event.agent_id)
|
|
44
|
+
* })
|
|
45
|
+
* // later:
|
|
46
|
+
* stop()
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
watchActivity(handler: (event: ActivityEvent) => void): () => void;
|
|
50
|
+
/** High-level stats: total agents, total interactions, uptime. */
|
|
51
|
+
networkStats(): Promise<NetworkStats>;
|
|
52
|
+
/** List currently active hosting nodes (seen in last 120s). */
|
|
53
|
+
hostingNodes(): Promise<HostingNode[]>;
|
|
54
|
+
/** Download a blob by CID. Returns raw Response for streaming. */
|
|
55
|
+
downloadBlob(cid: string): Promise<Response>;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=AggregatorClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AggregatorClient.d.ts","sourceRoot":"","sources":["../src/AggregatorClient.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,cAAc,EACf,MAAM,YAAY,CAAA;AAEnB,MAAM,WAAW,uBAAuB;IACtC,yDAAyD;IACzD,GAAG,EAAE,MAAM,CAAA;IACX,uEAAuE;IACvE,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAQ;IAChC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoB;gBAE/B,IAAI,EAAE,uBAAuB;IAOzC,OAAO,CAAC,OAAO;YAMD,GAAG;IAgBjB,+EAA+E;IACzE,MAAM,CAAC,MAAM,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAQ3D,uFAAuF;IACjF,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAI1D,qEAAqE;IAC/D,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAI3D,yEAAyE;IACnE,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAMnD,sDAAsD;IAChD,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAMnD;;;OAGG;IACG,QAAQ,CAAC,MAAM,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;IAOjE;;;;;;;;;;;OAWG;IACH,aAAa,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,GAAG,MAAM,IAAI;IA4BlE,kEAAkE;IAC5D,YAAY,IAAI,OAAO,CAAC,YAAY,CAAC;IAM3C,+DAA+D;IACzD,YAAY,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IAM5C,kEAAkE;IAC5D,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;CAKnD"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* AggregatorClient — HTTP + WebSocket client for the 0x01 aggregator REST API.
|
|
4
|
+
*
|
|
5
|
+
* The aggregator is a separate service from the node. It tracks reputation,
|
|
6
|
+
* agent discovery, and activity across the mesh. This client is read-mostly.
|
|
7
|
+
*
|
|
8
|
+
* Public aggregator: https://agg.0x01.world
|
|
9
|
+
*/
|
|
10
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
11
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.AggregatorClient = void 0;
|
|
15
|
+
const ws_1 = __importDefault(require("ws"));
|
|
16
|
+
class AggregatorClient {
|
|
17
|
+
constructor(opts) {
|
|
18
|
+
this.baseUrl = opts.url.replace(/\/$/, '');
|
|
19
|
+
this.apiKey = opts.apiKey;
|
|
20
|
+
}
|
|
21
|
+
// ── HTTP helpers ──────────────────────────────────────────────────────────
|
|
22
|
+
headers() {
|
|
23
|
+
const h = {};
|
|
24
|
+
if (this.apiKey)
|
|
25
|
+
h['Authorization'] = `Bearer ${this.apiKey}`;
|
|
26
|
+
return h;
|
|
27
|
+
}
|
|
28
|
+
async get(path, params) {
|
|
29
|
+
let url = `${this.baseUrl}${path}`;
|
|
30
|
+
if (params) {
|
|
31
|
+
const qs = Object.entries(params)
|
|
32
|
+
.filter(([, v]) => v !== undefined)
|
|
33
|
+
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
|
|
34
|
+
.join('&');
|
|
35
|
+
if (qs)
|
|
36
|
+
url += `?${qs}`;
|
|
37
|
+
}
|
|
38
|
+
const res = await fetch(url, { headers: this.headers() });
|
|
39
|
+
if (!res.ok)
|
|
40
|
+
throw new Error(`GET ${path} → ${res.status}: ${await res.text()}`);
|
|
41
|
+
return res.json();
|
|
42
|
+
}
|
|
43
|
+
// ── Agent discovery ───────────────────────────────────────────────────────
|
|
44
|
+
/** List agents on the mesh, optionally filtered by country or capabilities. */
|
|
45
|
+
async agents(params) {
|
|
46
|
+
return this.get('/agents', {
|
|
47
|
+
limit: params?.limit,
|
|
48
|
+
country: params?.country,
|
|
49
|
+
capabilities: params?.capabilities,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** Get full profile for a single agent (reputation, capabilities, recent disputes). */
|
|
53
|
+
async agentProfile(agentId) {
|
|
54
|
+
return this.get(`/agents/${agentId}/profile`);
|
|
55
|
+
}
|
|
56
|
+
/** Reverse-lookup: get all agents registered to a wallet address. */
|
|
57
|
+
async agentsByOwner(wallet) {
|
|
58
|
+
return this.get(`/agents/by-owner/${wallet}`);
|
|
59
|
+
}
|
|
60
|
+
/** Get the owner record for an agent (unclaimed / pending / claimed). */
|
|
61
|
+
async agentOwner(agentId) {
|
|
62
|
+
return this.get(`/agents/${agentId}/owner`);
|
|
63
|
+
}
|
|
64
|
+
// ── Reputation (API-key gated) ────────────────────────────────────────────
|
|
65
|
+
/** Detailed reputation snapshot. Requires API key. */
|
|
66
|
+
async reputation(agentId) {
|
|
67
|
+
return this.get(`/reputation/${agentId}`);
|
|
68
|
+
}
|
|
69
|
+
// ── Activity feed ─────────────────────────────────────────────────────────
|
|
70
|
+
/**
|
|
71
|
+
* Fetch recent activity events (JOIN, FEEDBACK, DISPUTE, VERDICT).
|
|
72
|
+
* Use `params.before` for cursor pagination (pass last seen `id`).
|
|
73
|
+
*/
|
|
74
|
+
async activity(params) {
|
|
75
|
+
return this.get('/activity', {
|
|
76
|
+
limit: params?.limit,
|
|
77
|
+
before: params?.before,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Subscribe to real-time activity events via WebSocket.
|
|
82
|
+
* Returns an unsubscribe function.
|
|
83
|
+
*
|
|
84
|
+
* ```ts
|
|
85
|
+
* const stop = agg.watchActivity((event) => {
|
|
86
|
+
* console.log(event.event_type, event.agent_id)
|
|
87
|
+
* })
|
|
88
|
+
* // later:
|
|
89
|
+
* stop()
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
watchActivity(handler) {
|
|
93
|
+
const wsUrl = this.baseUrl.replace(/^http/, 'ws') + '/ws/activity';
|
|
94
|
+
let ws = null;
|
|
95
|
+
let closed = false;
|
|
96
|
+
let reconnectTimer = null;
|
|
97
|
+
const connect = () => {
|
|
98
|
+
if (closed)
|
|
99
|
+
return;
|
|
100
|
+
ws = new ws_1.default(wsUrl);
|
|
101
|
+
ws.on('message', (data) => {
|
|
102
|
+
try {
|
|
103
|
+
handler(JSON.parse(data.toString()));
|
|
104
|
+
}
|
|
105
|
+
catch { /* ignore */ }
|
|
106
|
+
});
|
|
107
|
+
ws.on('close', () => {
|
|
108
|
+
if (!closed)
|
|
109
|
+
reconnectTimer = setTimeout(connect, 3000);
|
|
110
|
+
});
|
|
111
|
+
ws.on('error', () => { });
|
|
112
|
+
};
|
|
113
|
+
connect();
|
|
114
|
+
return () => {
|
|
115
|
+
closed = true;
|
|
116
|
+
if (reconnectTimer)
|
|
117
|
+
clearTimeout(reconnectTimer);
|
|
118
|
+
ws?.close();
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
// ── Network stats ─────────────────────────────────────────────────────────
|
|
122
|
+
/** High-level stats: total agents, total interactions, uptime. */
|
|
123
|
+
async networkStats() {
|
|
124
|
+
return this.get('/stats/network');
|
|
125
|
+
}
|
|
126
|
+
// ── Hosting nodes ─────────────────────────────────────────────────────────
|
|
127
|
+
/** List currently active hosting nodes (seen in last 120s). */
|
|
128
|
+
async hostingNodes() {
|
|
129
|
+
return this.get('/hosting/nodes');
|
|
130
|
+
}
|
|
131
|
+
// ── Blobs ─────────────────────────────────────────────────────────────────
|
|
132
|
+
/** Download a blob by CID. Returns raw Response for streaming. */
|
|
133
|
+
async downloadBlob(cid) {
|
|
134
|
+
const res = await fetch(`${this.baseUrl}/blobs/${cid}`, { headers: this.headers() });
|
|
135
|
+
if (!res.ok)
|
|
136
|
+
throw new Error(`GET /blobs/${cid} → ${res.status}`);
|
|
137
|
+
return res;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
exports.AggregatorClient = AggregatorClient;
|
|
141
|
+
//# sourceMappingURL=AggregatorClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AggregatorClient.js","sourceRoot":"","sources":["../src/AggregatorClient.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;AAEH,4CAA0B;AAkB1B,MAAa,gBAAgB;IAI3B,YAAY,IAA6B;QACvC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAA;QAC1C,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAA;IAC3B,CAAC;IAED,6EAA6E;IAErE,OAAO;QACb,MAAM,CAAC,GAA2B,EAAE,CAAA;QACpC,IAAI,IAAI,CAAC,MAAM;YAAE,CAAC,CAAC,eAAe,CAAC,GAAG,UAAU,IAAI,CAAC,MAAM,EAAE,CAAA;QAC7D,OAAO,CAAC,CAAA;IACV,CAAC;IAEO,KAAK,CAAC,GAAG,CAAI,IAAY,EAAE,MAAoD;QACrF,IAAI,GAAG,GAAG,GAAG,IAAI,CAAC,OAAO,GAAG,IAAI,EAAE,CAAA;QAClC,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,EAAE,GAAG,MAAM,CAAC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G,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;YACzD,CAAC,CAAC,CAAA;YACF,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,GAA0B,CAAC,CAAC,CAAA;QAClD,CAAC,CAAA;QAED,OAAO,EAAE,CAAA;QACT,OAAO,GAAG,EAAE;YACV,MAAM,GAAG,IAAI,CAAA;YACb,IAAI,cAAc;gBAAE,YAAY,CAAC,cAAc,CAAC,CAAA;YAChD,EAAE,EAAE,KAAK,EAAE,CAAA;QACb,CAAC,CAAA;IACH,CAAC;IAED,6EAA6E;IAE7E,kEAAkE;IAClE,KAAK,CAAC,YAAY;QAChB,OAAO,IAAI,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAA;IACnC,CAAC;IAED,6EAA6E;IAE7E,+DAA+D;IAC/D,KAAK,CAAC,YAAY;QAChB,OAAO,IAAI,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAA;IACnC,CAAC;IAED,6EAA6E;IAE7E,kEAAkE;IAClE,KAAK,CAAC,YAAY,CAAC,GAAW;QAC5B,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,OAAO,UAAU,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;QACpF,IAAI,CAAC,GAAG,CAAC,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,cAAc,GAAG,MAAM,GAAG,CAAC,MAAM,EAAE,CAAC,CAAA;QACjE,OAAO,GAAG,CAAA;IACZ,CAAC;CACF;AAzID,4CAyIC"}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HostedFleet — manages a pool of hosted agents on a single node.
|
|
3
|
+
*
|
|
4
|
+
* Hosted agents are sub-identities created by the node with their own
|
|
5
|
+
* Ed25519 keypairs. Apps use them to participate in the mesh without
|
|
6
|
+
* running their own node processes. The fleet handles:
|
|
7
|
+
*
|
|
8
|
+
* - Registration + token caching (survives app restarts via TokenStore)
|
|
9
|
+
* - Per-agent inbox WebSocket with auto-reconnect
|
|
10
|
+
* - Conversation thread tracking (both inbound and outbound)
|
|
11
|
+
* - Re-registration when node restarts (token invalidation)
|
|
12
|
+
*
|
|
13
|
+
* For multi-node deployments (distributing agents across different nodes),
|
|
14
|
+
* see `MultiFleet`.
|
|
15
|
+
*/
|
|
16
|
+
import type { InboundEnvelope, MsgType } from './types.js';
|
|
17
|
+
import type { ProposeParams, CounterParams, AcceptParams } from './NodeClient.js';
|
|
18
|
+
/** A tracked message thread between agents. */
|
|
19
|
+
export declare class Conversation {
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly messages: InboundEnvelope[];
|
|
22
|
+
readonly participants: Set<string>;
|
|
23
|
+
constructor(id: string);
|
|
24
|
+
/** @internal */
|
|
25
|
+
_push(env: InboundEnvelope): void;
|
|
26
|
+
lastMessage(): InboundEnvelope | undefined;
|
|
27
|
+
history(): InboundEnvelope[];
|
|
28
|
+
}
|
|
29
|
+
type Handler = (env: InboundEnvelope, conv: Conversation) => void;
|
|
30
|
+
export interface HostedAgentOptions {
|
|
31
|
+
nodeUrl: string;
|
|
32
|
+
agentId: string;
|
|
33
|
+
token: string;
|
|
34
|
+
}
|
|
35
|
+
export interface HostedSendParams {
|
|
36
|
+
msgType: MsgType;
|
|
37
|
+
recipient?: string;
|
|
38
|
+
conversationId?: string;
|
|
39
|
+
payload: Uint8Array;
|
|
40
|
+
}
|
|
41
|
+
/** A single hosted agent identity on a node. */
|
|
42
|
+
export declare class HostedAgent {
|
|
43
|
+
readonly agentId: string;
|
|
44
|
+
readonly token: string;
|
|
45
|
+
private readonly nodeUrl;
|
|
46
|
+
private readonly conversations;
|
|
47
|
+
private readonly handlers;
|
|
48
|
+
private ws;
|
|
49
|
+
private wsClosed;
|
|
50
|
+
private wsReconnectTimer;
|
|
51
|
+
constructor(opts: HostedAgentOptions);
|
|
52
|
+
/** Register a message handler. Use '*' to catch all types. Returns `this` for chaining. */
|
|
53
|
+
on(msgType: MsgType | '*', handler: Handler): this;
|
|
54
|
+
off(msgType: MsgType | '*', handler: Handler): this;
|
|
55
|
+
/**
|
|
56
|
+
* Start listening to the inbox WebSocket.
|
|
57
|
+
* Call this once after registering all handlers.
|
|
58
|
+
* Returns an unsubscribe/cleanup function.
|
|
59
|
+
*/
|
|
60
|
+
listen(): () => void;
|
|
61
|
+
private _connectWs;
|
|
62
|
+
private _emit;
|
|
63
|
+
private _getOrCreateConv;
|
|
64
|
+
/**
|
|
65
|
+
* Get a conversation by ID.
|
|
66
|
+
*
|
|
67
|
+
* Available immediately after any outbound send (propose/counter/accept/send)
|
|
68
|
+
* that uses or generates a conversation_id — not just after the first inbound
|
|
69
|
+
* reply arrives.
|
|
70
|
+
*/
|
|
71
|
+
conversation(id: string): Conversation | undefined;
|
|
72
|
+
allConversations(): Conversation[];
|
|
73
|
+
private _post;
|
|
74
|
+
/**
|
|
75
|
+
* Send a raw message to a recipient (or broadcast if recipient omitted).
|
|
76
|
+
* Pre-creates a Conversation so `agent.conversation(conversationId)` is
|
|
77
|
+
* available immediately after this call.
|
|
78
|
+
*/
|
|
79
|
+
send(params: HostedSendParams): Promise<{
|
|
80
|
+
conversation_id: string;
|
|
81
|
+
}>;
|
|
82
|
+
/**
|
|
83
|
+
* Propose a deal to a recipient.
|
|
84
|
+
* The returned conversation_id is immediately available via `agent.conversation(id)`.
|
|
85
|
+
*
|
|
86
|
+
* ```ts
|
|
87
|
+
* const { conversation_id } = await agent.propose({
|
|
88
|
+
* recipient: otherAgent.agentId,
|
|
89
|
+
* message: 'Build a REST API for our product',
|
|
90
|
+
* amountMicro: 10_000_000n, // 10 USDC
|
|
91
|
+
* })
|
|
92
|
+
* // conversation is already tracked:
|
|
93
|
+
* console.log(agent.conversation(conversation_id))
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
propose(params: ProposeParams): Promise<{
|
|
97
|
+
conversation_id: string;
|
|
98
|
+
}>;
|
|
99
|
+
counter(params: CounterParams): Promise<{
|
|
100
|
+
conversation_id: string;
|
|
101
|
+
}>;
|
|
102
|
+
accept(params: AcceptParams): Promise<{
|
|
103
|
+
conversation_id: string;
|
|
104
|
+
}>;
|
|
105
|
+
ping(): Promise<boolean>;
|
|
106
|
+
dispose(): void;
|
|
107
|
+
}
|
|
108
|
+
export interface TokenStore {
|
|
109
|
+
get(key: string): Promise<{
|
|
110
|
+
agentId: string;
|
|
111
|
+
token: string;
|
|
112
|
+
} | undefined>;
|
|
113
|
+
set(key: string, value: {
|
|
114
|
+
agentId: string;
|
|
115
|
+
token: string;
|
|
116
|
+
}): Promise<void>;
|
|
117
|
+
delete(key: string): Promise<void>;
|
|
118
|
+
}
|
|
119
|
+
export interface HostedFleetOptions {
|
|
120
|
+
/** Node API base URL */
|
|
121
|
+
nodeUrl: string;
|
|
122
|
+
/**
|
|
123
|
+
* Optional token store — implement to persist tokens across restarts.
|
|
124
|
+
* If omitted, tokens are kept in memory only (lost on process exit).
|
|
125
|
+
*
|
|
126
|
+
* Example SQLite implementation:
|
|
127
|
+
* ```ts
|
|
128
|
+
* const store: TokenStore = {
|
|
129
|
+
* get: async (key) => db.get('SELECT agent_id, token FROM identities WHERE role = ?', key)
|
|
130
|
+
* .then(r => r ? { agentId: r.agent_id, token: r.token } : undefined),
|
|
131
|
+
* set: async (key, { agentId, token }) =>
|
|
132
|
+
* db.run('INSERT OR REPLACE INTO identities (role, agent_id, token) VALUES (?, ?, ?)', key, agentId, token),
|
|
133
|
+
* delete: async (key) => db.run('DELETE FROM identities WHERE role = ?', key),
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
store?: TokenStore;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Manages a named pool of hosted agents on a single node.
|
|
141
|
+
*
|
|
142
|
+
* ```ts
|
|
143
|
+
* const fleet = new HostedFleet({ nodeUrl: 'http://localhost:9090', store })
|
|
144
|
+
* const ceo = await fleet.register('ceo')
|
|
145
|
+
* const dev = await fleet.register('dev')
|
|
146
|
+
*
|
|
147
|
+
* ceo.on('PROPOSE', async (env, conv) => {
|
|
148
|
+
* const p = decodeProposePayload(env.payload_b64)
|
|
149
|
+
* if (!p) return
|
|
150
|
+
* await ceo.accept({ recipient: env.sender, conversationId: env.conversation_id, amountMicro: p.amount_micro })
|
|
151
|
+
* }).listen()
|
|
152
|
+
*
|
|
153
|
+
* // propose() returns a conversation_id that is immediately trackable
|
|
154
|
+
* const { conversation_id } = await ceo.propose({
|
|
155
|
+
* recipient: dev.agentId,
|
|
156
|
+
* message: 'Build a REST API',
|
|
157
|
+
* amountMicro: 10_000_000n,
|
|
158
|
+
* })
|
|
159
|
+
* console.log(ceo.conversation(conversation_id)) // already exists
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
export declare class HostedFleet {
|
|
163
|
+
private readonly nodeUrl;
|
|
164
|
+
private readonly store?;
|
|
165
|
+
private readonly agents;
|
|
166
|
+
constructor(opts: HostedFleetOptions);
|
|
167
|
+
/**
|
|
168
|
+
* Register a named hosted agent.
|
|
169
|
+
* Reuses a cached token if valid (ping-validates first).
|
|
170
|
+
* Re-registers automatically on token expiry or node restart.
|
|
171
|
+
*/
|
|
172
|
+
register(name: string): Promise<HostedAgent>;
|
|
173
|
+
get(name: string): HostedAgent | undefined;
|
|
174
|
+
all(): HostedAgent[];
|
|
175
|
+
dispose(): void;
|
|
176
|
+
}
|
|
177
|
+
export interface MultiFleetOptions {
|
|
178
|
+
/**
|
|
179
|
+
* Named node URLs. Each key is a node name; the value is its API base URL.
|
|
180
|
+
*
|
|
181
|
+
* ```ts
|
|
182
|
+
* nodes: {
|
|
183
|
+
* primary: 'http://node1.corp.com:9090',
|
|
184
|
+
* secondary: 'http://node2.corp.com:9090',
|
|
185
|
+
* }
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
nodes: Record<string, string>;
|
|
189
|
+
/** Shared token store. Keys are scoped as `<nodeName>:<agentName>`. */
|
|
190
|
+
store?: TokenStore;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Manages hosted agents distributed across multiple nodes.
|
|
194
|
+
*
|
|
195
|
+
* Use this when different agent roles run on different zerox1-node instances —
|
|
196
|
+
* for example, public-facing agents on one node and internal agents on another.
|
|
197
|
+
*
|
|
198
|
+
* ```ts
|
|
199
|
+
* const multi = new MultiFleet({
|
|
200
|
+
* nodes: {
|
|
201
|
+
* us: 'http://us-node:9090',
|
|
202
|
+
* eu: 'http://eu-node:9090',
|
|
203
|
+
* },
|
|
204
|
+
* store,
|
|
205
|
+
* })
|
|
206
|
+
*
|
|
207
|
+
* const ceo = await multi.register('ceo', 'us')
|
|
208
|
+
* const dev = await multi.register('dev', 'us')
|
|
209
|
+
* const sales = await multi.register('sales', 'eu')
|
|
210
|
+
* const analyst = await multi.register('analyst', 'eu')
|
|
211
|
+
*
|
|
212
|
+
* // All agents work exactly like single-fleet HostedAgent instances
|
|
213
|
+
* ceo.on('PROPOSE', handler).listen()
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
export declare class MultiFleet {
|
|
217
|
+
private readonly fleets;
|
|
218
|
+
private readonly agents;
|
|
219
|
+
constructor(opts: MultiFleetOptions);
|
|
220
|
+
/**
|
|
221
|
+
* Register a named agent on a specific node.
|
|
222
|
+
* @param agentName - Unique name for this agent (e.g. 'ceo', 'dev')
|
|
223
|
+
* @param nodeName - Which node to register on (must be in `nodes` config)
|
|
224
|
+
*/
|
|
225
|
+
register(agentName: string, nodeName: string): Promise<HostedAgent>;
|
|
226
|
+
get(agentName: string): HostedAgent | undefined;
|
|
227
|
+
all(): HostedAgent[];
|
|
228
|
+
fleet(nodeName: string): HostedFleet | undefined;
|
|
229
|
+
dispose(): void;
|
|
230
|
+
}
|
|
231
|
+
export {};
|
|
232
|
+
//# sourceMappingURL=HostedFleet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Host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