@niq.run/worker-sdk 0.8.0 → 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/README.md +78 -2
- package/dist/iface.d.ts +147 -0
- package/dist/iface.d.ts.map +1 -0
- package/dist/iface.js +212 -0
- package/dist/iface.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/reply.d.ts +8 -0
- package/dist/reply.d.ts.map +1 -1
- package/dist/reply.js +19 -0
- package/dist/reply.js.map +1 -1
- package/dist/session.d.ts +72 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +266 -0
- package/dist/session.js.map +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -101,7 +101,7 @@ Properties are required unless wrapped in `s.optional(...)`; arrays, enums and
|
|
|
101
101
|
free-form values are `s.array(...)`, `s.enumOf([...])`, `s.any()`.
|
|
102
102
|
|
|
103
103
|
Both schemas ride `worker.ready`, so peers and the directory worker see them
|
|
104
|
-
(`
|
|
104
|
+
(`describe` → `parameters` / `response`). `response` is what lets an agent
|
|
105
105
|
— in particular a PTC script, written before anything runs — plan how to parse a
|
|
106
106
|
reply; declare it whenever the reply is structured.
|
|
107
107
|
|
|
@@ -285,4 +285,80 @@ npm run build # tsc → dist/
|
|
|
285
285
|
Early, moving fast. Pre-1.0; the wire protocol is stabilizing alongside
|
|
286
286
|
`core/bus` and the Go `httptrans` transport.
|
|
287
287
|
|
|
288
|
-
`node >= 18` (uses global `fetch` and `ReadableStream`).
|
|
288
|
+
`node >= 18` (uses global `fetch` and `ReadableStream`).
|
|
289
|
+
## Interface worker
|
|
290
|
+
|
|
291
|
+
`InterfaceWorker` is the TS mirror of Go's `core/impl/iface`: a worker whose job
|
|
292
|
+
is a seam between the bus and an external world (a chat-platform bridge, an admin
|
|
293
|
+
UI, a notifier). It faces the bus like any worker, and holds `Backend`s behind it.
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
import { InterfaceWorker, type Backend, type Handler } from "@niq.run/worker-sdk";
|
|
297
|
+
|
|
298
|
+
class FeishuBridge implements Backend {
|
|
299
|
+
private h?: Handler;
|
|
300
|
+
setHandler(h: Handler) { this.h = h; } // injected once, before start
|
|
301
|
+
async onMessage(text: string, to: string) {
|
|
302
|
+
await this.h!.send(createEvent("worker.input", { content: [{ type: "text", text }] }), to);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const worker = new InterfaceWorker({
|
|
307
|
+
id: "feishu",
|
|
308
|
+
channel, // any WorkerSideChannel (HTTPWorkerClient)
|
|
309
|
+
type: "interface",
|
|
310
|
+
backends: [new FeishuBridge()],
|
|
311
|
+
});
|
|
312
|
+
await worker.start();
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
- `Handler.request(target, evt, opts?)` — send and await the correlated reply;
|
|
316
|
+
a `request.failed`/`request.rejected` rejects with `ReplyError` (its `.reply`
|
|
317
|
+
carries the event). Timed out → rejects; `stop()` fails whatever is in flight.
|
|
318
|
+
- `Handler.onEvent(fn, filter?)` — inbound interest; each registration is isolated,
|
|
319
|
+
so a throwing backend cannot break the loop or another backend.
|
|
320
|
+
- What it does NOT do: edit ACLs or render the global event stream. Those are the
|
|
321
|
+
app's own bus handles, the same split as in Go.
|
|
322
|
+
|
|
323
|
+
### Stateless: `InterfaceSession`
|
|
324
|
+
|
|
325
|
+
`InterfaceSession` is the other half of the same contract: an ephemeral worker
|
|
326
|
+
for CLIs, scripts and one-shot tools. It dials, sends/requests/streams, and
|
|
327
|
+
closes. It keeps no presence (`worker.ready`), no backends and no state after
|
|
328
|
+
`close()` — the SSE session exists only for the object's lifetime.
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
import { HTTPWorkerClient, InterfaceSession, createEvent } from "@niq.run/worker-sdk";
|
|
332
|
+
|
|
333
|
+
const session = new InterfaceSession({
|
|
334
|
+
channel: new HTTPWorkerClient({ baseURL, workerID, credential }),
|
|
335
|
+
requestTimeoutMs: 30_000,
|
|
336
|
+
connectTimeoutMs: 15_000,
|
|
337
|
+
});
|
|
338
|
+
await session.connect();
|
|
339
|
+
|
|
340
|
+
await session.send(createEvent("worker.input", { text: "hi" }), "reason.0");
|
|
341
|
+
const reply = await session.request(
|
|
342
|
+
"hello@bus",
|
|
343
|
+
createEvent("hello.greet", { name: "niq" }),
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
for await (const evt of session.events()) {
|
|
347
|
+
// live events routed to this identity; ends on close() or channel end
|
|
348
|
+
}
|
|
349
|
+
await session.close();
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
| | `InterfaceWorker` (stateful) | `InterfaceSession` (stateless) |
|
|
353
|
+
|---|---|---|
|
|
354
|
+
| Lifetime | process | one command/scope |
|
|
355
|
+
| Presence | `worker.ready` + discover | none |
|
|
356
|
+
| Backends/tools | yes | no |
|
|
357
|
+
| Request correlation | yes | yes |
|
|
358
|
+
| Reconnect | caller-owned (currently) | no; fail fast |
|
|
359
|
+
| Typical use | bridge, daemon, UI backend | CLI, script, one-shot tool |
|
|
360
|
+
|
|
361
|
+
Both share the reply convention: `request.completed` resolves,
|
|
362
|
+
`request.failed`/`request.rejected` reject with `ReplyError`; a timeout rejects
|
|
363
|
+
with a plain error. `matchesType(pattern, type)` mirrors the bus's exact / `*` /
|
|
364
|
+
`prefix.*` filtering; `events()` is a single-consumer, buffered stream.
|
package/dist/iface.d.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { BaseWorker } from "./baseworker.js";
|
|
2
|
+
import type { HelpFunc } from "./help.js";
|
|
3
|
+
import { type Event, type EventTypeName, type Extension, type ExtensionHandler, type WorkerSideChannel } from "./types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* InterfaceWorker — the TypeScript half of niq's interface worker.
|
|
6
|
+
*
|
|
7
|
+
* A worker whose job is not a domain of its own but a seam between the bus and
|
|
8
|
+
* an external world (a chat platform bridge, an admin UI, a notifier). It faces
|
|
9
|
+
* the bus like every worker (identity, ACL, presence, discover, tools) and holds
|
|
10
|
+
* one or more {@link Backend}s behind it: the things that implement its
|
|
11
|
+
* capability.
|
|
12
|
+
*
|
|
13
|
+
* Mirrors Go's `core/impl/iface`:
|
|
14
|
+
*
|
|
15
|
+
* Backend.setHandler(handler) the worker injects the handles, once, before
|
|
16
|
+
* any event is delivered
|
|
17
|
+
* Handler.send/broadcast outbound as this worker
|
|
18
|
+
* Handler.request send + await the correlated reply
|
|
19
|
+
* Handler.onEvent(filter, fn) interest in inbound events (fan-out, isolated)
|
|
20
|
+
*
|
|
21
|
+
* It is NOT a bus control plane: editing ACLs and rendering the global event
|
|
22
|
+
* stream belong to the app's own bus handles, not here.
|
|
23
|
+
*/
|
|
24
|
+
/** A filter for inbound interest: event types, or omitted for everything. */
|
|
25
|
+
export type EventFilter = readonly string[];
|
|
26
|
+
/** What a backend may do on the bus, as this worker's identity. */
|
|
27
|
+
export interface Handler {
|
|
28
|
+
/** The worker id this handler speaks as. */
|
|
29
|
+
readonly id: string;
|
|
30
|
+
/** Send a directed event to the given targets. */
|
|
31
|
+
send(evt: Event, ...targets: string[]): Promise<void>;
|
|
32
|
+
/** Publish to every identity subscribing to the event type. */
|
|
33
|
+
broadcast(evt: Event): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Send and wait for the correlated `request.completed`, with the reply's error
|
|
36
|
+
* message surfaced for `request.failed` / `request.rejected`.
|
|
37
|
+
*
|
|
38
|
+
* The correlation is not a new protocol — the reply is an ordinary event
|
|
39
|
+
* addressed to this worker — but only the side holding the channel can pair it,
|
|
40
|
+
* so this is the one place that owns ids, the wait and the timeout.
|
|
41
|
+
*/
|
|
42
|
+
request(target: string, evt: Event, opts?: RequestOptions): Promise<Event>;
|
|
43
|
+
/**
|
|
44
|
+
* Register interest in inbound events. Returns an unsubscribe function.
|
|
45
|
+
* Delivery is per registration, in arrival order; a throwing subscriber cannot
|
|
46
|
+
* break the worker's loop or another subscriber.
|
|
47
|
+
*/
|
|
48
|
+
onEvent(fn: (evt: Event) => void, filter?: EventFilter): () => void;
|
|
49
|
+
}
|
|
50
|
+
export interface RequestOptions {
|
|
51
|
+
/** Override the worker's default request timeout. */
|
|
52
|
+
timeoutMs?: number;
|
|
53
|
+
}
|
|
54
|
+
/** One outward-facing surface: the thing behind the worker. */
|
|
55
|
+
export interface Backend {
|
|
56
|
+
/**
|
|
57
|
+
* Receive the handles for reaching the bus, and register inbound interest.
|
|
58
|
+
*
|
|
59
|
+
* Called exactly ONCE, before the worker starts its loop and before any event
|
|
60
|
+
* is delivered, so the backend can build its outward side already knowing how
|
|
61
|
+
* to reach inward.
|
|
62
|
+
*/
|
|
63
|
+
setHandler(handler: Handler): void;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A backend that needs a lifetime of its own: a WebSocket connection, a render
|
|
67
|
+
* loop, a subscription pump. `start()` runs after every backend has its handler;
|
|
68
|
+
* `stop()` runs in reverse order when the worker stops.
|
|
69
|
+
*/
|
|
70
|
+
export interface Runnable {
|
|
71
|
+
start(): Promise<void>;
|
|
72
|
+
stop(): Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The part of the worker a backend needs to serve its own tools: register the
|
|
76
|
+
* extension (schema included, so it rides `worker.ready`), then reply. The
|
|
77
|
+
* worker itself satisfies it, so a backend is wired by
|
|
78
|
+
* `backend.registerTools(worker)` — mirroring Go's `iface.ToolHost`.
|
|
79
|
+
*/
|
|
80
|
+
export interface ToolHost {
|
|
81
|
+
register(ext: Extension, handler: ExtensionHandler): void;
|
|
82
|
+
registerReadme(description: string, text: () => string, params?: Record<string, unknown>): void;
|
|
83
|
+
registerHelp(guides?: Record<string, HelpFunc>): void;
|
|
84
|
+
replyCompleted(callerID: string, callID: string, result: unknown, traceID?: string): Promise<void>;
|
|
85
|
+
replyFailed(callerID: string, callID: string, errorMessage: string, traceID?: string): Promise<void>;
|
|
86
|
+
}
|
|
87
|
+
export interface InterfaceWorkerOptions {
|
|
88
|
+
/** Worker identity on the bus. */
|
|
89
|
+
id: string;
|
|
90
|
+
/** Worker-side channel (already constructed; `start` connects it). */
|
|
91
|
+
channel: WorkerSideChannel;
|
|
92
|
+
/** The outward-facing surfaces this worker bridges to. */
|
|
93
|
+
backends?: Backend[];
|
|
94
|
+
/** Type label announced via `worker.ready` (e.g. "interface"). */
|
|
95
|
+
type?: string;
|
|
96
|
+
/** Presence contract: the events this worker declares it emits. */
|
|
97
|
+
publishes?: Record<string, unknown>[];
|
|
98
|
+
/** Default request timeout in ms (default 30s, mirroring Go). */
|
|
99
|
+
requestTimeoutMs?: number;
|
|
100
|
+
}
|
|
101
|
+
/** The default request timeout, matching Go's `iface.DefaultRequestTimeout`. */
|
|
102
|
+
export declare const DEFAULT_REQUEST_TIMEOUT_MS = 30000;
|
|
103
|
+
/** A failed or rejected reply. Carries the reply event for inspection. */
|
|
104
|
+
export { ReplyError } from "./reply.js";
|
|
105
|
+
export declare class InterfaceWorker extends BaseWorker {
|
|
106
|
+
private readonly backends;
|
|
107
|
+
private readonly typ;
|
|
108
|
+
private readonly publishes?;
|
|
109
|
+
private readonly requestTimeoutMs;
|
|
110
|
+
private readonly handler;
|
|
111
|
+
private readonly pending;
|
|
112
|
+
private readonly subs;
|
|
113
|
+
private loop?;
|
|
114
|
+
private stopping;
|
|
115
|
+
constructor(opts: InterfaceWorkerOptions);
|
|
116
|
+
/**
|
|
117
|
+
* Connect, inject the handler into every backend, then serve.
|
|
118
|
+
*
|
|
119
|
+
* Injection happens BEFORE the loop starts, so a backend can build its outward
|
|
120
|
+
* side already knowing how to reach inward.
|
|
121
|
+
*/
|
|
122
|
+
start(): Promise<void>;
|
|
123
|
+
/** Leave the bus: stop backends, fail in-flight requests, close. */
|
|
124
|
+
stop(): Promise<void>;
|
|
125
|
+
/** Waits for the serving loop to end (it ends when the channel closes). */
|
|
126
|
+
done(): Promise<void>;
|
|
127
|
+
private serve;
|
|
128
|
+
/**
|
|
129
|
+
* One inbound event: a reply resolves its pending request, a served tool is
|
|
130
|
+
* answered by the extension registry, everything else is a message for the
|
|
131
|
+
* backends.
|
|
132
|
+
*/
|
|
133
|
+
private dispatch;
|
|
134
|
+
/** Whether the extension registry handles this event type. */
|
|
135
|
+
private serves;
|
|
136
|
+
private resolve;
|
|
137
|
+
/** Fan out to interested subscriptions, reporting whether any took it. */
|
|
138
|
+
private publish;
|
|
139
|
+
/** Isolates a failing subscriber: one broken backend must not stop the rest. */
|
|
140
|
+
private deliver;
|
|
141
|
+
private newHandler;
|
|
142
|
+
private request;
|
|
143
|
+
private onEvent;
|
|
144
|
+
}
|
|
145
|
+
/** Convenience: build a reply event for a pending request. */
|
|
146
|
+
export declare function replyEvent(type: EventTypeName, requestId: string, payload: Record<string, unknown>, traceId?: string): Event;
|
|
147
|
+
//# sourceMappingURL=iface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iface.d.ts","sourceRoot":"","sources":["../src/iface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAI7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAEL,KAAK,KAAK,EACV,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACvB,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,6EAA6E;AAC7E,MAAM,MAAM,WAAW,GAAG,SAAS,MAAM,EAAE,CAAC;AAE5C,mEAAmE;AACnE,MAAM,WAAW,OAAO;IACtB,4CAA4C;IAC5C,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,kDAAkD;IAClD,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAA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rE,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;YAIb,KAAK;IAOnB;;;;OAIG;IACH,OAAO,CAAC,QAAQ;IAgBhB,8DAA8D;IAC9D,OAAO,CAAC,MAAM;IAId,OAAO,CAAC,OAAO;IAmBf,0EAA0E;IAC1E,OAAO,CAAC,OAAO;IAaf,gFAAgF;IAChF,OAAO,CAAC,OAAO;IAQf,OAAO,CAAC,UAAU;IAiBlB,OAAO,CAAC,OAAO;IAyBf,OAAO,CAAC,OAAO;CAQhB;AAED,8DAA8D;AAC9D,wBAAgB,UAAU,CAAC,IAAI,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,KAAK,CAK5H"}
|
package/dist/iface.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { BaseWorker } from "./baseworker.js";
|
|
2
|
+
import { createEvent } from "./createEvent.js";
|
|
3
|
+
import { newId } from "./id.js";
|
|
4
|
+
import { ReplyError } from "./reply.js";
|
|
5
|
+
import { EventType, } from "./types/index.js";
|
|
6
|
+
/** The default request timeout, matching Go's `iface.DefaultRequestTimeout`. */
|
|
7
|
+
export const DEFAULT_REQUEST_TIMEOUT_MS = 30_000;
|
|
8
|
+
/** A failed or rejected reply. Carries the reply event for inspection. */
|
|
9
|
+
export { ReplyError } from "./reply.js";
|
|
10
|
+
export class InterfaceWorker extends BaseWorker {
|
|
11
|
+
backends;
|
|
12
|
+
typ;
|
|
13
|
+
publishes;
|
|
14
|
+
requestTimeoutMs;
|
|
15
|
+
handler;
|
|
16
|
+
pending = new Map();
|
|
17
|
+
subs = new Set();
|
|
18
|
+
loop;
|
|
19
|
+
stopping = false;
|
|
20
|
+
constructor(opts) {
|
|
21
|
+
super({ id: opts.id, channel: opts.channel });
|
|
22
|
+
this.backends = opts.backends ?? [];
|
|
23
|
+
this.typ = opts.type ?? "interface";
|
|
24
|
+
this.publishes = opts.publishes;
|
|
25
|
+
this.requestTimeoutMs = opts.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
26
|
+
this.handler = this.newHandler();
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Connect, inject the handler into every backend, then serve.
|
|
30
|
+
*
|
|
31
|
+
* Injection happens BEFORE the loop starts, so a backend can build its outward
|
|
32
|
+
* side already knowing how to reach inward.
|
|
33
|
+
*/
|
|
34
|
+
async start() {
|
|
35
|
+
await this.channel.connect();
|
|
36
|
+
for (const backend of this.backends) {
|
|
37
|
+
backend.setHandler(this.handler);
|
|
38
|
+
}
|
|
39
|
+
// Then give each backend its own lifetime: a backend whose start fails would
|
|
40
|
+
// leave the worker deaf, so it is a start error.
|
|
41
|
+
for (const backend of this.backends) {
|
|
42
|
+
const runnable = backend;
|
|
43
|
+
if (typeof runnable.start === "function") {
|
|
44
|
+
await runnable.start();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
await this.announceReady(this.typ, this.publishes);
|
|
48
|
+
this.loop = this.serve();
|
|
49
|
+
}
|
|
50
|
+
/** Leave the bus: stop backends, fail in-flight requests, close. */
|
|
51
|
+
async stop() {
|
|
52
|
+
this.stopping = true;
|
|
53
|
+
// Backends stop first, in reverse, so a backend's last delivery can still
|
|
54
|
+
// reach the bus.
|
|
55
|
+
for (let i = this.backends.length - 1; i >= 0; i -= 1) {
|
|
56
|
+
const runnable = this.backends[i];
|
|
57
|
+
if (typeof runnable.stop === "function") {
|
|
58
|
+
try {
|
|
59
|
+
await runnable.stop();
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
console.warn(`[iface ${this.id}] backend stop failed:`, err);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (const [id, p] of this.pending) {
|
|
67
|
+
clearTimeout(p.timer);
|
|
68
|
+
p.reject(new Error(`interface worker stopped while waiting for request ${id}`));
|
|
69
|
+
}
|
|
70
|
+
this.pending.clear();
|
|
71
|
+
for (const sub of this.subs)
|
|
72
|
+
sub.active = false;
|
|
73
|
+
this.subs.clear();
|
|
74
|
+
await this.channel.close();
|
|
75
|
+
}
|
|
76
|
+
/** Waits for the serving loop to end (it ends when the channel closes). */
|
|
77
|
+
async done() {
|
|
78
|
+
await this.loop;
|
|
79
|
+
}
|
|
80
|
+
async serve() {
|
|
81
|
+
for await (const evt of this.channel.events()) {
|
|
82
|
+
if (this.stopping)
|
|
83
|
+
return;
|
|
84
|
+
this.dispatch(evt);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* One inbound event: a reply resolves its pending request, a served tool is
|
|
89
|
+
* answered by the extension registry, everything else is a message for the
|
|
90
|
+
* backends.
|
|
91
|
+
*/
|
|
92
|
+
dispatch(evt) {
|
|
93
|
+
if (this.resolve(evt)) {
|
|
94
|
+
// A reply is also an inbound event: a backend watching the conversation may
|
|
95
|
+
// want to render it.
|
|
96
|
+
this.publish(evt);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
// Order matters. A served tool is answered by the extension registry; any
|
|
100
|
+
// other event belongs to the backends, and a message a backend consumes is NOT
|
|
101
|
+
// a "no handler" error — that reply is only for events nobody wants. Asking
|
|
102
|
+
// the subscriptions first keeps a forwarded message from being answered with
|
|
103
|
+
// request.failed just because it carried a request id.
|
|
104
|
+
if (!this.serves(evt.type) && this.publish(evt))
|
|
105
|
+
return;
|
|
106
|
+
this.dispatchExtension(evt);
|
|
107
|
+
}
|
|
108
|
+
/** Whether the extension registry handles this event type. */
|
|
109
|
+
serves(type) {
|
|
110
|
+
return this.extensions().some((ext) => ext.event === type);
|
|
111
|
+
}
|
|
112
|
+
resolve(evt) {
|
|
113
|
+
if (evt.type !== EventType.RequestCompleted &&
|
|
114
|
+
evt.type !== EventType.RequestFailed &&
|
|
115
|
+
evt.type !== EventType.RequestRejected) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
const id = evt.request_id;
|
|
119
|
+
if (!id)
|
|
120
|
+
return false;
|
|
121
|
+
const p = this.pending.get(id);
|
|
122
|
+
if (!p)
|
|
123
|
+
return false;
|
|
124
|
+
this.pending.delete(id);
|
|
125
|
+
clearTimeout(p.timer);
|
|
126
|
+
if (evt.type === EventType.RequestCompleted)
|
|
127
|
+
p.resolve(evt);
|
|
128
|
+
else
|
|
129
|
+
p.reject(new ReplyError(evt));
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
/** Fan out to interested subscriptions, reporting whether any took it. */
|
|
133
|
+
publish(evt) {
|
|
134
|
+
let taken = false;
|
|
135
|
+
for (const sub of this.subs) {
|
|
136
|
+
if (!sub.active)
|
|
137
|
+
continue;
|
|
138
|
+
if (sub.filter && sub.filter.length > 0 && !sub.filter.includes(evt.type)) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
taken = true;
|
|
142
|
+
this.deliver(sub, evt);
|
|
143
|
+
}
|
|
144
|
+
return taken;
|
|
145
|
+
}
|
|
146
|
+
/** Isolates a failing subscriber: one broken backend must not stop the rest. */
|
|
147
|
+
deliver(sub, evt) {
|
|
148
|
+
try {
|
|
149
|
+
sub.fn(evt);
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
console.warn(`[iface ${this.id}] subscriber failed on ${evt.type}:`, err);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
newHandler() {
|
|
156
|
+
return {
|
|
157
|
+
id: this.id,
|
|
158
|
+
send: (evt, ...targets) => {
|
|
159
|
+
if (targets.length === 0) {
|
|
160
|
+
return Promise.reject(new Error("iface: send needs at least one target (use broadcast for everyone)"));
|
|
161
|
+
}
|
|
162
|
+
return this.channel.send(evt, ...targets);
|
|
163
|
+
},
|
|
164
|
+
broadcast: (evt) => this.channel.broadcast(evt),
|
|
165
|
+
request: (target, evt, opts) => this.request(target, evt, opts),
|
|
166
|
+
onEvent: (fn, filter) => this.onEvent(fn, filter),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
request(target, evt, opts) {
|
|
170
|
+
if (!target)
|
|
171
|
+
return Promise.reject(new Error("iface: request needs a target"));
|
|
172
|
+
if (!evt.trace_id)
|
|
173
|
+
evt.trace_id = newId();
|
|
174
|
+
if (!evt.request_id)
|
|
175
|
+
evt.request_id = newId();
|
|
176
|
+
const id = evt.request_id;
|
|
177
|
+
const timeout = opts?.timeoutMs ?? this.requestTimeoutMs;
|
|
178
|
+
return new Promise((resolve, reject) => {
|
|
179
|
+
if (this.stopping) {
|
|
180
|
+
reject(new Error("iface: worker is stopped"));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const timer = setTimeout(() => {
|
|
184
|
+
this.pending.delete(id);
|
|
185
|
+
reject(new Error(`iface: timed out after ${timeout}ms waiting for a reply from ${target}`));
|
|
186
|
+
}, timeout);
|
|
187
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
188
|
+
this.channel.send(evt, target).catch((err) => {
|
|
189
|
+
clearTimeout(timer);
|
|
190
|
+
this.pending.delete(id);
|
|
191
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
onEvent(fn, filter) {
|
|
196
|
+
const sub = { fn, filter, active: true };
|
|
197
|
+
this.subs.add(sub);
|
|
198
|
+
return () => {
|
|
199
|
+
sub.active = false;
|
|
200
|
+
this.subs.delete(sub);
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Convenience: build a reply event for a pending request. */
|
|
205
|
+
export function replyEvent(type, requestId, payload, traceId) {
|
|
206
|
+
const evt = createEvent(type, payload);
|
|
207
|
+
evt.request_id = requestId;
|
|
208
|
+
if (traceId)
|
|
209
|
+
evt.trace_id = traceId;
|
|
210
|
+
return evt;
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=iface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC9D,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,OAAO,CAAC,EAAwB,EAAE,MAAoB;QAC5D,MAAM,GAAG,GAAiB,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;QACvD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACnB,OAAO,GAAG,EAAE;YACV,GAAG,CAAC,MAAM,GAAG,KAAK,CAAC;YACnB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACxB,CAAC,CAAC;IACJ,CAAC;CACF;AAED,8DAA8D;AAC9D,MAAM,UAAU,UAAU,CAAC,IAAmB,EAAE,SAAiB,EAAE,OAAgC,EAAE,OAAgB;IACnH,MAAM,GAAG,GAAG,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACvC,GAAG,CAAC,UAAU,GAAG,SAAS,CAAC;IAC3B,IAAI,OAAO;QAAE,GAAG,CAAC,QAAQ,GAAG,OAAO,CAAC;IACpC,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
export { HTTPWorkerClient } from "./client.js";
|
|
2
2
|
export type { HTTPWorkerClientOptions } from "./client.js";
|
|
3
3
|
export { BaseWorker, parseToolCall, argString, argInt } from "./baseworker.js";
|
|
4
|
+
export { InterfaceWorker, ReplyError, DEFAULT_REQUEST_TIMEOUT_MS, replyEvent, } from "./iface.js";
|
|
5
|
+
export { InterfaceSession, DEFAULT_SESSION_REQUEST_TIMEOUT_MS, DEFAULT_SESSION_CONNECT_TIMEOUT_MS, matchesType, } from "./session.js";
|
|
6
|
+
export type { InterfaceSessionOptions, SessionRequestOptions, } from "./session.js";
|
|
7
|
+
export type { Backend as IfaceBackend, Handler as IfaceHandler, Runnable as IfaceRunnable, ToolHost as IfaceToolHost, EventFilter, InterfaceWorkerOptions, RequestOptions, } from "./iface.js";
|
|
4
8
|
export type { HelpFunc } from "./help.js";
|
|
5
9
|
export { replyValue, replyText } from "./reply.js";
|
|
6
10
|
export { s, isOptionalSchema, type JsonSchema } from "./schema.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE3D,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE3D,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAE/E,OAAO,EACL,eAAe,EACf,UAAU,EACV,0BAA0B,EAC1B,UAAU,GACX,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,gBAAgB,EAChB,kCAAkC,EAClC,kCAAkC,EAClC,WAAW,GACZ,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,OAAO,IAAI,YAAY,EACvB,OAAO,IAAI,YAAY,EACvB,QAAQ,IAAI,aAAa,EACzB,QAAQ,IAAI,aAAa,EACzB,WAAW,EACX,sBAAsB,EACtB,cAAc,GACf,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEnD,OAAO,EAAE,CAAC,EAAE,gBAAgB,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAEnE,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACtF,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,KAAK,MAAM,EAAE,MAAM,UAAU,CAAC;AACjE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C,OAAO,EACL,SAAS,EACT,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,iBAAiB,GACvB,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { HTTPWorkerClient } from "./client.js";
|
|
2
2
|
export { BaseWorker, parseToolCall, argString, argInt } from "./baseworker.js";
|
|
3
|
+
export { InterfaceWorker, ReplyError, DEFAULT_REQUEST_TIMEOUT_MS, replyEvent, } from "./iface.js";
|
|
4
|
+
export { InterfaceSession, DEFAULT_SESSION_REQUEST_TIMEOUT_MS, DEFAULT_SESSION_CONNECT_TIMEOUT_MS, matchesType, } from "./session.js";
|
|
3
5
|
export { replyValue, replyText } from "./reply.js";
|
|
4
6
|
export { s, isOptionalSchema } from "./schema.js";
|
|
5
7
|
export { parseCliArgs, busConnFromArgs, runWorkerApp, BUS_CLI_FLAGS } from "./cli.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG/C,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG/C,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAE/E,OAAO,EACL,eAAe,EACf,UAAU,EACV,0BAA0B,EAC1B,UAAU,GACX,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,gBAAgB,EAChB,kCAAkC,EAClC,kCAAkC,EAClC,WAAW,GACZ,MAAM,cAAc,CAAC;AAgBtB,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEnD,OAAO,EAAE,CAAC,EAAE,gBAAgB,EAAmB,MAAM,aAAa,CAAC;AAEnE,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAGtF,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,UAAU,EAAe,MAAM,UAAU,CAAC;AACjE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C,OAAO,EACL,SAAS,GAcV,MAAM,kBAAkB,CAAC"}
|
package/dist/reply.d.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* blocks. A structured reply must therefore not use `result` or `content` as a
|
|
10
10
|
* top-level field name.
|
|
11
11
|
*/
|
|
12
|
+
import type { Event } from "./types/index.js";
|
|
12
13
|
/**
|
|
13
14
|
* The value carried by a `request.completed` payload: the payload itself for an
|
|
14
15
|
* object reply, otherwise its `result` value.
|
|
@@ -20,4 +21,11 @@ export declare function replyValue(payload: Record<string, unknown>): unknown;
|
|
|
20
21
|
* rather than the JSON text `{}`.
|
|
21
22
|
*/
|
|
22
23
|
export declare function replyText(payload: Record<string, unknown>): string;
|
|
24
|
+
/** A failed or rejected reply. Carries the reply event for inspection. */
|
|
25
|
+
export declare class ReplyError extends Error {
|
|
26
|
+
readonly reply: Event;
|
|
27
|
+
constructor(reply: Event);
|
|
28
|
+
}
|
|
29
|
+
/** The message a failed/rejected reply carries (`error` or `reason`). */
|
|
30
|
+
export declare function replyErrorMessage(reply: Event): string;
|
|
23
31
|
//# sourceMappingURL=reply.d.ts.map
|
package/dist/reply.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reply.d.ts","sourceRoot":"","sources":["../src/reply.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;
|
|
1
|
+
{"version":3,"file":"reply.d.ts","sourceRoot":"","sources":["../src/reply.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAO9C;;;GAGG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAEpE;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAMlE;AAED,0EAA0E;AAC1E,qBAAa,UAAW,SAAQ,KAAK;IACnC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;gBAEV,KAAK,EAAE,KAAK;CAKzB;AAED,yEAAyE;AACzE,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,CAOtD"}
|
package/dist/reply.js
CHANGED
|
@@ -35,4 +35,23 @@ export function replyText(payload) {
|
|
|
35
35
|
return "";
|
|
36
36
|
return JSON.stringify(value);
|
|
37
37
|
}
|
|
38
|
+
/** A failed or rejected reply. Carries the reply event for inspection. */
|
|
39
|
+
export class ReplyError extends Error {
|
|
40
|
+
reply;
|
|
41
|
+
constructor(reply) {
|
|
42
|
+
super(replyErrorMessage(reply));
|
|
43
|
+
this.name = "ReplyError";
|
|
44
|
+
this.reply = reply;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** The message a failed/rejected reply carries (`error` or `reason`). */
|
|
48
|
+
export function replyErrorMessage(reply) {
|
|
49
|
+
const payload = reply.payload ?? {};
|
|
50
|
+
for (const key of ["error", "reason"]) {
|
|
51
|
+
const v = payload[key];
|
|
52
|
+
if (typeof v === "string" && v && v !== "<nil>")
|
|
53
|
+
return v;
|
|
54
|
+
}
|
|
55
|
+
return `reply ${reply.type} without a message`;
|
|
56
|
+
}
|
|
38
57
|
//# sourceMappingURL=reply.js.map
|
package/dist/reply.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reply.js","sourceRoot":"","sources":["../src/reply.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;
|
|
1
|
+
{"version":3,"file":"reply.js","sourceRoot":"","sources":["../src/reply.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,wDAAwD;AACxD,SAAS,QAAQ,CAAC,CAAU;IAC1B,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAClE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,OAAgC;IACzD,OAAO,QAAQ,IAA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type Event, type WorkerSideChannel } from "./types/index.js";
|
|
2
|
+
/** Default timeout for one request's reply. */
|
|
3
|
+
export declare const DEFAULT_SESSION_REQUEST_TIMEOUT_MS = 30000;
|
|
4
|
+
/** Default timeout for the channel handshake. */
|
|
5
|
+
export declare const DEFAULT_SESSION_CONNECT_TIMEOUT_MS = 15000;
|
|
6
|
+
export interface InterfaceSessionOptions {
|
|
7
|
+
/** The transport to dial (already constructed; `connect()` dials it). */
|
|
8
|
+
channel: WorkerSideChannel;
|
|
9
|
+
/** Default reply timeout for `request` (default 30s). */
|
|
10
|
+
requestTimeoutMs?: number;
|
|
11
|
+
/** Handshake timeout (default 15s). */
|
|
12
|
+
connectTimeoutMs?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface SessionRequestOptions {
|
|
15
|
+
/** Override the session's default reply timeout. */
|
|
16
|
+
timeoutMs?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A stateless interface worker: connect, send/request/stream, close.
|
|
20
|
+
*
|
|
21
|
+
* A request and a listener can share one session (the read loop always runs),
|
|
22
|
+
* but `events()` is a single-consumer stream. Events that arrive before a
|
|
23
|
+
* consumer reads them are buffered.
|
|
24
|
+
*/
|
|
25
|
+
export declare class InterfaceSession {
|
|
26
|
+
/** The identity this session speaks as. */
|
|
27
|
+
readonly id: string;
|
|
28
|
+
private readonly channel;
|
|
29
|
+
private readonly requestTimeoutMs;
|
|
30
|
+
private readonly connectTimeoutMs;
|
|
31
|
+
private readonly pending;
|
|
32
|
+
private readonly queue;
|
|
33
|
+
private loop?;
|
|
34
|
+
private connected;
|
|
35
|
+
private closed;
|
|
36
|
+
constructor(opts: InterfaceSessionOptions);
|
|
37
|
+
/** Dial the channel and start the read loop. Idempotent. */
|
|
38
|
+
connect(): Promise<void>;
|
|
39
|
+
/** Send a directed event. At least one target is required. */
|
|
40
|
+
send(evt: Event, ...targets: string[]): Promise<void>;
|
|
41
|
+
/** Publish to every identity subscribing to the event type. */
|
|
42
|
+
broadcast(evt: Event): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Send an event and await the correlated reply. A `request.failed` /
|
|
45
|
+
* `request.rejected` reply rejects with a {@link ReplyError} carrying the
|
|
46
|
+
* event. A timeout rejects with a plain Error.
|
|
47
|
+
*
|
|
48
|
+
* `request_id` and `trace_id` are filled in when absent, so the caller can
|
|
49
|
+
* read them back from the event it passed in.
|
|
50
|
+
*/
|
|
51
|
+
request(target: string, evt: Event, opts?: SessionRequestOptions): Promise<Event>;
|
|
52
|
+
/**
|
|
53
|
+
* Live inbound events, in arrival order. Single consumer; buffered until
|
|
54
|
+
* read. Iteration ends when the channel ends or `close()` is called.
|
|
55
|
+
*/
|
|
56
|
+
events(): AsyncIterable<Event>;
|
|
57
|
+
/** Leave the bus, fail in-flight requests and end `events()`. */
|
|
58
|
+
close(): Promise<void>;
|
|
59
|
+
/** Resolves when the read loop ends (channel closed or session closed). */
|
|
60
|
+
done(): Promise<void>;
|
|
61
|
+
private serve;
|
|
62
|
+
private dispatch;
|
|
63
|
+
private resolve;
|
|
64
|
+
private failPending;
|
|
65
|
+
private assertOpen;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Type match mirroring the bus: exact, `*`, and `prefix.*` (which also matches
|
|
69
|
+
* the bare prefix, e.g. `reason.*` matches `reason`).
|
|
70
|
+
*/
|
|
71
|
+
export declare function matchesType(pattern: string, type: string): boolean;
|
|
72
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAuBA,OAAO,EAEL,KAAK,KAAK,EACV,KAAK,iBAAiB,EACvB,MAAM,kBAAkB,CAAC;AAE1B,+CAA+C;AAC/C,eAAO,MAAM,kCAAkC,QAAS,CAAC;AACzD,iDAAiD;AACjD,eAAO,MAAM,kCAAkC,QAAS,CAAC;AAEzD,MAAM,WAAW,uBAAuB;IACtC,yEAA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OAAO,CAAC,IAAI,CAAC;YAIb,KAAK;IAqBnB,OAAO,CAAC,QAAQ;IAKhB,OAAO,CAAC,OAAO;IAmBf,OAAO,CAAC,WAAW;IAQnB,OAAO,CAAC,UAAU;CAKnB;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAOlE"}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* InterfaceSession — the stateless half of the interface worker.
|
|
3
|
+
*
|
|
4
|
+
* `InterfaceWorker` (iface.ts) is the stateful form: it announces presence,
|
|
5
|
+
* serves tools, owns backends and lives for the process. `InterfaceSession` is
|
|
6
|
+
* the ephemeral form used by CLIs, scripts and one-shot tools: dial, do the
|
|
7
|
+
* work, close. It keeps no presence, no backend and no state after `close()`;
|
|
8
|
+
* the SSE session itself still exists for the lifetime of the object.
|
|
9
|
+
*
|
|
10
|
+
* Both forms share the same request/reply convention: a request carries a fresh
|
|
11
|
+
* `request_id`, and the reply is an ordinary `request.completed` /
|
|
12
|
+
* `request.failed` / `request.rejected` addressed back to this identity.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* const session = new InterfaceSession({ channel });
|
|
16
|
+
* await session.connect();
|
|
17
|
+
* const reply = await session.request("hello@bus", createEvent("hello.greet", { name: "niq" }));
|
|
18
|
+
* for await (const evt of session.events()) { ... }
|
|
19
|
+
* await session.close();
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import { newId } from "./id.js";
|
|
23
|
+
import { ReplyError } from "./reply.js";
|
|
24
|
+
import { EventType, } from "./types/index.js";
|
|
25
|
+
/** Default timeout for one request's reply. */
|
|
26
|
+
export const DEFAULT_SESSION_REQUEST_TIMEOUT_MS = 30_000;
|
|
27
|
+
/** Default timeout for the channel handshake. */
|
|
28
|
+
export const DEFAULT_SESSION_CONNECT_TIMEOUT_MS = 15_000;
|
|
29
|
+
/**
|
|
30
|
+
* A stateless interface worker: connect, send/request/stream, close.
|
|
31
|
+
*
|
|
32
|
+
* A request and a listener can share one session (the read loop always runs),
|
|
33
|
+
* but `events()` is a single-consumer stream. Events that arrive before a
|
|
34
|
+
* consumer reads them are buffered.
|
|
35
|
+
*/
|
|
36
|
+
export class InterfaceSession {
|
|
37
|
+
/** The identity this session speaks as. */
|
|
38
|
+
id;
|
|
39
|
+
channel;
|
|
40
|
+
requestTimeoutMs;
|
|
41
|
+
connectTimeoutMs;
|
|
42
|
+
pending = new Map();
|
|
43
|
+
queue = new EventQueue();
|
|
44
|
+
loop;
|
|
45
|
+
connected = false;
|
|
46
|
+
closed = false;
|
|
47
|
+
constructor(opts) {
|
|
48
|
+
this.channel = opts.channel;
|
|
49
|
+
this.id = opts.channel.id;
|
|
50
|
+
this.requestTimeoutMs =
|
|
51
|
+
opts.requestTimeoutMs ?? DEFAULT_SESSION_REQUEST_TIMEOUT_MS;
|
|
52
|
+
this.connectTimeoutMs =
|
|
53
|
+
opts.connectTimeoutMs ?? DEFAULT_SESSION_CONNECT_TIMEOUT_MS;
|
|
54
|
+
}
|
|
55
|
+
/** Dial the channel and start the read loop. Idempotent. */
|
|
56
|
+
async connect() {
|
|
57
|
+
if (this.connected)
|
|
58
|
+
return;
|
|
59
|
+
if (this.closed)
|
|
60
|
+
throw new Error("niq: interface session is closed");
|
|
61
|
+
const connecting = this.channel.connect();
|
|
62
|
+
// The timeout may win the race; a late rejection still needs a handler.
|
|
63
|
+
connecting.catch(() => { });
|
|
64
|
+
let timer;
|
|
65
|
+
try {
|
|
66
|
+
await Promise.race([
|
|
67
|
+
connecting,
|
|
68
|
+
new Promise((_, reject) => {
|
|
69
|
+
timer = setTimeout(() => {
|
|
70
|
+
void this.channel.close().catch(() => { });
|
|
71
|
+
reject(new Error(`niq: interface session connect timed out after ${this.connectTimeoutMs}ms`));
|
|
72
|
+
}, this.connectTimeoutMs);
|
|
73
|
+
}),
|
|
74
|
+
]);
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
if (timer !== undefined)
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
}
|
|
80
|
+
this.connected = true;
|
|
81
|
+
this.loop = this.serve();
|
|
82
|
+
}
|
|
83
|
+
/** Send a directed event. At least one target is required. */
|
|
84
|
+
async send(evt, ...targets) {
|
|
85
|
+
if (targets.length === 0) {
|
|
86
|
+
throw new Error("niq: interface session send needs at least one target; use broadcast() for undirected delivery");
|
|
87
|
+
}
|
|
88
|
+
this.assertOpen();
|
|
89
|
+
await this.channel.send(evt, ...targets);
|
|
90
|
+
}
|
|
91
|
+
/** Publish to every identity subscribing to the event type. */
|
|
92
|
+
async broadcast(evt) {
|
|
93
|
+
this.assertOpen();
|
|
94
|
+
await this.channel.broadcast(evt);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Send an event and await the correlated reply. A `request.failed` /
|
|
98
|
+
* `request.rejected` reply rejects with a {@link ReplyError} carrying the
|
|
99
|
+
* event. A timeout rejects with a plain Error.
|
|
100
|
+
*
|
|
101
|
+
* `request_id` and `trace_id` are filled in when absent, so the caller can
|
|
102
|
+
* read them back from the event it passed in.
|
|
103
|
+
*/
|
|
104
|
+
request(target, evt, opts = {}) {
|
|
105
|
+
if (!target) {
|
|
106
|
+
return Promise.reject(new Error("niq: interface session request needs a target"));
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
this.assertOpen();
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
return Promise.reject(err);
|
|
113
|
+
}
|
|
114
|
+
const traceID = evt.trace_id ?? newId();
|
|
115
|
+
evt.trace_id = traceID;
|
|
116
|
+
const requestID = evt.request_id ?? newId();
|
|
117
|
+
evt.request_id = requestID;
|
|
118
|
+
const timeout = opts.timeoutMs ?? this.requestTimeoutMs;
|
|
119
|
+
return new Promise((resolve, reject) => {
|
|
120
|
+
const timer = setTimeout(() => {
|
|
121
|
+
this.pending.delete(requestID);
|
|
122
|
+
reject(new Error(`niq: interface session timed out after ${timeout}ms waiting for ` +
|
|
123
|
+
`${target} to answer ${evt.type} (request ${requestID})`));
|
|
124
|
+
}, timeout);
|
|
125
|
+
this.pending.set(requestID, { resolve, reject, timer });
|
|
126
|
+
void this.channel.send(evt, target).catch((err) => {
|
|
127
|
+
clearTimeout(timer);
|
|
128
|
+
this.pending.delete(requestID);
|
|
129
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Live inbound events, in arrival order. Single consumer; buffered until
|
|
135
|
+
* read. Iteration ends when the channel ends or `close()` is called.
|
|
136
|
+
*/
|
|
137
|
+
events() {
|
|
138
|
+
return this.queue;
|
|
139
|
+
}
|
|
140
|
+
/** Leave the bus, fail in-flight requests and end `events()`. */
|
|
141
|
+
async close() {
|
|
142
|
+
if (this.closed)
|
|
143
|
+
return;
|
|
144
|
+
this.closed = true;
|
|
145
|
+
this.connected = false;
|
|
146
|
+
this.failPending(new Error("niq: interface session closed while waiting for a reply"));
|
|
147
|
+
this.queue.close();
|
|
148
|
+
await this.channel.close().catch(() => { });
|
|
149
|
+
}
|
|
150
|
+
/** Resolves when the read loop ends (channel closed or session closed). */
|
|
151
|
+
async done() {
|
|
152
|
+
await this.loop;
|
|
153
|
+
}
|
|
154
|
+
async serve() {
|
|
155
|
+
try {
|
|
156
|
+
for await (const evt of this.channel.events()) {
|
|
157
|
+
this.dispatch(evt);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
catch (err) {
|
|
161
|
+
if (!this.closed) {
|
|
162
|
+
this.failPending(err instanceof Error ? err : new Error(String(err)));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
if (!this.closed) {
|
|
167
|
+
this.failPending(new Error("niq: the bus closed the interface session"));
|
|
168
|
+
}
|
|
169
|
+
this.queue.close();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
dispatch(evt) {
|
|
173
|
+
this.resolve(evt);
|
|
174
|
+
this.queue.push(evt);
|
|
175
|
+
}
|
|
176
|
+
resolve(evt) {
|
|
177
|
+
if (evt.type !== EventType.RequestCompleted &&
|
|
178
|
+
evt.type !== EventType.RequestFailed &&
|
|
179
|
+
evt.type !== EventType.RequestRejected) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const id = evt.request_id;
|
|
183
|
+
if (!id)
|
|
184
|
+
return;
|
|
185
|
+
const pending = this.pending.get(id);
|
|
186
|
+
if (!pending)
|
|
187
|
+
return;
|
|
188
|
+
this.pending.delete(id);
|
|
189
|
+
clearTimeout(pending.timer);
|
|
190
|
+
if (evt.type === EventType.RequestCompleted)
|
|
191
|
+
pending.resolve(evt);
|
|
192
|
+
else
|
|
193
|
+
pending.reject(new ReplyError(evt));
|
|
194
|
+
}
|
|
195
|
+
failPending(err) {
|
|
196
|
+
for (const [, pending] of this.pending) {
|
|
197
|
+
clearTimeout(pending.timer);
|
|
198
|
+
pending.reject(err);
|
|
199
|
+
}
|
|
200
|
+
this.pending.clear();
|
|
201
|
+
}
|
|
202
|
+
assertOpen() {
|
|
203
|
+
if (!this.connected || this.closed) {
|
|
204
|
+
throw new Error(`niq: interface session ${this.id} is not connected`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Type match mirroring the bus: exact, `*`, and `prefix.*` (which also matches
|
|
210
|
+
* the bare prefix, e.g. `reason.*` matches `reason`).
|
|
211
|
+
*/
|
|
212
|
+
export function matchesType(pattern, type) {
|
|
213
|
+
if (pattern === "*")
|
|
214
|
+
return true;
|
|
215
|
+
if (pattern.endsWith(".*")) {
|
|
216
|
+
const prefix = pattern.slice(0, -2);
|
|
217
|
+
return type === prefix || type.startsWith(`${prefix}.`);
|
|
218
|
+
}
|
|
219
|
+
return type === pattern;
|
|
220
|
+
}
|
|
221
|
+
/** A single-consumer async queue used by `events()`. */
|
|
222
|
+
class EventQueue {
|
|
223
|
+
items = [];
|
|
224
|
+
waiters = [];
|
|
225
|
+
done = false;
|
|
226
|
+
push(evt) {
|
|
227
|
+
if (this.done)
|
|
228
|
+
return;
|
|
229
|
+
const waiter = this.waiters.shift();
|
|
230
|
+
if (waiter)
|
|
231
|
+
waiter({ value: evt, done: false });
|
|
232
|
+
else
|
|
233
|
+
this.items.push(evt);
|
|
234
|
+
}
|
|
235
|
+
close() {
|
|
236
|
+
if (this.done)
|
|
237
|
+
return;
|
|
238
|
+
this.done = true;
|
|
239
|
+
for (const waiter of this.waiters.splice(0)) {
|
|
240
|
+
waiter({ value: undefined, done: true });
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
[Symbol.asyncIterator]() {
|
|
244
|
+
return {
|
|
245
|
+
next: () => {
|
|
246
|
+
if (this.items.length > 0) {
|
|
247
|
+
return Promise.resolve({ value: this.items.shift(), done: false });
|
|
248
|
+
}
|
|
249
|
+
if (this.done) {
|
|
250
|
+
return Promise.resolve({
|
|
251
|
+
value: undefined,
|
|
252
|
+
done: true,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
return new Promise((resolve) => this.waiters.push(resolve));
|
|
256
|
+
},
|
|
257
|
+
// A consumer leaving early does not close the queue: another consumer
|
|
258
|
+
// may read later. The session's close() is what ends it for everyone.
|
|
259
|
+
return: () => Promise.resolve({
|
|
260
|
+
value: undefined,
|
|
261
|
+
done: true,
|
|
262
|
+
}),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OA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C,EAAE,KAAK,EAAE,SAA6B,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QAC/D,CAAC;IACH,CAAC;IAED,CAAC,MAAM,CAAC,aAAa,CAAC;QACpB,OAAO;YACL,IAAI,EAAE,GAAmC,EAAE;gBACzC,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBAC1B,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,EAAG,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;gBACtE,CAAC;gBACD,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;oBACd,OAAO,OAAO,CAAC,OAAO,CAAC;wBACrB,KAAK,EAAE,SAA6B;wBACpC,IAAI,EAAE,IAAI;qBACX,CAAC,CAAC;gBACL,CAAC;gBACD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;YAC9D,CAAC;YACD,sEAAsE;YACtE,sEAAsE;YACtE,MAAM,EAAE,GAAmC,EAAE,CAC3C,OAAO,CAAC,OAAO,CAAC;gBACd,KAAK,EAAE,SAA6B;gBACpC,IAAI,EAAE,IAAI;aACX,CAAC;SACL,CAAC;IACJ,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@niq.run/worker-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "TypeScript SDK for authoring niq Workers that connect to a niq bus over HTTP (SSE + POST).",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/node": "^24.13.3",
|
|
36
36
|
"typescript": "^5.8.3",
|
|
37
|
-
"vitest": "^
|
|
37
|
+
"vitest": "^4.1.12"
|
|
38
38
|
},
|
|
39
39
|
"license": "MIT"
|
|
40
40
|
}
|