Skip to content

Commit 67b357a

Browse files
committed
feat: enforce interleaved thinking protocol in message handling
1 parent 8191930 commit 67b357a

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,20 @@ function translateAnthropicMessagesToOpenAI(
100100
: handleAssistantMessage(message, modelId),
101101
)
102102
if (modelId.startsWith("claude") && thinkingBudget) {
103-
const thinkingMessage = {
104-
role: "user",
105-
content:
106-
"<system-reminder>Please strictly follow Interleaved thinking</system-reminder>",
107-
} as Message
108-
return [...systemMessages, thinkingMessage, ...otherMessages]
103+
const reminder =
104+
"<system-reminder>you MUST follow interleaved_thinking_protocol</system-reminder>"
105+
const firstUserIndex = otherMessages.findIndex((m) => m.role === "user")
106+
if (firstUserIndex !== -1) {
107+
const userMessage = otherMessages[firstUserIndex]
108+
if (typeof userMessage.content === "string") {
109+
userMessage.content = reminder + "\n\n" + userMessage.content
110+
} else if (Array.isArray(userMessage.content)) {
111+
userMessage.content = [
112+
{ type: "text", text: reminder },
113+
...userMessage.content,
114+
] as Array<ContentPart>
115+
}
116+
}
109117
}
110118
return [...systemMessages, ...otherMessages]
111119
}
@@ -122,10 +130,16 @@ function handleSystemPrompt(
122130
let extraPrompt = ""
123131
if (modelId.startsWith("claude") && thinkingBudget) {
124132
extraPrompt = `
125-
## Interleaved thinking
126-
- Interleaved thinking is enabled
127-
- You MUST think after receiving tool results before deciding the next action or final answer.
128-
`
133+
<interleaved_thinking_protocol>
134+
ABSOLUTE REQUIREMENT - NON-NEGOTIABLE:
135+
The current thinking_mode is interleaved, Whenever you have the result of a function call, think carefully , MUST output a thinking block
136+
RULES:
137+
Tool result → thinking block (ALWAYS, no exceptions)
138+
This is NOT optional - it is a hard requirement
139+
The thinking block must contain substantive reasoning (minimum 3-5 sentences)
140+
Think about: what the results mean, what to do next, how to answer the user
141+
NEVER skip this step, even if the result seems simple or obvious
142+
</interleaved_thinking_protocol>`
129143
}
130144

131145
if (typeof system === "string") {

0 commit comments

Comments
 (0)