Skip to content

Commit

Permalink
feat (provider/openai-compatible): Allow extending message metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaper committed Dec 20, 2024
1 parent 9049c57 commit 2328a3d
Show file tree
Hide file tree
Showing 3 changed files with 430 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,186 @@ describe('tool calls', () => {
]);
});
});

describe('metadata merging', () => {
it('should merge system message metadata', async () => {
const result = convertToOpenAICompatibleChatMessages([
{
role: 'system',
content: 'You are a helpful assistant.',
providerMetadata: {
openaiCompatible: {
cacheControl: { type: 'ephemeral' },
},
},
},
]);

expect(result).toEqual([
{
role: 'system',
content: 'You are a helpful assistant.',
cacheControl: { type: 'ephemeral' },
},
]);
});

it('should merge user message content metadata', async () => {
const result = convertToOpenAICompatibleChatMessages([
{
role: 'user',
content: [
{
type: 'text',
text: 'Hello',
providerMetadata: {
openaiCompatible: {
cacheControl: { type: 'ephemeral' },
},
},
},
],
},
]);

expect(result).toEqual([
{
role: 'user',
content: 'Hello',
cacheControl: { type: 'ephemeral' },
},
]);
});

it('should merge metadata at multiple levels', async () => {
const result = convertToOpenAICompatibleChatMessages([
{
role: 'user',
providerMetadata: {
openaiCompatible: {
messageLevel: true,
},
},
content: [
{
type: 'text',
text: 'Hello',
providerMetadata: {
openaiCompatible: {
contentLevel: true,
},
},
},
],
},
]);

expect(result).toEqual([
{
role: 'user',
messageLevel: true,
content: 'Hello',
contentLevel: true,
},
]);
});

it('should handle tool calls with metadata', async () => {
const result = convertToOpenAICompatibleChatMessages([
{
role: 'assistant',
content: [
{
type: 'tool-call',
toolCallId: 'call1',
toolName: 'calculator',
args: { x: 1, y: 2 },
providerMetadata: {
openaiCompatible: {
cacheControl: { type: 'ephemeral' },
},
},
},
],
},
]);

expect(result).toEqual([
{
role: 'assistant',
content: '',
tool_calls: [
{
id: 'call1',
type: 'function',
function: {
name: 'calculator',
arguments: JSON.stringify({ x: 1, y: 2 }),
},
cacheControl: { type: 'ephemeral' },
},
],
},
]);
});

it('should handle image content with metadata', async () => {
const imageUrl = new URL('https://example.com/image.jpg');
const result = convertToOpenAICompatibleChatMessages([
{
role: 'user',
content: [
{
type: 'image',
image: imageUrl,
mimeType: 'image/jpeg',
providerMetadata: {
openaiCompatible: {
cacheControl: { type: 'ephemeral' },
},
},
},
],
},
]);

expect(result).toEqual([
{
role: 'user',
content: [
{
type: 'image_url',
image_url: { url: imageUrl.toString() },
cacheControl: { type: 'ephemeral' },
},
],
},
]);
});

it('should preserve non-openaiCompatible metadata', async () => {
const result = convertToOpenAICompatibleChatMessages([
{
role: 'system',
content: 'Hello',
providerMetadata: {
someOtherProvider: {
shouldBeIgnored: true,
},
},
},
]);

expect(result).toEqual([
{
role: 'system',
content: 'Hello',
providerMetadata: {
someOtherProvider: {
shouldBeIgnored: true,
},
},
},
]);
});
});
Loading

0 comments on commit 2328a3d

Please sign in to comment.