ompchamber 0.7.0 → 1.0.0

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 (165) hide show
  1. package/dist/client/index.html +1 -1
  2. package/dist/client/static/css/index.e880517fcd.css +2 -0
  3. package/dist/client/static/js/async/{3y.4c2596f64c.js → 3y.71f2ccf3b6.js} +2 -2
  4. package/dist/client/static/js/async/{xw.ae67ba06fd.js → 5b.0d6ae50fe8.js} +10 -10
  5. package/dist/client/static/js/async/5bh.8b2363424f.js +1 -0
  6. package/dist/client/static/js/async/{vy.bbe114c4e0.js → bi.09688ba75c.js} +1 -2
  7. package/dist/client/static/js/async/c.797d933882.js +1 -0
  8. package/dist/client/static/js/async/{dc.6dc4b0a04b.js → dc.856fda6560.js} +2 -2
  9. package/dist/client/static/js/async/g.a06ca3b3e9.js +2 -0
  10. package/dist/client/static/js/async/g.a06ca3b3e9.js.LICENSE.txt +11 -0
  11. package/dist/client/static/js/async/{m.4b29d34f93.js → m.c038c71b9f.js} +2 -2
  12. package/dist/client/static/js/async/ma.2c261b04dd.js +2 -0
  13. package/dist/client/static/js/async/ms.98960af4eb.js +67 -0
  14. package/dist/client/static/js/async/nam.b5663e919f.js +10 -0
  15. package/dist/client/static/js/async/nam.b5663e919f.js.LICENSE.txt +11 -0
  16. package/dist/client/static/js/async/ot.84c71b308f.js +2 -0
  17. package/dist/client/static/js/async/ot.84c71b308f.js.LICENSE.txt +11 -0
  18. package/dist/client/static/js/async/{q.542c184fec.js → q.23af2a358f.js} +3 -3
  19. package/dist/client/static/js/async/{s.413c2a4683.js → s.222ee974f6.js} +2 -2
  20. package/dist/client/static/js/async/t.4dca0e2ba8.js +1 -0
  21. package/dist/client/static/js/async/uf.46ad6209fa.js +2 -0
  22. package/dist/client/static/js/async/xn.6360379f71.js +2 -0
  23. package/dist/client/static/js/async/xnl.feb0021e50.js +2 -0
  24. package/dist/client/static/js/async/xnl.feb0021e50.js.LICENSE.txt +12 -0
  25. package/dist/client/static/js/index.0de15c5e17.js +33 -0
  26. package/dist/client/static/js/{uy.2e8929ed0c.js → jp.50817896f3.js} +6 -6
  27. package/package.json +2 -1
  28. package/src/client/components/common/MarkdownRenderer.tsx +69 -27
  29. package/src/client/components/common/code-editor/history.test.ts +118 -0
  30. package/src/client/components/common/code-editor/history.ts +101 -0
  31. package/src/client/components/common/code-editor/index.tsx +56 -66
  32. package/src/client/components/common/code-surface/index.tsx +84 -11
  33. package/src/client/components/common/code-surface/measure.ts +66 -17
  34. package/src/client/components/common/diagram-viewer/index.tsx +258 -0
  35. package/src/client/components/common/diagram-viewer/read-block.ts +53 -0
  36. package/src/client/components/layout/session-sidebar/CategoryItem.tsx +2 -1
  37. package/src/client/components/layout/session-sidebar/SessionItem.tsx +11 -4
  38. package/src/client/components/layout/session-sidebar/SessionList.tsx +1 -1
  39. package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +1 -1
  40. package/src/client/components/mobile/mobile-session-sidebar/List.tsx +1 -1
  41. package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +2 -2
  42. package/src/client/components/settings/LazyModal.tsx +1 -1
  43. package/src/client/components/settings/Modal.tsx +2 -1
  44. package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +11 -4
  45. package/src/client/components/workspace/chat-timeline/QueueList.tsx +18 -14
  46. package/src/client/components/workspace/chat-timeline/ToolCallCard.tsx +8 -1
  47. package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +11 -52
  48. package/src/client/components/workspace/chat-timeline/chat-input/selection.ts +57 -0
  49. package/src/client/components/workspace/chat-timeline/index.tsx +124 -108
  50. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/FrameControl.tsx +172 -0
  51. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/QuestionBlock.tsx +155 -0
  52. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/drafts.ts +113 -0
  53. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/index.tsx +92 -0
  54. package/src/client/components/workspace/chat-timeline/tool-renderers/{ask-dialog → extension-dialog}/Footer.tsx +3 -3
  55. package/src/client/components/workspace/chat-timeline/tool-renderers/{ask-dialog → extension-dialog}/Header.tsx +3 -3
  56. package/src/client/components/workspace/chat-timeline/tool-renderers/{ask-dialog → extension-dialog}/Lazy.tsx +8 -8
  57. package/src/client/components/workspace/chat-timeline/tool-renderers/{ask-dialog → extension-dialog}/SelectBody.tsx +3 -3
  58. package/src/client/components/workspace/chat-timeline/tool-renderers/{ask-dialog → extension-dialog}/index.tsx +16 -10
  59. package/src/client/components/workspace/chat-timeline/tool-renderers/index.tsx +2 -2
  60. package/src/client/components/workspace/file-explorer/index.tsx +18 -5
  61. package/src/client/components/workspace/git-panel/BranchToolbar.tsx +2 -2
  62. package/src/client/components/workspace/git-panel/index.tsx +5 -1
  63. package/src/client/components/workspace/terminal-panel/Header.tsx +51 -19
  64. package/src/client/components/workspace/terminal-panel/RealtimeXtermView.tsx +86 -282
  65. package/src/client/components/workspace/terminal-panel/index.tsx +76 -50
  66. package/src/client/components/workspace/terminal-panel/mount.ts +275 -0
  67. package/src/client/data/theme/terminal.ts +27 -29
  68. package/src/client/hooks/chat/omp/session-list.tsx +23 -2
  69. package/src/client/hooks/chat/omp/session-sidebar-controller.ts +1 -1
  70. package/src/client/hooks/chat/timeline/actions.ts +23 -34
  71. package/src/client/hooks/chat/timeline/ask-frames.ts +102 -0
  72. package/src/client/hooks/chat/timeline/extension-dialog.ts +12 -9
  73. package/src/client/hooks/chat/timeline/index.ts +18 -28
  74. package/src/client/hooks/chat/timeline/prompt-text.ts +35 -0
  75. package/src/client/hooks/chat/timeline/queue.ts +110 -46
  76. package/src/client/hooks/chat/timeline/send.ts +13 -27
  77. package/src/client/hooks/chat/timeline/session-load.ts +48 -106
  78. package/src/client/hooks/chat/timeline/session-pagination.ts +144 -0
  79. package/src/client/hooks/editor/use-code-window.ts +118 -0
  80. package/src/client/hooks/ui/input-required-alert.ts +43 -0
  81. package/src/client/hooks/ui/notification-sound.ts +65 -0
  82. package/src/client/hooks/ui/theme.ts +22 -6
  83. package/src/client/hooks/workspace/terminal/index.ts +217 -0
  84. package/src/client/hooks/workspace/terminal/socket.ts +227 -0
  85. package/src/client/tailwind.css +38 -12
  86. package/src/server/db.server.ts +19 -0
  87. package/src/server/lib/fs/git-sync.ts +191 -0
  88. package/src/server/lib/omp/rpc/manager.ts +72 -6
  89. package/src/server/lib/omp/rpc/session-commands.ts +6 -0
  90. package/src/server/lib/omp/rpc/session-registry.ts +18 -0
  91. package/src/server/lib/omp/session/sidebar-data.server.ts +10 -4
  92. package/src/server/lib/queue/delivery.server.ts +114 -0
  93. package/src/server/lib/queue/schema-migration.server.ts +50 -0
  94. package/src/server/lib/queue/store.server.ts +185 -0
  95. package/src/server/lib/terminal/foreground.ts +73 -0
  96. package/src/server/lib/terminal/history.test.ts +118 -0
  97. package/src/server/lib/terminal/history.ts +205 -0
  98. package/src/server/lib/terminal/runtime.server.ts +338 -0
  99. package/src/server/lib/terminal/session.server.ts +215 -0
  100. package/src/server/lib/terminal/shell.test.ts +97 -0
  101. package/src/server/lib/terminal/shell.ts +103 -0
  102. package/src/server/routes/fs/git.ts +22 -12
  103. package/src/server/routes/index.ts +2 -1
  104. package/src/server/routes/sessions/index.ts +11 -3
  105. package/src/server/routes/sessions/queue.ts +121 -0
  106. package/src/server/routes/sessions/session.ts +1 -132
  107. package/src/server/routes/settings/commands.ts +1 -1
  108. package/src/server/routes/terminal/index.ts +2 -2
  109. package/src/server/routes/terminal/run.ts +8 -100
  110. package/src/server/routes/terminal/sessions.ts +24 -0
  111. package/src/server/routes/terminal/ws.ts +149 -0
  112. package/src/shared/lib/chat/ask-questions.test.ts +169 -0
  113. package/src/shared/lib/chat/ask-questions.ts +264 -0
  114. package/src/shared/lib/chat/omp/agent-events.test.ts +37 -1
  115. package/src/shared/lib/chat/omp/agent-events.ts +30 -0
  116. package/src/shared/lib/chat/timeline/omp-callbacks.test.ts +9 -1
  117. package/src/shared/lib/chat/timeline/omp-callbacks.ts +45 -8
  118. package/src/shared/lib/code/lazy-window.test.ts +161 -0
  119. package/src/shared/lib/code/lazy-window.ts +156 -0
  120. package/src/shared/lib/code/syntax-highlight.test.ts +50 -1
  121. package/src/shared/lib/code/syntax-highlight.ts +14 -1
  122. package/src/shared/lib/code/windowed-highlight.ts +63 -0
  123. package/src/shared/lib/markdown/mermaid.ts +8 -6
  124. package/src/shared/lib/omp/session/stream-state.server.ts +2 -3
  125. package/src/shared/lib/workspace/session-state/store.ts +1 -4
  126. package/src/shared/lib/workspace/terminal/protocol.ts +222 -0
  127. package/src/shared/types/omp/agent.ts +14 -2
  128. package/src/shared/types/terminal.ts +17 -15
  129. package/src/shared/types/workspace.ts +6 -2
  130. package/dist/client/static/css/index.51f68210a5.css +0 -2
  131. package/dist/client/static/js/async/5b.d88304bc13.js +0 -1
  132. package/dist/client/static/js/async/c.cd45b0ac45.js +0 -1
  133. package/dist/client/static/js/async/fw.6a51998a8e.js +0 -6
  134. package/dist/client/static/js/async/g.86e2256a70.js +0 -2
  135. package/dist/client/static/js/async/kc.bbdb35b771.js +0 -1
  136. package/dist/client/static/js/async/mw.ca1512563c.js +0 -10
  137. package/dist/client/static/js/async/rf.d626afc367.js +0 -2
  138. package/dist/client/static/js/async/t.cda4891526.js +0 -1
  139. package/dist/client/static/js/async/uf.24ca3d49c3.js +0 -2
  140. package/dist/client/static/js/async/uf.24ca3d49c3.js.LICENSE.txt +0 -6
  141. package/dist/client/static/js/async/v0.394ed37c6b.js +0 -2
  142. package/dist/client/static/js/async/v0.394ed37c6b.js.LICENSE.txt +0 -6
  143. package/dist/client/static/js/async/vy.bbe114c4e0.js.LICENSE.txt +0 -6
  144. package/dist/client/static/js/async/xn.afecebbbd9.js +0 -2
  145. package/dist/client/static/js/index.eccad0b70a.js +0 -33
  146. package/dist/client/static/js/index.eccad0b70a.js.LICENSE.txt +0 -4
  147. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Ask.tsx +0 -205
  148. package/src/client/components/workspace/terminal-panel/InputBar.tsx +0 -82
  149. package/src/client/components/workspace/terminal-panel/LogEntry.tsx +0 -70
  150. package/src/client/hooks/workspace/terminal-output.ts +0 -66
  151. package/src/client/hooks/workspace/terminal.ts +0 -283
  152. package/src/server/lib/fs/terminal-cwd.ts +0 -49
  153. package/src/server/routes/terminal/stream.ts +0 -100
  154. /package/dist/client/static/js/async/{3y.4c2596f64c.js.LICENSE.txt → 3y.71f2ccf3b6.js.LICENSE.txt} +0 -0
  155. /package/dist/client/static/js/async/{g.86e2256a70.js.LICENSE.txt → 5b.0d6ae50fe8.js.LICENSE.txt} +0 -0
  156. /package/dist/client/static/js/async/{dc.6dc4b0a04b.js.LICENSE.txt → dc.856fda6560.js.LICENSE.txt} +0 -0
  157. /package/dist/client/static/js/async/{fw.6a51998a8e.js.LICENSE.txt → m.c038c71b9f.js.LICENSE.txt} +0 -0
  158. /package/dist/client/static/js/async/{m.4b29d34f93.js.LICENSE.txt → ma.2c261b04dd.js.LICENSE.txt} +0 -0
  159. /package/dist/client/static/js/async/{xn.afecebbbd9.js.LICENSE.txt → ms.98960af4eb.js.LICENSE.txt} +0 -0
  160. /package/dist/client/static/js/async/{mw.ca1512563c.js.LICENSE.txt → q.23af2a358f.js.LICENSE.txt} +0 -0
  161. /package/dist/client/static/js/async/{q.542c184fec.js.LICENSE.txt → s.222ee974f6.js.LICENSE.txt} +0 -0
  162. /package/dist/client/static/js/async/{rf.d626afc367.js.LICENSE.txt → uf.46ad6209fa.js.LICENSE.txt} +0 -0
  163. /package/dist/client/static/js/async/{s.413c2a4683.js.LICENSE.txt → xn.6360379f71.js.LICENSE.txt} +0 -0
  164. /package/dist/client/static/js/{async/xw.ae67ba06fd.js.LICENSE.txt → index.0de15c5e17.js.LICENSE.txt} +0 -0
  165. /package/dist/client/static/js/{uy.2e8929ed0c.js.LICENSE.txt → jp.50817896f3.js.LICENSE.txt} +0 -0
