@kirosnn/mosaic 0.0.7

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 (154) hide show
  1. package/.mosaic/mosaic.local.jsonc +0 -0
  2. package/MOSAIC.md +188 -0
  3. package/README.md +127 -0
  4. package/docs/mosaic.png +0 -0
  5. package/package.json +42 -0
  6. package/src/agent/Agent.ts +131 -0
  7. package/src/agent/context.ts +96 -0
  8. package/src/agent/index.ts +2 -0
  9. package/src/agent/prompts/systemPrompt.ts +138 -0
  10. package/src/agent/prompts/toolsPrompt.ts +139 -0
  11. package/src/agent/provider/anthropic.ts +122 -0
  12. package/src/agent/provider/google.ts +124 -0
  13. package/src/agent/provider/mistral.ts +117 -0
  14. package/src/agent/provider/ollama.ts +531 -0
  15. package/src/agent/provider/openai.ts +220 -0
  16. package/src/agent/provider/xai.ts +122 -0
  17. package/src/agent/tools/bash.ts +20 -0
  18. package/src/agent/tools/definitions.ts +27 -0
  19. package/src/agent/tools/edit.ts +23 -0
  20. package/src/agent/tools/executor.ts +751 -0
  21. package/src/agent/tools/explore.ts +18 -0
  22. package/src/agent/tools/exploreExecutor.ts +320 -0
  23. package/src/agent/tools/glob.ts +16 -0
  24. package/src/agent/tools/grep.ts +19 -0
  25. package/src/agent/tools/index.ts +4 -0
  26. package/src/agent/tools/list.ts +20 -0
  27. package/src/agent/tools/question.ts +20 -0
  28. package/src/agent/tools/read.ts +15 -0
  29. package/src/agent/tools/write.ts +21 -0
  30. package/src/agent/types.ts +155 -0
  31. package/src/components/App.tsx +174 -0
  32. package/src/components/CommandsModal.tsx +77 -0
  33. package/src/components/CustomInput.tsx +328 -0
  34. package/src/components/Main.tsx +1112 -0
  35. package/src/components/Notification.tsx +91 -0
  36. package/src/components/SelectList.tsx +47 -0
  37. package/src/components/Setup.tsx +528 -0
  38. package/src/components/ShortcutsModal.tsx +67 -0
  39. package/src/components/Welcome.tsx +39 -0
  40. package/src/components/main/ApprovalPanel.tsx +134 -0
  41. package/src/components/main/ChatPage.tsx +516 -0
  42. package/src/components/main/HomePage.tsx +111 -0
  43. package/src/components/main/QuestionPanel.tsx +85 -0
  44. package/src/components/main/ThinkingIndicator.tsx +101 -0
  45. package/src/components/main/types.ts +55 -0
  46. package/src/components/main/wrapText.ts +41 -0
  47. package/src/index.tsx +212 -0
  48. package/src/utils/approvalBridge.ts +129 -0
  49. package/src/utils/commands/echo.ts +22 -0
  50. package/src/utils/commands/help.ts +25 -0
  51. package/src/utils/commands/index.ts +68 -0
  52. package/src/utils/commands/init.ts +68 -0
  53. package/src/utils/commands/redo.ts +74 -0
  54. package/src/utils/commands/registry.ts +29 -0
  55. package/src/utils/commands/sessions.ts +129 -0
  56. package/src/utils/commands/types.ts +20 -0
  57. package/src/utils/commands/undo.ts +75 -0
  58. package/src/utils/commands/web.ts +77 -0
  59. package/src/utils/config.ts +357 -0
  60. package/src/utils/diff.ts +201 -0
  61. package/src/utils/diffRendering.tsx +62 -0
  62. package/src/utils/exploreBridge.ts +87 -0
  63. package/src/utils/fileChangeTracker.ts +98 -0
  64. package/src/utils/fileChangesBridge.ts +18 -0
  65. package/src/utils/history.ts +106 -0
  66. package/src/utils/markdown.tsx +232 -0
  67. package/src/utils/models.ts +304 -0
  68. package/src/utils/questionBridge.ts +122 -0
  69. package/src/utils/terminalUtils.ts +25 -0
  70. package/src/utils/toolFormatting.ts +384 -0
  71. package/src/utils/undoRedo.ts +429 -0
  72. package/src/utils/undoRedoBridge.ts +45 -0
  73. package/src/utils/undoRedoDb.ts +338 -0
  74. package/src/utils/uninstall.ts +45 -0
  75. package/src/utils/version.ts +3 -0
  76. package/src/web/app.tsx +606 -0
  77. package/src/web/assets/css/ChatPage.css +212 -0
  78. package/src/web/assets/css/FileExplorer.css +202 -0
  79. package/src/web/assets/css/HomePage.css +119 -0
  80. package/src/web/assets/css/Markdown.css +178 -0
  81. package/src/web/assets/css/MessageItem.css +160 -0
  82. package/src/web/assets/css/Sidebar.css +208 -0
  83. package/src/web/assets/css/SidebarModal.css +137 -0
  84. package/src/web/assets/css/ThinkingIndicator.css +47 -0
  85. package/src/web/assets/css/ToolMessage.css +148 -0
  86. package/src/web/assets/css/global.css +226 -0
  87. package/src/web/assets/fonts/Geist-Black.woff2 +0 -0
  88. package/src/web/assets/fonts/Geist-BlackItalic.woff2 +0 -0
  89. package/src/web/assets/fonts/Geist-Bold.woff2 +0 -0
  90. package/src/web/assets/fonts/Geist-BoldItalic.woff2 +0 -0
  91. package/src/web/assets/fonts/Geist-ExtraBold.woff2 +0 -0
  92. package/src/web/assets/fonts/Geist-ExtraBoldItalic.woff2 +0 -0
  93. package/src/web/assets/fonts/Geist-ExtraLight.woff2 +0 -0
  94. package/src/web/assets/fonts/Geist-ExtraLightItalic.woff2 +0 -0
  95. package/src/web/assets/fonts/Geist-Italic[wght].woff2 +0 -0
  96. package/src/web/assets/fonts/Geist-Light.woff2 +0 -0
  97. package/src/web/assets/fonts/Geist-LightItalic.woff2 +0 -0
  98. package/src/web/assets/fonts/Geist-Medium.woff2 +0 -0
  99. package/src/web/assets/fonts/Geist-MediumItalic.woff2 +0 -0
  100. package/src/web/assets/fonts/Geist-Regular.woff2 +0 -0
  101. package/src/web/assets/fonts/Geist-RegularItalic.woff2 +0 -0
  102. package/src/web/assets/fonts/Geist-SemiBold.woff2 +0 -0
  103. package/src/web/assets/fonts/Geist-SemiBoldItalic.woff2 +0 -0
  104. package/src/web/assets/fonts/Geist-Thin.woff2 +0 -0
  105. package/src/web/assets/fonts/Geist-ThinItalic.woff2 +0 -0
  106. package/src/web/assets/fonts/GeistMono-Black.woff2 +0 -0
  107. package/src/web/assets/fonts/GeistMono-BlackItalic.woff2 +0 -0
  108. package/src/web/assets/fonts/GeistMono-Bold.woff2 +0 -0
  109. package/src/web/assets/fonts/GeistMono-BoldItalic.woff2 +0 -0
  110. package/src/web/assets/fonts/GeistMono-ExtraBold.woff2 +0 -0
  111. package/src/web/assets/fonts/GeistMono-ExtraBoldItalic.woff2 +0 -0
  112. package/src/web/assets/fonts/GeistMono-ExtraLight.woff2 +0 -0
  113. package/src/web/assets/fonts/GeistMono-ExtraLightItalic.woff2 +0 -0
  114. package/src/web/assets/fonts/GeistMono-Italic.woff2 +0 -0
  115. package/src/web/assets/fonts/GeistMono-Italic[wght].woff2 +0 -0
  116. package/src/web/assets/fonts/GeistMono-Light.woff2 +0 -0
  117. package/src/web/assets/fonts/GeistMono-LightItalic.woff2 +0 -0
  118. package/src/web/assets/fonts/GeistMono-Medium.woff2 +0 -0
  119. package/src/web/assets/fonts/GeistMono-MediumItalic.woff2 +0 -0
  120. package/src/web/assets/fonts/GeistMono-Regular.woff2 +0 -0
  121. package/src/web/assets/fonts/GeistMono-SemiBold.woff2 +0 -0
  122. package/src/web/assets/fonts/GeistMono-SemiBoldItalic.woff2 +0 -0
  123. package/src/web/assets/fonts/GeistMono-Thin.woff2 +0 -0
  124. package/src/web/assets/fonts/GeistMono-ThinItalic.woff2 +0 -0
  125. package/src/web/assets/fonts/GeistMono[wght].woff2 +0 -0
  126. package/src/web/assets/fonts/Geist[wght].woff2 +0 -0
  127. package/src/web/assets/fonts/blauer-nue-regular.woff2 +0 -0
  128. package/src/web/assets/fonts/neue-montreal-regular.woff2 +0 -0
  129. package/src/web/assets/images/favicon-v2.svg +6 -0
  130. package/src/web/assets/images/favicon.png +0 -0
  131. package/src/web/assets/images/foruse.svg +5 -0
  132. package/src/web/assets/images/logo_black.svg +5 -0
  133. package/src/web/assets/images/logo_white.svg +5 -0
  134. package/src/web/assets/images/logoblack.png +0 -0
  135. package/src/web/assets/images/logowhite.png +0 -0
  136. package/src/web/build.ts +23 -0
  137. package/src/web/components/ApprovalPanel.tsx +191 -0
  138. package/src/web/components/ChatPage.tsx +273 -0
  139. package/src/web/components/FileExplorer.tsx +162 -0
  140. package/src/web/components/HomePage.tsx +121 -0
  141. package/src/web/components/MessageItem.tsx +178 -0
  142. package/src/web/components/Modal.tsx +30 -0
  143. package/src/web/components/QuestionPanel.tsx +149 -0
  144. package/src/web/components/Setup.tsx +211 -0
  145. package/src/web/components/Sidebar.tsx +292 -0
  146. package/src/web/components/ThinkingIndicator.tsx +85 -0
  147. package/src/web/logo_black.svg +5 -0
  148. package/src/web/logo_white.svg +5 -0
  149. package/src/web/router.ts +46 -0
  150. package/src/web/server.tsx +662 -0
  151. package/src/web/storage.ts +92 -0
  152. package/src/web/types.ts +17 -0
  153. package/src/web/utils.ts +61 -0
  154. package/tsconfig.json +33 -0
