-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat.test.ts
More file actions
348 lines (305 loc) · 9.93 KB
/
Copy pathcompat.test.ts
File metadata and controls
348 lines (305 loc) · 9.93 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
/**
* Compat Provider E2E Tests
*
* Tests the Compat provider's ability to translate various LLM message formats
* to GenAI. These tests verify the fallback behavior works correctly for
* formats that don't match any specific provider schema.
*/
import { Provider, translate } from "rosetta-ai";
import { describe, expect, it } from "vitest";
describe("Compat E2E", () => {
describe("auto-detection fallback", () => {
it("should fall back to Compat for unknown message format", () => {
// A format that doesn't match any specific provider
const messages = [
{
sender: "human",
text: "Hello there",
timestamp: 1234567890,
},
];
// Without specifying 'from', should use Compat as fallback
const result = translate(messages, { to: Provider.GenAI });
expect(result.messages).toHaveLength(1);
// Since there's no 'role' field, direction default will be used
expect(result.messages[0]?.role).toBe("user");
});
it("should explicitly use Compat provider", () => {
const messages = [{ role: "user", content: "Test message" }];
const result = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.parts[0]).toEqual({
type: "text",
content: "Test message",
});
});
});
describe("Cohere-style messages", () => {
it("should handle Cohere message format", () => {
// Cohere uses standard role/content format
const messages = [
{ role: "user", content: "Tell me about LLMs" },
{
role: "assistant",
content: "Large Language Models are...",
},
];
const result = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(2);
expect(result.messages[0]?.role).toBe("user");
expect(result.messages[1]?.role).toBe("assistant");
});
});
describe("Ollama-style messages", () => {
it("should handle Ollama message with thinking field", () => {
const messages = [
{
role: "assistant",
content: "The answer is 42",
thinking: "Let me calculate step by step...",
},
];
const result = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
});
expect(result.messages[0]?.parts).toHaveLength(2);
// Reasoning should come first
expect(result.messages[0]?.parts[0]).toEqual({
type: "reasoning",
content: "Let me calculate step by step...",
});
expect(result.messages[0]?.parts[1]).toEqual({
type: "text",
content: "The answer is 42",
});
});
});
describe("AWS Bedrock Converse-style messages", () => {
it("should handle Bedrock ContentBlock format", () => {
const messages = [
{
role: "user",
content: [{ text: "What is the weather?" }],
},
{
role: "assistant",
content: [
{
toolUse: {
toolUseId: "tool_123",
name: "get_weather",
input: { location: "Seattle" },
},
},
],
},
];
const result = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(2);
expect(result.messages[0]?.parts[0]).toEqual({
type: "text",
content: "What is the weather?",
});
// The toolUse should be detected as a generic part since it's not a standard format
const toolPart = result.messages[1]?.parts[0] as Record<string, unknown>;
expect(toolPart.type).toBe("unknown");
});
});
describe("Together AI style messages", () => {
it("should handle Together AI response with reasoning field", () => {
const messages = [
{
role: "assistant",
content: "The result is X",
reasoning: "First I considered A, then B...",
},
];
const result = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
});
expect(result.messages[0]?.parts[0]).toEqual({
type: "reasoning",
content: "First I considered A, then B...",
});
});
});
describe("Fireworks AI style messages", () => {
it("should handle Fireworks response with reasoning_content", () => {
const messages = [
{
role: "assistant",
content: "Here's my answer",
reasoning_content: "Step 1: ... Step 2: ...",
},
];
const result = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
});
expect(result.messages[0]?.parts[0]).toEqual({
type: "reasoning",
content: "Step 1: ... Step 2: ...",
});
});
});
describe("custom/proprietary formats", () => {
it("should handle completely custom message format", () => {
const messages = [
{
role: "user",
payload: {
message: "Hello",
attachments: [],
},
},
];
const result = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
});
expect(result.messages).toHaveLength(1);
expect(result.messages[0]?.role).toBe("user");
// Should serialize the unknown structure
expect(result.messages[0]?.parts[0]?.type).toBe("text");
});
it("should handle messages with mixed known and unknown fields", () => {
const messages = [
{
role: "assistant",
content: "Response text",
custom_metadata: { source: "api", version: "2.0" },
internal_id: "msg_12345",
},
];
const result = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
});
expect(result.messages[0]?.role).toBe("assistant");
expect(result.messages[0]?.parts[0]).toEqual({
type: "text",
content: "Response text",
});
});
});
describe("mixed-provider conversations", () => {
it("should handle conversation with various message styles", () => {
const messages = [
// Standard format
{ role: "system", content: "You are helpful" },
// OpenAI style with tool call
{
role: "assistant",
content: null,
tool_calls: [
{
id: "call_1",
type: "function",
function: { name: "search", arguments: '{"q": "weather"}' },
},
],
},
// Tool response
{ role: "tool", tool_call_id: "call_1", content: '{"temp": 20}' },
// Simple response
{ role: "assistant", content: "The temperature is 20°C" },
];
const result = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
});
// GenAI's fromGenAI separates system messages into result.system
expect(result.messages).toHaveLength(3); // Non-system messages
expect(result.system).toBeDefined();
expect(result.system?.[0]).toMatchObject({ type: "text", content: "You are helpful" });
expect(result.messages[0]?.parts[0]?.type).toBe("tool_call");
expect(result.messages[1]?.parts[0]?.type).toBe("tool_call_response");
expect(result.messages[2]?.parts[0]?.type).toBe("text");
});
});
describe("system instructions", () => {
it("should handle system instructions with Compat provider", () => {
const result = translate([], {
from: Provider.Compat,
to: Provider.GenAI,
system: "You are a helpful assistant specialized in coding.",
});
// GenAI's fromGenAI separates system into result.system
expect(result.messages).toHaveLength(0);
expect(result.system).toBeDefined();
expect(result.system).toHaveLength(1);
expect(result.system?.[0]).toMatchObject({
type: "text",
content: "You are a helpful assistant specialized in coding.",
});
});
it("should handle object system instructions", () => {
const result = translate([], {
from: Provider.Compat,
to: Provider.GenAI,
system: { text: "System prompt here" },
});
// GenAI's fromGenAI separates system into result.system
expect(result.system).toBeDefined();
expect(result.system?.[0]).toMatchObject({
type: "text",
content: "System prompt here",
});
});
});
describe("multimodal content", () => {
it("should handle various image formats", () => {
const messages = [
{
role: "user",
content: [
{ type: "text", text: "Describe this image" },
{ type: "image_url", image_url: { url: "https://example.com/img.png" } },
],
},
];
const result = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
});
expect(result.messages[0]?.parts).toHaveLength(2);
expect(result.messages[0]?.parts[0]).toEqual({
type: "text",
content: "Describe this image",
});
expect(result.messages[0]?.parts[1]).toEqual({
type: "uri",
modality: "image",
uri: "https://example.com/img.png",
});
});
});
describe("direction parameter", () => {
it("should use direction for role inference when role is missing", () => {
const messages = [{ content: "Some content without role" }];
const inputResult = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
direction: "input",
});
expect(inputResult.messages[0]?.role).toBe("user");
const outputResult = translate(messages, {
from: Provider.Compat,
to: Provider.GenAI,
direction: "output",
});
expect(outputResult.messages[0]?.role).toBe("assistant");
});
});
});