@cubicecho/agent-router-worker 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/_agents/docs/01-install.md +11 -4
- package/_agents/docs/custom-loop.md +14 -1
- package/chunks/{worker-DobLEExh.js → worker-Cm-rwbaS.js} +127 -86
- package/lib.js +1 -1
- package/package.json +1 -1
- package/types/server/runner/assignment.d.ts +4 -0
- package/types/server/runner/execute.d.ts +10 -1
- package/types/server/runner/protocol.d.ts +12 -0
- package/types/worker/config.d.ts +14 -0
- package/types/worker/lib.d.ts +2 -1
- package/types/worker/run.d.ts +2 -2
- package/types/worker/worker.d.ts +10 -2
- package/worker.js +1 -1
package/README.md
CHANGED
|
@@ -38,8 +38,8 @@ WORKER_CONFIG=./worker.json \
|
|
|
38
38
|
agent-router-worker
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
It prints the servers that started, and `connected to` once the router has answered.
|
|
42
|
-
`
|
|
41
|
+
It prints the servers that started, and `connected to` once the router has answered. Pin a workspace to it with
|
|
42
|
+
`setWorkspaceWorkers`, and that workspace's runs go there.
|
|
43
43
|
|
|
44
44
|
## Documentation
|
|
45
45
|
|
|
@@ -23,7 +23,7 @@ Once connected it prints `[worker] connected to <url> as <host>`. SIGTERM or SIG
|
|
|
23
23
|
| `AGENT_ROUTER_WORKER_KEY` | required | The worker key, which starts `aiw_`. |
|
|
24
24
|
| `WORKER_CONFIG` | none | A JSON file of the worker's own servers: [local servers](local-servers.md). |
|
|
25
25
|
| `WORKER_ROOT` | `~/agent-router-work` | The folder checkouts go in. Made when missing. |
|
|
26
|
-
| `WORKER_HOST` | the machine's host name | The name the worker gives the router for its machine. |
|
|
26
|
+
| `WORKER_HOST` | the machine's host name | The name the worker gives the router for its machine. Set it in a container. |
|
|
27
27
|
|
|
28
28
|
`WORKER_CONFIG` and `WORKER_ROOT` take `~`, `~/...`, an absolute path, or one relative to the current directory. A wrong variable or servers file prints `[worker] <what to fix>` and exits 1.
|
|
29
29
|
|
|
@@ -41,12 +41,12 @@ mutation {
|
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
```graphql
|
|
44
|
-
mutation ($workspace: UUID!, $worker: UUID) {
|
|
45
|
-
|
|
44
|
+
mutation ($workspace: UUID!, $worker: UUID!) {
|
|
45
|
+
setWorkspaceWorkers(workspaceId: $workspace, workerIds: [$worker]) { id }
|
|
46
46
|
}
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
Every run of that workspace goes to the worker from then on
|
|
49
|
+
Every run of that workspace goes to the worker from then on, when it offers the agent's model: this program offers every model of the router's providers, unless the admin gives it some with `attachProvider`. While it is offline, the router refuses them with 503 `worker_offline`. An empty `workerIds` lets the workspace's runs go to any worker.
|
|
50
50
|
|
|
51
51
|
## The Docker image
|
|
52
52
|
|
|
@@ -60,6 +60,13 @@ docker run -d --name agent-router-worker \
|
|
|
60
60
|
ghcr.io/cubicecho/agent-router-worker
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
+
### Name the machine
|
|
64
|
+
|
|
65
|
+
Inside a container, the host name is the container's id, which names no machine and changes each time the container is
|
|
66
|
+
made again. Set `WORKER_HOST` to the machine's name (`-e WORKER_HOST=gpu-box`), or give the container one (`--hostname
|
|
67
|
+
gpu-box`, or compose's `hostname:`). Without either, the worker starts with `[worker] WORKER_HOST is unset, so this
|
|
68
|
+
worker tells the router it runs on "<id>"…`.
|
|
69
|
+
|
|
63
70
|
## As a library
|
|
64
71
|
|
|
65
72
|
To start a worker from a program, with your own agent loop, see [a custom loop](custom-loop.md). The declarations in `types/worker/lib.d.ts` document every export.
|
|
@@ -44,6 +44,19 @@ The options are agent-core's `AgentLoopOptions`, and the result its `AgentLoopRe
|
|
|
44
44
|
- **Report through `options.onEvent`,** agent-core's `RunEventInput`s. They are the run's record on the router.
|
|
45
45
|
- **Stop on `options.signal`.** It fires on a cancel and when the worker stops. The run is then finished as failed with the reason, whatever the loop returns.
|
|
46
46
|
- **Return `messages`, `usage`, `loaded` and `turn`.** A throw fails the run with its message.
|
|
47
|
+
- **Offer your models, if you call them yourself.** A loop that calls its model through `options.config.fetch` needs do nothing: the worker offers the router's models, and the router records the model each call asks for. One that calls models of its own passes them to `startWorker` as `models`, each an `OfferedModel` (`{ provider, id }`). The router then sends this worker only runs of agents whose model is one of them, named `<provider>/<id>`, and none other. Run the one `options.config.model` names. Use a provider name every worker serving the same models shares, not the worker's own, so they share the runs.
|
|
48
|
+
- **Say the exact model.** A loop that calls a model of its own adds the model it ran to its result as `model`, typed by `LoopRuntime`, and the run records that.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import type { LoopRuntime, OfferedModel } from '@cubicecho/agent-router-worker';
|
|
52
|
+
|
|
53
|
+
const models: OfferedModel[] = [{ provider: 'my-provider', id: 'large' }];
|
|
54
|
+
const runtime: LoopRuntime = {
|
|
55
|
+
// options.config.model is 'my-provider/large'.
|
|
56
|
+
runAgentLoop: async (options) => ({ ...(await callMyOwnModel(options)), model: 'my-provider/large-2026-01' }),
|
|
57
|
+
};
|
|
58
|
+
// startWorker(config, { runtime, models })
|
|
59
|
+
```
|
|
47
60
|
|
|
48
61
|
## Test a loop without a router
|
|
49
62
|
|
|
@@ -110,7 +123,7 @@ Set the endpoint's `baseUrl` to exactly `http://models.ai-router.invalid/v1`, as
|
|
|
110
123
|
|
|
111
124
|
## Other things startWorker takes
|
|
112
125
|
|
|
113
|
-
`deps` also takes `fetch` for the requests to the router, `baseEnv` for what command servers inherit, `git` for `workdir`, and `settings` and `workdir` to change timeouts and limits. The declarations document each field.
|
|
126
|
+
`deps` also takes `models`, the models a loop of your own offers, which the hello says; `fetch` for the requests to the router, `baseEnv` for what command servers inherit, `git` for `workdir`, and `settings` and `workdir` to change timeouts and limits. The declarations document each field.
|
|
114
127
|
|
|
115
128
|
```ts
|
|
116
129
|
import { readWorkerConfig, startWorker } from '@cubicecho/agent-router-worker';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdirSync, readFileSync } from "node:fs";
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
2
2
|
import { homedir, hostname } from "node:os";
|
|
3
3
|
import path, { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
4
|
import { z } from "zod";
|
|
@@ -62,6 +62,10 @@ const DEFAULT_ROOT_FOLDER = "agent-router-work";
|
|
|
62
62
|
const HOME_PREFIX = "~/";
|
|
63
63
|
/** A lone `~`: the home folder itself. */
|
|
64
64
|
const HOME = "~";
|
|
65
|
+
/** A file Docker puts at the root of every container it runs. */
|
|
66
|
+
const DOCKER_MARKER = "/.dockerenv";
|
|
67
|
+
/** A container id, short or full, as a container's own host name. */
|
|
68
|
+
const CONTAINER_ID = /^(?:[0-9a-f]{12}|[0-9a-f]{64})$/;
|
|
65
69
|
/** A local server the worker starts itself: a command, run as the worker's user. */
|
|
66
70
|
const commandServerSchema = z.strictObject({
|
|
67
71
|
command: z.string().min(1, "A server's \"command\" cannot be empty."),
|
|
@@ -147,6 +151,18 @@ function readServersFile(path) {
|
|
|
147
151
|
}));
|
|
148
152
|
}
|
|
149
153
|
/**
|
|
154
|
+
* Says why a worker's own host name probably names no machine: inside a container it is the container's id, which
|
|
155
|
+
* changes each time the container is made again.
|
|
156
|
+
*
|
|
157
|
+
* @param name - The host name the worker would report.
|
|
158
|
+
* @param inContainer - Whether the worker runs in a container.
|
|
159
|
+
* @returns The warning, or null when the name looks like a machine's.
|
|
160
|
+
*/
|
|
161
|
+
function containerHostWarning(name, inContainer) {
|
|
162
|
+
if (inContainer === false && CONTAINER_ID.test(name) === false) return null;
|
|
163
|
+
return `WORKER_HOST is unset, so this worker tells the router it runs on "${name}", the container's own host name. Set WORKER_HOST to the machine's name, or give the container one (compose's hostname:).`;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
150
166
|
* Reads the worker's config from its environment, and makes its work root.
|
|
151
167
|
*
|
|
152
168
|
* @param env - The environment, `process.env` at boot.
|
|
@@ -165,14 +181,92 @@ function readWorkerConfig(env, cwd = process.cwd()) {
|
|
|
165
181
|
throw new WorkerConfigError(`WORKER_ROOT is ${root}, which can't be made: ${errorMessage(error)}`);
|
|
166
182
|
}
|
|
167
183
|
const servers = WORKER_CONFIG ? readServersFile(absolutePath(WORKER_CONFIG, cwd)) : {};
|
|
184
|
+
const host = WORKER_HOST || hostname();
|
|
185
|
+
const hostWarning = WORKER_HOST ? null : containerHostWarning(host, existsSync(DOCKER_MARKER));
|
|
168
186
|
return {
|
|
169
187
|
url: AGENT_ROUTER_URL.replace(/\/+$/, ""),
|
|
170
188
|
key: AGENT_ROUTER_WORKER_KEY,
|
|
171
189
|
root,
|
|
172
|
-
host
|
|
173
|
-
servers
|
|
190
|
+
host,
|
|
191
|
+
servers,
|
|
192
|
+
...hostWarning === null ? {} : { hostWarning }
|
|
174
193
|
};
|
|
175
194
|
}
|
|
195
|
+
Object.freeze({
|
|
196
|
+
secretBytes: 32,
|
|
197
|
+
visibleChars: 6
|
|
198
|
+
});
|
|
199
|
+
const NAME_DEFAULTS = Object.freeze({ maxLength: 64 });
|
|
200
|
+
Object.freeze({
|
|
201
|
+
maxDocumentBytes: 262144,
|
|
202
|
+
maxMessageLength: 500
|
|
203
|
+
});
|
|
204
|
+
Object.freeze({
|
|
205
|
+
timeoutMinutes: 30,
|
|
206
|
+
idempotencyHours: 24,
|
|
207
|
+
retentionDays: 30,
|
|
208
|
+
pruneEveryHours: 6,
|
|
209
|
+
bodyLimit: "8mb",
|
|
210
|
+
heartbeatSeconds: 15,
|
|
211
|
+
maxIdempotencyKeyLength: 255,
|
|
212
|
+
maxUserLength: 256,
|
|
213
|
+
maxErrorLength: 2e3,
|
|
214
|
+
eventFlushMs: 250,
|
|
215
|
+
streamHoldMs: 1e4,
|
|
216
|
+
eventBatchSize: 500,
|
|
217
|
+
eventPageSize: 500,
|
|
218
|
+
maxEventPageSize: 5e3,
|
|
219
|
+
runPageSize: 25,
|
|
220
|
+
maxRunPageSize: 100,
|
|
221
|
+
sessionTitleLength: 80,
|
|
222
|
+
waitTimeoutHours: 24,
|
|
223
|
+
expireEveryMinutes: 5,
|
|
224
|
+
taskPollIntervalMs: 2e3,
|
|
225
|
+
maxAgentDepth: 3
|
|
226
|
+
});
|
|
227
|
+
const WORKER_DEFAULTS = Object.freeze({
|
|
228
|
+
heartbeatMs: 15e3,
|
|
229
|
+
reconnectGraceMs: 6e4,
|
|
230
|
+
eventBatchSize: 100,
|
|
231
|
+
eventFlushMs: 250,
|
|
232
|
+
maxRunsAtOnce: 8,
|
|
233
|
+
cancelGraceMs: 1e4,
|
|
234
|
+
bodyLimit: "8mb"
|
|
235
|
+
});
|
|
236
|
+
Object.freeze({
|
|
237
|
+
maxAppTools: 128,
|
|
238
|
+
pruneAppToolsAcrossServers: false,
|
|
239
|
+
unlistedFields: "refuse"
|
|
240
|
+
});
|
|
241
|
+
Object.freeze({
|
|
242
|
+
idleHours: 72,
|
|
243
|
+
maxIdLength: 255
|
|
244
|
+
});
|
|
245
|
+
Object.freeze({
|
|
246
|
+
maxNameLength: 120,
|
|
247
|
+
maxDescriptionLength: 2e3,
|
|
248
|
+
maxKeyNameLength: 120,
|
|
249
|
+
maxKeyExpiryDays: 3650,
|
|
250
|
+
maxToolNameLength: 128
|
|
251
|
+
});
|
|
252
|
+
Object.freeze({ closeTimeoutSeconds: 5 });
|
|
253
|
+
// server/core/names.ts
|
|
254
|
+
/**
|
|
255
|
+
* What a router name may look like: lowercase letters, digits, dots, dashes and underscores,
|
|
256
|
+
* starting with a letter or digit. llm-router and mcp-router use the same rule.
|
|
257
|
+
*/
|
|
258
|
+
const NAME_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;
|
|
259
|
+
/**
|
|
260
|
+
* Builds the schema for a router name: a workspace's slug, a provider's or pool's name, and later a
|
|
261
|
+
* server's or agent's.
|
|
262
|
+
*
|
|
263
|
+
* @param label - What the name names, as the message starts it ("Slug").
|
|
264
|
+
* @returns The zod schema, which trims before it checks.
|
|
265
|
+
*/
|
|
266
|
+
function nameSchema(label) {
|
|
267
|
+
const { maxLength } = NAME_DEFAULTS;
|
|
268
|
+
return z.string().trim().min(1, `${label} cannot be empty.`).max(maxLength, `${label} is longer than ${maxLength} characters.`).regex(NAME_PATTERN, `${label} may hold lowercase letters, digits, dots, dashes and underscores, and must start with a letter or digit.`);
|
|
269
|
+
}
|
|
176
270
|
// server/runner/protocol.ts
|
|
177
271
|
/**
|
|
178
272
|
* The worker protocol: what a remote worker and the router say to each other, shared by both ends.
|
|
@@ -236,6 +330,17 @@ const localServerSchema = z.object({
|
|
|
236
330
|
name: z.string().min(1),
|
|
237
331
|
tools: z.array(localToolSchema)
|
|
238
332
|
});
|
|
333
|
+
/** What a worker says when its channel opens, and again whenever its servers or tools change. */
|
|
334
|
+
/** A model a worker's own loop offers: run by the worker, not through the router. */
|
|
335
|
+
const offeredModelSchema = z.object({
|
|
336
|
+
/**
|
|
337
|
+
* The provider's name, as a model id starts. Shared by workers that offer the same models, so they take the same
|
|
338
|
+
* agents' runs: not the worker's own name.
|
|
339
|
+
*/
|
|
340
|
+
provider: nameSchema("A model's provider"),
|
|
341
|
+
/** The model's id at that provider. */
|
|
342
|
+
id: z.string().min(1)
|
|
343
|
+
});
|
|
239
344
|
z.object({
|
|
240
345
|
/** The machine it runs on, as its owner would recognise it. */
|
|
241
346
|
host: z.string().min(1),
|
|
@@ -247,7 +352,13 @@ z.object({
|
|
|
247
352
|
* The runs it holds now. The router ends at once any run it sent this worker on an earlier channel that is not here:
|
|
248
353
|
* a worker that restarted holds none. Absent, from a worker older than the field, the router keeps them.
|
|
249
354
|
*/
|
|
250
|
-
runs: z.array(z.string().min(1)).optional()
|
|
355
|
+
runs: z.array(z.string().min(1)).optional(),
|
|
356
|
+
/**
|
|
357
|
+
* The models its own loop offers, for a worker whose loop calls its model itself rather than the router's. A worker
|
|
358
|
+
* that says some takes only the runs of agents that name one of them. Absent or empty for one on the standard loop,
|
|
359
|
+
* which takes the runs of agents whose model is a provider's it is given.
|
|
360
|
+
*/
|
|
361
|
+
models: z.array(offeredModelSchema).optional()
|
|
251
362
|
});
|
|
252
363
|
z.object({
|
|
253
364
|
/** The server's id, as the run's targets name it. */
|
|
@@ -268,70 +379,14 @@ z.discriminatedUnion("ok", [z.looseObject({
|
|
|
268
379
|
usage: z.unknown(),
|
|
269
380
|
loaded: z.array(z.string()),
|
|
270
381
|
finishReason: z.unknown(),
|
|
271
|
-
handedBack: z.array(z.string())
|
|
382
|
+
handedBack: z.array(z.string()),
|
|
383
|
+
model: z.string().min(1).optional()
|
|
272
384
|
}), z.object({
|
|
273
385
|
ok: z.literal(false),
|
|
274
386
|
error: z.string(),
|
|
275
|
-
usage: z.unknown().optional()
|
|
387
|
+
usage: z.unknown().optional(),
|
|
388
|
+
model: z.string().min(1).optional()
|
|
276
389
|
})]);
|
|
277
|
-
Object.freeze({
|
|
278
|
-
secretBytes: 32,
|
|
279
|
-
visibleChars: 6
|
|
280
|
-
});
|
|
281
|
-
const NAME_DEFAULTS = Object.freeze({ maxLength: 64 });
|
|
282
|
-
Object.freeze({
|
|
283
|
-
maxDocumentBytes: 262144,
|
|
284
|
-
maxMessageLength: 500
|
|
285
|
-
});
|
|
286
|
-
Object.freeze({
|
|
287
|
-
timeoutMinutes: 30,
|
|
288
|
-
idempotencyHours: 24,
|
|
289
|
-
retentionDays: 30,
|
|
290
|
-
pruneEveryHours: 6,
|
|
291
|
-
bodyLimit: "8mb",
|
|
292
|
-
heartbeatSeconds: 15,
|
|
293
|
-
maxIdempotencyKeyLength: 255,
|
|
294
|
-
maxUserLength: 256,
|
|
295
|
-
maxErrorLength: 2e3,
|
|
296
|
-
eventFlushMs: 250,
|
|
297
|
-
streamHoldMs: 1e4,
|
|
298
|
-
eventBatchSize: 500,
|
|
299
|
-
eventPageSize: 500,
|
|
300
|
-
maxEventPageSize: 5e3,
|
|
301
|
-
runPageSize: 25,
|
|
302
|
-
maxRunPageSize: 100,
|
|
303
|
-
sessionTitleLength: 80,
|
|
304
|
-
waitTimeoutHours: 24,
|
|
305
|
-
expireEveryMinutes: 5,
|
|
306
|
-
taskPollIntervalMs: 2e3,
|
|
307
|
-
maxAgentDepth: 3
|
|
308
|
-
});
|
|
309
|
-
const WORKER_DEFAULTS = Object.freeze({
|
|
310
|
-
heartbeatMs: 15e3,
|
|
311
|
-
reconnectGraceMs: 6e4,
|
|
312
|
-
eventBatchSize: 100,
|
|
313
|
-
eventFlushMs: 250,
|
|
314
|
-
maxRunsAtOnce: 8,
|
|
315
|
-
cancelGraceMs: 1e4,
|
|
316
|
-
bodyLimit: "8mb"
|
|
317
|
-
});
|
|
318
|
-
Object.freeze({
|
|
319
|
-
maxAppTools: 128,
|
|
320
|
-
pruneAppToolsAcrossServers: false,
|
|
321
|
-
unlistedFields: "refuse"
|
|
322
|
-
});
|
|
323
|
-
Object.freeze({
|
|
324
|
-
idleHours: 72,
|
|
325
|
-
maxIdLength: 255
|
|
326
|
-
});
|
|
327
|
-
Object.freeze({
|
|
328
|
-
maxNameLength: 120,
|
|
329
|
-
maxDescriptionLength: 2e3,
|
|
330
|
-
maxKeyNameLength: 120,
|
|
331
|
-
maxKeyExpiryDays: 3650,
|
|
332
|
-
maxToolNameLength: 128
|
|
333
|
-
});
|
|
334
|
-
Object.freeze({ closeTimeoutSeconds: 5 });
|
|
335
390
|
// worker/defaults.ts
|
|
336
391
|
const WORKDIR_DEFAULTS = Object.freeze({
|
|
337
392
|
cloneTimeoutMs: 6e5,
|
|
@@ -611,7 +666,8 @@ async function executeRun(assignment, ports, runtime) {
|
|
|
611
666
|
usage: result.usage,
|
|
612
667
|
loaded: result.loaded,
|
|
613
668
|
finishReason: result.turn.finishReason,
|
|
614
|
-
handedBack
|
|
669
|
+
handedBack,
|
|
670
|
+
...typeof result.model === "string" && result.model !== "" ? { model: result.model } : {}
|
|
615
671
|
};
|
|
616
672
|
} catch (error) {
|
|
617
673
|
return {
|
|
@@ -859,23 +915,6 @@ function createRunHost(options, overrides = {}) {
|
|
|
859
915
|
}
|
|
860
916
|
};
|
|
861
917
|
}
|
|
862
|
-
// server/core/names.ts
|
|
863
|
-
/**
|
|
864
|
-
* What a router name may look like: lowercase letters, digits, dots, dashes and underscores,
|
|
865
|
-
* starting with a letter or digit. llm-router and mcp-router use the same rule.
|
|
866
|
-
*/
|
|
867
|
-
const NAME_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;
|
|
868
|
-
/**
|
|
869
|
-
* Builds the schema for a router name: a workspace's slug, a provider's or pool's name, and later a
|
|
870
|
-
* server's or agent's.
|
|
871
|
-
*
|
|
872
|
-
* @param label - What the name names, as the message starts it ("Slug").
|
|
873
|
-
* @returns The zod schema, which trims before it checks.
|
|
874
|
-
*/
|
|
875
|
-
function nameSchema(label) {
|
|
876
|
-
const { maxLength } = NAME_DEFAULTS;
|
|
877
|
-
return z.string().trim().min(1, `${label} cannot be empty.`).max(maxLength, `${label} is longer than ${maxLength} characters.`).regex(NAME_PATTERN, `${label} may hold lowercase letters, digits, dots, dashes and underscores, and must start with a letter or digit.`);
|
|
878
|
-
}
|
|
879
918
|
// server/tools/child-transport.ts
|
|
880
919
|
/** The signals a stopping program gets, in order, after its stdin closes. */
|
|
881
920
|
const STOP_SIGNALS = ["SIGTERM", "SIGKILL"];
|
|
@@ -1058,7 +1097,7 @@ path.resolve(import.meta.dirname, "..", "..");
|
|
|
1058
1097
|
* A bundle can't read the repo's package.json the way `version()` does, because it isn't there.
|
|
1059
1098
|
*/
|
|
1060
1099
|
function workerVersion() {
|
|
1061
|
-
return "0.
|
|
1100
|
+
return "0.10.0";
|
|
1062
1101
|
}
|
|
1063
1102
|
// worker/workdir/git.ts
|
|
1064
1103
|
/** The git program, found on PATH. */
|
|
@@ -1677,15 +1716,17 @@ async function startLocalServers(options, overrides = {}) {
|
|
|
1677
1716
|
* @returns The running worker.
|
|
1678
1717
|
*/
|
|
1679
1718
|
async function startWorker(config, deps = {}) {
|
|
1680
|
-
const { url, key, root, host, servers } = config;
|
|
1681
|
-
const { fetch = globalThis.fetch, runtime = { runAgentLoop }, baseEnv, git, settings = {}, workdir } = deps;
|
|
1719
|
+
const { url, key, root, host, servers, hostWarning } = config;
|
|
1720
|
+
const { fetch = globalThis.fetch, runtime = { runAgentLoop }, baseEnv, git, settings = {}, workdir, models } = deps;
|
|
1721
|
+
if (hostWarning !== void 0) console.warn(`[worker] ${hostWarning}`);
|
|
1682
1722
|
let channel;
|
|
1683
1723
|
const sayHello = async () => {
|
|
1684
1724
|
const hello = {
|
|
1685
1725
|
host,
|
|
1686
1726
|
version: workerVersion(),
|
|
1687
1727
|
servers: local.listing(),
|
|
1688
|
-
runs: runs.ids()
|
|
1728
|
+
runs: runs.ids(),
|
|
1729
|
+
...models === void 0 || models.length === 0 ? {} : { models: [...models] }
|
|
1689
1730
|
};
|
|
1690
1731
|
const response = await fetch(`${url}${WORKERS_PATH}${WorkerDoor.Hello}`, {
|
|
1691
1732
|
method: "POST",
|
package/lib.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { r as TOOL_ORIGIN_LOCAL, i as TOOL_ORIGIN_ROUTER, a as WorkerConfigError, n as executeRun, o as readWorkerConfig, t as startWorker } from "./chunks/worker-
|
|
1
|
+
export { r as TOOL_ORIGIN_LOCAL, i as TOOL_ORIGIN_ROUTER, a as WorkerConfigError, n as executeRun, o as readWorkerConfig, t as startWorker } from "./chunks/worker-Cm-rwbaS.js";
|
package/package.json
CHANGED
|
@@ -109,6 +109,8 @@ export interface AnsweredOutcome {
|
|
|
109
109
|
finishReason: AgentLoopResult['turn']['finishReason'];
|
|
110
110
|
/** The loop ids of the app tool calls handed back, in the order they were made. Empty for an answer. */
|
|
111
111
|
handedBack: string[];
|
|
112
|
+
/** The model the loop called, when it called one itself rather than through `RunPorts.modelFetch`. */
|
|
113
|
+
model?: string;
|
|
112
114
|
}
|
|
113
115
|
/** A stretch the loop failed, or was stopped. */
|
|
114
116
|
export interface FailedOutcome {
|
|
@@ -117,6 +119,8 @@ export interface FailedOutcome {
|
|
|
117
119
|
error: string;
|
|
118
120
|
/** What the model spent before it failed, when the loop knows. */
|
|
119
121
|
usage: LoopUsage | undefined;
|
|
122
|
+
/** The model the loop called, when it called one itself rather than through `RunPorts.modelFetch`. */
|
|
123
|
+
model?: string;
|
|
120
124
|
}
|
|
121
125
|
/** How a stretch's loop ended. */
|
|
122
126
|
export type RunOutcome = AnsweredOutcome | FailedOutcome;
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { type Runtime } from '@cubicecho/agent-core';
|
|
2
2
|
import { type RunAssignment, type RunOutcome, type RunPorts } from './assignment.ts';
|
|
3
|
+
/**
|
|
4
|
+
* The loop an assignment runs on: agent-core's `runAgentLoop`, or one in its place. A loop that calls its model itself,
|
|
5
|
+
* rather than through the assignment's `fetch`, adds the model's name to its result as `model`, and the run records it.
|
|
6
|
+
*/
|
|
7
|
+
export interface LoopRuntime {
|
|
8
|
+
runAgentLoop: (options: Parameters<Runtime['runAgentLoop']>[0]) => Promise<Awaited<ReturnType<Runtime['runAgentLoop']>> & {
|
|
9
|
+
model?: string;
|
|
10
|
+
}>;
|
|
11
|
+
}
|
|
3
12
|
/**
|
|
4
13
|
* Runs one stretch of a run's agent loop: to its answer, or until the model calls an app tool.
|
|
5
14
|
*
|
|
@@ -12,4 +21,4 @@ import { type RunAssignment, type RunOutcome, type RunPorts } from './assignment
|
|
|
12
21
|
* Each app tool call ends the loop with a stand-in answer, which the app replaces when it resumes the run, so the
|
|
13
22
|
* stand-in's result is never published.
|
|
14
23
|
*/
|
|
15
|
-
export declare function executeRun(assignment: RunAssignment, ports: RunPorts, runtime:
|
|
24
|
+
export declare function executeRun(assignment: RunAssignment, ports: RunPorts, runtime: LoopRuntime): Promise<RunOutcome>;
|
|
@@ -86,6 +86,12 @@ export declare const localServerSchema: z.ZodObject<{
|
|
|
86
86
|
}, z.core.$strip>;
|
|
87
87
|
export type LocalServer = z.infer<typeof localServerSchema>;
|
|
88
88
|
/** What a worker says when its channel opens, and again whenever its servers or tools change. */
|
|
89
|
+
/** A model a worker's own loop offers: run by the worker, not through the router. */
|
|
90
|
+
export declare const offeredModelSchema: z.ZodObject<{
|
|
91
|
+
provider: z.ZodString;
|
|
92
|
+
id: z.ZodString;
|
|
93
|
+
}, z.core.$strip>;
|
|
94
|
+
export type OfferedModel = z.infer<typeof offeredModelSchema>;
|
|
89
95
|
export declare const workerHelloSchema: z.ZodObject<{
|
|
90
96
|
host: z.ZodString;
|
|
91
97
|
version: z.ZodString;
|
|
@@ -98,6 +104,10 @@ export declare const workerHelloSchema: z.ZodObject<{
|
|
|
98
104
|
}, z.core.$strip>>;
|
|
99
105
|
}, z.core.$strip>>;
|
|
100
106
|
runs: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
107
|
+
models: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
108
|
+
provider: z.ZodString;
|
|
109
|
+
id: z.ZodString;
|
|
110
|
+
}, z.core.$strip>>>;
|
|
101
111
|
}, z.core.$strip>;
|
|
102
112
|
export type WorkerHello = z.infer<typeof workerHelloSchema>;
|
|
103
113
|
/** A call to one of the router's servers, made for the run. */
|
|
@@ -137,10 +147,12 @@ export declare const runOutcomeSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
137
147
|
loaded: z.ZodArray<z.ZodString>;
|
|
138
148
|
finishReason: z.ZodUnknown;
|
|
139
149
|
handedBack: z.ZodArray<z.ZodString>;
|
|
150
|
+
model: z.ZodOptional<z.ZodString>;
|
|
140
151
|
}, z.core.$loose>, z.ZodObject<{
|
|
141
152
|
ok: z.ZodLiteral<false>;
|
|
142
153
|
error: z.ZodString;
|
|
143
154
|
usage: z.ZodOptional<z.ZodUnknown>;
|
|
155
|
+
model: z.ZodOptional<z.ZodString>;
|
|
144
156
|
}, z.core.$strip>], "ok">;
|
|
145
157
|
/** Re-exported so both ends name the same shapes. */
|
|
146
158
|
export type { RunAssignment, RunOutcome };
|
package/types/worker/config.d.ts
CHANGED
|
@@ -34,6 +34,11 @@ export interface WorkerConfig {
|
|
|
34
34
|
root: string;
|
|
35
35
|
/** The machine it says it runs on. */
|
|
36
36
|
host: string;
|
|
37
|
+
/**
|
|
38
|
+
* Why `host` probably names no machine: `WORKER_HOST` is unset and the worker runs in a container, whose own host
|
|
39
|
+
* name is its id. `startWorker` logs it. Absent when `host` is fine.
|
|
40
|
+
*/
|
|
41
|
+
hostWarning?: string;
|
|
37
42
|
/** Its own servers, by name, with each command server's `cwd` made absolute. */
|
|
38
43
|
servers: Record<string, LocalServerConfig>;
|
|
39
44
|
}
|
|
@@ -44,6 +49,15 @@ export declare class WorkerConfigError extends Error {
|
|
|
44
49
|
*/
|
|
45
50
|
constructor(message: string);
|
|
46
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Says why a worker's own host name probably names no machine: inside a container it is the container's id, which
|
|
54
|
+
* changes each time the container is made again.
|
|
55
|
+
*
|
|
56
|
+
* @param name - The host name the worker would report.
|
|
57
|
+
* @param inContainer - Whether the worker runs in a container.
|
|
58
|
+
* @returns The warning, or null when the name looks like a machine's.
|
|
59
|
+
*/
|
|
60
|
+
export declare function containerHostWarning(name: string, inContainer: boolean): string | null;
|
|
47
61
|
/**
|
|
48
62
|
* Reads the worker's config from its environment, and makes its work root.
|
|
49
63
|
*
|
package/types/worker/lib.d.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export type { AppToolDefinition, RunAssignment, RunOutcome, RunPorts, ToolDefinition, ToolTarget, TranscriptMessage, } from '../server/runner/assignment.ts';
|
|
9
9
|
export { TOOL_ORIGIN_LOCAL, TOOL_ORIGIN_ROUTER } from '../server/runner/assignment.ts';
|
|
10
|
-
export { executeRun } from '../server/runner/execute.ts';
|
|
10
|
+
export { executeRun, type LoopRuntime } from '../server/runner/execute.ts';
|
|
11
|
+
export type { OfferedModel } from '../server/runner/protocol.ts';
|
|
11
12
|
export { readWorkerConfig, type WorkerConfig, WorkerConfigError } from './config.ts';
|
|
12
13
|
export { type RunningWorker, startWorker, type WorkerDeps } from './worker.ts';
|
package/types/worker/run.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type Runtime } from '@cubicecho/agent-core';
|
|
2
1
|
import { type WorkerSettings } from '../server/core/defaults.ts';
|
|
3
2
|
import type { RunAssignment } from '../server/runner/assignment.ts';
|
|
3
|
+
import { type LoopRuntime } from '../server/runner/execute.ts';
|
|
4
4
|
import { type WorkerProgramSettings } from './defaults.ts';
|
|
5
5
|
import type { LocalServers } from './servers.ts';
|
|
6
6
|
/** Why a run ended when a door answered 404: the router no longer runs it on this worker. */
|
|
@@ -18,7 +18,7 @@ export interface RunHostOptions {
|
|
|
18
18
|
/** The worker's own servers. Absent when it has none. */
|
|
19
19
|
local?: LocalServers;
|
|
20
20
|
/** agent-core's runtime, whose loop the runs use. */
|
|
21
|
-
runtime:
|
|
21
|
+
runtime: LoopRuntime;
|
|
22
22
|
/**
|
|
23
23
|
* Makes the requests.
|
|
24
24
|
*
|
package/types/worker/worker.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { type Runtime } from '@cubicecho/agent-core';
|
|
2
1
|
import type { WorkerSettings } from '../server/core/defaults.ts';
|
|
2
|
+
import type { LoopRuntime } from '../server/runner/execute.ts';
|
|
3
|
+
import { type OfferedModel } from '../server/runner/protocol.ts';
|
|
3
4
|
import { type Channel } from './channel.ts';
|
|
4
5
|
import type { WorkerConfig } from './config.ts';
|
|
5
6
|
import type { WorkdirSettings, WorkerProgramSettings } from './defaults.ts';
|
|
@@ -19,7 +20,7 @@ export interface WorkerDeps {
|
|
|
19
20
|
*
|
|
20
21
|
* @defaultValue agent-core's `runAgentLoop`
|
|
21
22
|
*/
|
|
22
|
-
runtime?:
|
|
23
|
+
runtime?: LoopRuntime;
|
|
23
24
|
/**
|
|
24
25
|
* The environment a command server's allowlist reads from.
|
|
25
26
|
*
|
|
@@ -32,6 +33,13 @@ export interface WorkerDeps {
|
|
|
32
33
|
settings?: Partial<WorkerSettings & WorkerProgramSettings>;
|
|
33
34
|
/** Settings for `workdir`. */
|
|
34
35
|
workdir?: Partial<WorkdirSettings>;
|
|
36
|
+
/**
|
|
37
|
+
* The models the loop offers, for a `runtime` that calls its model itself rather than through the assignment's
|
|
38
|
+
* `fetch`. Said in the hello: the router then sends this worker only the runs of agents that name one of them, as
|
|
39
|
+
* `<provider>/<id>`, and the loop runs the one the assignment's agent names. Workers that offer the same models share
|
|
40
|
+
* a provider name. Each run's exact model is what its loop's result says as `model`.
|
|
41
|
+
*/
|
|
42
|
+
models?: readonly OfferedModel[];
|
|
35
43
|
}
|
|
36
44
|
/** A worker that is running. */
|
|
37
45
|
export interface RunningWorker {
|
package/worker.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { o as readWorkerConfig, s as errorMessage, t as startWorker } from "./chunks/worker-
|
|
2
|
+
import { o as readWorkerConfig, s as errorMessage, t as startWorker } from "./chunks/worker-Cm-rwbaS.js";
|
|
3
3
|
// worker/index.ts
|
|
4
4
|
/** Exits with this when the worker can't start, or is stopped twice. */
|
|
5
5
|
const EXIT_FAILED = 1;
|