@sharpps/agent 0.0.0-stage → 1.0.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/host.d.ts ADDED
@@ -0,0 +1,179 @@
1
+ import type { MachineInfo, ServerMessage } from "./protocol.js";
2
+ import type { AgentIdentity, Authenticator } from "./auth.js";
3
+ /**
4
+ * The server side of @sharpps/agent. An app adds behaviour only through plugins; the host routes calls, enforces the rules below and
5
+ * isolates plugin failures. It keeps no records of its own: an app stores its agents and maps them through the hooks.
6
+ *
7
+ * Rules the host enforces:
8
+ * - a call goes only to a capability some plugin declared, and only to an agent that advertised it in its hello;
9
+ * - two plugins cannot declare the same name or the same capability;
10
+ * - a plugin that throws is logged and skipped; only the calls that plugin handles fail;
11
+ * - by default one call per capability per agent runs at a time (CapabilitySpec.concurrency raises that).
12
+ */
13
+ export interface CapabilitySpec {
14
+ description?: string;
15
+ /** Documentation of the input shape. Not enforced by the host; the plugin validates its own input. */
16
+ input?: unknown;
17
+ /** Calls of this capability that one agent may run at once. Default 1. */
18
+ concurrency?: number;
19
+ }
20
+ export interface AgentInfo {
21
+ id: string;
22
+ name: string;
23
+ /** The person the agent belongs to, as the app's identity check returned it. */
24
+ userId: string;
25
+ machine: MachineInfo;
26
+ capabilities: string[];
27
+ }
28
+ export interface HelloInfo {
29
+ protocol: number;
30
+ agentVersion: string;
31
+ machine: MachineInfo;
32
+ capabilities: string[];
33
+ }
34
+ export interface Plugin {
35
+ /** Unique within the host. Used in logs and to name the plugin that handled a call. */
36
+ name: string;
37
+ capabilities?: Record<string, CapabilitySpec>;
38
+ /** Decide whether to accept a machine. Throw to refuse it; the message is shown to the agent. */
39
+ onAgentHello?(agent: AgentInfo, hello: HelloInfo): void | Promise<void>;
40
+ onAgentOnline?(agent: AgentInfo): void | Promise<void>;
41
+ onAgentOffline?(agent: AgentInfo, reason: string): void | Promise<void>;
42
+ /** Progress reported for a call this plugin owns. */
43
+ onEvent?(callId: string, data: unknown): void | Promise<void>;
44
+ /** Binary data for a call this plugin owns (for example a browser connection). */
45
+ onStream?(callId: string, streamId: number, payload: Uint8Array, kind: "text" | "binary"): void | Promise<void>;
46
+ /** Called once, when the host is built, so the plugin can send to agents (see `AgentHost.sendStream`). */
47
+ init?(host: AgentHost): void;
48
+ /** Runs when the listener starts, before it accepts machines. A throw stops the listener. */
49
+ start?(): void | Promise<void>;
50
+ /** Runs when the listener stops. */
51
+ stop?(): void | Promise<void>;
52
+ /**
53
+ * Maps a signed-in person and a machine to an agent (its id and name). Apps keep their own records here. The first plugin that
54
+ * defines it decides; without one, the listener uses `<user>:<machine id>`.
55
+ */
56
+ agentFor?(identity: AgentIdentity, machine: MachineInfo, hello: HelloInfo): Promise<{
57
+ id: string;
58
+ name: string;
59
+ }> | {
60
+ id: string;
61
+ name: string;
62
+ };
63
+ /**
64
+ * Answers a request on the listener's control port, under /plugins/<name>/… (JSON in, JSON out). Only processes that hold the control
65
+ * token can reach it, so it is for the apps that run on the same server, such as the Replayr runner.
66
+ */
67
+ handleHttp?(req: {
68
+ method: string;
69
+ path: string;
70
+ body: unknown;
71
+ }): Promise<{
72
+ status: number;
73
+ body: unknown;
74
+ }> | {
75
+ status: number;
76
+ body: unknown;
77
+ };
78
+ /** The machine ended a stream that belongs to a call this plugin owns. */
79
+ onStreamEnd?(callId: string, streamId: number): void | Promise<void>;
80
+ }
81
+ /** How the host reaches an agent. Returns false when the agent is not connected. */
82
+ export interface Transport {
83
+ send(agentId: string, msg: ServerMessage): boolean;
84
+ /** Sends a binary frame (a stream chunk). False when the agent is not connected. */
85
+ sendBinary(agentId: string, frame: Uint8Array): boolean;
86
+ }
87
+ export interface HostOptions {
88
+ plugins: Plugin[];
89
+ transport: Transport;
90
+ /** Who may connect. Without one, the host admits nobody (fails closed). Clerk: `clerkAuthenticator` in this package. */
91
+ authenticator?: Authenticator;
92
+ /** How long a call may take before it is cancelled and failed. Default 2 minutes. */
93
+ callTimeoutMs?: number;
94
+ log?: (msg: string, extra?: Record<string, unknown>) => void;
95
+ }
96
+ export type HostErrorCode = "unknown-capability" | "not-advertised" | "offline" | "busy" | "timeout" | "agent-offline" | "cancelled" | "plugin-error" | "not-running";
97
+ export declare class HostError extends Error {
98
+ readonly code: HostErrorCode;
99
+ constructor(code: HostErrorCode, message: string);
100
+ }
101
+ export declare class AgentHost {
102
+ private readonly plugins;
103
+ private readonly owners;
104
+ private readonly specs;
105
+ private readonly agents;
106
+ private readonly pending;
107
+ /** Running calls per "agentId|capability". */
108
+ private readonly running;
109
+ /** Binary streams announced by an agent for one of its calls: "agentId|streamId" -> call. */
110
+ private readonly streams;
111
+ /** Streams this host has announced to an agent: "agentId|streamId" -> call. */
112
+ private readonly outbound;
113
+ private readonly transport;
114
+ private readonly authenticator?;
115
+ private readonly timeoutMs;
116
+ private readonly log;
117
+ constructor(o: HostOptions);
118
+ /** The capabilities the host can call, with their plugin. For documentation and admin screens. */
119
+ capabilities(): {
120
+ name: string;
121
+ plugin: string;
122
+ description?: string;
123
+ }[];
124
+ /** Plugin names in the order they were given. */
125
+ pluginNames(): string[];
126
+ isOnline(agentId: string): boolean;
127
+ /**
128
+ * Checks the access token an agent connected with, before anything else. Fails closed: without an authenticator nobody is admitted,
129
+ * and a token that the authenticator rejects gets the same answer as a missing one.
130
+ */
131
+ admit(accessToken: string | undefined): Promise<{
132
+ ok: true;
133
+ identity: AgentIdentity;
134
+ } | {
135
+ ok: false;
136
+ message: string;
137
+ }>;
138
+ /**
139
+ * Called when an agent sends its hello. Every plugin may refuse. Returns the refusal message, or null when accepted.
140
+ * Capabilities the agent advertises that no plugin declares are ignored (and logged).
141
+ */
142
+ accept(agent: AgentInfo, hello: HelloInfo): Promise<string | null>;
143
+ /** The agent is connected and accepted: remember it and tell the plugins. */
144
+ online(agent: AgentInfo): Promise<void>;
145
+ /** The agent disconnected: fail its pending calls and tell the plugins. */
146
+ offline(agentId: string, reason: string): Promise<void>;
147
+ /** Ask an agent to run a capability and wait for its answer. */
148
+ call(agentId: string, capability: string, input: unknown): Promise<unknown>;
149
+ /** Like `call`, but also returns the call id at once, so the caller can send streams for the call while it runs. */
150
+ startCall(agentId: string, capability: string, input: unknown): {
151
+ callId: string;
152
+ result: Promise<unknown>;
153
+ };
154
+ /**
155
+ * Server → machine: sends bytes on a stream of a running call. The first chunk announces the stream to the machine. Returns false
156
+ * when the connection cannot take the bytes now.
157
+ */
158
+ sendStream(agentId: string, callId: string, streamId: number, payload: Uint8Array, kind?: "text" | "binary"): boolean;
159
+ /** Server → machine: ends a stream sent for a call. */
160
+ endStream(agentId: string, callId: string, streamId: number): void;
161
+ /** The machine ended a stream it sent. The plugin that owns the call is told. */
162
+ handleStreamEnd(agentId: string, streamId: number): Promise<void>;
163
+ /** Cancel a call from the server side. The agent is told, and the call fails with a "cancelled" error. */
164
+ cancel(callId: string): boolean;
165
+ /** The agent finished one of its calls. Answers from any other agent are ignored. */
166
+ handleResult(agentId: string, callId: string, output: unknown): void;
167
+ /** The agent could not finish one of its calls. */
168
+ handleError(agentId: string, callId: string, code: string, message: string): void;
169
+ /** Progress for one of the agent's calls, passed to the plugin that owns it. */
170
+ handleEvent(agentId: string, callId: string, data: unknown): Promise<void>;
171
+ /** The agent announced a binary stream for one of its running calls. Returns false if that call is not running. */
172
+ openStream(agentId: string, callId: string, streamId: number): boolean;
173
+ /** Binary data on an announced stream, passed to the plugin that owns the call. Unknown streams are dropped. */
174
+ handleStream(agentId: string, streamId: number, payload: Uint8Array, kind?: "text" | "binary"): Promise<void>;
175
+ /** Removes a call from the pending set and frees its concurrency slot. Its timer is cleared. */
176
+ private settle;
177
+ /** Runs a hook on every plugin. A failure is logged and never stops the others. */
178
+ private each;
179
+ }
package/dist/host.js ADDED
@@ -0,0 +1,283 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { encodeFrame, PROTOCOL_VERSION } from "./protocol.js";
3
+ export class HostError extends Error {
4
+ code;
5
+ constructor(code, message) {
6
+ super(message);
7
+ this.code = code;
8
+ }
9
+ }
10
+ const PLUGIN_NAME = /^[a-z][a-z0-9-]{1,39}$/;
11
+ const CAPABILITY = /^[a-z][a-z0-9-]*(\.[a-z][a-z0-9-]*)+$/;
12
+ export class AgentHost {
13
+ plugins;
14
+ owners = new Map();
15
+ specs = new Map();
16
+ agents = new Map();
17
+ pending = new Map();
18
+ /** Running calls per "agentId|capability". */
19
+ running = new Map();
20
+ /** Binary streams announced by an agent for one of its calls: "agentId|streamId" -> call. */
21
+ streams = new Map();
22
+ /** Streams this host has announced to an agent: "agentId|streamId" -> call. */
23
+ outbound = new Map();
24
+ transport;
25
+ authenticator;
26
+ timeoutMs;
27
+ log;
28
+ constructor(o) {
29
+ this.transport = o.transport;
30
+ this.authenticator = o.authenticator;
31
+ this.timeoutMs = o.callTimeoutMs ?? 120_000;
32
+ this.log = o.log ?? (() => { });
33
+ const names = new Set();
34
+ for (const p of o.plugins) {
35
+ if (!PLUGIN_NAME.test(p.name))
36
+ throw new Error(`plugin name "${p.name}" must be lowercase letters, digits and dashes`);
37
+ if (names.has(p.name))
38
+ throw new Error(`two plugins are named "${p.name}"`);
39
+ names.add(p.name);
40
+ for (const [cap, spec] of Object.entries(p.capabilities ?? {})) {
41
+ if (!CAPABILITY.test(cap))
42
+ throw new Error(`capability "${cap}" of plugin "${p.name}" must look like "area.action"`);
43
+ const owner = this.owners.get(cap);
44
+ if (owner)
45
+ throw new Error(`capability "${cap}" is declared by both "${owner.name}" and "${p.name}"`);
46
+ this.owners.set(cap, p);
47
+ this.specs.set(cap, spec);
48
+ }
49
+ }
50
+ this.plugins = o.plugins;
51
+ for (const p of o.plugins)
52
+ p.init?.(this);
53
+ }
54
+ /** The capabilities the host can call, with their plugin. For documentation and admin screens. */
55
+ capabilities() {
56
+ return [...this.owners].map(([name, p]) => ({ name, plugin: p.name, description: this.specs.get(name)?.description }));
57
+ }
58
+ /** Plugin names in the order they were given. */
59
+ pluginNames() {
60
+ return this.plugins.map((p) => p.name);
61
+ }
62
+ isOnline(agentId) {
63
+ return this.agents.has(agentId);
64
+ }
65
+ /**
66
+ * Checks the access token an agent connected with, before anything else. Fails closed: without an authenticator nobody is admitted,
67
+ * and a token that the authenticator rejects gets the same answer as a missing one.
68
+ */
69
+ async admit(accessToken) {
70
+ if (!this.authenticator)
71
+ return { ok: false, message: "this server has no sign-in configured" };
72
+ if (!accessToken)
73
+ return { ok: false, message: "sign in first" };
74
+ const identity = await this.authenticator(accessToken).catch(() => null);
75
+ return identity ? { ok: true, identity } : { ok: false, message: "the sign-in is not valid for this server; sign in again" };
76
+ }
77
+ /**
78
+ * Called when an agent sends its hello. Every plugin may refuse. Returns the refusal message, or null when accepted.
79
+ * Capabilities the agent advertises that no plugin declares are ignored (and logged).
80
+ */
81
+ async accept(agent, hello) {
82
+ if (hello.protocol !== PROTOCOL_VERSION)
83
+ return `protocol ${hello.protocol} is not supported (server speaks ${PROTOCOL_VERSION})`;
84
+ for (const p of this.plugins) {
85
+ try {
86
+ await p.onAgentHello?.(agent, hello);
87
+ }
88
+ catch (e) {
89
+ const message = e instanceof Error ? e.message : String(e);
90
+ this.log("plugin refused agent", { plugin: p.name, agent: agent.id, message });
91
+ return message;
92
+ }
93
+ }
94
+ const unknown = hello.capabilities.filter((c) => !this.owners.has(c));
95
+ if (unknown.length)
96
+ this.log("agent advertised capabilities the server does not declare; ignored", { agent: agent.id, unknown });
97
+ return null;
98
+ }
99
+ /** The agent is connected and accepted: remember it and tell the plugins. */
100
+ async online(agent) {
101
+ const stored = { ...agent, capabilities: agent.capabilities.filter((c) => this.owners.has(c)) };
102
+ this.agents.set(agent.id, stored);
103
+ await this.each((p) => p.onAgentOnline?.(stored), "onAgentOnline");
104
+ }
105
+ /** The agent disconnected: fail its pending calls and tell the plugins. */
106
+ async offline(agentId, reason) {
107
+ const agent = this.agents.get(agentId);
108
+ if (!agent)
109
+ return;
110
+ this.agents.delete(agentId);
111
+ for (const [callId, p] of [...this.pending]) {
112
+ if (p.agentId !== agentId)
113
+ continue;
114
+ this.settle(callId);
115
+ p.reject(new HostError("agent-offline", `the agent went offline: ${reason}`));
116
+ }
117
+ await this.each((p) => p.onAgentOffline?.(agent, reason), "onAgentOffline");
118
+ }
119
+ /** Ask an agent to run a capability and wait for its answer. */
120
+ call(agentId, capability, input) {
121
+ return this.startCall(agentId, capability, input).result;
122
+ }
123
+ /** Like `call`, but also returns the call id at once, so the caller can send streams for the call while it runs. */
124
+ startCall(agentId, capability, input) {
125
+ const callId = randomUUID();
126
+ const fail = (e) => ({ callId, result: Promise.reject(e) });
127
+ const owner = this.owners.get(capability);
128
+ if (!owner)
129
+ return fail(new HostError("unknown-capability", `no plugin declares "${capability}"`));
130
+ const agent = this.agents.get(agentId);
131
+ if (!agent)
132
+ return fail(new HostError("offline", "the agent is not connected"));
133
+ if (!agent.capabilities.includes(capability))
134
+ return fail(new HostError("not-advertised", `the agent does not offer "${capability}"`));
135
+ const key = `${agentId}|${capability}`;
136
+ const limit = this.specs.get(capability)?.concurrency ?? 1;
137
+ if ((this.running.get(key) ?? 0) >= limit)
138
+ return fail(new HostError("busy", `the agent is already running "${capability}"`));
139
+ const result = new Promise((resolve, reject) => {
140
+ const timer = setTimeout(() => {
141
+ if (!this.pending.has(callId))
142
+ return;
143
+ this.settle(callId);
144
+ this.transport.send(agentId, { t: "cancel", callId });
145
+ reject(new HostError("timeout", `"${capability}" took longer than ${Math.round(this.timeoutMs / 1000)} s`));
146
+ }, this.timeoutMs);
147
+ this.pending.set(callId, { agentId, capability, owner, timer, resolve, reject });
148
+ this.running.set(key, (this.running.get(key) ?? 0) + 1);
149
+ if (!this.transport.send(agentId, { t: "call", callId, capability, input })) {
150
+ this.settle(callId);
151
+ reject(new HostError("offline", "the agent is not connected"));
152
+ }
153
+ });
154
+ return { callId, result };
155
+ }
156
+ /**
157
+ * Server → machine: sends bytes on a stream of a running call. The first chunk announces the stream to the machine. Returns false
158
+ * when the connection cannot take the bytes now.
159
+ */
160
+ sendStream(agentId, callId, streamId, payload, kind = "binary") {
161
+ const p = this.pending.get(callId);
162
+ if (!p || p.agentId !== agentId)
163
+ throw new HostError("not-running", "that call is not running on this agent");
164
+ const key = `${agentId}|${streamId}`;
165
+ if (!this.outbound.has(key)) {
166
+ this.outbound.set(key, callId);
167
+ this.transport.send(agentId, { t: "stream", callId, streamId });
168
+ }
169
+ return this.transport.sendBinary(agentId, encodeFrame(kind, streamId, payload));
170
+ }
171
+ /** Server → machine: ends a stream sent for a call. */
172
+ endStream(agentId, callId, streamId) {
173
+ const key = `${agentId}|${streamId}`;
174
+ if (this.outbound.get(key) !== callId)
175
+ return;
176
+ this.outbound.delete(key);
177
+ this.transport.send(agentId, { t: "stream-end", callId, streamId });
178
+ }
179
+ /** The machine ended a stream it sent. The plugin that owns the call is told. */
180
+ async handleStreamEnd(agentId, streamId) {
181
+ const key = `${agentId}|${streamId}`;
182
+ const s = this.streams.get(key);
183
+ if (!s)
184
+ return;
185
+ this.streams.delete(key);
186
+ try {
187
+ await s.owner.onStreamEnd?.(s.callId, streamId);
188
+ }
189
+ catch (e) {
190
+ this.log("plugin stream-end handler failed", { plugin: s.owner.name, callId: s.callId, message: String(e) });
191
+ }
192
+ }
193
+ /** Cancel a call from the server side. The agent is told, and the call fails with a "cancelled" error. */
194
+ cancel(callId) {
195
+ const p = this.pending.get(callId);
196
+ if (!p)
197
+ return false;
198
+ this.settle(callId);
199
+ this.transport.send(p.agentId, { t: "cancel", callId });
200
+ p.reject(new HostError("cancelled", "the call was cancelled"));
201
+ return true;
202
+ }
203
+ /** The agent finished one of its calls. Answers from any other agent are ignored. */
204
+ handleResult(agentId, callId, output) {
205
+ const p = this.pending.get(callId);
206
+ if (!p || p.agentId !== agentId)
207
+ return;
208
+ this.settle(callId);
209
+ p.resolve(output);
210
+ }
211
+ /** The agent could not finish one of its calls. */
212
+ handleError(agentId, callId, code, message) {
213
+ const p = this.pending.get(callId);
214
+ if (!p || p.agentId !== agentId)
215
+ return;
216
+ this.settle(callId);
217
+ p.reject(new HostError("plugin-error", `${code}: ${message}`));
218
+ }
219
+ /** Progress for one of the agent's calls, passed to the plugin that owns it. */
220
+ async handleEvent(agentId, callId, data) {
221
+ const p = this.pending.get(callId);
222
+ if (!p || p.agentId !== agentId)
223
+ return;
224
+ try {
225
+ await p.owner.onEvent?.(callId, data);
226
+ }
227
+ catch (e) {
228
+ this.log("plugin event handler failed", { plugin: p.owner.name, callId, message: String(e) });
229
+ }
230
+ }
231
+ /** The agent announced a binary stream for one of its running calls. Returns false if that call is not running. */
232
+ openStream(agentId, callId, streamId) {
233
+ const p = this.pending.get(callId);
234
+ if (!p || p.agentId !== agentId)
235
+ return false;
236
+ this.streams.set(`${agentId}|${streamId}`, { callId, owner: p.owner });
237
+ return true;
238
+ }
239
+ /** Binary data on an announced stream, passed to the plugin that owns the call. Unknown streams are dropped. */
240
+ async handleStream(agentId, streamId, payload, kind = "binary") {
241
+ const s = this.streams.get(`${agentId}|${streamId}`);
242
+ if (!s)
243
+ return;
244
+ try {
245
+ await s.owner.onStream?.(s.callId, streamId, payload, kind);
246
+ }
247
+ catch (e) {
248
+ this.log("plugin stream handler failed", { plugin: s.owner.name, callId: s.callId, message: String(e) });
249
+ }
250
+ }
251
+ /** Removes a call from the pending set and frees its concurrency slot. Its timer is cleared. */
252
+ settle(callId) {
253
+ const p = this.pending.get(callId);
254
+ if (!p)
255
+ return;
256
+ for (const [k, v] of this.streams)
257
+ if (v.callId === callId)
258
+ this.streams.delete(k);
259
+ for (const [k, v] of this.outbound)
260
+ if (v === callId)
261
+ this.outbound.delete(k);
262
+ clearTimeout(p.timer);
263
+ this.pending.delete(callId);
264
+ const key = `${p.agentId}|${p.capability}`;
265
+ const left = (this.running.get(key) ?? 1) - 1;
266
+ if (left > 0)
267
+ this.running.set(key, left);
268
+ else
269
+ this.running.delete(key);
270
+ }
271
+ /** Runs a hook on every plugin. A failure is logged and never stops the others. */
272
+ async each(fn, hook) {
273
+ for (const p of this.plugins) {
274
+ try {
275
+ await fn(p);
276
+ }
277
+ catch (e) {
278
+ this.log("plugin hook failed", { plugin: p.name, hook, message: String(e) });
279
+ }
280
+ }
281
+ }
282
+ }
283
+ //# sourceMappingURL=host.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.js","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAyF9D,MAAM,OAAO,SAAU,SAAQ,KAAK;IACb;IAArB,YAAqB,IAAmB,EAAE,OAAe;QACvD,KAAK,CAAC,OAAO,CAAC,CAAC;QADI,SAAI,GAAJ,IAAI,CAAe;IAExC,CAAC;CACF;AAWD,MAAM,WAAW,GAAG,wBAAwB,CAAC;AAC7C,MAAM,UAAU,GAAG,uCAAuC,CAAC;AAE3D,MAAM,OAAO,SAAS;IACH,OAAO,CAAW;IAClB,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAC;IACnC,KAAK,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC1C,MAAM,GAAG,IAAI,GAAG,EAAqB,CAAC;IACtC,OAAO,GAAG,IAAI,GAAG,EAAmB,CAAC;IACtD,8CAA8C;IAC7B,OAAO,GAAG,IAAI,GAAG,EAAkB,CAAC;IACrD,6FAA6F;IAC5E,OAAO,GAAG,IAAI,GAAG,EAA6C,CAAC;IAChF,+EAA+E;IAC9D,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IACrC,SAAS,CAAY;IACrB,aAAa,CAAiB;IAC9B,SAAS,CAAS;IAClB,GAAG,CAAyD;IAE7E,YAAY,CAAc;QACxB,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC,SAAS,CAAC;QAC7B,IAAI,CAAC,aAAa,GAAG,CAAC,CAAC,aAAa,CAAC;QACrC,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC,aAAa,IAAI,OAAO,CAAC;QAC5C,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC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C;QAC9E,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QACtB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC5B,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,UAAU,EAAE,CAAC;QAC3C,MAAM,IAAI,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;QAC9C,IAAI,IAAI,GAAG,CAAC;YAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;;YACrC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAED,mFAAmF;IAC3E,KAAK,CAAC,IAAI,CAAC,EAAmD,EAAE,IAAY;QAClF,KAAK,MAAM,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAC7B,IAAI,CAAC;gBACH,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;YACd,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,IAAI,CAAC,GAAG,CAAC,oBAAoB,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YAC/E,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,10 @@
1
+ export * from "./protocol.js";
2
+ export * from "./host.js";
3
+ export * from "./device.js";
4
+ export * from "./credentials.js";
5
+ export * from "./auth.js";
6
+ export * from "./client.js";
7
+ export * from "./setup.js";
8
+ export * from "./server.js";
9
+ export * from "./plugin.js";
10
+ export * from "./registry.js";
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ export * from "./protocol.js";
2
+ export * from "./host.js";
3
+ export * from "./device.js";
4
+ export * from "./credentials.js";
5
+ export * from "./auth.js";
6
+ export * from "./client.js";
7
+ export * from "./setup.js";
8
+ export * from "./server.js";
9
+ export * from "./plugin.js";
10
+ export * from "./registry.js";
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
@@ -0,0 +1,60 @@
1
+ import type { AgentPlugin } from "./client.js";
2
+ import type { ConfigParam } from "./setup.js";
3
+ import type { Plugin } from "./host.js";
4
+ /**
5
+ * A plugin package is one module that can carry both sides: the part that runs on the person's machine (`client`) and the part that
6
+ * runs on the server (`server`). Either side is optional. The CLI reads `name`, `config` and `prerequisites` to install, check and set up
7
+ * the plugin before anything runs.
8
+ */
9
+ export type PrereqResult = {
10
+ ok: true;
11
+ detail?: string;
12
+ } | {
13
+ ok: false;
14
+ message: string;
15
+ fix?: string;
16
+ };
17
+ export interface Prerequisite {
18
+ id: string;
19
+ label: string;
20
+ /** A failing required prerequisite stops `install` and `run`. Default true. */
21
+ required?: boolean;
22
+ check(): Promise<PrereqResult> | PrereqResult;
23
+ }
24
+ /** The client side, minus the parts the CLI fills in (its name and settings). */
25
+ export type ClientSide = Omit<AgentPlugin, "name" | "config">;
26
+ export interface PluginModule {
27
+ /** Unique name. Lowercase letters, digits and dashes. */
28
+ name: string;
29
+ description?: string;
30
+ prerequisites?: Prerequisite[];
31
+ config?: ConfigParam[];
32
+ client?(): ClientSide;
33
+ server?(): Plugin;
34
+ }
35
+ /** Identity helper for plugin authors: checks the shape at build time. */
36
+ export declare function definePlugin(m: PluginModule): PluginModule;
37
+ /** Checks a loaded module and returns it. Throws with the reason when it is not a plugin. */
38
+ export declare function checkModule(m: unknown, from: string): PluginModule;
39
+ export interface PrereqReport {
40
+ id: string;
41
+ label: string;
42
+ required: boolean;
43
+ result: PrereqResult;
44
+ }
45
+ /** Runs every prerequisite of a plugin. Never throws: a check that crashes counts as a failure. */
46
+ export declare function checkPrerequisites(mod: PluginModule): Promise<PrereqReport[]>;
47
+ export declare const hasBlockingFailure: (reports: PrereqReport[]) => boolean;
48
+ /** Ready-made prerequisites for common needs. */
49
+ export declare const prereq: {
50
+ node(major: number): Prerequisite;
51
+ command(cmd: string, fix?: string): Prerequisite;
52
+ file(path: string, label: string, fix?: string): Prerequisite;
53
+ };
54
+ /**
55
+ * Loads a plugin module from a local file, or from a package installed under `pluginsDir`. The package's own `package.json` decides
56
+ * which file to load (its `exports` import entry, else `main`).
57
+ */
58
+ export declare function loadModuleFrom(entryPath: string, from: string): Promise<PluginModule>;
59
+ /** Resolves the entry file of an installed npm package: its ESM `import` entry under `exports`, else `main`, else index.js. */
60
+ export declare function packageEntry(pkgDir: string): string;
package/dist/plugin.js ADDED
@@ -0,0 +1,83 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import { existsSync, readFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { pathToFileURL } from "node:url";
5
+ /** Identity helper for plugin authors: checks the shape at build time. */
6
+ export function definePlugin(m) {
7
+ return m;
8
+ }
9
+ const NAME = /^[a-z][a-z0-9-]{1,39}$/;
10
+ /** Checks a loaded module and returns it. Throws with the reason when it is not a plugin. */
11
+ export function checkModule(m, from) {
12
+ const mod = (m && typeof m === "object" && "default" in m ? m.default : m);
13
+ if (!mod || typeof mod !== "object" || typeof mod.name !== "string")
14
+ throw new Error(`${from} does not export a plugin (expected a name)`);
15
+ if (!NAME.test(mod.name))
16
+ throw new Error(`${from}: plugin name "${mod.name}" must be lowercase letters, digits and dashes`);
17
+ if (!mod.client && !mod.server)
18
+ throw new Error(`${from}: the plugin has neither a client nor a server side`);
19
+ return mod;
20
+ }
21
+ /** Runs every prerequisite of a plugin. Never throws: a check that crashes counts as a failure. */
22
+ export async function checkPrerequisites(mod) {
23
+ const out = [];
24
+ for (const p of mod.prerequisites ?? []) {
25
+ let result;
26
+ try {
27
+ result = await p.check();
28
+ }
29
+ catch (e) {
30
+ result = { ok: false, message: e instanceof Error ? e.message : String(e) };
31
+ }
32
+ out.push({ id: p.id, label: p.label, required: p.required ?? true, result });
33
+ }
34
+ return out;
35
+ }
36
+ export const hasBlockingFailure = (reports) => reports.some((r) => r.required && !r.result.ok);
37
+ /** Ready-made prerequisites for common needs. */
38
+ export const prereq = {
39
+ node(major) {
40
+ return {
41
+ id: `node-${major}`,
42
+ label: `Node.js ${major} or newer`,
43
+ check: () => {
44
+ const v = Number(process.versions.node.split(".")[0]);
45
+ return v >= major ? { ok: true, detail: process.versions.node } : { ok: false, message: `found Node.js ${process.versions.node}`, fix: `install Node.js ${major} or newer` };
46
+ },
47
+ };
48
+ },
49
+ command(cmd, fix) {
50
+ return {
51
+ id: `command-${cmd}`,
52
+ label: `the "${cmd}" command`,
53
+ check: () => {
54
+ const finder = process.platform === "win32" ? "where" : "which";
55
+ const r = spawnSync(finder, [cmd], { encoding: "utf8" });
56
+ return r.status === 0 ? { ok: true } : { ok: false, message: `"${cmd}" was not found`, fix };
57
+ },
58
+ };
59
+ },
60
+ file(path, label, fix) {
61
+ return {
62
+ id: `file-${path}`,
63
+ label,
64
+ check: () => (existsSync(path) ? { ok: true } : { ok: false, message: `${path} is missing`, fix }),
65
+ };
66
+ },
67
+ };
68
+ /**
69
+ * Loads a plugin module from a local file, or from a package installed under `pluginsDir`. The package's own `package.json` decides
70
+ * which file to load (its `exports` import entry, else `main`).
71
+ */
72
+ export async function loadModuleFrom(entryPath, from) {
73
+ const mod = await import(pathToFileURL(entryPath).href);
74
+ return checkModule(mod, from);
75
+ }
76
+ /** Resolves the entry file of an installed npm package: its ESM `import` entry under `exports`, else `main`, else index.js. */
77
+ export function packageEntry(pkgDir) {
78
+ const pkg = JSON.parse(readFileSync(join(pkgDir, "package.json"), "utf8"));
79
+ const exp = pkg.exports?.["."];
80
+ const target = typeof exp === "string" ? exp : exp?.import ?? exp?.default ?? pkg.main ?? "index.js";
81
+ return join(pkgDir, target);
82
+ }
83
+ //# sourceMappingURL=plugin.js.map