@zyro-sec/session-ui 0.0.0-stage → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/AGENTS.md +25 -0
  2. package/bunfig.toml +2 -0
  3. package/component-tests/basic-tool.fixture.tsx +46 -0
  4. package/component-tests/basic-tool.spec.ts +63 -0
  5. package/component-tests/file-media.fixture.tsx +39 -0
  6. package/component-tests/file-media.spec.ts +13 -0
  7. package/component-tests/file-tool.spec.ts +62 -0
  8. package/component-tests/file-viewer.fixture.tsx +109 -0
  9. package/component-tests/file-viewer.spec.ts +134 -0
  10. package/component-tests/file.spec.ts +33 -0
  11. package/component-tests/line-comment-v2.spec.ts +15 -0
  12. package/component-tests/markdown-code-wrap.spec.ts +94 -0
  13. package/component-tests/markdown-readiness.spec.ts +95 -0
  14. package/component-tests/markdown.fixture.tsx +104 -0
  15. package/component-tests/markdown.spec.ts +1564 -0
  16. package/component-tests/mermaid-preview.spec.ts +43 -0
  17. package/component-tests/patch-group.spec.ts +79 -0
  18. package/component-tests/read-group.spec.ts +72 -0
  19. package/component-tests/read-image.fixture.tsx +66 -0
  20. package/component-tests/read-image.spec.ts +154 -0
  21. package/component-tests/session-compaction.spec.ts +254 -0
  22. package/component-tests/session-lifecycle.spec.ts +190 -0
  23. package/component-tests/session-message-projection.spec.ts +639 -0
  24. package/component-tests/session-notice-projection.spec.ts +20 -0
  25. package/component-tests/session-review-comments.spec.ts +179 -0
  26. package/component-tests/session-shell.spec.ts +93 -0
  27. package/component-tests/session-timeline-mobile.spec.ts +201 -0
  28. package/component-tests/session-timeline-notices.spec.ts +38 -0
  29. package/component-tests/session-timeline-reasoning.spec.ts +107 -0
  30. package/component-tests/session-timeline.spec.ts +157 -0
  31. package/component-tests/session-tool-projection.spec.ts +323 -0
  32. package/component-tests/text-lifecycle.fixture.tsx +26 -0
  33. package/component-tests/text-lifecycle.spec.ts +42 -0
  34. package/component-tests/timeline-subagent-rows.spec.ts +33 -0
  35. package/component-tests/tool-disclosure.spec.ts +48 -0
  36. package/component-tests/tool-group-sticky.spec.ts +37 -0
  37. package/component-tests/tool-group.spec.ts +290 -0
  38. package/component-tests/tool-status-title.fixture.tsx +22 -0
  39. package/component-tests/tool-status-title.spec.ts +30 -0
  40. package/component-tests/user-message-quotes.spec.ts +111 -0
  41. package/package.json +91 -4
  42. package/playwright.components.config.ts +4 -0
  43. package/playwright.local.config.ts +2 -0
  44. package/src/actions.ts +54 -0
  45. package/src/components/apply-patch-file.ts +122 -0
  46. package/src/components/basic-tool.css +487 -0
  47. package/src/components/basic-tool.tsx +258 -0
  48. package/src/components/copy-machine.ts +78 -0
  49. package/src/components/copy.ts +85 -0
  50. package/src/components/diagram-preview.tsx +28 -0
  51. package/src/components/disclosure-machine.ts +188 -0
  52. package/src/components/disclosure.ts +129 -0
  53. package/src/components/dock-prompt.stories.tsx +73 -0
  54. package/src/components/dock-prompt.tsx +29 -0
  55. package/src/components/file-comment-selection.tsx +146 -0
  56. package/src/components/file-media-machine.ts +165 -0
  57. package/src/components/file-media.stories.tsx +112 -0
  58. package/src/components/file-media.tsx +323 -0
  59. package/src/components/file-search.tsx +73 -0
  60. package/src/components/file.css +59 -0
  61. package/src/components/file.tsx +1300 -0
  62. package/src/components/image-preview.tsx +35 -0
  63. package/src/components/line-comment-annotations.tsx +580 -0
  64. package/src/components/line-comment-machine.ts +262 -0
  65. package/src/components/line-comment-styles.ts +293 -0
  66. package/src/components/line-comment.stories.tsx +152 -0
  67. package/src/components/line-comment.tsx +448 -0
  68. package/src/components/link-segments.ts +20 -0
  69. package/src/components/linked-text.tsx +21 -0
  70. package/src/components/markdown-cache-machine.ts +257 -0
  71. package/src/components/markdown-cache.tsx +556 -0
  72. package/src/components/markdown-clipboard.ts +101 -0
  73. package/src/components/markdown-code-machine.ts +200 -0
  74. package/src/components/markdown-code-state.ts +22 -0
  75. package/src/components/markdown-fragment-links.ts +74 -0
  76. package/src/components/markdown-github.svg +3 -0
  77. package/src/components/markdown-globe.svg +5 -0
  78. package/src/components/markdown-image-machine.ts +170 -0
  79. package/src/components/markdown-image.ts +156 -0
  80. package/src/components/markdown-inline-code-kind.ts +39 -0
  81. package/src/components/markdown-mermaid.ts +18 -0
  82. package/src/components/markdown-phase-machine.ts +102 -0
  83. package/src/components/markdown-projection.ts +13 -0
  84. package/src/components/markdown-session-links.ts +183 -0
  85. package/src/components/markdown-solid.test.tsx +92 -0
  86. package/src/components/markdown-solid.tsx +113 -0
  87. package/src/components/markdown-stream.ts +95 -0
  88. package/src/components/markdown-worker-machine.ts +516 -0
  89. package/src/components/markdown-worker-protocol.ts +73 -0
  90. package/src/components/markdown-worker.ts +250 -0
  91. package/src/components/markdown.css +672 -0
  92. package/src/components/markdown.stories.tsx +199 -0
  93. package/src/components/markdown.tsx +1612 -0
  94. package/src/components/markdown.worker.ts +188 -0
  95. package/src/components/message-file.ts +102 -0
  96. package/src/components/message-nav.css +134 -0
  97. package/src/components/message-nav.tsx +159 -0
  98. package/src/components/message-part.css +2109 -0
  99. package/src/components/message-part.tsx +3 -0
  100. package/src/components/question-prompt-experimental.css +576 -0
  101. package/src/components/question-prompt.css +527 -0
  102. package/src/components/quote-highlight.css +59 -0
  103. package/src/components/review-focus-driver.ts +142 -0
  104. package/src/components/review-focus-machine.ts +95 -0
  105. package/src/components/session-compaction.css +270 -0
  106. package/src/components/session-diff.ts +358 -0
  107. package/src/components/session-error.tsx +41 -0
  108. package/src/components/session-retry.tsx +92 -0
  109. package/src/components/session-review-machine.ts +241 -0
  110. package/src/components/session-review.css +264 -0
  111. package/src/components/session-review.stories.tsx +232 -0
  112. package/src/components/session-review.tsx +733 -0
  113. package/src/components/session-turn.css +282 -0
  114. package/src/components/shell-submessage.css +24 -0
  115. package/src/components/timeline-separator.tsx +69 -0
  116. package/src/components/tool-count-label.css +57 -0
  117. package/src/components/tool-count-label.tsx +76 -0
  118. package/src/components/tool-count-summary.css +102 -0
  119. package/src/components/tool-count-summary.stories.tsx +99 -0
  120. package/src/components/tool-count-summary.tsx +44 -0
  121. package/src/components/tool-error-card.css +178 -0
  122. package/src/components/tool-error-card.stories.tsx +113 -0
  123. package/src/components/tool-error-card.tsx +202 -0
  124. package/src/components/tool-status-title-machine.ts +128 -0
  125. package/src/components/tool-status-title.css +89 -0
  126. package/src/components/tool-status-title.tsx +163 -0
  127. package/src/context/data.tsx +107 -0
  128. package/src/context/index.ts +1 -0
  129. package/src/context/markdown.tsx +129 -0
  130. package/src/context/sources.tsx +34 -0
  131. package/src/document.ts +8 -0
  132. package/src/file-presentation.ts +34 -0
  133. package/src/message/action-machine.ts +72 -0
  134. package/src/message/agent-label.ts +3 -0
  135. package/src/message/attachment-card.css +119 -0
  136. package/src/message/attachment-card.tsx +40 -0
  137. package/src/message/attachment-media-machine.ts +213 -0
  138. package/src/message/comment-card.tsx +155 -0
  139. package/src/message/compaction-entrance-machine.ts +93 -0
  140. package/src/message/current-message.tsx +157 -0
  141. package/src/message/current-tool-state.ts +161 -0
  142. package/src/message/message-content.tsx +1391 -0
  143. package/src/message/paced-text-machine.ts +73 -0
  144. package/src/message/prompt-message.stories.tsx +149 -0
  145. package/src/message/relative-time.ts +45 -0
  146. package/src/message/user-message-quote-parts.ts +16 -0
  147. package/src/message/user-message-quotes.stories.tsx +797 -0
  148. package/src/message/user-message-quotes.tsx +135 -0
  149. package/src/pierre/comment-hover-machine.ts +53 -0
  150. package/src/pierre/comment-hover.ts +131 -0
  151. package/src/pierre/comment-selection-machine.ts +257 -0
  152. package/src/pierre/commented-lines.ts +106 -0
  153. package/src/pierre/diff-selection.ts +91 -0
  154. package/src/pierre/file-find-machine.ts +323 -0
  155. package/src/pierre/file-find.ts +537 -0
  156. package/src/pierre/file-runtime.ts +42 -0
  157. package/src/pierre/file-selection.ts +140 -0
  158. package/src/pierre/index.ts +344 -0
  159. package/src/pierre/media.ts +140 -0
  160. package/src/pierre/render-machine.ts +381 -0
  161. package/src/pierre/selection-bridge.ts +82 -0
  162. package/src/pierre/selection-machine.ts +198 -0
  163. package/src/pierre/virtualizer.ts +114 -0
  164. package/src/pierre/worker.ts +46 -0
  165. package/src/sources/experiment.ts +59 -0
  166. package/src/sources/site-icon.tsx +53 -0
  167. package/src/sources/sources-chip.tsx +57 -0
  168. package/src/sources/sources-expand-machine.ts +101 -0
  169. package/src/sources/sources-panel.tsx +249 -0
  170. package/src/sources/sources-title.tsx +21 -0
  171. package/src/sources/sources.css +266 -0
  172. package/src/sources/sources.ts +141 -0
  173. package/src/storybook/current-session-fixtures.ts +1492 -0
  174. package/src/storybook/current-session-scenarios.ts +76 -0
  175. package/src/storybook/current-session-story.tsx +154 -0
  176. package/src/storybook/index.ts +43 -0
  177. package/src/styles/index.css +19 -0
  178. package/src/timeline/__snapshots__/group-labels.test.ts.snap +110 -0
  179. package/src/timeline/__snapshots__/placement.test.ts.snap +1041 -0
  180. package/src/timeline/code-blocks.stories.tsx +105 -0
  181. package/src/timeline/commentary.stories.tsx +70 -0
  182. package/src/timeline/commentary.ts +38 -0
  183. package/src/timeline/detail.ts +111 -0
  184. package/src/timeline/error-card.stories.tsx +134 -0
  185. package/src/timeline/error-spacing.stories.tsx +71 -0
  186. package/src/timeline/facts.ts +493 -0
  187. package/src/timeline/file-changes.stories.tsx +436 -0
  188. package/src/timeline/group-label-fixtures.ts +97 -0
  189. package/src/timeline/mermaid.stories.tsx +131 -0
  190. package/src/timeline/place.ts +280 -0
  191. package/src/timeline/placement-fixtures.ts +365 -0
  192. package/src/timeline/placement-reactivity.ts +133 -0
  193. package/src/timeline/projection.ts +1261 -0
  194. package/src/timeline/research-and-agents.stories.tsx +461 -0
  195. package/src/timeline/session-timeline-row.tsx +1675 -0
  196. package/src/timeline/session-timeline.tsx +68 -0
  197. package/src/timeline/steer-arrival-machine.ts +134 -0
  198. package/src/timeline/terminal-work.stories.tsx +464 -0
  199. package/src/timeline/timeline-row.stories.tsx +1069 -0
  200. package/src/timeline/timeline-row.ts +238 -0
  201. package/src/timeline/turn-phase-machine.ts +112 -0
  202. package/src/timeline/user-message-fold.stories.tsx +194 -0
  203. package/src/timeline/user-runs.ts +87 -0
  204. package/src/timeline-next/__snapshots__/placement.test.ts.snap +990 -0
  205. package/src/timeline-next/compaction.tsx +236 -0
  206. package/src/timeline-next/errors.ts +101 -0
  207. package/src/timeline-next/event-row.tsx +168 -0
  208. package/src/timeline-next/events.ts +103 -0
  209. package/src/timeline-next/index.ts +10 -0
  210. package/src/timeline-next/nested-rail.ts +125 -0
  211. package/src/timeline-next/projection.ts +1028 -0
  212. package/src/timeline-next/row.tsx +1728 -0
  213. package/src/timeline-next/rules.ts +89 -0
  214. package/src/timeline-next/separator.tsx +73 -0
  215. package/src/timeline-next/spacing.ts +127 -0
  216. package/src/timeline-next/status-line.tsx +41 -0
  217. package/src/timeline-next/status.ts +33 -0
  218. package/src/timeline-next/stories/compare-renderer.stories.tsx +190 -0
  219. package/src/timeline-next/stories/compare-sessions/.gitignore +3 -0
  220. package/src/timeline-next/stories/compare.stories.tsx +486 -0
  221. package/src/timeline-next/stories/playground.stories.tsx +297 -0
  222. package/src/timeline-next/stories/session-replay.ts +1156 -0
  223. package/src/timeline-next/stories/session-timeline.tsx +50 -0
  224. package/src/timeline-next/stories/subagent-status.stories.tsx +132 -0
  225. package/src/timeline-next/timeline-next.css +649 -0
  226. package/src/timeline-next/tools/__snapshots__/activity-label.test.ts.snap +39 -0
  227. package/src/timeline-next/tools/activity-label.ts +166 -0
  228. package/src/timeline-next/tools/assistant-content.tsx +84 -0
  229. package/src/timeline-next/tools/context-group.tsx +619 -0
  230. package/src/timeline-next/tools/error-card.tsx +147 -0
  231. package/src/timeline-next/tools/explore.tsx +119 -0
  232. package/src/timeline-next/tools/file-group.tsx +128 -0
  233. package/src/timeline-next/tools/patch.tsx +174 -0
  234. package/src/timeline-next/tools/shell.tsx +186 -0
  235. package/src/timeline-next/tools/thought.tsx +87 -0
  236. package/src/timeline-next/tools/tool-display.tsx +119 -0
  237. package/src/tools/ansi.ts +8 -0
  238. package/src/tools/context-group-label.ts +123 -0
  239. package/src/tools/file-runs.ts +35 -0
  240. package/src/tools/project-path.ts +18 -0
  241. package/src/tools/read-group.stories.tsx +159 -0
  242. package/src/tools/run-machine.ts +185 -0
  243. package/src/tools/shell-output-machine.ts +233 -0
  244. package/src/tools/shell-output.ts +91 -0
  245. package/src/tools/subagent-row-variants.stories.tsx +129 -0
  246. package/src/tools/tool-group.stories.tsx +345 -0
  247. package/src/tools/tool-renderer.tsx +2876 -0
  248. package/src/v2/components/line-comment-annotations-v2.tsx +215 -0
  249. package/src/v2/components/session-file-panel-v2.tsx +149 -0
  250. package/src/v2/components/session-progress-indicator-v2.css +5 -0
  251. package/src/v2/components/session-progress-indicator-v2.stories.tsx +86 -0
  252. package/src/v2/components/session-progress-indicator-v2.tsx +16 -0
  253. package/src/v2/components/session-review-empty-changes-v2.tsx +17 -0
  254. package/src/v2/components/session-review-empty-no-git-v2.tsx +28 -0
  255. package/src/v2/components/session-review-file-preview-v2-virtualize.ts +5 -0
  256. package/src/v2/components/session-review-file-preview-v2.tsx +266 -0
  257. package/src/v2/components/session-review-v2.css +653 -0
  258. package/src/v2/components/session-review-v2.tsx +422 -0
  259. package/src/v2/components/sidebar-presence-machine.ts +69 -0
  260. package/tsconfig.json +20 -0
  261. package/README.md +0 -3
