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
5 changes: 5 additions & 0 deletions .changeset/gentle-laws-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kilo-code": patch
---

feat: support preserving reasoning content in OpenAI format conversion
34 changes: 33 additions & 1 deletion src/api/transform/openai-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ export interface ConvertToOpenAiMessagesOptions {
mergeToolResultText?: boolean
}

// kilocode_change start
type ReasoningBlockParam = {
/**
* Non-Anthropic block type used by some providers. We preserve it so we can
* round-trip it through OpenAI-format messages.
*/
type: "reasoning"
text?: string
thinking?: string
}
// kilocode_change end

function isReasoningBlockParam(part: unknown): part is ReasoningBlockParam {
return typeof part === "object" && part !== null && (part as { type?: unknown }).type === "reasoning"
}

export function convertToOpenAiMessages(
anthropicMessages: Anthropic.Messages.MessageParam[],
options?: ConvertToOpenAiMessagesOptions,
Expand Down Expand Up @@ -442,15 +458,26 @@ export function convertToOpenAiMessages(
}
} else if (anthropicMessage.role === "assistant") {
const { nonToolMessages, toolMessages } = anthropicMessage.content.reduce<{
nonToolMessages: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam)[]
// kilocode_change start
nonToolMessages: (
| Anthropic.TextBlockParam
| Anthropic.ImageBlockParam
| Anthropic.ThinkingBlockParam
| ReasoningBlockParam
)[]
// kilocode_change end
toolMessages: Anthropic.ToolUseBlockParam[]
}>(
(acc, part) => {
if (part.type === "tool_use") {
acc.toolMessages.push(part)
} else if (part.type === "text" || part.type === "image") {
acc.nonToolMessages.push(part)
// kilocode_change start
} else if (part.type === "thinking" || isReasoningBlockParam(part)) {
acc.nonToolMessages.push(part)
} // assistant cannot send tool_result messages
// kilocode_change end
return acc
},
{ nonToolMessages: [], toolMessages: [] },
Expand All @@ -463,6 +490,11 @@ export function convertToOpenAiMessages(
.map((part) => {
if (part.type === "image") {
return "" // impossible as the assistant cannot send images
} else if (part.type === "thinking") {
return "<think>" + part.thinking + "</think>"
} else if (part.type === "reasoning") {
// kilocode_change - support custom "reasoning" type used by some providers
return "<think>" + (part.text || part.thinking || "") + "</think>"
}
return part.text
})
Expand Down
Loading