diff --git a/cli/src/utils/think-tag-parser.ts b/cli/src/utils/think-tag-parser.ts index 58b9d86a8e..0e9fdbac01 100644 --- a/cli/src/utils/think-tag-parser.ts +++ b/cli/src/utils/think-tag-parser.ts @@ -11,29 +11,27 @@ export type ThinkSegment = { content: string } -/** - * Possible partial tag prefixes that we should buffer. - * These are prefixes that could become a complete tag with more input. - */ -const PARTIAL_OPEN_PREFIXES = ['<', ' 0) { + while (cursor < len) { if (insideThink) { // Look for closing tag - const closeIdx = remaining.indexOf(THINK_CLOSE_TAG) + const closeIdx = text.indexOf(THINK_CLOSE_TAG, cursor) if (closeIdx === -1) { // No closing tag found - all remaining is thinking content - if (remaining.length > 0) { - segments.push({ type: 'thinking', content: remaining }) + if (cursor < len) { + segments.push({ type: 'thinking', content: text.slice(cursor) }) } break } // Content before closing tag is thinking - if (closeIdx > 0) { - segments.push({ type: 'thinking', content: remaining.slice(0, closeIdx) }) + if (closeIdx > cursor) { + segments.push({ + type: 'thinking', + content: text.slice(cursor, closeIdx), + }) } - remaining = remaining.slice(closeIdx + THINK_CLOSE_TAG.length) + cursor = closeIdx + THINK_CLOSE_TAG.length insideThink = false } else { - const openIdx = remaining.indexOf(THINK_OPEN_TAG) - const closeIdx = remaining.indexOf(THINK_CLOSE_TAG) + const openIdx = text.indexOf(THINK_OPEN_TAG, cursor) + const closeIdx = text.indexOf(THINK_CLOSE_TAG, cursor) + if (closeIdx !== -1 && (openIdx === -1 || closeIdx < openIdx)) { - if (closeIdx > 0) { + if (closeIdx > cursor) { segments.push({ type: 'thinking', - content: remaining.slice(0, closeIdx), + content: text.slice(cursor, closeIdx), }) } - remaining = remaining.slice(closeIdx + THINK_CLOSE_TAG.length) + cursor = closeIdx + THINK_CLOSE_TAG.length continue } // Look for opening tag if (openIdx === -1) { // No opening tag found - all remaining is regular text - if (remaining.length > 0) { - segments.push({ type: 'text', content: remaining }) + if (cursor < len) { + segments.push({ type: 'text', content: text.slice(cursor) }) } break } // Content before opening tag is regular text - if (openIdx > 0) { - segments.push({ type: 'text', content: remaining.slice(0, openIdx) }) + if (openIdx > cursor) { + segments.push({ type: 'text', content: text.slice(cursor, openIdx) }) } - remaining = remaining.slice(openIdx + THINK_OPEN_TAG.length) + cursor = openIdx + THINK_OPEN_TAG.length insideThink = true } }