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

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,1156 @@
1
+ import type {
2
+ JsonValue,
3
+ SessionMessageAssistant,
4
+ SessionMessageAssistantTool,
5
+ SessionMessageInfo,
6
+ } from "@zyro-sec/client/promise"
7
+ import { createTwoFilesPatch, diffLines } from "diff"
8
+ import type { SessionDocument } from "../../document"
9
+ import {
10
+ CURRENT_SESSION_ID,
11
+ STORY_MODEL,
12
+ subagentReport,
13
+ type SubagentReport,
14
+ } from "../../storybook/current-session-fixtures"
15
+
16
+ /**
17
+ * A scripted session is a list of beats. Each beat takes one or more steps of the playhead: a tool call starts and then
18
+ * finishes, a thought starts empty, streams, and then finishes. Dragging the playhead replays the session up to that step
19
+ * with the same message shapes the server streams, so the timeline shows its live and finished states.
20
+ */
21
+ export type Beat =
22
+ | { kind: "user"; text: string }
23
+ | { kind: "shell"; command: string; output: string; exit: number }
24
+ | { kind: "goal"; objective: string }
25
+ /** A background subagent or shell reporting back, as the server delivers it. */
26
+ | ({ kind: "report"; source: "subagent" } & SubagentReport)
27
+ | {
28
+ kind: "report"
29
+ source: "shell"
30
+ state: "completed" | "error" | "cancelled"
31
+ description: string
32
+ shellID: string
33
+ }
34
+ | { kind: "model"; id: string }
35
+ | { kind: "step" }
36
+ | { kind: "think"; text: string }
37
+ | { kind: "say"; text: string }
38
+ | {
39
+ kind: "tool"
40
+ name: string
41
+ input: Record<string, JsonValue>
42
+ output?: string
43
+ metadata?: Record<string, JsonValue>
44
+ error?: string
45
+ /** Still running when the next beat arrives, such as a command the user interrupts. */
46
+ hold?: boolean
47
+ }
48
+ | { kind: "interrupt" }
49
+ | { kind: "idle" }
50
+
51
+ const steps: Record<Beat["kind"], number> = {
52
+ user: 1,
53
+ shell: 2,
54
+ goal: 1,
55
+ report: 1,
56
+ model: 1,
57
+ step: 0,
58
+ think: 3,
59
+ say: 2,
60
+ tool: 2,
61
+ interrupt: 1,
62
+ idle: 1,
63
+ }
64
+
65
+ export function ticks(beat: Beat) {
66
+ return beat.kind === "tool" && beat.hold ? 1 : steps[beat.kind]
67
+ }
68
+
69
+ // Files the scripted sessions change, before and after, so their diffs read like edits inside real code.
70
+ const generalBefore = `import { For, Show, createMemo } from "solid-js"
71
+ import { useLanguage } from "@/runtime/i18n/language"
72
+ import { useSettings } from "@/settings/model"
73
+ import { SettingsRow } from "./row"
74
+ import { SettingsSection } from "./section"
75
+ import { FontPicker } from "./font-picker"
76
+ import { LanguagePicker } from "./language-picker"
77
+
78
+ export function GeneralSettings() {
79
+ const language = useLanguage()
80
+ const settings = useSettings()
81
+
82
+ const fonts = createMemo(() => settings.fonts.available())
83
+
84
+ return (
85
+ <SettingsSection title={language.t("settings.general.title")}>
86
+ <SettingsRow
87
+ title={language.t("settings.general.language")}
88
+ description={language.t("settings.general.languageDescription")}
89
+ >
90
+ <LanguagePicker value={settings.general.language} onChange={settings.setLanguage} />
91
+ </SettingsRow>
92
+ <SettingsRow title={language.t("settings.general.font")}>
93
+ <FontPicker fonts={fonts()} value={settings.general.font} onChange={settings.setFont} />
94
+ </SettingsRow>
95
+ <Show when={settings.general.advanced}>
96
+ <SettingsRow title={language.t("settings.general.telemetry")}>
97
+ <For each={settings.telemetry.options}>{(option) => <span>{option.label}</span>}</For>
98
+ </SettingsRow>
99
+ </Show>
100
+ </SettingsSection>
101
+ )
102
+ }
103
+ `
104
+
105
+ const generalAfter = generalBefore
106
+ .replace(
107
+ `import { LanguagePicker } from "./language-picker"\n`,
108
+ `import { LanguagePicker } from "./language-picker"\nimport { ThemeToggle } from "./theme-toggle"\n`,
109
+ )
110
+ .replace(
111
+ ` <SettingsRow title={language.t("settings.general.font")}>
112
+ <FontPicker fonts={fonts()} value={settings.general.font} onChange={settings.setFont} />
113
+ </SettingsRow>
114
+ `,
115
+ ` <SettingsRow title={language.t("settings.general.font")}>
116
+ <FontPicker fonts={fonts()} value={settings.general.font} onChange={settings.setFont} />
117
+ </SettingsRow>
118
+ <SettingsRow
119
+ title={language.t("settings.general.darkMode")}
120
+ description={language.t("settings.general.darkModeDescription")}
121
+ >
122
+ <ThemeToggle />
123
+ </SettingsRow>
124
+ `,
125
+ )
126
+
127
+ const schemeBefore = `import { createSignal } from "solid-js"
128
+ import { persisted } from "@/runtime/persist"
129
+
130
+ export type ColorScheme = "light" | "dark" | "system"
131
+
132
+ const query = window.matchMedia("(prefers-color-scheme: dark)")
133
+
134
+ const [stored, setStored] = persisted<ColorScheme>("theme.scheme", "system")
135
+ const [system, setSystem] = createSignal<"light" | "dark">(query.matches ? "dark" : "light")
136
+
137
+ query.addEventListener("change", (event) => setSystem(event.matches ? "dark" : "light"))
138
+
139
+ /** The scheme the app renders with, after resolving "system" against the OS setting. */
140
+ export function resolvedScheme() {
141
+ const value = stored()
142
+
143
+ return value === "system" ? system() : value
144
+ }
145
+
146
+ export function storedScheme() {
147
+ return stored()
148
+ }
149
+
150
+ export function applyScheme(root: HTMLElement) {
151
+ root.dataset.scheme = resolvedScheme()
152
+ root.style.colorScheme = resolvedScheme()
153
+ }
154
+ `
155
+
156
+ const schemeAfter = schemeBefore
157
+ .replace(
158
+ `export function storedScheme() {
159
+ return stored()
160
+ }
161
+ `,
162
+ `export function storedScheme() {
163
+ return stored()
164
+ }
165
+
166
+ /** Sets the scheme and applies it straight away, so the page never shows the old one. */
167
+ export function setScheme(value: ColorScheme) {
168
+ setStored(value)
169
+ applyScheme(document.documentElement)
170
+ }
171
+ `,
172
+ )
173
+ .replace(
174
+ ` root.dataset.scheme = resolvedScheme()\n root.style.colorScheme = resolvedScheme()\n`,
175
+ ` const scheme = resolvedScheme()\n\n root.dataset.scheme = scheme\n root.style.colorScheme = scheme\n`,
176
+ )
177
+
178
+ const themeIndexBefore = `export { applyScheme, resolvedScheme, storedScheme, type ColorScheme } from "./scheme"
179
+ export { ThemeProvider, useTheme } from "./provider"
180
+ export { tokens } from "./tokens"
181
+ `
182
+
183
+ const themeIndexAfter = themeIndexBefore.replace(
184
+ "applyScheme, resolvedScheme, storedScheme,",
185
+ "applyScheme, resolvedScheme, setScheme, storedScheme,",
186
+ )
187
+
188
+ const toggleFirst = `import { Switch } from "@zyro-sec/ui/switch"
189
+ import { useLanguage } from "@/runtime/i18n/language"
190
+ import { resolvedScheme, setScheme } from "@/theme"
191
+
192
+ /** Switches between the light and dark scheme. Turning it off follows the system again. */
193
+ export function ThemeToggle() {
194
+ const language = useLanguage()
195
+
196
+ return (
197
+ <Switch
198
+ checked={resolvedScheme() === "dark"}
199
+ aria-label={language.t("settings.general.darkMode")}
200
+ onChange={(checked) => setScheme(checked ? "dark" : "system")}
201
+ />
202
+ )
203
+ }
204
+ `
205
+
206
+ const toggleFixed = toggleFirst
207
+ .replace(
208
+ `import { useLanguage } from "@/runtime/i18n/language"\n`,
209
+ `import { createEffect } from "solid-js"\nimport { useLanguage } from "@/runtime/i18n/language"\n`,
210
+ )
211
+ .replace(
212
+ ` const language = useLanguage()\n`,
213
+ ` const language = useLanguage()\n\n // The store subscribes after mount, so read the scheme in an effect rather than once while rendering.\n createEffect(() => resolvedScheme())\n`,
214
+ )
215
+
216
+ const docsBefore = `# Settings
217
+
218
+ Open Settings from the app menu, or press Cmd+, on macOS and Ctrl+, elsewhere.
219
+
220
+ ## General
221
+
222
+ ### Language
223
+
224
+ Choose the language the app uses. It follows your system language until you pick one.
225
+
226
+ ### Font
227
+
228
+ Choose the font for the timeline and the editor. Monospaced fonts apply to code only.
229
+
230
+ ## Keyboard shortcuts
231
+
232
+ Every command lists its shortcut. Click a shortcut to change it.
233
+ `
234
+
235
+ const docsAfter = docsBefore.replace(
236
+ `### Font
237
+
238
+ Choose the font for the timeline and the editor. Monospaced fonts apply to code only.
239
+ `,
240
+ `### Font
241
+
242
+ Choose the font for the timeline and the editor. Monospaced fonts apply to code only.
243
+
244
+ ### Dark mode
245
+
246
+ Turn on **Dark mode** to use the dark color scheme everywhere in the app.
247
+
248
+ - Turning it off follows your system setting again.
249
+ - The setting applies straight away, without a restart.
250
+ - It is stored on this device, so each computer keeps its own choice.
251
+
252
+ The terminal and code views follow the same scheme. To give code a different theme,
253
+ choose one under **Settings → Editor → Theme**.
254
+ `,
255
+ )
256
+
257
+ const changelogBefore = `# Changelog
258
+
259
+ ## Unreleased
260
+
261
+ ### Added
262
+
263
+ - Search across every open session from the command palette.
264
+
265
+ ### Fixed
266
+
267
+ - The timeline keeps its scroll position when a long diff loads.
268
+
269
+ ## 1.14.0
270
+
271
+ ### Added
272
+
273
+ - Pin sessions to the top of the sidebar.
274
+ `
275
+
276
+ const changelogAfter = changelogBefore.replace(
277
+ `- Search across every open session from the command palette.\n`,
278
+ `- Search across every open session from the command palette.\n- A **Dark mode** toggle in Settings → General.\n`,
279
+ )
280
+
281
+ /** A file change as the server records it: a jsdiff patch with its line counts, like core's `fileDiff`. */
282
+ function change(file: string, before: string, after: string, status: "modified" | "added" = "modified") {
283
+ const counts = diffLines(before, after).reduce(
284
+ (result, item) => ({
285
+ additions: result.additions + (item.added ? (item.count ?? 0) : 0),
286
+ deletions: result.deletions + (item.removed ? (item.count ?? 0) : 0),
287
+ }),
288
+ { additions: 0, deletions: 0 },
289
+ )
290
+
291
+ return { file, status, ...counts, patch: createTwoFilesPatch(file, file, before, after) }
292
+ }
293
+
294
+ const settingsFiles = [
295
+ "src/settings/general.tsx",
296
+ "src/settings/editor.tsx",
297
+ "src/settings/keyboard.tsx",
298
+ "src/settings/row.tsx",
299
+ "src/settings/section.tsx",
300
+ "src/settings/font-picker.tsx",
301
+ ].join("\n")
302
+
303
+ const colorSchemeMatches = [
304
+ "src/theme/provider.tsx:12: const scheme = colorScheme()",
305
+ "src/theme/provider.tsx:18: root.style.colorScheme = scheme",
306
+ 'src/app.tsx:40: <meta name="color-scheme" content={colorScheme()} />',
307
+ "src/settings/general.tsx:31: // colorScheme lives in the theme store",
308
+ ].join("\n")
309
+
310
+ const themeListing = ["index.ts", "provider.tsx", "scheme.ts", "scheme.test.ts", "tokens.ts"].join("\n")
311
+
312
+ const schemeSearchResults = [
313
+ "Title: prefers-color-scheme - CSS | MDN",
314
+ "URL: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme",
315
+ "",
316
+ "Title: Building a theme switch component | web.dev",
317
+ "URL: https://web.dev/articles/building/a-theme-switch-component",
318
+ "",
319
+ "Title: The best light/dark mode theme toggle in JavaScript",
320
+ "URL: https://whitep4nth3r.com/blog/best-light-dark-mode-theme-toggle-javascript/",
321
+ ].join("\n")
322
+
323
+ const safariSearchResults = [
324
+ "Title: Dark Mode Support in WebKit",
325
+ "URL: https://webkit.org/blog/8840/dark-mode-support-in-webkit/",
326
+ "",
327
+ "Title: color-scheme - CSS | MDN",
328
+ "URL: https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme",
329
+ ].join("\n")
330
+
331
+ const mdnPage =
332
+ "# prefers-color-scheme\n\nThe prefers-color-scheme CSS media feature detects whether a user has asked for a light or dark color theme."
333
+
334
+ const featureWork: Beat[] = [
335
+ { kind: "user", text: "Add a dark mode toggle to the settings page and make sure the tests pass." },
336
+ {
337
+ kind: "think",
338
+ text: "## Finding the settings page\n\nI need to find where the settings rows render and how the theme is stored before adding a toggle.",
339
+ },
340
+ {
341
+ kind: "tool",
342
+ name: "glob",
343
+ input: { pattern: "src/settings/**/*.tsx", path: "/workspace" },
344
+ output: settingsFiles,
345
+ metadata: { count: 6 },
346
+ },
347
+ {
348
+ kind: "tool",
349
+ name: "grep",
350
+ input: { pattern: "colorScheme", path: "/workspace/src", include: "*.tsx" },
351
+ output: colorSchemeMatches,
352
+ metadata: { matches: 4 },
353
+ },
354
+ { kind: "tool", name: "read", input: { path: "/workspace/src/settings/general.tsx" }, output: generalBefore },
355
+ { kind: "tool", name: "read", input: { path: "/workspace/src/theme/scheme.ts" }, output: schemeBefore },
356
+ { kind: "step" },
357
+ {
358
+ kind: "say",
359
+ text: "The settings page renders its rows from `general.tsx`, and the theme keeps the scheme in `scheme.ts`. I'll add a toggle row that writes the scheme.",
360
+ },
361
+ {
362
+ kind: "think",
363
+ text: "## Adding the toggle\n\nA new component keeps the row small. The scheme store needs a setter the row can call.",
364
+ },
365
+ {
366
+ kind: "tool",
367
+ name: "write",
368
+ input: { path: "/workspace/src/settings/theme-toggle.tsx", content: toggleFirst },
369
+ metadata: { files: [change("/workspace/src/settings/theme-toggle.tsx", "", toggleFirst, "added")] },
370
+ },
371
+ {
372
+ kind: "tool",
373
+ name: "edit",
374
+ input: { path: "/workspace/src/settings/general.tsx", oldString: "before", newString: "after" },
375
+ metadata: { files: [change("/workspace/src/settings/general.tsx", generalBefore, generalAfter)] },
376
+ },
377
+ {
378
+ kind: "tool",
379
+ name: "patch",
380
+ input: { patchText: "*** Begin Patch" },
381
+ metadata: {
382
+ files: [
383
+ change("/workspace/src/theme/scheme.ts", schemeBefore, schemeAfter),
384
+ change("/workspace/src/theme/index.ts", themeIndexBefore, themeIndexAfter),
385
+ ],
386
+ },
387
+ },
388
+ { kind: "step" },
389
+ {
390
+ kind: "tool",
391
+ name: "shell",
392
+ input: { command: "bun test src/settings", description: "Run the settings tests" },
393
+ output: 'settings.test.tsx:\n✗ toggles the scheme\n Expected: "dark"\n Received: "light"\n\n1 fail, 11 pass',
394
+ metadata: { exit: 1 },
395
+ },
396
+ {
397
+ kind: "think",
398
+ text: "The toggle writes the scheme before the store subscribes. Reading it in an effect fixes it.",
399
+ },
400
+ {
401
+ kind: "tool",
402
+ name: "edit",
403
+ input: { path: "/workspace/src/settings/theme-toggle.tsx", oldString: "before", newString: "after" },
404
+ metadata: { files: [change("/workspace/src/settings/theme-toggle.tsx", toggleFirst, toggleFixed)] },
405
+ },
406
+ {
407
+ kind: "tool",
408
+ name: "shell",
409
+ input: { command: "bun test src/settings", description: "Run the settings tests" },
410
+ output: "12 pass, 0 fail",
411
+ metadata: { exit: 0 },
412
+ },
413
+ { kind: "step" },
414
+ {
415
+ kind: "say",
416
+ text: "Added a **Dark mode** toggle to General settings. It writes the color scheme through the theme store, and the settings tests pass.",
417
+ },
418
+ { kind: "idle" },
419
+ {
420
+ kind: "shell",
421
+ command: "git status --short",
422
+ output: " M src/settings/general.tsx\n M src/theme/scheme.ts\n?? src/settings/theme-toggle.tsx",
423
+ exit: 0,
424
+ },
425
+ { kind: "goal", objective: "Ship dark mode with docs and a changelog entry" },
426
+ {
427
+ kind: "think",
428
+ text: "## Checking conventions\n\nI should match how other apps document a color scheme preference.",
429
+ },
430
+ {
431
+ kind: "tool",
432
+ name: "websearch",
433
+ input: { query: "prefers-color-scheme settings toggle best practices" },
434
+ output: schemeSearchResults,
435
+ },
436
+ {
437
+ kind: "tool",
438
+ name: "webfetch",
439
+ input: { url: "https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme" },
440
+ output: mdnPage,
441
+ },
442
+ { kind: "tool", name: "skill", input: { name: "docs-style" }, metadata: { name: "docs-style" } },
443
+ { kind: "step" },
444
+ {
445
+ kind: "tool",
446
+ name: "write",
447
+ input: { path: "/workspace/docs/settings.md", content: docsAfter },
448
+ metadata: { files: [change("/workspace/docs/settings.md", docsBefore, docsAfter)] },
449
+ },
450
+ {
451
+ kind: "tool",
452
+ name: "execute",
453
+ input: {
454
+ code: 'const issue = await tools.linear.create_issue({ title: "Announce dark mode", team: "Desktop" })\nawait tools.slack.post_message({ channel: "#desktop", text: issue.url })\nreturn issue.id',
455
+ },
456
+ output: "DES-412",
457
+ metadata: {
458
+ toolCalls: [
459
+ { tool: "search", status: "completed", input: { query: "linear" } },
460
+ { tool: "linear.create_issue", status: "completed", input: { title: "Announce dark mode", team: "Desktop" } },
461
+ { tool: "slack.post_message", status: "completed", input: { channel: "#desktop" } },
462
+ ],
463
+ },
464
+ },
465
+ {
466
+ kind: "tool",
467
+ name: "subagent",
468
+ input: { agent: "review", description: "Review the dark mode change", prompt: "Review the diff." },
469
+ output: "The subagent is working in the background.",
470
+ metadata: { sessionID: "session_child_review", status: "running" },
471
+ },
472
+ {
473
+ kind: "report",
474
+ source: "subagent",
475
+ state: "completed",
476
+ description: "Review the dark mode change",
477
+ agent: "review",
478
+ childID: "session_child_review",
479
+ body: "Looks good. One nit: name the store setter `setScheme`.",
480
+ },
481
+ { kind: "tool", name: "shell", input: { command: "bun run build", description: "Build the app" }, hold: true },
482
+ { kind: "interrupt" },
483
+ { kind: "model", id: "fast-nano" },
484
+ { kind: "user", text: "Skip the build, just add the changelog entry." },
485
+ {
486
+ kind: "think",
487
+ text: "## Writing the changelog\n\nThe entry goes under the next release. I'll keep it to one line that names the setting.",
488
+ },
489
+ {
490
+ kind: "tool",
491
+ name: "edit",
492
+ input: { path: "/workspace/CHANGELOG.md", oldString: "before", newString: "after" },
493
+ metadata: { files: [change("/workspace/CHANGELOG.md", changelogBefore, changelogAfter)] },
494
+ },
495
+ { kind: "step" },
496
+ { kind: "say", text: "Added the changelog entry under the next release." },
497
+ { kind: "idle" },
498
+ ]
499
+
500
+ // Every tool the timeline draws, finished and failed, so each row and its expanded view can be checked in one place.
501
+ const everyTool: Beat[] = [
502
+ { kind: "user", text: "Audit how the app handles the color scheme, and file anything broken." },
503
+ { kind: "tool", name: "list", input: { path: "/workspace/src/theme" }, output: themeListing },
504
+ {
505
+ kind: "tool",
506
+ name: "glob",
507
+ input: { pattern: "src/**/*.{test,spec}.tsx", path: "/workspace" },
508
+ output: "src/settings/settings.test.tsx\nsrc/theme/scheme.test.ts",
509
+ metadata: { count: 2 },
510
+ },
511
+ {
512
+ kind: "tool",
513
+ name: "glob",
514
+ input: { pattern: "src/**/[*.tsx", path: "/workspace" },
515
+ error: "Invalid glob pattern: unclosed character class in src/**/[*.tsx",
516
+ },
517
+ {
518
+ kind: "tool",
519
+ name: "grep",
520
+ input: { pattern: "matchMedia", path: "/workspace/src", include: "*.ts" },
521
+ output: 'src/theme/scheme.ts:6: const query = window.matchMedia("(prefers-color-scheme: dark)")',
522
+ metadata: { matches: 1 },
523
+ },
524
+ { kind: "tool", name: "read", input: { path: "/workspace/src/theme/scheme.ts" }, output: schemeBefore },
525
+ {
526
+ kind: "tool",
527
+ name: "read",
528
+ input: { path: "/workspace/src/theme/provider.tsx" },
529
+ output: "export function ThemeProvider() {}",
530
+ },
531
+ { kind: "tool", name: "read", input: { path: "/workspace/src/theme/tokens.ts" }, output: "export const tokens = {}" },
532
+ {
533
+ kind: "tool",
534
+ name: "read",
535
+ input: { path: "/workspace/src/theme/legacy.ts" },
536
+ error: "File not found: /workspace/src/theme/legacy.ts",
537
+ },
538
+ { kind: "step" },
539
+ {
540
+ kind: "tool",
541
+ name: "websearch",
542
+ input: { query: "safari color-scheme meta tag flash of light theme" },
543
+ output: safariSearchResults,
544
+ },
545
+ {
546
+ kind: "tool",
547
+ name: "websearch",
548
+ input: { query: '"applyScheme" opencode theme flash' },
549
+ output: "No results found.",
550
+ },
551
+ {
552
+ kind: "tool",
553
+ name: "webfetch",
554
+ input: { url: "https://webkit.org/blog/8840/dark-mode-support-in-webkit/" },
555
+ output: "# Dark Mode Support in WebKit\n\nThe color-scheme meta tag tells WebKit which schemes a page supports.",
556
+ },
557
+ {
558
+ kind: "tool",
559
+ name: "webfetch",
560
+ input: { url: "https://internal.example.com/design/theme-spec" },
561
+ error: "Request failed with status 403 Forbidden",
562
+ },
563
+ { kind: "tool", name: "skill", input: { name: "bug-report" }, metadata: { name: "bug-report" } },
564
+ { kind: "step" },
565
+ {
566
+ kind: "say",
567
+ text: "The scheme applies after the first paint, so Safari flashes the light theme on load. I'll file it and check the fix with a test.",
568
+ },
569
+ {
570
+ kind: "tool",
571
+ name: "linear_create_issue",
572
+ input: { title: "Light theme flashes on load in Safari", team: "Desktop" },
573
+ output: "Created DES-418: https://linear.app/opencode/issue/DES-418",
574
+ },
575
+ {
576
+ kind: "tool",
577
+ name: "slack_post_message",
578
+ input: { channel: "#desktop", text: "Filed DES-418" },
579
+ error: "MCP server slack is not connected",
580
+ },
581
+ {
582
+ kind: "tool",
583
+ name: "execute",
584
+ input: {
585
+ code: 'const issue = await tools.linear.get_issue({ id: "DES-418" })\nawait tools.linear.add_label({ id: issue.id, label: "regression" })\nreturn issue.url',
586
+ },
587
+ output: "linear.add_label: label regression does not exist",
588
+ metadata: {
589
+ error: true,
590
+ toolCalls: [
591
+ { tool: "linear.get_issue", status: "completed", input: { id: "DES-418" } },
592
+ { tool: "linear.add_label", status: "error", input: { id: "DES-418", label: "regression" } },
593
+ ],
594
+ },
595
+ },
596
+ { kind: "step" },
597
+ {
598
+ kind: "tool",
599
+ name: "edit",
600
+ input: { path: "/workspace/src/theme/scheme.ts", oldString: "missing", newString: "after" },
601
+ error: "oldString not found in /workspace/src/theme/scheme.ts",
602
+ },
603
+ {
604
+ kind: "tool",
605
+ name: "edit",
606
+ input: { path: "/workspace/src/theme/scheme.ts", oldString: "before", newString: "after" },
607
+ metadata: { files: [change("/workspace/src/theme/scheme.ts", schemeBefore, schemeAfter)] },
608
+ },
609
+ {
610
+ kind: "tool",
611
+ name: "shell",
612
+ input: { command: "bun test src/theme", description: "Run the theme tests" },
613
+ output:
614
+ "scheme.test.ts:\n✓ resolves the system scheme\n✓ applies the stored scheme\n✓ sets the scheme straight away\n✓ keeps the scheme after a reload\n\n4 pass, 0 fail",
615
+ metadata: { exit: 0 },
616
+ },
617
+ {
618
+ kind: "tool",
619
+ name: "shell",
620
+ input: { command: "bun run lint src/theme", description: "Lint the theme" },
621
+ output:
622
+ "src/theme/scheme.ts\n 14:3 error Unexpected console statement no-console\n\n1 problem (1 error, 0 warnings)",
623
+ metadata: { exit: 1 },
624
+ },
625
+ {
626
+ kind: "tool",
627
+ name: "subagent",
628
+ input: { agent: "review", description: "Review the scheme fix", prompt: "Review the diff." },
629
+ output: "The subagent is working in the background.",
630
+ metadata: { sessionID: "session_child_review_failed", status: "running" },
631
+ },
632
+ {
633
+ kind: "report",
634
+ source: "subagent",
635
+ state: "error",
636
+ description: "Review the scheme fix",
637
+ agent: "review",
638
+ childID: "session_child_review_failed",
639
+ body: "The model is overloaded.",
640
+ },
641
+ {
642
+ kind: "say",
643
+ text: "Filed **DES-418** and fixed the flash: the scheme now applies before the first paint. Tests pass; lint flags one stray `console` call.",
644
+ },
645
+ { kind: "idle" },
646
+ ]
647
+
648
+ // Subagents and shells the agent starts in the background, and how they report back.
649
+ const backgroundWork: Beat[] = [
650
+ { kind: "user", text: "Review the theme code and check the docs while you keep the dev server running." },
651
+ {
652
+ kind: "tool",
653
+ name: "subagent",
654
+ input: { agent: "review", description: "Review the theme store", prompt: "Review it." },
655
+ output: "The subagent is working in the background.",
656
+ metadata: { sessionID: "session_child_store", status: "running" },
657
+ },
658
+ {
659
+ kind: "tool",
660
+ name: "subagent",
661
+ input: { agent: "explore", description: "Check the theme docs", prompt: "Check them." },
662
+ output: "The subagent is working in the background.",
663
+ metadata: { sessionID: "session_child_docs", status: "running" },
664
+ },
665
+ {
666
+ kind: "tool",
667
+ name: "subagent",
668
+ input: {
669
+ sessionID: "session_child_docs",
670
+ agent: "explore",
671
+ description: "Check the migration guide",
672
+ prompt: "Include the migration guide in your review.",
673
+ },
674
+ metadata: { sessionID: "session_child_docs", status: "running" },
675
+ },
676
+ {
677
+ kind: "tool",
678
+ name: "shell",
679
+ input: { command: "bun run dev", description: "Start the dev server", background: true },
680
+ output: "Command moved to the background (shell ID: shell_dev).",
681
+ metadata: { shellID: "shell_dev", status: "running" },
682
+ },
683
+ { kind: "tool", name: "read", input: { path: "/workspace/src/theme/scheme.ts" }, output: schemeBefore },
684
+ { kind: "step" },
685
+ {
686
+ kind: "say",
687
+ text: "Two reviews are running in the background, and the dev server is up. I'll pick up their findings when they report back.",
688
+ },
689
+ { kind: "idle" },
690
+ {
691
+ kind: "report",
692
+ source: "subagent",
693
+ state: "completed",
694
+ description: "Review the theme store",
695
+ agent: "review",
696
+ childID: "session_child_store",
697
+ body: "Nothing to change in the theme store.",
698
+ },
699
+ {
700
+ kind: "report",
701
+ source: "subagent",
702
+ state: "error",
703
+ description: "Check the theme docs",
704
+ agent: "explore",
705
+ childID: "session_child_docs",
706
+ body: "The docs check failed.",
707
+ },
708
+ {
709
+ kind: "say",
710
+ text: "The store review found nothing to change. I'll retry the docs check and ask for a review of the toggle.",
711
+ },
712
+ {
713
+ kind: "tool",
714
+ name: "subagent",
715
+ input: { agent: "explore", description: "Check the theme docs", prompt: "Check them." },
716
+ output: "The subagent is working in the background.",
717
+ metadata: { sessionID: "session_child_docs_again", status: "running" },
718
+ },
719
+ {
720
+ kind: "tool",
721
+ name: "subagent",
722
+ input: { agent: "review", description: "Review the toggle", prompt: "Review it." },
723
+ output: "The subagent is working in the background.",
724
+ metadata: { sessionID: "session_child_toggle", status: "running" },
725
+ },
726
+ { kind: "idle" },
727
+ {
728
+ kind: "report",
729
+ source: "subagent",
730
+ state: "completed",
731
+ description: "Check the theme docs",
732
+ agent: "explore",
733
+ childID: "session_child_docs_again",
734
+ body: "The docs describe the scheme setting correctly.",
735
+ },
736
+ {
737
+ kind: "report",
738
+ source: "subagent",
739
+ state: "cancelled",
740
+ description: "Review the toggle",
741
+ agent: "review",
742
+ childID: "session_child_toggle",
743
+ body: "The review was stopped.",
744
+ },
745
+ { kind: "say", text: "The docs are accurate. The toggle review was stopped before it finished." },
746
+ { kind: "idle" },
747
+ { kind: "report", source: "shell", state: "cancelled", description: "bun run dev", shellID: "shell_dev" },
748
+ ]
749
+
750
+ const thinkingOnly: Beat[] = [
751
+ { kind: "user", text: "Why does the timeline jump when a turn finishes?" },
752
+ { kind: "think", text: "" },
753
+ {
754
+ kind: "think",
755
+ text: "## Tracing row identity\n\nRows are keyed by group type, so a group that changes type remounts and loses its measured height.",
756
+ },
757
+ { kind: "step" },
758
+ {
759
+ kind: "say",
760
+ text: "Rows are keyed by their group type. When a tool fails, its group changes type and the row remounts, which loses its measured height.",
761
+ },
762
+ { kind: "idle" },
763
+ ]
764
+
765
+ // The model thinks after reading, in the same stretch, so the old timeline labels the group by the reads it made.
766
+ const thinkingAfterTools: Beat[] = [
767
+ { kind: "user", text: "Is the scheme store safe to read during render?" },
768
+ { kind: "tool", name: "read", input: { path: "/workspace/src/theme/scheme.ts" } },
769
+ {
770
+ kind: "tool",
771
+ name: "grep",
772
+ input: { pattern: "useScheme", path: "/workspace/src", include: "*.tsx" },
773
+ metadata: { matches: 7 },
774
+ },
775
+ {
776
+ kind: "think",
777
+ text: "## Checking reads during render\n\nThe store reads a signal, so a read in render subscribes the component. None of the seven callers write during render.",
778
+ },
779
+ { kind: "step" },
780
+ {
781
+ kind: "say",
782
+ text: "Yes. The store only reads a signal during render, and none of its seven callers write to it there.",
783
+ },
784
+ { kind: "idle" },
785
+ ]
786
+
787
+ // Some providers send thinking with no readable text, so the thought never has words to show.
788
+ const encryptedThinking: Beat[] = [
789
+ { kind: "user", text: "Plan the dark mode change before touching any code." },
790
+ { kind: "think", text: "" },
791
+ { kind: "think", text: "" },
792
+ { kind: "step" },
793
+ { kind: "say", text: "Plan: add a scheme setter to the theme store, a toggle row in General settings, then a test." },
794
+ { kind: "idle" },
795
+ ]
796
+
797
+ const subagentBetweenTools: Beat[] = [
798
+ { kind: "user", text: "Check the theme store, ask for a review, then run its tests." },
799
+ { kind: "tool", name: "read", input: { path: "/workspace/src/theme/scheme.ts" }, output: schemeBefore },
800
+ {
801
+ kind: "tool",
802
+ name: "subagent",
803
+ input: { agent: "review", description: "Review the theme store", prompt: "Review it." },
804
+ metadata: { sessionID: "session_child_store", status: "running" },
805
+ },
806
+ {
807
+ kind: "tool",
808
+ name: "shell",
809
+ input: { command: "bun test src/theme", description: "Run the theme tests" },
810
+ output: "4 pass, 0 fail",
811
+ },
812
+ { kind: "say", text: "The theme tests pass. The store review is running." },
813
+ { kind: "idle" },
814
+ ]
815
+
816
+ export const scenarios = {
817
+ "Feature work": featureWork,
818
+ "Every tool": everyTool,
819
+ "Background work": backgroundWork,
820
+ "Subagent between tools": subagentBetweenTools,
821
+ "Thinking only": thinkingOnly,
822
+ "Thinking after tools": thinkingAfterTools,
823
+ "Encrypted thinking": encryptedThinking,
824
+ } as const
825
+
826
+ export type ScenarioName = keyof typeof scenarios
827
+
828
+ export function replayScript(script: Beat[], limit: number, start: number) {
829
+ const messages: SessionMessageInfo[] = []
830
+ // SAFETY: no step is open yet; the assertion names the step this holds once one starts.
831
+ const state = { used: 0, busy: false, caption: "Start", assistant: undefined as SessionMessageAssistant | undefined }
832
+ const at = () => start + state.used * 1_500
833
+ const id = (prefix: string) => `${prefix}_${messages.length}_${state.used}`
834
+
835
+ const closeStep = () => {
836
+ if (state.assistant) state.assistant.time.completed = at()
837
+ state.assistant = undefined
838
+ }
839
+
840
+ const assistant = () => {
841
+ if (state.assistant) return state.assistant
842
+
843
+ const message: SessionMessageAssistant = {
844
+ id: id("msg_assistant"),
845
+ type: "assistant",
846
+ agent: "build",
847
+ model: STORY_MODEL,
848
+ content: [],
849
+ time: { created: at() },
850
+ }
851
+
852
+ messages.push(message)
853
+ state.assistant = message
854
+
855
+ return message
856
+ }
857
+
858
+ for (const beat of script) {
859
+ const available = limit - state.used
860
+
861
+ if (available <= 0) break
862
+ const progress = Math.min(available, ticks(beat))
863
+ const done = progress === ticks(beat) && !(beat.kind === "tool" && beat.hold)
864
+ state.caption = caption(beat, done)
865
+
866
+ if (beat.kind === "user") {
867
+ closeStep()
868
+ messages.push({
869
+ id: id("msg_user"),
870
+ type: "user",
871
+ text: beat.text,
872
+ time: { created: at() },
873
+ metadata: { agent: "build", model: STORY_MODEL },
874
+ })
875
+ state.busy = true
876
+ }
877
+
878
+ if (beat.kind === "shell") {
879
+ closeStep()
880
+ messages.push({
881
+ id: id("msg_shell"),
882
+ type: "shell",
883
+ shellID: id("shell"),
884
+ command: beat.command,
885
+ status: done ? "exited" : "running",
886
+ exit: done ? beat.exit : undefined,
887
+ output: done
888
+ ? { output: beat.output, cursor: beat.output.length, size: beat.output.length, truncated: false }
889
+ : undefined,
890
+ time: { created: at(), completed: done ? at() + 800 : undefined },
891
+ })
892
+ }
893
+
894
+ if (beat.kind === "goal") {
895
+ closeStep()
896
+ messages.push({
897
+ id: id("msg_goal"),
898
+ type: "synthetic",
899
+ text: `Keep working until this objective is met: ${beat.objective}`,
900
+ description: `Goal: ${beat.objective}`,
901
+ metadata: { source: "goal" },
902
+ time: { created: at() },
903
+ })
904
+ state.busy = true
905
+ }
906
+
907
+ if (beat.kind === "report" && beat.source === "subagent") {
908
+ closeStep()
909
+ messages.push(subagentReport(id("msg_report"), at(), beat))
910
+ state.busy = true
911
+ }
912
+
913
+ if (beat.kind === "report" && beat.source === "shell") {
914
+ closeStep()
915
+ messages.push({
916
+ id: id("msg_report"),
917
+ type: "synthetic",
918
+ text: `${beat.source} ${beat.state}: ${beat.description}`,
919
+ description: beat.description,
920
+ metadata: { source: beat.source, state: beat.state, shellID: beat.shellID },
921
+ time: { created: at() },
922
+ })
923
+ }
924
+
925
+ if (beat.kind === "model") {
926
+ closeStep()
927
+ messages.push({
928
+ id: id("msg_model"),
929
+ type: "model-switched",
930
+ model: { id: beat.id, providerID: "company-gateway" },
931
+ previous: STORY_MODEL,
932
+ time: { created: at() },
933
+ })
934
+ }
935
+
936
+ if (beat.kind === "step") closeStep()
937
+
938
+ if (beat.kind === "think")
939
+ assistant().content.push({
940
+ type: "reasoning",
941
+ // An empty thought shows that the model is thinking before its first words arrive.
942
+ text: thoughtSoFar(beat.text, progress),
943
+ time: { created: at(), completed: done ? at() + 1_000 : undefined },
944
+ })
945
+
946
+ if (beat.kind === "say")
947
+ assistant().content.push({ type: "text", text: done ? beat.text : beat.text.slice(0, beat.text.length / 2) })
948
+
949
+ if (beat.kind === "tool") assistant().content.push(tool(id("tool"), beat, done, at()))
950
+
951
+ if (beat.kind === "interrupt" && state.assistant) {
952
+ state.assistant.content = state.assistant.content.map((content) =>
953
+ content.type === "tool" && content.state.status === "running"
954
+ ? tool(
955
+ content.id,
956
+ { kind: "tool", name: content.name, input: content.state.input, error: "Tool execution interrupted" },
957
+ true,
958
+ at(),
959
+ )
960
+ : content,
961
+ )
962
+ state.assistant.error = { type: "aborted", message: "The user interrupted the turn" }
963
+ closeStep()
964
+ messages.push({ id: id("msg_idle"), type: "idle", outcome: "interrupted", time: { created: at() } })
965
+ state.busy = false
966
+ }
967
+
968
+ if (beat.kind === "idle") {
969
+ closeStep()
970
+ messages.push({ id: id("msg_idle"), type: "idle", outcome: "succeeded", time: { created: at() } })
971
+ state.busy = false
972
+ }
973
+
974
+ state.used += progress
975
+ }
976
+
977
+ return {
978
+ document: {
979
+ sessionID: CURRENT_SESSION_ID,
980
+ // New arrays and objects on every step, as a streamed update would deliver them.
981
+ messages: structuredClone(messages),
982
+ status: { type: state.busy ? "busy" : "idle" },
983
+ diffs: [],
984
+ } satisfies SessionDocument,
985
+ caption: state.caption,
986
+ }
987
+ }
988
+
989
+ function tool(
990
+ id: string,
991
+ beat: Extract<Beat, { kind: "tool" }>,
992
+ done: boolean,
993
+ time: number,
994
+ ): SessionMessageAssistantTool {
995
+ if (!done)
996
+ return {
997
+ type: "tool",
998
+ id,
999
+ name: beat.name,
1000
+ state: { status: "running", input: beat.input, metadata: running(beat.metadata ?? {}) },
1001
+ time: { created: time, ran: time + 100 },
1002
+ }
1003
+
1004
+ if (beat.error)
1005
+ return {
1006
+ type: "tool",
1007
+ id,
1008
+ name: beat.name,
1009
+ state: {
1010
+ status: "error",
1011
+ input: beat.input,
1012
+ error: {
1013
+ type: beat.error === "Tool execution interrupted" ? "aborted" : "ToolExecutionError",
1014
+ message: beat.error,
1015
+ },
1016
+ metadata: beat.metadata,
1017
+ },
1018
+ time: { created: time, ran: time + 100, completed: time + 700 },
1019
+ }
1020
+
1021
+ return {
1022
+ type: "tool",
1023
+ id,
1024
+ name: beat.name,
1025
+ state: {
1026
+ status: "completed",
1027
+ input: beat.input,
1028
+ content: [{ type: "text", text: beat.output ?? "Done" }],
1029
+ metadata: beat.metadata,
1030
+ },
1031
+ time: { created: time, ran: time + 100, completed: time + 900 },
1032
+ }
1033
+ }
1034
+
1035
+ // How much of a thought shows at each step: nothing yet, then half, then all of it.
1036
+ function thoughtSoFar(text: string, progress: number) {
1037
+ if (progress === 1) return ""
1038
+
1039
+ if (progress === 2) return text.slice(0, Math.ceil(text.length / 2))
1040
+
1041
+ return text
1042
+ }
1043
+
1044
+ // A running Code Mode script is still inside its last call.
1045
+ function running(metadata: Record<string, JsonValue>) {
1046
+ const calls = metadata.toolCalls
1047
+
1048
+ if (!Array.isArray(calls) || calls.length === 0) return metadata
1049
+
1050
+ // SAFETY: Code Mode records each call as an object; the spread only copies its fields.
1051
+ return { ...metadata, toolCalls: [...calls.slice(0, -1), { ...(calls.at(-1) as object), status: "running" }] }
1052
+ }
1053
+
1054
+ function caption(beat: Beat, done: boolean) {
1055
+ if (beat.kind === "user") return "You sent a message"
1056
+
1057
+ if (beat.kind === "shell") return done ? `You ran ${beat.command}` : `Running ${beat.command}`
1058
+
1059
+ if (beat.kind === "goal") return "You set a goal"
1060
+
1061
+ if (beat.kind === "report") return `${beat.source} ${beat.state}`
1062
+
1063
+ if (beat.kind === "model") return "You switched models"
1064
+
1065
+ if (beat.kind === "think") return done ? "Finished thinking" : "Thinking"
1066
+
1067
+ if (beat.kind === "say") return done ? "Answered" : "Writing"
1068
+
1069
+ if (beat.kind === "tool") return done ? `${beat.name} finished` : `${beat.name} running`
1070
+
1071
+ if (beat.kind === "interrupt") return "You interrupted the turn"
1072
+
1073
+ if (beat.kind === "idle") return "Turn finished"
1074
+
1075
+ return "Next step"
1076
+ }
1077
+
1078
+ /** The playhead's steps through an exported session: one per message, and one per part of an assistant message. */
1079
+ export function sessionSteps(messages: readonly SessionMessageInfo[]) {
1080
+ return messages.reduce(
1081
+ (sum, message) => sum + (message.type === "assistant" ? Math.max(1, message.content.length) : 1),
1082
+ 0,
1083
+ )
1084
+ }
1085
+
1086
+ /**
1087
+ * An exported session up to a step of the playhead. The step's message is live: its assistant message is not finished,
1088
+ * the part the step lands on is a thought still streaming or a call still running, and the session is busy.
1089
+ */
1090
+ export function replaySession(sessionID: string, messages: readonly SessionMessageInfo[], limit: number) {
1091
+ const out: SessionMessageInfo[] = []
1092
+ const state = { used: 0, caption: "Start", live: false }
1093
+
1094
+ for (const message of messages) {
1095
+ const available = limit - state.used
1096
+
1097
+ if (available <= 0) break
1098
+
1099
+ if (message.type !== "assistant") {
1100
+ out.push(message)
1101
+ state.used += 1
1102
+ state.caption = message.type
1103
+ state.live = message.type === "user" || message.type === "shell"
1104
+ continue
1105
+ }
1106
+
1107
+ const length = Math.max(1, message.content.length)
1108
+ const shown = Math.min(available, length)
1109
+ const finished = shown === length && available > length
1110
+
1111
+ const content = message.content
1112
+ .slice(0, shown)
1113
+ .map((part, index) => (finished || index < shown - 1 ? part : livePart(part)))
1114
+
1115
+ out.push(finished ? message : { ...message, content, error: undefined, time: { created: message.time.created } })
1116
+ state.used += shown
1117
+ state.live = !finished
1118
+ state.caption = finished ? "Step finished" : liveCaption(content.at(-1))
1119
+ }
1120
+
1121
+ const last = out.at(-1)
1122
+
1123
+ return {
1124
+ document: {
1125
+ sessionID,
1126
+ messages: structuredClone(out),
1127
+ status: { type: state.live || (last?.type !== "idle" && state.used < sessionSteps(messages)) ? "busy" : "idle" },
1128
+ diffs: [],
1129
+ } satisfies SessionDocument,
1130
+ caption: state.caption,
1131
+ }
1132
+ }
1133
+
1134
+ function livePart(part: SessionMessageAssistant["content"][number]): SessionMessageAssistant["content"][number] {
1135
+ if (part.type === "reasoning") return { ...part, time: part.time ? { created: part.time.created } : undefined }
1136
+
1137
+ if (part.type !== "tool" || part.state.status === "streaming") return part
1138
+
1139
+ return {
1140
+ ...part,
1141
+ state: {
1142
+ status: "running",
1143
+ input: part.state.input,
1144
+ metadata: "metadata" in part.state ? (part.state.metadata ?? {}) : {},
1145
+ },
1146
+ time: { created: part.time.created, ran: part.time.ran },
1147
+ }
1148
+ }
1149
+
1150
+ function liveCaption(part: SessionMessageAssistant["content"][number] | undefined) {
1151
+ if (part?.type === "reasoning") return "Thinking"
1152
+
1153
+ if (part?.type === "tool") return `${part.name} running`
1154
+
1155
+ return "Writing"
1156
+ }