* Computes the diff stats for all the file edits in-between two messages.
(messages: Message[], fromMessageId: UUID, toMessageId: UUID | undefined)
| 721 | * Computes the diff stats for all the file edits in-between two messages. |
| 722 | */ |
| 723 | function computeDiffStatsBetweenMessages(messages: Message[], fromMessageId: UUID, toMessageId: UUID | undefined): DiffStats | undefined { |
| 724 | const startIndex = messages.findIndex(msg => msg.uuid === fromMessageId); |
| 725 | if (startIndex === -1) { |
| 726 | return undefined; |
| 727 | } |
| 728 | let endIndex = toMessageId ? messages.findIndex(msg => msg.uuid === toMessageId) : messages.length; |
| 729 | if (endIndex === -1) { |
| 730 | endIndex = messages.length; |
| 731 | } |
| 732 | const filesChanged: string[] = []; |
| 733 | let insertions = 0; |
| 734 | let deletions = 0; |
| 735 | for (let i = startIndex + 1; i < endIndex; i++) { |
| 736 | const msg = messages[i]; |
| 737 | if (!msg || !isToolUseResultMessage(msg)) { |
| 738 | continue; |
| 739 | } |
| 740 | const result = msg.toolUseResult as FileEditOutput | FileWriteToolOutput; |
| 741 | if (!result || !result.filePath || !result.structuredPatch) { |
| 742 | continue; |
| 743 | } |
| 744 | if (!filesChanged.includes(result.filePath)) { |
| 745 | filesChanged.push(result.filePath); |
| 746 | } |
| 747 | try { |
| 748 | if ('type' in result && result.type === 'create') { |
| 749 | insertions += result.content.split(/\r?\n/).length; |
| 750 | } else { |
| 751 | for (const hunk of result.structuredPatch) { |
| 752 | const additions = count(hunk.lines, line => line.startsWith('+')); |
| 753 | const removals = count(hunk.lines, line => line.startsWith('-')); |
| 754 | insertions += additions; |
| 755 | deletions += removals; |
| 756 | } |
| 757 | } |
| 758 | } catch { |
| 759 | continue; |
| 760 | } |
| 761 | } |
| 762 | return { |
| 763 | filesChanged, |
| 764 | insertions, |
| 765 | deletions |
| 766 | }; |
| 767 | } |
| 768 | export function selectableUserMessagesFilter(message: Message): message is UserMessage { |
| 769 | if (message.type !== 'user') { |
| 770 | return false; |
no test coverage detected