(source: Buffer, contentBounds: BufferJsonValueBounds | null)
| 657 | } |
| 658 | |
| 659 | function extractLargeToolBlocksBuffer(source: Buffer, contentBounds: BufferJsonValueBounds | null): ToolUseBlock[] { |
| 660 | if (!contentBounds || contentBounds.kind !== 'array') return [] |
| 661 | const tools: ToolUseBlock[] = [] |
| 662 | let i = contentBounds.start + 1 |
| 663 | while (i < contentBounds.end - 1 && tools.length < MAX_TOOL_BLOCKS) { |
| 664 | while (i < contentBounds.end && isJsonWhitespaceByte(source[i])) i++ |
| 665 | if (source[i] === 0x2c) { |
| 666 | i++ |
| 667 | continue |
| 668 | } |
| 669 | if (source[i] !== 0x7b) { |
| 670 | i++ |
| 671 | continue |
| 672 | } |
| 673 | const objectEnd = findJsonContainerEndBuffer(source, i, 0x7b, 0x7d, contentBounds.end) |
| 674 | if (objectEnd === -1) break |
| 675 | const objectBounds = { start: i, end: objectEnd + 1, kind: 'object' as const } |
| 676 | const blockType = readJsonStringBuffer(source, findObjectFieldValueBuffer(source, objectBounds.start, objectBounds.end, 'type')) |
| 677 | if (blockType === 'tool_use') { |
| 678 | const name = readJsonStringBuffer(source, findObjectFieldValueBuffer(source, objectBounds.start, objectBounds.end, 'name')) ?? '' |
| 679 | const id = readJsonStringBuffer(source, findObjectFieldValueBuffer(source, objectBounds.start, objectBounds.end, 'id')) ?? '' |
| 680 | const inputBounds = findObjectFieldValueBuffer(source, objectBounds.start, objectBounds.end, 'input') |
| 681 | const input: Record<string, unknown> = {} |
| 682 | if (inputBounds?.kind === 'object') { |
| 683 | if (name === 'Skill') { |
| 684 | const skill = readJsonStringBuffer(source, findObjectFieldValueBuffer(source, inputBounds.start, inputBounds.end, 'skill'), 200) |
| 685 | const skillName = readJsonStringBuffer(source, findObjectFieldValueBuffer(source, inputBounds.start, inputBounds.end, 'name'), 200) |
| 686 | if (skill !== undefined) input['skill'] = skill |
| 687 | if (skillName !== undefined) input['name'] = skillName |
| 688 | } else if (name === 'Read' || name === 'FileReadTool' || EDIT_TOOLS.has(name)) { |
| 689 | const filePath = readJsonStringBuffer(source, findObjectFieldValueBuffer(source, inputBounds.start, inputBounds.end, 'file_path'), BASH_COMMAND_CAP) |
| 690 | if (filePath !== undefined) input['file_path'] = filePath |
| 691 | } else if (name === 'Agent' || name === 'Task') { |
| 692 | const subagentType = readJsonStringBuffer(source, findObjectFieldValueBuffer(source, inputBounds.start, inputBounds.end, 'subagent_type'), 200) |
| 693 | if (subagentType !== undefined) input['subagent_type'] = subagentType |
| 694 | } else if (BASH_TOOLS.has(name)) { |
| 695 | const command = readJsonStringBuffer(source, findObjectFieldValueBuffer(source, inputBounds.start, inputBounds.end, 'command'), BASH_COMMAND_CAP) |
| 696 | if (command !== undefined) input['command'] = command |
| 697 | } |
| 698 | } |
| 699 | tools.push({ type: 'tool_use', id, name, input }) |
| 700 | } |
| 701 | i = objectEnd + 1 |
| 702 | } |
| 703 | return tools |
| 704 | } |
| 705 | |
| 706 | function extractLargeUserTextBuffer(source: Buffer, contentBounds: BufferJsonValueBounds | null): string | undefined { |
| 707 | if (!contentBounds) return undefined |
no test coverage detected