Skip to content

Commit b13938e

Browse files
committed
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
1 parent 1000dcc commit b13938e

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/core/task/Task.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,21 +2647,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
26472647
})
26482648
}
26492649

2650-
// Check if we should preserve reasoning in the assistant message
2651-
let finalAssistantMessage = assistantMessage
2652-
if (reasoningMessage && streamModelInfo.preserveReasoning) {
2653-
// Prepend reasoning in XML tags to the assistant message so it's included in API history
2654-
finalAssistantMessage = `<think>${reasoningMessage}</think>\n${assistantMessage}`
2655-
}
2656-
26572650
// Build the assistant message content array
26582651
const assistantContent: Array<Anthropic.TextBlockParam | Anthropic.ToolUseBlockParam> = []
26592652

26602653
// Add text content if present
2661-
if (finalAssistantMessage) {
2654+
if (assistantMessage) {
26622655
assistantContent.push({
26632656
type: "text" as const,
2664-
text: finalAssistantMessage,
2657+
text: assistantMessage,
26652658
})
26662659
}
26672660

@@ -3432,16 +3425,28 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
34323425

34333426
continue
34343427
} else if (hasPlainTextReasoning) {
3435-
// For plain text reasoning, just strip it out and send the assistant message without it
3436-
// The reasoning was stored for history purposes, not for API requests
3428+
const reasoningBlock = first as any
3429+
3430+
// Check if the model's preserveReasoning flag is set
3431+
// If true, include the reasoning block in API requests
3432+
// If false/undefined, strip it out (stored for history only, not sent back to API)
3433+
const modelInfo = this.cachedStreamingModel?.info ?? this.api.getModel().info
3434+
const shouldPreserveForApi = modelInfo.preserveReasoning === true
3435+
34373436
let assistantContent: Anthropic.Messages.MessageParam["content"]
34383437

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

34473452
cleanConversationHistory.push({

0 commit comments

Comments
 (0)