( items: readonly ToolActivityItem[], )
| 789 | * revision. |
| 790 | */ |
| 791 | export function foldShellRunToolActivities( |
| 792 | items: readonly ToolActivityItem[], |
| 793 | ): ToolActivityItem[] { |
| 794 | const ownedRefs = new Set<string>(); |
| 795 | for (const item of items) { |
| 796 | if (item.toolName === "Bash" && item.result?.kind === "shell_run") |
| 797 | ownedRefs.add(item.result.ref); |
| 798 | } |
| 799 | |
| 800 | const folded: ToolActivityItem[] = []; |
| 801 | const parentIndexByRef = new Map<string, number>(); |
| 802 | const childResultsByRef = new Map<string, ShellRunToolResult[]>(); |
| 803 | |
| 804 | for (const item of items) { |
| 805 | const result = item.result?.kind === "shell_run" ? item.result : undefined; |
| 806 | if (!result || item.toolName === "Bash") { |
| 807 | if (result) parentIndexByRef.set(result.ref, folded.length); |
| 808 | folded.push(item); |
| 809 | continue; |
| 810 | } |
| 811 | if (ownedRefs.has(result.ref)) { |
| 812 | const pending = childResultsByRef.get(result.ref); |
| 813 | if (pending) pending.push(result); |
| 814 | else childResultsByRef.set(result.ref, [result]); |
| 815 | if (item.toolName === "Read" || item.toolName === "StopBackgroundTask") |
| 816 | continue; |
| 817 | } |
| 818 | folded.push(item); |
| 819 | } |
| 820 | |
| 821 | for (const [ref, results] of childResultsByRef) { |
| 822 | const index = parentIndexByRef.get(ref)!; |
| 823 | const parent = folded[index]!; |
| 824 | let current = |
| 825 | parent.result?.kind === "shell_run" ? parent.result : undefined; |
| 826 | let changed = false; |
| 827 | for (const result of results) { |
| 828 | const merged = mergeShellRunStateWithDiagnostics( |
| 829 | current, |
| 830 | result, |
| 831 | "ui.fold-shell-run-child", |
| 832 | ); |
| 833 | if (merged.changed) { |
| 834 | current = merged.result; |
| 835 | changed = true; |
| 836 | } |
| 837 | } |
| 838 | if (changed && current) folded[index] = { ...parent, result: current }; |
| 839 | } |
| 840 | |
| 841 | return folded; |
| 842 | } |
| 843 | |
| 844 | function foldShellRunTurns( |
| 845 | turns: readonly TurnViewModel[], |
no test coverage detected