@@ -0,0 +1,217 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Live terminal session for the right panel: the socket, its status, and the
8
+ * attach/resize/input verbs the xterm view drives.
9
+ *
10
+ * The terminal id is per chamber session and persisted, which is what makes the
11
+ * shell survive a page reload: the panel reattaches to the id it already owns
12
+ * and the server replays its scrollback. `restart` mints a new id, so a shell
13
+ * that was exited (or wedged) is left behind rather than resurrected.
14
+ *
15
+ * The view owns the grid — it is the only thing that knows the container's
16
+ * size — so attach and resize flow view → hook, while output flows hook → view
17
+ * through the imperative handle the panel wires up.
18
+ */
19
+
20
+ import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
21
+ import { useSessionState } from '@/client/hooks/workspace/session-state';
22
+ import { useSessionStateContext } from '@/client/hooks/workspace/session-state/context';
23
+ import { getSessionValue } from '@/shared/lib/workspace/session-state/store';
24
+ import { connectTerminal, type TerminalSocket } from '@/client/hooks/workspace/terminal/socket';
25
+ import { newTerminalId, type TerminalReadyFrame } from '@/shared/lib/workspace/terminal/protocol';
26
+
27
+ export type TerminalStatus = 'connecting' | 'running' | 'exited' | 'error';
28
+
29
+ export interface TerminalSystemInfo {
30
+ gitBranch: string;
31
+ }
32
+
33
+ export interface UseTerminalOptions {
34
+ root?: string;
35
+ repo?: string;
36
+ theme?: 'light' | 'dark';
37
+ onOutput?: (bytes: Uint8Array) => void;
38
+ onReplay?: (bytes: Uint8Array, frame: TerminalReadyFrame) => void;
39
+ /** Fired after the shell is replaced, so the view can drop its buffer. */
40
+ onRestart?: () => void;
41
+ }
42
+
43
+ export interface UseTerminalResult {
44
+ status: TerminalStatus;
45
+ /** Working directory the shell runs in, once known. */
46
+ cwd: string;
47
+ shell: string;
48
+ /** Exit code of a finished shell, or null while it runs. */
49
+ exitCode: number | null;
50
+ /** Set when the socket reported an error frame. */
51
+ error: string | null;
52
+ systemInfo: TerminalSystemInfo;
53
+ attach: (cols: number, rows: number) => void;
54
+ resize: (cols: number, rows: number) => void;
55
+ input: (data: Uint8Array) => void;
56
+ /** Replace the shell with a fresh one. */
57
+ restart: () => void;
58
+ }
59
+
60
+ export function useTerminal(options: UseTerminalOptions = {}): UseTerminalResult {
61
+ const { sessionId, ready } = useSessionStateContext();
62
+ const [terminalId, setTerminalId] = useSessionState<string>('terminal.id', '');
63
+ const [status, setStatus] = useState<TerminalStatus>('connecting');
64
+ const [cwd, setCwd] = useState('');
65
+ const [shell, setShell] = useState('');
66
+ const [exitCode, setExitCode] = useState<number | null>(null);
67
+ const [error, setError] = useState<string | null>(null);
68
+ const [systemInfo, setSystemInfo] = useState<TerminalSystemInfo>({ gitBranch: 'main' });
69
+
70
+ const socketRef = useRef<TerminalSocket | null>(null);
71
+ // The grid the view reported, replayed on attach so a reconnect lands on the
72
+ // size the panel actually has instead of the default.
73
+ const sizeRef = useRef<{ cols: number; rows: number } | null>(null);
74
+ const outputRef = useRef(options.onOutput);
75
+ outputRef.current = options.onOutput;
76
+ const replayRef = useRef(options.onReplay);
77
+ replayRef.current = options.onReplay;
78
+ const restartRef = useRef(options.onRestart);
79
+ restartRef.current = options.onRestart;
80
+ const rootRef = useRef(options.root);
81
+ rootRef.current = options.root;
82
+ const repoRef = useRef(options.repo);
83
+ repoRef.current = options.repo;
84
+ const themeRef = useRef(options.theme);
85
+ themeRef.current = options.theme;
86
+ const setIdRef = useRef(setTerminalId);
87
+ setIdRef.current = setTerminalId;
88
+ /** Last `root\0repo` the shell was launched for; null until the first. */
89
+ const scopeRef = useRef<string | null>(null);
90
+
91
+ // One id per chamber session. The check reads the store, not the hook's local
92
+ // value: on the render where the blob first becomes ready, `useSessionState`
93
+ // still reports the fallback (its restore effect has not run yet), so
94
+ // deciding from the local value would mint a second id and overwrite the
95
+ // stored one — orphaning the live shell and its scrollback.
96
+ useEffect(() => {
97
+ if (!ready) return;
98
+ if (getSessionValue<string>(sessionId, 'terminal.id')) return;
99
+ setIdRef.current(newTerminalId());
100
+ }, [ready, sessionId]);
101
+
102
+ useEffect(() => {
103
+ if (!ready || !terminalId) return;
104
+
105
+ setStatus('connecting');
106
+ setError(null);
107
+ setExitCode(null);
108
+
109
+ const socket = connectTerminal(terminalId, {
110
+ onReady(frame) {
111
+ setStatus('running');
112
+ setCwd(frame.cwd);
113
+ setShell(frame.shell);
114
+ setExitCode(null);
115
+ },
116
+ onReplay(bytes, frame) {
117
+ replayRef.current?.(bytes, frame);
118
+ },
119
+ onOutput(bytes) {
120
+ outputRef.current?.(bytes);
121
+ },
122
+ onExit(code) {
123
+ setStatus('exited');
124
+ setExitCode(code);
125
+ },
126
+ onError(code, message) {
127
+ setStatus('error');
128
+ setError(`${code}: ${message}`);
129
+ },
130
+ onConnectionChange(connected) {
131
+ if (!connected) setStatus('connecting');
132
+ },
133
+ });
134
+ socketRef.current = socket;
135
+ // The socket re-dials the same id after a drop; the panel's size is the one
136
+ // to come back at. On a restart the view is already mounted and will not
137
+ // report its grid again, so the remembered size is what makes the new shell
138
+ // spawn at the right dimensions.
139
+ const size = sizeRef.current;
140
+ if (size) socket.attach(size.cols, size.rows, rootRef.current, repoRef.current, themeRef.current);
141
+
142
+ return () => {
143
+ socket.release();
144
+ socketRef.current = null;
145
+ };
146
+ }, [ready, terminalId]);
147
+
148
+ const restart = useCallback(() => {
149
+ // Kill the old shell before minting a new id: leaving it to the idle
150
+ // reaper would keep a dead terminal attached to this session for 30 min.
151
+ socketRef.current?.close();
152
+ restartRef.current?.();
153
+ setIdRef.current(newTerminalId());
154
+ }, []);
155
+
156
+ /**
157
+ * The repo picker in the header changes the shell's scope, and a PTY's cwd is
158
+ * fixed at spawn — so a scope change replaces the shell instead of trying to
159
+ * `cd` it (which would leave the user's own shell state — exports, jobs,
160
+ * history — describing the old tree).
161
+ *
162
+ * The first scope seen is adopted without restarting: `root` arrives
163
+ * asynchronously (the active project resolves with the sidebar data), and
164
+ * restarting on that first transition would kill the shell the view is still
165
+ * attaching.
166
+ */
167
+ useEffect(() => {
168
+ if (!ready) return;
169
+ const root = options.root;
170
+ if (!root) return;
171
+ const scope = `${root}\u0000${options.repo ?? '.'}`;
172
+ if (scopeRef.current === null) {
173
+ scopeRef.current = scope;
174
+ return;
175
+ }
176
+ if (scopeRef.current === scope) return;
177
+ scopeRef.current = scope;
178
+ restart();
179
+ }, [ready, options.root, options.repo, restart]);
180
+
181
+ // Launch context for the header (git branch), scoped like the shell itself.
182
+ useEffect(() => {
183
+ const root = options.root;
184
+ const repo = options.repo;
185
+ if (!root) return;
186
+ const params = new URLSearchParams({ root });
187
+ if (repo && repo !== '.') params.set('repo', repo);
188
+ let cancelled = false;
189
+ fetch(`/api/terminal/run?${params.toString()}`)
190
+ .then((response) => (response.ok ? response.json() : null))
191
+ .then((data: { gitBranch?: string } | null) => {
192
+ if (!cancelled && data?.gitBranch) setSystemInfo({ gitBranch: data.gitBranch });
193
+ })
194
+ .catch(() => {
195
+ // The header simply keeps its previous branch label.
196
+ });
197
+ return () => {
198
+ cancelled = true;
199
+ };
200
+ }, [options.root, options.repo]);
201
+
202
+ const attach = useCallback((cols: number, rows: number) => {
203
+ sizeRef.current = { cols, rows };
204
+ socketRef.current?.attach(cols, rows, rootRef.current, repoRef.current, themeRef.current);
205
+ }, []);
206
+
207
+ const resize = useCallback((cols: number, rows: number) => {
208
+ sizeRef.current = { cols, rows };
209
+ socketRef.current?.resize(cols, rows);
210
+ }, []);
211
+
212
+ const input = useCallback((data: Uint8Array) => {
213
+ socketRef.current?.input(data);
214
+ }, []);
215
+
216
+ return { status, cwd, shell, exitCode, error, systemInfo, attach, resize, input, restart };
217
+ }
@@ -0,0 +1,227 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Client transport for one terminal: dial, attach, and stay attached.
8
+ *
9
+ * One socket carries both directions — JSON control frames out, raw PTY bytes
10
+ * both ways. Three behaviours are deliberate:
11
+ *
12
+ * - **Binary in, binary out.** `binaryType = 'arraybuffer'` so output arrives as
13
+ * bytes and is handed straight to `term.write(Uint8Array)`; the client never
14
+ * decodes UTF-8 itself, which is what keeps a character split across two PTY
15
+ * reads intact.
16
+ * - **`onBinary` carries the same bytes as `onData`.** xterm reports non-UTF-8
17
+ * payloads (some mouse reports, paste of raw bytes) on `onBinary`; forwarding
18
+ * only `onData` would silently drop them.
19
+ * - **Reconnect, not re-create.** A dropped socket re-dials the same terminal
20
+ * id, so the shell and its scrollback survive a network blip or a page reload
21
+ * instead of a second shell appearing.
22
+ */
23
+
24
+ import {
25
+ decodeServerFrame,
26
+ encodeClientFrame,
27
+ terminalSocketUrl,
28
+ type TerminalAttachFrame,
29
+ type TerminalClientFrame,
30
+ type TerminalReadyFrame,
31
+ type TerminalServerFrame,
32
+ } from '@/shared/lib/workspace/terminal/protocol';
33
+
34
+ const RECONNECT_BASE_MS = 500;
35
+ const RECONNECT_MAX_MS = 8000;
36
+ /** Consecutive re-dials that never opened before the stream is given up on. */
37
+ const MAX_FAILED_DIALS = 4;
38
+
39
+ export interface TerminalSocketHandlers {
40
+ /** The attach was accepted; `replayBytes` of scrollback follow. */
41
+ onReady: (frame: TerminalReadyFrame) => void;
42
+ /**
43
+ * The retained scrollback, once. The view must render this at the reported
44
+ * `replayCols`/`replayRows` before fitting its own viewport.
45
+ */
46
+ onReplay: (bytes: Uint8Array, frame: TerminalReadyFrame) => void;
47
+ /** Live PTY output, after the replay. */
48
+ onOutput: (bytes: Uint8Array) => void;
49
+ onExit: (exitCode: number | null, signal: string | null) => void;
50
+ onError: (code: string, message: string) => void;
51
+ /** Connection state for the panel's status badge. */
52
+ onConnectionChange: (connected: boolean) => void;
53
+ }
54
+
55
+ export interface TerminalSocket {
56
+ /** Send one attach frame; ignored once a terminal is attached. */
57
+ attach: (cols: number, rows: number, root?: string, repo?: string, theme?: 'light' | 'dark') => void;
58
+ resize: (cols: number, rows: number) => void;
59
+ /** Raw keystrokes or paste; never JSON-encoded. */
60
+ input: (data: Uint8Array) => void;
61
+ /** Kill the shell server-side. */
62
+ close: () => void;
63
+ /** Release the socket; the shell keeps running for the next attach. */
64
+ release: () => void;
65
+ }
66
+
67
+ export function connectTerminal(terminalId: string, handlers: TerminalSocketHandlers): TerminalSocket {
68
+ let socket: WebSocket | null = null;
69
+ let retryTimer: ReturnType<typeof setTimeout> | null = null;
70
+ let failures = 0;
71
+ let established = false;
72
+ let released = false;
73
+ let attached = false;
74
+ let lastAttach: TerminalAttachFrame | null = null;
75
+ /** Scrollback accumulated so far, plus how many bytes are still expected. */
76
+ let replayParts: Uint8Array[] = [];
77
+ let replayRemaining = 0;
78
+ let replayFrame: TerminalReadyFrame | null = null;
79
+
80
+ const dial = () => {
81
+ const ws = new WebSocket(terminalSocketUrl(terminalId));
82
+ ws.binaryType = 'arraybuffer';
83
+ socket = ws;
84
+
85
+ ws.onopen = () => {
86
+ if (released) return;
87
+ failures = 0;
88
+ established = true;
89
+ handlers.onConnectionChange(true);
90
+ // Re-attaching replays the retained scrollback, so a reconnected panel
91
+ // converges on the shell's real state instead of showing a gap.
92
+ if (lastAttach) ws.send(encodeClientFrame(lastAttach));
93
+ };
94
+
95
+ ws.onmessage = (event) => {
96
+ if (released) return;
97
+ if (event.data instanceof ArrayBuffer) {
98
+ handleOutput(new Uint8Array(event.data));
99
+ return;
100
+ }
101
+ const frame = decodeServerFrame(typeof event.data === 'string' ? event.data : '');
102
+ if (frame) applyFrame(frame);
103
+ };
104
+
105
+ ws.onclose = () => {
106
+ socket = null;
107
+ if (released) return;
108
+ handlers.onConnectionChange(false);
109
+ attached = false;
110
+ replayParts = [];
111
+ replayRemaining = 0;
112
+ replayFrame = null;
113
+ if (!established) return;
114
+ failures += 1;
115
+ if (failures > MAX_FAILED_DIALS) return;
116
+ const delay = Math.min(RECONNECT_BASE_MS * 2 ** (failures - 1), RECONNECT_MAX_MS);
117
+ retryTimer = setTimeout(() => {
118
+ retryTimer = null;
119
+ if (!released) dial();
120
+ }, delay);
121
+ };
122
+
123
+ ws.onerror = () => {
124
+ // `onclose` follows and owns the retry decision.
125
+ };
126
+ };
127
+
128
+ const applyFrame = (frame: TerminalServerFrame) => {
129
+ if (frame.t === 'ready') {
130
+ attached = true;
131
+ replayFrame = frame;
132
+ replayRemaining = frame.replayBytes;
133
+ replayParts = [];
134
+ handlers.onReady(frame);
135
+ // An empty scrollback never produces a binary frame, so complete it here.
136
+ if (replayRemaining === 0) finishReplay();
137
+ return;
138
+ }
139
+ if (frame.t === 'exit') {
140
+ handlers.onExit(frame.exitCode, frame.signal);
141
+ return;
142
+ }
143
+ handlers.onError(frame.code, frame.message);
144
+ };
145
+
146
+ const handleOutput = (bytes: Uint8Array) => {
147
+ if (replayRemaining <= 0) {
148
+ handlers.onOutput(bytes);
149
+ return;
150
+ }
151
+ // The server sends the scrollback as one frame, so this normally completes
152
+ // in a single step; the accounting is what keeps a fragmented delivery
153
+ // from being mistaken for live output.
154
+ replayParts.push(bytes);
155
+ replayRemaining -= bytes.length;
156
+ if (replayRemaining > 0) return;
157
+ finishReplay();
158
+ };
159
+
160
+ const finishReplay = () => {
161
+ const frame = replayFrame;
162
+ const parts = replayParts;
163
+ replayFrame = null;
164
+ replayParts = [];
165
+ replayRemaining = 0;
166
+ if (!frame) return;
167
+ handlers.onReplay(parts.length === 1 ? parts[0] : concat(parts), frame);
168
+ };
169
+
170
+ dial();
171
+
172
+ const send = (frame: TerminalClientFrame) => {
173
+ const ws = socket;
174
+ if (!ws || ws.readyState !== WebSocket.OPEN) return;
175
+ ws.send(encodeClientFrame(frame));
176
+ };
177
+
178
+ return {
179
+ attach(cols, rows, root, repo, theme) {
180
+ if (attached) return;
181
+ const frame: TerminalAttachFrame = { t: 'attach', cols, rows };
182
+ if (root) frame.root = root;
183
+ if (repo) frame.repo = repo;
184
+ if (theme) frame.theme = theme;
185
+ lastAttach = frame;
186
+ send(frame);
187
+ },
188
+ resize(cols, rows) {
189
+ send({ t: 'resize', cols, rows });
190
+ },
191
+ input(data) {
192
+ const ws = socket;
193
+ if (!ws || ws.readyState !== WebSocket.OPEN) return;
194
+ ws.send(data);
195
+ },
196
+ close() {
197
+ send({ t: 'close' });
198
+ },
199
+ release() {
200
+ released = true;
201
+ if (retryTimer !== null) {
202
+ clearTimeout(retryTimer);
203
+ retryTimer = null;
204
+ }
205
+ const ws = socket;
206
+ socket = null;
207
+ if (!ws) return;
208
+ ws.onopen = null;
209
+ ws.onmessage = null;
210
+ ws.onclose = null;
211
+ ws.onerror = null;
212
+ ws.close();
213
+ },
214
+ };
215
+ }
216
+
217
+ function concat(parts: Uint8Array[]): Uint8Array {
218
+ let size = 0;
219
+ for (const part of parts) size += part.length;
220
+ const out = new Uint8Array(size);
221
+ let offset = 0;
222
+ for (const part of parts) {
223
+ out.set(part, offset);
224
+ offset += part.length;
225
+ }
226
+ return out;
227
+ }
@@ -318,6 +318,11 @@
318
318
  border-radius: 0.5rem;
