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
112 changes: 112 additions & 0 deletions web/src/llm-api/__tests__/kimi-tool-compat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { describe, expect, it } from 'bun:test'

import { addKimiToolCompatibilityFields, isKimiModel } from '../kimi-tool-compat'

import type { ChatCompletionRequestBody } from '../types'

describe('addKimiToolCompatibilityFields', () => {
it('adds declaration ids and tool-result names without mutating input', () => {
const body: ChatCompletionRequestBody = {
model: 'moonshotai/kimi-k2.6',
messages: [
{
role: 'assistant',
content: '',
tool_calls: [
{
id: 'call_123',
type: 'function',
function: {
name: 'read_files',
arguments: JSON.stringify({ paths: ['README.md'] }),
},
},
],
},
{
role: 'tool',
tool_call_id: 'call_123',
content: JSON.stringify({ message: 'ok' }),
},
],
tools: [
{
type: 'function',
function: {
name: 'read_files',
description: 'Read files',
parameters: { type: 'object' },
},
},
],
}

const result = addKimiToolCompatibilityFields(body)

expect(result.tools?.[0]).toEqual({
id: 'tool_1',
type: 'function',
function: {
name: 'read_files',
description: 'Read files',
parameters: { type: 'object' },
},
})
expect(result.messages[1]).toEqual({
role: 'tool',
tool_call_id: 'call_123',
name: 'read_files',
content: JSON.stringify({ message: 'ok' }),
})
expect(body.tools?.[0]).not.toHaveProperty('id')
expect(body.messages[1]).not.toHaveProperty('name')
})

it('preserves existing ids and names', () => {
const body: ChatCompletionRequestBody = {
model: 'moonshotai/kimi-k2.6',
messages: [
{
role: 'assistant',
content: '',
tool_calls: [
{
id: 'call_456',
type: 'function',
function: {
name: 'write_todos',
arguments: JSON.stringify({ todos: [] }),
},
},
],
},
{
role: 'tool',
tool_call_id: 'call_456',
name: 'existing_name',
content: '{}',
},
],
tools: [
{
id: 'existing_tool_id',
type: 'function',
function: {
name: 'write_todos',
parameters: { type: 'object' },
},
},
],
}

expect(addKimiToolCompatibilityFields(body)).toEqual(body)
})
})

describe('isKimiModel', () => {
it('matches only Moonshot model ids', () => {
expect(isKimiModel('moonshotai/kimi-k2.6')).toBe(true)
expect(isKimiModel('anthropic/claude-sonnet-4.5')).toBe(false)
expect(isKimiModel(undefined)).toBe(false)
})
})
6 changes: 5 additions & 1 deletion web/src/llm-api/canopywave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
extractRequestMetadata,
insertMessageToBigQuery,
} from './helpers'
import { addKimiToolCompatibilityFields, isKimiModel } from './kimi-tool-compat'

import type { UsageData } from './helpers'
import type { InsertMessageBigqueryFn } from '@codebuff/common/types/contracts/bigquery'
Expand Down Expand Up @@ -88,8 +89,11 @@ function createCanopyWaveRequest(params: {
fetch: typeof globalThis.fetch
}) {
const { body, originalModel, fetch } = params
const providerBody = isKimiModel(originalModel)
? addKimiToolCompatibilityFields(body)
: body
const canopywaveBody: Record<string, unknown> = {
...body,
...providerBody,
model: getCanopyWaveModelId(originalModel),
}

Expand Down
67 changes: 67 additions & 0 deletions web/src/llm-api/kimi-tool-compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { ChatCompletionRequestBody } from './types'

export function isKimiModel(model: unknown): model is string {
return typeof model === 'string' && model.startsWith('moonshotai/')
}

function getToolCallNamesById(
messages: ChatCompletionRequestBody['messages'],
): Map<string, string> {
const namesById = new Map<string, string>()

for (const message of messages) {
if (message.role !== 'assistant') {
continue
}
for (const toolCall of message.tool_calls ?? []) {
if (toolCall.id && toolCall.function.name) {
namesById.set(toolCall.id, toolCall.function.name)
}
}
}

return namesById
}

/**
* Kimi-compatible providers require two OpenAI-compatible extensions that are
* not part of the strict Chat Completions schema: ids on tool declarations and
* names on tool-result messages.
*/
export function addKimiToolCompatibilityFields(
body: ChatCompletionRequestBody,
): ChatCompletionRequestBody {
const namesByToolCallId = getToolCallNamesById(body.messages)

return {
...body,
tools: body.tools?.map((tool, index) => {
if (tool.type !== 'function' || tool.id) {
return tool
}
return {
...tool,
id: `tool_${index + 1}`,
}
}),
messages: body.messages.map((message) => {
if (
message.role !== 'tool' ||
message.name ||
typeof message.tool_call_id !== 'string'
) {
return message
}

const name = namesByToolCallId.get(message.tool_call_id)
if (!name) {
return message
}

return {
...message,
name,
}
}),
}
}
7 changes: 6 additions & 1 deletion web/src/llm-api/openrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
extractRequestMetadata,
insertMessageToBigQuery,
} from './helpers'
import { addKimiToolCompatibilityFields, isKimiModel } from './kimi-tool-compat'
import {
OpenRouterErrorResponseSchema,
OpenRouterStreamChatCompletionChunkSchema,
Expand Down Expand Up @@ -61,6 +62,10 @@ function createOpenRouterRequest(params: {
fetch: typeof globalThis.fetch
}) {
const { body, openrouterApiKey, fetch } = params
const providerBody = isKimiModel(body.model)
? addKimiToolCompatibilityFields(body)
: body

return fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
Expand All @@ -69,7 +74,7 @@ function createOpenRouterRequest(params: {
'X-Title': 'Codebuff',
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
body: JSON.stringify(providerBody),
// Use custom agent with extended headers timeout for deep-thinking models
// @ts-expect-error - dispatcher is a valid undici option not in fetch types
dispatcher: openrouterAgent,
Expand Down
12 changes: 12 additions & 0 deletions web/src/llm-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@ export interface ChatMessage {
tool_call_id?: string
}

export interface ChatCompletionTool {
id?: string
type: string
function?: {
name: string
description?: string
parameters?: unknown
strict?: boolean
}
}

export interface ChatCompletionRequestBody {
model: string
messages: ChatMessage[]
tools?: ChatCompletionTool[]
stream?: boolean
temperature?: number
max_tokens?: number
Expand Down
Loading