@unagents/messenger-broker 0.0.0-stage → 0.10.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 +111 -0
- package/dist/index.d.ts +111 -0
- package/dist/index.mjs +12 -0
- package/dist/main.d.mts +2 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.mjs +23 -0
- package/dist/shared/messenger-broker.B8W77sqB.mjs +1177 -0
- package/package.json +35 -4
- package/README.md +0 -3
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Peer, Message, RelayedMessage, StoredProtocol } from '@unagents/messenger-protocol';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Broker configuration. Every field can be given to `startBroker(options)`;
|
|
5
|
+
* anything left out falls back to the environment (`AGENT_MESSENGER_*`,
|
|
6
|
+
* same names and defaults as the JavaScript `legacy/src/config.js`).
|
|
7
|
+
*/
|
|
8
|
+
interface BrokerOptions {
|
|
9
|
+
/** HTTP listen port (`AGENT_MESSENGER_PORT`, default 7901). `0` = random free port. */
|
|
10
|
+
port?: number;
|
|
11
|
+
/** HTTP bind address (`AGENT_MESSENGER_BIND`, default `0.0.0.0`). */
|
|
12
|
+
bind?: string;
|
|
13
|
+
/** Bearer token; `""` disables auth (`AGENT_MESSENGER_TOKEN`, default `""`). */
|
|
14
|
+
token?: string;
|
|
15
|
+
/** Broker id shown in `/health` and discovery replies (`AGENT_MESSENGER_BROKER_ID`, default hostname). */
|
|
16
|
+
id?: string;
|
|
17
|
+
/** State file (`AGENT_MESSENGER_STATE`, default `~/.agent-messenger/broker.json`). */
|
|
18
|
+
stateFile?: string;
|
|
19
|
+
/** UDP discovery responder on/off (`AGENT_MESSENGER_DISCOVERY`, `"off"` disables; default on). */
|
|
20
|
+
discovery?: boolean;
|
|
21
|
+
/** UDP discovery port (`AGENT_MESSENGER_DISCOVERY_PORT`, default 7902). */
|
|
22
|
+
discoveryPort?: number;
|
|
23
|
+
/** UDP bind address when pinned (`AGENT_MESSENGER_DISCOVERY_ADDR`, default `""` = all interfaces). */
|
|
24
|
+
discoveryAddr?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Run as a local fallback broker (`AGENT_MESSENGER_FALLBACK=1`, set by the client when it spawns one).
|
|
27
|
+
* A fallback probes for other brokers every `fallbackIntervalMs`; when it finds one it relays its
|
|
28
|
+
* undelivered messages there and exits. A permanent broker (default) never does this.
|
|
29
|
+
*/
|
|
30
|
+
fallback?: boolean;
|
|
31
|
+
/** Fallback discovery interval in ms (`AGENT_MESSENGER_FALLBACK_INTERVAL_MS`, default 10000). */
|
|
32
|
+
fallbackIntervalMs?: number;
|
|
33
|
+
/** Called after a fallback broker has relayed its messages and closed itself (`main.ts` exits the process). */
|
|
34
|
+
onSupersede?: (url: string) => void;
|
|
35
|
+
/**
|
|
36
|
+
* Join the permanent-broker mesh (`AGENT_MESSENGER_MESH`, `"on"` enables; default off).
|
|
37
|
+
* Fallback brokers must leave this off: only brokers started with it take part.
|
|
38
|
+
*/
|
|
39
|
+
mesh?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Mesh members to sync with. Empty (default) = the `brokers` list of the client
|
|
42
|
+
* config file (`AGENT_MESSENGER_CONFIG`, default `~/.agent-messenger/config.json`), re-read every round.
|
|
43
|
+
*/
|
|
44
|
+
meshBrokers?: string[];
|
|
45
|
+
/** Mesh sync interval in ms (`AGENT_MESSENGER_MESH_INTERVAL_MS`, default 5000). */
|
|
46
|
+
meshIntervalMs?: number;
|
|
47
|
+
/** Replicated peers disappear this long after their origin stopped syncing (`AGENT_MESSENGER_MESH_PEER_TTL_MS`, default 300000). */
|
|
48
|
+
meshPeerTtlMs?: number;
|
|
49
|
+
/** Client config file read for the mesh member list (`AGENT_MESSENGER_CONFIG`). */
|
|
50
|
+
configFile?: string;
|
|
51
|
+
}
|
|
52
|
+
type BrokerConfig = Required<Omit<BrokerOptions, "onSupersede">> & Pick<BrokerOptions, "onSupersede">;
|
|
53
|
+
/** Marks a broker as a local fallback. Broker-private (not in the protocol ENV table, which mirrors legacy). */
|
|
54
|
+
declare const FALLBACK_ENV = "AGENT_MESSENGER_FALLBACK";
|
|
55
|
+
declare const FALLBACK_INTERVAL_ENV = "AGENT_MESSENGER_FALLBACK_INTERVAL_MS";
|
|
56
|
+
/** Directory of the default state file (`~/.agent-messenger`). Not exported by protocol. */
|
|
57
|
+
declare const STATE_DIR: string;
|
|
58
|
+
/** Default state file path (`~/.agent-messenger/broker.json`). Not exported by protocol. */
|
|
59
|
+
declare const DEFAULT_STATE_FILE: string;
|
|
60
|
+
/** Mesh environment variables. Broker-private (not in the protocol's `ENV`, which mirrors the legacy names). */
|
|
61
|
+
declare const MESH_ENV: {
|
|
62
|
+
readonly MESH: "AGENT_MESSENGER_MESH";
|
|
63
|
+
readonly INTERVAL_MS: "AGENT_MESSENGER_MESH_INTERVAL_MS";
|
|
64
|
+
readonly PEER_TTL_MS: "AGENT_MESSENGER_MESH_PEER_TTL_MS";
|
|
65
|
+
};
|
|
66
|
+
/** Read the broker-side environment exactly like `legacy/src/config.js`. */
|
|
67
|
+
declare function configFromEnv(env?: NodeJS.ProcessEnv): BrokerConfig;
|
|
68
|
+
/** `options` over environment over built-in defaults. */
|
|
69
|
+
declare function resolveConfig(options?: BrokerOptions): BrokerConfig;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* On-disk shape of `~/.agent-messenger/broker.json` (written by core 0.4.0
|
|
73
|
+
* `legacy/src/broker.js`). Not part of the protocol package — broker-private.
|
|
74
|
+
*/
|
|
75
|
+
interface BrokerStateFile {
|
|
76
|
+
peers: Peer[];
|
|
77
|
+
messages: Message[];
|
|
78
|
+
nextMsgId: number;
|
|
79
|
+
/** Relayed messages waiting for their recipient to register (absent in files written before the fallback relay). */
|
|
80
|
+
pending?: PendingRelay[];
|
|
81
|
+
/** Protocol definitions (absent in files written before the protocol registry, or when none is held). */
|
|
82
|
+
protocols?: StoredProtocol[];
|
|
83
|
+
}
|
|
84
|
+
/** A message relayed from a fallback broker whose recipient has not registered here yet. */
|
|
85
|
+
interface PendingRelay extends RelayedMessage {
|
|
86
|
+
/** ISO 8601; pending items older than the TTL are dropped. */
|
|
87
|
+
queued_at: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Package version, reported by `/health` and discovery replies (inlined at build time). */
|
|
91
|
+
declare const VERSION: string;
|
|
92
|
+
interface BrokerHandle {
|
|
93
|
+
/** Stop HTTP + UDP, flush a pending state save. Idempotent. */
|
|
94
|
+
close(): Promise<void>;
|
|
95
|
+
/** Port the HTTP server listens on (actual port when `port: 0` was given). */
|
|
96
|
+
port: number;
|
|
97
|
+
/** Broker id (`AGENT_MESSENGER_BROKER_ID`, default hostname). */
|
|
98
|
+
id: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Start the broker daemon: load state, sweep stale peers, listen on HTTP
|
|
102
|
+
* (h3 + listhen) and answer UDP discovery probes.
|
|
103
|
+
*
|
|
104
|
+
* Unlike the JavaScript `startBroker()`, this does not install SIGINT /
|
|
105
|
+
* SIGTERM handlers — callers own the lifecycle through `close()`
|
|
106
|
+
* (`src/main.ts` does that for the standalone entry).
|
|
107
|
+
*/
|
|
108
|
+
declare function startBroker(options?: BrokerOptions): Promise<BrokerHandle>;
|
|
109
|
+
|
|
110
|
+
export { DEFAULT_STATE_FILE, FALLBACK_ENV, FALLBACK_INTERVAL_ENV, MESH_ENV, STATE_DIR, VERSION, configFromEnv, resolveConfig, startBroker };
|
|
111
|
+
export type { BrokerConfig, BrokerHandle, BrokerOptions, BrokerStateFile };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Peer, Message, RelayedMessage, StoredProtocol } from '@unagents/messenger-protocol';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Broker configuration. Every field can be given to `startBroker(options)`;
|
|
5
|
+
* anything left out falls back to the environment (`AGENT_MESSENGER_*`,
|
|
6
|
+
* same names and defaults as the JavaScript `legacy/src/config.js`).
|
|
7
|
+
*/
|
|
8
|
+
interface BrokerOptions {
|
|
9
|
+
/** HTTP listen port (`AGENT_MESSENGER_PORT`, default 7901). `0` = random free port. */
|
|
10
|
+
port?: number;
|
|
11
|
+
/** HTTP bind address (`AGENT_MESSENGER_BIND`, default `0.0.0.0`). */
|
|
12
|
+
bind?: string;
|
|
13
|
+
/** Bearer token; `""` disables auth (`AGENT_MESSENGER_TOKEN`, default `""`). */
|
|
14
|
+
token?: string;
|
|
15
|
+
/** Broker id shown in `/health` and discovery replies (`AGENT_MESSENGER_BROKER_ID`, default hostname). */
|
|
16
|
+
id?: string;
|
|
17
|
+
/** State file (`AGENT_MESSENGER_STATE`, default `~/.agent-messenger/broker.json`). */
|
|
18
|
+
stateFile?: string;
|
|
19
|
+
/** UDP discovery responder on/off (`AGENT_MESSENGER_DISCOVERY`, `"off"` disables; default on). */
|
|
20
|
+
discovery?: boolean;
|
|
21
|
+
/** UDP discovery port (`AGENT_MESSENGER_DISCOVERY_PORT`, default 7902). */
|
|
22
|
+
discoveryPort?: number;
|
|
23
|
+
/** UDP bind address when pinned (`AGENT_MESSENGER_DISCOVERY_ADDR`, default `""` = all interfaces). */
|
|
24
|
+
discoveryAddr?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Run as a local fallback broker (`AGENT_MESSENGER_FALLBACK=1`, set by the client when it spawns one).
|
|
27
|
+
* A fallback probes for other brokers every `fallbackIntervalMs`; when it finds one it relays its
|
|
28
|
+
* undelivered messages there and exits. A permanent broker (default) never does this.
|
|
29
|
+
*/
|
|
30
|
+
fallback?: boolean;
|
|
31
|
+
/** Fallback discovery interval in ms (`AGENT_MESSENGER_FALLBACK_INTERVAL_MS`, default 10000). */
|
|
32
|
+
fallbackIntervalMs?: number;
|
|
33
|
+
/** Called after a fallback broker has relayed its messages and closed itself (`main.ts` exits the process). */
|
|
34
|
+
onSupersede?: (url: string) => void;
|
|
35
|
+
/**
|
|
36
|
+
* Join the permanent-broker mesh (`AGENT_MESSENGER_MESH`, `"on"` enables; default off).
|
|
37
|
+
* Fallback brokers must leave this off: only brokers started with it take part.
|
|
38
|
+
*/
|
|
39
|
+
mesh?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Mesh members to sync with. Empty (default) = the `brokers` list of the client
|
|
42
|
+
* config file (`AGENT_MESSENGER_CONFIG`, default `~/.agent-messenger/config.json`), re-read every round.
|
|
43
|
+
*/
|
|
44
|
+
meshBrokers?: string[];
|
|
45
|
+
/** Mesh sync interval in ms (`AGENT_MESSENGER_MESH_INTERVAL_MS`, default 5000). */
|
|
46
|
+
meshIntervalMs?: number;
|
|
47
|
+
/** Replicated peers disappear this long after their origin stopped syncing (`AGENT_MESSENGER_MESH_PEER_TTL_MS`, default 300000). */
|
|
48
|
+
meshPeerTtlMs?: number;
|
|
49
|
+
/** Client config file read for the mesh member list (`AGENT_MESSENGER_CONFIG`). */
|
|
50
|
+
configFile?: string;
|
|
51
|
+
}
|
|
52
|
+
type BrokerConfig = Required<Omit<BrokerOptions, "onSupersede">> & Pick<BrokerOptions, "onSupersede">;
|
|
53
|
+
/** Marks a broker as a local fallback. Broker-private (not in the protocol ENV table, which mirrors legacy). */
|
|
54
|
+
declare const FALLBACK_ENV = "AGENT_MESSENGER_FALLBACK";
|
|
55
|
+
declare const FALLBACK_INTERVAL_ENV = "AGENT_MESSENGER_FALLBACK_INTERVAL_MS";
|
|
56
|
+
/** Directory of the default state file (`~/.agent-messenger`). Not exported by protocol. */
|
|
57
|
+
declare const STATE_DIR: string;
|
|
58
|
+
/** Default state file path (`~/.agent-messenger/broker.json`). Not exported by protocol. */
|
|
59
|
+
declare const DEFAULT_STATE_FILE: string;
|
|
60
|
+
/** Mesh environment variables. Broker-private (not in the protocol's `ENV`, which mirrors the legacy names). */
|
|
61
|
+
declare const MESH_ENV: {
|
|
62
|
+
readonly MESH: "AGENT_MESSENGER_MESH";
|
|
63
|
+
readonly INTERVAL_MS: "AGENT_MESSENGER_MESH_INTERVAL_MS";
|
|
64
|
+
readonly PEER_TTL_MS: "AGENT_MESSENGER_MESH_PEER_TTL_MS";
|
|
65
|
+
};
|
|
66
|
+
/** Read the broker-side environment exactly like `legacy/src/config.js`. */
|
|
67
|
+
declare function configFromEnv(env?: NodeJS.ProcessEnv): BrokerConfig;
|
|
68
|
+
/** `options` over environment over built-in defaults. */
|
|
69
|
+
declare function resolveConfig(options?: BrokerOptions): BrokerConfig;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* On-disk shape of `~/.agent-messenger/broker.json` (written by core 0.4.0
|
|
73
|
+
* `legacy/src/broker.js`). Not part of the protocol package — broker-private.
|
|
74
|
+
*/
|
|
75
|
+
interface BrokerStateFile {
|
|
76
|
+
peers: Peer[];
|
|
77
|
+
messages: Message[];
|
|
78
|
+
nextMsgId: number;
|
|
79
|
+
/** Relayed messages waiting for their recipient to register (absent in files written before the fallback relay). */
|
|
80
|
+
pending?: PendingRelay[];
|
|
81
|
+
/** Protocol definitions (absent in files written before the protocol registry, or when none is held). */
|
|
82
|
+
protocols?: StoredProtocol[];
|
|
83
|
+
}
|
|
84
|
+
/** A message relayed from a fallback broker whose recipient has not registered here yet. */
|
|
85
|
+
interface PendingRelay extends RelayedMessage {
|
|
86
|
+
/** ISO 8601; pending items older than the TTL are dropped. */
|
|
87
|
+
queued_at: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Package version, reported by `/health` and discovery replies (inlined at build time). */
|
|
91
|
+
declare const VERSION: string;
|
|
92
|
+
interface BrokerHandle {
|
|
93
|
+
/** Stop HTTP + UDP, flush a pending state save. Idempotent. */
|
|
94
|
+
close(): Promise<void>;
|
|
95
|
+
/** Port the HTTP server listens on (actual port when `port: 0` was given). */
|
|
96
|
+
port: number;
|
|
97
|
+
/** Broker id (`AGENT_MESSENGER_BROKER_ID`, default hostname). */
|
|
98
|
+
id: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Start the broker daemon: load state, sweep stale peers, listen on HTTP
|
|
102
|
+
* (h3 + listhen) and answer UDP discovery probes.
|
|
103
|
+
*
|
|
104
|
+
* Unlike the JavaScript `startBroker()`, this does not install SIGINT /
|
|
105
|
+
* SIGTERM handlers — callers own the lifecycle through `close()`
|
|
106
|
+
* (`src/main.ts` does that for the standalone entry).
|
|
107
|
+
*/
|
|
108
|
+
declare function startBroker(options?: BrokerOptions): Promise<BrokerHandle>;
|
|
109
|
+
|
|
110
|
+
export { DEFAULT_STATE_FILE, FALLBACK_ENV, FALLBACK_INTERVAL_ENV, MESH_ENV, STATE_DIR, VERSION, configFromEnv, resolveConfig, startBroker };
|
|
111
|
+
export type { BrokerConfig, BrokerHandle, BrokerOptions, BrokerStateFile };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import 'listhen';
|
|
2
|
+
import 'h3';
|
|
3
|
+
import '@unagents/messenger-protocol';
|
|
4
|
+
export { D as DEFAULT_STATE_FILE, F as FALLBACK_ENV, a as FALLBACK_INTERVAL_ENV, M as MESH_ENV, S as STATE_DIR, V as VERSION, c as configFromEnv, r as resolveConfig, s as startBroker } from './shared/messenger-broker.B8W77sqB.mjs';
|
|
5
|
+
import 'node:os';
|
|
6
|
+
import 'node:path';
|
|
7
|
+
import 'defu';
|
|
8
|
+
import 'node:fs';
|
|
9
|
+
import 'consola';
|
|
10
|
+
import 'node:crypto';
|
|
11
|
+
import 'node:http';
|
|
12
|
+
import 'node:dgram';
|
package/dist/main.d.mts
ADDED
package/dist/main.d.ts
ADDED
package/dist/main.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { s as startBroker, l as log } from './shared/messenger-broker.B8W77sqB.mjs';
|
|
2
|
+
import 'listhen';
|
|
3
|
+
import 'h3';
|
|
4
|
+
import '@unagents/messenger-protocol';
|
|
5
|
+
import 'node:os';
|
|
6
|
+
import 'node:path';
|
|
7
|
+
import 'defu';
|
|
8
|
+
import 'node:fs';
|
|
9
|
+
import 'consola';
|
|
10
|
+
import 'node:crypto';
|
|
11
|
+
import 'node:http';
|
|
12
|
+
import 'node:dgram';
|
|
13
|
+
|
|
14
|
+
const broker = await startBroker({
|
|
15
|
+
// fallback only: messages are relayed and the broker is closed, nothing left to do
|
|
16
|
+
onSupersede: () => process.exit(0)
|
|
17
|
+
});
|
|
18
|
+
const shutdown = () => {
|
|
19
|
+
log.info("shutting down");
|
|
20
|
+
broker.close().finally(() => process.exit(0));
|
|
21
|
+
};
|
|
22
|
+
process.once("SIGINT", shutdown);
|
|
23
|
+
process.once("SIGTERM", shutdown);
|