Skip to content

Commit 420dfe7

Browse files
committed
fix: add default thinking text for opencode compatibility in translation functions
1 parent 67b357a commit 420dfe7

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

src/routes/messages/non-stream-translation.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ import {
2626
} from "./anthropic-types"
2727
import { mapOpenAIStopReasonToAnthropic } from "./utils"
2828

29-
// Payload translation
29+
// Compatible with opencode, it will filter out blocks where the thinking text is empty, so we need add a default thinking text
30+
export const THINKING_TEXT = "Thinking..."
3031

32+
// Payload translation
3133
export function translateToOpenAI(
3234
payload: AnthropicMessagesPayload,
3335
): ChatCompletionsPayload {
@@ -219,24 +221,21 @@ function handleAssistantMessage(
219221
thinkingBlocks = thinkingBlocks.filter(
220222
(b) =>
221223
b.thinking
222-
&& b.thinking.length > 0
224+
&& b.thinking !== THINKING_TEXT
223225
&& b.signature
224-
&& b.signature.length > 0
225226
// gpt signature has @ in it, so filter those out for claude models
226227
&& !b.signature.includes("@"),
227228
)
228229
}
229230

230231
const thinkingContents = thinkingBlocks
231-
.filter((b) => b.thinking && b.thinking.length > 0)
232+
.filter((b) => b.thinking && b.thinking !== THINKING_TEXT)
232233
.map((b) => b.thinking)
233234

234235
const allThinkingContent =
235236
thinkingContents.length > 0 ? thinkingContents.join("\n\n") : undefined
236237

237-
const signature = thinkingBlocks.find(
238-
(b) => b.signature && b.signature.length > 0,
239-
)?.signature
238+
const signature = thinkingBlocks.find((b) => b.signature)?.signature
240239

241240
return toolUseBlocks.length > 0 ?
242241
[
@@ -436,7 +435,7 @@ function getAnthropicThinkBlocks(
436435
return [
437436
{
438437
type: "thinking",
439-
thinking: "",
438+
thinking: THINKING_TEXT, // Compatible with opencode, it will filter out blocks where the thinking text is empty, so we add a default thinking text here
440439
signature: reasoningOpaque,
441440
},
442441
]

src/routes/messages/stream-translation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type AnthropicStreamEventData,
99
type AnthropicStreamState,
1010
} from "./anthropic-types"
11+
import { THINKING_TEXT } from "./non-stream-translation"
1112
import { mapOpenAIStopReasonToAnthropic } from "./utils"
1213

1314
function isToolBlockOpen(state: AnthropicStreamState): boolean {
@@ -292,7 +293,7 @@ function handleReasoningOpaque(
292293
index: state.contentBlockIndex,
293294
delta: {
294295
type: "thinking_delta",
295-
thinking: "",
296+
thinking: THINKING_TEXT, // Compatible with opencode, it will filter out blocks where the thinking text is empty, so we add a default thinking text here
296297
},
297298
},
298299
{

0 commit comments

Comments
 (0)