Skip to content

Commit 2328a3d

Browse files
committed
feat (provider/openai-compatible): Allow extending message metadata.
1 parent 9049c57 commit 2328a3d

File tree

3 files changed

+430
-66
lines changed

3 files changed

+430
-66
lines changed

packages/openai-compatible/src/convert-to-openai-compatible-chat-messages.test.ts

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,186 @@ describe('tool calls', () => {
119119
]);
120120
});
121121
});
122+
123+
describe('metadata merging', () => {
124+
it('should merge system message metadata', async () => {
125+
const result = convertToOpenAICompatibleChatMessages([
126+
{
127+
role: 'system',
128+
content: 'You are a helpful assistant.',
129+
providerMetadata: {
130+
openaiCompatible: {
131+
cacheControl: { type: 'ephemeral' },
132+
},
133+
},
134+
},
135+
]);
136+
137+
expect(result).toEqual([
138+
{
139+
role: 'system',
140+
content: 'You are a helpful assistant.',
141+
cacheControl: { type: 'ephemeral' },
142+
},
143+
]);
144+
});
145+
146+
it('should merge user message content metadata', async () => {
147+
const result = convertToOpenAICompatibleChatMessages([
148+
{
149+
role: 'user',
150+
content: [
151+
{
152+
type: 'text',
153+
text: 'Hello',
154+
providerMetadata: {
155+
openaiCompatible: {
156+
cacheControl: { type: 'ephemeral' },
157+
},
158+
},
159+
},
160+
],
161+
},
162+
]);
163+
164+
expect(result).toEqual([
165+
{
166+
role: 'user',
167+
content: 'Hello',
168+
cacheControl: { type: 'ephemeral' },
169+
},
170+
]);
171+
});
172+
173+
it('should merge metadata at multiple levels', async () => {
174+
const result = convertToOpenAICompatibleChatMessages([
175+
{
176+
role: 'user',
177+
providerMetadata: {
178+
openaiCompatible: {
179+
messageLevel: true,
180+
},
181+
},
182+
content: [
183+
{
184+
type: 'text',
185+
text: 'Hello',
186+
providerMetadata: {
187+
openaiCompatible: {
188+
contentLevel: true,
189+
},
190+
},
191+
},
192+
],
193+
},
194+
]);
195+
196+
expect(result).toEqual([
197+
{
198+
role: 'user',
199+
messageLevel: true,
200+
content: 'Hello',
201+
contentLevel: true,
202+
},
203+
]);
204+
});
205+
206+
it('should handle tool calls with metadata', async () => {
207+
const result = convertToOpenAICompatibleChatMessages([
208+
{
209+
role: 'assistant',
210+
content: [
211+
{
212+
type: 'tool-call',
213+
toolCallId: 'call1',
214+
toolName: 'calculator',
215+
args: { x: 1, y: 2 },
216+
providerMetadata: {
217+
openaiCompatible: {
218+
cacheControl: { type: 'ephemeral' },
219+
},
220+
},
221+
},
222+
],
223+
},
224+
]);
225+
226+
expect(result).toEqual([
227+
{
228+
role: 'assistant',
229+
content: '',
230+
tool_calls: [
231+
{
232+
id: 'call1',
233+
type: 'function',
234+
function: {
235+
name: 'calculator',
236+
arguments: JSON.stringify({ x: 1, y: 2 }),
237+
},
238+
cacheControl: { type: 'ephemeral' },
239+
},
240+
],
241+
},
242+
]);
243+
});
244+
245+
it('should handle image content with metadata', async () => {
246+
const imageUrl = new URL('https://example.com/image.jpg');
247+
const result = convertToOpenAICompatibleChatMessages([
248+
{
249+
role: 'user',
250+
content: [
251+
{
252+
type: 'image',
253+
image: imageUrl,
254+
mimeType: 'image/jpeg',
255+
providerMetadata: {
256+
openaiCompatible: {
257+
cacheControl: { type: 'ephemeral' },
258+
},
259+
},
260+
},
261+
],
262+
},
263+
]);
264+
265+
expect(result).toEqual([
266+
{
267+
role: 'user',
268+
content: [
269+
{
270+
type: 'image_url',
271+
image_url: { url: imageUrl.toString() },
272+
cacheControl: { type: 'ephemeral' },
273+
},
274+
],
275+
},
276+
]);
277+
});
278+
279+
it('should preserve non-openaiCompatible metadata', async () => {
280+
const result = convertToOpenAICompatibleChatMessages([
281+
{
282+
role: 'system',
283+
content: 'Hello',
284+
providerMetadata: {
285+
someOtherProvider: {
286+
shouldBeIgnored: true,
287+
},
288+
},
289+
},
290+
]);
291+
292+
expect(result).toEqual([
293+
{
294+
role: 'system',
295+
content: 'Hello',
296+
providerMetadata: {
297+
someOtherProvider: {
298+
shouldBeIgnored: true,
299+
},
300+
},
301+
},
302+
]);
303+
});
304+
});

0 commit comments

Comments
 (0)