@copilotkitnext/runtime 0.0.1

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.
Files changed (47) hide show
  1. package/.cursor/rules/runtime.always.mdc +9 -0
  2. package/.turbo/turbo-build.log +22 -0
  3. package/.turbo/turbo-check-types.log +4 -0
  4. package/.turbo/turbo-lint.log +56 -0
  5. package/.turbo/turbo-test$colon$coverage.log +149 -0
  6. package/.turbo/turbo-test.log +107 -0
  7. package/LICENSE +11 -0
  8. package/README-RUNNERS.md +78 -0
  9. package/dist/index.d.mts +245 -0
  10. package/dist/index.d.ts +245 -0
  11. package/dist/index.js +1873 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/index.mjs +1841 -0
  14. package/dist/index.mjs.map +1 -0
  15. package/eslint.config.mjs +3 -0
  16. package/package.json +62 -0
  17. package/src/__tests__/get-runtime-info.test.ts +117 -0
  18. package/src/__tests__/handle-run.test.ts +69 -0
  19. package/src/__tests__/handle-transcribe.test.ts +289 -0
  20. package/src/__tests__/in-process-agent-runner-messages.test.ts +599 -0
  21. package/src/__tests__/in-process-agent-runner.test.ts +726 -0
  22. package/src/__tests__/middleware.test.ts +432 -0
  23. package/src/__tests__/routing.test.ts +257 -0
  24. package/src/endpoint.ts +150 -0
  25. package/src/handler.ts +3 -0
  26. package/src/handlers/get-runtime-info.ts +50 -0
  27. package/src/handlers/handle-connect.ts +144 -0
  28. package/src/handlers/handle-run.ts +156 -0
  29. package/src/handlers/handle-transcribe.ts +126 -0
  30. package/src/index.ts +8 -0
  31. package/src/middleware.ts +232 -0
  32. package/src/runner/__tests__/enterprise-runner.test.ts +992 -0
  33. package/src/runner/__tests__/event-compaction.test.ts +253 -0
  34. package/src/runner/__tests__/in-memory-runner.test.ts +483 -0
  35. package/src/runner/__tests__/sqlite-runner.test.ts +975 -0
  36. package/src/runner/agent-runner.ts +27 -0
  37. package/src/runner/enterprise.ts +653 -0
  38. package/src/runner/event-compaction.ts +250 -0
  39. package/src/runner/in-memory.ts +322 -0
  40. package/src/runner/index.ts +0 -0
  41. package/src/runner/sqlite.ts +481 -0
  42. package/src/runtime.ts +53 -0
  43. package/src/transcription-service/transcription-service-openai.ts +29 -0
  44. package/src/transcription-service/transcription-service.ts +11 -0
  45. package/tsconfig.json +13 -0
  46. package/tsup.config.ts +11 -0
  47. package/vitest.config.mjs +15 -0
