( blocks: ContentBlock[], text: string, )
| 132 | * Used by both root stream and agent blocks. |
| 133 | */ |
| 134 | const appendTextWithThinkParsingToBlocks = ( |
| 135 | blocks: ContentBlock[], |
| 136 | text: string, |
| 137 | ): ContentBlock[] => { |
| 138 | if (!text) { |
| 139 | return blocks |
| 140 | } |
| 141 | |
| 142 | const nextBlocks = [...blocks] |
| 143 | const lastBlock = nextBlocks[nextBlocks.length - 1] |
| 144 | const wasInsideThinking = isOpenThinkingBlock(lastBlock) |
| 145 | |
| 146 | let textToParse = text |
| 147 | let lastBlockContent = '' |
| 148 | |
| 149 | if (wasInsideThinking && lastBlock?.type === 'text') { |
| 150 | lastBlockContent = lastBlock.content |
| 151 | |
| 152 | const partialLen = getPartialTagLength(lastBlockContent) |
| 153 | if (partialLen > 0) { |
| 154 | const potentialTag = lastBlockContent.slice(-partialLen) + text |
| 155 | if (potentialTag.startsWith(THINK_CLOSE_TAG)) { |
| 156 | const newLastContent = lastBlockContent.slice(0, -partialLen) |
| 157 | textToParse = lastBlockContent.slice(-partialLen) + text |
| 158 | |
| 159 | if (newLastContent) { |
| 160 | nextBlocks[nextBlocks.length - 1] = { |
| 161 | ...lastBlock, |
| 162 | content: newLastContent, |
| 163 | } |
| 164 | } else { |
| 165 | nextBlocks.pop() |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } else if ( |
| 170 | !wasInsideThinking && |
| 171 | lastBlock?.type === 'text' && |
| 172 | lastBlock.textType === 'text' |
| 173 | ) { |
| 174 | lastBlockContent = lastBlock.content |
| 175 | const partialLen = getPartialTagLength(lastBlockContent) |
| 176 | if (partialLen > 0) { |
| 177 | const potentialTag = lastBlockContent.slice(-partialLen) + text |
| 178 | if (potentialTag.startsWith(THINK_OPEN_TAG)) { |
| 179 | const newLastContent = lastBlockContent.slice(0, -partialLen) |
| 180 | textToParse = lastBlockContent.slice(-partialLen) + text |
| 181 | |
| 182 | if (newLastContent) { |
| 183 | nextBlocks[nextBlocks.length - 1] = { |
| 184 | ...lastBlock, |
| 185 | content: newLastContent, |
| 186 | } |
| 187 | } else { |
| 188 | nextBlocks.pop() |
| 189 | } |
| 190 | } |
| 191 | } |
no test coverage detected