@niq.run/worker-sdk 0.4.0 → 0.6.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 +107 -3
- package/dist/baseworker.d.ts +50 -3
- package/dist/baseworker.d.ts.map +1 -1
- package/dist/baseworker.js +98 -12
- package/dist/baseworker.js.map +1 -1
- package/dist/index.d.ts +3 -1
- 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 +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 +2 -2
- 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/cli.test.d.ts +0 -2
- package/dist/cli.test.d.ts.map +0 -1
- package/dist/cli.test.js +0 -59
- package/dist/cli.test.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/README.md
CHANGED
|
@@ -58,12 +58,114 @@ 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
|
|
64
165
|
on top of a `WorkerSideChannel`: an extension registry (what the worker
|
|
65
166
|
responds to and how), tool-request reply plumbing, and the `worker.ready`
|
|
66
|
-
presence announcement.
|
|
167
|
+
presence announcement. Presence is transient (live-only, never persisted), and
|
|
168
|
+
a `worker.discover` is answered automatically with a directed `worker.ready`.
|
|
67
169
|
|
|
68
170
|
```ts
|
|
69
171
|
import { BaseWorker, HTTPWorkerClient, parseToolCall, EventType } from "@niq.run/worker-sdk";
|
|
@@ -84,12 +186,14 @@ worker.register(
|
|
|
84
186
|
{ event: "tool.invoke", keyField: "name", key: "echo", description: "Echo a message" },
|
|
85
187
|
(evt) => {
|
|
86
188
|
const call = parseToolCall(evt);
|
|
87
|
-
worker.replyCompleted(call.callerID, call.callID,
|
|
189
|
+
worker.replyCompleted(call.callerID, call.callID, `echo: ${call.args["text"] ?? ""}`, call.traceID);
|
|
88
190
|
},
|
|
89
191
|
);
|
|
90
192
|
|
|
91
193
|
await worker.channel.connect();
|
|
92
|
-
|
|
194
|
+
// Broadcast worker.ready (transient, self excluded) and remember the contract,
|
|
195
|
+
// so a later worker.discover is answered directly — no extra code needed here.
|
|
196
|
+
await worker.announceReady("echo");
|
|
93
197
|
|
|
94
198
|
for await (const evt of worker.channel.events()) {
|
|
95
199
|
if (!worker.dispatchExtension(evt)) {
|
package/dist/baseworker.d.ts
CHANGED
|
@@ -15,17 +15,33 @@ export declare class BaseWorker {
|
|
|
15
15
|
readonly subscriptions: EventPattern[];
|
|
16
16
|
readonly channel: WorkerSideChannel;
|
|
17
17
|
private readonly regs;
|
|
18
|
+
/** The contract last announced, so a `worker.discover` can be answered. */
|
|
19
|
+
private announced;
|
|
18
20
|
constructor(opts: {
|
|
19
21
|
id: string;
|
|
20
22
|
subscriptions?: EventPattern[];
|
|
21
23
|
channel: WorkerSideChannel;
|
|
22
24
|
});
|
|
23
|
-
/**
|
|
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
|
+
*/
|
|
24
33
|
register(ext: Extension, handler: ExtensionHandler): void;
|
|
25
34
|
/**
|
|
26
35
|
* Route an event to the registered extension matching its event type.
|
|
27
36
|
* Returns whether a handler ran; the first matching registration wins. Async
|
|
28
37
|
* handlers are not awaited.
|
|
38
|
+
*
|
|
39
|
+
* `worker.discover` is protocol traffic rather than an extension: it is
|
|
40
|
+
* answered here with the contract last passed to {@link announceReady} (a
|
|
41
|
+
* directed, transient `worker.ready` to the asker), mirroring Go's
|
|
42
|
+
* AnnounceReadyTo. Presence is live-only and never persisted, so a peer that
|
|
43
|
+
* starts later — or a UI that cannot read presence from history — learns the
|
|
44
|
+
* worker's tools by asking.
|
|
29
45
|
*/
|
|
30
46
|
dispatchExtension(evt: Event): boolean;
|
|
31
47
|
/** Snapshot of the registered extensions (insertion order). */
|
|
@@ -41,10 +57,41 @@ export declare class BaseWorker {
|
|
|
41
57
|
* contract (SelfOnly left out), self excluded via `exclude_worker_id`.
|
|
42
58
|
* `publishes` is the wire form of the events this worker declares it emits
|
|
43
59
|
* (`worker.ready` "publishes").
|
|
60
|
+
*
|
|
61
|
+
* The contract is remembered, so {@link dispatchExtension} can answer a
|
|
62
|
+
* `worker.discover` with it. The event is transient: presence is delivered
|
|
63
|
+
* live and never written to the durable event log (mirroring Go).
|
|
44
64
|
*/
|
|
45
65
|
announceReady(workerType: string, publishes?: Record<string, unknown>[]): Promise<void>;
|
|
46
|
-
/**
|
|
47
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Send this worker's presence directly to one target instead of broadcasting
|
|
68
|
+
* it — the answer to a `worker.discover`, and the direct equivalent of Go's
|
|
69
|
+
* AnnounceReadyTo. Omitted arguments reuse the contract last announced with
|
|
70
|
+
* {@link announceReady}; with no contract announced yet, nothing is sent.
|
|
71
|
+
*/
|
|
72
|
+
announceReadyTo(target: string, workerType?: string, publishes?: Record<string, unknown>[]): Promise<void>;
|
|
73
|
+
/** Render one `worker.ready` event from a declared contract. */
|
|
74
|
+
private presenceEvent;
|
|
75
|
+
/**
|
|
76
|
+
* Answer a request caller with a request.completed result.
|
|
77
|
+
*
|
|
78
|
+
* A convenience, not the protocol: the bus only requires the payload to be a
|
|
79
|
+
* JSON object, and a worker may build one by hand. What this does:
|
|
80
|
+
*
|
|
81
|
+
* - an OBJECT reply is the payload — no wrapper, so the event log shows the
|
|
82
|
+
* reply itself;
|
|
83
|
+
* - anything else (a string, a number, an array) cannot be a payload on its
|
|
84
|
+
* own, so it is wrapped as `{ result: value }`.
|
|
85
|
+
*
|
|
86
|
+
* `result` and `content` are therefore reserved top-level keys of a reply
|
|
87
|
+
* payload (the latter for multi-modal blocks). replyObject / replyString say
|
|
88
|
+
* which shape you mean.
|
|
89
|
+
*/
|
|
90
|
+
replyCompleted(callerID: string, callID: string, result: unknown, traceID?: string): Promise<void>;
|
|
91
|
+
/** Answer with a structured payload: the object IS the payload. */
|
|
92
|
+
replyObject(callerID: string, callID: string, payload: Record<string, unknown>, traceID?: string): Promise<void>;
|
|
93
|
+
/** Answer with text: the payload is `{ result: text }`. */
|
|
94
|
+
replyString(callerID: string, callID: string, text: string, traceID?: string): Promise<void>;
|
|
48
95
|
/** Answer a request caller with a request.failed error message. */
|
|
49
96
|
replyFailed(callerID: string, callID: string, error: string, traceID?: string): Promise<void>;
|
|
50
97
|
/**
|
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,E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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
|
|
@@ -16,21 +17,50 @@ export class BaseWorker {
|
|
|
16
17
|
subscriptions;
|
|
17
18
|
channel;
|
|
18
19
|
regs = new Map();
|
|
20
|
+
/** The contract last announced, so a `worker.discover` can be answered. */
|
|
21
|
+
announced = null;
|
|
19
22
|
constructor(opts) {
|
|
20
23
|
this.id = opts.id;
|
|
21
24
|
this.subscriptions = opts.subscriptions ?? [];
|
|
22
25
|
this.channel = opts.channel;
|
|
23
26
|
}
|
|
24
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Bind an extension to a handler. Safe at any time, including at runtime.
|
|
29
|
+
*
|
|
30
|
+
* Declared schemas are normalized here: `parameters` / `response` may be plain
|
|
31
|
+
* JSON Schema, or anything that converts to one (zod 4, arktype, …), so the
|
|
32
|
+
* announced contract — and therefore every peer's tool list — is always plain
|
|
33
|
+
* JSON Schema.
|
|
34
|
+
*/
|
|
25
35
|
register(ext, handler) {
|
|
26
|
-
|
|
36
|
+
const normalized = { ...ext };
|
|
37
|
+
if (ext.parameters !== undefined) {
|
|
38
|
+
normalized.parameters = toJsonSchema(ext.parameters, `parameters of ${ext.event}`);
|
|
39
|
+
}
|
|
40
|
+
if (ext.response !== undefined) {
|
|
41
|
+
normalized.response = toJsonSchema(ext.response, `response of ${ext.event}`);
|
|
42
|
+
}
|
|
43
|
+
this.regs.set(extensionKey(ext), { ext: normalized, handler });
|
|
27
44
|
}
|
|
28
45
|
/**
|
|
29
46
|
* Route an event to the registered extension matching its event type.
|
|
30
47
|
* Returns whether a handler ran; the first matching registration wins. Async
|
|
31
48
|
* handlers are not awaited.
|
|
49
|
+
*
|
|
50
|
+
* `worker.discover` is protocol traffic rather than an extension: it is
|
|
51
|
+
* answered here with the contract last passed to {@link announceReady} (a
|
|
52
|
+
* directed, transient `worker.ready` to the asker), mirroring Go's
|
|
53
|
+
* AnnounceReadyTo. Presence is live-only and never persisted, so a peer that
|
|
54
|
+
* starts later — or a UI that cannot read presence from history — learns the
|
|
55
|
+
* worker's tools by asking.
|
|
32
56
|
*/
|
|
33
57
|
dispatchExtension(evt) {
|
|
58
|
+
if (evt.type === EventType.WorkerDiscover) {
|
|
59
|
+
const asker = evt.worker_id;
|
|
60
|
+
if (asker && asker !== this.id)
|
|
61
|
+
void this.announceReadyTo(asker);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
34
64
|
for (const { ext, handler } of this.regs.values()) {
|
|
35
65
|
if (ext.event !== evt.type)
|
|
36
66
|
continue;
|
|
@@ -60,6 +90,9 @@ export class BaseWorker {
|
|
|
60
90
|
if (ext.parameters) {
|
|
61
91
|
entry["parameters"] = ext.parameters;
|
|
62
92
|
}
|
|
93
|
+
if (ext.response) {
|
|
94
|
+
entry["response"] = ext.response;
|
|
95
|
+
}
|
|
63
96
|
out.push(entry);
|
|
64
97
|
}
|
|
65
98
|
return out;
|
|
@@ -69,16 +102,14 @@ export class BaseWorker {
|
|
|
69
102
|
* contract (SelfOnly left out), self excluded via `exclude_worker_id`.
|
|
70
103
|
* `publishes` is the wire form of the events this worker declares it emits
|
|
71
104
|
* (`worker.ready` "publishes").
|
|
105
|
+
*
|
|
106
|
+
* The contract is remembered, so {@link dispatchExtension} can answer a
|
|
107
|
+
* `worker.discover` with it. The event is transient: presence is delivered
|
|
108
|
+
* live and never written to the durable event log (mirroring Go).
|
|
72
109
|
*/
|
|
73
110
|
async announceReady(workerType, publishes) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
watch: this.watchEntries(false),
|
|
77
|
-
};
|
|
78
|
-
if (publishes && publishes.length > 0) {
|
|
79
|
-
payload["publishes"] = publishes;
|
|
80
|
-
}
|
|
81
|
-
const presence = createEvent(EventType.WorkerReady, payload);
|
|
111
|
+
this.announced = { workerType, publishes };
|
|
112
|
+
const presence = this.presenceEvent(this.announced);
|
|
82
113
|
presence.exclude_worker_id = this.id;
|
|
83
114
|
try {
|
|
84
115
|
await this.channel.broadcast(presence);
|
|
@@ -87,15 +118,70 @@ export class BaseWorker {
|
|
|
87
118
|
// Presence announcements are best-effort, mirroring Go.
|
|
88
119
|
}
|
|
89
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Send this worker's presence directly to one target instead of broadcasting
|
|
123
|
+
* it — the answer to a `worker.discover`, and the direct equivalent of Go's
|
|
124
|
+
* AnnounceReadyTo. Omitted arguments reuse the contract last announced with
|
|
125
|
+
* {@link announceReady}; with no contract announced yet, nothing is sent.
|
|
126
|
+
*/
|
|
127
|
+
async announceReadyTo(target, workerType, publishes) {
|
|
128
|
+
const announced = workerType !== undefined
|
|
129
|
+
? { workerType, publishes }
|
|
130
|
+
: this.announced;
|
|
131
|
+
if (!announced)
|
|
132
|
+
return;
|
|
133
|
+
try {
|
|
134
|
+
await this.channel.send(this.presenceEvent(announced), target);
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
// Presence announcements are best-effort, mirroring Go.
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/** Render one `worker.ready` event from a declared contract. */
|
|
141
|
+
presenceEvent(announced) {
|
|
142
|
+
const payload = {
|
|
143
|
+
type: announced.workerType,
|
|
144
|
+
watch: this.watchEntries(false),
|
|
145
|
+
};
|
|
146
|
+
if (announced.publishes && announced.publishes.length > 0) {
|
|
147
|
+
payload["publishes"] = announced.publishes;
|
|
148
|
+
}
|
|
149
|
+
const presence = createEvent(EventType.WorkerReady, payload);
|
|
150
|
+
presence.transient = true; // presence: live-only, not durable history
|
|
151
|
+
return presence;
|
|
152
|
+
}
|
|
90
153
|
// ── Tool-serving helpers ──
|
|
91
154
|
//
|
|
92
155
|
// The repetitive parts every tool-serving worker repeats: replying to a
|
|
93
156
|
// tool-invocation with a request.completed / request.failed /
|
|
94
157
|
// request.rejected result, echoing the request's id and propagating the
|
|
95
158
|
// trace ID. Reply errors are ignored, mirroring Go.
|
|
96
|
-
/**
|
|
159
|
+
/**
|
|
160
|
+
* Answer a request caller with a request.completed result.
|
|
161
|
+
*
|
|
162
|
+
* A convenience, not the protocol: the bus only requires the payload to be a
|
|
163
|
+
* JSON object, and a worker may build one by hand. What this does:
|
|
164
|
+
*
|
|
165
|
+
* - an OBJECT reply is the payload — no wrapper, so the event log shows the
|
|
166
|
+
* reply itself;
|
|
167
|
+
* - anything else (a string, a number, an array) cannot be a payload on its
|
|
168
|
+
* own, so it is wrapped as `{ result: value }`.
|
|
169
|
+
*
|
|
170
|
+
* `result` and `content` are therefore reserved top-level keys of a reply
|
|
171
|
+
* payload (the latter for multi-modal blocks). replyObject / replyString say
|
|
172
|
+
* which shape you mean.
|
|
173
|
+
*/
|
|
97
174
|
async replyCompleted(callerID, callID, result, traceID) {
|
|
98
|
-
|
|
175
|
+
const payload = isRecord(result) ? result : { result };
|
|
176
|
+
await this.reply(EventType.RequestCompleted, callerID, callID, payload, traceID);
|
|
177
|
+
}
|
|
178
|
+
/** Answer with a structured payload: the object IS the payload. */
|
|
179
|
+
async replyObject(callerID, callID, payload, traceID) {
|
|
180
|
+
await this.replyCompleted(callerID, callID, payload, traceID);
|
|
181
|
+
}
|
|
182
|
+
/** Answer with text: the payload is `{ result: text }`. */
|
|
183
|
+
async replyString(callerID, callID, text, traceID) {
|
|
184
|
+
await this.replyCompleted(callerID, callID, text, traceID);
|
|
99
185
|
}
|
|
100
186
|
/** Answer a request caller with a request.failed error message. */
|
|
101
187
|
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,E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AAE,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;QAC9C,OAAO,EAAE,GAAG,CAAC,QAAQ,IAAI,EAAE;KAC5B,CAAC;AACJ,CAAC;AAED,mEAAmE;AACnE,MAAM,UAAU,SAAS,CAAC,IAA6B,EAAE,GAAW;IAClE,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;IACpB,OAAO,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACxC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,MAAM,CACpB,IAA6B,EAC7B,GAAW,EACX,GAAW;IAEX,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;IACpB,IAAI,OAAO,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/index.d.ts
CHANGED
|
@@ -1,11 +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";
|
|
4
6
|
export { parseCliArgs, busConnFromArgs, runWorkerApp, BUS_CLI_FLAGS } from "./cli.js";
|
|
5
7
|
export type { ParsedCliArgs } from "./cli.js";
|
|
6
8
|
export { createEvent } from "./createEvent.js";
|
|
7
9
|
export { BUS_ENV_VARS, readBusEnv, type BusEnv } from "./env.js";
|
|
8
10
|
export { newId } from "./id.js";
|
|
9
11
|
export { parseEventStream } from "./sse.js";
|
|
10
|
-
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";
|
|
11
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,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,OAA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rB,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 { replyValue, replyText } from "./reply.js";
|
|
4
|
+
export { s, isOptionalSchema } from "./schema.js";
|
|
3
5
|
export { parseCliArgs, busConnFromArgs, runWorkerApp, BUS_CLI_FLAGS } from "./cli.js";
|
|
4
6
|
export { createEvent } from "./createEvent.js";
|
|
5
7
|
export { BUS_ENV_VARS, readBusEnv } from "./env.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,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,
|
|
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"}
|