1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
- import OpenAI from 'openai';
- import { disableCache, enableCache } from '../../../src/cache';
- import { OpenAiAssistantProvider } from '../../../src/providers/openai/assistant';
- jest.mock('openai');
- describe('OpenAI Provider', () => {
- beforeEach(() => {
- jest.resetAllMocks();
- disableCache();
- });
- afterEach(() => {
- enableCache();
- });
- describe('OpenAiAssistantProvider', () => {
- let mockClient: any;
- beforeEach(() => {
- jest.clearAllMocks();
- mockClient = {
- beta: {
- threads: {
- createAndRun: jest.fn(),
- runs: {
- retrieve: jest.fn(),
- submitToolOutputs: jest.fn(),
- steps: {
- list: jest.fn(),
- },
- },
- messages: {
- retrieve: jest.fn(),
- },
- },
- },
- };
- jest.mocked(OpenAI).mockImplementation(function (this: any) {
- Object.assign(this, mockClient);
- return this;
- });
- });
- const provider = new OpenAiAssistantProvider('test-assistant-id', {
- config: {
- apiKey: 'test-key',
- organization: 'test-org',
- functionToolCallbacks: {
- test_function: async (args: string) => 'Function result',
- },
- },
- });
- it('should handle successful assistant completion', async () => {
- const mockRun = {
- id: 'run_123',
- thread_id: 'thread_123',
- status: 'completed',
- };
- const mockSteps = {
- data: [
- {
- id: 'step_1',
- step_details: {
- type: 'message_creation',
- message_creation: {
- message_id: 'msg_1',
- },
- },
- },
- ],
- };
- const mockMessage = {
- role: 'assistant',
- content: [
- {
- type: 'text',
- text: {
- value: 'Test response',
- },
- },
- ],
- };
- mockClient.beta.threads.createAndRun.mockResolvedValue(mockRun);
- mockClient.beta.threads.runs.retrieve.mockResolvedValue(mockRun);
- mockClient.beta.threads.runs.steps.list.mockResolvedValue(mockSteps);
- mockClient.beta.threads.messages.retrieve.mockResolvedValue(mockMessage);
- const result = await provider.callApi('Test prompt');
- expect(result.output).toBe('[Assistant] Test response');
- expect(mockClient.beta.threads.createAndRun).toHaveBeenCalledTimes(1);
- expect(mockClient.beta.threads.runs.retrieve).toHaveBeenCalledTimes(1);
- expect(mockClient.beta.threads.runs.steps.list).toHaveBeenCalledTimes(1);
- expect(mockClient.beta.threads.messages.retrieve).toHaveBeenCalledTimes(1);
- });
- it('should handle function calling', async () => {
- const mockRun = {
- id: 'run_123',
- thread_id: 'thread_123',
- status: 'requires_action',
- required_action: {
- type: 'submit_tool_outputs',
- submit_tool_outputs: {
- tool_calls: [
- {
- id: 'call_123',
- type: 'function',
- function: {
- name: 'test_function',
- arguments: '{"arg": "value"}',
- },
- },
- ],
- },
- },
- };
- const mockCompletedRun = {
- ...mockRun,
- status: 'completed',
- required_action: null,
- };
- const mockSteps = {
- data: [
- {
- id: 'step_1',
- step_details: {
- type: 'tool_calls',
- tool_calls: [
- {
- type: 'function',
- function: {
- name: 'test_function',
- arguments: '{"arg": "value"}',
- output: 'Function result',
- },
- },
- ],
- },
- },
- ],
- };
- mockClient.beta.threads.createAndRun.mockResolvedValue(mockRun);
- mockClient.beta.threads.runs.retrieve
- .mockResolvedValueOnce(mockRun)
- .mockResolvedValueOnce(mockCompletedRun);
- mockClient.beta.threads.runs.submitToolOutputs.mockResolvedValue(mockCompletedRun);
- mockClient.beta.threads.runs.steps.list.mockResolvedValue(mockSteps);
- const result = await provider.callApi('Test prompt');
- expect(result.output).toBe(
- '[Call function test_function with arguments {"arg": "value"}]\n\n[Function output: Function result]',
- );
- expect(mockClient.beta.threads.createAndRun).toHaveBeenCalledTimes(1);
- expect(mockClient.beta.threads.runs.retrieve).toHaveBeenCalledTimes(2);
- expect(mockClient.beta.threads.runs.submitToolOutputs).toHaveBeenCalledTimes(1);
- expect(mockClient.beta.threads.runs.steps.list).toHaveBeenCalledTimes(1);
- });
- it('should handle run failures', async () => {
- const mockRun = {
- id: 'run_123',
- thread_id: 'thread_123',
- status: 'failed',
- last_error: {
- message: 'Test error message',
- },
- };
- mockClient.beta.threads.createAndRun.mockResolvedValue(mockRun);
- mockClient.beta.threads.runs.retrieve.mockResolvedValue(mockRun);
- const result = await provider.callApi('Test prompt');
- expect(result.error).toBe('Thread run failed: Test error message');
- expect(mockClient.beta.threads.createAndRun).toHaveBeenCalledTimes(1);
- expect(mockClient.beta.threads.runs.retrieve).toHaveBeenCalledTimes(1);
- });
- it('should handle API errors', async () => {
- const error = new OpenAI.APIError(500, {}, 'API Error', {});
- Object.defineProperty(error, 'type', {
- value: 'API Error',
- writable: true,
- configurable: true,
- });
- Object.defineProperty(error, 'message', {
- value: 'API Error',
- writable: true,
- configurable: true,
- });
- mockClient.beta.threads.createAndRun.mockRejectedValueOnce(error);
- const provider = new OpenAiAssistantProvider('test-assistant-id', {
- config: {
- apiKey: 'test-key',
- },
- });
- const result = await provider.callApi('Test prompt');
- expect(result.error).toBe('API error: API Error API Error');
- expect(mockClient.beta.threads.createAndRun).toHaveBeenCalledTimes(1);
- });
- it('should handle missing API key', async () => {
- const providerNoKey = new OpenAiAssistantProvider('test-assistant-id');
- process.env.OPENAI_API_KEY = '';
- await expect(providerNoKey.callApi('Test prompt')).rejects.toThrow(
- 'OpenAI API key is not set',
- );
- process.env.OPENAI_API_KEY = 'test-key'; // Restore for other tests
- });
- });
- });
|