-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvercelai.test.ts
More file actions
1300 lines (1120 loc) · 46.8 KB
/
Copy pathvercelai.test.ts
File metadata and controls
1300 lines (1120 loc) · 46.8 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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* VercelAI Provider E2E Tests
*
* Tests translating Vercel AI SDK format messages to/from GenAI.
* Includes real API tests (when OPENAI_API_KEY is set) and hardcoded tests.
*/
import { openai } from "@ai-sdk/openai";
import { openai as openaiV7 } from "@ai-sdk/openai7";
import { generateText } from "ai";
import { generateText as generateTextV7 } from "ai7";
import { Provider, translate } from "rosetta-ai";
import { describe, expect, it } from "vitest";
import { z } from "zod";
const hasApiKey = !!process.env.OPENAI_API_KEY;
describe("VercelAI E2E", () => {
describe.skipIf(!hasApiKey)("real Vercel AI SDK calls", { timeout: 30000 }, () => {
it("should translate a real Vercel AI SDK response", async () => {
const { text, response } = await generateText({
model: openai("gpt-4o-mini"),
messages: [
{ role: "system", content: "You are a helpful assistant. Reply in one sentence." },
{ role: "user", content: "What is 2+2?" },
],
maxOutputTokens: 50,
});
expect(text).toBeTruthy();
// Get the response messages
const responseMessages = response.messages;
expect(responseMessages.length).toBeGreaterThan(0);
// Translate the response messages
const result = translate(responseMessages, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
expect(result.messages.length).toBeGreaterThan(0);
});
it("should translate a full conversation with real API", async () => {
// Test that we can translate a user message plus the API response
const userMessage = { role: "user" as const, content: "Say hello in exactly 3 words." };
const { response } = await generateText({
model: openai("gpt-4o-mini"),
system: "You are a helpful assistant.",
messages: [userMessage],
maxOutputTokens: 20,
});
// Combine user message with response messages to get full conversation
const fullConversation = [userMessage, ...response.messages];
// Translate the full conversation
const result = translate(fullConversation, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
expect(result.messages.length).toBeGreaterThanOrEqual(2);
// Check that we have user and assistant messages
const roles = result.messages.map((m) => m.role);
expect(roles).toContain("user");
expect(roles).toContain("assistant");
});
it("should translate tool calls from real API", async () => {
const { response } = await generateText({
model: openai("gpt-4o-mini"),
messages: [{ role: "user", content: "What's the weather in Paris?" }],
tools: {
get_weather: {
description: "Get the current weather for a location",
inputSchema: z.object({
location: z.string().describe("The city name"),
}),
},
},
toolChoice: "required",
maxOutputTokens: 100,
});
// Translate all messages
const result = translate(response.messages, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
// Should have assistant message with tool call
const assistantMsg = result.messages.find((m) => m.role === "assistant");
expect(assistantMsg).toBeDefined();
// Check for tool_call part
const toolCallPart = assistantMsg?.parts.find((p) => p.type === "tool_call");
expect(toolCallPart).toBeDefined();
expect((toolCallPart as { name: string }).name).toBe("get_weather");
});
it("should translate real API response to Promptl format", async () => {
const { response } = await generateText({
model: openai("gpt-4o-mini"),
messages: [{ role: "user", content: "Reply with exactly: Hello World" }],
maxOutputTokens: 16,
});
const result = translate(response.messages, {
from: Provider.VercelAI,
to: Provider.Promptl,
});
expect(result.messages.length).toBeGreaterThan(0);
const assistantMsg = result.messages.find((m) => m.role === "assistant");
expect(assistantMsg).toBeDefined();
});
});
// End-to-end round-trip against the real v7 SDK (aliased as `ai7` / `@ai-sdk/openai7`).
describe.skipIf(!hasApiKey)("real v7 SDK round-trip", { timeout: 30000 }, () => {
it("should ingest real v7 generateText output (v7 -> Rosetta)", async () => {
const { response } = await generateTextV7({
model: openaiV7("gpt-4o-mini"),
messages: [{ role: "user", content: "What is 2+2? Reply in one sentence." }],
maxOutputTokens: 50,
});
// response.messages are real v7 ModelMessage[]
const result = translate(response.messages, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
expect(result.messages.length).toBeGreaterThan(0);
expect(result.messages.some((m) => m.role === "assistant")).toBe(true);
});
it("should feed Rosetta's v6 output back into v7 generateText (Rosetta -> v7)", async () => {
// Rosetta emits canonical v6 with system inline.
const { messages: rosettaMessages } = translate(
[
{ role: "system", parts: [{ type: "text", content: "You are concise. Reply in 3 words." }] },
{ role: "user", parts: [{ type: "text", content: "Greet me." }] },
],
{ from: Provider.GenAI, to: Provider.VercelAI },
);
const { text } = await generateTextV7({
model: openaiV7("gpt-4o-mini"),
messages: rosettaMessages,
// REQUIRED: Rosetta emits system inline; v7 rejects inline system by default.
allowSystemInMessages: true,
maxOutputTokens: 20,
});
expect(text).toBeTruthy();
});
it("should round-trip real v7 tool calls through Rosetta", async () => {
const { response } = await generateTextV7({
model: openaiV7("gpt-4o-mini"),
messages: [{ role: "user", content: "What's the weather in Paris?" }],
tools: {
get_weather: {
description: "Get the current weather for a location",
inputSchema: z.object({ location: z.string().describe("The city name") }),
},
},
toolChoice: "required",
maxOutputTokens: 100,
});
const result = translate(response.messages, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
const assistantMsg = result.messages.find((m) => m.role === "assistant");
expect(assistantMsg).toBeDefined();
const toolCallPart = assistantMsg?.parts.find((p) => p.type === "tool_call");
expect(toolCallPart).toBeDefined();
expect((toolCallPart as { name: string }).name).toBe("get_weather");
});
});
describe("hardcoded messages (no API key required)", () => {
it("should translate simple VercelAI messages to GenAI", () => {
const vercelAIMessages = [
{ role: "system" as const, content: "You are a helpful assistant." },
{ role: "user" as const, content: "What is the capital of France?" },
{ role: "assistant" as const, content: "The capital of France is Paris." },
];
const result = translate(vercelAIMessages, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
// GenAI extracts system messages to the system field
expect(result.system).toBeDefined();
expect(result.messages).toHaveLength(2);
expect(result.messages[0]?.role).toBe("user");
expect(result.messages[1]?.role).toBe("assistant");
});
it("should translate image content", () => {
const vercelAIMessages = [
{
role: "user" as const,
content: [
{ type: "text" as const, text: "What's in this image?" },
{ type: "image" as const, image: "https://example.com/cat.jpg" },
],
},
];
const result = translate(vercelAIMessages, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.parts).toHaveLength(2);
expect(result.messages[0]?.parts[0]?.type).toBe("text");
expect(result.messages[0]?.parts[1]?.type).toBe("uri");
});
it("should translate tool calls", () => {
const vercelAIMessages = [
{ role: "user" as const, content: "What's the weather in London?" },
{
role: "assistant" as const,
content: [
{
type: "tool-call" as const,
toolCallId: "call_abc123",
toolName: "get_weather",
input: { location: "London", unit: "celsius" },
},
],
},
];
const result = translate(vercelAIMessages, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(2);
const toolCallMsg = result.messages[1];
expect(toolCallMsg?.role).toBe("assistant");
expect(toolCallMsg?.parts[0]?.type).toBe("tool_call");
const toolCallPart = toolCallMsg?.parts[0] as { name: string; arguments: unknown };
expect(toolCallPart.name).toBe("get_weather");
expect(toolCallPart.arguments).toEqual({ location: "London", unit: "celsius" });
});
it("should translate tool results", () => {
const vercelAIMessages = [
{
role: "tool" as const,
content: [
{
type: "tool-result" as const,
toolCallId: "call_abc123",
toolName: "get_weather",
output: { type: "json" as const, value: { temperature: 15, condition: "cloudy" } },
},
],
},
];
const result = translate(vercelAIMessages, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
const toolMsg = result.messages[0];
expect(toolMsg?.role).toBe("tool");
expect(toolMsg?.parts[0]?.type).toBe("tool_call_response");
});
it("should translate reasoning content", () => {
const vercelAIMessages = [
{
role: "assistant" as const,
content: [
{ type: "reasoning" as const, text: "Let me think about this..." },
{ type: "text" as const, text: "The answer is 42." },
],
},
];
const result = translate(vercelAIMessages, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
expect(result.messages[0]?.parts[0]?.type).toBe("reasoning");
expect(result.messages[0]?.parts[1]?.type).toBe("text");
});
it("should auto-detect VercelAI format", () => {
const vercelAIMessages = [
{ role: "system" as const, content: "You are helpful." },
{ role: "user" as const, content: "Hello" },
];
// No explicit 'from' - should auto-infer
const result = translate(vercelAIMessages);
expect(result.messages).toBeDefined();
expect(result.messages.length).toBeGreaterThan(0);
});
it("should translate VercelAI to Promptl via GenAI", () => {
const vercelAIMessages = [
{ role: "system" as const, content: "You are a helpful assistant." },
{ role: "user" as const, content: "Hello!" },
{ role: "assistant" as const, content: "Hi there!" },
];
const result = translate(vercelAIMessages, {
from: Provider.VercelAI,
to: Provider.Promptl,
});
expect(result.messages).toHaveLength(3);
expect(result.messages[0]?.role).toBe("system");
expect(result.messages[1]?.role).toBe("user");
expect(result.messages[2]?.role).toBe("assistant");
});
it("should round-trip VercelAI -> GenAI -> VercelAI", () => {
const original = [
{ role: "system" as const, content: "You are helpful." },
{ role: "user" as const, content: "Hello" },
{ role: "assistant" as const, content: "Hi there!" },
];
// VercelAI -> GenAI
const genAI = translate(original, {
from: Provider.VercelAI,
to: Provider.GenAI,
});
// GenAI extracts system to separate field, so we need to pass it back
// GenAI -> VercelAI (include system)
const backToVercelAI = translate(genAI.messages, {
from: Provider.GenAI,
to: Provider.VercelAI,
system: genAI.system,
});
expect(backToVercelAI.messages).toHaveLength(3);
expect(backToVercelAI.messages[0]?.content).toBe("You are helpful.");
expect(backToVercelAI.messages[1]?.content).toBe("Hello");
expect(backToVercelAI.messages[2]?.content).toBe("Hi there!");
});
});
// v7-only part shapes a basic generateText call won't emit; run without an API key.
describe("hardcoded v7 message shapes (no API key required)", () => {
it("should translate a tagged FileData (data) file part to a blob", () => {
const messages = [
{
role: "user" as const,
content: [
{ type: "file" as const, data: { type: "data" as const, data: "SGVsbG8=" }, mediaType: "text/plain" },
],
},
];
const result = translate(messages, { from: Provider.VercelAI, to: Provider.GenAI });
expect(result.messages[0]?.parts[0]?.type).toBe("blob");
});
it("should translate a provider-reference file part to a GenAI file part", () => {
const messages = [
{
role: "user" as const,
content: [
{
type: "file" as const,
data: { type: "reference" as const, reference: { openai: "file-abc" } },
mediaType: "application/pdf",
},
],
},
];
const result = translate(messages, { from: Provider.VercelAI, to: Provider.GenAI });
const part = result.messages[0]?.parts[0] as { type: string; file_id: string };
expect(part.type).toBe("file");
expect(part.file_id).toBe("file-abc");
});
it("should translate an image with a ProviderReference to an image file part", () => {
const messages = [
{
role: "user" as const,
content: [{ type: "image" as const, image: { openai: "file-img" }, mediaType: "image/png" }],
},
];
const result = translate(messages, { from: Provider.VercelAI, to: Provider.GenAI });
const part = result.messages[0]?.parts[0] as { type: string; modality: string };
expect(part.type).toBe("file");
expect(part.modality).toBe("image");
});
it("should keep a v7 custom part to GenAI but drop it on v6 emit", () => {
const messages = [
{
role: "assistant" as const,
content: [
{ type: "custom" as const, kind: "acme.widget" },
{ type: "text" as const, text: "hi" },
],
},
];
// to GenAI: custom preserved as a generic part
const toGenAI = translate(messages, { from: Provider.VercelAI, to: Provider.GenAI });
expect(toGenAI.messages[0]?.parts.some((p) => p.type === "custom")).toBe(true);
// to VercelAI (canonical v6): custom dropped, text retained
const toVercel = translate(messages, { from: Provider.VercelAI, to: Provider.VercelAI });
const content = toVercel.messages[0]?.content;
const types = Array.isArray(content) ? content.map((p) => (p as { type: string }).type) : ["text"];
expect(types).not.toContain("custom");
expect(types).toContain("text");
});
it("should translate a v7 reasoning-file assistant part to a blob", () => {
const messages = [
{
role: "assistant" as const,
content: [
{
type: "reasoning-file" as const,
data: { type: "data" as const, data: "cmVhc29u" },
mediaType: "text/plain",
},
],
},
];
const result = translate(messages, { from: Provider.VercelAI, to: Provider.GenAI });
expect(result.messages[0]?.parts[0]?.type).toBe("blob");
});
it("should translate tool-result content with v7 file / file-reference / image-file-reference members", () => {
const messages = [
{
role: "tool" as const,
content: [
{
type: "tool-result" as const,
toolCallId: "call_1",
toolName: "lookup",
output: {
type: "content" as const,
value: [
{ type: "file" as const, data: { type: "data" as const, data: "Zm9v" }, mediaType: "text/plain" },
{ type: "file-reference" as const, providerReference: { openai: "file-a" } },
{ type: "image-file-reference" as const, providerReference: { openai: "file-b" } },
],
},
},
],
},
];
const result = translate(messages, { from: Provider.VercelAI, to: Provider.GenAI });
expect(result.messages[0]?.parts[0]?.type).toBe("tool_call_response");
});
it("should accept system via the separated system field (string)", () => {
const result = translate([{ role: "user" as const, content: "Hello" }], {
from: Provider.VercelAI,
to: Provider.GenAI,
system: "You are helpful.",
});
// GenAI extracts system instructions into the system field
expect(result.system).toBeDefined();
});
it("should accept system as an array of SystemModelMessage objects (emitted inline)", () => {
const result = translate([{ role: "user" as const, content: "Hello" }], {
from: Provider.VercelAI,
to: Provider.VercelAI,
system: [
{ role: "system" as const, content: "First." },
{ role: "system" as const, content: "Second." },
],
});
// VercelAI emits system inline, prepended in order before the conversation
expect(result.messages[0]?.content).toBe("First.");
expect(result.messages[1]?.content).toBe("Second.");
expect(result.messages[2]?.role).toBe("user");
});
});
describe("Promptl to VercelAI translation", () => {
it("should translate simple Promptl conversation to VercelAI", () => {
const promptlMessages = [
{ role: "system" as const, content: [{ type: "text" as const, text: "You are helpful." }] },
{ role: "user" as const, content: [{ type: "text" as const, text: "Hello" }] },
{ role: "assistant" as const, content: [{ type: "text" as const, text: "Hi there!" }] },
];
const result = translate(promptlMessages, {
from: Provider.Promptl,
to: Provider.VercelAI,
});
expect(result.messages).toHaveLength(3);
expect(result.messages[0]?.role).toBe("system");
expect(result.messages[1]?.role).toBe("user");
expect(result.messages[2]?.role).toBe("assistant");
});
it("should translate Promptl tool calls to VercelAI format", () => {
const promptlMessages = [
{ role: "user" as const, content: [{ type: "text" as const, text: "What's the weather?" }] },
{
role: "assistant" as const,
content: [
{
type: "tool-call" as const,
toolCallId: "call_123",
toolName: "get_weather",
args: { location: "Paris" },
},
],
},
{
role: "tool" as const,
toolName: "get_weather",
toolId: "call_123",
content: [{ type: "text" as const, text: '{"temp": 20}' }],
},
];
const result = translate(promptlMessages, {
from: Provider.Promptl,
to: Provider.VercelAI,
});
// Verify VercelAI format structure
expect(result.messages).toHaveLength(3);
const toolCallMsg = result.messages[1];
const content = toolCallMsg?.content as Array<{ type: string; toolName: string }>;
expect(content[0]?.type).toBe("tool-call");
expect(content[0]?.toolName).toBe("get_weather");
});
it("should translate Promptl images to VercelAI format", () => {
const promptlMessages = [
{
role: "user" as const,
content: [
{ type: "text" as const, text: "What's in this image?" },
{ type: "image" as const, image: "https://example.com/cat.jpg" },
],
},
];
const result = translate(promptlMessages, {
from: Provider.Promptl,
to: Provider.VercelAI,
});
const content = result.messages[0]?.content as Array<{ type: string }>;
expect(content[1]?.type).toBe("image");
});
it("should translate Promptl reasoning content to VercelAI", () => {
const promptlMessages = [
{
role: "assistant" as const,
content: [
{ type: "reasoning" as const, text: "Let me think about this..." },
{ type: "text" as const, text: "The answer is 42." },
],
},
];
const result = translate(promptlMessages, {
from: Provider.Promptl,
to: Provider.VercelAI,
});
const content = result.messages[0]?.content as Array<{ type: string }>;
expect(content[0]?.type).toBe("reasoning");
expect(content[1]?.type).toBe("text");
});
});
describe("VercelAI to Promptl translation", () => {
it("should translate simple VercelAI conversation to Promptl", () => {
const vercelAIMessages = [
{ role: "system" as const, content: "You are helpful." },
{ role: "user" as const, content: "Hello" },
{ role: "assistant" as const, content: "Hi there!" },
];
const result = translate(vercelAIMessages, {
from: Provider.VercelAI,
to: Provider.Promptl,
});
expect(result.messages).toHaveLength(3);
expect(result.messages[0]?.role).toBe("system");
expect(result.messages[1]?.role).toBe("user");
expect(result.messages[2]?.role).toBe("assistant");
});
it("should translate VercelAI multimodal content to Promptl", () => {
const vercelAIMessages = [
{
role: "user" as const,
content: [
{ type: "text" as const, text: "What's in this image?" },
{ type: "image" as const, image: "https://example.com/cat.jpg" },
],
},
];
const result = translate(vercelAIMessages, {
from: Provider.VercelAI,
to: Provider.Promptl,
});
const content = result.messages[0]?.content as Array<{ type: string }>;
expect(content).toHaveLength(2);
expect(content[0]?.type).toBe("text");
expect(content[1]?.type).toBe("image");
});
it("should translate VercelAI tool calls to Promptl format", () => {
const vercelAIMessages = [
{ role: "user" as const, content: "What's the weather?" },
{
role: "assistant" as const,
content: [
{
type: "tool-call" as const,
toolCallId: "call_123",
toolName: "get_weather",
input: { location: "Paris" },
},
],
},
{
role: "tool" as const,
content: [
{
type: "tool-result" as const,
toolCallId: "call_123",
toolName: "get_weather",
output: { type: "json" as const, value: { temp: 20 } },
},
],
},
];
const result = translate(vercelAIMessages, {
from: Provider.VercelAI,
to: Provider.Promptl,
});
// Verify Promptl format structure
const toolCallContent = result.messages[1]?.content as Array<{ type: string; toolName?: string }>;
expect(toolCallContent[0]?.type).toBe("tool-call");
expect(toolCallContent[0]?.toolName).toBe("get_weather");
const toolResultMsg = result.messages[2];
expect(toolResultMsg?.role).toBe("tool");
});
it("should translate VercelAI reasoning to Promptl format", () => {
const vercelAIMessages = [
{
role: "assistant" as const,
content: [
{ type: "reasoning" as const, text: "Let me think..." },
{ type: "text" as const, text: "The answer is 42." },
],
},
];
const result = translate(vercelAIMessages, {
from: Provider.VercelAI,
to: Provider.Promptl,
});
const content = result.messages[0]?.content as Array<{ type: string }>;
expect(content[0]?.type).toBe("reasoning");
expect(content[1]?.type).toBe("text");
});
it("should round-trip Promptl -> VercelAI -> Promptl", () => {
const originalPromptl = [
{ role: "system" as const, content: [{ type: "text" as const, text: "You are helpful." }] },
{ role: "user" as const, content: [{ type: "text" as const, text: "Hello" }] },
{
role: "assistant" as const,
content: [
{
type: "tool-call" as const,
toolCallId: "call_1",
toolName: "greet",
args: { name: "World" },
},
],
},
];
// Promptl -> VercelAI
const vercelAI = translate(originalPromptl, {
from: Provider.Promptl,
to: Provider.VercelAI,
});
// VercelAI -> Promptl
const backToPromptl = translate(vercelAI.messages, {
from: Provider.VercelAI,
to: Provider.Promptl,
});
expect(backToPromptl.messages).toHaveLength(3);
expect((backToPromptl.messages[2]?.content[0] as { toolName: string }).toolName).toBe("greet");
});
});
describe("metadata preservation (Promptl -> VercelAI with _promptlSourceMap)", () => {
const sourceMap = [{ start: 0, end: 10, identifier: "test" }];
describe("passthrough mode", () => {
it("should preserve _promptlSourceMap on user message when translating Promptl to VercelAI", () => {
const promptlMessages = [
{
role: "user" as const,
content: [{ type: "text" as const, text: "Hello", _promptlSourceMap: sourceMap }],
},
];
const result = translate(promptlMessages, {
from: Provider.Promptl,
to: Provider.VercelAI,
providerMetadata: "passthrough",
});
// Content should be array to preserve part metadata
expect(Array.isArray(result.messages[0]?.content)).toBe(true);
const content = result.messages[0]?.content as Array<{
type: string;
text: string;
_promptlSourceMap?: unknown;
}>;
expect(content[0]?._promptlSourceMap).toEqual(sourceMap);
});
it("should lose _promptlSourceMap on system message in passthrough mode (string content)", () => {
const promptlMessages = [
{
role: "system" as const,
content: [{ type: "text" as const, text: "Be helpful", _promptlSourceMap: sourceMap }],
},
];
const result = translate(promptlMessages, {
from: Provider.Promptl,
to: Provider.VercelAI,
providerMetadata: "passthrough",
});
// System message uses string content, no parts to apply metadata to
// In passthrough mode, _partsMetadata is stripped (not spread), so metadata is lost
expect(result.messages[0]?.content).toBe("Be helpful");
expect((result.messages[0] as { _promptlSourceMap?: unknown })._promptlSourceMap).toBeUndefined();
});
it("should preserve _promptlSourceMap on assistant message when translating Promptl to VercelAI", () => {
const promptlMessages = [
{
role: "assistant" as const,
content: [{ type: "text" as const, text: "Response", _promptlSourceMap: sourceMap }],
},
];
const result = translate(promptlMessages, {
from: Provider.Promptl,
to: Provider.VercelAI,
providerMetadata: "passthrough",
});
// Content should be array to preserve part metadata
expect(Array.isArray(result.messages[0]?.content)).toBe(true);
const content = result.messages[0]?.content as Array<{
type: string;
text: string;
_promptlSourceMap?: unknown;
}>;
expect(content[0]?._promptlSourceMap).toEqual(sourceMap);
});
});
describe("preserve mode", () => {
it("should preserve _promptlSourceMap in _providerMetadata on user message", () => {
const promptlMessages = [
{
role: "user" as const,
content: [{ type: "text" as const, text: "Hello", _promptlSourceMap: sourceMap }],
},
];
const result = translate(promptlMessages, {
from: Provider.Promptl,
to: Provider.VercelAI,
providerMetadata: "preserve",
});
// Content should be array to preserve part metadata
expect(Array.isArray(result.messages[0]?.content)).toBe(true);
const content = result.messages[0]?.content as Array<{
type: string;
text: string;
_providerMetadata?: { _promptlSourceMap?: unknown };
}>;
expect(content[0]?._providerMetadata?._promptlSourceMap).toEqual(sourceMap);
});
it("should preserve _promptlSourceMap in _providerMetadata._partsMetadata on system message", () => {
const promptlMessages = [
{
role: "system" as const,
content: [{ type: "text" as const, text: "Be helpful", _promptlSourceMap: sourceMap }],
},
];
const result = translate(promptlMessages, {
from: Provider.Promptl,
to: Provider.VercelAI,
providerMetadata: "preserve",
});
// System message uses string content, part metadata is stored in _partsMetadata
expect(result.messages[0]?.content).toBe("Be helpful");
expect(
(result.messages[0] as { _providerMetadata?: { _partsMetadata?: { _promptlSourceMap?: unknown } } })
._providerMetadata?._partsMetadata?._promptlSourceMap,
).toEqual(sourceMap);
});
});
});
describe("round-trip metadata preservation (Promptl -> VercelAI -> Promptl)", () => {
const sourceMap = [{ start: 15, end: 25, identifier: "color" }];
describe("passthrough mode", () => {
it("should preserve _promptlSourceMap through full round-trip for user message", () => {
const originalPromptl = [
{
role: "user" as const,
content: [{ type: "text" as const, text: "Why is the sky ?", _promptlSourceMap: sourceMap }],
},
];
// Promptl -> VercelAI (passthrough)
const vercelAI = translate(originalPromptl, {
from: Provider.Promptl,
to: Provider.VercelAI,
providerMetadata: "passthrough",
});
// Verify metadata is on VercelAI message
const vercelContent = vercelAI.messages[0]?.content as Array<{ _promptlSourceMap?: unknown }>;
expect(vercelContent[0]?._promptlSourceMap).toEqual(sourceMap);
// VercelAI -> Promptl (passthrough)
const backToPromptl = translate(vercelAI.messages, {
from: Provider.VercelAI,
to: Provider.Promptl,
providerMetadata: "passthrough",
});
// Verify metadata survives full round-trip
const promptlContent = backToPromptl.messages[0]?.content as Array<{ _promptlSourceMap?: unknown }>;
expect(promptlContent[0]?._promptlSourceMap).toEqual(sourceMap);
});
it("should lose _promptlSourceMap through round-trip for system message in passthrough mode", () => {
const systemSourceMap = [{ start: 30, end: 30, identifier: "mode" }];
const originalPromptl = [
{
role: "system" as const,
content: [
{ type: "text" as const, text: "Answer the following question :", _promptlSourceMap: systemSourceMap },
],
},
];
// Promptl -> VercelAI (passthrough)
const vercelAI = translate(originalPromptl, {
from: Provider.Promptl,
to: Provider.VercelAI,
providerMetadata: "passthrough",
});
// System uses string content, _partsMetadata is stripped in passthrough, so metadata is lost
expect((vercelAI.messages[0] as { _promptlSourceMap?: unknown })._promptlSourceMap).toBeUndefined();
// VercelAI -> Promptl (passthrough)
const backToPromptl = translate(vercelAI.messages, {
from: Provider.VercelAI,
to: Provider.Promptl,
providerMetadata: "passthrough",
});
// Metadata was lost in the Promptl -> VercelAI step
const sysMsg = backToPromptl.messages[0];
const msgLevelMeta = (sysMsg as { _promptlSourceMap?: unknown })._promptlSourceMap;
const contentLevelMeta = (sysMsg?.content[0] as { _promptlSourceMap?: unknown })?._promptlSourceMap;
expect(msgLevelMeta ?? contentLevelMeta).toBeUndefined();
});
it("should semi-preserve _promptlSourceMap through full round-trip for conversation", () => {
const systemMap = [{ start: 30, end: 30, identifier: "mode" }];
const userMap = [{ start: 15, end: 15, identifier: "color" }];
const originalPromptl = [
{
role: "system" as const,
content: [{ type: "text" as const, text: "Answer the following question :", _promptlSourceMap: systemMap }],
},
{
role: "user" as const,
content: [{ type: "text" as const, text: "Why is the sky ?", _promptlSourceMap: userMap }],
},
];
// Promptl -> VercelAI (passthrough)
const vercelAI = translate(originalPromptl, {
from: Provider.Promptl,
to: Provider.VercelAI,
providerMetadata: "passthrough",
});
// VercelAI -> Promptl (passthrough)
const backToPromptl = translate(vercelAI.messages, {
from: Provider.VercelAI,
to: Provider.Promptl,
providerMetadata: "passthrough",
});
expect(backToPromptl.messages).toHaveLength(2);
// Check system message metadata
const systemContent = backToPromptl.messages[0]?.content as Array<{ _promptlSourceMap?: unknown }>;
expect(systemContent[0]?._promptlSourceMap).toBeUndefined();
// Check user message metadata
const userContent = backToPromptl.messages[1]?.content as Array<{ _promptlSourceMap?: unknown }>;
expect(userContent[0]?._promptlSourceMap).toEqual(userMap);
});
});
describe("preserve mode", () => {
it("should preserve _promptlSourceMap through full round-trip for user message", () => {
const originalPromptl = [
{
role: "user" as const,
content: [{ type: "text" as const, text: "Why is the sky ?", _promptlSourceMap: sourceMap }],
},
];
// Promptl -> VercelAI (preserve)
const vercelAI = translate(originalPromptl, {
from: Provider.Promptl,
to: Provider.VercelAI,
providerMetadata: "preserve",
});
// Verify metadata is in _providerMetadata on VercelAI message
const vercelContent = vercelAI.messages[0]?.content as Array<{
_providerMetadata?: { _promptlSourceMap?: unknown };
}>;
expect(vercelContent[0]?._providerMetadata?._promptlSourceMap).toEqual(sourceMap);
// VercelAI -> Promptl (preserve)