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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
- import { fetchWithCache } from '../src/cache';
- import { HttpProvider, processBody } from '../src/providers/http';
- jest.mock('../src/cache', () => ({
- fetchWithCache: jest.fn(),
- }));
- describe('HttpProvider', () => {
- const mockUrl = 'http://example.com/api';
- let provider: HttpProvider;
- beforeEach(() => {
- jest.clearAllMocks();
- });
- it('should call the API and return the response', async () => {
- provider = new HttpProvider(mockUrl, {
- config: {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: { key: '{{ prompt }}' },
- responseParser: (data: any) => data.result,
- },
- });
- const mockResponse = { data: { result: 'response text' }, cached: false };
- jest.mocked(fetchWithCache).mockResolvedValueOnce(mockResponse);
- const result = await provider.callApi('test prompt');
- expect(result.output).toBe('response text');
- expect(fetchWithCache).toHaveBeenCalledWith(
- mockUrl,
- expect.objectContaining({
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ key: 'test prompt' }),
- }),
- expect.any(Number),
- 'json',
- );
- });
- it('should handle API call errors', async () => {
- provider = new HttpProvider(mockUrl, {
- config: {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: { key: 'value' },
- responseParser: (data: any) => data.result,
- },
- });
- const mockError = new Error('Network error');
- jest.mocked(fetchWithCache).mockRejectedValueOnce(mockError);
- const result = await provider.callApi('test prompt');
- expect(result).toEqual({
- error: 'HTTP call error: Error: Network error',
- });
- });
- it('should use custom method and headers', async () => {
- provider = new HttpProvider(mockUrl, {
- config: {
- method: 'GET',
- headers: { Authorization: 'Bearer token' },
- body: { key: '{{ prompt }}' },
- responseParser: (data: any) => data,
- },
- });
- const mockResponse = { data: 'custom response', cached: false };
- jest.mocked(fetchWithCache).mockResolvedValueOnce(mockResponse);
- await provider.callApi('test prompt');
- expect(fetchWithCache).toHaveBeenCalledWith(
- mockUrl,
- expect.objectContaining({
- method: 'GET',
- headers: { Authorization: 'Bearer token' },
- body: JSON.stringify({ key: 'test prompt' }),
- }),
- expect.any(Number),
- 'json',
- );
- });
- const testCases = [
- { parser: (data: any) => data.custom, expected: 'parsed' },
- { parser: 'json.result', expected: 'parsed' },
- ];
- testCases.forEach(({ parser, expected }) => {
- it(`should handle response parser type: ${parser}`, async () => {
- provider = new HttpProvider(mockUrl, {
- config: {
- body: { key: '{{ prompt }}' },
- responseParser: parser,
- },
- });
- const mockResponse = { data: { result: 'parsed', custom: 'parsed' }, cached: false };
- jest.mocked(fetchWithCache).mockResolvedValueOnce(mockResponse);
- const result = await provider.callApi('test prompt');
- expect(result.output).toEqual(expected);
- });
- });
- it('should correctly render Nunjucks templates in config', async () => {
- provider = new HttpProvider(mockUrl, {
- config: {
- method: 'POST',
- headers: { 'X-Custom-Header': '{{ prompt | upper }}' },
- body: { key: '{{ prompt }}' },
- responseParser: (data: any) => data,
- },
- });
- const mockResponse = { data: 'custom response', cached: false };
- jest.mocked(fetchWithCache).mockResolvedValueOnce(mockResponse);
- await provider.callApi('test prompt');
- expect(fetchWithCache).toHaveBeenCalledWith(
- mockUrl,
- {
- method: 'POST',
- headers: { 'X-Custom-Header': 'TEST PROMPT' },
- body: JSON.stringify({ key: 'test prompt' }),
- },
- expect.any(Number),
- 'json',
- );
- });
- it('should escape JSON prompts in Nunjucks rendering', async () => {
- provider = new HttpProvider(mockUrl, {
- config: {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: { key: '{{ prompt }}' },
- responseParser: (data: any) => data,
- },
- });
- const jsonPrompt = '{"key": "value"}';
- const mockResponse = { data: 'response', cached: false };
- jest.mocked(fetchWithCache).mockResolvedValueOnce(mockResponse);
- await provider.callApi(jsonPrompt);
- expect(fetchWithCache).toHaveBeenCalledWith(
- mockUrl,
- expect.objectContaining({
- body: JSON.stringify({ key: { key: 'value' } }),
- }),
- expect.any(Number),
- 'json',
- );
- });
- it('should throw an error when creating HttpProvider with invalid config', () => {
- const invalidConfig = 'this isnt json';
- expect(() => {
- new HttpProvider(mockUrl, {
- config: invalidConfig as any,
- });
- }).toThrow(
- new Error(
- 'Invariant failed: Expected HTTP provider http://example.com/api to have a config containing {body}, but instead got "this isnt json"',
- ),
- );
- });
- it('should return provider id and string representation', () => {
- provider = new HttpProvider(mockUrl, {
- config: { body: 'yo mama' },
- });
- expect(provider.id()).toBe(mockUrl);
- expect(provider.toString()).toBe(`[HTTP Provider ${mockUrl}]`);
- });
- describe('processBody', () => {
- it('should process simple key-value pairs', () => {
- const body = { key: 'value', prompt: '{{ prompt }}' };
- const vars = { prompt: 'test prompt' };
- const result = processBody(body, vars);
- expect(result).toEqual({ key: 'value', prompt: 'test prompt' });
- });
- it('should process nested objects', () => {
- const body = {
- outer: {
- inner: '{{ prompt }}',
- static: 'value',
- },
- };
- const vars = { prompt: 'test prompt' };
- const result = processBody(body, vars);
- expect(result).toEqual({
- outer: {
- inner: 'test prompt',
- static: 'value',
- },
- });
- });
- it('should process arrays', () => {
- const body = {
- list: ['{{ prompt }}', 'static', '{{ prompt }}'],
- };
- const vars = { prompt: 'test prompt' };
- const result = processBody(body, vars);
- expect(result).toEqual({
- list: ['test prompt', 'static', 'test prompt'],
- });
- });
- it('should handle JSON strings', () => {
- const body = {
- jsonString: '{"key": "{{ prompt }}"}',
- };
- const vars = { prompt: 'test prompt' };
- const result = processBody(body, vars);
- expect(result).toEqual({
- jsonString: { key: 'test prompt' },
- });
- });
- it('should handle empty vars', () => {
- const body = { key: '{{ prompt }}' };
- const result = processBody(body, {});
- expect(result).toEqual({ key: '' });
- });
- it('should handle complex nested structures', () => {
- const body = {
- outer: {
- inner: ['{{ prompt }}', { nestedKey: '{{ prompt }}' }],
- static: 'value',
- },
- jsonArray: '[1, 2, "{{ prompt }}"]',
- };
- const vars = { prompt: 'test prompt' };
- const result = processBody(body, vars);
- expect(result).toEqual({
- outer: {
- inner: ['test prompt', { nestedKey: 'test prompt' }],
- static: 'value',
- },
- jsonArray: [1, 2, 'test prompt'],
- });
- });
- });
- });
|