( data: NotebookCellSource[], toolUseID: string, )
| 186 | * Maps notebook cell data to tool result block parameters with sophisticated text block merging |
| 187 | */ |
| 188 | export function mapNotebookCellsToToolResult( |
| 189 | data: NotebookCellSource[], |
| 190 | toolUseID: string, |
| 191 | ): ToolResultBlockParam { |
| 192 | const allResults = data.flatMap(getToolResultFromCell) |
| 193 | |
| 194 | // Merge adjacent text blocks |
| 195 | return { |
| 196 | tool_use_id: toolUseID, |
| 197 | type: 'tool_result' as const, |
| 198 | content: allResults.reduce<(TextBlockParam | ImageBlockParam)[]>( |
| 199 | (acc, curr) => { |
| 200 | if (acc.length === 0) return [curr] |
| 201 | |
| 202 | const prev = acc[acc.length - 1] |
| 203 | if (prev && prev.type === 'text' && curr.type === 'text') { |
| 204 | // Merge the text blocks |
| 205 | prev.text += '\n' + curr.text |
| 206 | return acc |
| 207 | } |
| 208 | |
| 209 | acc.push(curr) |
| 210 | return acc |
| 211 | }, |
| 212 | [], |
| 213 | ), |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | export function parseCellId(cellId: string): number | undefined { |
| 218 | const match = cellId.match(/^cell-(\d+)$/) |
no test coverage detected