Skip to content

Commit 5c5a893

Browse files
authored
Fix preserveReasoning flag to control API reasoning inclusion (#9453)
* feat: store reasoning in conversation history for all providers * refactor: address review feedback - Move comments inside else block - Combine reasoning checks into single if block - Make comments more concise * refactor: make comments more concise * Fix preserveReasoning flag to control API reasoning inclusion Changes: 1. Removed hardcoded <think> tag logic in streaming - Previously hardcoded reasoning into assistant message text - Now passes reasoning to addToApiConversationHistory as parameter 2. Updated buildCleanConversationHistory to respect preserveReasoning flag - When preserveReasoning: true → reasoning block included in API requests - When preserveReasoning: false/undefined → reasoning stripped from API - Reasoning stored in history for all cases 3. Added temporary debug logs to base-openai-compatible-provider.ts - Shows preserveReasoning flag value - Logs reasoning blocks in incoming messages - Logs <think> tags in converted messages sent to API * Fix: Use api.getModel() directly instead of cachedStreamingModel Addresses review comment: cachedStreamingModel is set during streaming but buildCleanConversationHistory is called before streaming starts. Using the cached value could cause stale model info when switching models between requests. Now directly uses this.api.getModel().info.preserveReasoning to ensure we always check the current model's flag, not a potentially stale cached value. * Clean up comments in Task.ts Removed outdated comment regarding model's preserveReasoning flag. * fix: remove unnecessary reasoningBlock variable in task reasoning logic
1 parent 7715158 commit 5c5a893

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/core/task/Task.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,21 +2660,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
26602660
})
26612661
}
26622662

2663-
// Check if we should preserve reasoning in the assistant message
2664-
let finalAssistantMessage = assistantMessage
2665-
if (reasoningMessage && streamModelInfo.preserveReasoning) {
2666-
// Prepend reasoning in XML tags to the assistant message so it's included in API history
2667-
finalAssistantMessage = `<think>${reasoningMessage}</think>\n${assistantMessage}`
2668-
}
2669-
26702663
// Build the assistant message content array
26712664
const assistantContent: Array<Anthropic.TextBlockParam | Anthropic.ToolUseBlockParam> = []
26722665

26732666
// Add text content if present
2674-
if (finalAssistantMessage) {
2667+
if (assistantMessage) {
26752668
assistantContent.push({
26762669
type: "text" as const,
2677-
text: finalAssistantMessage,
2670+
text: assistantMessage,
26782671
})
26792672
}
26802673

@@ -3439,15 +3432,24 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
34393432

34403433
continue
34413434
} else if (hasPlainTextReasoning) {
3442-
// Strip plain text reasoning, send assistant message only
3435+
// Check if the model's preserveReasoning flag is set
3436+
// If true, include the reasoning block in API requests
3437+
// If false/undefined, strip it out (stored for history only, not sent back to API)
3438+
const shouldPreserveForApi = this.api.getModel().info.preserveReasoning === true
34433439
let assistantContent: Anthropic.Messages.MessageParam["content"]
34443440

3445-
if (rest.length === 0) {
3446-
assistantContent = ""
3447-
} else if (rest.length === 1 && rest[0].type === "text") {
3448-
assistantContent = (rest[0] as Anthropic.Messages.TextBlockParam).text
3441+
if (shouldPreserveForApi) {
3442+
// Include reasoning block in the content sent to API
3443+
assistantContent = contentArray
34493444
} else {
3450-
assistantContent = rest
3445+
// Strip reasoning out - stored for history only, not sent back to API
3446+
if (rest.length === 0) {
3447+
assistantContent = ""
3448+
} else if (rest.length === 1 && rest[0].type === "text") {
3449+
assistantContent = (rest[0] as Anthropic.Messages.TextBlockParam).text
3450+
} else {
3451+
assistantContent = rest
3452+
}
34513453
}
34523454

34533455
cleanConversationHistory.push({

0 commit comments

Comments
 (0)