-
-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathtest_openai_responses.lua
More file actions
607 lines (552 loc) · 16.2 KB
/
test_openai_responses.lua
File metadata and controls
607 lines (552 loc) · 16.2 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
local h = require("tests.helpers")
local adapter
local new_set = MiniTest.new_set
T = new_set()
T["Responses"] = new_set({
hooks = {
pre_case = function()
adapter = require("codecompanion.adapters").resolve("openai_responses")
end,
},
})
T["Responses"]["can form reasoning output"] = function()
local input = {
{
content = "Ruby ",
},
{
content = "is a ",
},
{
content = "dynamic, expressive programming language",
},
{
id = "rs_123",
encrypted_content = "somefakebase64encoding",
},
}
local expected = {
content = "Ruby is a dynamic, expressive programming language",
id = "rs_123",
encrypted_content = "somefakebase64encoding",
}
h.eq(expected, adapter.handlers.request.build_reasoning(adapter, input))
end
T["Responses"]["can output tool calls"] = function()
local output = "The weather in London is 15 degrees"
local tool_call = {
["function"] = {
arguments = '{"location": "London", "units": "celsius"}',
name = "weather",
},
id = "fc_0cf9af0f913994140068e27139a1948193bbf214a9664ec92c",
call_id = "call_a9oyUMlFhnX8HvqzlfIx5Uek",
type = "function",
}
h.eq({
content = output,
opts = {
visible = false,
},
role = "tool",
tools = {
id = "fc_0cf9af0f913994140068e27139a1948193bbf214a9664ec92c",
call_id = "call_a9oyUMlFhnX8HvqzlfIx5Uek",
},
}, adapter.handlers.tools.format_response(adapter, tool_call, output))
end
T["Responses"]["build_messages"] = new_set()
T["Responses"]["build_messages"]["messages only"] = function()
local messages = {
{
content = "You are a helpful assistant.",
role = "system",
},
{
content = "Who knows about Ruby",
role = "system",
},
{
content = "Explain Ruby in two words",
role = "user",
},
}
h.eq({
instructions = messages[1].content .. "\n" .. messages[2].content,
input = {
{
role = messages[3].role,
content = messages[3].content,
},
},
}, adapter.handlers.request.build_messages(adapter, messages))
end
T["Responses"]["build_messages"]["images"] = function()
local messages = {
{
_meta = { sent = true },
content = "How are you?",
role = "user",
},
{
_meta = { sent = true },
content = "I am fine, thanks. How can I help?",
role = "assistant",
},
{
content = "somefakebase64encoding",
role = "user",
opts = {
visible = false,
},
context = {
id = "<image>https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg</image>",
mimetype = "image/jpg",
},
_meta = {
tag = "image",
},
},
{
content = "What is this an image of?",
role = "user",
},
}
local expected = {
input = {
{
content = "How are you?",
role = "user",
},
{
content = "I am fine, thanks. How can I help?",
role = "assistant",
},
{
content = {
{
type = "input_image",
image_url = "data:image/jpg;base64,somefakebase64encoding",
},
{
type = "input_text",
text = "What is this an image of?",
},
},
role = "user",
},
},
}
h.eq(expected, adapter.handlers.request.build_messages(adapter, messages))
end
T["Responses"]["build_messages"]["multiple consecutive images are not merged as text"] = function()
local messages = {
{
content = "img1_base64",
role = "user",
opts = { visible = false },
context = { mimetype = "image/png" },
_meta = { tag = "image" },
},
{
content = "img2_base64",
role = "user",
opts = { visible = false },
context = { mimetype = "image/png" },
_meta = { tag = "image" },
},
{
content = "img3_base64",
role = "user",
opts = { visible = false },
context = { mimetype = "image/png" },
_meta = { tag = "image" },
},
{
content = "How many images do you see?",
role = "user",
},
}
local expected = {
input = {
{
role = "user",
content = {
{
type = "input_image",
image_url = "data:image/png;base64,img1_base64",
},
},
},
{
role = "user",
content = {
{
type = "input_image",
image_url = "data:image/png;base64,img2_base64",
},
},
},
{
role = "user",
content = {
{
type = "input_image",
image_url = "data:image/png;base64,img3_base64",
},
{
type = "input_text",
text = "How many images do you see?",
},
},
},
},
}
h.eq(expected, adapter.handlers.request.build_messages(adapter, messages))
end
T["Responses"]["build_messages"]["format tool calls"] = function()
local messages = {
{
role = "assistant",
tools = {
calls = {
{
_index = 0,
id = "fc_0cf9af0f913994140068e2713964448193a723d7191832a56f",
call_id = "call_RJU6xfk0OzQF3Gg9cOFS5RY7",
["function"] = {
name = "weather",
arguments = '{"location": "London", "units": "celsius"}',
},
},
{
_index = 1,
id = "fc_0cf9af0f913994140068e27139a1948193bbf214a9664ec92c",
call_id = "call_a9oyUMlFhnX8HvqzlfIx5Uek",
["function"] = {
name = "weather",
arguments = '{"location": "Paris", "units": "celsius"}',
},
},
},
},
},
}
local expected = {
{
type = "function_call",
id = "fc_0cf9af0f913994140068e2713964448193a723d7191832a56f",
call_id = "call_RJU6xfk0OzQF3Gg9cOFS5RY7",
name = "weather",
arguments = '{"location": "London", "units": "celsius"}',
},
{
type = "function_call",
id = "fc_0cf9af0f913994140068e27139a1948193bbf214a9664ec92c",
call_id = "call_a9oyUMlFhnX8HvqzlfIx5Uek",
name = "weather",
arguments = '{"location": "Paris", "units": "celsius"}',
},
}
h.eq({ input = expected }, adapter.handlers.request.build_messages(adapter, messages))
end
T["Responses"]["build_messages"]["format tool output"] = function()
local messages = {
{
role = "tool",
content = "The weather in London is 15 degrees",
tools = {
call_id = "call_RJU6xfk0OzQF3Gg9cOFS5RY7",
id = "fc_0cf9af0f913994140068e2713964448193a723d7191832a56f",
},
},
{
role = "tool",
content = "The weather in Paris is 15 degrees",
tools = {
call_id = "call_a9oyUMlFhnX8HvqzlfIx5Uek",
id = "fc_0cf9af0f913994140068e27139a1948193bbf214a9664ec92c",
},
},
}
local expected = {
{
type = "function_call_output",
call_id = "call_RJU6xfk0OzQF3Gg9cOFS5RY7",
output = "The weather in London is 15 degrees",
},
{
type = "function_call_output",
call_id = "call_a9oyUMlFhnX8HvqzlfIx5Uek",
output = "The weather in Paris is 15 degrees",
},
}
h.eq({ input = expected }, adapter.handlers.request.build_messages(adapter, messages))
end
T["Responses"]["build_messages"]["can handle reasoning"] = function()
local messages = {
{
_meta = {
sent = true,
cycle = 1,
},
content = "Can you tell me what the the weather tool is for London and Paris?",
id = 449094129,
opts = {
visible = true,
},
role = "user",
},
{
_meta = { cycle = 1 },
id = 486936684,
opts = {
visible = false,
},
reasoning = {
encrypted_content = "somefakebase64encoding",
reasoning_id = "rs_123",
content = "I need to workout the weather",
},
role = "llm",
tools = {
calls = {
{
call_id = "call_balVirseGsQYwrVoigfUfF5G",
["function"] = {
arguments = '{"location":"London, United Kingdom","units":"celsius"}',
name = "weather",
},
id = "fc_08b1c96172854ff00168e8340c67c8819387d953e1ce970203",
type = "function",
},
{
call_id = "call_zktz1zuc65awPKojbwCKMLOD",
["function"] = {
arguments = '{"location":"Paris, France","units":"celsius"}',
name = "weather",
},
id = "fc_08b1c96172854ff00168e8340c7dec8193a11f0eedd9a85af5",
type = "function",
},
},
},
},
{
content = "Ran the weather tool The weather in London, United Kingdom is 15° celsius",
_meta = { cycle = 1 },
id = 1997051449,
opts = {
visible = true,
},
role = "tool",
tools = {
call_id = "call_balVirseGsQYwrVoigfUfF5G",
id = "fc_08b1c96172854ff00168e8340c67c8819387d953e1ce970203",
},
},
{
content = "Ran the weather tool The weather in Paris, France is 15° celsius",
_meta = {
cycle = 1,
},
id = 210818266,
opts = {
visible = true,
},
role = "tool",
tools = {
call_id = "call_zktz1zuc65awPKojbwCKMLOD",
id = "fc_08b1c96172854ff00168e8340c7dec8193a11f0eedd9a85af5",
},
},
{
_meta = {
response_id = "resp_123",
cycle = 1,
},
content = "- London: 15°C\n- Paris: 15°C\n\nNeed anything else, like Fahrenheit or a weekly forecast?",
id = 933614700,
opts = {
visible = true,
},
role = "llm",
},
}
local result = adapter.handlers.request.build_messages(adapter, messages)
h.eq({
summary = { {
text = "I need to workout the weather",
type = "summary_text",
} },
encrypted_content = "somefakebase64encoding",
type = "reasoning",
}, result.input[2])
end
T["Responses"]["build_tools"] = new_set()
T["Responses"]["build_tools"]["format available tools to call"] = function()
local weather = require("tests.interactions.chat.tools.builtin.stubs.weather").schema
local tools = { weather = { weather } }
local expected = {
description = "Retrieves current weather for the given location.",
name = "weather",
parameters = {
additionalProperties = false,
properties = {
location = {
description = "City and country e.g. Bogotá, Colombia",
type = { "string", "null" },
},
units = {
description = "Units the temperature will be returned in.",
enum = { "celsius", "fahrenheit" },
type = { "string", "null" },
},
},
required = { "location", "units" },
type = "object",
},
strict = true,
type = "function",
}
-- We need to adjust the tools format slightly with Responses
-- https://platform.openai.com/docs/api-reference/responses
h.eq({ tools = { expected } }, adapter.handlers.request.build_tools(adapter, tools))
end
T["Responses"]["build_tools"]["can format for an adapter's remote tools"] = function()
local tools = {
{
["<tool>web_search</tool>"] = {
_meta = {
adapter_tool = true,
},
description = "Allow models to search the web for the latest information before generating a response.",
name = "web_search",
},
},
}
h.eq({ tools = { { type = "web_search" } } }, adapter.handlers.request.build_tools(adapter, tools))
end
T["Responses"]["No Streaming"] = new_set({
hooks = {
pre_case = function()
adapter = require("codecompanion.adapters").extend("openai_responses", {
opts = {
stream = false,
},
})
end,
},
})
T["Responses"]["No Streaming"]["chat_output"] = function()
local data = vim.fn.readfile("tests/adapters/http/stubs/openai_responses_no_streaming.txt")
data = table.concat(data, "\n")
-- Match the format of the actual request
local json = { body = data }
h.eq("Dynamic, expressive", adapter.handlers.response.parse_chat(adapter, json).output.content)
end
T["Responses"]["No Streaming"]["can process tools"] = function()
local data = vim.fn.readfile("tests/adapters/http/stubs/openai_responses_tools_no_streaming.txt")
data = table.concat(data, "\n")
local tools = {}
-- Match the format of the actual request
local json = { body = data }
adapter.handlers.response.parse_chat(adapter, json, tools)
local tool_output = {
{
_index = 1,
["function"] = {
arguments = '{"location":"London, UK","units":"celsius"}',
name = "weather",
},
call_id = "call_tgWgQU4IzqjLCPTdsbODFoOh",
id = "fc_07f118f077c91f1a0068e4319f231481969324b4a9180f3bda",
type = "function",
},
{
_index = 2,
["function"] = {
arguments = '{"location":"Paris, France","units":"celsius"}',
name = "weather",
},
call_id = "call_kGgpknBihLExIymnhL9421wC",
id = "fc_07f118f077c91f1a0068e4319f6ac481969fde8ab4fb4e0f50",
type = "function",
},
}
h.eq(tool_output, tools)
end
T["Responses"]["No Streaming"]["can output for the inline assistant"] = function()
local data = vim.fn.readfile("tests/adapters/http/stubs/openai_responses_inline.txt")
data = table.concat(data, "\n")
-- Match the format of the actual request
local json = { body = data }
h.eq(
'{"code": "print(\'Hello World\')","language": "lua","placement": "add"}',
adapter.handlers.response.parse_inline(adapter, json).output
)
end
T["Responses"]["No Streaming"]["can process reasoning output"] = function()
local data = vim.fn.readfile("tests/adapters/http/stubs/openai_responses_reasoning_no_streaming.txt")
data = table.concat(data, "\n")
-- Match the format of the actual request
local json = { body = data }
local output = adapter.handlers.response.parse_chat(adapter, json).output
h.expect_contains("**Choosing descriptive terms**", output.reasoning.content)
h.eq("rs_0a10a8c968d594670168e91d0204ac8195b26b3e4de997f65c", output.reasoning.id)
h.eq("gAAAAABo6", output.reasoning.encrypted_content)
h.eq("Dynamic expressive", output.content)
end
T["Responses"]["Streaming"] = new_set()
T["Responses"]["Streaming"]["can output streamed data into the chat buffer"] = function()
local output = ""
local lines = vim.fn.readfile("tests/adapters/http/stubs/openai_responses_streaming.txt")
for _, line in ipairs(lines) do
local chat_output = adapter.handlers.response.parse_chat(adapter, line)
if chat_output and chat_output.output.content then
output = output .. chat_output.output.content
end
end
h.expect_starts_with("Elegant language", output)
end
T["Responses"]["Streaming"]["can process reasoning output"] = function()
local output = ""
local lines = vim.fn.readfile("tests/adapters/http/stubs/openai_responses_reasoning_streaming.txt")
for _, line in ipairs(lines) do
local chat_output = adapter.handlers.response.parse_chat(adapter, line)
if chat_output and chat_output.output and chat_output.output.reasoning and chat_output.output.reasoning.content then
output = output .. chat_output.output.reasoning.content
end
end
h.expect_starts_with("**Deciding on Ruby's description**", output)
end
T["Responses"]["Streaming"]["can process tools"] = function()
-- Adds tool calls to the tools table
local tools = {}
local lines = vim.fn.readfile("tests/adapters/http/stubs/openai_responses_tools_streaming.txt")
for _, line in ipairs(lines) do
adapter.handlers.response.parse_chat(adapter, line, tools)
end
local expected = {
{
call_id = "call_hvKk3FjuupQx8xeHeVbQNZkM",
["function"] = {
arguments = '{"units":"celsius","location":"London, United Kingdom"}',
name = "weather",
},
id = "fc_0cebe04c7f5006bd0068e827962aa8819592a7e51f7fa0d0b3",
type = "function",
},
{
call_id = "call_lzBOVwzUEAuTss7Gifvp1Rwi",
["function"] = {
arguments = '{"units":"celsius","location":"Paris, France"}',
name = "weather",
},
id = "fc_0cebe04c7f5006bd0068e827963dfc81958741d3a9f70c8a94",
type = "function",
},
}
h.eq(expected, tools)
end
return T