319
319
  background: var(--theme-paper);
320
320
  padding: 0.75rem;
321
+ /* `applySvg` swaps the `.code-block` shell (the positioned ancestor the copy
322
+ button relied on) for the SVG, so the block owns the anchor itself. */
323
+ position: relative;
324
+ /* The block is the door into the zoom/pan viewer. */
325
+ cursor: zoom-in;
321
326
  }
322
327
  .prose-content .mermaid-block > svg {
323
328
  display: block;
@@ -332,6 +337,27 @@
332
337
  color: var(--theme-error);
333
338
  }
334
339
 
340
+ /* Full-screen mermaid viewer (`common/diagram-viewer/`): a chart opened from
341
+ the timeline. The stage is the fixed-size pan surface, the canvas child
342
+ carries the zoom/pan transform, so the SVG keeps its natural pixel size and
343
+ `100%` on it always means "the size mermaid rendered". */
344
+ .diagram-viewer-stage {
345
+ touch-action: none;
346
+ overscroll-behavior: contain;
347
+ }
348
+ .diagram-viewer-canvas {
349
+ transform-origin: center center;
350
+ will-change: transform;
351
+ background: var(--theme-paper);
352
+ box-shadow: 0 25px 50px -12px color-mix(in srgb, var(--theme-ink) 45%, transparent);
353
+ }
354
+ .diagram-viewer-canvas > svg {
355
+ display: block;
356
+ width: 100% !important;
357
+ height: 100% !important;
358
+ max-width: none !important;
359
+ }
360
+
335
361
  /* PWA Window Controls Overlay and Native Titlebar Styling */
