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