@unagents/messenger-protocol 0.0.0-stage → 0.9.0
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.mts +394 -0
- package/dist/index.d.ts +394 -0
- package/dist/index.mjs +65 -0
- package/package.json +27 -4
- package/README.md +0 -3
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protocol constants shared by broker, client and CLI.
|
|
3
|
+
*
|
|
4
|
+
* Values are transcribed from the current JavaScript implementation
|
|
5
|
+
* (`legacy/src/config.js`, `legacy/src/broker.js`, `legacy/src/server.js`). Do not change them here
|
|
6
|
+
* without changing the implementation on both ends.
|
|
7
|
+
*/
|
|
8
|
+
/** Broker HTTP listen port (`AGENT_MESSENGER_PORT` default). */
|
|
9
|
+
declare const DEFAULT_HTTP_PORT = 7901;
|
|
10
|
+
/** Broker HTTP bind address (`AGENT_MESSENGER_BIND` default). */
|
|
11
|
+
declare const DEFAULT_BIND = "0.0.0.0";
|
|
12
|
+
/** UDP discovery port (`AGENT_MESSENGER_DISCOVERY_PORT` default). */
|
|
13
|
+
declare const DEFAULT_DISCOVERY_PORT = 7902;
|
|
14
|
+
/** How long a client waits for discovery answers (`AGENT_MESSENGER_DISCOVERY_TIMEOUT_MS` default). */
|
|
15
|
+
declare const DEFAULT_DISCOVERY_TIMEOUT_MS = 1000;
|
|
16
|
+
/** Client heartbeat interval (`AGENT_MESSENGER_HEARTBEAT_MS` default). */
|
|
17
|
+
declare const HEARTBEAT_INTERVAL_MS = 15000;
|
|
18
|
+
/** Broker drops a peer whose `last_seen` is older than this. Not configurable. */
|
|
19
|
+
declare const HEARTBEAT_TIMEOUT_MS = 60000;
|
|
20
|
+
/** Client `/poll-messages` interval. Not configurable. */
|
|
21
|
+
declare const POLL_INTERVAL_MS = 1000;
|
|
22
|
+
/** Loopback hosts treated as "local broker" by the client resolver. */
|
|
23
|
+
declare const LOCAL_HOSTS: readonly ["127.0.0.1", "localhost", "::1"];
|
|
24
|
+
/**
|
|
25
|
+
* Environment variable names. Source of truth: `legacy/src/config.js`
|
|
26
|
+
* (`DELIVERY` lives in `legacy/src/server.js`).
|
|
27
|
+
*/
|
|
28
|
+
declare const ENV: {
|
|
29
|
+
/** Explicit broker host. `"auto"` or unset = resolve by config / discovery / local. */
|
|
30
|
+
readonly HOST: "AGENT_MESSENGER_HOST";
|
|
31
|
+
/** Broker HTTP port (client target / broker listen). */
|
|
32
|
+
readonly PORT: "AGENT_MESSENGER_PORT";
|
|
33
|
+
/** Broker bind address. */
|
|
34
|
+
readonly BIND: "AGENT_MESSENGER_BIND";
|
|
35
|
+
/** Bearer token; when set, every POST must carry `Authorization: Bearer <token>`. */
|
|
36
|
+
readonly TOKEN: "AGENT_MESSENGER_TOKEN";
|
|
37
|
+
/** Peer name for this session. */
|
|
38
|
+
readonly NAME: "AGENT_MESSENGER_NAME";
|
|
39
|
+
/** Broker id shown in `/health` and discovery replies (default hostname). */
|
|
40
|
+
readonly BROKER_ID: "AGENT_MESSENGER_BROKER_ID";
|
|
41
|
+
/** Broker state file (default `~/.agent-messenger/broker.json`). */
|
|
42
|
+
readonly STATE: "AGENT_MESSENGER_STATE";
|
|
43
|
+
/** Client config file (default `~/.agent-messenger/config.json`). */
|
|
44
|
+
readonly CONFIG: "AGENT_MESSENGER_CONFIG";
|
|
45
|
+
/** `"off"` disables UDP discovery (broker responder and client probe). */
|
|
46
|
+
readonly DISCOVERY: "AGENT_MESSENGER_DISCOVERY";
|
|
47
|
+
/** UDP discovery port. */
|
|
48
|
+
readonly DISCOVERY_PORT: "AGENT_MESSENGER_DISCOVERY_PORT";
|
|
49
|
+
/** Discovery probe target address. */
|
|
50
|
+
readonly DISCOVERY_ADDR: "AGENT_MESSENGER_DISCOVERY_ADDR";
|
|
51
|
+
/** Discovery answer wait time in ms. */
|
|
52
|
+
readonly DISCOVERY_TIMEOUT_MS: "AGENT_MESSENGER_DISCOVERY_TIMEOUT_MS";
|
|
53
|
+
/** Client heartbeat interval in ms. */
|
|
54
|
+
readonly HEARTBEAT_MS: "AGENT_MESSENGER_HEARTBEAT_MS";
|
|
55
|
+
/** Inbound delivery mode: `"auto"` (default) / `"socket"` / `"channel"`. */
|
|
56
|
+
readonly DELIVERY: "AGENT_MESSENGER_DELIVERY";
|
|
57
|
+
};
|
|
58
|
+
type EnvName = (typeof ENV)[keyof typeof ENV];
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Broker HTTP API — request / response JSON shapes.
|
|
62
|
+
*
|
|
63
|
+
* Transcribed from `legacy/src/broker.js` (handlers) and `legacy/src/server.js` /
|
|
64
|
+
* `legacy/bin/agent-messenger.js` (callers). Field optionality follows the broker:
|
|
65
|
+
* a field the broker defaults with `??` is optional here.
|
|
66
|
+
*
|
|
67
|
+
* Transport rules (from `legacy/src/broker.js` `startBroker`):
|
|
68
|
+
* - `GET /health` is the only GET route.
|
|
69
|
+
* - Every other route is `POST` with a JSON body; empty body is treated as `{}`.
|
|
70
|
+
* - When the broker has a token, each POST must send `Authorization: Bearer <token>`,
|
|
71
|
+
* otherwise `401 {"error":"unauthorized"}`.
|
|
72
|
+
* - Unknown POST path: `404 {"error":"not found"}`. Handler exception: `500 {"error":<message>}`.
|
|
73
|
+
* - Any other method: `200 text/plain` `agent-messenger broker <version>\n`.
|
|
74
|
+
*/
|
|
75
|
+
/** A registered session as stored and returned by the broker. */
|
|
76
|
+
interface Peer {
|
|
77
|
+
/** 8-char broker-generated id (`[a-z0-9]`). */
|
|
78
|
+
id: string;
|
|
79
|
+
/** Stable name set via `/register` or `/set-name`; `""` when unset. */
|
|
80
|
+
name: string;
|
|
81
|
+
/** Client process id. */
|
|
82
|
+
pid: number;
|
|
83
|
+
hostname: string;
|
|
84
|
+
cwd: string;
|
|
85
|
+
git_root: string | null;
|
|
86
|
+
tty: string | null;
|
|
87
|
+
summary: string;
|
|
88
|
+
/** ISO 8601 timestamp. */
|
|
89
|
+
registered_at: string;
|
|
90
|
+
/** ISO 8601 timestamp; refreshed by `/heartbeat`. */
|
|
91
|
+
last_seen: string;
|
|
92
|
+
/**
|
|
93
|
+
* Mesh only: id of the permanent broker this peer is registered with, set on
|
|
94
|
+
* copies a mesh broker replicated from another one. Absent for peers that
|
|
95
|
+
* registered directly. Never returned by `/list-peers`.
|
|
96
|
+
*/
|
|
97
|
+
origin?: string;
|
|
98
|
+
}
|
|
99
|
+
type MessageMode = "pull" | "push" | "ping";
|
|
100
|
+
/** A message as stored and returned by the broker. */
|
|
101
|
+
interface Message {
|
|
102
|
+
/** Broker-assigned incrementing integer. */
|
|
103
|
+
id: number;
|
|
104
|
+
/** Sender peer id, or `"cli"` when sent from the CLI. */
|
|
105
|
+
from_id: string;
|
|
106
|
+
from_name: string;
|
|
107
|
+
to_id: string;
|
|
108
|
+
text: string;
|
|
109
|
+
/**
|
|
110
|
+
* How the receiver shows it: `"pull"` = unread notice only, text via
|
|
111
|
+
* `check_messages`; `"push"` = text shown directly; `"ping"` = liveness
|
|
112
|
+
* notice; `text` is its optional subject (e.g. a task id), `""` when none. Missing (older broker / sender) means `"pull"`.
|
|
113
|
+
*/
|
|
114
|
+
mode?: MessageMode;
|
|
115
|
+
/** ISO 8601 timestamp. */
|
|
116
|
+
sent_at: string;
|
|
117
|
+
/** 0 until returned by `/poll-messages`, then 1. */
|
|
118
|
+
delivered: 0 | 1;
|
|
119
|
+
/** Always 0 in the current implementation (acknowledged messages are deleted). */
|
|
120
|
+
acknowledged: 0 | 1;
|
|
121
|
+
/** Mesh only: `<sender broker id>:<id there>` of a message forwarded by another broker (duplicate guard). */
|
|
122
|
+
mesh_key?: string;
|
|
123
|
+
}
|
|
124
|
+
/** `/list-peers` scope filter. Unknown values behave as `"all"`. */
|
|
125
|
+
type PeerScope = "all" | "host" | "directory" | "repo";
|
|
126
|
+
/** Generic `{ok:true}` / `{ok:false,error}` result. */
|
|
127
|
+
type OkResult = {
|
|
128
|
+
ok: true;
|
|
129
|
+
} | {
|
|
130
|
+
ok: false;
|
|
131
|
+
error: string;
|
|
132
|
+
};
|
|
133
|
+
/** Body of a non-2xx broker response. */
|
|
134
|
+
interface BrokerErrorResponse {
|
|
135
|
+
error: string;
|
|
136
|
+
}
|
|
137
|
+
interface HealthResponse {
|
|
138
|
+
status: "ok";
|
|
139
|
+
/** Broker package version. */
|
|
140
|
+
version: string;
|
|
141
|
+
/** Broker id (`AGENT_MESSENGER_BROKER_ID`, default hostname). */
|
|
142
|
+
id: string;
|
|
143
|
+
/** Broker HTTP port. */
|
|
144
|
+
port: number;
|
|
145
|
+
/** Count of peers seen within `HEARTBEAT_TIMEOUT_MS`. */
|
|
146
|
+
peers: number;
|
|
147
|
+
/** Present (`true`) only when the broker was started as a local fallback (`AGENT_MESSENGER_FALLBACK=1`). */
|
|
148
|
+
fallback?: true;
|
|
149
|
+
}
|
|
150
|
+
interface RegisterRequest {
|
|
151
|
+
pid: number;
|
|
152
|
+
hostname?: string;
|
|
153
|
+
cwd?: string;
|
|
154
|
+
git_root?: string | null;
|
|
155
|
+
tty?: string | null;
|
|
156
|
+
summary?: string;
|
|
157
|
+
name?: string;
|
|
158
|
+
}
|
|
159
|
+
interface RegisterResponse {
|
|
160
|
+
id: string;
|
|
161
|
+
}
|
|
162
|
+
interface HeartbeatRequest {
|
|
163
|
+
id: string;
|
|
164
|
+
}
|
|
165
|
+
/** `ok:false` with `"unknown peer (re-register)"` when the broker does not know the id. */
|
|
166
|
+
type HeartbeatResponse = OkResult;
|
|
167
|
+
interface SetSummaryRequest {
|
|
168
|
+
id: string;
|
|
169
|
+
summary?: string;
|
|
170
|
+
}
|
|
171
|
+
type SetSummaryResponse = OkResult;
|
|
172
|
+
interface SetNameRequest {
|
|
173
|
+
id: string;
|
|
174
|
+
name?: string;
|
|
175
|
+
}
|
|
176
|
+
type SetNameResponse = OkResult;
|
|
177
|
+
interface ListPeersRequest {
|
|
178
|
+
scope?: PeerScope;
|
|
179
|
+
/** Used by scope `"host"`. */
|
|
180
|
+
hostname?: string;
|
|
181
|
+
/** Used by scope `"directory"`, and by `"repo"` when `git_root` is empty. */
|
|
182
|
+
cwd?: string;
|
|
183
|
+
/** Used by scope `"repo"`. */
|
|
184
|
+
git_root?: string | null;
|
|
185
|
+
/** Peer id to omit from the result (the caller itself). */
|
|
186
|
+
exclude_id?: string;
|
|
187
|
+
}
|
|
188
|
+
type ListPeersResponse = Peer[];
|
|
189
|
+
interface SendMessageRequest {
|
|
190
|
+
/** Target peer id or unique peer name. */
|
|
191
|
+
to: string;
|
|
192
|
+
/** Defaults to `"cli"`. */
|
|
193
|
+
from_id?: string;
|
|
194
|
+
from_name?: string;
|
|
195
|
+
text?: string;
|
|
196
|
+
/** Defaults to `"pull"`. Unknown values are rejected. */
|
|
197
|
+
mode?: MessageMode;
|
|
198
|
+
}
|
|
199
|
+
type SendMessageResponse = {
|
|
200
|
+
ok: true;
|
|
201
|
+
to_id: string;
|
|
202
|
+
to_name: string;
|
|
203
|
+
message_id: number;
|
|
204
|
+
} | {
|
|
205
|
+
ok: false;
|
|
206
|
+
error: string;
|
|
207
|
+
};
|
|
208
|
+
interface PollMessagesRequest {
|
|
209
|
+
id: string;
|
|
210
|
+
}
|
|
211
|
+
/** Undelivered messages for `id`; each is marked `delivered: 1` on return. */
|
|
212
|
+
interface PollMessagesResponse {
|
|
213
|
+
messages: Message[];
|
|
214
|
+
}
|
|
215
|
+
interface AcknowledgeMessagesRequest {
|
|
216
|
+
id: string;
|
|
217
|
+
message_ids?: number[];
|
|
218
|
+
}
|
|
219
|
+
type AcknowledgeMessagesResponse = {
|
|
220
|
+
ok: true;
|
|
221
|
+
};
|
|
222
|
+
interface GetBufferedMessagesRequest {
|
|
223
|
+
id: string;
|
|
224
|
+
}
|
|
225
|
+
/** Messages for `id` with `delivered: 1` and `acknowledged: 0`. */
|
|
226
|
+
interface GetBufferedMessagesResponse {
|
|
227
|
+
messages: Message[];
|
|
228
|
+
}
|
|
229
|
+
interface UnregisterRequest {
|
|
230
|
+
id: string;
|
|
231
|
+
}
|
|
232
|
+
type UnregisterResponse = {
|
|
233
|
+
ok: true;
|
|
234
|
+
};
|
|
235
|
+
/** One undelivered message handed over by a fallback broker. The recipient is addressed by name: peer ids are broker-local. */
|
|
236
|
+
interface RelayedMessage {
|
|
237
|
+
from_id: string;
|
|
238
|
+
from_name: string;
|
|
239
|
+
/** Recipient peer name on the sending broker. Items without a name cannot be relayed. */
|
|
240
|
+
to_name: string;
|
|
241
|
+
/** Recipient hostname on the sending broker; narrows ambiguous names. */
|
|
242
|
+
to_hostname?: string;
|
|
243
|
+
text: string;
|
|
244
|
+
mode?: MessageMode;
|
|
245
|
+
sent_at: string;
|
|
246
|
+
}
|
|
247
|
+
interface RelayMessagesRequest {
|
|
248
|
+
messages: RelayedMessage[];
|
|
249
|
+
}
|
|
250
|
+
/** `delivered` = queued for a registered peer, `queued` = held until a peer of that name registers, `dropped` = no usable name. */
|
|
251
|
+
interface RelayMessagesResponse {
|
|
252
|
+
ok: true;
|
|
253
|
+
delivered: number;
|
|
254
|
+
queued: number;
|
|
255
|
+
dropped: number;
|
|
256
|
+
}
|
|
257
|
+
/** Extra POST route served by the TypeScript broker. Not in `BROKER_POST_ROUTES` (that table mirrors the legacy broker). */
|
|
258
|
+
declare const RELAY_ROUTE: "/relay-messages";
|
|
259
|
+
/** The only GET route. */
|
|
260
|
+
declare const HEALTH_ROUTE: "/health";
|
|
261
|
+
/** POST routes, in the order of `ROUTES` in `legacy/src/broker.js`. */
|
|
262
|
+
declare const BROKER_POST_ROUTES: readonly ["/register", "/heartbeat", "/set-summary", "/set-name", "/list-peers", "/send-message", "/poll-messages", "/acknowledge-messages", "/get-buffered-messages", "/unregister"];
|
|
263
|
+
type BrokerPostRoute = (typeof BROKER_POST_ROUTES)[number];
|
|
264
|
+
/** Request / response pair per POST route. */
|
|
265
|
+
interface BrokerRouteMap {
|
|
266
|
+
"/register": {
|
|
267
|
+
request: RegisterRequest;
|
|
268
|
+
response: RegisterResponse;
|
|
269
|
+
};
|
|
270
|
+
"/heartbeat": {
|
|
271
|
+
request: HeartbeatRequest;
|
|
272
|
+
response: HeartbeatResponse;
|
|
273
|
+
};
|
|
274
|
+
"/set-summary": {
|
|
275
|
+
request: SetSummaryRequest;
|
|
276
|
+
response: SetSummaryResponse;
|
|
277
|
+
};
|
|
278
|
+
"/set-name": {
|
|
279
|
+
request: SetNameRequest;
|
|
280
|
+
response: SetNameResponse;
|
|
281
|
+
};
|
|
282
|
+
"/list-peers": {
|
|
283
|
+
request: ListPeersRequest;
|
|
284
|
+
response: ListPeersResponse;
|
|
285
|
+
};
|
|
286
|
+
"/send-message": {
|
|
287
|
+
request: SendMessageRequest;
|
|
288
|
+
response: SendMessageResponse;
|
|
289
|
+
};
|
|
290
|
+
"/poll-messages": {
|
|
291
|
+
request: PollMessagesRequest;
|
|
292
|
+
response: PollMessagesResponse;
|
|
293
|
+
};
|
|
294
|
+
"/acknowledge-messages": {
|
|
295
|
+
request: AcknowledgeMessagesRequest;
|
|
296
|
+
response: AcknowledgeMessagesResponse;
|
|
297
|
+
};
|
|
298
|
+
"/get-buffered-messages": {
|
|
299
|
+
request: GetBufferedMessagesRequest;
|
|
300
|
+
response: GetBufferedMessagesResponse;
|
|
301
|
+
};
|
|
302
|
+
"/unregister": {
|
|
303
|
+
request: UnregisterRequest;
|
|
304
|
+
response: UnregisterResponse;
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
type BrokerRequest<R extends BrokerPostRoute> = BrokerRouteMap[R]["request"];
|
|
308
|
+
type BrokerResponse<R extends BrokerPostRoute> = BrokerRouteMap[R]["response"];
|
|
309
|
+
/** Broker-to-broker sync route. Old brokers answer 404 and are simply not part of the mesh. */
|
|
310
|
+
declare const MESH_SYNC_ROUTE: "/mesh/sync";
|
|
311
|
+
/**
|
|
312
|
+
* POST /mesh/sync — one round of peer / message replication, sent by each
|
|
313
|
+
* mesh broker to every other one.
|
|
314
|
+
*/
|
|
315
|
+
interface MeshSyncRequest {
|
|
316
|
+
/** Id of the sending broker (= `origin` of the peers below). */
|
|
317
|
+
from: string;
|
|
318
|
+
/** Live peers that registered directly with the sender (replaces what the receiver holds for `from`). */
|
|
319
|
+
peers: Peer[];
|
|
320
|
+
/** Undelivered messages the sender holds for peers that live on the receiver. Ids are the sender's. */
|
|
321
|
+
messages: Message[];
|
|
322
|
+
}
|
|
323
|
+
interface MeshSyncResponse {
|
|
324
|
+
/** Id of the receiving broker. */
|
|
325
|
+
id: string;
|
|
326
|
+
/** Live peers that registered directly with the receiver. */
|
|
327
|
+
peers: Peer[];
|
|
328
|
+
/** Sender-side ids of the messages the receiver took over; the sender drops them. */
|
|
329
|
+
accepted: number[];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* UDP LAN discovery — probe / reply JSON shapes.
|
|
334
|
+
*
|
|
335
|
+
* Transcribed from `legacy/src/resolve.js` (`discoverBrokers`) and `legacy/src/broker.js`
|
|
336
|
+
* (discovery responder). Both packets are single JSON objects; the broker's
|
|
337
|
+
* address is the reply packet's UDP source address.
|
|
338
|
+
*/
|
|
339
|
+
/** JSON key present in both probe and reply. */
|
|
340
|
+
declare const DISCOVERY_KEY: "agent-messenger";
|
|
341
|
+
/** Value of `DISCOVERY_KEY` in a client probe. */
|
|
342
|
+
declare const DISCOVERY_PROBE_VALUE: "probe";
|
|
343
|
+
/** Value of `DISCOVERY_KEY` in a broker reply. */
|
|
344
|
+
declare const DISCOVERY_REPLY_VALUE: "broker";
|
|
345
|
+
/** Client → broadcast: `{"agent-messenger":"probe"}` */
|
|
346
|
+
interface DiscoveryProbe {
|
|
347
|
+
[DISCOVERY_KEY]: typeof DISCOVERY_PROBE_VALUE;
|
|
348
|
+
}
|
|
349
|
+
/** Broker → client: `{"agent-messenger":"broker","id":..,"port":..,"version":..}` */
|
|
350
|
+
interface DiscoveryReply {
|
|
351
|
+
[DISCOVERY_KEY]: typeof DISCOVERY_REPLY_VALUE;
|
|
352
|
+
/** Broker id (`AGENT_MESSENGER_BROKER_ID`, default hostname). */
|
|
353
|
+
id: string;
|
|
354
|
+
/** Broker HTTP port. */
|
|
355
|
+
port: number;
|
|
356
|
+
/** Broker package version. */
|
|
357
|
+
version: string;
|
|
358
|
+
/** Present (`true`) only when the broker is a local fallback; clients prefer other brokers, fallbacks never relay to each other. */
|
|
359
|
+
fallback?: true;
|
|
360
|
+
}
|
|
361
|
+
/** The exact probe object sent by the client. */
|
|
362
|
+
declare const DISCOVERY_PROBE: DiscoveryProbe;
|
|
363
|
+
/** Default probe target when `AGENT_MESSENGER_DISCOVERY_ADDR` is unset (plus per-interface directed broadcast). */
|
|
364
|
+
declare const DISCOVERY_BROADCAST_ADDR = "255.255.255.255";
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Claude Code session inbox socket — line shapes.
|
|
368
|
+
*
|
|
369
|
+
* Transcribed from `legacy/src/server.js` (`postToSession`). The client connects to
|
|
370
|
+
* the unix socket named by `CLAUDE_CODE_MESSAGING_SOCKET`, writes one JSON
|
|
371
|
+
* object per line (`\n` terminated), then half-closes:
|
|
372
|
+
*
|
|
373
|
+
* 1. `{"type":"auth","token":<CLAUDE_CODE_MESSAGING_TOKEN>}` only when the token is set
|
|
374
|
+
* 2. `{"type":"user","message":{"role":"user","content":<text>}}`
|
|
375
|
+
*/
|
|
376
|
+
/** Environment variable holding the session socket path. */
|
|
377
|
+
declare const SESSION_SOCKET_ENV = "CLAUDE_CODE_MESSAGING_SOCKET";
|
|
378
|
+
/** Environment variable holding the session socket auth token. */
|
|
379
|
+
declare const SESSION_TOKEN_ENV = "CLAUDE_CODE_MESSAGING_TOKEN";
|
|
380
|
+
interface SessionAuthLine {
|
|
381
|
+
type: "auth";
|
|
382
|
+
token: string;
|
|
383
|
+
}
|
|
384
|
+
interface SessionUserLine {
|
|
385
|
+
type: "user";
|
|
386
|
+
message: {
|
|
387
|
+
role: "user";
|
|
388
|
+
content: string;
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
type SessionSocketLine = SessionAuthLine | SessionUserLine;
|
|
392
|
+
|
|
393
|
+
export { BROKER_POST_ROUTES, DEFAULT_BIND, DEFAULT_DISCOVERY_PORT, DEFAULT_DISCOVERY_TIMEOUT_MS, DEFAULT_HTTP_PORT, DISCOVERY_BROADCAST_ADDR, DISCOVERY_KEY, DISCOVERY_PROBE, DISCOVERY_PROBE_VALUE, DISCOVERY_REPLY_VALUE, ENV, HEALTH_ROUTE, HEARTBEAT_INTERVAL_MS, HEARTBEAT_TIMEOUT_MS, LOCAL_HOSTS, MESH_SYNC_ROUTE, POLL_INTERVAL_MS, RELAY_ROUTE, SESSION_SOCKET_ENV, SESSION_TOKEN_ENV };
|
|
394
|
+
export type { AcknowledgeMessagesRequest, AcknowledgeMessagesResponse, BrokerErrorResponse, BrokerPostRoute, BrokerRequest, BrokerResponse, BrokerRouteMap, DiscoveryProbe, DiscoveryReply, EnvName, GetBufferedMessagesRequest, GetBufferedMessagesResponse, HealthResponse, HeartbeatRequest, HeartbeatResponse, ListPeersRequest, ListPeersResponse, MeshSyncRequest, MeshSyncResponse, Message, MessageMode, OkResult, Peer, PeerScope, PollMessagesRequest, PollMessagesResponse, RegisterRequest, RegisterResponse, RelayMessagesRequest, RelayMessagesResponse, RelayedMessage, SendMessageRequest, SendMessageResponse, SessionAuthLine, SessionSocketLine, SessionUserLine, SetNameRequest, SetNameResponse, SetSummaryRequest, SetSummaryResponse, UnregisterRequest, UnregisterResponse };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protocol constants shared by broker, client and CLI.
|
|
3
|
+
*
|
|
4
|
+
* Values are transcribed from the current JavaScript implementation
|
|
5
|
+
* (`legacy/src/config.js`, `legacy/src/broker.js`, `legacy/src/server.js`). Do not change them here
|
|
6
|
+
* without changing the implementation on both ends.
|
|
7
|
+
*/
|
|
8
|
+
/** Broker HTTP listen port (`AGENT_MESSENGER_PORT` default). */
|
|
9
|
+
declare const DEFAULT_HTTP_PORT = 7901;
|
|
10
|
+
/** Broker HTTP bind address (`AGENT_MESSENGER_BIND` default). */
|
|
11
|
+
declare const DEFAULT_BIND = "0.0.0.0";
|
|
12
|
+
/** UDP discovery port (`AGENT_MESSENGER_DISCOVERY_PORT` default). */
|
|
13
|
+
declare const DEFAULT_DISCOVERY_PORT = 7902;
|
|
14
|
+
/** How long a client waits for discovery answers (`AGENT_MESSENGER_DISCOVERY_TIMEOUT_MS` default). */
|
|
15
|
+
declare const DEFAULT_DISCOVERY_TIMEOUT_MS = 1000;
|
|
16
|
+
/** Client heartbeat interval (`AGENT_MESSENGER_HEARTBEAT_MS` default). */
|
|
17
|
+
declare const HEARTBEAT_INTERVAL_MS = 15000;
|
|
18
|
+
/** Broker drops a peer whose `last_seen` is older than this. Not configurable. */
|
|
19
|
+
declare const HEARTBEAT_TIMEOUT_MS = 60000;
|
|
20
|
+
/** Client `/poll-messages` interval. Not configurable. */
|
|
21
|
+
declare const POLL_INTERVAL_MS = 1000;
|
|
22
|
+
/** Loopback hosts treated as "local broker" by the client resolver. */
|
|
23
|
+
declare const LOCAL_HOSTS: readonly ["127.0.0.1", "localhost", "::1"];
|
|
24
|
+
/**
|
|
25
|
+
* Environment variable names. Source of truth: `legacy/src/config.js`
|
|
26
|
+
* (`DELIVERY` lives in `legacy/src/server.js`).
|
|
27
|
+
*/
|
|
28
|
+
declare const ENV: {
|
|
29
|
+
/** Explicit broker host. `"auto"` or unset = resolve by config / discovery / local. */
|
|
30
|
+
readonly HOST: "AGENT_MESSENGER_HOST";
|
|
31
|
+
/** Broker HTTP port (client target / broker listen). */
|
|
32
|
+
readonly PORT: "AGENT_MESSENGER_PORT";
|
|
33
|
+
/** Broker bind address. */
|
|
34
|
+
readonly BIND: "AGENT_MESSENGER_BIND";
|
|
35
|
+
/** Bearer token; when set, every POST must carry `Authorization: Bearer <token>`. */
|
|
36
|
+
readonly TOKEN: "AGENT_MESSENGER_TOKEN";
|
|
37
|
+
/** Peer name for this session. */
|
|
38
|
+
readonly NAME: "AGENT_MESSENGER_NAME";
|
|
39
|
+
/** Broker id shown in `/health` and discovery replies (default hostname). */
|
|
40
|
+
readonly BROKER_ID: "AGENT_MESSENGER_BROKER_ID";
|
|
41
|
+
/** Broker state file (default `~/.agent-messenger/broker.json`). */
|
|
42
|
+
readonly STATE: "AGENT_MESSENGER_STATE";
|
|
43
|
+
/** Client config file (default `~/.agent-messenger/config.json`). */
|
|
44
|
+
readonly CONFIG: "AGENT_MESSENGER_CONFIG";
|
|
45
|
+
/** `"off"` disables UDP discovery (broker responder and client probe). */
|
|
46
|
+
readonly DISCOVERY: "AGENT_MESSENGER_DISCOVERY";
|
|
47
|
+
/** UDP discovery port. */
|
|
48
|
+
readonly DISCOVERY_PORT: "AGENT_MESSENGER_DISCOVERY_PORT";
|
|
49
|
+
/** Discovery probe target address. */
|
|
50
|
+
readonly DISCOVERY_ADDR: "AGENT_MESSENGER_DISCOVERY_ADDR";
|
|
51
|
+
/** Discovery answer wait time in ms. */
|
|
52
|
+
readonly DISCOVERY_TIMEOUT_MS: "AGENT_MESSENGER_DISCOVERY_TIMEOUT_MS";
|
|
53
|
+
/** Client heartbeat interval in ms. */
|
|
54
|
+
readonly HEARTBEAT_MS: "AGENT_MESSENGER_HEARTBEAT_MS";
|
|
55
|
+
/** Inbound delivery mode: `"auto"` (default) / `"socket"` / `"channel"`. */
|
|
56
|
+
readonly DELIVERY: "AGENT_MESSENGER_DELIVERY";
|
|
57
|
+
};
|
|
58
|
+
type EnvName = (typeof ENV)[keyof typeof ENV];
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Broker HTTP API — request / response JSON shapes.
|
|
62
|
+
*
|
|
63
|
+
* Transcribed from `legacy/src/broker.js` (handlers) and `legacy/src/server.js` /
|
|
64
|
+
* `legacy/bin/agent-messenger.js` (callers). Field optionality follows the broker:
|
|
65
|
+
* a field the broker defaults with `??` is optional here.
|
|
66
|
+
*
|
|
67
|
+
* Transport rules (from `legacy/src/broker.js` `startBroker`):
|
|
68
|
+
* - `GET /health` is the only GET route.
|
|
69
|
+
* - Every other route is `POST` with a JSON body; empty body is treated as `{}`.
|
|
70
|
+
* - When the broker has a token, each POST must send `Authorization: Bearer <token>`,
|
|
71
|
+
* otherwise `401 {"error":"unauthorized"}`.
|
|
72
|
+
* - Unknown POST path: `404 {"error":"not found"}`. Handler exception: `500 {"error":<message>}`.
|
|
73
|
+
* - Any other method: `200 text/plain` `agent-messenger broker <version>\n`.
|
|
74
|
+
*/
|
|
75
|
+
/** A registered session as stored and returned by the broker. */
|
|
76
|
+
interface Peer {
|
|
77
|
+
/** 8-char broker-generated id (`[a-z0-9]`). */
|
|
78
|
+
id: string;
|
|
79
|
+
/** Stable name set via `/register` or `/set-name`; `""` when unset. */
|
|
80
|
+
name: string;
|
|
81
|
+
/** Client process id. */
|
|
82
|
+
pid: number;
|
|
83
|
+
hostname: string;
|
|
84
|
+
cwd: string;
|
|
85
|
+
git_root: string | null;
|
|
86
|
+
tty: string | null;
|
|
87
|
+
summary: string;
|
|
88
|
+
/** ISO 8601 timestamp. */
|
|
89
|
+
registered_at: string;
|
|
90
|
+
/** ISO 8601 timestamp; refreshed by `/heartbeat`. */
|
|
91
|
+
last_seen: string;
|
|
92
|
+
/**
|
|
93
|
+
* Mesh only: id of the permanent broker this peer is registered with, set on
|
|
94
|
+
* copies a mesh broker replicated from another one. Absent for peers that
|
|
95
|
+
* registered directly. Never returned by `/list-peers`.
|
|
96
|
+
*/
|
|
97
|
+
origin?: string;
|
|
98
|
+
}
|
|
99
|
+
type MessageMode = "pull" | "push" | "ping";
|
|
100
|
+
/** A message as stored and returned by the broker. */
|
|
101
|
+
interface Message {
|
|
102
|
+
/** Broker-assigned incrementing integer. */
|
|
103
|
+
id: number;
|
|
104
|
+
/** Sender peer id, or `"cli"` when sent from the CLI. */
|
|
105
|
+
from_id: string;
|
|
106
|
+
from_name: string;
|
|
107
|
+
to_id: string;
|
|
108
|
+
text: string;
|
|
109
|
+
/**
|
|
110
|
+
* How the receiver shows it: `"pull"` = unread notice only, text via
|
|
111
|
+
* `check_messages`; `"push"` = text shown directly; `"ping"` = liveness
|
|
112
|
+
* notice; `text` is its optional subject (e.g. a task id), `""` when none. Missing (older broker / sender) means `"pull"`.
|
|
113
|
+
*/
|
|
114
|
+
mode?: MessageMode;
|
|
115
|
+
/** ISO 8601 timestamp. */
|
|
116
|
+
sent_at: string;
|
|
117
|
+
/** 0 until returned by `/poll-messages`, then 1. */
|
|
118
|
+
delivered: 0 | 1;
|
|
119
|
+
/** Always 0 in the current implementation (acknowledged messages are deleted). */
|
|
120
|
+
acknowledged: 0 | 1;
|
|
121
|
+
/** Mesh only: `<sender broker id>:<id there>` of a message forwarded by another broker (duplicate guard). */
|
|
122
|
+
mesh_key?: string;
|
|
123
|
+
}
|
|
124
|
+
/** `/list-peers` scope filter. Unknown values behave as `"all"`. */
|
|
125
|
+
type PeerScope = "all" | "host" | "directory" | "repo";
|
|
126
|
+
/** Generic `{ok:true}` / `{ok:false,error}` result. */
|
|
127
|
+
type OkResult = {
|
|
128
|
+
ok: true;
|
|
129
|
+
} | {
|
|
130
|
+
ok: false;
|
|
131
|
+
error: string;
|
|
132
|
+
};
|
|
133
|
+
/** Body of a non-2xx broker response. */
|
|
134
|
+
interface BrokerErrorResponse {
|
|
135
|
+
error: string;
|
|
136
|
+
}
|
|
137
|
+
interface HealthResponse {
|
|
138
|
+
status: "ok";
|
|
139
|
+
/** Broker package version. */
|
|
140
|
+
version: string;
|
|
141
|
+
/** Broker id (`AGENT_MESSENGER_BROKER_ID`, default hostname). */
|
|
142
|
+
id: string;
|
|
143
|
+
/** Broker HTTP port. */
|
|
144
|
+
port: number;
|
|
145
|
+
/** Count of peers seen within `HEARTBEAT_TIMEOUT_MS`. */
|
|
146
|
+
peers: number;
|
|
147
|
+
/** Present (`true`) only when the broker was started as a local fallback (`AGENT_MESSENGER_FALLBACK=1`). */
|
|
148
|
+
fallback?: true;
|
|
149
|
+
}
|
|
150
|
+
interface RegisterRequest {
|
|
151
|
+
pid: number;
|
|
152
|
+
hostname?: string;
|
|
153
|
+
cwd?: string;
|
|
154
|
+
git_root?: string | null;
|
|
155
|
+
tty?: string | null;
|
|
156
|
+
summary?: string;
|
|
157
|
+
name?: string;
|
|
158
|
+
}
|
|
159
|
+
interface RegisterResponse {
|
|
160
|
+
id: string;
|
|
161
|
+
}
|
|
162
|
+
interface HeartbeatRequest {
|
|
163
|
+
id: string;
|
|
164
|
+
}
|
|
165
|
+
/** `ok:false` with `"unknown peer (re-register)"` when the broker does not know the id. */
|
|
166
|
+
type HeartbeatResponse = OkResult;
|
|
167
|
+
interface SetSummaryRequest {
|
|
168
|
+
id: string;
|
|
169
|
+
summary?: string;
|
|
170
|
+
}
|
|
171
|
+
type SetSummaryResponse = OkResult;
|
|
172
|
+
interface SetNameRequest {
|
|
173
|
+
id: string;
|
|
174
|
+
name?: string;
|
|
175
|
+
}
|
|
176
|
+
type SetNameResponse = OkResult;
|
|
177
|
+
interface ListPeersRequest {
|
|
178
|
+
scope?: PeerScope;
|
|
179
|
+
/** Used by scope `"host"`. */
|
|
180
|
+
hostname?: string;
|
|
181
|
+
/** Used by scope `"directory"`, and by `"repo"` when `git_root` is empty. */
|
|
182
|
+
cwd?: string;
|
|
183
|
+
/** Used by scope `"repo"`. */
|
|
184
|
+
git_root?: string | null;
|
|
185
|
+
/** Peer id to omit from the result (the caller itself). */
|
|
186
|
+
exclude_id?: string;
|
|
187
|
+
}
|
|
188
|
+
type ListPeersResponse = Peer[];
|
|
189
|
+
interface SendMessageRequest {
|
|
190
|
+
/** Target peer id or unique peer name. */
|
|
191
|
+
to: string;
|
|
192
|
+
/** Defaults to `"cli"`. */
|
|
193
|
+
from_id?: string;
|
|
194
|
+
from_name?: string;
|
|
195
|
+
text?: string;
|
|
196
|
+
/** Defaults to `"pull"`. Unknown values are rejected. */
|
|
197
|
+
mode?: MessageMode;
|
|
198
|
+
}
|
|
199
|
+
type SendMessageResponse = {
|
|
200
|
+
ok: true;
|
|
201
|
+
to_id: string;
|
|
202
|
+
to_name: string;
|
|
203
|
+
message_id: number;
|
|
204
|
+
} | {
|
|
205
|
+
ok: false;
|
|
206
|
+
error: string;
|
|
207
|
+
};
|
|
208
|
+
interface PollMessagesRequest {
|
|
209
|
+
id: string;
|
|
210
|
+
}
|
|
211
|
+
/** Undelivered messages for `id`; each is marked `delivered: 1` on return. */
|
|
212
|
+
interface PollMessagesResponse {
|
|
213
|
+
messages: Message[];
|
|
214
|
+
}
|
|
215
|
+
interface AcknowledgeMessagesRequest {
|
|
216
|
+
id: string;
|
|
217
|
+
message_ids?: number[];
|
|
218
|
+
}
|
|
219
|
+
type AcknowledgeMessagesResponse = {
|
|
220
|
+
ok: true;
|
|
221
|
+
};
|
|
222
|
+
interface GetBufferedMessagesRequest {
|
|
223
|
+
id: string;
|
|
224
|
+
}
|
|
225
|
+
/** Messages for `id` with `delivered: 1` and `acknowledged: 0`. */
|
|
226
|
+
interface GetBufferedMessagesResponse {
|
|
227
|
+
messages: Message[];
|
|
228
|
+
}
|
|
229
|
+
interface UnregisterRequest {
|
|
230
|
+
id: string;
|
|
231
|
+
}
|
|
232
|
+
type UnregisterResponse = {
|
|
233
|
+
ok: true;
|
|
234
|
+
};
|
|
235
|
+
/** One undelivered message handed over by a fallback broker. The recipient is addressed by name: peer ids are broker-local. */
|
|
236
|
+
interface RelayedMessage {
|
|
237
|
+
from_id: string;
|
|
238
|
+
from_name: string;
|
|
239
|
+
/** Recipient peer name on the sending broker. Items without a name cannot be relayed. */
|
|
240
|
+
to_name: string;
|
|
241
|
+
/** Recipient hostname on the sending broker; narrows ambiguous names. */
|
|
242
|
+
to_hostname?: string;
|
|
243
|
+
text: string;
|
|
244
|
+
mode?: MessageMode;
|
|
245
|
+
sent_at: string;
|
|
246
|
+
}
|
|
247
|
+
interface RelayMessagesRequest {
|
|
248
|
+
messages: RelayedMessage[];
|
|
249
|
+
}
|
|
250
|
+
/** `delivered` = queued for a registered peer, `queued` = held until a peer of that name registers, `dropped` = no usable name. */
|
|
251
|
+
interface RelayMessagesResponse {
|
|
252
|
+
ok: true;
|
|
253
|
+
delivered: number;
|
|
254
|
+
queued: number;
|
|
255
|
+
dropped: number;
|
|
256
|
+
}
|
|
257
|
+
/** Extra POST route served by the TypeScript broker. Not in `BROKER_POST_ROUTES` (that table mirrors the legacy broker). */
|
|
258
|
+
declare const RELAY_ROUTE: "/relay-messages";
|
|
259
|
+
/** The only GET route. */
|
|
260
|
+
declare const HEALTH_ROUTE: "/health";
|
|
261
|
+
/** POST routes, in the order of `ROUTES` in `legacy/src/broker.js`. */
|
|
262
|
+
declare const BROKER_POST_ROUTES: readonly ["/register", "/heartbeat", "/set-summary", "/set-name", "/list-peers", "/send-message", "/poll-messages", "/acknowledge-messages", "/get-buffered-messages", "/unregister"];
|
|
263
|
+
type BrokerPostRoute = (typeof BROKER_POST_ROUTES)[number];
|
|
264
|
+
/** Request / response pair per POST route. */
|
|
265
|
+
interface BrokerRouteMap {
|
|
266
|
+
"/register": {
|
|
267
|
+
request: RegisterRequest;
|
|
268
|
+
response: RegisterResponse;
|
|
269
|
+
};
|
|
270
|
+
"/heartbeat": {
|
|
271
|
+
request: HeartbeatRequest;
|
|
272
|
+
response: HeartbeatResponse;
|
|
273
|
+
};
|
|
274
|
+
"/set-summary": {
|
|
275
|
+
request: SetSummaryRequest;
|
|
276
|
+
response: SetSummaryResponse;
|
|
277
|
+
};
|
|
278
|
+
"/set-name": {
|
|
279
|
+
request: SetNameRequest;
|
|
280
|
+
response: SetNameResponse;
|
|
281
|
+
};
|
|
282
|
+
"/list-peers": {
|
|
283
|
+
request: ListPeersRequest;
|
|
284
|
+
response: ListPeersResponse;
|
|
285
|
+
};
|
|
286
|
+
"/send-message": {
|
|
287
|
+
request: SendMessageRequest;
|
|
288
|
+
response: SendMessageResponse;
|
|
289
|
+
};
|
|
290
|
+
"/poll-messages": {
|
|
291
|
+
request: PollMessagesRequest;
|
|
292
|
+
response: PollMessagesResponse;
|
|
293
|
+
};
|
|
294
|
+
"/acknowledge-messages": {
|
|
295
|
+
request: AcknowledgeMessagesRequest;
|
|
296
|
+
response: AcknowledgeMessagesResponse;
|
|
297
|
+
};
|
|
298
|
+
"/get-buffered-messages": {
|
|
299
|
+
request: GetBufferedMessagesRequest;
|
|
300
|
+
response: GetBufferedMessagesResponse;
|
|
301
|
+
};
|
|
302
|
+
"/unregister": {
|
|
303
|
+
request: UnregisterRequest;
|
|
304
|
+
response: UnregisterResponse;
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
type BrokerRequest<R extends BrokerPostRoute> = BrokerRouteMap[R]["request"];
|
|
308
|
+
type BrokerResponse<R extends BrokerPostRoute> = BrokerRouteMap[R]["response"];
|
|
309
|
+
/** Broker-to-broker sync route. Old brokers answer 404 and are simply not part of the mesh. */
|
|
310
|
+
declare const MESH_SYNC_ROUTE: "/mesh/sync";
|
|
311
|
+
/**
|
|
312
|
+
* POST /mesh/sync — one round of peer / message replication, sent by each
|
|
313
|
+
* mesh broker to every other one.
|
|
314
|
+
*/
|
|
315
|
+
interface MeshSyncRequest {
|
|
316
|
+
/** Id of the sending broker (= `origin` of the peers below). */
|
|
317
|
+
from: string;
|
|
318
|
+
/** Live peers that registered directly with the sender (replaces what the receiver holds for `from`). */
|
|
319
|
+
peers: Peer[];
|
|
320
|
+
/** Undelivered messages the sender holds for peers that live on the receiver. Ids are the sender's. */
|
|
321
|
+
messages: Message[];
|
|
322
|
+
}
|
|
323
|
+
interface MeshSyncResponse {
|
|
324
|
+
/** Id of the receiving broker. */
|
|
325
|
+
id: string;
|
|
326
|
+
/** Live peers that registered directly with the receiver. */
|
|
327
|
+
peers: Peer[];
|
|
328
|
+
/** Sender-side ids of the messages the receiver took over; the sender drops them. */
|
|
329
|
+
accepted: number[];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* UDP LAN discovery — probe / reply JSON shapes.
|
|
334
|
+
*
|
|
335
|
+
* Transcribed from `legacy/src/resolve.js` (`discoverBrokers`) and `legacy/src/broker.js`
|
|
336
|
+
* (discovery responder). Both packets are single JSON objects; the broker's
|
|
337
|
+
* address is the reply packet's UDP source address.
|
|
338
|
+
*/
|
|
339
|
+
/** JSON key present in both probe and reply. */
|
|
340
|
+
declare const DISCOVERY_KEY: "agent-messenger";
|
|
341
|
+
/** Value of `DISCOVERY_KEY` in a client probe. */
|
|
342
|
+
declare const DISCOVERY_PROBE_VALUE: "probe";
|
|
343
|
+
/** Value of `DISCOVERY_KEY` in a broker reply. */
|
|
344
|
+
declare const DISCOVERY_REPLY_VALUE: "broker";
|
|
345
|
+
/** Client → broadcast: `{"agent-messenger":"probe"}` */
|
|
346
|
+
interface DiscoveryProbe {
|
|
347
|
+
[DISCOVERY_KEY]: typeof DISCOVERY_PROBE_VALUE;
|
|
348
|
+
}
|
|
349
|
+
/** Broker → client: `{"agent-messenger":"broker","id":..,"port":..,"version":..}` */
|
|
350
|
+
interface DiscoveryReply {
|
|
351
|
+
[DISCOVERY_KEY]: typeof DISCOVERY_REPLY_VALUE;
|
|
352
|
+
/** Broker id (`AGENT_MESSENGER_BROKER_ID`, default hostname). */
|
|
353
|
+
id: string;
|
|
354
|
+
/** Broker HTTP port. */
|
|
355
|
+
port: number;
|
|
356
|
+
/** Broker package version. */
|
|
357
|
+
version: string;
|
|
358
|
+
/** Present (`true`) only when the broker is a local fallback; clients prefer other brokers, fallbacks never relay to each other. */
|
|
359
|
+
fallback?: true;
|
|
360
|
+
}
|
|
361
|
+
/** The exact probe object sent by the client. */
|
|
362
|
+
declare const DISCOVERY_PROBE: DiscoveryProbe;
|
|
363
|
+
/** Default probe target when `AGENT_MESSENGER_DISCOVERY_ADDR` is unset (plus per-interface directed broadcast). */
|
|
364
|
+
declare const DISCOVERY_BROADCAST_ADDR = "255.255.255.255";
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Claude Code session inbox socket — line shapes.
|
|
368
|
+
*
|
|
369
|
+
* Transcribed from `legacy/src/server.js` (`postToSession`). The client connects to
|
|
370
|
+
* the unix socket named by `CLAUDE_CODE_MESSAGING_SOCKET`, writes one JSON
|
|
371
|
+
* object per line (`\n` terminated), then half-closes:
|
|
372
|
+
*
|
|
373
|
+
* 1. `{"type":"auth","token":<CLAUDE_CODE_MESSAGING_TOKEN>}` only when the token is set
|
|
374
|
+
* 2. `{"type":"user","message":{"role":"user","content":<text>}}`
|
|
375
|
+
*/
|
|
376
|
+
/** Environment variable holding the session socket path. */
|
|
377
|
+
declare const SESSION_SOCKET_ENV = "CLAUDE_CODE_MESSAGING_SOCKET";
|
|
378
|
+
/** Environment variable holding the session socket auth token. */
|
|
379
|
+
declare const SESSION_TOKEN_ENV = "CLAUDE_CODE_MESSAGING_TOKEN";
|
|
380
|
+
interface SessionAuthLine {
|
|
381
|
+
type: "auth";
|
|
382
|
+
token: string;
|
|
383
|
+
}
|
|
384
|
+
interface SessionUserLine {
|
|
385
|
+
type: "user";
|
|
386
|
+
message: {
|
|
387
|
+
role: "user";
|
|
388
|
+
content: string;
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
type SessionSocketLine = SessionAuthLine | SessionUserLine;
|
|
392
|
+
|
|
393
|
+
export { BROKER_POST_ROUTES, DEFAULT_BIND, DEFAULT_DISCOVERY_PORT, DEFAULT_DISCOVERY_TIMEOUT_MS, DEFAULT_HTTP_PORT, DISCOVERY_BROADCAST_ADDR, DISCOVERY_KEY, DISCOVERY_PROBE, DISCOVERY_PROBE_VALUE, DISCOVERY_REPLY_VALUE, ENV, HEALTH_ROUTE, HEARTBEAT_INTERVAL_MS, HEARTBEAT_TIMEOUT_MS, LOCAL_HOSTS, MESH_SYNC_ROUTE, POLL_INTERVAL_MS, RELAY_ROUTE, SESSION_SOCKET_ENV, SESSION_TOKEN_ENV };
|
|
394
|
+
export type { AcknowledgeMessagesRequest, AcknowledgeMessagesResponse, BrokerErrorResponse, BrokerPostRoute, BrokerRequest, BrokerResponse, BrokerRouteMap, DiscoveryProbe, DiscoveryReply, EnvName, GetBufferedMessagesRequest, GetBufferedMessagesResponse, HealthResponse, HeartbeatRequest, HeartbeatResponse, ListPeersRequest, ListPeersResponse, MeshSyncRequest, MeshSyncResponse, Message, MessageMode, OkResult, Peer, PeerScope, PollMessagesRequest, PollMessagesResponse, RegisterRequest, RegisterResponse, RelayMessagesRequest, RelayMessagesResponse, RelayedMessage, SendMessageRequest, SendMessageResponse, SessionAuthLine, SessionSocketLine, SessionUserLine, SetNameRequest, SetNameResponse, SetSummaryRequest, SetSummaryResponse, UnregisterRequest, UnregisterResponse };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const DEFAULT_HTTP_PORT = 7901;
|
|
2
|
+
const DEFAULT_BIND = "0.0.0.0";
|
|
3
|
+
const DEFAULT_DISCOVERY_PORT = 7902;
|
|
4
|
+
const DEFAULT_DISCOVERY_TIMEOUT_MS = 1e3;
|
|
5
|
+
const HEARTBEAT_INTERVAL_MS = 15e3;
|
|
6
|
+
const HEARTBEAT_TIMEOUT_MS = 6e4;
|
|
7
|
+
const POLL_INTERVAL_MS = 1e3;
|
|
8
|
+
const LOCAL_HOSTS = ["127.0.0.1", "localhost", "::1"];
|
|
9
|
+
const ENV = {
|
|
10
|
+
/** Explicit broker host. `"auto"` or unset = resolve by config / discovery / local. */
|
|
11
|
+
HOST: "AGENT_MESSENGER_HOST",
|
|
12
|
+
/** Broker HTTP port (client target / broker listen). */
|
|
13
|
+
PORT: "AGENT_MESSENGER_PORT",
|
|
14
|
+
/** Broker bind address. */
|
|
15
|
+
BIND: "AGENT_MESSENGER_BIND",
|
|
16
|
+
/** Bearer token; when set, every POST must carry `Authorization: Bearer <token>`. */
|
|
17
|
+
TOKEN: "AGENT_MESSENGER_TOKEN",
|
|
18
|
+
/** Peer name for this session. */
|
|
19
|
+
NAME: "AGENT_MESSENGER_NAME",
|
|
20
|
+
/** Broker id shown in `/health` and discovery replies (default hostname). */
|
|
21
|
+
BROKER_ID: "AGENT_MESSENGER_BROKER_ID",
|
|
22
|
+
/** Broker state file (default `~/.agent-messenger/broker.json`). */
|
|
23
|
+
STATE: "AGENT_MESSENGER_STATE",
|
|
24
|
+
/** Client config file (default `~/.agent-messenger/config.json`). */
|
|
25
|
+
CONFIG: "AGENT_MESSENGER_CONFIG",
|
|
26
|
+
/** `"off"` disables UDP discovery (broker responder and client probe). */
|
|
27
|
+
DISCOVERY: "AGENT_MESSENGER_DISCOVERY",
|
|
28
|
+
/** UDP discovery port. */
|
|
29
|
+
DISCOVERY_PORT: "AGENT_MESSENGER_DISCOVERY_PORT",
|
|
30
|
+
/** Discovery probe target address. */
|
|
31
|
+
DISCOVERY_ADDR: "AGENT_MESSENGER_DISCOVERY_ADDR",
|
|
32
|
+
/** Discovery answer wait time in ms. */
|
|
33
|
+
DISCOVERY_TIMEOUT_MS: "AGENT_MESSENGER_DISCOVERY_TIMEOUT_MS",
|
|
34
|
+
/** Client heartbeat interval in ms. */
|
|
35
|
+
HEARTBEAT_MS: "AGENT_MESSENGER_HEARTBEAT_MS",
|
|
36
|
+
/** Inbound delivery mode: `"auto"` (default) / `"socket"` / `"channel"`. */
|
|
37
|
+
DELIVERY: "AGENT_MESSENGER_DELIVERY"
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const RELAY_ROUTE = "/relay-messages";
|
|
41
|
+
const HEALTH_ROUTE = "/health";
|
|
42
|
+
const BROKER_POST_ROUTES = [
|
|
43
|
+
"/register",
|
|
44
|
+
"/heartbeat",
|
|
45
|
+
"/set-summary",
|
|
46
|
+
"/set-name",
|
|
47
|
+
"/list-peers",
|
|
48
|
+
"/send-message",
|
|
49
|
+
"/poll-messages",
|
|
50
|
+
"/acknowledge-messages",
|
|
51
|
+
"/get-buffered-messages",
|
|
52
|
+
"/unregister"
|
|
53
|
+
];
|
|
54
|
+
const MESH_SYNC_ROUTE = "/mesh/sync";
|
|
55
|
+
|
|
56
|
+
const DISCOVERY_KEY = "agent-messenger";
|
|
57
|
+
const DISCOVERY_PROBE_VALUE = "probe";
|
|
58
|
+
const DISCOVERY_REPLY_VALUE = "broker";
|
|
59
|
+
const DISCOVERY_PROBE = { [DISCOVERY_KEY]: DISCOVERY_PROBE_VALUE };
|
|
60
|
+
const DISCOVERY_BROADCAST_ADDR = "255.255.255.255";
|
|
61
|
+
|
|
62
|
+
const SESSION_SOCKET_ENV = "CLAUDE_CODE_MESSAGING_SOCKET";
|
|
63
|
+
const SESSION_TOKEN_ENV = "CLAUDE_CODE_MESSAGING_TOKEN";
|
|
64
|
+
|
|
65
|
+
export { BROKER_POST_ROUTES, DEFAULT_BIND, DEFAULT_DISCOVERY_PORT, DEFAULT_DISCOVERY_TIMEOUT_MS, DEFAULT_HTTP_PORT, DISCOVERY_BROADCAST_ADDR, DISCOVERY_KEY, DISCOVERY_PROBE, DISCOVERY_PROBE_VALUE, DISCOVERY_REPLY_VALUE, ENV, HEALTH_ROUTE, HEARTBEAT_INTERVAL_MS, HEARTBEAT_TIMEOUT_MS, LOCAL_HOSTS, MESH_SYNC_ROUTE, POLL_INTERVAL_MS, RELAY_ROUTE, SESSION_SOCKET_ENV, SESSION_TOKEN_ENV };
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unagents/messenger-protocol",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "agent-messenger protocol: types and constants shared by broker, client and CLI. No runtime logic.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/unagents/agent-messenger.git",
|
|
10
|
+
"directory": "packages/protocol"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"import": "./dist/index.mjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=20"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "unbuild",
|
|
26
|
+
"typecheck": "tsgo -p tsconfig.json",
|
|
27
|
+
"test": "vitest run"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/README.md
DELETED