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