@radaros/queue 0.1.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.
@@ -0,0 +1,89 @@
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 ADDED
@@ -0,0 +1,197 @@
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
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@radaros/queue",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ }
12
+ },
13
+ "scripts": {
14
+ "build": "tsup src/index.ts --format esm --dts --clean",
15
+ "dev": "tsup src/index.ts --format esm --dts --watch"
16
+ },
17
+ "devDependencies": {
18
+ "@types/node": "^25.3.1",
19
+ "tsup": "^8.0.0",
20
+ "typescript": "^5.6.0"
21
+ },
22
+ "peerDependencies": {
23
+ "@radaros/core": "^0.1.0",
24
+ "bullmq": "^5.0.0",
25
+ "ioredis": "^5.0.0"
26
+ },
27
+ "peerDependenciesMeta": {
28
+ "bullmq": {
29
+ "optional": true
30
+ },
31
+ "ioredis": {
32
+ "optional": true
33
+ }
34
+ }
35
+ }
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ export { AgentQueue } from "./job-producer.js";
2
+ export type { QueueConfig } from "./job-producer.js";
3
+
4
+ export { AgentWorker } from "./job-worker.js";
5
+ export type { WorkerConfig } from "./job-worker.js";
6
+
7
+ export { bridgeEventBusToJob } from "./progress.js";
8
+
9
+ export type {
10
+ AgentJobPayload,
11
+ WorkflowJobPayload,
12
+ JobPayload,
13
+ JobStatus,
14
+ } from "./job-types.js";
@@ -0,0 +1,137 @@
1
+ import type { RunOutput } from "@radaros/core";
2
+ import type { AgentJobPayload, WorkflowJobPayload, JobStatus } from "./job-types.js";
3
+
4
+ export interface QueueConfig {
5
+ connection: { host: string; port: number };
6
+ queueName?: string;
7
+ defaultJobOptions?: Record<string, unknown>;
8
+ }
9
+
10
+ export class AgentQueue {
11
+ private queue: any;
12
+ private queueEvents: any;
13
+ private queueName: string;
14
+
15
+ constructor(config: QueueConfig) {
16
+ this.queueName = config.queueName ?? "radaros:jobs";
17
+ try {
18
+ const { Queue, QueueEvents } = require("bullmq");
19
+ this.queue = new Queue(this.queueName, {
20
+ connection: config.connection,
21
+ defaultJobOptions: config.defaultJobOptions,
22
+ });
23
+ this.queueEvents = new QueueEvents(this.queueName, {
24
+ connection: config.connection,
25
+ });
26
+ } catch {
27
+ throw new Error(
28
+ "bullmq and ioredis are required for AgentQueue. Install them: npm install bullmq ioredis"
29
+ );
30
+ }
31
+ }
32
+
33
+ async enqueueAgentRun(opts: {
34
+ agentName: string;
35
+ input: string;
36
+ sessionId?: string;
37
+ userId?: string;
38
+ priority?: number;
39
+ delay?: number;
40
+ attempts?: number;
41
+ repeat?: { pattern: string };
42
+ }): Promise<{ jobId: string }> {
43
+ const payload: AgentJobPayload = {
44
+ type: "agent",
45
+ agentName: opts.agentName,
46
+ input: opts.input,
47
+ sessionId: opts.sessionId,
48
+ userId: opts.userId,
49
+ };
50
+
51
+ const jobOpts: Record<string, unknown> = {};
52
+ if (opts.priority !== undefined) jobOpts.priority = opts.priority;
53
+ if (opts.delay !== undefined) jobOpts.delay = opts.delay;
54
+ if (opts.attempts !== undefined) jobOpts.attempts = opts.attempts;
55
+ if (opts.repeat) jobOpts.repeat = opts.repeat;
56
+
57
+ const job = await this.queue.add(
58
+ `agent:${opts.agentName}`,
59
+ payload,
60
+ jobOpts
61
+ );
62
+
63
+ return { jobId: job.id };
64
+ }
65
+
66
+ async enqueueWorkflow(opts: {
67
+ workflowName: string;
68
+ initialState?: Record<string, unknown>;
69
+ sessionId?: string;
70
+ priority?: number;
71
+ }): Promise<{ jobId: string }> {
72
+ const payload: WorkflowJobPayload = {
73
+ type: "workflow",
74
+ workflowName: opts.workflowName,
75
+ initialState: opts.initialState,
76
+ sessionId: opts.sessionId,
77
+ };
78
+
79
+ const jobOpts: Record<string, unknown> = {};
80
+ if (opts.priority !== undefined) jobOpts.priority = opts.priority;
81
+
82
+ const job = await this.queue.add(
83
+ `workflow:${opts.workflowName}`,
84
+ payload,
85
+ jobOpts
86
+ );
87
+
88
+ return { jobId: job.id };
89
+ }
90
+
91
+ async getJobStatus(jobId: string): Promise<JobStatus> {
92
+ const job = await this.queue.getJob(jobId);
93
+ if (!job) {
94
+ throw new Error(`Job ${jobId} not found`);
95
+ }
96
+
97
+ const state = await job.getState();
98
+
99
+ return {
100
+ jobId: job.id,
101
+ state: state as JobStatus["state"],
102
+ progress:
103
+ typeof job.progress === "number" ? job.progress : undefined,
104
+ result: job.returnvalue,
105
+ error: job.failedReason,
106
+ createdAt: new Date(job.timestamp),
107
+ processedAt: job.processedOn ? new Date(job.processedOn) : undefined,
108
+ finishedAt: job.finishedOn ? new Date(job.finishedOn) : undefined,
109
+ };
110
+ }
111
+
112
+ async cancelJob(jobId: string): Promise<void> {
113
+ const job = await this.queue.getJob(jobId);
114
+ if (job) {
115
+ await job.remove();
116
+ }
117
+ }
118
+
119
+ onCompleted(
120
+ handler: (jobId: string, result: RunOutput) => void
121
+ ): void {
122
+ this.queueEvents.on("completed", ({ jobId, returnvalue }: any) => {
123
+ handler(jobId, returnvalue);
124
+ });
125
+ }
126
+
127
+ onFailed(handler: (jobId: string, error: Error) => void): void {
128
+ this.queueEvents.on("failed", ({ jobId, failedReason }: any) => {
129
+ handler(jobId, new Error(failedReason));
130
+ });
131
+ }
132
+
133
+ async close(): Promise<void> {
134
+ await this.queue.close();
135
+ await this.queueEvents.close();
136
+ }
137
+ }
@@ -0,0 +1,29 @@
1
+ import type { RunOutput } from "@radaros/core";
2
+
3
+ export type AgentJobPayload = {
4
+ type: "agent";
5
+ agentName: string;
6
+ input: string;
7
+ sessionId?: string;
8
+ userId?: string;
9
+ };
10
+
11
+ export type WorkflowJobPayload = {
12
+ type: "workflow";
13
+ workflowName: string;
14
+ initialState?: Record<string, unknown>;
15
+ sessionId?: string;
16
+ };
17
+
18
+ export type JobPayload = AgentJobPayload | WorkflowJobPayload;
19
+
20
+ export type JobStatus = {
21
+ jobId: string;
22
+ state: "waiting" | "active" | "completed" | "failed" | "delayed";
23
+ progress?: number;
24
+ result?: RunOutput;
25
+ error?: string;
26
+ createdAt: Date;
27
+ processedAt?: Date;
28
+ finishedAt?: Date;
29
+ };
@@ -0,0 +1,89 @@
1
+ import type { Agent, Workflow } from "@radaros/core";
2
+ import type { JobPayload } from "./job-types.js";
3
+
4
+ export interface WorkerConfig {
5
+ connection: { host: string; port: number };
6
+ queueName?: string;
7
+ concurrency?: number;
8
+ agentRegistry: Record<string, Agent>;
9
+ workflowRegistry?: Record<string, Workflow<any>>;
10
+ }
11
+
12
+ export class AgentWorker {
13
+ private worker: any;
14
+
15
+ constructor(config: WorkerConfig) {
16
+ const queueName = config.queueName ?? "radaros:jobs";
17
+ const concurrency = config.concurrency ?? 5;
18
+
19
+ try {
20
+ const { Worker } = require("bullmq");
21
+
22
+ this.worker = new Worker(
23
+ queueName,
24
+ async (job: any) => {
25
+ const payload = job.data as JobPayload;
26
+
27
+ if (payload.type === "agent") {
28
+ const agent = config.agentRegistry[payload.agentName];
29
+ if (!agent) {
30
+ throw new Error(
31
+ `Agent "${payload.agentName}" not found in registry`
32
+ );
33
+ }
34
+
35
+ const onChunk = (evt: any) => {
36
+ job.updateProgress(
37
+ typeof job.progress === "number" ? job.progress + 1 : 1
38
+ );
39
+ };
40
+ agent.eventBus.on("run.stream.chunk", onChunk);
41
+
42
+ try {
43
+ const result = await agent.run(payload.input, {
44
+ sessionId: payload.sessionId,
45
+ userId: payload.userId,
46
+ });
47
+ return result;
48
+ } finally {
49
+ agent.eventBus.off("run.stream.chunk", onChunk);
50
+ }
51
+ }
52
+
53
+ if (payload.type === "workflow") {
54
+ const workflow =
55
+ config.workflowRegistry?.[payload.workflowName];
56
+ if (!workflow) {
57
+ throw new Error(
58
+ `Workflow "${payload.workflowName}" not found in registry`
59
+ );
60
+ }
61
+
62
+ const result = await workflow.run({
63
+ sessionId: payload.sessionId,
64
+ });
65
+ return result;
66
+ }
67
+
68
+ throw new Error(`Unknown job type: ${(payload as any).type}`);
69
+ },
70
+ {
71
+ connection: config.connection,
72
+ concurrency,
73
+ }
74
+ );
75
+ } catch {
76
+ throw new Error(
77
+ "bullmq and ioredis are required for AgentWorker. Install them: npm install bullmq ioredis"
78
+ );
79
+ }
80
+ }
81
+
82
+ start(): void {
83
+ // Worker starts automatically on construction in BullMQ
84
+ }
85
+
86
+ async stop(): Promise<void> {
87
+ await this.worker.close();
88
+ }
89
+ }
@@ -0,0 +1,28 @@
1
+ import type { EventBus } from "@radaros/core";
2
+
3
+ export function bridgeEventBusToJob(
4
+ eventBus: EventBus,
5
+ job: any,
6
+ runId: string
7
+ ): () => void {
8
+ let progressCount = 0;
9
+
10
+ const onChunk = (evt: { runId: string; chunk: string }) => {
11
+ if (evt.runId !== runId) return;
12
+ progressCount++;
13
+ job.updateProgress(progressCount).catch(() => {});
14
+ };
15
+
16
+ const onToolCall = (evt: { runId: string; toolName: string }) => {
17
+ if (evt.runId !== runId) return;
18
+ job.log(`Tool call: ${evt.toolName}`).catch(() => {});
19
+ };
20
+
21
+ eventBus.on("run.stream.chunk", onChunk);
22
+ eventBus.on("tool.call", onToolCall);
23
+
24
+ return () => {
25
+ eventBus.off("run.stream.chunk", onChunk);
26
+ eventBus.off("tool.call", onToolCall);
27
+ };
28
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ "paths": {
7
+ "@radaros/core": ["../core/dist/index.d.ts"]
8
+ }
9
+ },
10
+ "include": ["src"]
11
+ }