Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ export function removeChatCompletionsReasoning(request: OpenRouterChatCompletion
}
}

export function removeChatCompletionsToolNames(request: OpenRouterChatCompletionRequest) {
for (const message of request.messages) {
if (message.role === 'tool' && 'name' in message) {
delete message.name;
}
}
}

/**
* Inverse of the `mapReasoningContentToDetails` response transform: folds
* OpenRouter-style `reasoning_details` back into the DeepSeek-style
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { getEnvVariable } from '@/lib/dotenvx';
import { isReasoningExplicitlyDisabled } from '@/lib/ai-gateway/providers/openrouter/request-helpers';
import {
isReasoningExplicitlyDisabled,
removeChatCompletionsToolNames,
} from '@/lib/ai-gateway/providers/openrouter/request-helpers';
import type { Provider } from '@/lib/ai-gateway/providers/types';
import { applyVercelSettings } from '@/lib/ai-gateway/providers/vercel';

Expand Down Expand Up @@ -118,8 +121,10 @@ export default {
context.request.body.reasoning_effort ??
undefined);
delete context.request.body.reasoning;
removeChatCompletionsToolNames(context.request.body);
}
delete context.request.body.provider;
delete context.request.body.user;
},
},
STREAMLAKE: {
Expand Down
30 changes: 28 additions & 2 deletions apps/web/src/tests/openrouter-request-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import { describe, expect, test } from '@jest/globals';
import { addCacheBreakpoints } from '@/lib/ai-gateway/providers/openrouter/request-helpers';
import type { GatewayRequest } from '@/lib/ai-gateway/providers/openrouter/types';
import {
addCacheBreakpoints,
removeChatCompletionsToolNames,
} from '@/lib/ai-gateway/providers/openrouter/request-helpers';
import type {
GatewayRequest,
OpenRouterChatCompletionRequest,
} from '@/lib/ai-gateway/providers/openrouter/types';
import type OpenAI from 'openai';

describe('removeChatCompletionsToolNames', () => {
test('removes names from tool messages only', () => {
const toolMessage: OpenAI.ChatCompletionToolMessageParam & { name?: string } = {
role: 'tool',
content: 'Tool result',
tool_call_id: 'call_123',
name: 'read_file',
};
const request: OpenRouterChatCompletionRequest = {
model: 'test-model',
messages: [{ role: 'user', content: 'Run the tool', name: 'user-name' }, toolMessage],
};

removeChatCompletionsToolNames(request);

expect(toolMessage.name).toBeUndefined();
expect(request.messages[0]).toHaveProperty('name', 'user-name');
});
});

describe('addCacheBreakpoints', () => {
test('adds a cache breakpoint to the system message and the last eligible chat completions message when none exist', () => {
const request: GatewayRequest = {
Expand Down