336
362
  .titlebar-drag-region {
337
363
  -webkit-app-region: drag;
@@ -344,31 +370,31 @@
344
370
  }
345
371
 
346
372
 
347
- /* Ask-dialog enter animation — replaces the retired `motion` dependency.
348
- Exit is intentionally instant: a 160ms fade-out on a blocking modal is not
349
- load-bearing, and keeping the element mounted to animate it out would add
350
- state the dialog does not otherwise need. */
351
- @keyframes ask-dialog-backdrop-in {
373
+ /* Blocking extension-dialog enter animation — replaces the retired `motion`
374
+ dependency. Exit is intentionally instant: a 160ms fade-out on a blocking
375
+ modal is not load-bearing, and keeping the element mounted to animate it out
376
+ would add state the dialog does not otherwise need. */
377
+ @keyframes extension-dialog-backdrop-in {
352
378
  from { opacity: 0; }
353
379
  to { opacity: 1; }
354
380
  }
355
381
 
356
- @keyframes ask-dialog-panel-in {
382
+ @keyframes extension-dialog-panel-in {
357
383
  from { opacity: 0; transform: translateY(10px) scale(0.95); }
358
384
  to { opacity: 1; transform: none; }
359
385
  }
360
386
 
361
- .ask-dialog-backdrop {
362
- animation: ask-dialog-backdrop-in 150ms ease-out;
387
+ .extension-dialog-backdrop {
388
+ animation: extension-dialog-backdrop-in 150ms ease-out;
363
389
  }
364
390
 
365
- .ask-dialog-panel {
366
- animation: ask-dialog-panel-in 180ms ease-out;
391
+ .extension-dialog-panel {
392
+ animation: extension-dialog-panel-in 180ms ease-out;
367
393
  }
368
394
 
369
395
  @media (prefers-reduced-motion: reduce) {
370
- .ask-dialog-backdrop,
371
- .ask-dialog-panel {
396
+ .extension-dialog-backdrop,
397
+ .extension-dialog-panel {
372
398
  animation: none;
373
399
  }
374
400
  }
@@ -10,6 +10,9 @@ import { loadOmpSidebarData } from '@/server/lib/omp/session/reader';
10
10
  import { pathExists, projectPathKey } from '@/server/lib/omp/core/paths';
11
11
 
12
12
  let dbPromise: Promise<DbClient> | null = null;
13
+ /** Resolved handle cached by getDb(); promise callbacks never run sync, so
14
+ * hot paths unwrap through `getDbSync` instead of awaiting. */
15
+ let dbResolved: DbClient | null = null;
13
16
 
14
17
  /** SYNC_WORKSPACE env flag (default true when unset). When enabled in real
15
18
  * mode, workspace folders are auto-created from discovered omp projects. */
@@ -123,6 +126,10 @@ export async function getDb(): Promise<DbClient> {
123
126
 
124
127
  const { migrateWorkspaceFolderColumns } = await import('@/shared/lib/workspace/schema-migrations');
125
128
  await migrateWorkspaceFolderColumns(db);
129
+ // Installs older `queued_messages` schemas carry an invalid FK (see the
130
+ // migration module); rebuild once so real-session queue inserts work.
131
+ const { migrateQueueTableFk } = await import('@/server/lib/queue/schema-migration.server');
132
+ await migrateQueueTableFk(db);
126
133
 
127
134
  await db.exec(`
128
135
  CREATE TABLE IF NOT EXISTS files (
@@ -292,12 +299,24 @@ export async function getDb(): Promise<DbClient> {
292
299
  const { migrateLegacyProjectSettings } = await import('@/shared/lib/workspace/project-settings-migration');
293
300
  await migrateLegacyProjectSettings(db);
294
301
 
302
+ dbResolved = db;
295
303
  return db;
296
304
  })();
297
305
 
298
306
  return dbPromise;
299
307
  }
300
308
 
309
+ /**
310
+ * Synchronous handle for hot paths needing raw sync transactions
311
+ * (`bun:sqlite` is sync, so BEGIN…COMMIT cannot interleave with another
312
+ * request). The server initializes the db at startup; anything earlier must
313
+ * use `getDb()`.
314
+ */
315
+ export function getDbSync(): DbClient {
316
+ if (!dbResolved) throw new Error('Database not initialized — call getDb() first');
317
+ return dbResolved;
318
+ }
319
+
301
320
  /**
302
321
  * Additive-only folder ↔ omp project sync: creates a workspace folder for
303
322
  * every project discovered from ~/.omp/agent (folder name = lowercase