@@ -0,0 +1,429 @@
1
+ import { existsSync, mkdirSync, writeFileSync, unlinkSync } from 'fs';
2
+ import { join, resolve } from 'path';
3
+ import { execSync } from 'child_process';
4
+ import type { Message } from '../components/main/types';
5
+ import {
6
+ createSession,
7
+ getCurrentSession,
8
+ pushUndoState,
9
+ popUndoState,
10
+ pushRedoState,
11
+ popRedoState,
12
+ clearRedoStates,
13
+ getUndoCount,
14
+ getRedoCount,
15
+ cleanupOldSessions,
16
+ type UndoRedoState,
17
+ type FileSnapshot
18
+ } from './undoRedoDb';
19
+
20
+ export type { UndoRedoState, FileSnapshot };
21
+
22
+ let currentSessionId: string | null = null;
23
+ let pendingFileSnapshots: FileSnapshot[] = [];
24
+
25
+ function getWorkspaceMosaicDir(): string {
26
+ const workspace = process.cwd();
27
+ const mosaicDir = join(workspace, '.mosaic');
28
+
29
+ if (!existsSync(mosaicDir)) {
30
+ mkdirSync(mosaicDir, { recursive: true });
31
+ }
32
+
33
+ return mosaicDir;
34
+ }
35
+
36
+ function generateId(): string {
37
+ return `${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
38
+ }
39
+
40
+ export function isGitRepository(): boolean {
41
+ try {
42
+ execSync('git rev-parse --git-dir', { stdio: 'ignore', cwd: process.cwd() });
43
+ return true;
44
+ } catch {
45
+ return false;
46
+ }
47
+ }
48
+
49
+ export function getGitStatus(): { clean: boolean; hasChanges: boolean } {
50
+ try {
51
+ const status = execSync('git status --porcelain', { encoding: 'utf-8', cwd: process.cwd() });
52
+ const hasChanges = status.trim().length > 0;
53
+ return { clean: !hasChanges, hasChanges };
54
+ } catch {
55
+ return { clean: false, hasChanges: false };
56
+ }
57
+ }
58
+
59
+ export function initializeSession(): void {
60
+ cleanupOldSessions(7);
61
+
62
+ const existingSession = getCurrentSession();
63
+ if (existingSession) {
64
+ currentSessionId = existingSession.id;
65
+ } else {
66
+ currentSessionId = createSession();
67
+ }
68
+
69
+ pendingFileSnapshots = [];
70
+ }
71
+
72
+ export function getCurrentSessionId(): string | null {
73
+ return currentSessionId;
74
+ }
75
+
76
+ export function captureFileSnapshot(filePath: string): void {
77
+ const workspace = process.cwd();
78
+ const fullPath = resolve(workspace, filePath);
79
+
80
+ let content = '';
81
+ let existed = false;
82
+
83
+ try {
84
+ const fs = require('fs');
85
+ content = fs.readFileSync(fullPath, 'utf-8');
86
+ existed = true;
87
+ } catch {
88
+ existed = false;
89
+ }
90
+
91
+ const alreadyCaptured = pendingFileSnapshots.some(s => s.path === filePath);
92
+ if (!alreadyCaptured) {
93
+ pendingFileSnapshots.push({
94
+ path: filePath,
95
+ content,
96
+ existed
97
+ });
98
+ }
99
+ }
100
+
101
+ function createGitCommit(message: string): string | null {
102
+ try {
103
+ const workspace = process.cwd();
104
+
105
+ execSync('git add -A', { cwd: workspace, stdio: 'ignore' });
106
+
107
+ execSync(`git commit -m "${message.replace(/"/g, '\\"')}"`, {
108
+ cwd: workspace,
109
+ stdio: 'ignore',
110
+ env: {
111
+ ...process.env,
112
+ GIT_AUTHOR_NAME: 'Mosaic',
113
+ GIT_AUTHOR_EMAIL: 'mosaic@local',
114
+ GIT_COMMITTER_NAME: 'Mosaic',
115
+ GIT_COMMITTER_EMAIL: 'mosaic@local'
116
+ }
117
+ });
118
+
119
+ const hash = execSync('git rev-parse HEAD', { encoding: 'utf-8', cwd: workspace }).trim();
120
+ return hash;
121
+ } catch (error) {
122
+ console.error('Failed to create Git commit:', error);
123
+ return null;
124
+ }
125
+ }
126
+
127
+ export function saveState(messages: Message[]): void {
128
+ if (!currentSessionId) {
129
+ return;
130
+ }
131
+
132
+ const useGit = isGitRepository();
133
+ let gitCommitHash: string | undefined;
134
+
135
+ if (useGit) {
136
+ const gitStatus = getGitStatus();
137
+ if (gitStatus.hasChanges) {
138
+ const hash = createGitCommit(`[Mosaic] Save state - ${new Date().toISOString()}`);
139
+ if (hash) {
140
+ gitCommitHash = hash;
141
+ }
142
+ } else {
143
+ try {
144
+ const currentHash = execSync('git rev-parse HEAD', { encoding: 'utf-8', cwd: process.cwd() }).trim();
145
+ gitCommitHash = currentHash;
146
+ } catch {
147
+ gitCommitHash = undefined;
148
+ }
149
+ }
150
+ }
151
+
152
+ const state: UndoRedoState = {
153
+ id: generateId(),
154
+ timestamp: Date.now(),
155
+ messages: JSON.parse(JSON.stringify(messages)),
156
+ gitCommitHash,
157
+ fileSnapshots: JSON.parse(JSON.stringify(pendingFileSnapshots)),
158
+ useGit
159
+ };
160
+
161
+ pushUndoState(currentSessionId, state);
162
+ clearRedoStates(currentSessionId);
163
+ pendingFileSnapshots = [];
164
+ }
165
+
166
+ function restoreFileSnapshots(snapshots: FileSnapshot[]): { success: boolean; errors: string[] } {
167
+ const errors: string[] = [];
168
+ const workspace = process.cwd();
169
+
170
+ for (const snapshot of snapshots) {
171
+ try {
172
+ const fullPath = resolve(workspace, snapshot.path);
173
+ if (snapshot.existed) {
174
+ writeFileSync(fullPath, snapshot.content, 'utf-8');
175
+ } else {
176
+ if (existsSync(fullPath)) {
177
+ unlinkSync(fullPath);
178
+ }
179
+ }
180
+ } catch (error) {
181
+ const errorMsg = error instanceof Error ? error.message : 'Unknown error';
182
+ errors.push(`Failed to restore ${snapshot.path}: ${errorMsg}`);
183
+ }
184
+ }
185
+
186
+ return {
187
+ success: errors.length === 0,
188
+ errors
189
+ };
190
+ }
191
+
192
+ export function canUndo(): boolean {
193
+ if (!currentSessionId) return false;
194
+ return getUndoCount(currentSessionId) > 0;
195
+ }
196
+
197
+ export function canRedo(): boolean {
198
+ if (!currentSessionId) return false;
199
+ return getRedoCount(currentSessionId) > 0;
200
+ }
201
+
202
+
203
+ function getUntrackedFiles(): string[] {
204
+ try {
205
+ const workspace = process.cwd();
206
+ const output = execSync('git ls-files --others --exclude-standard', { encoding: 'utf-8', cwd: workspace });
207
+ return output.trim().split('\n').filter(Boolean);
208
+ } catch {
209
+ return [];
210
+ }
211
+ }
212
+
213
+ function getFileSnapshot(filePath: string): FileSnapshot {
214
+ const workspace = process.cwd();
215
+ const fullPath = resolve(workspace, filePath);
216
+ let content = '';
217
+ let existed = false;
218
+
219
+ try {
220
+ if (existsSync(fullPath)) {
221
+ const fs = require('fs');
222
+ content = fs.readFileSync(fullPath, 'utf-8');
223
+ existed = true;
224
+ }
225
+ } catch {
226
+ existed = false;
227
+ }
228
+
229
+ return { path: filePath, content, existed };
230
+ }
231
+
232
+ function getGitChanges(targetHash: string): FileChange[] {
233
+ if (!isGitRepository()) {
234
+ return [];
235
+ }
236
+
237
+ try {
238
+ const workspace = process.cwd();
239
+ const diffOutput = execSync(`git diff --name-status HEAD ${targetHash}`, {
240
+ encoding: 'utf-8',
241
+ cwd: workspace
242
+ });
243
+
244
+ const changes = diffOutput
245
+ .trim()
246
+ .split('\n')
247
+ .filter(line => line.length > 0)
248
+ .map(line => {
249
+ const parts = line.split('\t');
250
+ const status = parts[0];
251
+ const pathParts = parts.slice(1);
252
+
253
+ if (!status) return null;
254
+
255
+ return {
256
+ status: status.charAt(0),
257
+ path: pathParts.join('\t')
258
+ };
259
+ })
260
+ .filter((item): item is FileChange => item !== null);
261
+
262
+ const untracked = getUntrackedFiles();
263
+ const untrackedChanges = untracked.map(path => ({
264
+ status: 'D',
265
+ path
266
+ }));
267
+
268
+ return [...changes, ...untrackedChanges];
269
+ } catch (error) {
270
+ console.error('Failed to get git changes:', error);
271
+ return [];
272
+ }
273
+ }
274
+
275
+ export interface FileChange {
276
+ path: string;
277
+ status: string;
278
+ }
279
+
280
+ export function undo(): { state: UndoRedoState; success: boolean; error?: string; currentState?: UndoRedoState; gitChanges?: FileChange[] } | null {
281
+ if (!currentSessionId || !canUndo()) {
282
+ return null;
283
+ }
284
+
285
+ const workspace = process.cwd();
286
+ const useGit = isGitRepository();
287
+
288
+ let currentStateForRedo: UndoRedoState;
289
+
290
+ try {
291
+ if (useGit) {
292
+ let currentHash: string | undefined;
293
+ try {
294
+ currentHash = execSync('git rev-parse HEAD', { encoding: 'utf-8', cwd: workspace }).trim();
295
+ } catch {
296
+ currentHash = undefined;
297
+ }
298
+
299
+ const untrackedFiles = getUntrackedFiles();
300
+ const untrackedSnapshots = untrackedFiles.map(f => getFileSnapshot(f));
301
+
302
+ currentStateForRedo = {
303
+ id: generateId(),
304
+ timestamp: Date.now(),
305
+ messages: [],
306
+ gitCommitHash: currentHash,
307
+ fileSnapshots: untrackedSnapshots,
308
+ useGit: true
309
+ };
310
+ } else {
311
+ currentStateForRedo = {
312
+ id: generateId(),
313
+ timestamp: Date.now(),
314
+ messages: [],
315
+ gitCommitHash: undefined,
316
+ fileSnapshots: pendingFileSnapshots.length > 0 ? JSON.parse(JSON.stringify(pendingFileSnapshots)) : [],
317
+ useGit: false
318
+ };
319
+ }
320
+ } catch (error) {
321
+ const errorMsg = error instanceof Error ? error.message : 'Unknown error';
322
+ return { state: { id: '', timestamp: 0, messages: [], fileSnapshots: [], useGit: false }, success: false, error: errorMsg };
323
+ }
324
+
325
+ const state = popUndoState(currentSessionId);
326
+ if (!state) {
327
+ return null;
328
+ }
329
+
330
+ let gitChanges: FileChange[] = [];
331
+
332
+ try {
333
+ if (state.useGit && state.gitCommitHash) {
334
+ gitChanges = getGitChanges(state.gitCommitHash);
335
+
336
+ const filesToClean = getUntrackedFiles();
337
+
338
+ execSync(`git reset --hard ${state.gitCommitHash}`, { cwd: workspace, stdio: 'ignore' });
339
+ execSync('git clean -fd', { cwd: workspace, stdio: 'ignore' });
340
+ if (filesToClean.length > 0) {
341
+ const fs = require('fs');
342
+ for (const file of filesToClean) {
343
+ const fullPath = resolve(workspace, file);
344
+ if (existsSync(fullPath)) {
345
+ try {
346
+ const stat = fs.lstatSync(fullPath);
347
+ if (stat.isDirectory()) {
348
+ fs.rmSync(fullPath, { recursive: true, force: true });
349
+ } else {
350
+ fs.unlinkSync(fullPath);
351
+ }
352
+ } catch (cleanupError) {
353
+ console.error(`Failed to force delete ${file}:`, cleanupError);
354
+ }
355
+ }
356
+ }
357
+ }
358
+ } else {
359
+ const restoreResult = restoreFileSnapshots(state.fileSnapshots);
360
+ if (!restoreResult.success) {
361
+ throw new Error(restoreResult.errors.join('\n'));
362
+ }
363
+ }
364
+
365
+ pushRedoState(currentSessionId, currentStateForRedo);
366
+
367
+ return { state, success: true, currentState: currentStateForRedo, gitChanges };
368
+ } catch (error) {
369
+ pushUndoState(currentSessionId, state);
370
+
371
+ const errorMsg = error instanceof Error ? error.message : 'Unknown error';
372
+ return { state, success: false, error: errorMsg };
373
+ }
374
+ }
375
+
376
+ export function redo(): { state: UndoRedoState; success: boolean; error?: string; gitChanges?: FileChange[] } | null {
377
+ if (!currentSessionId || !canRedo()) {
378
+ return null;
379
+ }
380
+
381
+ const state = popRedoState(currentSessionId);
382
+ if (!state) {
383
+ return null;
384
+ }
385
+
386
+ let gitChanges: FileChange[] = [];
387
+
388
+ try {
389
+ if (state.useGit && state.gitCommitHash) {
390
+ const workspace = process.cwd();
391
+ gitChanges = getGitChanges(state.gitCommitHash);
392
+
393
+ execSync(`git reset --hard ${state.gitCommitHash}`, { cwd: workspace, stdio: 'ignore' });
394
+ restoreFileSnapshots(state.fileSnapshots);
395
+ } else {
396
+ const restoreResult = restoreFileSnapshots(state.fileSnapshots);
397
+ if (!restoreResult.success) {
398
+ throw new Error(restoreResult.errors.join('\n'));
399
+ }
400
+ }
401
+
402
+ pushUndoState(currentSessionId, state);
403
+
404
+ return { state, success: true, gitChanges };
405
+ } catch (error) {
406
+ pushRedoState(currentSessionId, state);
407
+
408
+ const errorMsg = error instanceof Error ? error.message : 'Unknown error';
409
+ return { state, success: false, error: errorMsg };
410
+ }
411
+ }
412
+
413
+ export function getUndoStack(): UndoRedoState[] {
414
+ return [];
415
+ }
416
+
417
+ export function getRedoStack(): UndoRedoState[] {
418
+ return [];
419
+ }
420
+
421
+ export function clearSession(): void {
422
+ if (!currentSessionId) return;
423
+
424
+ clearRedoStates(currentSessionId);
425
+ currentSessionId = null;
426
+ pendingFileSnapshots = [];
427
+ }
428
+
429
+ export { getAllSessions, setCurrentSession, deleteSession } from './undoRedoDb';
@@ -0,0 +1,45 @@
1
+ import type { Message } from '../components/main/types';
2
+ import type { UndoRedoState } from './undoRedo';
3
+
4
+ type UndoRedoCallback = (state: UndoRedoState | null, action: 'undo' | 'redo') => void;
5
+
6
+ let callback: UndoRedoCallback | null = null;
7
+
8
+ export function subscribeUndoRedo(cb: UndoRedoCallback): () => void {
9
+ callback = cb;
10
+ return () => {
11
+ callback = null;
12
+ };
13
+ }
14
+
15
+ export function notifyUndoRedo(state: UndoRedoState | null, action: 'undo' | 'redo'): void {
16
+ if (callback) {
17
+ callback(state, action);
18
+ }
19
+ }
20
+
21
+ export interface CaptureSnapshotRequest {
22
+ messages: Message[];
23
+ resolve: () => void;
24
+ }
25
+
26
+ type CaptureSnapshotCallback = (request: CaptureSnapshotRequest) => void;
27
+
28
+ let captureCallback: CaptureSnapshotCallback | null = null;
29
+
30
+ export function subscribeCaptureSnapshot(cb: CaptureSnapshotCallback): () => void {
31
+ captureCallback = cb;
32
+ return () => {
33
+ captureCallback = null;
34
+ };
35
+ }
36
+
37
+ export function requestCaptureSnapshot(messages: Message[]): Promise<void> {
38
+ return new Promise((resolve) => {
39
+ if (captureCallback) {
40
+ captureCallback({ messages, resolve });
41
+ } else {
42
+ resolve();
43
+ }
44
+ });
45
+ }