@cubicecho/agent-router-worker 0.9.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 +3 -3
- package/_agents/docs/custom-loop.md +8 -4
- package/chunks/{worker-NNsrVSoq.js → worker-Cm-rwbaS.js} +93 -81
- package/lib.js +1 -1
- package/package.json +1 -1
- package/types/server/runner/protocol.d.ts +10 -1
- package/types/worker/lib.d.ts +1 -0
- package/types/worker/worker.d.ts +6 -3
- 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
|
|
|
@@ -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
|
|
|
@@ -44,14 +44,18 @@ 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
|
-
- **
|
|
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.
|
|
48
49
|
|
|
49
50
|
```ts
|
|
50
|
-
import type { LoopRuntime } from '@cubicecho/agent-router-worker';
|
|
51
|
+
import type { LoopRuntime, OfferedModel } from '@cubicecho/agent-router-worker';
|
|
51
52
|
|
|
53
|
+
const models: OfferedModel[] = [{ provider: 'my-provider', id: 'large' }];
|
|
52
54
|
const runtime: LoopRuntime = {
|
|
53
|
-
|
|
55
|
+
// options.config.model is 'my-provider/large'.
|
|
56
|
+
runAgentLoop: async (options) => ({ ...(await callMyOwnModel(options)), model: 'my-provider/large-2026-01' }),
|
|
54
57
|
};
|
|
58
|
+
// startWorker(config, { runtime, models })
|
|
55
59
|
```
|
|
56
60
|
|
|
57
61
|
## Test a loop without a router
|
|
@@ -119,7 +123,7 @@ Set the endpoint's `baseUrl` to exactly `http://models.ai-router.invalid/v1`, as
|
|
|
119
123
|
|
|
120
124
|
## Other things startWorker takes
|
|
121
125
|
|
|
122
|
-
`deps` also takes `
|
|
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.
|
|
123
127
|
|
|
124
128
|
```ts
|
|
125
129
|
import { readWorkerConfig, startWorker } from '@cubicecho/agent-router-worker';
|
|
@@ -192,6 +192,81 @@ function readWorkerConfig(env, cwd = process.cwd()) {
|
|
|
192
192
|
...hostWarning === null ? {} : { hostWarning }
|
|
193
193
|
};
|
|
194
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
|
+
}
|
|
195
270
|
// server/runner/protocol.ts
|
|
196
271
|
/**
|
|
197
272
|
* The worker protocol: what a remote worker and the router say to each other, shared by both ends.
|
|
@@ -255,6 +330,17 @@ const localServerSchema = z.object({
|
|
|
255
330
|
name: z.string().min(1),
|
|
256
331
|
tools: z.array(localToolSchema)
|
|
257
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
|
+
});
|
|
258
344
|
z.object({
|
|
259
345
|
/** The machine it runs on, as its owner would recognise it. */
|
|
260
346
|
host: z.string().min(1),
|
|
@@ -268,10 +354,11 @@ z.object({
|
|
|
268
354
|
*/
|
|
269
355
|
runs: z.array(z.string().min(1)).optional(),
|
|
270
356
|
/**
|
|
271
|
-
* The
|
|
272
|
-
*
|
|
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.
|
|
273
360
|
*/
|
|
274
|
-
|
|
361
|
+
models: z.array(offeredModelSchema).optional()
|
|
275
362
|
});
|
|
276
363
|
z.object({
|
|
277
364
|
/** The server's id, as the run's targets name it. */
|
|
@@ -300,64 +387,6 @@ z.discriminatedUnion("ok", [z.looseObject({
|
|
|
300
387
|
usage: z.unknown().optional(),
|
|
301
388
|
model: z.string().min(1).optional()
|
|
302
389
|
})]);
|
|
303
|
-
Object.freeze({
|
|
304
|
-
secretBytes: 32,
|
|
305
|
-
visibleChars: 6
|
|
306
|
-
});
|
|
307
|
-
const NAME_DEFAULTS = Object.freeze({ maxLength: 64 });
|
|
308
|
-
Object.freeze({
|
|
309
|
-
maxDocumentBytes: 262144,
|
|
310
|
-
maxMessageLength: 500
|
|
311
|
-
});
|
|
312
|
-
Object.freeze({
|
|
313
|
-
timeoutMinutes: 30,
|
|
314
|
-
idempotencyHours: 24,
|
|
315
|
-
retentionDays: 30,
|
|
316
|
-
pruneEveryHours: 6,
|
|
317
|
-
bodyLimit: "8mb",
|
|
318
|
-
heartbeatSeconds: 15,
|
|
319
|
-
maxIdempotencyKeyLength: 255,
|
|
320
|
-
maxUserLength: 256,
|
|
321
|
-
maxErrorLength: 2e3,
|
|
322
|
-
eventFlushMs: 250,
|
|
323
|
-
streamHoldMs: 1e4,
|
|
324
|
-
eventBatchSize: 500,
|
|
325
|
-
eventPageSize: 500,
|
|
326
|
-
maxEventPageSize: 5e3,
|
|
327
|
-
runPageSize: 25,
|
|
328
|
-
maxRunPageSize: 100,
|
|
329
|
-
sessionTitleLength: 80,
|
|
330
|
-
waitTimeoutHours: 24,
|
|
331
|
-
expireEveryMinutes: 5,
|
|
332
|
-
taskPollIntervalMs: 2e3,
|
|
333
|
-
maxAgentDepth: 3
|
|
334
|
-
});
|
|
335
|
-
const WORKER_DEFAULTS = Object.freeze({
|
|
336
|
-
heartbeatMs: 15e3,
|
|
337
|
-
reconnectGraceMs: 6e4,
|
|
338
|
-
eventBatchSize: 100,
|
|
339
|
-
eventFlushMs: 250,
|
|
340
|
-
maxRunsAtOnce: 8,
|
|
341
|
-
cancelGraceMs: 1e4,
|
|
342
|
-
bodyLimit: "8mb"
|
|
343
|
-
});
|
|
344
|
-
Object.freeze({
|
|
345
|
-
maxAppTools: 128,
|
|
346
|
-
pruneAppToolsAcrossServers: false,
|
|
347
|
-
unlistedFields: "refuse"
|
|
348
|
-
});
|
|
349
|
-
Object.freeze({
|
|
350
|
-
idleHours: 72,
|
|
351
|
-
maxIdLength: 255
|
|
352
|
-
});
|
|
353
|
-
Object.freeze({
|
|
354
|
-
maxNameLength: 120,
|
|
355
|
-
maxDescriptionLength: 2e3,
|
|
356
|
-
maxKeyNameLength: 120,
|
|
357
|
-
maxKeyExpiryDays: 3650,
|
|
358
|
-
maxToolNameLength: 128
|
|
359
|
-
});
|
|
360
|
-
Object.freeze({ closeTimeoutSeconds: 5 });
|
|
361
390
|
// worker/defaults.ts
|
|
362
391
|
const WORKDIR_DEFAULTS = Object.freeze({
|
|
363
392
|
cloneTimeoutMs: 6e5,
|
|
@@ -886,23 +915,6 @@ function createRunHost(options, overrides = {}) {
|
|
|
886
915
|
}
|
|
887
916
|
};
|
|
888
917
|
}
|
|
889
|
-
// server/core/names.ts
|
|
890
|
-
/**
|
|
891
|
-
* What a router name may look like: lowercase letters, digits, dots, dashes and underscores,
|
|
892
|
-
* starting with a letter or digit. llm-router and mcp-router use the same rule.
|
|
893
|
-
*/
|
|
894
|
-
const NAME_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;
|
|
895
|
-
/**
|
|
896
|
-
* Builds the schema for a router name: a workspace's slug, a provider's or pool's name, and later a
|
|
897
|
-
* server's or agent's.
|
|
898
|
-
*
|
|
899
|
-
* @param label - What the name names, as the message starts it ("Slug").
|
|
900
|
-
* @returns The zod schema, which trims before it checks.
|
|
901
|
-
*/
|
|
902
|
-
function nameSchema(label) {
|
|
903
|
-
const { maxLength } = NAME_DEFAULTS;
|
|
904
|
-
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.`);
|
|
905
|
-
}
|
|
906
918
|
// server/tools/child-transport.ts
|
|
907
919
|
/** The signals a stopping program gets, in order, after its stdin closes. */
|
|
908
920
|
const STOP_SIGNALS = ["SIGTERM", "SIGKILL"];
|
|
@@ -1085,7 +1097,7 @@ path.resolve(import.meta.dirname, "..", "..");
|
|
|
1085
1097
|
* A bundle can't read the repo's package.json the way `version()` does, because it isn't there.
|
|
1086
1098
|
*/
|
|
1087
1099
|
function workerVersion() {
|
|
1088
|
-
return "0.
|
|
1100
|
+
return "0.10.0";
|
|
1089
1101
|
}
|
|
1090
1102
|
// worker/workdir/git.ts
|
|
1091
1103
|
/** The git program, found on PATH. */
|
|
@@ -1705,7 +1717,7 @@ async function startLocalServers(options, overrides = {}) {
|
|
|
1705
1717
|
*/
|
|
1706
1718
|
async function startWorker(config, deps = {}) {
|
|
1707
1719
|
const { url, key, root, host, servers, hostWarning } = config;
|
|
1708
|
-
const { fetch = globalThis.fetch, runtime = { runAgentLoop }, baseEnv, git, settings = {}, workdir,
|
|
1720
|
+
const { fetch = globalThis.fetch, runtime = { runAgentLoop }, baseEnv, git, settings = {}, workdir, models } = deps;
|
|
1709
1721
|
if (hostWarning !== void 0) console.warn(`[worker] ${hostWarning}`);
|
|
1710
1722
|
let channel;
|
|
1711
1723
|
const sayHello = async () => {
|
|
@@ -1714,7 +1726,7 @@ async function startWorker(config, deps = {}) {
|
|
|
1714
1726
|
version: workerVersion(),
|
|
1715
1727
|
servers: local.listing(),
|
|
1716
1728
|
runs: runs.ids(),
|
|
1717
|
-
...
|
|
1729
|
+
...models === void 0 || models.length === 0 ? {} : { models: [...models] }
|
|
1718
1730
|
};
|
|
1719
1731
|
const response = await fetch(`${url}${WORKERS_PATH}${WorkerDoor.Hello}`, {
|
|
1720
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
|
@@ -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,7 +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>>;
|
|
101
|
-
|
|
107
|
+
models: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
108
|
+
provider: z.ZodString;
|
|
109
|
+
id: z.ZodString;
|
|
110
|
+
}, z.core.$strip>>>;
|
|
102
111
|
}, z.core.$strip>;
|
|
103
112
|
export type WorkerHello = z.infer<typeof workerHelloSchema>;
|
|
104
113
|
/** A call to one of the router's servers, made for the run. */
|
package/types/worker/lib.d.ts
CHANGED
|
@@ -8,5 +8,6 @@
|
|
|
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
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/worker.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { WorkerSettings } from '../server/core/defaults.ts';
|
|
2
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';
|
|
@@ -33,10 +34,12 @@ export interface WorkerDeps {
|
|
|
33
34
|
/** Settings for `workdir`. */
|
|
34
35
|
workdir?: Partial<WorkdirSettings>;
|
|
35
36
|
/**
|
|
36
|
-
* The
|
|
37
|
-
* Said in the hello
|
|
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`.
|
|
38
41
|
*/
|
|
39
|
-
|
|
42
|
+
models?: readonly OfferedModel[];
|
|
40
43
|
}
|
|
41
44
|
/** A worker that is running. */
|
|
42
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;
|