(messages: T[], briefToolNames: string[])
| 167 | * model forgets, text still shows — otherwise the user would see nothing. |
| 168 | */ |
| 169 | export function dropTextInBriefTurns<T extends { |
| 170 | type: string; |
| 171 | isMeta?: boolean; |
| 172 | message?: { |
| 173 | content: Array<{ |
| 174 | type: string; |
| 175 | name?: string; |
| 176 | }>; |
| 177 | }; |
| 178 | }>(messages: T[], briefToolNames: string[]): T[] { |
| 179 | const nameSet = new Set(briefToolNames); |
| 180 | // First pass: find which turns (bounded by non-meta user messages) contain |
| 181 | // a Brief tool_use. Tag each assistant text block with its turn index. |
| 182 | const turnsWithBrief = new Set<number>(); |
| 183 | const textIndexToTurn: number[] = []; |
| 184 | let turn = 0; |
| 185 | for (let i = 0; i < messages.length; i++) { |
| 186 | const msg = messages[i]!; |
| 187 | const block = msg.message?.content[0]; |
| 188 | if (msg.type === 'user' && block?.type !== 'tool_result' && !msg.isMeta) { |
| 189 | turn++; |
| 190 | continue; |
| 191 | } |
| 192 | if (msg.type === 'assistant') { |
| 193 | if (block?.type === 'text') { |
| 194 | textIndexToTurn[i] = turn; |
| 195 | } else if (block?.type === 'tool_use' && block.name && nameSet.has(block.name)) { |
| 196 | turnsWithBrief.add(turn); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | if (turnsWithBrief.size === 0) return messages; |
| 201 | // Second pass: drop text blocks whose turn called Brief. |
| 202 | return messages.filter((_, i) => { |
| 203 | const t = textIndexToTurn[i]; |
| 204 | return t === undefined || !turnsWithBrief.has(t); |
| 205 | }); |
| 206 | } |
| 207 | type Props = { |
| 208 | messages: MessageType[]; |
| 209 | tools: Tools; |
no test coverage detected