@radaros/queue 0.3.1 → 0.3.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radaros/queue",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -20,7 +20,7 @@
20
20
  "typescript": "^5.6.0"
21
21
  },
22
22
  "peerDependencies": {
23
- "@radaros/core": "^0.3.1",
23
+ "@radaros/core": "^0.3.3",
24
24
  "bullmq": "^5.0.0",
25
25
  "ioredis": "^5.0.0"
26
26
  },
package/dist/index.d.ts DELETED
@@ -1,89 +0,0 @@
1
- import { RunOutput, Agent, Workflow, EventBus } from '@radaros/core';
2
-
3
- type AgentJobPayload = {
4
- type: "agent";
5
- agentName: string;
6
- input: string;
7
- sessionId?: string;
8
- userId?: string;
9
- };
10
- type WorkflowJobPayload = {
11
- type: "workflow";
12
- workflowName: string;
13
- initialState?: Record<string, unknown>;
14
- sessionId?: string;
15
- };
16
- type JobPayload = AgentJobPayload | WorkflowJobPayload;
17
- type JobStatus = {
18
- jobId: string;
19
- state: "waiting" | "active" | "completed" | "failed" | "delayed";
20
- progress?: number;
21
- result?: RunOutput;
22
- error?: string;
23
- createdAt: Date;
24
- processedAt?: Date;
25
- finishedAt?: Date;
26
- };
27
-
28
- interface QueueConfig {
29
- connection: {
30
- host: string;
31
- port: number;
32
- };
33
- queueName?: string;
34
- defaultJobOptions?: Record<string, unknown>;
35
- }
36
- declare class AgentQueue {
37
- private queue;
38
- private queueEvents;
39
- private queueName;
40
- constructor(config: QueueConfig);
41
- enqueueAgentRun(opts: {
42
- agentName: string;
43
- input: string;
44
- sessionId?: string;
45
- userId?: string;
46
- priority?: number;
47
- delay?: number;
48
- attempts?: number;
49
- repeat?: {
50
- pattern: string;
51
- };
52
- }): Promise<{
53
- jobId: string;
54
- }>;
55
- enqueueWorkflow(opts: {
56
- workflowName: string;
57
- initialState?: Record<string, unknown>;
58
- sessionId?: string;
59
- priority?: number;
60
- }): Promise<{
61
- jobId: string;
62
- }>;
63
- getJobStatus(jobId: string): Promise<JobStatus>;
64
- cancelJob(jobId: string): Promise<void>;
65
- onCompleted(handler: (jobId: string, result: RunOutput) => void): void;
66
- onFailed(handler: (jobId: string, error: Error) => void): void;
67
- close(): Promise<void>;
68
- }
69
-
70
- interface WorkerConfig {
71
- connection: {
72
- host: string;
73
- port: number;
74
- };
75
- queueName?: string;
76
- concurrency?: number;
77
- agentRegistry: Record<string, Agent>;
78
- workflowRegistry?: Record<string, Workflow<any>>;
79
- }
80
- declare class AgentWorker {
81
- private worker;
82
- constructor(config: WorkerConfig);
83
- start(): void;
84
- stop(): Promise<void>;
85
- }
86
-
87
- declare function bridgeEventBusToJob(eventBus: EventBus, job: any, runId: string): () => void;
88
-
89
- export { type AgentJobPayload, AgentQueue, AgentWorker, type JobPayload, type JobStatus, type QueueConfig, type WorkerConfig, type WorkflowJobPayload, bridgeEventBusToJob };
package/dist/index.js DELETED
@@ -1,197 +0,0 @@
1
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
- }) : x)(function(x) {
4
- if (typeof require !== "undefined") return require.apply(this, arguments);
5
- throw Error('Dynamic require of "' + x + '" is not supported');
6
- });
7
-
8
- // src/job-producer.ts
9
- var AgentQueue = class {
10
- queue;
11
- queueEvents;
12
- queueName;
13
- constructor(config) {
14
- this.queueName = config.queueName ?? "radaros:jobs";
15
- try {
16
- const { Queue, QueueEvents } = __require("bullmq");
17
- this.queue = new Queue(this.queueName, {
18
- connection: config.connection,
19
- defaultJobOptions: config.defaultJobOptions
20
- });
21
- this.queueEvents = new QueueEvents(this.queueName, {
22
- connection: config.connection
23
- });
24
- } catch {
25
- throw new Error(
26
- "bullmq and ioredis are required for AgentQueue. Install them: npm install bullmq ioredis"
27
- );
28
- }
29
- }
30
- async enqueueAgentRun(opts) {
31
- const payload = {
32
- type: "agent",
33
- agentName: opts.agentName,
34
- input: opts.input,
35
- sessionId: opts.sessionId,
36
- userId: opts.userId
37
- };
38
- const jobOpts = {};
39
- if (opts.priority !== void 0) jobOpts.priority = opts.priority;
40
- if (opts.delay !== void 0) jobOpts.delay = opts.delay;
41
- if (opts.attempts !== void 0) jobOpts.attempts = opts.attempts;
42
- if (opts.repeat) jobOpts.repeat = opts.repeat;
43
- const job = await this.queue.add(
44
- `agent:${opts.agentName}`,
45
- payload,
46
- jobOpts
47
- );
48
- return { jobId: job.id };
49
- }
50
- async enqueueWorkflow(opts) {
51
- const payload = {
52
- type: "workflow",
53
- workflowName: opts.workflowName,
54
- initialState: opts.initialState,
55
- sessionId: opts.sessionId
56
- };
57
- const jobOpts = {};
58
- if (opts.priority !== void 0) jobOpts.priority = opts.priority;
59
- const job = await this.queue.add(
60
- `workflow:${opts.workflowName}`,
61
- payload,
62
- jobOpts
63
- );
64
- return { jobId: job.id };
65
- }
66
- async getJobStatus(jobId) {
67
- const job = await this.queue.getJob(jobId);
68
- if (!job) {
69
- throw new Error(`Job ${jobId} not found`);
70
- }
71
- const state = await job.getState();
72
- return {
73
- jobId: job.id,
74
- state,
75
- progress: typeof job.progress === "number" ? job.progress : void 0,
76
- result: job.returnvalue,
77
- error: job.failedReason,
78
- createdAt: new Date(job.timestamp),
79
- processedAt: job.processedOn ? new Date(job.processedOn) : void 0,
80
- finishedAt: job.finishedOn ? new Date(job.finishedOn) : void 0
81
- };
82
- }
83
- async cancelJob(jobId) {
84
- const job = await this.queue.getJob(jobId);
85
- if (job) {
86
- await job.remove();
87
- }
88
- }
89
- onCompleted(handler) {
90
- this.queueEvents.on("completed", ({ jobId, returnvalue }) => {
91
- handler(jobId, returnvalue);
92
- });
93
- }
94
- onFailed(handler) {
95
- this.queueEvents.on("failed", ({ jobId, failedReason }) => {
96
- handler(jobId, new Error(failedReason));
97
- });
98
- }
99
- async close() {
100
- await this.queue.close();
101
- await this.queueEvents.close();
102
- }
103
- };
104
-
105
- // src/job-worker.ts
106
- var AgentWorker = class {
107
- worker;
108
- constructor(config) {
109
- const queueName = config.queueName ?? "radaros:jobs";
110
- const concurrency = config.concurrency ?? 5;
111
- try {
112
- const { Worker } = __require("bullmq");
113
- this.worker = new Worker(
114
- queueName,
115
- async (job) => {
116
- const payload = job.data;
117
- if (payload.type === "agent") {
118
- const agent = config.agentRegistry[payload.agentName];
119
- if (!agent) {
120
- throw new Error(
121
- `Agent "${payload.agentName}" not found in registry`
122
- );
123
- }
124
- const onChunk = (evt) => {
125
- job.updateProgress(
126
- typeof job.progress === "number" ? job.progress + 1 : 1
127
- );
128
- };
129
- agent.eventBus.on("run.stream.chunk", onChunk);
130
- try {
131
- const result = await agent.run(payload.input, {
132
- sessionId: payload.sessionId,
133
- userId: payload.userId
134
- });
135
- return result;
136
- } finally {
137
- agent.eventBus.off("run.stream.chunk", onChunk);
138
- }
139
- }
140
- if (payload.type === "workflow") {
141
- const workflow = config.workflowRegistry?.[payload.workflowName];
142
- if (!workflow) {
143
- throw new Error(
144
- `Workflow "${payload.workflowName}" not found in registry`
145
- );
146
- }
147
- const result = await workflow.run({
148
- sessionId: payload.sessionId
149
- });
150
- return result;
151
- }
152
- throw new Error(`Unknown job type: ${payload.type}`);
153
- },
154
- {
155
- connection: config.connection,
156
- concurrency
157
- }
158
- );
159
- } catch {
160
- throw new Error(
161
- "bullmq and ioredis are required for AgentWorker. Install them: npm install bullmq ioredis"
162
- );
163
- }
164
- }
165
- start() {
166
- }
167
- async stop() {
168
- await this.worker.close();
169
- }
170
- };
171
-
172
- // src/progress.ts
173
- function bridgeEventBusToJob(eventBus, job, runId) {
174
- let progressCount = 0;
175
- const onChunk = (evt) => {
176
- if (evt.runId !== runId) return;
177
- progressCount++;
178
- job.updateProgress(progressCount).catch(() => {
179
- });
180
- };
181
- const onToolCall = (evt) => {
182
- if (evt.runId !== runId) return;
183
- job.log(`Tool call: ${evt.toolName}`).catch(() => {
184
- });
185
- };
186
- eventBus.on("run.stream.chunk", onChunk);
187
- eventBus.on("tool.call", onToolCall);
188
- return () => {
189
- eventBus.off("run.stream.chunk", onChunk);
190
- eventBus.off("tool.call", onToolCall);
191
- };
192
- }
193
- export {
194
- AgentQueue,
195
- AgentWorker,
196
- bridgeEventBusToJob
197
- };