@@ -0,0 +1,726 @@
1
+ import { describe, it, expect, beforeEach } from "vitest";
2
+ import { InMemoryAgentRunner } from "../runner/in-memory";
3
+ import { AbstractAgent, BaseEvent, RunAgentInput } from "@ag-ui/client";
4
+ import { firstValueFrom } from "rxjs";
5
+ import { toArray } from "rxjs/operators";
6
+
7
+ // Mock agent implementations for testing
8
+ class MockAgent extends AbstractAgent {
9
+ private events: BaseEvent[];
10
+ private delay: number;
11
+
12
+ constructor(events: BaseEvent[] = [], delay: number = 0) {
13
+ super();
14
+ this.events = events;
15
+ this.delay = delay;
16
+ }
17
+
18
+ async runAgent(
19
+ input: RunAgentInput,
20
+ options: { onEvent: (event: { event: BaseEvent }) => void }
21
+ ): Promise<void> {
22
+ for (const event of this.events) {
23
+ if (this.delay > 0) {
24
+ await new Promise((resolve) => setTimeout(resolve, this.delay));
25
+ }
26
+ options.onEvent({ event });
27
+ }
28
+ }
29
+
30
+ clone(): AbstractAgent {
31
+ return new MockAgent(this.events, this.delay);
32
+ }
33
+ }
34
+
35
+ class DelayedEventAgent extends AbstractAgent {
36
+ private eventCount: number;
37
+ private eventDelay: number;
38
+ private prefix: string;
39
+
40
+ constructor(eventCount: number = 5, eventDelay: number = 10, prefix: string = "delayed") {
41
+ super();
42
+ this.eventCount = eventCount;
43
+ this.eventDelay = eventDelay;
44
+ this.prefix = prefix;
45
+ }
46
+
47
+ async runAgent(
48
+ input: RunAgentInput,
49
+ options: { onEvent: (event: { event: BaseEvent }) => void }
50
+ ): Promise<void> {
51
+ for (let i = 0; i < this.eventCount; i++) {
52
+ await new Promise((resolve) => setTimeout(resolve, this.eventDelay));
53
+ options.onEvent({
54
+ event: {
55
+ type: "message",
56
+ id: `${this.prefix}-${i}`,
57
+ timestamp: new Date().toISOString(),
58
+ data: { index: i, prefix: this.prefix }
59
+ } as BaseEvent
60
+ });
61
+ }
62
+ }
63
+
64
+ clone(): AbstractAgent {
65
+ return new DelayedEventAgent(this.eventCount, this.eventDelay, this.prefix);
66
+ }
67
+ }
68
+
69
+ class ErrorThrowingAgent extends AbstractAgent {
70
+ private throwAfterEvents: number;
71
+ private errorMessage: string;
72
+
73
+ constructor(throwAfterEvents: number = 2, errorMessage: string = "Test error") {
74
+ super();
75
+ this.throwAfterEvents = throwAfterEvents;
76
+ this.errorMessage = errorMessage;
77
+ }
78
+
79
+ async runAgent(
80
+ input: RunAgentInput,
81
+ options: { onEvent: (event: { event: BaseEvent }) => void }
82
+ ): Promise<void> {
83
+ for (let i = 0; i < this.throwAfterEvents; i++) {
84
+ options.onEvent({
85
+ event: {
86
+ type: "message",
87
+ id: `error-agent-${i}`,
88
+ timestamp: new Date().toISOString(),
89
+ data: { index: i }
90
+ } as BaseEvent
91
+ });
92
+ }
93
+ throw new Error(this.errorMessage);
94
+ }
95
+
96
+ clone(): AbstractAgent {
97
+ return new ErrorThrowingAgent(this.throwAfterEvents, this.errorMessage);
98
+ }
99
+ }
100
+
101
+ class MultiEventAgent extends AbstractAgent {
102
+ private runId: string;
103
+
104
+ constructor(runId: string) {
105
+ super();
106
+ this.runId = runId;
107
+ }
108
+
109
+ async runAgent(
110
+ input: RunAgentInput,
111
+ options: { onEvent: (event: { event: BaseEvent }) => void }
112
+ ): Promise<void> {
113
+ // Emit different types of events
114
+ const eventTypes = ["start", "message", "tool_call", "tool_result", "end"];
115
+
116
+ for (const eventType of eventTypes) {
117
+ options.onEvent({
118
+ event: {
119
+ type: eventType,
120
+ id: `${this.runId}-${eventType}`,
121
+ timestamp: new Date().toISOString(),
122
+ data: {
123
+ runId: this.runId,
124
+ eventType,
125
+ metadata: { source: "MultiEventAgent" }
126
+ }
127
+ } as BaseEvent
128
+ });
129
+ }
130
+ }
131
+
132
+ clone(): AbstractAgent {
133
+ return new MultiEventAgent(this.runId);
134
+ }
135
+ }
136
+
137
+ describe("InMemoryAgentRunner", () => {
138
+ let runner: InMemoryAgentRunner;
139
+
140
+ beforeEach(() => {
141
+ runner = new InMemoryAgentRunner();
142
+ });
143
+
144
+ describe("Basic Functionality", () => {
145
+ it("should run a single agent and collect all events", async () => {
146
+ const threadId = "test-thread-1";
147
+ const events: BaseEvent[] = [
148
+ { type: "start", id: "1", timestamp: new Date().toISOString(), data: {} } as BaseEvent,
149
+ { type: "message", id: "2", timestamp: new Date().toISOString(), data: { text: "Hello" } } as BaseEvent,
150
+ { type: "end", id: "3", timestamp: new Date().toISOString(), data: {} } as BaseEvent,
151
+ ];
152
+
153
+ const agent = new MockAgent(events);
154
+ const input: RunAgentInput = {
155
+ messages: [],
156
+ state: {},
157
+ threadId,
158
+ runId: "run-1",
159
+ };
160
+
161
+ const runObservable = runner.run({ threadId, agent, input });
162
+ const collectedEvents = await firstValueFrom(runObservable.pipe(toArray()));
163
+
164
+ expect(collectedEvents).toHaveLength(3);
165
+ expect(collectedEvents).toEqual(events);
166
+ });
167
+
168
+ it("should allow connecting after run completes and receive all past events", async () => {
169
+ const threadId = "test-thread-3";
170
+ const events: BaseEvent[] = [
171
+ { type: "message", id: "past-1", timestamp: new Date().toISOString(), data: {} } as BaseEvent,
172
+ { type: "message", id: "past-2", timestamp: new Date().toISOString(), data: {} } as BaseEvent,
173
+ ];
174
+
175
+ const agent = new MockAgent(events);
176
+ const input: RunAgentInput = {
177
+ messages: [],
178
+ state: {},
179
+ threadId,
180
+ runId: "run-3",
181
+ };
182
+
183
+ // Run and wait for completion
184
+ const runObservable = runner.run({ threadId, agent, input });
185
+ await firstValueFrom(runObservable.pipe(toArray()));
186
+
187
+ // Connect after completion
188
+ const connectObservable = runner.connect({ threadId });
189
+ const collectedEvents = await firstValueFrom(connectObservable.pipe(toArray()));
190
+
191
+ expect(collectedEvents).toHaveLength(2);
192
+ expect(collectedEvents).toEqual(events);
193
+ });
194
+ });
195
+
196
+ describe("Multiple Runs", () => {
197
+ it("should accumulate events from multiple sequential runs on same thread", async () => {
198
+ const threadId = "test-thread-multi-1";
199
+
200
+ // First run
201
+ const agent1 = new MultiEventAgent("run-1");
202
+ const input1: RunAgentInput = {
203
+ messages: [],
204
+ state: {},
205
+ threadId,
206
+ runId: "run-1",
207
+ };
208
+
209
+ const run1 = runner.run({ threadId, agent: agent1, input: input1 });
210
+ await firstValueFrom(run1.pipe(toArray()));
211
+
212
+ // Second run
213
+ const agent2 = new MultiEventAgent("run-2");
214
+ const input2: RunAgentInput = {
215
+ messages: [],
216
+ state: {},
217
+ threadId,
218
+ runId: "run-2",
219
+ };
220
+
221
+ const run2 = runner.run({ threadId, agent: agent2, input: input2 });
222
+ await firstValueFrom(run2.pipe(toArray()));
223
+
224
+ // Third run
225
+ const agent3 = new MultiEventAgent("run-3");
226
+ const input3: RunAgentInput = {
227
+ messages: [],
228
+ state: {},
229
+ threadId,
230
+ runId: "run-3",
231
+ };
232
+
233
+ const run3 = runner.run({ threadId, agent: agent3, input: input3 });
234
+ await firstValueFrom(run3.pipe(toArray()));
235
+
236
+ // Connect and verify all events
237
+ const connectObservable = runner.connect({ threadId });
238
+ const allEvents = await firstValueFrom(connectObservable.pipe(toArray()));
239
+
240
+ expect(allEvents).toHaveLength(15); // 5 events per run × 3 runs
241
+
242
+ // Verify events from all runs are present
243
+ const run1Events = allEvents.filter(e => e.id?.startsWith("run-1"));
244
+ const run2Events = allEvents.filter(e => e.id?.startsWith("run-2"));
245
+ const run3Events = allEvents.filter(e => e.id?.startsWith("run-3"));
246
+
247
+ expect(run1Events).toHaveLength(5);
248
+ expect(run2Events).toHaveLength(5);
249
+ expect(run3Events).toHaveLength(5);
250
+
251
+ // Verify order preservation
252
+ const runOrder = allEvents.map(e => e.id?.split("-")[0] + "-" + e.id?.split("-")[1]);
253
+ expect(runOrder.slice(0, 5).every(id => id.startsWith("run-1"))).toBe(true);
254
+ expect(runOrder.slice(5, 10).every(id => id.startsWith("run-2"))).toBe(true);
255
+ expect(runOrder.slice(10, 15).every(id => id.startsWith("run-3"))).toBe(true);
256
+ });
257
+
258
+ it("should handle connect during multiple runs", async () => {
259
+ const threadId = "test-thread-multi-2";
260
+
261
+ // Start first run
262
+ const agent1 = new DelayedEventAgent(5, 20, "first");
263
+ const input1: RunAgentInput = {
264
+ messages: [],
265
+ state: {},
266
+ threadId,
267
+ runId: "run-1",
268
+ };
269
+
270
+ const run1Observable = runner.run({ threadId, agent: agent1, input: input1 });
271
+
272
+ // Wait a bit to ensure first run is in progress
273
+ await new Promise(resolve => setTimeout(resolve, 50));
274
+
275
+ // Connect during first run
276
+ const connectObservable = runner.connect({ threadId });
277
+ const eventCollector = firstValueFrom(connectObservable.pipe(toArray()));
278
+
279
+ // Wait for first run to complete
280
+ await firstValueFrom(run1Observable.pipe(toArray()));
281
+
282
+ // Collect all events from the connect during first run
283
+ const allEvents = await eventCollector;
284
+
285
+ // Connect only receives events from the first run since it completes
286
+ expect(allEvents).toHaveLength(5); // Only events from first run
287
+ const firstRunEvents = allEvents.filter(e => e.id?.startsWith("first"));
288
+
289
+ expect(firstRunEvents).toHaveLength(5);
290
+
291
+ // Start second run
292
+ const agent2 = new DelayedEventAgent(3, 10, "second");
293
+ const input2: RunAgentInput = {
294
+ messages: [],
295
+ state: {},
296
+ threadId,
297
+ runId: "run-2",
298
+ };
299
+
300
+ const run2Observable = runner.run({ threadId, agent: agent2, input: input2 });
301
+ await firstValueFrom(run2Observable.pipe(toArray()));
302
+
303
+ // Connect after both runs to verify all events are accumulated
304
+ const allEventsAfter = await firstValueFrom(runner.connect({ threadId }).pipe(toArray()));
305
+ expect(allEventsAfter).toHaveLength(8); // 5 from first + 3 from second
306
+ });
307
+
308
+ it("should preserve event order across different agent types", async () => {
309
+ const threadId = "test-thread-multi-3";
310
+
311
+ // Run different types of agents
312
+ const agents = [
313
+ new MockAgent([
314
+ { type: "mock", id: "mock-1", timestamp: new Date().toISOString(), data: {} } as BaseEvent,
315
+ { type: "mock", id: "mock-2", timestamp: new Date().toISOString(), data: {} } as BaseEvent,
316
+ ]),
317
+ new MultiEventAgent("multi"),
318
+ new DelayedEventAgent(2, 0, "delayed"),
319
+ ];
320
+
321
+ for (let i = 0; i < agents.length; i++) {
322
+ const input: RunAgentInput = {
323
+ messages: [],
324
+ state: {},
325
+ threadId,
326
+ runId: `run-${i}`,
327
+ };
328
+
329
+ const run = runner.run({ threadId, agent: agents[i], input });
330
+ await firstValueFrom(run.pipe(toArray()));
331
+ }
332
+
333
+ // Verify all events are preserved
334
+ const connectObservable = runner.connect({ threadId });
335
+ const allEvents = await firstValueFrom(connectObservable.pipe(toArray()));
336
+
337
+ expect(allEvents).toHaveLength(9); // 2 + 5 + 2
338
+
339
+ // Verify event groups are in order
340
+ expect(allEvents[0].id).toBe("mock-1");
341
+ expect(allEvents[1].id).toBe("mock-2");
342
+ expect(allEvents[2].id).toContain("multi");
343
+ expect(allEvents[7].id).toContain("delayed");
344
+ });
345
+ });
346
+
347
+ describe("Concurrent Subscribers", () => {
348
+ it("should provide same events to multiple concurrent connect calls", async () => {
349
+ const threadId = "test-thread-concurrent-1";
350
+ const agent = new MultiEventAgent("concurrent");
351
+ const input: RunAgentInput = {
352
+ messages: [],
353
+ state: {},
354
+ threadId,
355
+ runId: "run-concurrent",
356
+ };
357
+
358
+ // Run agent
359
+ const runObservable = runner.run({ threadId, agent, input });
360
+ await firstValueFrom(runObservable.pipe(toArray()));
361
+
362
+ // Multiple concurrent connects
363
+ const connect1 = runner.connect({ threadId });
364
+ const connect2 = runner.connect({ threadId });
365
+ const connect3 = runner.connect({ threadId });
366
+
367
+ const [events1, events2, events3] = await Promise.all([
368
+ firstValueFrom(connect1.pipe(toArray())),
369
+ firstValueFrom(connect2.pipe(toArray())),
370
+ firstValueFrom(connect3.pipe(toArray())),
371
+ ]);
372
+
373
+ // All should receive same events
374
+ expect(events1).toHaveLength(5);
375
+ expect(events2).toHaveLength(5);
376
+ expect(events3).toHaveLength(5);
377
+ expect(events1).toEqual(events2);
378
+ expect(events2).toEqual(events3);
379
+ });
380
+
381
+ it("should handle late subscribers during active run", async () => {
382
+ const threadId = "test-thread-concurrent-2";
383
+ const agent = new DelayedEventAgent(10, 20, "late-sub");
384
+ const input: RunAgentInput = {
385
+ messages: [],
386
+ state: {},
387
+ threadId,
388
+ runId: "run-late",
389
+ };
390
+
391
+ // Start run
392
+ runner.run({ threadId, agent, input });
393
+
394
+ // Connect at different times during the run
395
+ await new Promise(resolve => setTimeout(resolve, 50)); // After ~2 events
396
+ const connect1 = runner.connect({ threadId });
397
+
398
+ await new Promise(resolve => setTimeout(resolve, 60)); // After ~5 events
399
+ const connect2 = runner.connect({ threadId });
400
+
401
+ await new Promise(resolve => setTimeout(resolve, 80)); // After ~9 events
402
+ const connect3 = runner.connect({ threadId });
403
+
404
+ const [events1, events2, events3] = await Promise.all([
405
+ firstValueFrom(connect1.pipe(toArray())),
406
+ firstValueFrom(connect2.pipe(toArray())),
407
+ firstValueFrom(connect3.pipe(toArray())),
408
+ ]);
409
+
410
+ // All subscribers should eventually receive all events
411
+ expect(events1).toHaveLength(10);
412
+ expect(events2).toHaveLength(10);
413
+ expect(events3).toHaveLength(10);
414
+
415
+ // Verify they all have the same events
416
+ expect(events1.map(e => e.id)).toEqual(events2.map(e => e.id));
417
+ expect(events2.map(e => e.id)).toEqual(events3.map(e => e.id));
418
+ });
419
+ });
420
+
421
+ describe("Error Handling", () => {
422
+ it("should throw error when thread is already running", async () => {
423
+ const threadId = "test-thread-error-1";
424
+ const agent = new DelayedEventAgent(5, 50, "blocking");
425
+ const input: RunAgentInput = {
426
+ messages: [],
427
+ state: {},
428
+ threadId,
429
+ runId: "run-1",
430
+ };
431
+
432
+ // Start first run
433
+ runner.run({ threadId, agent, input });
434
+
435
+ // Try to start another run on same thread immediately
436
+ expect(() => {
437
+ runner.run({ threadId, agent, input });
438
+ }).toThrow("Thread already running");
439
+ });
440
+
441
+ it("should handle agent errors gracefully", async () => {
442
+ const threadId = "test-thread-error-2";
443
+ const agent = new ErrorThrowingAgent(3, "Agent crashed!");
444
+ const input: RunAgentInput = {
445
+ messages: [],
446
+ state: {},
447
+ threadId,
448
+ runId: "run-error-1",
449
+ };
450
+
451
+ const runObservable = runner.run({ threadId, agent, input });
452
+ const events = await firstValueFrom(runObservable.pipe(toArray()));
453
+
454
+ // Should still receive events emitted before error
455
+ expect(events).toHaveLength(3);
456
+ expect(events.every(e => e.id?.startsWith("error-agent"))).toBe(true);
457
+
458
+ // Should be able to run again after error
459
+ const agent2 = new MockAgent([
460
+ { type: "recovery", id: "recovery-1", timestamp: new Date().toISOString(), data: {} } as BaseEvent,
461
+ ]);
462
+
463
+ const input2: RunAgentInput = {
464
+ messages: [],
465
+ state: {},
466
+ threadId,
467
+ runId: "run-error-2",
468
+ };
469
+
470
+ const run2 = runner.run({ threadId, agent: agent2, input: input2 });
471
+ const events2 = await firstValueFrom(run2.pipe(toArray()));
472
+
473
+ expect(events2).toHaveLength(1); // Only events from current run
474
+ expect(events2[0].id).toBe("recovery-1");
475
+
476
+ // Connect should have all events including from errored run
477
+ const allEvents = await firstValueFrom(runner.connect({ threadId }).pipe(toArray()));
478
+ expect(allEvents).toHaveLength(4); // 3 from error run + 1 from recovery
479
+ });
480
+
481
+ it("should properly set isRunning to false after agent error", async () => {
482
+ const threadId = "test-thread-error-3";
483
+ const agent = new ErrorThrowingAgent(1, "Quick fail");
484
+ const input: RunAgentInput = {
485
+ messages: [],
486
+ state: {},
487
+ threadId,
488
+ runId: "run-fail",
489
+ };
490
+
491
+ // Run and wait for completion (even with error)
492
+ const runObservable = runner.run({ threadId, agent, input });
493
+ await firstValueFrom(runObservable.pipe(toArray()));
494
+
495
+ // Verify thread is not running
496
+ const isRunning = await runner.isRunning({ threadId });
497
+ expect(isRunning).toBe(false);
498
+
499
+ // Should be able to run again
500
+ const agent2 = new MockAgent([
501
+ { type: "test", id: "after-error", timestamp: new Date().toISOString(), data: {} } as BaseEvent,
502
+ ]);
503
+
504
+ const input2: RunAgentInput = {
505
+ messages: [],
506
+ state: {},
507
+ threadId,
508
+ runId: "run-fail-2",
509
+ };
510
+
511
+ expect(() => {
512
+ runner.run({ threadId, agent: agent2, input: input2 });
513
+ }).not.toThrow();
514
+ });
515
+ });
516
+
517
+ describe("Edge Cases", () => {
518
+ it("should return EMPTY observable when connecting to non-existent thread", async () => {
519
+ const connectObservable = runner.connect({ threadId: "non-existent-thread" });
520
+
521
+ // EMPTY completes immediately with no values
522
+ let completed = false;
523
+ let eventCount = 0;
524
+
525
+ await new Promise<void>((resolve) => {
526
+ connectObservable.subscribe({
527
+ next: () => eventCount++,
528
+ complete: () => {
529
+ completed = true;
530
+ resolve();
531
+ }
532
+ });
533
+ });
534
+
535
+ expect(completed).toBe(true);
536
+ expect(eventCount).toBe(0);
537
+ });
538
+
539
+ it("should handle very large number of events", async () => {
540
+ const threadId = "test-thread-large";
541
+ const eventCount = 1000;
542
+ const events: BaseEvent[] = [];
543
+
544
+ for (let i = 0; i < eventCount; i++) {
545
+ events.push({
546
+ type: "bulk",
547
+ id: `bulk-${i}`,
548
+ timestamp: new Date().toISOString(),
549
+ data: { index: i, payload: "x".repeat(100) }
550
+ } as BaseEvent);
551
+ }
552
+
553
+ const agent = new MockAgent(events);
554
+ const input: RunAgentInput = {
555
+ messages: [],
556
+ state: {},
557
+ threadId,
558
+ runId: "run-large",
559
+ };
560
+
561
+ // Run with large event set
562
+ const runObservable = runner.run({ threadId, agent, input });
563
+ await firstValueFrom(runObservable.pipe(toArray()));
564
+
565
+ // Connect and verify all events are preserved
566
+ const connectObservable = runner.connect({ threadId });
567
+ const collectedEvents = await firstValueFrom(connectObservable.pipe(toArray()));
568
+
569
+ expect(collectedEvents).toHaveLength(eventCount);
570
+ expect(collectedEvents[0].id).toBe("bulk-0");
571
+ expect(collectedEvents[eventCount - 1].id).toBe(`bulk-${eventCount - 1}`);
572
+ });
573
+
574
+ it("should return false for isRunning on non-existent thread", async () => {
575
+ const isRunning = await runner.isRunning({ threadId: "non-existent" });
576
+ expect(isRunning).toBe(false);
577
+ });
578
+
579
+ it("should properly track isRunning state", async () => {
580
+ const threadId = "test-thread-running";
581
+ const agent = new DelayedEventAgent(5, 20, "running");
582
+ const input: RunAgentInput = {
583
+ messages: [],
584
+ state: {},
585
+ threadId,
586
+ runId: "run-running",
587
+ };
588
+
589
+ // Check before run
590
+ expect(await runner.isRunning({ threadId })).toBe(false);
591
+
592
+ // Start run
593
+ const runObservable = runner.run({ threadId, agent, input });
594
+
595
+ // Check during run
596
+ expect(await runner.isRunning({ threadId })).toBe(true);
597
+
598
+ // Wait for completion
599
+ await firstValueFrom(runObservable.pipe(toArray()));
600
+
601
+ // Check after run
602
+ expect(await runner.isRunning({ threadId })).toBe(false);
603
+ });
604
+
605
+ it("should throw error for stop method (not implemented)", async () => {
606
+ expect(() => {
607
+ runner.stop({ threadId: "any-thread" });
608
+ }).toThrow("Method not implemented");
609
+ });
610
+
611
+ it("should handle thread isolation correctly", async () => {
612
+ const thread1 = "test-thread-iso-1";
613
+ const thread2 = "test-thread-iso-2";
614
+
615
+ const agent1 = new MockAgent([
616
+ { type: "thread1", id: "t1-event", timestamp: new Date().toISOString(), data: {} } as BaseEvent,
617
+ ]);
618
+ const agent2 = new MockAgent([
619
+ { type: "thread2", id: "t2-event", timestamp: new Date().toISOString(), data: {} } as BaseEvent,
620
+ ]);
621
+
622
+ // Run on different threads
623
+ const run1 = runner.run({
624
+ threadId: thread1,
625
+ agent: agent1,
626
+ input: { messages: [], state: {}, threadId: thread1, runId: "run-t1" }
627
+ });
628
+ const run2 = runner.run({
629
+ threadId: thread2,
630
+ agent: agent2,
631
+ input: { messages: [], state: {}, threadId: thread2, runId: "run-t2" }
632
+ });
633
+
634
+ await Promise.all([
635
+ firstValueFrom(run1.pipe(toArray())),
636
+ firstValueFrom(run2.pipe(toArray()))
637
+ ]);
638
+
639
+ // Connect to each thread
640
+ const events1 = await firstValueFrom(runner.connect({ threadId: thread1 }).pipe(toArray()));
641
+ const events2 = await firstValueFrom(runner.connect({ threadId: thread2 }).pipe(toArray()));
642
+
643
+ // Verify isolation
644
+ expect(events1).toHaveLength(1);
645
+ expect(events1[0].id).toBe("t1-event");
646
+
647
+ expect(events2).toHaveLength(1);
648
+ expect(events2[0].id).toBe("t2-event");
649
+ });
650
+ });
651
+
652
+ describe("Complex Scenarios", () => {
653
+ it("should handle rapid sequential runs with mixed event patterns", async () => {
654
+ const threadId = "test-thread-complex-1";
655
+ const runs = [
656
+ { agent: new MockAgent([{ type: "instant", id: "instant-1", timestamp: new Date().toISOString(), data: {} } as BaseEvent]), runId: "run-1" },
657
+ { agent: new DelayedEventAgent(3, 5, "delayed"), runId: "run-2" },
658
+ { agent: new MockAgent([{ type: "instant", id: "instant-2", timestamp: new Date().toISOString(), data: {} } as BaseEvent]), runId: "run-3" },
659
+ { agent: new MultiEventAgent("multi"), runId: "run-4" },
660
+ { agent: new DelayedEventAgent(2, 10, "slow"), runId: "run-5" },
661
+ ];
662
+
663
+ for (const { agent, runId } of runs) {
664
+ const input: RunAgentInput = {
665
+ messages: [],
666
+ state: {},
667
+ threadId,
668
+ runId,
669
+ };
670
+
671
+ const run = runner.run({ threadId, agent, input });
672
+ await firstValueFrom(run.pipe(toArray()));
673
+ }
674
+
675
+ const allEvents = await firstValueFrom(runner.connect({ threadId }).pipe(toArray()));
676
+
677
+ expect(allEvents).toHaveLength(12); // 1 + 3 + 1 + 5 + 2
678
+
679
+ // Verify event ordering
680
+ expect(allEvents[0].id).toBe("instant-1");
681
+ expect(allEvents[1].id).toContain("delayed-0");
682
+ expect(allEvents[4].id).toBe("instant-2");
683
+ expect(allEvents[5].id).toContain("multi-start");
684
+ expect(allEvents[10].id).toContain("slow-0");
685
+ });
686
+
687
+ it("should handle subscriber that connects between runs", async () => {
688
+ const threadId = "test-thread-complex-2";
689
+
690
+ // First run
691
+ const agent1 = new MultiEventAgent("first");
692
+ const run1 = runner.run({
693
+ threadId,
694
+ agent: agent1,
695
+ input: { messages: [], state: {}, threadId, runId: "run-1" }
696
+ });
697
+ await firstValueFrom(run1.pipe(toArray()));
698
+
699
+ // Connect after first run - should only get first run events
700
+ const midConnectObservable = runner.connect({ threadId });
701
+ const midEvents = await firstValueFrom(midConnectObservable.pipe(toArray()));
702
+
703
+ expect(midEvents).toHaveLength(5); // Only events from first run
704
+ const firstRunEvents = midEvents.filter(e => e.id?.includes("first"));
705
+ expect(firstRunEvents).toHaveLength(5);
706
+
707
+ // Second run
708
+ const agent2 = new MultiEventAgent("second");
709
+ const run2 = runner.run({
710
+ threadId,
711
+ agent: agent2,
712
+ input: { messages: [], state: {}, threadId, runId: "run-2" }
713
+ });
714
+ await firstValueFrom(run2.pipe(toArray()));
715
+
716
+ // Connect after both runs to verify all events
717
+ const allEvents = await firstValueFrom(runner.connect({ threadId }).pipe(toArray()));
718
+ expect(allEvents).toHaveLength(10); // Events from both runs
719
+
720
+ const allFirstRunEvents = allEvents.filter(e => e.id?.includes("first"));
721
+ const allSecondRunEvents = allEvents.filter(e => e.id?.includes("second"));
722
+ expect(allFirstRunEvents).toHaveLength(5);
723
+ expect(allSecondRunEvents).toHaveLength(5);
724
+ });
725
+ });
726
+ });