@cogitator-ai/worker 0.8.9 → 0.9.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 +54 -14
- package/dist/distributed-swarm-worker.d.ts +12 -0
- package/dist/distributed-swarm-worker.d.ts.map +1 -1
- package/dist/distributed-swarm-worker.js +51 -1
- package/dist/distributed-swarm-worker.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/processors/agent.d.ts +5 -13
- package/dist/processors/agent.d.ts.map +1 -1
- package/dist/processors/agent.js +20 -44
- package/dist/processors/agent.js.map +1 -1
- package/dist/processors/shared.d.ts +10 -38
- package/dist/processors/shared.d.ts.map +1 -1
- package/dist/processors/shared.js +9 -62
- package/dist/processors/shared.js.map +1 -1
- package/dist/processors/swarm-agent.d.ts +3 -3
- package/dist/processors/swarm-agent.d.ts.map +1 -1
- package/dist/processors/swarm-agent.js +5 -10
- package/dist/processors/swarm-agent.js.map +1 -1
- package/dist/processors/swarm.d.ts +11 -3
- package/dist/processors/swarm.d.ts.map +1 -1
- package/dist/processors/swarm.js +73 -7
- package/dist/processors/swarm.js.map +1 -1
- package/dist/processors/workflow.d.ts +2 -2
- package/dist/processors/workflow.d.ts.map +1 -1
- package/dist/processors/workflow.js +53 -28
- package/dist/processors/workflow.js.map +1 -1
- package/dist/queue.d.ts +36 -3
- package/dist/queue.d.ts.map +1 -1
- package/dist/queue.js +52 -2
- package/dist/queue.js.map +1 -1
- package/dist/serialize.d.ts +11 -6
- package/dist/serialize.d.ts.map +1 -1
- package/dist/serialize.js +10 -30
- package/dist/serialize.js.map +1 -1
- package/dist/types.d.ts +64 -63
- package/dist/types.d.ts.map +1 -1
- package/dist/worker.d.ts +18 -3
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +57 -10
- package/dist/worker.js.map +1 -1
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -133,7 +133,7 @@ interface QueueConfig {
|
|
|
133
133
|
|
|
134
134
|
**Agent Jobs:**
|
|
135
135
|
|
|
136
|
-
The simplest way is to serialize an agent you already have. `serializeAgent` keeps
|
|
136
|
+
The simplest way is to serialize an agent you already have. `serializeAgent` writes it in the agent wire format of `@cogitator-ai/core` (`toAgentWire`), shared by agent, workflow and swarm jobs and by distributed swarm turns. It keeps the whole configuration (model and provider, instructions, sampling, stop sequences, reasoning effort, response format, iteration limit, timeout), the tool schemas and every agent it can hand over to, and turns a Zod response schema into JSON Schema, so the worker validates the structured answer the same way an in-process run does. A config with a key the worker does not know is refused rather than run without that setting:
|
|
137
137
|
|
|
138
138
|
```typescript
|
|
139
139
|
import { Agent } from '@cogitator-ai/core';
|
|
@@ -184,7 +184,7 @@ const job = await queue.addAgentJob(agentConfig, 'Research quantum computing', {
|
|
|
184
184
|
});
|
|
185
185
|
```
|
|
186
186
|
|
|
187
|
-
The worker routes `model` exactly like the same agent in-process: a prefix that names a built-in provider, a backend in the worker Cogitator's `llm.backends` or a registered plugin picks that provider, so `'openrouter/deepseek/deepseek-v4-pro'` runs on an `openrouter` backend whatever `provider` says. `provider` (optional, any provider the worker routes to, custom backends and plugins included) is prepended only to a model whose prefix names none, such as `'meta-llama/llama-4-scout'` with `provider: 'openrouter'`. Without `provider` such a model runs on the worker's `llm.defaultProvider`, and a `provider` the worker cannot route to fails the job.
|
|
187
|
+
The worker routes `model` exactly like the same agent in-process: a prefix that names a built-in provider, a backend in the worker Cogitator's `llm.backends` or a registered plugin picks that provider, so `'openrouter/deepseek/deepseek-v4-pro'` runs on an `openrouter` backend whatever `provider` says. `provider` (optional, any provider the worker routes to, custom backends and plugins included) is prepended only to a model whose prefix names none, such as `'meta-llama/llama-4-scout'` with `provider: 'openrouter'`. Without `provider` such a model runs on the worker's `llm.defaultProvider`, and a `provider` the worker cannot route to fails the job. `serializeAgent` sends an agent with an explicit `provider` as `<provider>/<model>`, so it takes the same route on the worker as in-process.
|
|
188
188
|
|
|
189
189
|
**Workflow Jobs:**
|
|
190
190
|
|
|
@@ -199,7 +199,7 @@ const workflowConfig: SerializedWorkflow = {
|
|
|
199
199
|
id: 'classify',
|
|
200
200
|
type: 'agent',
|
|
201
201
|
config: {
|
|
202
|
-
agentConfig: classifierAgent, // SerializedAgent
|
|
202
|
+
agentConfig: classifierAgent, // SerializedAgent; a json_schema agent writes its structured answer
|
|
203
203
|
prompt: 'Classify this ticket as BUG or QUESTION: {{ticket}}',
|
|
204
204
|
outputKey: 'category',
|
|
205
205
|
},
|
|
@@ -252,13 +252,13 @@ await queue.addSwarmJob(swarmConfig, 'Write an article about AI', {
|
|
|
252
252
|
});
|
|
253
253
|
```
|
|
254
254
|
|
|
255
|
-
| Topology | Swarm strategy | Notes
|
|
256
|
-
| --------------- | -------------- |
|
|
257
|
-
| `sequential` | `pipeline` | One stage per agent, in order
|
|
258
|
-
| `hierarchical` | `hierarchical` | `coordinator` is required and becomes the supervisor
|
|
259
|
-
| `collaborative` | `
|
|
260
|
-
| `debate` | `debate` | `maxRounds` rounds, `coordinator` moderates
|
|
261
|
-
| `voting` | `consensus` | `consensusThreshold`, `maxRounds
|
|
255
|
+
| Topology | Swarm strategy | Notes |
|
|
256
|
+
| --------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
257
|
+
| `sequential` | `pipeline` | One stage per agent, in order |
|
|
258
|
+
| `hierarchical` | `hierarchical` | `coordinator` is required and becomes the supervisor |
|
|
259
|
+
| `collaborative` | `pipeline` | Every agent contributes in each of `maxRounds` rounds (default 3), seeing all contributions so far, `coordinator` combines them, otherwise the last contribution is the answer |
|
|
260
|
+
| `debate` | `debate` | `maxRounds` rounds, `coordinator` moderates |
|
|
261
|
+
| `voting` | `consensus` | `consensusThreshold`, `maxRounds`, `coordinator` breaks ties |
|
|
262
262
|
|
|
263
263
|
### Queue Methods
|
|
264
264
|
|
|
@@ -269,6 +269,8 @@ const state: JobState = await queue.getJobState('job-id');
|
|
|
269
269
|
// 'waiting' | 'prioritized' | 'delayed' | 'active' | 'completed' | 'failed'
|
|
270
270
|
// | 'waiting-children' | 'unknown'
|
|
271
271
|
|
|
272
|
+
await queue.resumeAgentJob(agentConfig, pausedResult, { decisions }); // see Job Results
|
|
273
|
+
|
|
272
274
|
const metrics = await queue.getMetrics();
|
|
273
275
|
// { waiting, active, completed, failed, delayed, depth, workerCount }
|
|
274
276
|
|
|
@@ -368,13 +370,19 @@ const metrics = await pool.getMetrics(await queue.getMetrics());
|
|
|
368
370
|
// Job duration histogram and per-type counters
|
|
369
371
|
pool.metrics.format(await pool.getMetrics(await queue.getMetrics()));
|
|
370
372
|
|
|
371
|
-
//
|
|
373
|
+
// Cancel a job this pool is running: its agent runs stop and the job fails without retries
|
|
374
|
+
pool.cancelJob(jobId, 'No longer needed');
|
|
375
|
+
|
|
376
|
+
// Graceful shutdown (waits up to 30s for active jobs, then aborts them and force-closes;
|
|
377
|
+
// BullMQ hands the aborted jobs to another worker once their locks expire)
|
|
372
378
|
await pool.stop(30000);
|
|
373
379
|
|
|
374
|
-
// Force shutdown
|
|
380
|
+
// Force shutdown (aborts the jobs still running)
|
|
375
381
|
await pool.forceStop();
|
|
376
382
|
```
|
|
377
383
|
|
|
384
|
+
Every job gets the abort signal BullMQ gives it, down to each agent run, workflow node and swarm turn of the job. The processors take it too: `processAgentJob(payload, runtime, { signal })`, and the same third argument for `processWorkflowJob`, `processSwarmJob` and `executeSwarmAgentJob` (`processSwarmAgentJob` reads `signal` from its options).
|
|
385
|
+
|
|
378
386
|
---
|
|
379
387
|
|
|
380
388
|
## Job Processors
|
|
@@ -430,6 +438,7 @@ const worker = new DistributedSwarmWorker(
|
|
|
430
438
|
{
|
|
431
439
|
onJobCompleted: (job) => console.log('done', job.agentName),
|
|
432
440
|
onJobFailed: (job, error) => console.error(job.agentName, error.message),
|
|
441
|
+
onJobCancelled: (job) => console.log('the swarm gave up on', job.jobId),
|
|
433
442
|
onError: (error) => console.error(error),
|
|
434
443
|
}
|
|
435
444
|
);
|
|
@@ -438,7 +447,7 @@ await worker.start();
|
|
|
438
447
|
process.on('SIGTERM', () => void worker.stop()); // waits for in-flight turns
|
|
439
448
|
```
|
|
440
449
|
|
|
441
|
-
Failed turns are reported back to the swarm as errors, so the swarm's own `errorHandling` (retry, failover, skip) applies. Each turn runs on the model its agent would use in-process, routed by the worker's `cogitator`, so give the worker the same `llm.backends`, plugins and provider keys as the process that runs the swarm.
|
|
450
|
+
Failed turns are reported back to the swarm as errors, so the swarm's own `errorHandling` (retry, failover, skip) applies. A turn the swarm gave up on (its run aborted or timed out, the swarm closed, another attempt answered) is skipped, or aborted while it runs (checked every `cancelCheckInterval` ms, default 1000), and reported to `onJobCancelled` instead of being published. Each turn runs on the model its agent would use in-process, routed by the worker's `cogitator`, so give the worker the same `llm.backends`, plugins and provider keys as the process that runs the swarm.
|
|
442
451
|
|
|
443
452
|
---
|
|
444
453
|
|
|
@@ -467,6 +476,10 @@ interface AgentJobResult {
|
|
|
467
476
|
input: unknown;
|
|
468
477
|
output: unknown;
|
|
469
478
|
}[];
|
|
479
|
+
/** 'paused' when tool calls wait for approval: output is then not the answer */
|
|
480
|
+
status?: 'completed' | 'paused';
|
|
481
|
+
pendingApprovals?: ToolApprovalRequest[]; // the calls a paused run waits on
|
|
482
|
+
checkpoint?: RunCheckpoint; // what a paused run continues from, see resumeAgentJob
|
|
470
483
|
/** @deprecated use usage */
|
|
471
484
|
tokenUsage?: {
|
|
472
485
|
prompt: number;
|
|
@@ -476,6 +489,18 @@ interface AgentJobResult {
|
|
|
476
489
|
}
|
|
477
490
|
```
|
|
478
491
|
|
|
492
|
+
A run that paused for tool approvals completes its job with `status: 'paused'`. Continue it with the decisions as a new job, on any worker:
|
|
493
|
+
|
|
494
|
+
```typescript
|
|
495
|
+
if (result.status === 'paused') {
|
|
496
|
+
await queue.resumeAgentJob(agentConfig, result, {
|
|
497
|
+
decisions: { [result.pendingApprovals![0].toolCallId]: { approved: true } },
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
Workflow and swarm jobs cannot wait for a decision: when one of their agents pauses, the job fails without retries with an `AgentRunPausedError`.
|
|
503
|
+
|
|
479
504
|
### Workflow Job Result
|
|
480
505
|
|
|
481
506
|
```typescript
|
|
@@ -643,19 +668,34 @@ Jobs use serialized configurations that can be stored in Redis.
|
|
|
643
668
|
|
|
644
669
|
### SerializedAgent
|
|
645
670
|
|
|
671
|
+
`SerializedAgent` is `AgentWireConfig` from `@cogitator-ai/types`:
|
|
672
|
+
|
|
646
673
|
```typescript
|
|
647
674
|
interface SerializedAgent {
|
|
675
|
+
id?: string;
|
|
648
676
|
name: string;
|
|
677
|
+
description?: string;
|
|
649
678
|
instructions: string;
|
|
650
679
|
model: string; // routed like in-process: a known provider prefix picks the provider
|
|
651
|
-
provider?: LLMBackendProvider; //
|
|
680
|
+
provider?: LLMBackendProvider; // used when model names no provider the worker routes to
|
|
652
681
|
temperature?: number;
|
|
682
|
+
topP?: number;
|
|
653
683
|
maxTokens?: number;
|
|
684
|
+
stopSequences?: string[];
|
|
685
|
+
responseFormat?:
|
|
686
|
+
{ type: 'text' } | { type: 'json' } | { type: 'json_schema'; schema: JSONSchema };
|
|
687
|
+
reasoning?: ReasoningConfig;
|
|
654
688
|
maxIterations?: number;
|
|
689
|
+
onIterationLimit?: 'answer' | 'stop';
|
|
690
|
+
timeout?: number;
|
|
655
691
|
tools: ToolSchema[]; // resolved by name against the worker's tools
|
|
692
|
+
handoffs?: { agent: string; toolName?: string; description?: string }[];
|
|
693
|
+
handoffAgents?: Record<string, Omit<SerializedAgent, 'handoffAgents'>>; // handoff targets by name
|
|
656
694
|
}
|
|
657
695
|
```
|
|
658
696
|
|
|
697
|
+
Unknown keys are refused. Agent job results (`AgentJobResult`) carry `output`, `structured`, `structuredError`, `reasoning`, `usage` (tokens, `cost`, `duration`, reasoning and cache tokens), `toolCalls` with their outputs, and `truncated`, `blocked` and `iterationLimitReached`.
|
|
698
|
+
|
|
659
699
|
### SerializedWorkflow
|
|
660
700
|
|
|
661
701
|
```typescript
|
|
@@ -22,11 +22,19 @@ export interface DistributedSwarmWorkerConfig extends WorkerRuntime {
|
|
|
22
22
|
concurrency?: number;
|
|
23
23
|
/** Seconds a blocking poll waits for new jobs before re-checking state (default: 1) */
|
|
24
24
|
pollTimeout?: number;
|
|
25
|
+
/**
|
|
26
|
+
* How often, in ms, a running turn checks whether the coordinator cancelled it (its run
|
|
27
|
+
* aborted or timed out, the swarm closed, or another attempt answered), default 1000. A
|
|
28
|
+
* cancelled turn is aborted and its result is not published
|
|
29
|
+
*/
|
|
30
|
+
cancelCheckInterval?: number;
|
|
25
31
|
}
|
|
26
32
|
export interface DistributedSwarmWorkerEvents {
|
|
27
33
|
onJobStarted?: (job: SwarmAgentJobPayload) => void;
|
|
28
34
|
onJobCompleted?: (job: SwarmAgentJobPayload, result: SwarmAgentJobResult) => void;
|
|
29
35
|
onJobFailed?: (job: SwarmAgentJobPayload, error: Error) => void;
|
|
36
|
+
/** The coordinator gave up on the turn: it was skipped, or aborted while it ran */
|
|
37
|
+
onJobCancelled?: (job: SwarmAgentJobPayload) => void;
|
|
30
38
|
onError?: (error: Error) => void;
|
|
31
39
|
}
|
|
32
40
|
export declare class DistributedSwarmWorker {
|
|
@@ -52,5 +60,9 @@ export declare class DistributedSwarmWorker {
|
|
|
52
60
|
getActiveJobCount(): number;
|
|
53
61
|
private consume;
|
|
54
62
|
private handle;
|
|
63
|
+
/** Whether the coordinator gave up on the job (only coordinators that name a cancel set). */
|
|
64
|
+
private isCancelled;
|
|
65
|
+
/** Abort `controller` once the coordinator cancels the job while it runs. */
|
|
66
|
+
private watchCancellation;
|
|
55
67
|
}
|
|
56
68
|
//# sourceMappingURL=distributed-swarm-worker.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"distributed-swarm-worker.d.ts","sourceRoot":"","sources":["../src/distributed-swarm-worker.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OA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await this.isCancelled(job)) {
|
|
135
|
+
this.events.onJobCancelled?.(job);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
134
138
|
this.events.onJobStarted?.(job);
|
|
135
|
-
const
|
|
139
|
+
const controller = new AbortController();
|
|
140
|
+
const watch = this.watchCancellation(job, controller);
|
|
141
|
+
let result;
|
|
142
|
+
try {
|
|
143
|
+
result = await executeSwarmAgentJob(job, this.runtime, { signal: controller.signal });
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
clearInterval(watch);
|
|
147
|
+
}
|
|
148
|
+
if (controller.signal.aborted) {
|
|
149
|
+
this.events.onJobCancelled?.(job);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
136
152
|
try {
|
|
137
153
|
await this.publisher.publish(job.stateKeys.results, JSON.stringify(result));
|
|
138
154
|
}
|
|
@@ -147,5 +163,39 @@ export class DistributedSwarmWorker {
|
|
|
147
163
|
this.events.onJobCompleted?.(job, result);
|
|
148
164
|
}
|
|
149
165
|
}
|
|
166
|
+
/** Whether the coordinator gave up on the job (only coordinators that name a cancel set). */
|
|
167
|
+
async isCancelled(job) {
|
|
168
|
+
const key = job.stateKeys.cancelled;
|
|
169
|
+
if (!key || !this.publisher)
|
|
170
|
+
return false;
|
|
171
|
+
try {
|
|
172
|
+
return (await this.publisher.sismember(key, job.jobId)) === 1;
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
this.events.onError?.(error instanceof Error ? error : new Error(toErrorMessage(error)));
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/** Abort `controller` once the coordinator cancels the job while it runs. */
|
|
180
|
+
watchCancellation(job, controller) {
|
|
181
|
+
if (!job.stateKeys.cancelled)
|
|
182
|
+
return undefined;
|
|
183
|
+
const interval = Math.max(10, this.config.cancelCheckInterval ?? 1000);
|
|
184
|
+
let checking = false;
|
|
185
|
+
return setInterval(() => {
|
|
186
|
+
if (checking || controller.signal.aborted)
|
|
187
|
+
return;
|
|
188
|
+
checking = true;
|
|
189
|
+
void this.isCancelled(job)
|
|
190
|
+
.then((cancelled) => {
|
|
191
|
+
if (cancelled) {
|
|
192
|
+
controller.abort(new Error(`Swarm turn ${job.jobId} was cancelled by its coordinator`));
|
|
193
|
+
}
|
|
194
|
+
})
|
|
195
|
+
.finally(() => {
|
|
196
|
+
checking = false;
|
|
197
|
+
});
|
|
198
|
+
}, interval);
|
|
199
|
+
}
|
|
150
200
|
}
|
|
151
201
|
//# sourceMappingURL=distributed-swarm-worker.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"distributed-swarm-worker.js","sourceRoot":"","sources":["../src/distributed-swarm-worker.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAExD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"distributed-swarm-worker.js","sourceRoot":"","sources":["../src/distributed-swarm-worker.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAExD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAmC/C,SAAS,eAAe,CAAC,KAAc;IACrC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC9D,MAAM,GAAG,GAAG,KAAsC,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AG,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;QAChE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACzF,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED,6EAA6E;IACrE,iBAAiB,CACvB,GAAyB,EACzB,UAA2B;QAE3B,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,SAAS;YAAE,OAAO,SAAS,CAAC;QAC/C,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,MAAM,CAAC,mBAAmB,IAAI,IAAI,CAAC,CAAC;QACvE,IAAI,QAAQ,GAAG,KAAK,CAAC;QACrB,OAAO,WAAW,CAAC,GAAG,EAAE;YACtB,IAAI,QAAQ,IAAI,UAAU,CAAC,MAAM,CAAC,OAAO;gBAAE,OAAO;YAClD,QAAQ,GAAG,IAAI,CAAC;YAChB,KAAK,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC;iBACvB,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE;gBAClB,IAAI,SAAS,EAAE,CAAC;oBACd,UAAU,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,cAAc,GAAG,CAAC,KAAK,mCAAmC,CAAC,CAAC,CAAC;gBAC1F,CAAC;YACH,CAAC,CAAC;iBACD,OAAO,CAAC,GAAG,EAAE;gBACZ,QAAQ,GAAG,KAAK,CAAC;YACnB,CAAC,CAAC,CAAC;QACP,CAAC,EAAE,QAAQ,CAAC,CAAC;IACf,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -15,5 +15,5 @@ export { formatPrometheusMetrics, DurationHistogram, MetricsCollector } from './
|
|
|
15
15
|
export { serializeAgent, serializeResponseFormat } from './serialize.js';
|
|
16
16
|
export { resolveRedisOptions, type RedisConnectionOptions } from './connection.js';
|
|
17
17
|
export { processAgentJob, processWorkflowJob, processSwarmJob, processSwarmAgentJob, executeSwarmAgentJob, buildSwarmConfig, validateWorkflow, type SwarmAgentJobOptions, type SwarmResultPublisher, } from './processors/index.js';
|
|
18
|
-
export type { SerializedAgent, SerializedResponseFormat, AgentJobUsage, SerializedWorkflow, SerializedWorkflowNode, SerializedWorkflowEdge, AgentNodeConfig, TransformNodeConfig, TransformOperation, ConditionNodeConfig, ConditionOperator, SerializedSwarm, JobPayload, AgentJobPayload, WorkflowJobPayload, SwarmJobPayload, SwarmAgentJobPayload, JobResult, AgentJobResult, WorkflowJobResult, SwarmJobResult, SwarmAgentJobResult, QueueConfig, WorkerConfig, WorkerRuntime, QueueMetrics, JobState, } from './types.js';
|
|
18
|
+
export type { SerializedAgent, SerializedResponseFormat, AgentJobUsage, SerializedWorkflow, SerializedWorkflowNode, SerializedWorkflowEdge, AgentNodeConfig, TransformNodeConfig, TransformOperation, ConditionNodeConfig, ConditionOperator, SerializedSwarm, SwarmTopology, JobPayload, AgentJobPayload, AgentJobResume, WorkflowJobPayload, SwarmJobPayload, SwarmAgentJobPayload, JobResult, AgentJobResult, WorkflowJobResult, SwarmJobResult, SwarmAgentJobResult, QueueConfig, WorkerConfig, WorkerRuntime, JobExecutionOptions, QueueMetrics, JobState, } from './types.js';
|
|
19
19
|
//# 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;;;;;;;;;GASG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC7D,OAAO,EACL,sBAAsB,EACtB,KAAK,4BAA4B,EACjC,KAAK,4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,aAAa,CAAC;AACtE,OAAO,EAAE,mBAAmB,EAAE,KAAK,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAEhF,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,eAAe,EACf,oBAAoB,EACpB,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,KAAK,oBAAoB,EACzB,KAAK,oBAAoB,GAC1B,MAAM,oBAAoB,CAAC;AAE5B,YAAY,EACV,eAAe,EACf,wBAAwB,EACxB,aAAa,EACb,kBAAkB,EAClB,sBAAsB,EACtB,sBAAsB,EACtB,eAAe,EACf,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,EACjB,eAAe,EACf,aAAa,EACb,UAAU,EACV,eAAe,EACf,cAAc,EACd,kBAAkB,EAClB,eAAe,EACf,oBAAoB,EACpB,SAAS,EACT,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,mBAAmB,EACnB,WAAW,EACX,YAAY,EACZ,aAAa,EACb,mBAAmB,EACnB,YAAY,EACZ,QAAQ,GACT,MAAM,SAAS,CAAC"}
|
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Agent job processor
|
|
3
3
|
*
|
|
4
|
-
* Recreates an Agent from
|
|
4
|
+
* Recreates an Agent from its wire form and runs it, or resumes the run a previous job paused.
|
|
5
|
+
* A paused run is a result, not a failure: it carries `status: 'paused'` and its checkpoint.
|
|
5
6
|
*/
|
|
6
|
-
import type { AgentJobPayload, AgentJobResult, WorkerRuntime } from '../types.js';
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
role: string;
|
|
10
|
-
toolCallId?: string;
|
|
11
|
-
content: unknown;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Tool results are recorded as `tool` messages; recover the output for a tool call.
|
|
15
|
-
*/
|
|
16
|
-
export declare function findToolOutput(messages: readonly ToolMessageLike[], toolCallId: string): unknown;
|
|
17
|
-
export {};
|
|
7
|
+
import type { AgentJobPayload, AgentJobResult, JobExecutionOptions, WorkerRuntime } from '../types.js';
|
|
8
|
+
export { findToolOutput } from '@cogitator-ai/core';
|
|
9
|
+
export declare function processAgentJob(payload: AgentJobPayload, runtime?: WorkerRuntime, execution?: JobExecutionOptions): Promise<AgentJobResult>;
|
|
18
10
|
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/processors/agent.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/processors/agent.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAGpG,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,wBAAsB,eAAe,CACnC,OAAO,EAAE,eAAe,EACxB,OAAO,GAAE,aAAkB,EAC3B,SAAS,GAAE,mBAAwB,GAClC,OAAO,CAAC,cAAc,CAAC,CA4BzB"}
|
package/dist/processors/agent.js
CHANGED
|
@@ -1,40 +1,32 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Agent job processor
|
|
3
3
|
*
|
|
4
|
-
* Recreates an Agent from
|
|
4
|
+
* Recreates an Agent from its wire form and runs it, or resumes the run a previous job paused.
|
|
5
|
+
* A paused run is a result, not a failure: it carries `status: 'paused'` and its checkpoint.
|
|
5
6
|
*/
|
|
7
|
+
import { toAgentWireRunResult } from '@cogitator-ai/core';
|
|
6
8
|
import { createAgentFromConfig, resolveRuntime } from './shared.js';
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
+
export { findToolOutput } from '@cogitator-ai/core';
|
|
10
|
+
export async function processAgentJob(payload, runtime = {}, execution = {}) {
|
|
11
|
+
const { agentConfig, input, threadId, userId, resume } = payload;
|
|
9
12
|
const resolved = resolveRuntime(runtime);
|
|
10
13
|
const agent = createAgentFromConfig(agentConfig, resolved);
|
|
11
|
-
const result =
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const result = resume
|
|
15
|
+
? await resolved.cogitator.resume(agent, resume.checkpoint ?? threadId, {
|
|
16
|
+
...(resume.decisions && { decisions: resume.decisions }),
|
|
17
|
+
...(resume.defaultDecision && { defaultDecision: resume.defaultDecision }),
|
|
18
|
+
...(userId !== undefined && { userId }),
|
|
19
|
+
...(execution.signal && { signal: execution.signal }),
|
|
20
|
+
})
|
|
21
|
+
: await resolved.cogitator.run(agent, {
|
|
22
|
+
input,
|
|
23
|
+
threadId,
|
|
24
|
+
...(userId !== undefined && { userId }),
|
|
25
|
+
...(execution.signal && { signal: execution.signal }),
|
|
26
|
+
});
|
|
16
27
|
return {
|
|
17
28
|
type: 'agent',
|
|
18
|
-
|
|
19
|
-
...(result.structured !== undefined && { structured: result.structured }),
|
|
20
|
-
...(result.reasoning && { reasoning: result.reasoning }),
|
|
21
|
-
usage: {
|
|
22
|
-
inputTokens: result.usage.inputTokens,
|
|
23
|
-
outputTokens: result.usage.outputTokens,
|
|
24
|
-
totalTokens: result.usage.totalTokens,
|
|
25
|
-
cost: result.usage.cost,
|
|
26
|
-
...(result.usage.reasoningTokens !== undefined && {
|
|
27
|
-
reasoningTokens: result.usage.reasoningTokens,
|
|
28
|
-
}),
|
|
29
|
-
...(result.usage.cachedInputTokens !== undefined && {
|
|
30
|
-
cachedInputTokens: result.usage.cachedInputTokens,
|
|
31
|
-
}),
|
|
32
|
-
},
|
|
33
|
-
toolCalls: result.toolCalls.map((tc) => ({
|
|
34
|
-
name: tc.name,
|
|
35
|
-
input: tc.arguments,
|
|
36
|
-
output: findToolOutput(result.messages, tc.id),
|
|
37
|
-
})),
|
|
29
|
+
...toAgentWireRunResult(result),
|
|
38
30
|
tokenUsage: {
|
|
39
31
|
prompt: result.usage.inputTokens,
|
|
40
32
|
completion: result.usage.outputTokens,
|
|
@@ -42,20 +34,4 @@ export async function processAgentJob(payload, runtime = {}) {
|
|
|
42
34
|
},
|
|
43
35
|
};
|
|
44
36
|
}
|
|
45
|
-
/**
|
|
46
|
-
* Tool results are recorded as `tool` messages; recover the output for a tool call.
|
|
47
|
-
*/
|
|
48
|
-
export function findToolOutput(messages, toolCallId) {
|
|
49
|
-
const message = messages.find((m) => m.role === 'tool' && m.toolCallId === toolCallId);
|
|
50
|
-
if (!message)
|
|
51
|
-
return undefined;
|
|
52
|
-
if (typeof message.content !== 'string')
|
|
53
|
-
return message.content;
|
|
54
|
-
try {
|
|
55
|
-
return JSON.parse(message.content);
|
|
56
|
-
}
|
|
57
|
-
catch {
|
|
58
|
-
return message.content;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
37
|
//# sourceMappingURL=agent.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/processors/agent.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/processors/agent.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE1D,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAEpE,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,OAAwB,EACxB,UAAyB,EAAE,EAC3B,YAAiC,EAAE;IAEnC,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAEjE,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,CAAC,CAAC;IACzC,MAAM,KAAK,GAAG,qBAAqB,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;IAC3D,MAAM,MAAM,GAAG,MAAM;QACnB,CAAC,CAAC,MAAM,QAAQ,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,UAAU,IAAI,QAAQ,EAAE;YACpE,GAAG,CAAC,MAAM,CAAC,SAAS,IAAI,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE,CAAC;YACxD,GAAG,CAAC,MAAM,CAAC,eAAe,IAAI,EAAE,eAAe,EAAE,MAAM,CAAC,eAAe,EAAE,CAAC;YAC1E,GAAG,CAAC,MAAM,KAAK,SAAS,IAAI,EAAE,MAAM,EAAE,CAAC;YACvC,GAAG,CAAC,SAAS,CAAC,MAAM,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,CAAC;SACtD,CAAC;QACJ,CAAC,CAAC,MAAM,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,EAAE;YAClC,KAAK;YACL,QAAQ;YACR,GAAG,CAAC,MAAM,KAAK,SAAS,IAAI,EAAE,MAAM,EAAE,CAAC;YACvC,GAAG,CAAC,SAAS,CAAC,MAAM,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,CAAC;SACtD,CAAC,CAAC;IAEP,OAAO;QACL,IAAI,EAAE,OAAO;QACb,GAAG,oBAAoB,CAAC,MAAM,CAAC;QAC/B,UAAU,EAAE;YACV,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,WAAW;YAChC,UAAU,EAAE,MAAM,CAAC,KAAK,CAAC,YAAY;YACrC,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,WAAW;SAChC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -1,40 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
import type { SerializedResponseFormat, WorkerRuntime } from '../types.js';
|
|
4
|
-
/**
|
|
5
|
-
* Model string to run on `cogitator`, so a serialized agent routes like the same agent
|
|
6
|
-
* would in-process. A model whose prefix names a provider, backend or plugin the Cogitator
|
|
7
|
-
* routes to is used as-is. Otherwise `provider` is prepended, and without a provider the
|
|
8
|
-
* model stays unchanged and runs on the Cogitator's `llm.defaultProvider`.
|
|
9
|
-
*
|
|
10
|
-
* @throws Error when `provider` is needed but the Cogitator cannot route to it
|
|
11
|
-
*/
|
|
12
|
-
export declare function resolveModelString(model: string, provider: string | undefined, cogitator: Cogitator): string;
|
|
13
|
-
/**
|
|
14
|
-
* Resolve serialized tool references against the worker's tool registry.
|
|
15
|
-
* Fails when the agent needs tools this worker does not provide, instead of silently
|
|
16
|
-
* running the agent with fake tools.
|
|
17
|
-
*/
|
|
18
|
-
export declare function resolveTools(schemas: readonly ToolSchema[], available: readonly Tool[]): Tool[];
|
|
19
|
-
export interface SerializedAgentLike {
|
|
20
|
-
name: string;
|
|
21
|
-
instructions: string;
|
|
22
|
-
model: string;
|
|
23
|
-
provider?: string;
|
|
24
|
-
temperature?: number;
|
|
25
|
-
topP?: number;
|
|
26
|
-
maxTokens?: number;
|
|
27
|
-
maxIterations?: number;
|
|
28
|
-
onIterationLimit?: 'answer' | 'stop';
|
|
29
|
-
responseFormat?: SerializedResponseFormat;
|
|
30
|
-
reasoning?: ReasoningConfig;
|
|
31
|
-
tools: readonly ToolSchema[];
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* The agent's response format back from its queue form: JSON Schema becomes a Zod schema, so the
|
|
35
|
-
* runtime validates the structured output and asks for a repair as it does in-process.
|
|
36
|
-
*/
|
|
37
|
-
export declare function toResponseFormat(format: SerializedResponseFormat | undefined): ResponseFormat | undefined;
|
|
1
|
+
import { Cogitator, type Agent } from '@cogitator-ai/core';
|
|
2
|
+
import type { SerializedAgent, WorkerRuntime } from '../types.js';
|
|
38
3
|
/**
|
|
39
4
|
* Worker runtime with the Cogitator settled, so the agents of a job are routed by the
|
|
40
5
|
* same Cogitator that runs them.
|
|
@@ -43,6 +8,13 @@ export interface ResolvedRuntime extends WorkerRuntime {
|
|
|
43
8
|
cogitator: Cogitator;
|
|
44
9
|
}
|
|
45
10
|
export declare function resolveRuntime(runtime: WorkerRuntime): ResolvedRuntime;
|
|
46
|
-
|
|
11
|
+
/**
|
|
12
|
+
* The agent a job describes, ready to run on this worker: routed by the worker's Cogitator,
|
|
13
|
+
* with the worker's tool implementations (see `fromAgentWire`).
|
|
14
|
+
*
|
|
15
|
+
* @throws AgentWireError when the config is invalid, needs a tool the worker lacks or a
|
|
16
|
+
* provider it cannot route to
|
|
17
|
+
*/
|
|
18
|
+
export declare function createAgentFromConfig(config: SerializedAgent, runtime: ResolvedRuntime): Agent;
|
|
47
19
|
export declare function toErrorMessage(error: unknown): string;
|
|
48
20
|
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/processors/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/processors/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAiB,KAAK,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE/D;;;GAGG;AACH,MAAM,WAAW,eAAgB,SAAQ,aAAa;IACpD,SAAS,EAAE,SAAS,CAAC;CACtB;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,aAAa,GAAG,eAAe,CAEtE;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,eAAe,EAAE,OAAO,EAAE,eAAe,GAAG,KAAK,CAE9F;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAErD"}
|
|
@@ -1,69 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
/**
|
|
4
|
-
* Model string to run on `cogitator`, so a serialized agent routes like the same agent
|
|
5
|
-
* would in-process. A model whose prefix names a provider, backend or plugin the Cogitator
|
|
6
|
-
* routes to is used as-is. Otherwise `provider` is prepended, and without a provider the
|
|
7
|
-
* model stays unchanged and runs on the Cogitator's `llm.defaultProvider`.
|
|
8
|
-
*
|
|
9
|
-
* @throws Error when `provider` is needed but the Cogitator cannot route to it
|
|
10
|
-
*/
|
|
11
|
-
export function resolveModelString(model, provider, cogitator) {
|
|
12
|
-
const slash = model.indexOf('/');
|
|
13
|
-
if (slash > 0 && cogitator.knowsProvider(model.slice(0, slash)))
|
|
14
|
-
return model;
|
|
15
|
-
if (!provider)
|
|
16
|
-
return model;
|
|
17
|
-
if (!cogitator.knowsProvider(provider)) {
|
|
18
|
-
throw new Error(`Provider "${provider}" of model "${model}" is not available on this worker: ` +
|
|
19
|
-
'it is not a built-in provider, a backend in llm.backends or a registered plugin of the worker Cogitator.');
|
|
20
|
-
}
|
|
21
|
-
return `${provider}/${model}`;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Resolve serialized tool references against the worker's tool registry.
|
|
25
|
-
* Fails when the agent needs tools this worker does not provide, instead of silently
|
|
26
|
-
* running the agent with fake tools.
|
|
27
|
-
*/
|
|
28
|
-
export function resolveTools(schemas, available) {
|
|
29
|
-
if (schemas.length === 0)
|
|
30
|
-
return [];
|
|
31
|
-
const registry = new Map(available.map((t) => [t.name, t]));
|
|
32
|
-
const missing = schemas.map((s) => s.name).filter((name) => !registry.has(name));
|
|
33
|
-
if (missing.length > 0) {
|
|
34
|
-
throw new Error(`Tools not registered on this worker: ${missing.join(', ')}. ` +
|
|
35
|
-
'Pass their implementations via the worker `tools` option.');
|
|
36
|
-
}
|
|
37
|
-
return schemas.map((schema) => registry.get(schema.name));
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* The agent's response format back from its queue form: JSON Schema becomes a Zod schema, so the
|
|
41
|
-
* runtime validates the structured output and asks for a repair as it does in-process.
|
|
42
|
-
*/
|
|
43
|
-
export function toResponseFormat(format) {
|
|
44
|
-
if (!format)
|
|
45
|
-
return undefined;
|
|
46
|
-
if (format.type !== 'json_schema')
|
|
47
|
-
return format;
|
|
48
|
-
return { type: 'json_schema', schema: z.fromJSONSchema(format.schema) };
|
|
49
|
-
}
|
|
1
|
+
import { Cogitator, fromAgentWire } from '@cogitator-ai/core';
|
|
50
2
|
export function resolveRuntime(runtime) {
|
|
51
3
|
return { ...runtime, cogitator: runtime.cogitator ?? new Cogitator() };
|
|
52
4
|
}
|
|
5
|
+
/**
|
|
6
|
+
* The agent a job describes, ready to run on this worker: routed by the worker's Cogitator,
|
|
7
|
+
* with the worker's tool implementations (see `fromAgentWire`).
|
|
8
|
+
*
|
|
9
|
+
* @throws AgentWireError when the config is invalid, needs a tool the worker lacks or a
|
|
10
|
+
* provider it cannot route to
|
|
11
|
+
*/
|
|
53
12
|
export function createAgentFromConfig(config, runtime) {
|
|
54
|
-
return
|
|
55
|
-
name: config.name,
|
|
56
|
-
model: resolveModelString(config.model, config.provider, runtime.cogitator),
|
|
57
|
-
instructions: config.instructions,
|
|
58
|
-
temperature: config.temperature,
|
|
59
|
-
topP: config.topP,
|
|
60
|
-
maxTokens: config.maxTokens,
|
|
61
|
-
maxIterations: config.maxIterations,
|
|
62
|
-
...(config.onIterationLimit && { onIterationLimit: config.onIterationLimit }),
|
|
63
|
-
responseFormat: toResponseFormat(config.responseFormat),
|
|
64
|
-
reasoning: config.reasoning,
|
|
65
|
-
tools: resolveTools(config.tools, runtime.tools ?? []),
|
|
66
|
-
});
|
|
13
|
+
return fromAgentWire(config, runtime);
|
|
67
14
|
}
|
|
68
15
|
export function toErrorMessage(error) {
|
|
69
16
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../src/processors/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../src/processors/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAc,MAAM,oBAAoB,CAAC;AAW1E,MAAM,UAAU,cAAc,CAAC,OAAsB;IACnD,OAAO,EAAE,GAAG,OAAO,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,IAAI,SAAS,EAAE,EAAE,CAAC;AACzE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAuB,EAAE,OAAwB;IACrF,OAAO,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAChE,CAAC"}
|
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
* the job id so the swarm coordinator can match it; failures are reported as results with
|
|
6
6
|
* an `error` field instead of being thrown.
|
|
7
7
|
*/
|
|
8
|
-
import type { SwarmAgentJobPayload, SwarmAgentJobResult, WorkerRuntime } from '../types.js';
|
|
9
|
-
export declare function executeSwarmAgentJob(payload: SwarmAgentJobPayload, runtime?: WorkerRuntime): Promise<SwarmAgentJobResult>;
|
|
8
|
+
import type { JobExecutionOptions, SwarmAgentJobPayload, SwarmAgentJobResult, WorkerRuntime } from '../types.js';
|
|
9
|
+
export declare function executeSwarmAgentJob(payload: SwarmAgentJobPayload, runtime?: WorkerRuntime, execution?: JobExecutionOptions): Promise<SwarmAgentJobResult>;
|
|
10
10
|
/**
|
|
11
11
|
* Publishes a job result on the channel the swarm coordinator listens to.
|
|
12
12
|
*/
|
|
13
13
|
export interface SwarmResultPublisher {
|
|
14
14
|
publish(channel: string, message: string): Promise<unknown>;
|
|
15
15
|
}
|
|
16
|
-
export interface SwarmAgentJobOptions extends WorkerRuntime {
|
|
16
|
+
export interface SwarmAgentJobOptions extends WorkerRuntime, JobExecutionOptions {
|
|
17
17
|
/** Connection used to publish the result back to the coordinator */
|
|
18
18
|
publisher: SwarmResultPublisher;
|
|
19
19
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swarm-agent.d.ts","sourceRoot":"","sources":["../../src/processors/swarm-agent.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;
|
|
1
|
+
{"version":3,"file":"swarm-agent.d.ts","sourceRoot":"","sources":["../../src/processors/swarm-agent.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EACV,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,aAAa,EACd,MAAM,UAAU,CAAC;AAGlB,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,oBAAoB,EAC7B,OAAO,GAAE,aAAkB,EAC3B,SAAS,GAAE,mBAAwB,GAClC,OAAO,CAAC,mBAAmB,CAAC,CAwC9B;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC7D;AAED,MAAM,WAAW,oBAAqB,SAAQ,aAAa,EAAE,mBAAmB;IAC9E,oEAAoE;IACpE,SAAS,EAAE,oBAAoB,CAAC;IAChC;;;OAGG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;;GAGG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,oBAAoB,EAC7B,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,mBAAmB,CAAC,CAa9B"}
|