feat: add two-layer thinking block validation (proactive + reactive) (#248)
- Add thinking-block-validator hook for proactive prevention before API calls - Enhance session-recovery to include previous thinking content - Fix hook registration to actually invoke the validator Addresses extended thinking errors with Claude Opus/Sonnet 4.5 using tool calls. Related: https://github.com/vercel/ai/issues/7729 Related: https://github.com/sst/opencode/issues/2599
This commit is contained in:
@@ -223,6 +223,41 @@ export function findMessagesWithOrphanThinking(sessionID: string): string[] {
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the most recent thinking content from previous assistant messages
|
||||
* Following Anthropic's recommendation to include thinking blocks from previous turns
|
||||
*/
|
||||
function findLastThinkingContent(sessionID: string, beforeMessageID: string): string {
|
||||
const messages = readMessages(sessionID)
|
||||
|
||||
// Find the index of the current message
|
||||
const currentIndex = messages.findIndex(m => m.id === beforeMessageID)
|
||||
if (currentIndex === -1) return ""
|
||||
|
||||
// Search backwards through previous assistant messages
|
||||
for (let i = currentIndex - 1; i >= 0; i--) {
|
||||
const msg = messages[i]
|
||||
if (msg.role !== "assistant") continue
|
||||
|
||||
// Look for thinking parts in this message
|
||||
const parts = readParts(msg.id)
|
||||
for (const part of parts) {
|
||||
if (THINKING_TYPES.has(part.type)) {
|
||||
// Found thinking content - return it
|
||||
// Note: 'thinking' type uses 'thinking' property, 'reasoning' type uses 'text' property
|
||||
const thinking = (part as { thinking?: string; text?: string }).thinking
|
||||
const reasoning = (part as { thinking?: string; text?: string }).text
|
||||
const content = thinking || reasoning
|
||||
if (content && content.trim().length > 0) {
|
||||
return content
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
export function prependThinkingPart(sessionID: string, messageID: string): boolean {
|
||||
const partDir = join(PART_STORAGE, messageID)
|
||||
|
||||
@@ -230,13 +265,16 @@ export function prependThinkingPart(sessionID: string, messageID: string): boole
|
||||
mkdirSync(partDir, { recursive: true })
|
||||
}
|
||||
|
||||
// Try to get thinking content from previous turns (Anthropic's recommendation)
|
||||
const previousThinking = findLastThinkingContent(sessionID, messageID)
|
||||
|
||||
const partId = `prt_0000000000_thinking`
|
||||
const part = {
|
||||
id: partId,
|
||||
sessionID,
|
||||
messageID,
|
||||
type: "thinking",
|
||||
thinking: "",
|
||||
thinking: previousThinking || "[Continuing from previous reasoning]",
|
||||
synthetic: true,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user