-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanthropic.test.ts
More file actions
613 lines (539 loc) · 21 KB
/
Copy pathanthropic.test.ts
File metadata and controls
613 lines (539 loc) · 21 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
/**
* Anthropic Provider E2E Tests
*
* Tests translating Anthropic Messages API format messages to GenAI.
* Includes both hardcoded messages and real API calls (when ANTHROPIC_API_KEY is set).
* This is a source-only provider, so only toGenAI translations are tested.
*/
import Anthropic from "@anthropic-ai/sdk";
import { Provider, translate } from "rosetta-ai";
import { describe, expect, it } from "vitest";
const hasApiKey = !!process.env.ANTHROPIC_API_KEY;
// Current Anthropic model used for the live API calls below. Bump when models are retired.
const MODEL = "claude-sonnet-4-6";
describe("Anthropic E2E", () => {
describe.skipIf(!hasApiKey)("real Anthropic API calls", { timeout: 30000 }, () => {
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
it("should translate a real Anthropic message response", async () => {
const message = await anthropic.messages.create({
model: MODEL,
max_tokens: 50,
messages: [{ role: "user", content: "What is 2+2? Reply in one word." }],
});
// Translate the response message (Message output format)
const result = translate([message], {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.role).toBe("assistant");
expect(result.messages[0]?.parts[0]?.type).toBe("text");
expect(result.messages[0]?.finish_reason).toBe("stop");
});
it("should translate a conversation with system prompt", async () => {
const message = await anthropic.messages.create({
model: MODEL,
max_tokens: 50,
system: "You are a helpful assistant. Reply in exactly 3 words.",
messages: [{ role: "user", content: "Say hello" }],
});
// Build full conversation including system
const result = translate([message], {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.role).toBe("assistant");
});
it("should translate tool calls from real API", async () => {
const message = await anthropic.messages.create({
model: MODEL,
max_tokens: 100,
messages: [{ role: "user", content: "What's the weather in Paris?" }],
tools: [
{
name: "get_weather",
description: "Get the current weather for a location",
input_schema: {
type: "object" as const,
properties: {
location: { type: "string", description: "The city name" },
},
required: ["location"],
},
},
],
tool_choice: { type: "any" },
});
expect(message.stop_reason).toBe("tool_use");
const result = translate([message], {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.role).toBe("assistant");
expect(result.messages[0]?.finish_reason).toBe("tool_call");
const toolCallPart = result.messages[0]?.parts.find((p) => p.type === "tool_call");
expect(toolCallPart).toBeDefined();
expect((toolCallPart as { name: string }).name).toBe("get_weather");
});
it("should translate tool call and response round-trip", async () => {
// Step 1: Get a tool call from Anthropic
const message1 = await anthropic.messages.create({
model: MODEL,
max_tokens: 100,
messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
tools: [
{
name: "get_weather",
description: "Get the current weather",
input_schema: {
type: "object" as const,
properties: {
location: { type: "string" },
},
required: ["location"],
},
},
],
tool_choice: { type: "any" },
});
const toolUseBlock = message1.content.find((b) => b.type === "tool_use");
expect(toolUseBlock).toBeDefined();
if (!toolUseBlock || toolUseBlock.type !== "tool_use") throw new Error("No tool use block");
// Step 2: Build the tool result and get final response
const message2 = await anthropic.messages.create({
model: MODEL,
max_tokens: 100,
messages: [
{ role: "user", content: "What's the weather in Tokyo?" },
{ role: "assistant", content: message1.content },
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: toolUseBlock.id,
content: JSON.stringify({ temperature: 22, condition: "sunny" }),
},
],
},
],
tools: [
{
name: "get_weather",
description: "Get the current weather",
input_schema: {
type: "object" as const,
properties: { location: { type: "string" } },
required: ["location"],
},
},
],
});
// Build full conversation
const fullConversation = [
{ role: "user" as const, content: "What's the weather in Tokyo?" },
{ role: "assistant" as const, content: message1.content },
{
role: "user" as const,
content: [
{
type: "tool_result" as const,
tool_use_id: toolUseBlock.id,
content: JSON.stringify({ temperature: 22, condition: "sunny" }),
},
],
},
message2,
];
const result = translate(fullConversation, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(4);
expect(result.messages[0]?.role).toBe("user");
expect(result.messages[1]?.role).toBe("assistant");
expect(result.messages[1]?.parts.some((p) => p.type === "tool_call")).toBe(true);
expect(result.messages[2]?.role).toBe("user");
expect(result.messages[2]?.parts[0]?.type).toBe("tool_call_response");
expect(result.messages[3]?.role).toBe("assistant");
});
it("should translate real API response to Promptl format", async () => {
const message = await anthropic.messages.create({
model: MODEL,
max_tokens: 10,
messages: [{ role: "user", content: "Reply with exactly: Hello World" }],
});
const result = translate([message], {
from: Provider.Anthropic,
to: Provider.Promptl,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.role).toBe("assistant");
});
});
describe("hardcoded messages (no API key required)", () => {
it("should translate simple Anthropic messages to GenAI", () => {
const anthropicMessages = [
{ 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(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(2);
expect(result.messages[0]?.role).toBe("user");
expect(result.messages[1]?.role).toBe("assistant");
});
it("should translate with system parameter", () => {
const anthropicMessages = [{ role: "user" as const, content: "Hello" }];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
system: "You are a helpful assistant.",
});
// System is extracted to the separate system field when translating to GenAI
expect(result.messages).toHaveLength(1);
expect(result.system).toBeDefined();
expect(result.system?.[0]).toMatchObject({ type: "text", content: "You are a helpful assistant." });
expect(result.messages[0]?.role).toBe("user");
});
it("should translate inline system messages (mid-conversation system) to an inline-system target", () => {
// Anthropic allows `role: "system"` entries inline in the messages array, in
// addition to the separate top-level `system` field. Both are supported as input.
// Translating to a target that keeps system inline (Vercel AI) preserves the position.
const anthropicMessages = [
{ role: "user" as const, content: "Write a helper." },
{ role: "system" as const, content: "This project is written in Go. Write code in Go." },
{ role: "assistant" as const, content: "Here is the Go helper." },
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.VercelAI,
});
expect(result.messages).toHaveLength(3);
expect(result.messages[0]?.role).toBe("user");
// The inline system message keeps its original conversation position
expect(result.messages[1]?.role).toBe("system");
expect(result.messages[1]?.content).toBe("This project is written in Go. Write code in Go.");
expect(result.messages[2]?.role).toBe("assistant");
});
it("should preserve an inline system message's position when extracted to the GenAI system field", () => {
const anthropicMessages = [
{ role: "user" as const, content: "Hello" },
{ role: "system" as const, content: "Be concise." },
{ role: "assistant" as const, content: "Hi." },
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
// Translating to GenAI extracts system messages into the separate system field
expect(result.messages).toHaveLength(2);
expect(result.system).toBeDefined();
expect(result.system?.[0]).toMatchObject({ type: "text", content: "Be concise." });
// The extracted system part records its original conversation position so it can be
// re-inserted at the right spot when translating to an inline-system target.
expect(
(result.system?.[0]?._provider_metadata?._known_fields as Record<string, unknown> | undefined)?.messageIndex,
).toBe(1);
});
it("should translate a mid_conv_system block built with the real Anthropic SDK type", () => {
// Build the conversation with the real SDK `MessageParam` type, including the
// `MidConversationSystemBlockParam` (mid_conv_system) block, to verify rosetta
// accepts the exact wire shape the Anthropic SDK produces.
const anthropicMessages: Anthropic.MessageParam[] = [
{ role: "user", content: "Write a helper." },
{
role: "system",
content: [
{
type: "mid_conv_system",
content: [{ type: "text", text: "This project is written in Go." }],
},
],
},
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
// The system-role message is extracted to the GenAI system field; the block's text
// is preserved and tagged with its origin so it could be reconstructed later.
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.role).toBe("user");
expect(result.system?.[0]).toMatchObject({ type: "text", content: "This project is written in Go." });
expect(result.system?.[0]?._provider_metadata?._known_fields).toMatchObject({
originalType: "mid_conv_system",
});
});
it("should translate a mid_conv_system block embedded in a user turn without losing the text", () => {
const anthropicMessages: Anthropic.MessageParam[] = [
{
role: "user",
content: [
{ type: "text", text: "Here is the file." },
{
type: "mid_conv_system",
content: [{ type: "text", text: "Reply only with valid JSON." }],
},
],
},
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.role).toBe("user");
expect(result.messages[0]?.parts).toHaveLength(2);
expect((result.messages[0]?.parts[1] as { content: string }).content).toBe("Reply only with valid JSON.");
expect(result.messages[0]?.parts[1]?._provider_metadata?._known_fields).toMatchObject({
originalType: "mid_conv_system",
});
});
it("should translate Message output format", () => {
const anthropicMessage = {
id: "msg_abc123",
type: "message" as const,
role: "assistant" as const,
content: [{ type: "text" as const, text: "Hello! How can I help you today?" }],
model: MODEL,
stop_reason: "end_turn" as const,
stop_sequence: null,
usage: { input_tokens: 10, output_tokens: 12 },
};
const result = translate([anthropicMessage], {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.role).toBe("assistant");
expect(result.messages[0]?.parts[0]).toEqual({
type: "text",
content: "Hello! How can I help you today?",
});
expect(result.messages[0]?.finish_reason).toBe("stop");
});
it("should translate assistant message with tool use", () => {
const anthropicMessages = [
{ role: "user" as const, content: "What's the weather in London?" },
{
role: "assistant" as const,
content: [
{
type: "tool_use" as const,
id: "toolu_abc123",
name: "get_weather",
input: { location: "London", unit: "celsius" },
},
],
},
{
role: "user" as const,
content: [
{
type: "tool_result" as const,
tool_use_id: "toolu_abc123",
content: '{"temperature":15,"condition":"cloudy"}',
},
],
},
{ role: "assistant" as const, content: "The weather in London is 15°C and cloudy." },
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(4);
// Check tool call message
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" });
// Check tool response message
const toolResponseMsg = result.messages[2];
expect(toolResponseMsg?.role).toBe("user");
expect(toolResponseMsg?.parts[0]?.type).toBe("tool_call_response");
});
it("should translate thinking blocks (extended thinking)", () => {
const anthropicMessages = [
{
role: "assistant" as const,
content: [
{
type: "thinking" as const,
thinking: "Let me think about this step by step...",
signature: "sig_abc123",
},
{ type: "text" as const, text: "Here's my answer." },
],
},
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.parts).toHaveLength(2);
expect(result.messages[0]?.parts[0]?.type).toBe("reasoning");
expect((result.messages[0]?.parts[0] as { content: string }).content).toBe(
"Let me think about this step by step...",
);
expect(result.messages[0]?.parts[1]?.type).toBe("text");
});
it("should translate image content (base64)", () => {
const anthropicMessages = [
{
role: "user" as const,
content: [
{ type: "text" as const, text: "What's in this image?" },
{
type: "image" as const,
source: {
type: "base64" as const,
media_type: "image/png" as const,
data: "iVBORw0KGgoAAAANSUhEUg...",
},
},
],
},
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
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("blob");
expect((result.messages[0]?.parts[1] as { modality: string }).modality).toBe("image");
});
it("should translate image content (URL)", () => {
const anthropicMessages = [
{
role: "user" as const,
content: [
{ type: "text" as const, text: "What's in this image?" },
{
type: "image" as const,
source: {
type: "url" as const,
url: "https://example.com/cat.jpg",
},
},
],
},
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.parts).toHaveLength(2);
expect(result.messages[0]?.parts[1]?.type).toBe("uri");
expect((result.messages[0]?.parts[1] as { uri: string }).uri).toBe("https://example.com/cat.jpg");
});
it("should translate PDF document content", () => {
const anthropicMessages = [
{
role: "user" as const,
content: [
{ type: "text" as const, text: "Summarize this document" },
{
type: "document" as const,
source: {
type: "base64" as const,
media_type: "application/pdf" as const,
data: "JVBERi0xLjQK...",
},
},
],
},
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.parts).toHaveLength(2);
expect(result.messages[0]?.parts[1]?.type).toBe("blob");
expect((result.messages[0]?.parts[1] as { modality: string }).modality).toBe("document");
expect((result.messages[0]?.parts[1] as { mime_type: string }).mime_type).toBe("application/pdf");
});
it("should auto-detect Anthropic format", () => {
const anthropicMessages = [
{ role: "user" as const, content: "Hello" },
{ role: "assistant" as const, content: "Hi there!" },
];
// No explicit 'from' - should auto-infer
const result = translate(anthropicMessages);
expect(result.messages).toHaveLength(2);
expect(result.messages[0]?.role).toBe("user");
expect(result.messages[1]?.role).toBe("assistant");
});
it("should translate Anthropic to Promptl via GenAI", () => {
const anthropicMessages = [
{ role: "user" as const, content: "Hello!" },
{ role: "assistant" as const, content: "Hi there!" },
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.Promptl,
});
expect(result.messages).toHaveLength(2);
expect(result.messages[0]?.role).toBe("user");
expect(result.messages[1]?.role).toBe("assistant");
});
it("should preserve cache_control in metadata", () => {
const anthropicMessages = [
{
role: "user" as const,
content: [
{
type: "text" as const,
text: "Important context to cache",
cache_control: { type: "ephemeral" as const },
},
],
},
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
// Extra fields are now at root level of _provider_metadata
expect(result.messages[0]?.parts[0]?._provider_metadata).toMatchObject({
cache_control: { type: "ephemeral" },
});
});
it("should handle tool result with is_error flag", () => {
const anthropicMessages = [
{
role: "user" as const,
content: [
{
type: "tool_result" as const,
tool_use_id: "toolu_abc123",
content: "Error: API rate limit exceeded",
is_error: true,
},
],
},
];
const result = translate(anthropicMessages, {
from: Provider.Anthropic,
to: Provider.GenAI,
});
// isError is stored in _known_fields for cross-provider access
expect(
(result.messages[0]?.parts[0]?._provider_metadata?._known_fields as Record<string, unknown> | undefined)
?.isError,
).toBe(true);
});
});
});