Skip to content

Commit 3f52575

Browse files
committed
feat: enhance reasoning handling in tool calls and change the thinking order when stream=false
1 parent da6a0a1 commit 3f52575

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export function translateToAnthropic(
287287
)
288288
const toolUseBlocks = getAnthropicToolUseBlocks(choice.message.tool_calls)
289289

290-
assistantContentBlocks.push(...textBlocks, ...thingBlocks, ...toolUseBlocks)
290+
assistantContentBlocks.push(...thingBlocks, ...textBlocks, ...toolUseBlocks)
291291

292292
// Use the finish_reason from the first choice, or prioritize tool_calls
293293
if (choice.finish_reason === "tool_calls" || stopReason === "stop") {
@@ -320,7 +320,7 @@ export function translateToAnthropic(
320320
function getAnthropicTextBlocks(
321321
messageContent: Message["content"],
322322
): Array<AnthropicTextBlock> {
323-
if (typeof messageContent === "string") {
323+
if (typeof messageContent === "string" && messageContent.length > 0) {
324324
return [{ type: "text", text: messageContent }]
325325
}
326326

src/routes/messages/stream-translation.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ function handleToolCalls(
101101
if (delta.tool_calls && delta.tool_calls.length > 0) {
102102
closeThinkingBlockIfOpen(state, events)
103103

104+
handleReasoningOpaqueInToolCalls(state, events, delta)
105+
104106
for (const toolCall of delta.tool_calls) {
105107
if (toolCall.id && toolCall.function?.name) {
106108
// New tool call starting.
@@ -153,6 +155,22 @@ function handleToolCalls(
153155
}
154156
}
155157

158+
function handleReasoningOpaqueInToolCalls(
159+
state: AnthropicStreamState,
160+
events: Array<AnthropicStreamEventData>,
161+
delta: Delta,
162+
) {
163+
if (state.contentBlockOpen) {
164+
events.push({
165+
type: "content_block_stop",
166+
index: state.contentBlockIndex,
167+
})
168+
state.contentBlockIndex++
169+
state.contentBlockOpen = false
170+
}
171+
handleReasoningOpaque(delta, events, state)
172+
}
173+
156174
function handleContent(
157175
delta: Delta,
158176
state: AnthropicStreamState,
@@ -263,6 +281,7 @@ function handleReasoningOpaque(
263281
index: state.contentBlockIndex,
264282
},
265283
)
284+
state.contentBlockIndex++
266285
}
267286
}
268287

0 commit comments

Comments
 (0)