@niq.run/worker-sdk 0.3.0 → 0.5.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 +126 -1
- package/dist/baseworker.d.ts +28 -3
- package/dist/baseworker.d.ts.map +1 -1
- package/dist/baseworker.js +45 -4
- package/dist/baseworker.js.map +1 -1
- package/dist/cli.d.ts +43 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +73 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/reply.d.ts +23 -0
- package/dist/reply.d.ts.map +1 -0
- package/dist/reply.js +38 -0
- package/dist/reply.js.map +1 -0
- package/dist/schema.d.ts +63 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +141 -0
- package/dist/schema.js.map +1 -0
- package/dist/types/extension.d.ts +33 -1
- package/dist/types/extension.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/package.json +8 -3
- package/dist/baseworker.test.d.ts +0 -2
- package/dist/baseworker.test.d.ts.map +0 -1
- package/dist/baseworker.test.js +0 -181
- package/dist/baseworker.test.js.map +0 -1
- package/dist/channel.d.ts +0 -35
- package/dist/channel.d.ts.map +0 -1
- package/dist/channel.js +0 -2
- package/dist/channel.js.map +0 -1
- package/dist/client.test.d.ts +0 -2
- package/dist/client.test.d.ts.map +0 -1
- package/dist/client.test.js +0 -127
- package/dist/client.test.js.map +0 -1
- package/dist/createEvent.test.d.ts +0 -2
- package/dist/createEvent.test.d.ts.map +0 -1
- package/dist/createEvent.test.js +0 -20
- package/dist/createEvent.test.js.map +0 -1
- package/dist/env.test.d.ts +0 -2
- package/dist/env.test.d.ts.map +0 -1
- package/dist/env.test.js +0 -31
- package/dist/env.test.js.map +0 -1
- package/dist/events.d.ts +0 -25
- package/dist/events.d.ts.map +0 -1
- package/dist/events.js +0 -26
- package/dist/events.js.map +0 -1
- package/dist/types.d.ts +0 -62
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
package/README.md
CHANGED
|
@@ -58,6 +58,107 @@ import { HTTPWorkerClient, readBusEnv } from "@niq.run/worker-sdk";
|
|
|
58
58
|
const client = new HTTPWorkerClient(readBusEnv());
|
|
59
59
|
```
|
|
60
60
|
|
|
61
|
+
### Declaring a tool's schemas
|
|
62
|
+
|
|
63
|
+
**What the contract accepts.** `parameters` (the request) and `response` (the
|
|
64
|
+
reply) are typed `SchemaSource` (`object`): registration normalizes whatever you
|
|
65
|
+
pass to plain JSON Schema, and that plain schema is what rides `worker.ready`.
|
|
66
|
+
Accepted sources, all verified against the built SDK:
|
|
67
|
+
|
|
68
|
+
| You pass | Accepted | Why |
|
|
69
|
+
|---|---|---|
|
|
70
|
+
| plain JSON Schema object | ✅ as-is | already the wire form |
|
|
71
|
+
| `s.object({...})` | ✅ as-is | the builders emit JSON Schema |
|
|
72
|
+
| TypeBox `Type.Object({...})` | ✅ as-is | TypeBox *is* JSON Schema |
|
|
73
|
+
| zod 4 `z.object({...})` | ✅ converted | the schema exposes `.toJSONSchema()` |
|
|
74
|
+
| arktype `type({...})` | ✅ converted | exposes `.toJsonSchema()` |
|
|
75
|
+
| valibot / zod 3 | ✅ after converting | no converter method: pass `toJsonSchema(...)` / `zod-to-json-schema` output |
|
|
76
|
+
| valibot / zod 3 **raw** | ❌ throws at registration | their schemas carry library keys (`kind`, `_def`); announcing them would put internals on the wire |
|
|
77
|
+
| a string / number | ❌ compile error | a schema is an object |
|
|
78
|
+
|
|
79
|
+
Write them with the `s` builders instead of by hand:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { s } from "@niq.run/worker-sdk";
|
|
83
|
+
|
|
84
|
+
worker.register(
|
|
85
|
+
{
|
|
86
|
+
event: "lark.send",
|
|
87
|
+
description: "Send a message to a chat",
|
|
88
|
+
parameters: s.object({
|
|
89
|
+
target: s.string("chat_id, open_id or user_id"),
|
|
90
|
+
text: s.string("message body"),
|
|
91
|
+
file: s.optional(s.string("local path or http(s) URL")),
|
|
92
|
+
mode: s.optional(s.enumOf(["fast", "deep"])),
|
|
93
|
+
}),
|
|
94
|
+
response: s.object({ messageId: s.string(), ok: s.boolean() }),
|
|
95
|
+
},
|
|
96
|
+
handler,
|
|
97
|
+
);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Properties are required unless wrapped in `s.optional(...)`; arrays, enums and
|
|
101
|
+
free-form values are `s.array(...)`, `s.enumOf([...])`, `s.any()`.
|
|
102
|
+
|
|
103
|
+
Both schemas ride `worker.ready`, so peers and the directory worker see them
|
|
104
|
+
(`get_worker_info` → `parameters` / `response`). `response` is what lets an agent
|
|
105
|
+
— in particular a PTC script, written before anything runs — plan how to parse a
|
|
106
|
+
reply; declare it whenever the reply is structured.
|
|
107
|
+
|
|
108
|
+
**Bring your own schema library** — the SDK depends on none, and normalizes
|
|
109
|
+
whatever you pass:
|
|
110
|
+
|
|
111
|
+
| Library | What you write | Note |
|
|
112
|
+
|---|---|---|
|
|
113
|
+
| **zod 4** | `parameters: z.object({…})` | schemas expose `.toJSONSchema()`; the SDK calls it for you |
|
|
114
|
+
| **arktype** | `parameters: type({…})` | same, via `.toJsonSchema()` |
|
|
115
|
+
| **TypeBox** | `parameters: Type.Object({…})` | TypeBox *is* JSON Schema — passes through untouched |
|
|
116
|
+
| **valibot** | `parameters: toJsonSchema(v.object({…}))` | convert once at registration (`@valibot/to-json-schema`) |
|
|
117
|
+
| plain | `parameters: { type: "object", … }` | a JSON Schema object is used as-is |
|
|
118
|
+
| `s` helpers | `parameters: s.object({…})` | no dependency at all; see above |
|
|
119
|
+
|
|
120
|
+
Registration normalizes both schemas to plain JSON Schema, so the announced
|
|
121
|
+
contract (and every peer's tool list) is identical whichever route you take. A
|
|
122
|
+
value that is neither a schema nor convertible throws, naming the extension —
|
|
123
|
+
**including a library schema with no converter** (valibot, zod 3): those carry
|
|
124
|
+
`type`/`kind` at the top level, and announcing them would put library internals on
|
|
125
|
+
the wire, so they are rejected rather than silently passed through.
|
|
126
|
+
|
|
127
|
+
Validating args is your call, in the handler — nothing on the bus validates. With
|
|
128
|
+
zod/valibot the schema you already declared does it.
|
|
129
|
+
|
|
130
|
+
> **Full inference from TypeScript types** (a `handler(args: { text: string })`
|
|
131
|
+
> signature becoming a schema) is only possible at build time, because types are
|
|
132
|
+
> erased at runtime. The mature route is [typia](https://typia.io)
|
|
133
|
+
> (`typia.json.schemas<[Args]>()`, via a ts-patch transformer) or a CLI
|
|
134
|
+
> generator such as `ts-json-schema-generator`. Schema-first libraries
|
|
135
|
+
> (TypeBox, zod) are the usual trade-off: write the schema, derive the TS type
|
|
136
|
+
> from it (`Static<typeof T>`, `z.infer<T>`).
|
|
137
|
+
|
|
138
|
+
### Reply shape
|
|
139
|
+
|
|
140
|
+
`request.completed` payloads follow one rule (the same as the Go `baseworker`):
|
|
141
|
+
|
|
142
|
+
- an **object** reply *is* the payload — no wrapper;
|
|
143
|
+
- anything else (text, a number, an array) is wrapped as `{ result: value }`.
|
|
144
|
+
|
|
145
|
+
So a tool that returns structured data just passes the object:
|
|
146
|
+
`replyCompleted(caller, callID, { mounts, primary })`; text goes through
|
|
147
|
+
unchanged: `replyCompleted(caller, callID, "Exit code: 0")`. `replyObject(...)`
|
|
148
|
+
and `replyString(...)` are the same calls with the shape made explicit.
|
|
149
|
+
|
|
150
|
+
`result` and `content` are reserved top-level keys (the latter for multi-modal
|
|
151
|
+
blocks), so a structured reply should pick its own field names.
|
|
152
|
+
|
|
153
|
+
Reading a peer's reply uses the mirror helpers:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
import { replyValue, replyText } from "@niq.run/worker-sdk";
|
|
157
|
+
|
|
158
|
+
const listing = replyValue(evt.payload); // the object itself, or the result value
|
|
159
|
+
const line = replyText(evt.payload); // string verbatim, else JSON
|
|
160
|
+
```
|
|
161
|
+
|
|
61
162
|
### Base worker
|
|
62
163
|
|
|
63
164
|
`BaseWorker` (mirrors Go `pkg/baseworker`) layers the shared worker plumbing
|
|
@@ -84,7 +185,7 @@ worker.register(
|
|
|
84
185
|
{ event: "tool.invoke", keyField: "name", key: "echo", description: "Echo a message" },
|
|
85
186
|
(evt) => {
|
|
86
187
|
const call = parseToolCall(evt);
|
|
87
|
-
worker.replyCompleted(call.callerID, call.callID,
|
|
188
|
+
worker.replyCompleted(call.callerID, call.callID, `echo: ${call.args["text"] ?? ""}`, call.traceID);
|
|
88
189
|
},
|
|
89
190
|
);
|
|
90
191
|
|
|
@@ -98,6 +199,30 @@ for await (const evt of worker.channel.events()) {
|
|
|
98
199
|
}
|
|
99
200
|
```
|
|
100
201
|
|
|
202
|
+
### CLI helpers (writing a worker `start` entrypoint)
|
|
203
|
+
|
|
204
|
+
Out-of-process workers ship a `src/start.ts` (compiled to the package `bin`) to
|
|
205
|
+
run the worker as its own process. `cli` provides the shared launch glue:
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
import { busConnFromArgs, parseCliArgs, runWorkerApp, HTTPWorkerClient } from "@niq.run/worker-sdk";
|
|
209
|
+
|
|
210
|
+
const { help, opts } = parseCliArgs(process.argv);
|
|
211
|
+
if (help) { printUsage(); process.exit(0); }
|
|
212
|
+
|
|
213
|
+
await runWorkerApp(async () => {
|
|
214
|
+
const client = new HTTPWorkerClient(busConnFromArgs(opts)); // CLI flags layered over NIQ_BUS_* env
|
|
215
|
+
await client.connect();
|
|
216
|
+
// ... run your worker ...
|
|
217
|
+
});
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
- `parseCliArgs(argv)` — parse `--key value` / `--key`, flag `--help` / `-h`.
|
|
221
|
+
- `busConnFromArgs(opts, env?)` — merge bus connection params: explicit
|
|
222
|
+
`--bus-url` / `--worker-id` / `--credential` flags override the
|
|
223
|
+
supervisor-injected env (`NIQ_BUS_URL` / `NIQ_WORKER_ID` / `NIQ_WORKER_CREDENTIAL`).
|
|
224
|
+
- `runWorkerApp(main)` — run the app body, log + set `process.exitCode = 1` on failure.
|
|
225
|
+
|
|
101
226
|
## Protocol notes
|
|
102
227
|
|
|
103
228
|
- **Every request carries its own `worker_id` + `credential`** — no session
|
package/dist/baseworker.d.ts
CHANGED
|
@@ -20,7 +20,14 @@ export declare class BaseWorker {
|
|
|
20
20
|
subscriptions?: EventPattern[];
|
|
21
21
|
channel: WorkerSideChannel;
|
|
22
22
|
});
|
|
23
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* Bind an extension to a handler. Safe at any time, including at runtime.
|
|
25
|
+
*
|
|
26
|
+
* Declared schemas are normalized here: `parameters` / `response` may be plain
|
|
27
|
+
* JSON Schema, or anything that converts to one (zod 4, arktype, …), so the
|
|
28
|
+
* announced contract — and therefore every peer's tool list — is always plain
|
|
29
|
+
* JSON Schema.
|
|
30
|
+
*/
|
|
24
31
|
register(ext: Extension, handler: ExtensionHandler): void;
|
|
25
32
|
/**
|
|
26
33
|
* Route an event to the registered extension matching its event type.
|
|
@@ -43,8 +50,26 @@ export declare class BaseWorker {
|
|
|
43
50
|
* (`worker.ready` "publishes").
|
|
44
51
|
*/
|
|
45
52
|
announceReady(workerType: string, publishes?: Record<string, unknown>[]): Promise<void>;
|
|
46
|
-
/**
|
|
47
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Answer a request caller with a request.completed result.
|
|
55
|
+
*
|
|
56
|
+
* A convenience, not the protocol: the bus only requires the payload to be a
|
|
57
|
+
* JSON object, and a worker may build one by hand. What this does:
|
|
58
|
+
*
|
|
59
|
+
* - an OBJECT reply is the payload — no wrapper, so the event log shows the
|
|
60
|
+
* reply itself;
|
|
61
|
+
* - anything else (a string, a number, an array) cannot be a payload on its
|
|
62
|
+
* own, so it is wrapped as `{ result: value }`.
|
|
63
|
+
*
|
|
64
|
+
* `result` and `content` are therefore reserved top-level keys of a reply
|
|
65
|
+
* payload (the latter for multi-modal blocks). replyObject / replyString say
|
|
66
|
+
* which shape you mean.
|
|
67
|
+
*/
|
|
68
|
+
replyCompleted(callerID: string, callID: string, result: unknown, traceID?: string): Promise<void>;
|
|
69
|
+
/** Answer with a structured payload: the object IS the payload. */
|
|
70
|
+
replyObject(callerID: string, callID: string, payload: Record<string, unknown>, traceID?: string): Promise<void>;
|
|
71
|
+
/** Answer with text: the payload is `{ result: text }`. */
|
|
72
|
+
replyString(callerID: string, callID: string, text: string, traceID?: string): Promise<void>;
|
|
48
73
|
/** Answer a request caller with a request.failed error message. */
|
|
49
74
|
replyFailed(callerID: string, callID: string, error: string, traceID?: string): Promise<void>;
|
|
50
75
|
/**
|
package/dist/baseworker.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"baseworker.d.ts","sourceRoot":"","sources":["../src/baseworker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,KAAK,EACL,YAAY,EACZ,SAAS,EACT,gBAAgB,EAChB,QAAQ,EACR,iBAAiB,EAClB,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"baseworker.d.ts","sourceRoot":"","sources":["../src/baseworker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,KAAK,EACL,YAAY,EACZ,SAAS,EACT,gBAAgB,EAChB,QAAQ,EACR,iBAAiB,EAClB,MAAM,kBAAkB,CAAC;AAK1B;;;;;;;;;;GAUG;AACH,qBAAa,UAAU;IACrB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,aAAa,EAAE,YAAY,EAAE,CAAC;IACvC,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAAC;IAEpC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAGjB;gBAEQ,IAAI,EAAE;QAChB,EAAE,EAAE,MAAM,CAAC;QACX,aAAa,CAAC,EAAE,YAAY,EAAE,CAAC;QAC/B,OAAO,EAAE,iBAAiB,CAAC;KAC5B;IAM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OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAUhB,gEAAgE;IAC1D,gBAAgB,CAAC,EAAE,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;YASrC,KAAK;CAgBpB;AAED,kGAAkG;AAClG,wBAAgB,aAAa,CAAC,GAAG,EAAE,KAAK,GAAG,QAAQ,CAQlD;AAED,mEAAmE;AACnE,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAG5E;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CACpB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,GACV,MAAM,CAIR"}
|
package/dist/baseworker.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EventType } from "./types/index.js";
|
|
2
2
|
import { createEvent } from "./createEvent.js";
|
|
3
|
+
import { toJsonSchema } from "./schema.js";
|
|
3
4
|
/**
|
|
4
5
|
* BaseWorker — the shared base implementation every worker builds on,
|
|
5
6
|
* mirroring Go `pkg/baseworker.BaseWorker`. It holds identity, subscription
|
|
@@ -21,9 +22,23 @@ export class BaseWorker {
|
|
|
21
22
|
this.subscriptions = opts.subscriptions ?? [];
|
|
22
23
|
this.channel = opts.channel;
|
|
23
24
|
}
|
|
24
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Bind an extension to a handler. Safe at any time, including at runtime.
|
|
27
|
+
*
|
|
28
|
+
* Declared schemas are normalized here: `parameters` / `response` may be plain
|
|
29
|
+
* JSON Schema, or anything that converts to one (zod 4, arktype, …), so the
|
|
30
|
+
* announced contract — and therefore every peer's tool list — is always plain
|
|
31
|
+
* JSON Schema.
|
|
32
|
+
*/
|
|
25
33
|
register(ext, handler) {
|
|
26
|
-
|
|
34
|
+
const normalized = { ...ext };
|
|
35
|
+
if (ext.parameters !== undefined) {
|
|
36
|
+
normalized.parameters = toJsonSchema(ext.parameters, `parameters of ${ext.event}`);
|
|
37
|
+
}
|
|
38
|
+
if (ext.response !== undefined) {
|
|
39
|
+
normalized.response = toJsonSchema(ext.response, `response of ${ext.event}`);
|
|
40
|
+
}
|
|
41
|
+
this.regs.set(extensionKey(ext), { ext: normalized, handler });
|
|
27
42
|
}
|
|
28
43
|
/**
|
|
29
44
|
* Route an event to the registered extension matching its event type.
|
|
@@ -60,6 +75,9 @@ export class BaseWorker {
|
|
|
60
75
|
if (ext.parameters) {
|
|
61
76
|
entry["parameters"] = ext.parameters;
|
|
62
77
|
}
|
|
78
|
+
if (ext.response) {
|
|
79
|
+
entry["response"] = ext.response;
|
|
80
|
+
}
|
|
63
81
|
out.push(entry);
|
|
64
82
|
}
|
|
65
83
|
return out;
|
|
@@ -93,9 +111,32 @@ export class BaseWorker {
|
|
|
93
111
|
// tool-invocation with a request.completed / request.failed /
|
|
94
112
|
// request.rejected result, echoing the request's id and propagating the
|
|
95
113
|
// trace ID. Reply errors are ignored, mirroring Go.
|
|
96
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* Answer a request caller with a request.completed result.
|
|
116
|
+
*
|
|
117
|
+
* A convenience, not the protocol: the bus only requires the payload to be a
|
|
118
|
+
* JSON object, and a worker may build one by hand. What this does:
|
|
119
|
+
*
|
|
120
|
+
* - an OBJECT reply is the payload — no wrapper, so the event log shows the
|
|
121
|
+
* reply itself;
|
|
122
|
+
* - anything else (a string, a number, an array) cannot be a payload on its
|
|
123
|
+
* own, so it is wrapped as `{ result: value }`.
|
|
124
|
+
*
|
|
125
|
+
* `result` and `content` are therefore reserved top-level keys of a reply
|
|
126
|
+
* payload (the latter for multi-modal blocks). replyObject / replyString say
|
|
127
|
+
* which shape you mean.
|
|
128
|
+
*/
|
|
97
129
|
async replyCompleted(callerID, callID, result, traceID) {
|
|
98
|
-
|
|
130
|
+
const payload = isRecord(result) ? result : { result };
|
|
131
|
+
await this.reply(EventType.RequestCompleted, callerID, callID, payload, traceID);
|
|
132
|
+
}
|
|
133
|
+
/** Answer with a structured payload: the object IS the payload. */
|
|
134
|
+
async replyObject(callerID, callID, payload, traceID) {
|
|
135
|
+
await this.replyCompleted(callerID, callID, payload, traceID);
|
|
136
|
+
}
|
|
137
|
+
/** Answer with text: the payload is `{ result: text }`. */
|
|
138
|
+
async replyString(callerID, callID, text, traceID) {
|
|
139
|
+
await this.replyCompleted(callerID, callID, text, traceID);
|
|
99
140
|
}
|
|
100
141
|
/** Answer a request caller with a request.failed error message. */
|
|
101
142
|
async replyFailed(callerID, callID, error, traceID) {
|
package/dist/baseworker.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"baseworker.js","sourceRoot":"","sources":["../src/baseworker.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"baseworker.js","sourceRoot":"","sources":["../src/baseworker.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C;;;;;;;;;;GAUG;AACH,MAAM,OAAO,UAAU;IACZ,EAAE,CAAS;IACX,aAAa,CAAiB;IAC9B,OAAO,CAAoB;IAEnB,IAAI,GAAG,IAAI,GAAG,EAG5B,CAAC;IAEJ,YAAY,IAIX;QACC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC;QAClB,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,IAAI,EAAE,CAAC;QAC9C,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;IAC9B,CAAC;IAED;;;;;;;OAOG;IACH,QA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AAO,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACtE,OAAO,GAAG,CAAC;AACb,CAAC;AAED,2EAA2E;AAC3E,uEAAuE;AACvE,sDAAsD;AACtD,SAAS,YAAY,CAAC,GAAc;IAClC,OAAO,GAAG,GAAG,CAAC,KAAK,MAAM,CAAC;AAC5B,CAAC;AAED,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"}
|
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
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],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;IACpB,WAAW,EAAE,UAAU;IACvB,UAAU,EAAE,YAAY;CAChB,CAAC;AAEX;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAC7B,IAAmC,EACnC,MAA0C,OAAO,CAAC,GAAG;IAErD,MAAM,IAAI,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;IAC7B,KAAK,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC;QACxD,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,SAAS;YAAE,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;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"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
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 { replyValue, replyText } from "./reply.js";
|
|
5
|
+
export { s, isOptionalSchema, type JsonSchema } from "./schema.js";
|
|
6
|
+
export { parseCliArgs, busConnFromArgs, runWorkerApp, BUS_CLI_FLAGS } from "./cli.js";
|
|
7
|
+
export type { ParsedCliArgs } from "./cli.js";
|
|
4
8
|
export { createEvent } from "./createEvent.js";
|
|
5
9
|
export { BUS_ENV_VARS, readBusEnv, type BusEnv } from "./env.js";
|
|
6
10
|
export { newId } from "./id.js";
|
|
7
11
|
export { parseEventStream } from "./sse.js";
|
|
8
|
-
export { EventType, type Event, type EventPattern, type EventStatus, type EventTypeName, type Extension, type ExtensionHandler, type Identity, type MessageType, type RequestType, type ToolCall, type WorkerMessage, type WorkerSideChannel, } from "./types/index.js";
|
|
12
|
+
export { EventType, type Event, type EventPattern, type EventStatus, type EventTypeName, type Extension, type SchemaSource, type ExtensionHandler, type Identity, type MessageType, type RequestType, type ToolCall, type WorkerMessage, type WorkerSideChannel, } from "./types/index.js";
|
|
9
13
|
//# sourceMappingURL=index.d.ts.map
|
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;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,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,8 @@
|
|
|
1
1
|
export { HTTPWorkerClient } from "./client.js";
|
|
2
2
|
export { BaseWorker, parseToolCall, argString, argInt } from "./baseworker.js";
|
|
3
|
+
export { replyValue, replyText } from "./reply.js";
|
|
4
|
+
export { s, isOptionalSchema } from "./schema.js";
|
|
5
|
+
export { parseCliArgs, busConnFromArgs, runWorkerApp, BUS_CLI_FLAGS } from "./cli.js";
|
|
3
6
|
export { createEvent } from "./createEvent.js";
|
|
4
7
|
export { BUS_ENV_VARS, readBusEnv } from "./env.js";
|
|
5
8
|
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,
|
|
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,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
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reply conventions of a `request.completed` payload — the TS mirror of Go's
|
|
3
|
+
* `event.ReplyValue` / `event.ReplyText`.
|
|
4
|
+
*
|
|
5
|
+
* The bus only requires a payload to be a JSON object. What a caller is after is
|
|
6
|
+
* either the payload itself (an OBJECT reply) or, when the reply is not an object
|
|
7
|
+
* (text, a number, an array), its `result` entry. `content` is the second
|
|
8
|
+
* reserved key: multi-modal blocks a reason worker decodes into llm content
|
|
9
|
+
* blocks. A structured reply must therefore not use `result` or `content` as a
|
|
10
|
+
* top-level field name.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* The value carried by a `request.completed` payload: the payload itself for an
|
|
14
|
+
* object reply, otherwise its `result` value.
|
|
15
|
+
*/
|
|
16
|
+
export declare function replyValue(payload: Record<string, unknown>): unknown;
|
|
17
|
+
/**
|
|
18
|
+
* Render that value as text: a string verbatim, anything else JSON. This is what
|
|
19
|
+
* goes into a transcript or a log line. An empty payload means "nothing to say"
|
|
20
|
+
* rather than the JSON text `{}`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function replyText(payload: Record<string, unknown>): string;
|
|
23
|
+
//# sourceMappingURL=reply.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reply.d.ts","sourceRoot":"","sources":["../src/reply.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAOH;;;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"}
|
package/dist/reply.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reply conventions of a `request.completed` payload — the TS mirror of Go's
|
|
3
|
+
* `event.ReplyValue` / `event.ReplyText`.
|
|
4
|
+
*
|
|
5
|
+
* The bus only requires a payload to be a JSON object. What a caller is after is
|
|
6
|
+
* either the payload itself (an OBJECT reply) or, when the reply is not an object
|
|
7
|
+
* (text, a number, an array), its `result` entry. `content` is the second
|
|
8
|
+
* reserved key: multi-modal blocks a reason worker decodes into llm content
|
|
9
|
+
* blocks. A structured reply must therefore not use `result` or `content` as a
|
|
10
|
+
* top-level field name.
|
|
11
|
+
*/
|
|
12
|
+
/** True for a plain object (not null, not an array). */
|
|
13
|
+
function isRecord(v) {
|
|
14
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The value carried by a `request.completed` payload: the payload itself for an
|
|
18
|
+
* object reply, otherwise its `result` value.
|
|
19
|
+
*/
|
|
20
|
+
export function replyValue(payload) {
|
|
21
|
+
return "result" in payload ? payload.result : payload;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Render that value as text: a string verbatim, anything else JSON. This is what
|
|
25
|
+
* goes into a transcript or a log line. An empty payload means "nothing to say"
|
|
26
|
+
* rather than the JSON text `{}`.
|
|
27
|
+
*/
|
|
28
|
+
export function replyText(payload) {
|
|
29
|
+
const value = replyValue(payload);
|
|
30
|
+
if (typeof value === "string")
|
|
31
|
+
return value;
|
|
32
|
+
if (value === null || value === undefined)
|
|
33
|
+
return "";
|
|
34
|
+
if (isRecord(value) && Object.keys(value).length === 0)
|
|
35
|
+
return "";
|
|
36
|
+
return JSON.stringify(value);
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=reply.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reply.js","sourceRoot":"","sources":["../src/reply.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,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,IAAI,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;AACxD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,OAAgC;IACxD,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;IAClC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5C,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IACrD,IAAI,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IAClE,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC"}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema builders for a capability declaration — the TS mirror of Go's
|
|
3
|
+
* `baseworker.ObjectSchema` / `ArraySchema` / `StringSchema`.
|
|
4
|
+
*
|
|
5
|
+
* `parameters` describes the request, `response` describes the reply. Both are
|
|
6
|
+
* plain JSON Schema objects on the wire (`worker.ready` -> `watch[]`), so hand
|
|
7
|
+
* writing them is possible but noisy; these helpers build the same thing in a
|
|
8
|
+
* line or two:
|
|
9
|
+
*
|
|
10
|
+
* worker.register({
|
|
11
|
+
* event: "lark.send",
|
|
12
|
+
* description: "Send a message",
|
|
13
|
+
* parameters: s.object({
|
|
14
|
+
* target: s.string("chat_id, open_id or user_id"),
|
|
15
|
+
* text: s.string("message body"),
|
|
16
|
+
* file: s.optional(s.string("local path or http(s) URL")),
|
|
17
|
+
* size: s.optional(s.integer("max characters")),
|
|
18
|
+
* }),
|
|
19
|
+
* response: s.object({ messageId: s.string(), ok: s.boolean() }),
|
|
20
|
+
* }, handler);
|
|
21
|
+
*
|
|
22
|
+
* They are documentation, not validation: nothing on the bus checks a payload
|
|
23
|
+
* against them. Validate in the handler if you need to (zod/valibot work fine —
|
|
24
|
+
* `z.toJSONSchema(schema)` also produces a `parameters` value directly).
|
|
25
|
+
*/
|
|
26
|
+
export type JsonSchema = Record<string, unknown>;
|
|
27
|
+
declare function objectSchema(props: Record<string, JsonSchema>, desc?: string): JsonSchema;
|
|
28
|
+
declare function arraySchema(items: JsonSchema, desc?: string): JsonSchema;
|
|
29
|
+
/**
|
|
30
|
+
* Marks a property optional. It also reads as a no-op — the schema itself is
|
|
31
|
+
* unchanged, so an optional property stays a perfectly normal schema.
|
|
32
|
+
*/
|
|
33
|
+
declare function optional<T extends JsonSchema>(schema: T): T;
|
|
34
|
+
export declare const s: {
|
|
35
|
+
object: typeof objectSchema;
|
|
36
|
+
array: typeof arraySchema;
|
|
37
|
+
string: (desc?: string) => JsonSchema;
|
|
38
|
+
number: (desc?: string) => JsonSchema;
|
|
39
|
+
integer: (desc?: string) => JsonSchema;
|
|
40
|
+
boolean: (desc?: string) => JsonSchema;
|
|
41
|
+
/** A string restricted to a fixed set of values. */
|
|
42
|
+
enumOf: (values: readonly string[], desc?: string) => JsonSchema;
|
|
43
|
+
/** Anything (an object with no declared properties, a free-form value). */
|
|
44
|
+
any: (desc?: string) => JsonSchema;
|
|
45
|
+
optional: typeof optional;
|
|
46
|
+
};
|
|
47
|
+
/** True when `schema` was marked optional by `s.optional`. */
|
|
48
|
+
export declare function isOptionalSchema(schema: JsonSchema): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Normalizes a declared schema to plain JSON Schema.
|
|
51
|
+
*
|
|
52
|
+
* A JSON Schema object passes through untouched, and so does anything that can
|
|
53
|
+
* produce one — zod v4 (`z.object({…})` has `.toJSONSchema()`), arktype
|
|
54
|
+
* (`.toJsonSchema()`), or a wrapper you wrote. That is why the SDK depends on no
|
|
55
|
+
* schema library: bring whichever you already use, and the wire form stays a
|
|
56
|
+
* plain JSON Schema either way.
|
|
57
|
+
*
|
|
58
|
+
* Throws when the value is neither, naming what was expected — silently
|
|
59
|
+
* announcing a non-schema would poison every peer's tool list.
|
|
60
|
+
*/
|
|
61
|
+
export declare function toJsonSchema(value: unknown, what: string): JsonSchema;
|
|
62
|
+
export {};
|
|
63
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAUjD,iBAAS,YAAY,CACnB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,EACjC,IAAI,CAAC,EAAE,MAAM,GACZ,UAAU,CAUZ;AAED,iBAAS,WAAW,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,UAAU,CAEjE;AASD;;;GAGG;AACH,iBAAS,QAAQ,CAAC,CAAC,SAAS,UAAU,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAGpD;AAED,eAAO,MAAM,CAAC;;;oBAGI,MAAM;oBACN,MAAM;qBACL,MAAM;qBACN,MAAM;IACvB,oDAAoD;qBACnC,SAAS,MAAM,EAAE,SAAS,MAAM,KAAG,UAAU;IAE9D,2EAA2E;iBAC9D,MAAM,KAAG,UAAU;;CAEjC,CAAC;AAEF,8DAA8D;AAC9D,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAE5D;AAgCD;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,CAiBrE"}
|