(line: Buffer)
| 768 | } |
| 769 | |
| 770 | function parseLargeJsonlBuffer(line: Buffer): JournalEntry | null { |
| 771 | let rootStart = 0 |
| 772 | while (rootStart < line.length && isJsonWhitespaceByte(line[rootStart])) rootStart++ |
| 773 | if (line[rootStart] !== 0x7b) return null |
| 774 | const rootEnd = findJsonContainerEndBuffer(line, rootStart, 0x7b, 0x7d) |
| 775 | if (rootEnd === -1) return null |
| 776 | const rootLimit = rootEnd + 1 |
| 777 | const type = readJsonStringBuffer(line, findObjectFieldValueBuffer(line, rootStart, rootLimit, 'type')) |
| 778 | if (!type) return null |
| 779 | |
| 780 | const entry: JournalEntry = { type } |
| 781 | const timestamp = readJsonStringBuffer(line, findObjectFieldValueBuffer(line, rootStart, rootLimit, 'timestamp')) |
| 782 | const sessionId = readJsonStringBuffer(line, findObjectFieldValueBuffer(line, rootStart, rootLimit, 'sessionId')) |
| 783 | const cwd = readJsonStringBuffer(line, findObjectFieldValueBuffer(line, rootStart, rootLimit, 'cwd')) |
| 784 | if (timestamp !== undefined) entry.timestamp = timestamp |
| 785 | if (sessionId !== undefined) entry.sessionId = sessionId |
| 786 | if (cwd !== undefined) entry.cwd = cwd |
| 787 | const addedNames = extractLargeAddedNamesBuffer(line, findObjectFieldValueBuffer(line, rootStart, rootLimit, 'attachment')) |
| 788 | if (addedNames.length > 0) { |
| 789 | ;(entry as Record<string, unknown>)['attachment'] = { type: 'deferred_tools_delta', addedNames } |
| 790 | } |
| 791 | |
| 792 | if (type === 'user') { |
| 793 | const message = findObjectFieldValueBuffer(line, rootStart, rootLimit, 'message') |
| 794 | if (message?.kind === 'object') { |
| 795 | const content = findObjectFieldValueBuffer(line, message.start, message.end, 'content') |
| 796 | const text = extractLargeUserTextBuffer(line, content) |
| 797 | if (text !== undefined) entry.message = { role: 'user', content: text } |
| 798 | } |
| 799 | return entry |
| 800 | } |
| 801 | |
| 802 | if (type !== 'assistant') return entry |
| 803 | const message = findObjectFieldValueBuffer(line, rootStart, rootLimit, 'message') |
| 804 | if (message?.kind !== 'object') return entry |
| 805 | const model = readJsonStringBuffer(line, findObjectFieldValueBuffer(line, message.start, message.end, 'model')) |
| 806 | const usageBounds = findObjectFieldValueBuffer(line, message.start, message.end, 'usage') |
| 807 | if (!model || usageBounds?.kind !== 'object') return entry |
| 808 | const id = readJsonStringBuffer(line, findObjectFieldValueBuffer(line, message.start, message.end, 'id')) |
| 809 | const contentBounds = findObjectFieldValueBuffer(line, message.start, message.end, 'content') |
| 810 | |
| 811 | entry.message = { |
| 812 | type: 'message', |
| 813 | role: 'assistant', |
| 814 | model, |
| 815 | ...(id !== undefined ? { id } : {}), |
| 816 | content: extractLargeToolBlocksBuffer(line, contentBounds), |
| 817 | usage: parseLargeUsageBuffer(line, usageBounds), |
| 818 | } |
| 819 | |
| 820 | return entry |
| 821 | } |
| 822 | |
| 823 | function getTopLevelRawJsonStringField(head: string, field: string): string | null { |
| 824 | let i = 0 |
no test coverage detected