2
import { OpenAIProvider } from '../../src/provider/OpenAIProvider';
3
import { Tool } from '../../src/types/tool.types';
4
import { OpenAI } from 'openai';
5
-import { Stream } from 'openai/streaming';
5
import { toolMocks } from '../utils/mocks/data.mock';
6
import { ExecuteToolModifiers } from '../../src/types/modifiers.types';
7
12
};
13
});
14
16
-vi.mock('openai/streaming', () => {
17
- return {
18
- Stream: vi.fn(),
19
- };
20
-});
21
-
15
describe('OpenAIProvider', () => {
16
let provider: OpenAIProvider;
17
let mockTool: Tool;
420
]);
421
});
422
});
430
-
431
- describe('handleAssistantMessage', () => {
432
- it('should handle assistant message with tool calls', async () => {
433
- const userId = 'test-user';
434
- // Cast to OpenAI.Beta.Threads.Run to bypass property requirements
435
- const run = {
436
- id: 'run-123',
437
- object: 'thread.run',
438
- status: 'requires_action',
439
- created_at: 123456789,
440
- thread_id: 'thread-123',
441
- assistant_id: 'asst-123',
442
- required_action: {
443
- type: 'submit_tool_outputs',
444
- submit_tool_outputs: {
445
- tool_calls: [
446
- {
447
- id: 'call-123',
448
- type: 'function',
449
- function: {
450
- name: 'test-tool',
451
- arguments: JSON.stringify({ input: 'test-value' }),
452
- },
453
- },
454
- ],
455
- },
456
- },
457
- } as unknown as OpenAI.Beta.Threads.Run;
458
-
459
- const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall');
460
- executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' }));
461
-
462
- const toolOutputs = await provider.handleAssistantMessage(userId, run);
463
-
464
- expect(executeToolCallSpy).toHaveBeenCalledWith(
465
- userId,
466
- run.required_action!.submit_tool_outputs!
467
- .tool_calls[0] as OpenAI.ChatCompletionMessageToolCall,
468
- undefined,
469
- undefined
470
- );
471
- expect(toolOutputs).toEqual([
472
- {
473
- tool_call_id: 'call-123',
474
- output: JSON.stringify(JSON.stringify({ result: 'success' })),
475
- },
476
- ]);
477
- });
478
-
479
- it('should handle run with no tool calls', async () => {
480
- const userId = 'test-user';
481
- const run = {
482
- id: 'run-123',
483
- object: 'thread.run',
484
- status: 'completed',
485
- created_at: 123456789,
486
- thread_id: 'thread-123',
487
- assistant_id: 'asst-123',
488
- } as unknown as OpenAI.Beta.Threads.Run;
489
-
490
- const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall');
491
-
492
- const toolOutputs = await provider.handleAssistantMessage(userId, run);
493
-
494
- expect(executeToolCallSpy).not.toHaveBeenCalled();
495
- expect(toolOutputs).toEqual([]);
496
- });
497
- });
498
-
499
- describe('waitAndHandleAssistantToolCalls', () => {
500
- it('should handle assistant tool calls until complete', async () => {
501
- const userId = 'test-user';
502
- // Create a mock OpenAI client with the necessary functions
503
- const client = {
504
- beta: {
505
- threads: {
506
- runs: {
507
- retrieve: vi.fn(),
508
- submitToolOutputs: vi.fn(),
509
- },
510
- },
511
- },
512
- } as unknown as OpenAI;
513
-
514
- // Initial run requiring action
515
- const initialRun = {
516
- id: 'run-123',
517
- object: 'thread.run',
518
- status: 'requires_action',
519
- created_at: 123456789,
520
- thread_id: 'thread-123',
521
- assistant_id: 'asst-123',
522
- required_action: {
523
- type: 'submit_tool_outputs',
524
- submit_tool_outputs: {
525
- tool_calls: [
526
- {
527
- id: 'call-123',
528
- type: 'function',
529
- function: {
530
- name: 'test-tool',
531
- arguments: JSON.stringify({ input: 'test-value' }),
532
- },
533
- },
534
- ],
535
- },
536
- },
537
- } as unknown as OpenAI.Beta.Threads.Run;
538
-
539
- // Run after submitting tool outputs, now in progress
540
- const inProgressRun = {
541
- id: 'run-123',
542
- object: 'thread.run',
543
- status: 'in_progress',
544
- created_at: 123456789,
545
- thread_id: 'thread-123',
546
- assistant_id: 'asst-123',
547
- } as unknown as OpenAI.Beta.Threads.Run;
548
-
549
- // Final completed run
550
- const completedRun = {
551
- id: 'run-123',
552
- object: 'thread.run',
553
- status: 'completed',
554
- created_at: 123456789,
555
- thread_id: 'thread-123',
556
- assistant_id: 'asst-123',
557
- } as unknown as OpenAI.Beta.Threads.Run;
558
-
559
- const thread = {
560
- id: 'thread-123',
561
- object: 'thread',
562
- created_at: 123456789,
563
- metadata: {},
564
- tool_resources: {},
565
- } as unknown as OpenAI.Beta.Threads.Thread;
566
-
567
- // Mock the handleAssistantMessage method
568
- const handleAssistantMessageSpy = vi.spyOn(provider, 'handleAssistantMessage');
569
- handleAssistantMessageSpy.mockResolvedValue([
570
- {
571
- tool_call_id: 'call-123',
572
- output: JSON.stringify({ result: 'success' }),
573
- },
574
- ]);
575
-
576
- // Setup the client mock behavior
577
- client.beta.threads.runs.submitToolOutputs = vi.fn().mockResolvedValue(inProgressRun);
578
- client.beta.threads.runs.retrieve = vi
579
- .fn()
580
- .mockResolvedValueOnce(inProgressRun)
581
- .mockResolvedValue(completedRun);
582
-
583
- // Test the method
584
- const result = await provider.waitAndHandleAssistantToolCalls(
585
- userId,
586
- client,
587
- initialRun,
588
- thread
589
- );
590
-
591
- // Verify the behavior
592
- expect(handleAssistantMessageSpy).toHaveBeenCalledWith(
593
- userId,
594
- initialRun,
595
- undefined,
596
- undefined
597
- );
598
- expect(client.beta.threads.runs.submitToolOutputs).toHaveBeenCalledWith('run-123', {
599
- thread_id: 'thread-123',
600
- tool_outputs: [
601
- {
602
- tool_call_id: 'call-123',
603
- output: JSON.stringify({ result: 'success' }),
604
- },
605
- ],
606
- });
607
- expect(client.beta.threads.runs.retrieve).toHaveBeenCalledWith('run-123', {
608
- thread_id: 'thread-123',
609
- });
610
- expect(result).toEqual(completedRun);
611
- });
612
- });
613
-
614
- describe('waitAndHandleAssistantStreamToolCalls', () => {
615
- it('should handle streamed assistant tool calls', async () => {
616
- const userId = 'test-user';
617
- const client = {
618
- beta: {
619
- threads: {
620
- runs: {
621
- retrieve: vi.fn(),
622
- submitToolOutputs: vi.fn(),
623
- },
624
- },
625
- },
626
- } as unknown as OpenAI;
627
-
628
- const thread = {
629
- id: 'thread-123',
630
- object: 'thread',
631
- created_at: 123456789,
632
- metadata: {},
633
- tool_resources: {},
634
- } as unknown as OpenAI.Beta.Threads.Thread;
635
-
636
- // Mock events in the stream
637
- const mockEvents = [
638
- {
639
- event: 'thread.run.created',
640
- data: { id: 'run-123' },
641
- },
642
- {
643
- event: 'thread.run.requires_action',
644
- data: {
645
- id: 'run-123',
646
- status: 'requires_action',
647
- required_action: {
648
- type: 'submit_tool_outputs',
649
- submit_tool_outputs: {
650
- tool_calls: [
651
- {
652
- id: 'call-123',
653
- type: 'function',
654
- function: {
655
- name: 'test-tool',
656
- arguments: JSON.stringify({ input: 'test-value' }),
657
- },
658
- },
659
- ],
660
- },
661
- },
662
- },
663
- },
664
- {
665
- event: 'thread.run.completed',
666
- data: { id: 'run-123', status: 'completed' },
667
- },
668
- ];
669
-
670
- // Create a mock async iterator for the stream
671
- const mockStream = {
672
- [Symbol.asyncIterator]: async function* () {
673
- for (const event of mockEvents) {
674
- yield event;
675
- }
676
- },
677
- } as unknown as Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>;
678
-
679
- // Mock final run
680
- const finalRun = {
681
- id: 'run-123',
682
- object: 'thread.run',
683
- status: 'completed',
684
- created_at: 123456789,
685
- thread_id: 'thread-123',
686
- assistant_id: 'asst-123',
687
- } as unknown as OpenAI.Beta.Threads.Run;
688
-
689
- // Mock the handleAssistantMessage method
690
- const handleAssistantMessageSpy = vi.spyOn(provider, 'handleAssistantMessage');
691
- handleAssistantMessageSpy.mockResolvedValue([
692
- {
693
- tool_call_id: 'call-123',
694
- output: JSON.stringify({ result: 'success' }),
695
- },
696
- ]);
697
-
698
- // Setup the client mock behavior
699
- client.beta.threads.runs.submitToolOutputs = vi.fn().mockResolvedValue(finalRun);
700
- client.beta.threads.runs.retrieve = vi.fn().mockResolvedValue(finalRun);
701
-
702
- // Collect the yielded events
703
- const collectedEvents: unknown[] = [];
704
- for await (const event of provider.waitAndHandleAssistantStreamToolCalls(
705
- userId,
706
- client,
707
- mockStream,
708
- thread
709
- )) {
710
- collectedEvents.push(event);
711
- }
712
-
713
- // Verify the behavior
714
- expect(collectedEvents).toEqual(mockEvents);
715
- expect(handleAssistantMessageSpy).toHaveBeenCalledWith(
716
- userId,
717
- mockEvents[1].data,
718
- undefined,
719
- undefined
720
- );
721
- expect(client.beta.threads.runs.submitToolOutputs).toHaveBeenCalledWith('run-123', {
722
- thread_id: 'thread-123',
723
- tool_outputs: [
724
- {
725
- tool_call_id: 'call-123',
726
- output: JSON.stringify({ result: 'success' }),
727
- },
728
- ],
729
- });
730
- });
731
- });
423
});