@niq.run/worker-sdk 0.3.0 → 0.4.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 CHANGED
@@ -98,6 +98,30 @@ for await (const evt of worker.channel.events()) {
98
98
  }
99
99
  ```
100
100
 
101
+ ### CLI helpers (writing a worker `start` entrypoint)
102
+
103
+ Out-of-process workers ship a `src/start.ts` (compiled to the package `bin`) to
104
+ run the worker as its own process. `cli` provides the shared launch glue:
105
+
106
+ ```ts
107
+ import { busConnFromArgs, parseCliArgs, runWorkerApp, HTTPWorkerClient } from "@niq.run/worker-sdk";
108
+
109
+ const { help, opts } = parseCliArgs(process.argv);
110
+ if (help) { printUsage(); process.exit(0); }
111
+
112
+ await runWorkerApp(async () => {
113
+ const client = new HTTPWorkerClient(busConnFromArgs(opts)); // CLI flags layered over NIQ_BUS_* env
114
+ await client.connect();
115
+ // ... run your worker ...
116
+ });
117
+ ```
118
+
119
+ - `parseCliArgs(argv)` — parse `--key value` / `--key`, flag `--help` / `-h`.
120
+ - `busConnFromArgs(opts, env?)` — merge bus connection params: explicit
121
+ `--bus-url` / `--worker-id` / `--credential` flags override the
122
+ supervisor-injected env (`NIQ_BUS_URL` / `NIQ_WORKER_ID` / `NIQ_WORKER_CREDENTIAL`).
123
+ - `runWorkerApp(main)` — run the app body, log + set `process.exitCode = 1` on failure.
124
+
101
125
  ## Protocol notes
102
126
 
103
127
  - **Every request carries its own `worker_id` + `credential`** — no session
package/dist/cli.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Small CLI utilities for writing a worker's `start` entrypoint — the launch
3
+ * glue that turns a worker into a runnable app (e.g. the `bin` target compiled
4
+ * from a `src/start.ts`).
5
+ *
6
+ * Every out-of-process worker builds on these so the connection bootstrap and
7
+ * arg parsing don't get re-implemented per worker.
8
+ */
9
+ import { type BusEnv } from "./env.js";
10
+ /** Result of parsing a worker's command-line arguments. */
11
+ export interface ParsedCliArgs {
12
+ /** True when `--help` / `-h` was passed. The caller prints its usage and exits. */
13
+ help: boolean;
14
+ /** Parsed `--key value` pairs; a flag given without a value maps to `true`. */
15
+ opts: Record<string, string | true>;
16
+ }
17
+ /**
18
+ * Parse `--key value` / `--key` arguments (strips the leading `node script`).
19
+ * Does NOT exit or print — the caller decides how to react to `--help`.
20
+ * Throws on an unexpected positional (non-flag) argument so the app's error
21
+ * wrapper can report it and exit non-zero.
22
+ */
23
+ export declare function parseCliArgs(argv: string[]): ParsedCliArgs;
24
+ /** `--flag` names recognized for the bus connection, mapped to option keys. */
25
+ export declare const BUS_CLI_FLAGS: {
26
+ readonly "bus-url": "baseURL";
27
+ readonly "worker-id": "workerID";
28
+ readonly credential: "credential";
29
+ };
30
+ /**
31
+ * Build bus connection parameters by layering explicit CLI flags on top of the
32
+ * supervisor-injected environment (`NIQ_BUS_URL` / `NIQ_WORKER_ID` /
33
+ * `NIQ_WORKER_CREDENTIAL`, see {@link readBusEnv}). Explicit flags win.
34
+ *
35
+ * `env` is injectable for tests; defaults to `process.env`.
36
+ */
37
+ export declare function busConnFromArgs(opts: Record<string, string | true>, env?: Record<string, string | undefined>): BusEnv;
38
+ /**
39
+ * Run a worker app body, setting `process.exitCode = 1` and logging on
40
+ * failure. Shared by every worker's `start` so failure handling is uniform.
41
+ */
42
+ export declare function runWorkerApp(run: () => Promise<void>): Promise<void>;
43
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAc,KAAK,MAAM,EAAE,MAAM,UAAU,CAAC;AAEnD,2DAA2D;AAC3D,MAAM,WAAW,aAAa;IAC5B,mFAAmF;IACnF,IAAI,EAAE,OAAO,CAAC;IACd,+EAA+E;IAC/E,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,CAAC;CACrC;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,aAAa,CAoB1D;AAED,+EAA+E;AAC/E,eAAO,MAAM,aAAa;;;;CAIhB,CAAC;AAEX;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,EACnC,GAAG,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAe,GACpD,MAAM,CAMR;AAED;;;GAGG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAO1E"}
package/dist/cli.js ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Small CLI utilities for writing a worker's `start` entrypoint — the launch
3
+ * glue that turns a worker into a runnable app (e.g. the `bin` target compiled
4
+ * from a `src/start.ts`).
5
+ *
6
+ * Every out-of-process worker builds on these so the connection bootstrap and
7
+ * arg parsing don't get re-implemented per worker.
8
+ */
9
+ import { readBusEnv } from "./env.js";
10
+ /**
11
+ * Parse `--key value` / `--key` arguments (strips the leading `node script`).
12
+ * Does NOT exit or print — the caller decides how to react to `--help`.
13
+ * Throws on an unexpected positional (non-flag) argument so the app's error
14
+ * wrapper can report it and exit non-zero.
15
+ */
16
+ export function parseCliArgs(argv) {
17
+ const args = argv.slice(2);
18
+ const help = args.includes("--help") || args.includes("-h");
19
+ const opts = {};
20
+ for (let i = 0; i < args.length; i++) {
21
+ const a = args[i];
22
+ if (a === "--help" || a === "-h")
23
+ continue;
24
+ if (!a.startsWith("--")) {
25
+ throw new Error(`unexpected argument: ${a}`);
26
+ }
27
+ const key = a.slice(2);
28
+ const next = args[i + 1];
29
+ if (next !== undefined && !next.startsWith("--")) {
30
+ opts[key] = next;
31
+ i++;
32
+ }
33
+ else {
34
+ opts[key] = true;
35
+ }
36
+ }
37
+ return { help, opts };
38
+ }
39
+ /** `--flag` names recognized for the bus connection, mapped to option keys. */
40
+ export const BUS_CLI_FLAGS = {
41
+ "bus-url": "baseURL",
42
+ "worker-id": "workerID",
43
+ credential: "credential",
44
+ };
45
+ /**
46
+ * Build bus connection parameters by layering explicit CLI flags on top of the
47
+ * supervisor-injected environment (`NIQ_BUS_URL` / `NIQ_WORKER_ID` /
48
+ * `NIQ_WORKER_CREDENTIAL`, see {@link readBusEnv}). Explicit flags win.
49
+ *
50
+ * `env` is injectable for tests; defaults to `process.env`.
51
+ */
52
+ export function busConnFromArgs(opts, env = process.env) {
53
+ const conn = readBusEnv(env);
54
+ for (const [flag, key] of Object.entries(BUS_CLI_FLAGS)) {
55
+ if (opts[flag] !== undefined)
56
+ conn[key] = String(opts[flag]);
57
+ }
58
+ return conn;
59
+ }
60
+ /**
61
+ * Run a worker app body, setting `process.exitCode = 1` and logging on
62
+ * failure. Shared by every worker's `start` so failure handling is uniform.
63
+ */
64
+ export async function runWorkerApp(run) {
65
+ try {
66
+ await run();
67
+ }
68
+ catch (err) {
69
+ console.error("[niq] worker exited:", err);
70
+ process.exitCode = 1;
71
+ }
72
+ }
73
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,UAAU,EAAe,MAAM,UAAU,CAAC;AAUnD;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,IAAc;IACzC,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3B,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC5D,MAAM,IAAI,GAAkC,EAAE,CAAC;IAC/C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EA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IAC/D,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,GAAwB;IACzD,IAAI,CAAC;QACH,MAAM,GAAG,EAAE,CAAC;IACd,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,sBAAsB,EAAE,GAAG,CAAC,CAAC;QAC3C,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;IACvB,CAAC;AACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=cli.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.test.d.ts","sourceRoot":"","sources":["../src/cli.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,59 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { BUS_CLI_FLAGS, busConnFromArgs, parseCliArgs } from "./cli.js";
3
+ import { BUS_ENV_VARS } from "./env.js";
4
+ describe("parseCliArgs", () => {
5
+ it("parses --key value and bare --flag", () => {
6
+ const { help, opts } = parseCliArgs([
7
+ "node",
8
+ "start.js",
9
+ "--bus-url",
10
+ "http://localhost:8080",
11
+ "--worker-id",
12
+ "w@me",
13
+ "--verbose",
14
+ ]);
15
+ expect(help).toBe(false);
16
+ expect(opts).toEqual({
17
+ "bus-url": "http://localhost:8080",
18
+ "worker-id": "w@me",
19
+ verbose: true,
20
+ });
21
+ });
22
+ it("flags --help / -h", () => {
23
+ expect(parseCliArgs(["node", "start.js", "--help"]).help).toBe(true);
24
+ expect(parseCliArgs(["node", "start.js", "-h"]).help).toBe(true);
25
+ });
26
+ it("strips --help/-h from opts", () => {
27
+ const { opts } = parseCliArgs(["node", "start.js", "--bus-url", "x", "--help"]);
28
+ expect(opts).toEqual({ "bus-url": "x" });
29
+ });
30
+ it("throws on a stray positional argument", () => {
31
+ expect(() => parseCliArgs(["node", "start.js", "nope"])).toThrow(/unexpected argument/);
32
+ });
33
+ });
34
+ describe("busConnFromArgs", () => {
35
+ const env = {
36
+ [BUS_ENV_VARS.busURL]: "http://localhost:8080/",
37
+ [BUS_ENV_VARS.workerID]: "w@me",
38
+ [BUS_ENV_VARS.credential]: "secret",
39
+ };
40
+ it("reads from env when no flags given", () => {
41
+ const conn = busConnFromArgs({}, env);
42
+ expect(conn.baseURL).toBe("http://localhost:8080");
43
+ expect(conn.workerID).toBe("w@me");
44
+ });
45
+ it("layers explicit flags over env", () => {
46
+ const conn = busConnFromArgs({ "bus-url": "http://alt:9999", "worker-id": "cli@me" }, env);
47
+ expect(conn.baseURL).toBe("http://alt:9999");
48
+ expect(conn.workerID).toBe("cli@me");
49
+ expect(conn.credential).toBe("secret");
50
+ });
51
+ it("exposes BUS_CLI_FLAGS mapping", () => {
52
+ expect(BUS_CLI_FLAGS).toEqual({
53
+ "bus-url": "baseURL",
54
+ "worker-id": "workerID",
55
+ credential: "credential",
56
+ });
57
+ });
58
+ });
59
+ //# sourceMappingURL=cli.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.test.js","sourceRoot":"","sources":["../src/cli.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,EAAE,CAAC,oCAAoC,EAAE,GAAG,EAAE;QAC5C,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,YAAY,CAAC;YAClC,MAAM;YACN,UAAU;YACV,WAAW;YACX,uBAAuB;YACvB,aAAa;YACb,MAAM;YACN,WAAW;SACZ,CAAC,CAAC;QACH,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACzB,MAAM,CAA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AAC;IAExC,EAAE,CAAC,oCAAoC,EAAE,GAAG,EAAE;QAC5C,MAAM,IAAI,GAAG,eAAe,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;QACtC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC;QACnD,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACrC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,gCAAgC,EAAE,GAAG,EAAE;QACxC,MAAM,IAAI,GAAG,eAAe,CAAC,EAAE,SAAS,EAAE,iBAAiB,EAAE,WAAW,EAAE,QAAQ,EAAE,EAAE,GAAG,CAAC,CAAC;QAC3F,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;QAC7C,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACrC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,+BAA+B,EAAE,GAAG,EAAE;QACvC,MAAM,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC;YAC5B,SAAS,EAAE,SAAS;YACpB,WAAW,EAAE,UAAU;YACvB,UAAU,EAAE,YAAY;SACzB,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
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 { parseCliArgs, busConnFromArgs, runWorkerApp, BUS_CLI_FLAGS } from "./cli.js";
5
+ export type { ParsedCliArgs } from "./cli.js";
4
6
  export { createEvent } from "./createEvent.js";
5
7
  export { BUS_ENV_VARS, readBusEnv, type BusEnv } from "./env.js";
6
8
  export { newId } from "./id.js";
@@ -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;AAE/E,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,gBAAgB,EACrB,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,iBAAiB,GACvB,MAAM,kBAAkB,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,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,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,6 @@
1
1
  export { HTTPWorkerClient } from "./client.js";
2
2
  export { BaseWorker, parseToolCall, argString, argInt } from "./baseworker.js";
3
+ export { parseCliArgs, busConnFromArgs, runWorkerApp, BUS_CLI_FLAGS } from "./cli.js";
3
4
  export { createEvent } from "./createEvent.js";
4
5
  export { BUS_ENV_VARS, readBusEnv } from "./env.js";
5
6
  export { newId } from "./id.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;AAE/E,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,GAaV,MAAM,kBAAkB,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,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,GAaV,MAAM,kBAAkB,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "@niq.run/worker-sdk",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "TypeScript SDK for authoring niq Workers that connect to a niq bus over HTTP (SSE + POST).",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/niq-run/niq.git"
8
+ },
5
9
  "type": "module",
6
10
  "main": "./dist/index.js",
7
11
  "types": "./dist/index.d.ts",
@@ -21,7 +25,8 @@
21
25
  "scripts": {
22
26
  "build": "tsc -p tsconfig.json",
23
27
  "test": "vitest run",
24
- "typecheck": "tsc --noEmit"
28
+ "typecheck": "tsc --noEmit",
29
+ "release": "git diff --quiet && git diff --cached --quiet || { echo \"Error: git working tree not clean\" >&2; exit 1; } && npm run test && npm run typecheck && npm run build && npm version --no-git-tag-version && npm publish"
25
30
  },
26
31
  "engines": {
27
32
  "node": ">=18"
package/dist/channel.d.ts DELETED
@@ -1,35 +0,0 @@
1
- import type { Event } from "./types.js";
2
- /**
3
- * WorkerSideChannel — the worker's view of a connection to the bus.
4
- *
5
- * Mirrors `core/bus.WorkerSideChannel`. Implementations hide whether the bus
6
- * is in-process, over HTTP, or behind a relay; the interface is the same.
7
- *
8
- * Semantics of Send vs Broadcast (deliberately distinct, not interchangeable):
9
- * - Send — "I know who should handle this." Directed to explicit targets.
10
- * targets must be non-empty (use Broadcast for undirected).
11
- * - Broadcast — "Whoever is interested handles it." Delivered to all online
12
- * workers whose subscription matches. If no one subscribes, the
13
- * event silently disappears — a valid end of the execution path.
14
- */
15
- export interface WorkerSideChannel {
16
- /** Transport-level connection identifier (this worker's ID). */
17
- readonly id: string;
18
- /**
19
- * Establish the connection to the bus. No-op if already connected.
20
- * @throws if the transport rejects the connection (bad credential, etc.)
21
- */
22
- connect(): Promise<void>;
23
- /** Send a directed event to specific target workers. targets must be non-empty. */
24
- send(event: Event, ...targets: string[]): Promise<void>;
25
- /** Broadcast an event to all matching subscribers. */
26
- broadcast(event: Event): Promise<void>;
27
- /**
28
- * Iterate events routed to this worker (directed or broadcast).
29
- * The stream ends when the connection closes.
30
- */
31
- events(): AsyncIterable<Event>;
32
- /** Close the connection and release resources. */
33
- close(): Promise<void>;
34
- }
35
- //# sourceMappingURL=channel.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,iBAAiB;IAChC,gEAAgE;IAChE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzB,mFAAmF;IACnF,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAExD,sDAAsD;IACtD,SAAS,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvC;;;OAGG;IACH,MAAM,IAAI,aAAa,CAAC,KAAK,CAAC,CAAC;IAE/B,kDAAkD;IAClD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
package/dist/channel.js DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=channel.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"channel.js","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":""}
package/dist/events.d.ts DELETED
@@ -1,25 +0,0 @@
1
- /**
2
- * Universal bus protocol event types.
3
- *
4
- * Every worker must use these exact names to interoperate — a worker
5
- * implementing a capability must publish/subscribe with the matching event
6
- * type. Worker-specific domain events (e.g. `reason.*`, `timer.*`) are not
7
- * listed here; they are defined by their owning worker.
8
- *
9
- * Mirrors `core/event` constants.
10
- */
11
- export declare const EventType: {
12
- readonly WorkerReady: "worker.ready";
13
- readonly WorkerGone: "worker.gone";
14
- readonly WorkerDiscover: "worker.discover";
15
- readonly WorkerInput: "worker.input";
16
- readonly WorkerAbort: "worker.abort";
17
- readonly ToolRequested: "tool.requested";
18
- readonly ToolCancel: "tool.cancel";
19
- readonly ToolCompleted: "tool.completed";
20
- readonly ToolFailed: "tool.failed";
21
- readonly ToolRejected: "tool.rejected";
22
- readonly ToolPartial: "tool.partial";
23
- };
24
- export type EventTypeName = (typeof EventType)[keyof typeof EventType];
25
- //# sourceMappingURL=events.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,eAAO,MAAM,SAAS;;;;;;;;;;;;CAeZ,CAAC;AAEX,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,OAAO,SAAS,CAAC,CAAC"}
package/dist/events.js DELETED
@@ -1,26 +0,0 @@
1
- /**
2
- * Universal bus protocol event types.
3
- *
4
- * Every worker must use these exact names to interoperate — a worker
5
- * implementing a capability must publish/subscribe with the matching event
6
- * type. Worker-specific domain events (e.g. `reason.*`, `timer.*`) are not
7
- * listed here; they are defined by their owning worker.
8
- *
9
- * Mirrors `core/event` constants.
10
- */
11
- export const EventType = {
12
- // Worker presence and lifecycle.
13
- WorkerReady: "worker.ready",
14
- WorkerGone: "worker.gone",
15
- WorkerDiscover: "worker.discover",
16
- WorkerInput: "worker.input",
17
- WorkerAbort: "worker.abort",
18
- // Tool invocation lifecycle.
19
- ToolRequested: "tool.requested",
20
- ToolCancel: "tool.cancel",
21
- ToolCompleted: "tool.completed",
22
- ToolFailed: "tool.failed",
23
- ToolRejected: "tool.rejected",
24
- ToolPartial: "tool.partial",
25
- };
26
- //# sourceMappingURL=events.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,iCAAiC;IACjC,WAAW,EAAE,cAAc;IAC3B,UAAU,EAAE,aAAa;IACzB,cAAc,EAAE,iBAAiB;IACjC,WAAW,EAAE,cAAc;IAC3B,WAAW,EAAE,cAAc;IAE3B,6BAA6B;IAC7B,aAAa,EAAE,gBAAgB;IAC/B,UAAU,EAAE,aAAa;IACzB,aAAa,EAAE,gBAAgB;IAC/B,UAAU,EAAE,aAAa;IACzB,YAAY,EAAE,eAAe;IAC7B,WAAW,EAAE,cAAc;CACnB,CAAC"}
package/dist/types.d.ts DELETED
@@ -1,62 +0,0 @@
1
- /**
2
- * RequestType classifies a delivery request sent from a worker to the bus.
3
- *
4
- * These are the API-names in the `type` field of the HTTP publish payload —
5
- * never change the serialized strings without changing the Go transport.
6
- */
7
- export type RequestType = "send" | "broadcast";
8
- /** Message type sent from a worker to the bus (worker → bus direction). */
9
- export type MessageType = RequestType | "heartbeat" | "disconnect";
10
- /** Lifecycle stage of an event. Mirrors `core/event.EventStatus`. */
11
- export type EventStatus = "created" | "routed" | "delivered";
12
- /**
13
- * Subscription declaration: an event of a type, optionally from a specific
14
- * source worker. Mirrors `core/event.EventPattern`.
15
- *
16
- * `type` supports exact match, `"*"` (any), and `"Prefix.*"` (prefix wildcard —
17
- * also matches the bare prefix, e.g. `"github.*"` matches `"github"` and
18
- * `"github.issue.new"`). An empty `type` matches nothing (fails closed).
19
- */
20
- export interface EventPattern {
21
- type: string;
22
- source_id?: string;
23
- }
24
- /**
25
- * The core data unit of the niq event bus. Field names are the wire names
26
- * (`snake_case`) and must match `core/event.Event` exactly.
27
- *
28
- * Note on authorship: the bus overwrites `worker_id` on receipt (anti-spoofing).
29
- * The worker MUST supply `id` and `timestamp` — the bus does not fill them.
30
- * Use {@link createEvent} to get these right.
31
- */
32
- export interface Event {
33
- id: string;
34
- type: string;
35
- status: EventStatus;
36
- payload: Record<string, unknown>;
37
- worker_id: string;
38
- target_worker_id?: string;
39
- trace_id?: string;
40
- specversion?: string;
41
- dataschema?: string;
42
- timestamp: number;
43
- recipients?: string[];
44
- }
45
- /**
46
- * A delivery request from a worker to the bus. Mirrors `core/bus.Request`.
47
- * Serialized over HTTP as the publish body; `MessageType` is the wire `type`.
48
- */
49
- export interface WorkerMessage {
50
- type: MessageType;
51
- events: Event[];
52
- targets?: string[];
53
- trace_id?: string;
54
- }
55
- /** Publish detail: which event types a worker may publish. */
56
- export interface Identity {
57
- worker_id: string;
58
- credential: string;
59
- publish_allow?: string[];
60
- subscribe_allow?: EventPattern[];
61
- }
62
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,WAAW,CAAC;AAE/C,2EAA2E;AAC3E,MAAM,MAAM,WAAW,GAAG,WAAW,GAAG,WAAW,GAAG,YAAY,CAAC;AAEnE,qEAAqE;AACrE,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,QAAQ,GAAG,WAAW,CAAC;AAE7D;;;;;;;GAOG;AACH,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,KAAK;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,WAAW,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;CACvB;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,WAAW,CAAC;IAClB,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,8DAA8D;AAC9D,MAAM,WAAW,QAAQ;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,eAAe,CAAC,EAAE,YAAY,EAAE,CAAC;CAClC"}
package/dist/types.js DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}