@gutilab/sdk 0.0.0-stage → 0.10.0-rc.1
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/LICENSE +21 -0
- package/README.md +678 -2
- package/dist/index.d.ts +414 -0
- package/dist/index.js +148 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +292 -0
- package/dist/run-agent.js +1532 -0
- package/package.json +26 -3
- package/template/gitignore +1 -0
- package/template/package.json +19 -0
- package/template/src/main.ts.txt +14 -0
- package/template/src/nodes.ts.txt +21 -0
- package/template/tsconfig.json +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gutilab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,679 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @gutilab/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The SDK to write the agents of an orchestrator, in TypeScript. You define nodes, with their inputs, outputs and
|
|
4
|
+
parameters as Zod schemas; the SDK declares them, keeps the agent connected, and runs the Steps the
|
|
5
|
+
orchestrator assigns to it.
|
|
6
|
+
|
|
7
|
+
Install it in the project of the agent, together with `zod` 4, at the version of your orchestrator. It runs
|
|
8
|
+
under Node 24 or 26, from 24.11.0, where Node starts an agent written in TypeScript without a build step.
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install @gutilab/sdk@<version of your orchestrator> zod
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
The API has two functions and one error class: `defineNode` defines a node, `runAgent` runs the agent, and
|
|
15
|
+
`AgentError` carries each refusal with its code. An agent is a list of nodes, in `src/nodes.ts`:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { defineNode } from '@gutilab/sdk';
|
|
19
|
+
import { z } from 'zod';
|
|
20
|
+
|
|
21
|
+
export const ocr = defineNode({
|
|
22
|
+
identifier: 'acme.ocr',
|
|
23
|
+
version: '1.0.0',
|
|
24
|
+
inputs: { file: z.string() },
|
|
25
|
+
outputs: { text: z.string(), lang: z.string().optional() },
|
|
26
|
+
parameters: { lang: z.enum(['fr', 'en']).default('fr') },
|
|
27
|
+
async run({ inputs, parameters, signal, log }) {
|
|
28
|
+
log.info(`Reading ${inputs.file} in ${parameters.lang}`);
|
|
29
|
+
const response = await fetch(inputs.file, { signal });
|
|
30
|
+
if (!response.ok) {
|
|
31
|
+
throw new Error(`Cannot read ${inputs.file}: HTTP ${response.status}`);
|
|
32
|
+
}
|
|
33
|
+
return { text: await response.text(), lang: parameters.lang };
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// The nodes of this agent: `gutilab-sdk dev` and `gutilab-sdk validate` read this list, and `main.ts` runs it.
|
|
38
|
+
export const nodes = [ocr];
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
and the file that runs them, `src/main.ts`, started by `node src/main.ts`:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { runAgent } from '@gutilab/sdk';
|
|
45
|
+
import { nodes } from './nodes.ts';
|
|
46
|
+
|
|
47
|
+
const stop = new AbortController();
|
|
48
|
+
process.once('SIGTERM', () => {
|
|
49
|
+
stop.abort();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
await runAgent({
|
|
53
|
+
url: process.env.ORCHESTRATOR_URL ?? '',
|
|
54
|
+
auth: { kind: 'agent', token: process.env.AGENT_TOKEN ?? '' },
|
|
55
|
+
nodes,
|
|
56
|
+
signal: stop.signal,
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The command of the SDK writes these two files in a new project, then runs its nodes as a development agent,
|
|
61
|
+
which declares them again each time a file changes. It reads a developer token from `DEVELOPER_TOKEN`:
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
npx @gutilab/sdk@<version of your orchestrator> init my-agent
|
|
65
|
+
cd my-agent
|
|
66
|
+
npm install
|
|
67
|
+
export DEVELOPER_TOKEN=<your developer token>
|
|
68
|
+
npx --no gutilab-sdk dev --url wss://<host>/agents --environment <id of a development environment>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
In that project, `npx --no gutilab-sdk validate` checks the node definitions without connecting. `--no` keeps npx
|
|
72
|
+
from downloading anything: outside a project where the SDK is installed, the command stops instead.
|
|
73
|
+
|
|
74
|
+
## Classes
|
|
75
|
+
|
|
76
|
+
### AgentError
|
|
77
|
+
|
|
78
|
+
A refusal by the SDK or by the orchestrator, or the reason why a Step was interrupted (`signal.reason`). Its
|
|
79
|
+
`code`, stable and in snake_case, says which one; `details`, when present, gives more information: the issues
|
|
80
|
+
of a refused node definition, for example.
|
|
81
|
+
|
|
82
|
+
#### Example
|
|
83
|
+
|
|
84
|
+
```ts @import.meta.vitest
|
|
85
|
+
const error = new AgentError('invalid_token', 'The token is unknown, revoked or expired.');
|
|
86
|
+
expect(error.code).toBe('invalid_token');
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### Extends
|
|
90
|
+
|
|
91
|
+
- `Error`
|
|
92
|
+
|
|
93
|
+
#### Constructors
|
|
94
|
+
|
|
95
|
+
##### Constructor
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
new AgentError(
|
|
99
|
+
code,
|
|
100
|
+
message,
|
|
101
|
+
details?
|
|
102
|
+
): AgentError;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
###### Parameters
|
|
106
|
+
|
|
107
|
+
| Parameter | Type | Description |
|
|
108
|
+
| ------ | ------ | ------ |
|
|
109
|
+
| `code` | `string` | The stable code of the refusal, in snake_case. |
|
|
110
|
+
| `message` | `string` | What a person reads: one line per issue, for a refused definition. |
|
|
111
|
+
| `details?` | `unknown` | More information about the refusal, when there is some. |
|
|
112
|
+
|
|
113
|
+
###### Returns
|
|
114
|
+
|
|
115
|
+
[`AgentError`](#agenterror)
|
|
116
|
+
|
|
117
|
+
###### Overrides
|
|
118
|
+
|
|
119
|
+
`Error.constructor`
|
|
120
|
+
|
|
121
|
+
#### Properties
|
|
122
|
+
|
|
123
|
+
##### code
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
readonly code: string;
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The stable code of the refusal, in snake_case: `invalid_token` or `invalid_node_definition`, for example.
|
|
130
|
+
|
|
131
|
+
##### details?
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
readonly details?: unknown;
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
More information about the refusal, when there is some: the issues of a refused node definition, for example.
|
|
138
|
+
|
|
139
|
+
## Type Aliases
|
|
140
|
+
|
|
141
|
+
### AgentEvent
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
type AgentEvent =
|
|
145
|
+
| {
|
|
146
|
+
type: "connected";
|
|
147
|
+
connectionId: string;
|
|
148
|
+
resumed: boolean;
|
|
149
|
+
}
|
|
150
|
+
| {
|
|
151
|
+
type: "disconnected";
|
|
152
|
+
code: number;
|
|
153
|
+
reason: string;
|
|
154
|
+
retryInMs: number;
|
|
155
|
+
}
|
|
156
|
+
| {
|
|
157
|
+
type: "draining";
|
|
158
|
+
runningSteps: number;
|
|
159
|
+
};
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
A change of state of the connection, that `onEvent` receives.
|
|
163
|
+
|
|
164
|
+
#### Union Members
|
|
165
|
+
|
|
166
|
+
##### Type Literal
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
{
|
|
170
|
+
type: "connected";
|
|
171
|
+
connectionId: string;
|
|
172
|
+
resumed: boolean;
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
| Name | Type | Description |
|
|
177
|
+
| ------ | ------ | ------ |
|
|
178
|
+
| `type` | `"connected"` | The orchestrator accepted the agent: its connection is ready. |
|
|
179
|
+
| `connectionId` | `string` | The id of the connection on the orchestrator. |
|
|
180
|
+
| `resumed` | `boolean` | True when the agent resumed its connection after a cut. |
|
|
181
|
+
|
|
182
|
+
***
|
|
183
|
+
|
|
184
|
+
##### Type Literal
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
{
|
|
188
|
+
type: "disconnected";
|
|
189
|
+
code: number;
|
|
190
|
+
reason: string;
|
|
191
|
+
retryInMs: number;
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
| Name | Type | Description |
|
|
196
|
+
| ------ | ------ | ------ |
|
|
197
|
+
| `type` | `"disconnected"` | The socket of the agent closed, and the SDK will retry. |
|
|
198
|
+
| `code` | `number` | The close code of the socket: 1006 for an unreachable orchestrator or a network cut. |
|
|
199
|
+
| `reason` | `string` | The close reason of the socket, empty for a network cut. |
|
|
200
|
+
| `retryInMs` | `number` | The delay before the next attempt, in milliseconds. |
|
|
201
|
+
|
|
202
|
+
***
|
|
203
|
+
|
|
204
|
+
##### Type Literal
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
{
|
|
208
|
+
type: "draining";
|
|
209
|
+
runningSteps: number;
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
| Name | Type | Description |
|
|
214
|
+
| ------ | ------ | ------ |
|
|
215
|
+
| `type` | `"draining"` | The graceful shutdown starts. |
|
|
216
|
+
| `runningSteps` | `number` | How many Steps the agent still runs. |
|
|
217
|
+
|
|
218
|
+
***
|
|
219
|
+
|
|
220
|
+
### AgentOptions
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
type AgentOptions = object;
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
The options of `runAgent`.
|
|
227
|
+
|
|
228
|
+
#### Properties
|
|
229
|
+
|
|
230
|
+
##### url
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
url: string;
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
The address of the orchestrator, `wss://<host>/agents`: a `ws:` or `wss:` URL, without a fragment.
|
|
237
|
+
|
|
238
|
+
##### auth
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
auth: Auth;
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
The token of the agent: `{ kind: 'agent', token }`, an agent token, or
|
|
245
|
+
`{ kind: 'developer', token, environmentId }`, a developer token and the id of its development environment.
|
|
246
|
+
|
|
247
|
+
##### nodes
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
nodes: readonly AnyNode[];
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
The nodes returned by `defineNode`.
|
|
254
|
+
|
|
255
|
+
##### slots?
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
slots?: number;
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
How many Steps run at the same time, from 1 to 256; 1 by default.
|
|
262
|
+
|
|
263
|
+
##### signal?
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
signal?: AbortSignal;
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Requests the graceful shutdown: the agent takes no more Step, finishes those it runs, then leaves.
|
|
270
|
+
|
|
271
|
+
##### onEvent?
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
onEvent?: (event) => void;
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Receives the changes of state of the connection; without it, each one is written to standard error.
|
|
278
|
+
|
|
279
|
+
###### Parameters
|
|
280
|
+
|
|
281
|
+
| Parameter | Type |
|
|
282
|
+
| ------ | ------ |
|
|
283
|
+
| `event` | [`AgentEvent`](#agentevent) |
|
|
284
|
+
|
|
285
|
+
###### Returns
|
|
286
|
+
|
|
287
|
+
`void`
|
|
288
|
+
|
|
289
|
+
***
|
|
290
|
+
|
|
291
|
+
### Schemas
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
type Schemas = Readonly<Record<string, z.ZodType>>;
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
Zod schemas, one key per port or per parameter.
|
|
298
|
+
|
|
299
|
+
***
|
|
300
|
+
|
|
301
|
+
### NoSchema
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
type NoSchema = Record<string, never>;
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
No schema: what `inputs`, `outputs` or `parameters` is when the definition leaves it out.
|
|
308
|
+
|
|
309
|
+
***
|
|
310
|
+
|
|
311
|
+
### Awaitable
|
|
312
|
+
|
|
313
|
+
```ts
|
|
314
|
+
type Awaitable<Value> = Value | Promise<Value>;
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
A value, directly or as a promise.
|
|
318
|
+
|
|
319
|
+
#### Type Parameters
|
|
320
|
+
|
|
321
|
+
| Type Parameter |
|
|
322
|
+
| ------ |
|
|
323
|
+
| <a id="type-parameter-value"></a> `Value` |
|
|
324
|
+
|
|
325
|
+
***
|
|
326
|
+
|
|
327
|
+
### RunResult
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
type RunResult<Outputs> = NoSchema extends z.input<z.ZodObject<Outputs>> ?
|
|
331
|
+
| Awaitable<z.input<z.ZodObject<Outputs>>>
|
|
332
|
+
| Awaitable<void> : Awaitable<z.input<z.ZodObject<Outputs>>>;
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
What `run` returns: its outputs, as the input of their schemas, directly or as a promise; or nothing, when no
|
|
336
|
+
output is required.
|
|
337
|
+
|
|
338
|
+
#### Type Parameters
|
|
339
|
+
|
|
340
|
+
| Type Parameter |
|
|
341
|
+
| ------ |
|
|
342
|
+
| <a id="type-parameter-outputs"></a> `Outputs` *extends* [`Schemas`](#schemas) |
|
|
343
|
+
|
|
344
|
+
***
|
|
345
|
+
|
|
346
|
+
### StepContext
|
|
347
|
+
|
|
348
|
+
```ts
|
|
349
|
+
type StepContext<Inputs, Parameters> = object;
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
What the function of a node receives for one Step.
|
|
353
|
+
|
|
354
|
+
#### Type Parameters
|
|
355
|
+
|
|
356
|
+
| Type Parameter |
|
|
357
|
+
| ------ |
|
|
358
|
+
| <a id="type-parameter-inputs"></a> `Inputs` *extends* [`Schemas`](#schemas) |
|
|
359
|
+
| <a id="type-parameter-parameters"></a> `Parameters` *extends* [`Schemas`](#schemas) |
|
|
360
|
+
|
|
361
|
+
#### Properties
|
|
362
|
+
|
|
363
|
+
##### inputs
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
inputs: z.output<z.ZodObject<Inputs>>;
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
One key per input that has a value. For a Step, the SDK validates them with their schemas first.
|
|
370
|
+
|
|
371
|
+
##### parameters
|
|
372
|
+
|
|
373
|
+
```ts
|
|
374
|
+
parameters: z.output<z.ZodObject<Parameters>>;
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
The parameters of the node. For a Step, the SDK validates them with their schemas and fills in each `.default()` first.
|
|
378
|
+
|
|
379
|
+
##### signal
|
|
380
|
+
|
|
381
|
+
```ts
|
|
382
|
+
signal: AbortSignal;
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
Aborted when the result of the Step is no longer awaited: a cancellation, its timeout, or a connection the
|
|
386
|
+
orchestrator closed for good or let expire, for example. Its `reason` is an `AgentError` whose `code` says
|
|
387
|
+
why. Pass it to what the function awaits.
|
|
388
|
+
|
|
389
|
+
##### log
|
|
390
|
+
|
|
391
|
+
```ts
|
|
392
|
+
log: object;
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Writes one log line of the Step, shown with its Execution. The call is synchronous and never throws.
|
|
396
|
+
|
|
397
|
+
###### info()
|
|
398
|
+
|
|
399
|
+
```ts
|
|
400
|
+
info(message): void;
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
Writes a line at the `info` level.
|
|
404
|
+
|
|
405
|
+
###### Parameters
|
|
406
|
+
|
|
407
|
+
| Parameter | Type |
|
|
408
|
+
| ------ | ------ |
|
|
409
|
+
| `message` | `string` |
|
|
410
|
+
|
|
411
|
+
###### Returns
|
|
412
|
+
|
|
413
|
+
`void`
|
|
414
|
+
|
|
415
|
+
###### warn()
|
|
416
|
+
|
|
417
|
+
```ts
|
|
418
|
+
warn(message): void;
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
Writes a line at the `warn` level.
|
|
422
|
+
|
|
423
|
+
###### Parameters
|
|
424
|
+
|
|
425
|
+
| Parameter | Type |
|
|
426
|
+
| ------ | ------ |
|
|
427
|
+
| `message` | `string` |
|
|
428
|
+
|
|
429
|
+
###### Returns
|
|
430
|
+
|
|
431
|
+
`void`
|
|
432
|
+
|
|
433
|
+
###### error()
|
|
434
|
+
|
|
435
|
+
```ts
|
|
436
|
+
error(message): void;
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
Writes a line at the `error` level.
|
|
440
|
+
|
|
441
|
+
###### Parameters
|
|
442
|
+
|
|
443
|
+
| Parameter | Type |
|
|
444
|
+
| ------ | ------ |
|
|
445
|
+
| `message` | `string` |
|
|
446
|
+
|
|
447
|
+
###### Returns
|
|
448
|
+
|
|
449
|
+
`void`
|
|
450
|
+
|
|
451
|
+
##### executionId
|
|
452
|
+
|
|
453
|
+
```ts
|
|
454
|
+
executionId: string;
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
The id of the Execution of the Step: write it in the logs of the agent, to find the Execution in the web interface.
|
|
458
|
+
|
|
459
|
+
##### stepId
|
|
460
|
+
|
|
461
|
+
```ts
|
|
462
|
+
stepId: string;
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
The id of the Step: an idempotency key for an external call.
|
|
466
|
+
|
|
467
|
+
***
|
|
468
|
+
|
|
469
|
+
### NodeDefinition
|
|
470
|
+
|
|
471
|
+
```ts
|
|
472
|
+
type NodeDefinition<Inputs, Outputs, Parameters> = object;
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
The definition of a node: what the developer writes, and gives to `defineNode`.
|
|
476
|
+
|
|
477
|
+
#### Type Parameters
|
|
478
|
+
|
|
479
|
+
| Type Parameter |
|
|
480
|
+
| ------ |
|
|
481
|
+
| <a id="type-parameter-inputs-1"></a> `Inputs` *extends* [`Schemas`](#schemas) |
|
|
482
|
+
| <a id="type-parameter-outputs-1"></a> `Outputs` *extends* [`Schemas`](#schemas) |
|
|
483
|
+
| <a id="type-parameter-parameters-1"></a> `Parameters` *extends* [`Schemas`](#schemas) |
|
|
484
|
+
|
|
485
|
+
#### Properties
|
|
486
|
+
|
|
487
|
+
##### identifier
|
|
488
|
+
|
|
489
|
+
```ts
|
|
490
|
+
identifier: string;
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
The namespaced identifier of the node type, `acme.ocr` for example.
|
|
494
|
+
|
|
495
|
+
##### version
|
|
496
|
+
|
|
497
|
+
```ts
|
|
498
|
+
version: string;
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
Its semver version, `1.0.0` for example.
|
|
502
|
+
|
|
503
|
+
##### timeoutMs?
|
|
504
|
+
|
|
505
|
+
```ts
|
|
506
|
+
timeoutMs?: number;
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
The timeout of a Step, in milliseconds: from 600,000, the default, 10 minutes, to 21,600,000, 6 hours.
|
|
510
|
+
|
|
511
|
+
##### inputs?
|
|
512
|
+
|
|
513
|
+
```ts
|
|
514
|
+
inputs?: Inputs;
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
One Zod schema per data input: its key is the name of the port. A port is `z.string()`, `z.int()`,
|
|
518
|
+
`z.number()`, `z.boolean()`, `z.enum([...])`, `z.unknown()` or `z.array()` of one of them, plus `.optional()`.
|
|
519
|
+
|
|
520
|
+
##### outputs?
|
|
521
|
+
|
|
522
|
+
```ts
|
|
523
|
+
outputs?: Outputs;
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
One Zod schema per data output, written as an input is.
|
|
527
|
+
|
|
528
|
+
##### parameters?
|
|
529
|
+
|
|
530
|
+
```ts
|
|
531
|
+
parameters?: Parameters;
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
One Zod schema per parameter, set in the editor: a string, an integer, a number, a boolean or an array of one
|
|
535
|
+
of them, with its bounds, `z.enum([...])`, `.optional()`, `.default()`, `.describe()` and `.meta({ title })`.
|
|
536
|
+
|
|
537
|
+
#### Methods
|
|
538
|
+
|
|
539
|
+
##### run()
|
|
540
|
+
|
|
541
|
+
```ts
|
|
542
|
+
run(context): RunResult<NoInfer<Outputs>>;
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
The function of the node, called for each Step. An exception fails the Step, with its message.
|
|
546
|
+
|
|
547
|
+
###### Parameters
|
|
548
|
+
|
|
549
|
+
| Parameter | Type |
|
|
550
|
+
| ------ | ------ |
|
|
551
|
+
| `context` | [`StepContext`](#stepcontext)\<`Inputs`, `Parameters`\> |
|
|
552
|
+
|
|
553
|
+
###### Returns
|
|
554
|
+
|
|
555
|
+
[`RunResult`](#runresult)\<`NoInfer`\<`Outputs`\>\>
|
|
556
|
+
|
|
557
|
+
***
|
|
558
|
+
|
|
559
|
+
### DefinedNode
|
|
560
|
+
|
|
561
|
+
```ts
|
|
562
|
+
type DefinedNode<Inputs, Outputs, Parameters> = object;
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
A defined node: what `defineNode` returns, and `runAgent` takes.
|
|
566
|
+
|
|
567
|
+
#### Type Parameters
|
|
568
|
+
|
|
569
|
+
| Type Parameter |
|
|
570
|
+
| ------ |
|
|
571
|
+
| <a id="type-parameter-inputs-2"></a> `Inputs` *extends* [`Schemas`](#schemas) |
|
|
572
|
+
| <a id="type-parameter-outputs-2"></a> `Outputs` *extends* [`Schemas`](#schemas) |
|
|
573
|
+
| <a id="type-parameter-parameters-2"></a> `Parameters` *extends* [`Schemas`](#schemas) |
|
|
574
|
+
|
|
575
|
+
#### Properties
|
|
576
|
+
|
|
577
|
+
##### nodeType
|
|
578
|
+
|
|
579
|
+
```ts
|
|
580
|
+
nodeType: NodeType;
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
The node type the agent will declare, as the orchestrator receives it:
|
|
584
|
+
`{ identifier, version, timeoutMs, inputs, outputs, parameterSchema }`. `inputs` and `outputs` list the data
|
|
585
|
+
ports, each `{ name, type, required }`, and `parameterSchema` is the JSON Schema of the parameters.
|
|
586
|
+
|
|
587
|
+
##### definition
|
|
588
|
+
|
|
589
|
+
```ts
|
|
590
|
+
definition: NodeDefinition<Inputs, Outputs, Parameters>;
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
The definition as it was written, with its types: call `definition.run` to test the node alone. Called this way, the function receives what the test gives it, neither validated nor filled in.
|
|
594
|
+
|
|
595
|
+
***
|
|
596
|
+
|
|
597
|
+
### AnyNode
|
|
598
|
+
|
|
599
|
+
```ts
|
|
600
|
+
type AnyNode = DefinedNode<Schemas, Schemas, Schemas>;
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
A node of any definition: what `runAgent` takes in `nodes`.
|
|
604
|
+
|
|
605
|
+
## Functions
|
|
606
|
+
|
|
607
|
+
### defineNode()
|
|
608
|
+
|
|
609
|
+
```ts
|
|
610
|
+
function defineNode<Inputs, Outputs, Parameters>(definition): DefinedNode<Inputs, Outputs, Parameters>;
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
Defines a node, to give to `runAgent`: the node type the agent will declare, read from the Zod schemas of the
|
|
614
|
+
definition, and the definition itself. The definition is validated at once, with the schema the orchestrator
|
|
615
|
+
applies to a declared node type: refused, it throws an `AgentError` of code `invalid_node_definition`, whose
|
|
616
|
+
message gives one line per issue, naming the node, the field and the rule, and whose `details.issues` lists
|
|
617
|
+
them, each `{ path, message }`.
|
|
618
|
+
|
|
619
|
+
#### Type Parameters
|
|
620
|
+
|
|
621
|
+
| Type Parameter | Default type |
|
|
622
|
+
| ------ | ------ |
|
|
623
|
+
| <a id="type-parameter-inputs-3"></a> `Inputs` *extends* `Readonly`\<`Record`\<`string`, `ZodType`\<`unknown`, `unknown`, `$ZodTypeInternals`\<`unknown`, `unknown`\>\>\>\> | [`NoSchema`](#noschema) |
|
|
624
|
+
| <a id="type-parameter-outputs-3"></a> `Outputs` *extends* `Readonly`\<`Record`\<`string`, `ZodType`\<`unknown`, `unknown`, `$ZodTypeInternals`\<`unknown`, `unknown`\>\>\>\> | [`NoSchema`](#noschema) |
|
|
625
|
+
| <a id="type-parameter-parameters-3"></a> `Parameters` *extends* `Readonly`\<`Record`\<`string`, `ZodType`\<`unknown`, `unknown`, `$ZodTypeInternals`\<`unknown`, `unknown`\>\>\>\> | [`NoSchema`](#noschema) |
|
|
626
|
+
|
|
627
|
+
#### Parameters
|
|
628
|
+
|
|
629
|
+
| Parameter | Type |
|
|
630
|
+
| ------ | ------ |
|
|
631
|
+
| `definition` | [`NodeDefinition`](#nodedefinition)\<`Inputs`, `Outputs`, `Parameters`\> |
|
|
632
|
+
|
|
633
|
+
#### Returns
|
|
634
|
+
|
|
635
|
+
[`DefinedNode`](#definednode)\<`Inputs`, `Outputs`, `Parameters`\>
|
|
636
|
+
|
|
637
|
+
#### Example
|
|
638
|
+
|
|
639
|
+
```ts @import.meta.vitest
|
|
640
|
+
const upper = defineNode({
|
|
641
|
+
identifier: 'acme.upper',
|
|
642
|
+
version: '1.0.0',
|
|
643
|
+
inputs: { text: z.string() },
|
|
644
|
+
outputs: { upper: z.string() },
|
|
645
|
+
run: ({ inputs }) => ({ upper: inputs.text.toUpperCase() }),
|
|
646
|
+
});
|
|
647
|
+
expect(upper.nodeType.inputs).toStrictEqual([{ name: 'text', type: { name: 'string' }, required: true }]);
|
|
648
|
+
expect(() => defineNode({ ...upper.definition, inputs: { text: z.email() } })).toThrow('acme.upper@1.0.0 inputs.text: a port is');
|
|
649
|
+
```
|
|
650
|
+
|
|
651
|
+
***
|
|
652
|
+
|
|
653
|
+
### runAgent()
|
|
654
|
+
|
|
655
|
+
```ts
|
|
656
|
+
function runAgent(options): Promise<void>;
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
Runs an agent: it connects to the orchestrator, declares its nodes and its slots, runs the Steps it is
|
|
660
|
+
assigned and keeps its connection through network cuts. The promise lasts as long as the agent: it resolves
|
|
661
|
+
after a graceful shutdown, requested by `signal`, and rejects with an `AgentError` when an option is refused,
|
|
662
|
+
before any connection, or when the orchestrator refuses the agent for good, an unknown token for example.
|
|
663
|
+
|
|
664
|
+
#### Parameters
|
|
665
|
+
|
|
666
|
+
| Parameter | Type |
|
|
667
|
+
| ------ | ------ |
|
|
668
|
+
| `options` | [`AgentOptions`](#agentoptions) |
|
|
669
|
+
|
|
670
|
+
#### Returns
|
|
671
|
+
|
|
672
|
+
`Promise`\<`void`\>
|
|
673
|
+
|
|
674
|
+
#### Example
|
|
675
|
+
|
|
676
|
+
```ts @import.meta.vitest
|
|
677
|
+
const refused = runAgent({ url: 'https://orchestrator.example.com', auth: { kind: 'agent', token: 'token_agent_x' }, nodes: [] });
|
|
678
|
+
await expect(refused).rejects.toMatchObject({ code: 'invalid_agent_options', message: 'url: Expected a ws:// or wss:// URL, without a fragment' });
|
|
679
|
+
```
|