@@ -0,0 +1,358 @@
1
+ import { parseDiffFromFile, parsePatchFiles, processFile, type FileDiffMetadata } from "@pierre/diffs"
2
+ import { checksum } from "@zyro-sec/util/encode"
3
+ import type { FileDiffInfo } from "@zyro-sec/client/promise"
4
+ import type { PresentationFileDiff } from "../file-presentation"
5
+
6
+ type LegacyDiff = {
7
+ file: string
8
+ patch?: string
9
+ before?: string
10
+ after?: string
11
+ additions: number
12
+ deletions: number
13
+ status?: "added" | "deleted" | "modified"
14
+ }
15
+
16
+ type ReviewDiff = (PresentationFileDiff & { file: string }) | FileDiffInfo | LegacyDiff
17
+
18
+ export type DiffSource = Pick<LegacyDiff, "file" | "patch" | "before" | "after">
19
+
20
+ export type ViewDiff = {
21
+ file: string
22
+ additions: number
23
+ deletions: number
24
+ status?: "added" | "deleted" | "modified"
25
+ fileDiff: FileDiffMetadata
26
+ }
27
+
28
+ const diffCacheLimit = 16
29
+
30
+ const fileDiffCache = new Map<string, FileDiffMetadata>()
31
+
32
+ /** `context` is how many unchanged lines a diff built from complete contents keeps around each change. */
33
+ export function resolveFileDiff(diff: DiffSource, context = hunkContext) {
34
+ // Untyped persisted payloads may carry `patch: null`; those render from their contents.
35
+ if (diff.patch != null) return fileDiffFromPatch(diff.file, diff.patch, context)
36
+
37
+ return fileDiffFromContent(diff.file, diff.before ?? "", diff.after ?? "")
38
+ }
39
+
40
+ export function normalize(diff: ReviewDiff, context?: number): ViewDiff {
41
+ return {
42
+ file: diff.file,
43
+ additions: diff.additions,
44
+ deletions: diff.deletions,
45
+ status: diff.status,
46
+ fileDiff: resolveFileDiff(diff, context),
47
+ }
48
+ }
49
+
50
+ export function text(diff: ViewDiff, side: "deletions" | "additions") {
51
+ if (side === "deletions") return diff.fileDiff.deletionLines.join("")
52
+
53
+ return diff.fileDiff.additionLines.join("")
54
+ }
55
+
56
+ function fileDiffFromPatch(file: string, patch: string, context: number) {
57
+ const key = context === hunkContext ? `${file}\0${patch}` : `${file}\0${patch}\0${context}`
58
+
59
+ return cachedFileDiff(`patch\0${key}`, () => {
60
+ const complete = completePatch(patch)
61
+
62
+ const value = complete
63
+ ? fileDiffFromCompletePatch(file, complete, context)
64
+ : (partialFileDiff(file, trimContext(patch, context)) ?? emptyFileDiff(file))
65
+
66
+ // The patch text fixes both the content and the producer's alignment, so equal keys always mean equal hunks.
67
+ value.cacheKey = highlightKey(key)
68
+
69
+ return value
70
+ })
71
+ }
72
+
73
+ function cachedFileDiff(key: string, parse: () => FileDiffMetadata) {
74
+ const hit = fileDiffCache.get(key)
75
+
76
+ if (hit) {
77
+ fileDiffCache.delete(key)
78
+ fileDiffCache.set(key, hit)
79
+
80
+ return hit
81
+ }
82
+
83
+ const value = parse()
84
+ fileDiffCache.set(key, value)
85
+
86
+ while (fileDiffCache.size > diffCacheLimit) fileDiffCache.delete(fileDiffCache.keys().next().value!)
87
+
88
+ return value
89
+ }
90
+
91
+ export function completePatchContents(patch: string) {
92
+ const complete = completePatch(patch)
93
+
94
+ if (!complete) return
95
+
96
+ return { before: complete.before, after: complete.after }
97
+ }
98
+
99
+ function completePatch(patch: string) {
100
+ const parsed = splitPatch(patch)
101
+
102
+ if (!parsed) return
103
+
104
+ // Snapshot and VCS producers request full context. Tool patches use jsdiff's shorter default context.
105
+ if (!parsed.header.startsWith("diff --git ") && !/^--- [^\n]*\t\r?\n\+\+\+ [^\n]*\t(?:\r?\n|$)/m.test(parsed.header))
106
+ return
107
+
108
+ // Full patches collapse into one leading hunk. Separated hunks omit ranges and must stay partial.
109
+ if (parsed.hunks.length !== 1) return
110
+
111
+ const hunk = parsed.hunks[0]!
112
+
113
+ // An empty side's start names the line before the hunk, so `-1,0` begins after line 1.
114
+ if (hunk.deletionStart + (hunk.deletionCount ? 0 : 1) > 1 || hunk.additionStart + (hunk.additionCount ? 0 : 1) > 1)
115
+ return
116
+
117
+ const lines: string[] = []
118
+ const before: Array<{ text: string; newline: boolean }> = []
119
+ const after: Array<{ text: string; newline: boolean }> = []
120
+ let previous: "-" | "+" | " " | undefined
121
+
122
+ for (const line of parsed.body.split("\n").slice(1)) {
123
+ if (before.length >= hunk.deletionCount && after.length >= hunk.additionCount && !line.startsWith("\\")) break
124
+ lines.push(line)
125
+
126
+ if (line.startsWith("\\")) {
127
+ if (previous === "-" || previous === " ") {
128
+ const value = before.at(-1)
129
+
130
+ if (value) value.newline = false
131
+ }
132
+
133
+ if (previous === "+" || previous === " ") {
134
+ const value = after.at(-1)
135
+
136
+ if (value) value.newline = false
137
+ }
138
+
139
+ continue
140
+ }
141
+
142
+ if (line.startsWith("-")) {
143
+ before.push({ text: line.slice(1), newline: true })
144
+ previous = "-"
145
+ continue
146
+ }
147
+
148
+ if (line.startsWith("+")) {
149
+ after.push({ text: line.slice(1), newline: true })
150
+ previous = "+"
151
+ continue
152
+ }
153
+
154
+ before.push({ text: line.slice(1), newline: true })
155
+ after.push({ text: line.slice(1), newline: true })
156
+ previous = " "
157
+ }
158
+
159
+ const text = (lines: Array<{ text: string; newline: boolean }>) =>
160
+ lines.map((line) => line.text + (line.newline ? "\n" : "")).join("")
161
+
162
+ return { before: text(before), after: text(after), lines }
163
+ }
164
+
165
+ // Splits a one-file patch at its first hunk. Pierre's strict parser checks the hunks behind a fixed header, so a
166
+ // malformed hunk rejects the patch while unusual file headers stay readable.
167
+ function splitPatch(patch: string) {
168
+ const start = patch.search(/^@@ /m)
169
+ const header = start === -1 ? patch : patch.slice(0, start)
170
+ const body = start === -1 ? "" : patch.slice(start)
171
+
172
+ try {
173
+ const hunks = body
174
+ ? (processFile(`--- a\n+++ a\n${body}`, { isGitDiff: false, throwOnError: true })?.hunks ?? [])
175
+ : []
176
+
177
+ return { header, body, hunks }
178
+ } catch {
179
+ return
180
+ }
181
+ }
182
+
183
+ // A complete patch already carries the producer's line diff. Splitting its single full-context hunk at long
184
+ // unchanged runs yields, in linear time, the collapsed hunks Pierre would get by diffing the reconstructed files
185
+ // again whenever both algorithms choose the same alignment.
186
+ function fileDiffFromCompletePatch(
187
+ file: string,
188
+ patch: { before: string; after: string; lines: string[] },
189
+ context: number,
190
+ ) {
191
+ // Pierre reads names back from header text, so a fixed header keeps tabs, quotes, and newlines in `file` intact.
192
+ const value =
193
+ processFile(`--- a\n+++ a\n${splitHunk(patch.lines, context)}`, {
194
+ isGitDiff: false,
195
+ oldFile: { name: file, contents: patch.before },
196
+ newFile: { name: file, contents: patch.after },
197
+ }) ?? emptyFileDiff(file)
198
+
199
+ value.name = file
200
+
201
+ return value
202
+ }
203
+
204
+ // Pierre's file diffs use jsdiff's default of four context lines; longer unchanged runs end one hunk and start the next.
205
+ const hunkContext = 4
206
+
207
+ function splitHunk(lines: string[], context: number) {
208
+ const rows = lines.reduce<{ line: string; marker?: string }[]>((result, line) => {
209
+ const previous = result.at(-1)
210
+
211
+ // A marker annotates the line before it; completePatch ignores a marker with nothing to annotate.
212
+ if (line.startsWith("\\")) {
213
+ if (previous) previous.marker = line
214
+
215
+ return result
216
+ }
217
+
218
+ result.push({ line })
219
+
220
+ return result
221
+ }, [])
222
+
223
+ const changes = rows.flatMap((row, index) => (row.line.startsWith(" ") ? [] : [index]))
224
+
225
+ const ranges = changes.reduce<{ start: number; end: number }[]>((result, index) => {
226
+ const previous = result.at(-1)
227
+ const start = Math.max(0, index - context)
228
+ const end = Math.min(rows.length - 1, index + context)
229
+
230
+ if (previous && start <= previous.end + 1) {
231
+ previous.end = end
232
+
233
+ return result
234
+ }
235
+
236
+ result.push({ start, end })
237
+
238
+ return result
239
+ }, [])
240
+
241
+ const positions = rows.reduce<{ old: number; new: number }[]>(
242
+ (result, row) => {
243
+ const current = result.at(-1)!
244
+
245
+ result.push({
246
+ old: current.old + (row.line.startsWith("+") ? 0 : 1),
247
+ new: current.new + (row.line.startsWith("-") ? 0 : 1),
248
+ })
249
+
250
+ return result
251
+ },
252
+ [{ old: 0, new: 0 }],
253
+ )
254
+
255
+ return ranges
256
+ .map((range) => {
257
+ const start = positions[range.start]!
258
+ const end = positions[range.end + 1]!
259
+ const oldCount = end.old - start.old
260
+ const newCount = end.new - start.new
261
+
262
+ const body = rows
263
+ .slice(range.start, range.end + 1)
264
+ .map((row) => (row.marker ? `${row.line}\n${row.marker}` : row.line))
265
+ .join("\n")
266
+
267
+ return `@@ -${oldCount === 0 ? start.old : start.old + 1},${oldCount} +${newCount === 0 ? start.new : start.new + 1},${newCount} @@\n${body}`
268
+ })
269
+ .join("\n")
270
+ }
271
+
272
+ // A tool patch carries jsdiff's context around each hunk. Asking for less drops the outer unchanged lines of each hunk
273
+ // and moves its ranges to match; context between two changes stays, as the hunk already joined them.
274
+ function trimContext(patch: string, context: number) {
275
+ if (context >= hunkContext) return patch
276
+ const lines = patch.split("\n")
277
+
278
+ return lines
279
+ .reduce<{ out: string[]; skip: number }>(
280
+ (result, line, index) => {
281
+ if (index < result.skip) return result
282
+ const header = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)$/.exec(line)
283
+
284
+ if (!header) {
285
+ result.out.push(line)
286
+
287
+ return result
288
+ }
289
+
290
+ const end = lines.findIndex((next, at) => at > index && !/^[ +\-\\]/.test(next))
291
+ const body = lines.slice(index + 1, end === -1 ? lines.length : end)
292
+ const first = body.findIndex((row) => row.startsWith("+") || row.startsWith("-"))
293
+ const last = body.findLastIndex((row) => row.startsWith("+") || row.startsWith("-"))
294
+ const lead = first === -1 ? 0 : Math.max(0, first - context)
295
+ // The first lines of trailing context, each with the no-newline marker that may annotate it.
296
+ const tail = body.slice(last + 1)
297
+
298
+ const keepTail = tail.filter(
299
+ (row, at) =>
300
+ tail.slice(0, at + (row.startsWith("\\") ? 0 : 1)).filter((item) => item.startsWith(" ")).length <= context,
301
+ )
302
+
303
+ const dropTail =
304
+ tail.filter((row) => row.startsWith(" ")).length - keepTail.filter((row) => row.startsWith(" ")).length
305
+
306
+ const kept = first === -1 ? body : [...body.slice(lead, last + 1), ...keepTail]
307
+ const shrink = first === -1 ? 0 : lead + dropTail
308
+ const oldStart = Number(header[1]) + (first === -1 ? 0 : lead)
309
+ const newStart = Number(header[3]) + (first === -1 ? 0 : lead)
310
+ const oldCount = Number(header[2] ?? 1) - shrink
311
+ const newCount = Number(header[4] ?? 1) - shrink
312
+
313
+ result.out.push(`@@ -${oldStart},${oldCount} +${newStart},${newCount} @@${header[5]}`, ...kept)
314
+ result.skip = index + 1 + body.length
315
+
316
+ return result
317
+ },
318
+ { out: [], skip: 0 },
319
+ )
320
+ .out.join("\n")
321
+ }
322
+
323
+ function partialFileDiff(file: string, patch: string) {
324
+ const parsed = splitPatch(patch)
325
+
326
+ if (!parsed) return
327
+
328
+ if (/^(?:Index:|diff(?: -r \w+)+|---|\+\+\+)\s/m.test(parsed.header)) return parsePatchFiles(patch)[0]?.files[0]
329
+
330
+ if (!parsed.hunks.length) return
331
+
332
+ return parsePatchFiles(
333
+ `Index: ${file}\n===================================================================\n--- ${file}\t\n+++ ${file}\t\n${patch}`,
334
+ )[0]?.files[0]
335
+ }
336
+
337
+ function fileDiffFromContent(file: string, before: string, after: string) {
338
+ if (!before && !after) return emptyFileDiff(file)
339
+ const key = `${file}\0${before}\0${after}`
340
+
341
+ return cachedFileDiff(`content\0${key}`, () => {
342
+ const value = parseDiffFromFile({ name: file, contents: before }, { name: file, contents: after })
343
+ value.cacheKey = highlightKey(key)
344
+
345
+ return value
346
+ })
347
+ }
348
+
349
+ // Pierre reuses and dedups worker highlighting only for diffs that carry a cacheKey, and it treats diffs with equal
350
+ // keys as the same target. Derive the key from every input that shapes the highlighted output: the file name selects
351
+ // the language and the content selects the lines.
352
+ function highlightKey(value: string) {
353
+ return `${value.length}:${checksum(value) ?? 0}`
354
+ }
355
+
356
+ function emptyFileDiff(file: string) {
357
+ return parseDiffFromFile({ name: file, contents: "" }, { name: file, contents: "" })
358
+ }
@@ -0,0 +1,41 @@
1
+ import { createMemo, Show } from "solid-js"
2
+ import { Card } from "@zyro-sec/ui/card"
3
+ import { Icon } from "@zyro-sec/ui/icon"
4
+ import { LinkedText } from "./linked-text"
5
+
6
+ export function SessionErrorMessage(props: { message: string; links?: boolean }) {
7
+ const content = createMemo(() => {
8
+ // Only a colon before the first URL ends a title, so a scheme or port never splits a URL.
9
+ const url = props.message.search(/https?:\/\//)
10
+ const separator = props.message.slice(0, url === -1 ? undefined : url).indexOf(":")
11
+
12
+ if (separator === -1) return { detail: props.message }
13
+
14
+ return {
15
+ title: props.message.slice(0, separator + 1),
16
+ detail: props.message.slice(separator + 1),
17
+ }
18
+ })
19
+
20
+ return (
21
+ <>
22
+ <Show when={content().title}>{(title) => <strong class="font-[530]">{title()}</strong>}</Show>
23
+ <Show when={props.links} fallback={content().detail}>
24
+ <LinkedText text={content().detail} />
25
+ </Show>
26
+ </>
27
+ )
28
+ }
29
+
30
+ export function SessionError(props: { message: string }) {
31
+ return (
32
+ <Card variant="error" class="error-card" data-kind="session-error-card">
33
+ <div class="flex w-full min-w-0 items-start gap-2">
34
+ <Icon name="outline-hexagonal-warning" class="shrink-0 text-v2-state-fg-danger" />
35
+ <div class="min-w-0">
36
+ <SessionErrorMessage message={props.message} links />
37
+ </div>
38
+ </div>
39
+ </Card>
40
+ )
41
+ }
@@ -0,0 +1,92 @@
1
+ import { createEffect, createMemo, createSignal, on, onCleanup, Show } from "solid-js"
2
+ import type { SessionStatus } from "@zyro-sec/client/promise"
3
+ import { useI18n } from "@zyro-sec/ui/context/i18n"
4
+ import { Card } from "@zyro-sec/ui/card"
5
+ import { Icon } from "@zyro-sec/ui/icon"
6
+ import { TextShimmer } from "@zyro-sec/ui/text-shimmer"
7
+ import { Tooltip } from "@zyro-sec/ui/tooltip"
8
+ import { SessionErrorMessage } from "./session-error"
9
+
10
+ export function SessionRetry(props: { status: SessionStatus; show?: boolean }) {
11
+ const i18n = useI18n()
12
+
13
+ const retry = createMemo(() => {
14
+ if (props.status.type !== "retry") return
15
+
16
+ return props.status
17
+ })
18
+
19
+ const [seconds, setSeconds] = createSignal(0)
20
+ createEffect(
21
+ on(retry, (current) => {
22
+ if (!current) return
23
+
24
+ const update = () => {
25
+ const next = retry()?.next
26
+
27
+ if (!next) return
28
+ setSeconds(Math.round((next - Date.now()) / 1000))
29
+ }
30
+
31
+ update()
32
+ const timer = setInterval(update, 1000)
33
+ onCleanup(() => clearInterval(timer))
34
+ }),
35
+ )
36
+
37
+ const message = createMemo(() => {
38
+ const current = retry()
39
+
40
+ if (!current) return ""
41
+
42
+ if (current.message.includes("exceeded your current quota") && current.message.includes("gemini")) {
43
+ return i18n.t("ui.sessionTurn.retry.geminiHot")
44
+ }
45
+
46
+ if (current.message.length > 80) return current.message.slice(0, 80) + "…"
47
+
48
+ return current.message
49
+ })
50
+
51
+ const info = createMemo(() => {
52
+ const current = retry()
53
+
54
+ if (!current) return ""
55
+ const count = Math.max(0, seconds())
56
+
57
+ if (count > 0) return i18n.plural("ui.sessionTurn.retry.attemptWaiting", count, { attempt: current.attempt })
58
+
59
+ return i18n.t("ui.sessionTurn.retry.attemptRetryingNow", { attempt: current.attempt })
60
+ })
61
+
62
+ return (
63
+ <Show when={retry() && (props.show ?? true)}>
64
+ <div data-slot="session-turn-retry" class="w-full min-w-0">
65
+ <Card variant="error" class="error-card" data-kind="session-retry-card">
66
+ <div class="flex w-full items-start gap-2">
67
+ <Icon name="outline-hexagonal-warning" class="shrink-0 text-v2-state-fg-danger" />
68
+ <div class="min-w-0 flex-1">
69
+ <Tooltip
70
+ appearance="large"
71
+ contentClass="max-w-80 break-words"
72
+ value={retry()?.message ?? ""}
73
+ placement="top"
74
+ >
75
+ <div data-slot="session-turn-retry-message" class="cursor-help truncate">
76
+ <SessionErrorMessage message={message()} />
77
+ </div>
78
+ </Tooltip>
79
+ <Show when={info()}>
80
+ {(line) => (
81
+ <div data-slot="session-turn-retry-info">
82
+ <TextShimmer text={line()} active />
83
+ </div>
84
+ )}
85
+ </Show>
86
+ </div>
87
+ </div>
88
+ </Card>
89
+ </div>
90
+ </Show>
91
+ )
92
+ }