@kabmo-labs/kabmo 0.28.0 → 0.29.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 (225) hide show
  1. package/bunfig.toml +6 -0
  2. package/dist/client/index.html +2 -2
  3. package/dist/client/index.js +481 -472
  4. package/dist/client/static/js/{index-xzvyrxya.js → index-43z47ngg.js} +1193 -1049
  5. package/dist/client/static/js/index-naa91727.css +1 -0
  6. package/dist/client/static/{manifest-q6h8k9e0.webmanifest → manifest-vdvdqpre.webmanifest} +1 -0
  7. package/package.json +3 -2
  8. package/public/manifest.webmanifest +1 -0
  9. package/src/client/components/common/ComposerTextarea.test.tsx +109 -0
  10. package/src/client/components/common/ComposerTextarea.tsx +6 -0
  11. package/src/client/components/common/ReloadPageButton.test.tsx +14 -0
  12. package/src/client/components/common/ReloadPageButton.tsx +11 -2
  13. package/src/client/components/common/diagram-viewer/read-block.test.ts +71 -0
  14. package/src/client/components/common/diagram-viewer/read-block.ts +40 -16
  15. package/src/client/components/common/file-icon/index.tsx +7 -1
  16. package/src/client/components/common/harness-glyph/brands.ts +7 -1
  17. package/src/client/components/common/image-viewer/index.tsx +37 -165
  18. package/src/client/components/common/picker-search/index.tsx +70 -0
  19. package/src/client/components/common/search-select/index.tsx +2 -7
  20. package/src/client/components/common/zoom-pan-stage/index.tsx +258 -0
  21. package/src/client/components/common/zoom-pan-stage/zoom-pan-stage.test.tsx +87 -0
  22. package/src/client/components/layout/session-sidebar/ClassicView.tsx +1 -1
  23. package/src/client/components/layout/session-sidebar/SessionListNotice.tsx +7 -3
  24. package/src/client/components/layout/session-sidebar/about-modal/HarnessRow.tsx +4 -1
  25. package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +12 -1
  26. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.test.tsx +82 -0
  27. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +23 -3
  28. package/src/client/components/layout/session-sidebar/about-modal/index.tsx +1 -0
  29. package/src/client/components/layout/session-sidebar/modern/index.tsx +1 -1
  30. package/src/client/components/layout/session-sidebar/scheduler-modal/TaskList.tsx +6 -4
  31. package/src/client/components/layout/session-sidebar/session-list-states.test.tsx +8 -0
  32. package/src/client/components/mobile/mobile-right-sidebar/EditorSurface.tsx +25 -0
  33. package/src/client/components/mobile/mobile-session-sidebar/ClassicDrawer.tsx +1 -1
  34. package/src/client/components/settings/Sidebar.tsx +6 -8
  35. package/src/client/components/settings/categories/HarnessSettings.tsx +4 -2
  36. package/src/client/components/settings/categories/PanelPluginSettings.tsx +2 -2
  37. package/src/client/components/settings/categories/PluginSettings.tsx +1 -1
  38. package/src/client/components/settings/categories/harness-settings/ConfigForm.tsx +3 -6
  39. package/src/client/components/settings/categories/panel-plugins/GitFreshness.test.tsx +36 -0
  40. package/src/client/components/settings/categories/panel-plugins/Section.tsx +1 -3
  41. package/src/client/components/settings/categories/panel-plugins/Toolbar.tsx +18 -4
  42. package/src/client/components/settings/categories/plugin-settings/MarketplacePane.tsx +64 -61
  43. package/src/client/components/settings/categories/plugin-settings/SidebarList.tsx +15 -4
  44. package/src/client/components/settings/groups.test.tsx +2 -2
  45. package/src/client/components/workspace/chat-timeline/IrcNotice.test.tsx +55 -0
  46. package/src/client/components/workspace/chat-timeline/IrcNotice.tsx +71 -0
  47. package/src/client/components/workspace/chat-timeline/MessageItem.tsx +4 -1
  48. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.test.ts +19 -0
  49. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.tsx +8 -3
  50. package/src/client/components/workspace/chat-timeline/chat-input/WaitingDependencyPicker.search.test.tsx +72 -0
  51. package/src/client/components/workspace/chat-timeline/chat-input/WaitingDependencyPicker.tsx +42 -15
  52. package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.test.tsx +28 -0
  53. package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.tsx +28 -10
  54. package/src/client/components/workspace/chat-timeline/target-picker/WorkspacePicker.test.tsx +47 -1
  55. package/src/client/components/workspace/chat-timeline/target-picker/WorkspacePicker.tsx +55 -5
  56. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.tsx +3 -2
  57. package/src/client/components/workspace/editor/FilePane.tsx +32 -0
  58. package/src/client/components/workspace/editor/Toolbar.tsx +4 -0
  59. package/src/client/components/workspace/editor/toolbar-clusters.test.ts +1 -0
  60. package/src/client/components/workspace/editor/toolbar-clusters.tsx +5 -2
  61. package/src/client/components/workspace/editor/viewers/CsvGrid.tsx +52 -5
  62. package/src/client/components/workspace/editor/viewers/DataGrid.tsx +227 -41
  63. package/src/client/components/workspace/editor/viewers/DrawioView.tsx +93 -0
  64. package/src/client/components/workspace/editor/viewers/GridRow.tsx +91 -0
  65. package/src/client/components/workspace/editor/viewers/GridToolbar.tsx +53 -0
  66. package/src/client/components/workspace/editor/viewers/SheetView.tsx +152 -57
  67. package/src/client/components/workspace/editor/viewers/dispatch.tsx +42 -9
  68. package/src/client/components/workspace/editor/viewers/drawio-pages.test.ts +80 -0
  69. package/src/client/components/workspace/editor/viewers/drawio-pages.ts +63 -0
  70. package/src/client/components/workspace/editor/viewers/grid.test.ts +296 -0
  71. package/src/client/components/workspace/editor/viewers/use-cell-edit.ts +64 -0
  72. package/src/client/components/workspace/editor/viewers/use-grid-interaction.ts +216 -0
  73. package/src/client/components/workspace/editor/viewers/viewers.test.ts +47 -2
  74. package/src/client/components/workspace/editor/viewers/workbook.ts +149 -0
  75. package/src/client/components/workspace/file-explorer/ArchiveModals.tsx +262 -0
  76. package/src/client/components/workspace/file-explorer/ContextMenu.tsx +30 -6
  77. package/src/client/components/workspace/file-explorer/Modals.tsx +8 -3
  78. package/src/client/components/workspace/file-explorer/RootOverlays.tsx +60 -0
  79. package/src/client/components/workspace/file-explorer/RowLabel.tsx +6 -2
  80. package/src/client/components/workspace/file-explorer/TreeItem.tsx +39 -49
  81. package/src/client/components/workspace/file-explorer/TreeItemModals.tsx +80 -0
  82. package/src/client/components/workspace/file-explorer/index.tsx +53 -29
  83. package/src/client/components/workspace/file-explorer/selection.test.tsx +109 -0
  84. package/src/client/data/settings/omp-schema.json +169 -22
  85. package/src/client/glass.css +39 -14
  86. package/src/client/glass.test.ts +37 -0
  87. package/src/client/hooks/chat/composer/history.test.ts +267 -0
  88. package/src/client/hooks/chat/composer/history.ts +146 -0
  89. package/src/client/hooks/chat/timeline/ask-frames.test.ts +54 -0
  90. package/src/client/hooks/chat/timeline/ask-frames.ts +29 -1
  91. package/src/client/hooks/chat/timeline/timeline-rail.test.ts +34 -2
  92. package/src/client/hooks/chat/timeline/user-turns.ts +31 -2
  93. package/src/client/hooks/editor/file-editor-types.ts +15 -0
  94. package/src/client/hooks/editor/save-bytes.ts +79 -0
  95. package/src/client/hooks/editor/use-file-editor.test.ts +30 -0
  96. package/src/client/hooks/editor/use-file-editor.ts +53 -5
  97. package/src/client/hooks/editor/use-file-read.ts +21 -2
  98. package/src/client/hooks/editor/use-grid-selection.ts +160 -0
  99. package/src/client/hooks/models/use-harness-update.test.ts +35 -2
  100. package/src/client/hooks/models/use-harness-update.ts +49 -10
  101. package/src/client/hooks/workspace/diff-file.test.ts +72 -0
  102. package/src/client/hooks/workspace/diff-file.ts +12 -6
  103. package/src/client/hooks/workspace/file-archive.ts +220 -0
  104. package/src/client/hooks/workspace/file-clipboard.test.ts +4 -4
  105. package/src/client/hooks/workspace/file-clipboard.ts +37 -26
  106. package/src/client/hooks/workspace/file-selection.test.ts +67 -0
  107. package/src/client/hooks/workspace/file-selection.ts +156 -0
  108. package/src/client/hooks/workspace/file-tree-actions-clipboard.test.ts +5 -5
  109. package/src/client/hooks/workspace/file-tree-actions.ts +36 -8
  110. package/src/client/lib/editor/workbook-drafts.ts +42 -0
  111. package/src/client/lib/launch/file-handlers.ts +2 -2
  112. package/src/client/lib/scrollbar-activity.test.ts +21 -88
  113. package/src/client/lib/scrollbar-activity.ts +71 -73
  114. package/src/client/lib/scrollbar-events.ts +143 -0
  115. package/src/client/lib/scrollbar-input.test.ts +126 -0
  116. package/src/client/lib/scrollbar-probe.test-util.ts +107 -0
  117. package/src/client/tailwind.css +32 -1
  118. package/src/server/db.server.test.ts +50 -1
  119. package/src/server/db.server.ts +36 -3
  120. package/src/server/lib/chat/prompt-history.server.test.ts +92 -0
  121. package/src/server/lib/chat/prompt-history.server.ts +65 -0
  122. package/src/server/lib/chat/schema.server.ts +35 -0
  123. package/src/server/lib/db/schema.ts +3 -0
  124. package/src/server/lib/fs/archive.test.ts +139 -0
  125. package/src/server/lib/fs/archive.ts +259 -0
  126. package/src/server/lib/fs/git-diff.test.ts +26 -1
  127. package/src/server/lib/fs/git-diff.ts +43 -15
  128. package/src/server/lib/harness/launch-table.ts +19 -0
  129. package/src/server/lib/harness/provider-entry.ts +1 -1
  130. package/src/server/lib/harness/provider-file.server.ts +1 -1
  131. package/src/server/lib/harness/provider-sources.server.ts +2 -1
  132. package/src/server/lib/harness/provider-write.server.ts +2 -1
  133. package/src/server/lib/harness/update-job-store.ts +12 -0
  134. package/src/server/lib/harness/update-queue.server.ts +15 -10
  135. package/src/server/lib/omp/rpc/frame-fold-signals.test.ts +5 -1
  136. package/src/server/lib/omp/rpc/frame-fold.test.ts +5 -1
  137. package/src/server/lib/omp/rpc/lifecycle.test.ts +4 -0
  138. package/src/server/lib/omp/session/auto-title.server.test.ts +4 -0
  139. package/src/server/lib/omp/session/delete.server.ts +1 -0
  140. package/src/server/lib/omp/session/goal-driver.test.ts +5 -1
  141. package/src/server/lib/omp/session/goal-settings.test.ts +5 -1
  142. package/src/server/lib/panels/auto-update.server.test.ts +5 -2
  143. package/src/server/lib/panels/git-update.server.test.ts +5 -2
  144. package/src/server/lib/panels/install.server.test.ts +5 -2
  145. package/src/server/lib/panels/remove.server.test.ts +5 -2
  146. package/src/server/routes/agent/command.ts +11 -0
  147. package/src/server/routes/agent/new.ts +5 -0
  148. package/src/server/routes/chat/index.ts +5 -0
  149. package/src/server/routes/chat/prompt-history.test.ts +67 -0
  150. package/src/server/routes/chat/prompt-history.ts +31 -0
  151. package/src/server/routes/chat/stream.test.ts +21 -0
  152. package/src/server/routes/chat/stream.ts +5 -0
  153. package/src/server/routes/fs/action-archive.test.ts +128 -0
  154. package/src/server/routes/fs/action-bulk.ts +106 -0
  155. package/src/server/routes/fs/action-save-bytes.test.ts +59 -0
  156. package/src/server/routes/fs/action-transfer.test.ts +26 -0
  157. package/src/server/routes/fs/action.ts +91 -11
  158. package/src/server/routes/fs/archive.ts +20 -0
  159. package/src/server/routes/fs/grant.test.ts +5 -2
  160. package/src/server/routes/fs/index.ts +2 -0
  161. package/src/server/routes/fs/scope.test.ts +6 -2
  162. package/src/server/routes/harness/import-action.ts +1 -1
  163. package/src/server/routes/harness/root.ts +6 -1
  164. package/src/server/routes/harness/summary.test.ts +50 -0
  165. package/src/server/routes/harness/update-status.ts +41 -2
  166. package/src/server/routes/harness/update.test.ts +56 -2
  167. package/src/server/routes/sessions/delete.test.ts +4 -0
  168. package/src/shared/lib/chat/ask-form/index.ts +39 -31
  169. package/src/shared/lib/chat/ask-form/labels.ts +46 -0
  170. package/src/shared/lib/chat/ask-form/tokens.ts +11 -2
  171. package/src/shared/lib/chat/ask-form.test.ts +57 -0
  172. package/src/shared/lib/chat/ask-questions.ts +31 -0
  173. package/src/shared/lib/chat/composer/history.test.ts +67 -0
  174. package/src/shared/lib/chat/composer/history.ts +61 -0
  175. package/src/shared/lib/chat/irc-notice.ts +42 -0
  176. package/src/shared/lib/chat/timeline/fork-anchor.test.ts +2 -0
  177. package/src/shared/lib/chat/todo/parser.ts +6 -4
  178. package/src/shared/lib/fs/archive.test.ts +101 -0
  179. package/src/shared/lib/fs/archive.ts +191 -0
  180. package/src/shared/lib/fs/clipboard.test.ts +32 -7
  181. package/src/shared/lib/fs/clipboard.ts +59 -14
  182. package/src/shared/lib/fs/csv.test.ts +50 -0
  183. package/src/shared/lib/fs/csv.ts +40 -0
  184. package/src/shared/lib/fs/document-kind.test.ts +13 -6
  185. package/src/shared/lib/fs/document-kind.ts +18 -6
  186. package/src/shared/lib/fs/file-kind-classify.test.ts +4 -0
  187. package/src/shared/lib/fs/file-kind.test.ts +2 -7
  188. package/src/shared/lib/fs/file-kind.ts +4 -2
  189. package/src/shared/lib/fs/grid-edit.test.ts +24 -0
  190. package/src/shared/lib/fs/grid-edit.ts +29 -0
  191. package/src/shared/lib/fs/grid-selection.test.ts +137 -0
  192. package/src/shared/lib/fs/grid-selection.ts +138 -0
  193. package/src/shared/lib/harness/brand-color.ts +5 -0
  194. package/src/shared/lib/harness/catalog.test.ts +22 -0
  195. package/src/shared/lib/harness/catalog.ts +12 -1
  196. package/src/shared/lib/harness/config-fields.test.ts +25 -0
  197. package/src/shared/lib/harness/config-fields.ts +33 -3
  198. package/src/shared/lib/harness/dialects.ts +1 -1
  199. package/src/shared/lib/harness/mcp-facts.ts +4 -0
  200. package/src/shared/lib/harness/provider-contracts.ts +109 -0
  201. package/src/shared/lib/harness/provider-facts.test.ts +2 -1
  202. package/src/shared/lib/harness/provider-facts.ts +24 -115
  203. package/src/shared/lib/harness/provider-lookup.ts +37 -0
  204. package/src/shared/lib/harness/providers.ts +2 -1
  205. package/src/shared/lib/harness/updates.ts +10 -0
  206. package/src/shared/lib/omp/session/mapper.test.ts +31 -0
  207. package/src/shared/lib/omp/session/mapper.ts +17 -0
  208. package/src/shared/lib/omp/session/messages-map-notice.test.ts +68 -0
  209. package/src/shared/lib/omp/session/messages-map.test.ts +1 -35
  210. package/src/shared/lib/omp/session/messages-map.ts +17 -0
  211. package/src/shared/lib/omp/session/wait.ts +10 -1
  212. package/src/shared/lib/settings/sections.ts +8 -4
  213. package/src/shared/lib/ui/picker-filter.test.ts +23 -0
  214. package/src/shared/lib/ui/picker-filter.ts +20 -0
  215. package/src/shared/types/chat.ts +4 -0
  216. package/src/shared/types/harness.ts +5 -1
  217. package/src/shared/types/omp/session.ts +18 -0
  218. package/src/shared/types/settings/state.ts +6 -6
  219. package/src/shared/types/todo.ts +3 -1
  220. package/src/test-support/file-actions.test-util.ts +4 -0
  221. package/src/test-support/file-archive.test-util.ts +41 -0
  222. package/src/test-support/file-clipboard.test-util.ts +9 -6
  223. package/src/test-support/harness-table.test-util.ts +2 -0
  224. package/src/test-support/preload.ts +46 -0
  225. package/dist/client/static/js/index-81f7berm.css +0 -1
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The diff panel's read is keyed on the staged flag, not only on the path.
8
+ *
9
+ * One file can be open as two tabs — its staged diff and its unstaged diff —
10
+ * and the editor mounts ONE `DiffPanel` for whichever tab is active, so the
11
+ * second tab arrives as a prop change on the same hook instance. When the
12
+ * fetch was keyed on the path alone, that second tab kept the first tab's
13
+ * patch on screen: the reported "staged and unstaged diffs are identical".
14
+ */
15
+
16
+ import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
17
+ import { Window } from 'happy-dom';
18
+ import { h, render } from 'preact';
19
+ import { act } from 'preact/test-utils';
20
+ import { useFileDiff } from '@/client/hooks/workspace/diff-file';
21
+ import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
22
+
23
+ const originalFetch = globalThis.fetch;
24
+ let requested: string[] = [];
25
+
26
+ function Probe({ isStaged }: { isStaged: boolean }) {
27
+ useFileDiff({ filePath: 'src/a.ts', status: 'M', isStaged, repo: '.' });
28
+ return null;
29
+ }
30
+
31
+ beforeAll(() => {
32
+ installDomGlobals(new Window({ url: 'http://localhost' }));
33
+ });
34
+
35
+ afterAll(() => {
36
+ restoreDomGlobals();
37
+ globalThis.fetch = originalFetch;
38
+ });
39
+
40
+ beforeEach(() => {
41
+ requested = [];
42
+ globalThis.fetch = (async (input: RequestInfo | URL) => {
43
+ const url = String(input);
44
+ requested.push(url);
45
+ const staged = new URL(url, 'http://localhost').searchParams.get('staged') === '1';
46
+ return new Response(
47
+ JSON.stringify({ success: true, diff: '', oldContent: '', newContent: '', status: 'M', staged }),
48
+ { headers: { 'content-type': 'application/json' } },
49
+ );
50
+ }) as typeof fetch;
51
+ });
52
+
53
+ describe('useFileDiff', () => {
54
+ test('re-reads when the same path is opened as the other staged view', async () => {
55
+ const container = document.createElement('div');
56
+ await act(async () => {
57
+ render(h(Probe, { isStaged: false }), container);
58
+ });
59
+ expect(requested).toHaveLength(1);
60
+ expect(requested[0]).toContain('staged=0');
61
+
62
+ await act(async () => {
63
+ render(h(Probe, { isStaged: true }), container);
64
+ });
65
+ expect(requested).toHaveLength(2);
66
+ expect(requested[1]).toContain('staged=1');
67
+
68
+ await act(async () => {
69
+ render(null, container);
70
+ });
71
+ });
72
+ });
@@ -111,16 +111,22 @@ export function useFileDiff({ filePath, status, isStaged, repo, root }: UseFileD
111
111
  }
112
112
  }, [filePath, repo, root]);
113
113
 
114
+ /**
115
+ * The read is keyed on the pair the caller asked for, not only on the path.
116
+ *
117
+ * One file can be open twice — its staged diff and its unstaged diff — and
118
+ * the two tabs share one mounted panel with only the props changing. Keying
119
+ * the fetch on `fetchDiff` alone (path/repo/root) left the previous read on
120
+ * screen, so the second tab showed the first tab's patch. The refs are
121
+ * seeded first: `fetchDiff` reads them, and the state copies lag a render.
122
+ */
114
123
  useEffect(() => {
115
- setCurrentStatus(status);
116
- setCurrentStaged(isStaged);
117
124
  stagedRef.current = isStaged;
118
125
  statusRef.current = status;
119
- }, [status, isStaged, filePath]);
120
-
121
- useEffect(() => {
126
+ setCurrentStatus(status);
127
+ setCurrentStaged(isStaged);
122
128
  void fetchDiff();
123
- }, [fetchDiff]);
129
+ }, [fetchDiff, isStaged, status]);
124
130
 
125
131
  const adoptState = useCallback((next: { staged?: boolean; status?: string }) => {
126
132
  if (typeof next.staged === 'boolean') {
@@ -0,0 +1,220 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The Files panel's archive dialogs: the draft a modal edits, the server call
8
+ * it submits, and the format list this machine can actually write.
9
+ *
10
+ * The format list is READ from `/api/fs/archive` rather than hardcoded, because
11
+ * `rar` and `7z` are binaries the machine may not have and a dialog that offers
12
+ * them would only produce a refusal after the user typed a name. When the read
13
+ * fails the full list is still offered — the server's own refusal is the honest
14
+ * answer, and hiding every format because one probe failed is worse.
15
+ *
16
+ * A refusal keeps the dialog OPEN with the server's words beside the field,
17
+ * exactly like Create and Rename: "already exists" is a message about what to
18
+ * type next, not a notification about something that happened.
19
+ */
20
+
21
+ import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
22
+ import { useFetcher } from '@/client/lib/router/fetcher';
23
+ import type { ClipboardTarget } from '@/client/hooks/workspace/file-clipboard';
24
+ import {
25
+ COMPRESS_FORMATS,
26
+ defaultArchiveDestination,
27
+ defaultArchiveName,
28
+ defaultExtractDestination,
29
+ type ArchiveFormat,
30
+ } from '@/shared/lib/fs/archive';
31
+
32
+ /** What the capability route answers. */
33
+ interface ArchiveFormatAvailability {
34
+ id: ArchiveFormat;
35
+ label: string;
36
+ extension: string;
37
+ available: boolean;
38
+ tool: string;
39
+ }
40
+
41
+ interface ArchiveResponse {
42
+ success?: boolean;
43
+ path?: string;
44
+ error?: string;
45
+ }
46
+
47
+ export interface CompressDraft {
48
+ entries: ClipboardTarget[];
49
+ format: ArchiveFormat;
50
+ name: string;
51
+ destination: string;
52
+ }
53
+
54
+ export interface ExtractDraft {
55
+ archive: ClipboardTarget;
56
+ destination: string;
57
+ }
58
+
59
+ export interface FileArchiveDeps {
60
+ rootPath?: string;
61
+ repo: string;
62
+ /** Fired after an archive landed or a folder was unpacked. */
63
+ onComplete: () => void;
64
+ }
65
+
66
+ export interface FileArchive {
67
+ formats: ArchiveFormatAvailability[];
68
+ compress: CompressDraft | null;
69
+ extract: ExtractDraft | null;
70
+ /** The server's refusal for the open dialog. */
71
+ error: string | null;
72
+ busy: boolean;
73
+ openCompress: (entries: readonly ClipboardTarget[]) => void;
74
+ openExtract: (archive: ClipboardTarget) => void;
75
+ setFormat: (format: ArchiveFormat) => void;
76
+ setName: (name: string) => void;
77
+ setCompressDestination: (destination: string) => void;
78
+ setExtractDestination: (destination: string) => void;
79
+ cancel: () => void;
80
+ submitCompress: () => void;
81
+ submitExtract: () => void;
82
+ }
83
+
84
+ const FALLBACK_FORMATS: ArchiveFormatAvailability[] = COMPRESS_FORMATS.map((entry) => ({
85
+ ...entry,
86
+ available: true,
87
+ tool: entry.id,
88
+ }));
89
+
90
+ export function useFileArchive({ rootPath, repo, onComplete }: FileArchiveDeps): FileArchive {
91
+ const fetcher = useFetcher<ArchiveResponse>();
92
+ const [formats, setFormats] = useState<ArchiveFormatAvailability[]>(FALLBACK_FORMATS);
93
+ const [compress, setCompress] = useState<CompressDraft | null>(null);
94
+ const [extract, setExtract] = useState<ExtractDraft | null>(null);
95
+ const [error, setError] = useState<string | null>(null);
96
+ /** Whether the response in flight came from the compress or extract dialog. */
97
+ const pendingRef = useRef<'compress' | 'extract' | null>(null);
98
+ /** The response already acted on; the fetcher keeps `data` until the next call. */
99
+ const handledRef = useRef<ArchiveResponse | null>(null);
100
+
101
+ useEffect(() => {
102
+ let cancelled = false;
103
+ void (async () => {
104
+ try {
105
+ const res = await fetch('/api/fs/archive');
106
+ const body = (await res.json()) as { formats?: ArchiveFormatAvailability[] };
107
+ if (!cancelled && Array.isArray(body.formats) && body.formats.length > 0) setFormats(body.formats);
108
+ } catch {
109
+ // Keep the fallback list; the server's refusal names a missing tool.
110
+ }
111
+ })();
112
+ return () => {
113
+ cancelled = true;
114
+ };
115
+ }, []);
116
+
117
+ useEffect(() => {
118
+ if (fetcher.state !== 'idle' || !fetcher.data) return;
119
+ const data = fetcher.data;
120
+ if (handledRef.current === data) return;
121
+ handledRef.current = data;
122
+ const pending = pendingRef.current;
123
+ pendingRef.current = null;
124
+ if (data.success) {
125
+ setError(null);
126
+ if (pending === 'compress') setCompress(null);
127
+ if (pending === 'extract') setExtract(null);
128
+ onComplete();
129
+ } else if (pending) {
130
+ setError(data.error || 'The archive request failed');
131
+ }
132
+ // eslint-disable-next-line react-hooks/exhaustive-deps
133
+ }, [fetcher.state, fetcher.data]);
134
+
135
+ const post = useCallback((fields: Record<string, string>) => {
136
+ const formData = new FormData();
137
+ for (const [key, value] of Object.entries(fields)) formData.append(key, value);
138
+ if (repo && repo !== '.') formData.append('repo', repo);
139
+ if (rootPath) formData.append('root', rootPath);
140
+ void fetcher.submit(formData, { method: 'post', action: '/api/fs/action' });
141
+ }, [fetcher, repo, rootPath]);
142
+
143
+ const openCompress = useCallback((entries: readonly ClipboardTarget[]) => {
144
+ if (entries.length === 0) return;
145
+ // Zip first when the machine can write it, otherwise the first writer it
146
+ // has: a dialog that opens on a format the server cannot run is a refusal
147
+ // the user has to discover by submitting.
148
+ const format = formats.find((option) => option.id === 'zip' && option.available)?.id
149
+ ?? formats.find((option) => option.available)?.id
150
+ ?? 'zip';
151
+ setError(null);
152
+ setExtract(null);
153
+ setCompress({
154
+ entries: entries.map(({ path, name, kind }) => ({ path, name, kind })),
155
+ format,
156
+ name: defaultArchiveName(entries, format),
157
+ destination: defaultArchiveDestination(entries),
158
+ });
159
+ }, [formats]);
160
+
161
+ const openExtract = useCallback((archive: ClipboardTarget) => {
162
+ setError(null);
163
+ setCompress(null);
164
+ setExtract({ archive, destination: defaultExtractDestination(archive.path) });
165
+ }, []);
166
+
167
+ const setFormat = useCallback((format: ArchiveFormat) => {
168
+ setCompress((current) => {
169
+ if (!current) return current;
170
+ // Only rewrite the name when it is still the one the dialog proposed;
171
+ // a name the user typed is theirs, even if the extension disagrees.
172
+ const proposed = defaultArchiveName(current.entries, current.format);
173
+ const name = current.name === proposed ? defaultArchiveName(current.entries, format) : current.name;
174
+ return { ...current, format, name };
175
+ });
176
+ }, []);
177
+
178
+ return {
179
+ formats,
180
+ compress,
181
+ extract,
182
+ error,
183
+ busy: fetcher.state !== 'idle',
184
+ openCompress,
185
+ openExtract,
186
+ setFormat,
187
+ setName: (name) => setCompress((current) => current && { ...current, name }),
188
+ setCompressDestination: (destination) => setCompress((current) => current && { ...current, destination }),
189
+ setExtractDestination: (destination) => setExtract((current) => current && { ...current, destination }),
190
+ cancel: () => {
191
+ setCompress(null);
192
+ setExtract(null);
193
+ setError(null);
194
+ },
195
+ submitCompress: () => {
196
+ const draft = compress;
197
+ if (!draft) return;
198
+ pendingRef.current = 'compress';
199
+ setError(null);
200
+ post({
201
+ actionType: 'compress',
202
+ format: draft.format,
203
+ name: draft.name.trim(),
204
+ destination: draft.destination.trim() || '.',
205
+ paths: JSON.stringify(draft.entries.map((entry) => entry.path)),
206
+ });
207
+ },
208
+ submitExtract: () => {
209
+ const draft = extract;
210
+ if (!draft) return;
211
+ pendingRef.current = 'extract';
212
+ setError(null);
213
+ post({
214
+ actionType: 'extract',
215
+ path: draft.archive.path,
216
+ destination: draft.destination.trim() || '.',
217
+ });
218
+ },
219
+ };
220
+ }
@@ -165,7 +165,7 @@ describe('useFileClipboard', () => {
165
165
  await mountHook();
166
166
 
167
167
  await act(async () => {
168
- mounted?.cut({ path: 'src/a.ts', name: 'a.ts', kind: 'file' });
168
+ mounted?.cut([{ path: 'src/a.ts', name: 'a.ts', kind: 'file' }]);
169
169
  });
170
170
  expect(mounted?.clip?.mode).toBe('cut');
171
171
 
@@ -182,7 +182,7 @@ describe('useFileClipboard', () => {
182
182
  await mountHook();
183
183
 
184
184
  await act(async () => {
185
- mounted?.copy({ path: 'src/a.ts', name: 'a.ts', kind: 'file' });
185
+ mounted?.copy([{ path: 'src/a.ts', name: 'a.ts', kind: 'file' }]);
186
186
  });
187
187
  await act(async () => {
188
188
  mounted?.paste({ path: 'dest', name: 'dest', kind: 'folder' });
@@ -194,13 +194,13 @@ describe('useFileClipboard', () => {
194
194
  await settle();
195
195
 
196
196
  expect(requests.map((request) => request.mode)).toEqual(['copy', 'copy']);
197
- expect(mounted?.clip?.path).toBe('src/a.ts');
197
+ expect(mounted?.clip?.entries[0]?.path).toBe('src/a.ts');
198
198
  });
199
199
 
200
200
  test('a paste of an entry from another listing is refused with the reason', async () => {
201
201
  await mountHook();
202
202
  await act(async () => {
203
- mounted?.copy({ path: 'src/a.ts', name: 'a.ts', kind: 'file' });
203
+ mounted?.copy([{ path: 'src/a.ts', name: 'a.ts', kind: 'file' }]);
204
204
  });
205
205
  // The scope is part of the entry: the same relative path in another
206
206
  // workspace names a different file.
@@ -39,6 +39,10 @@ import {
39
39
  interface TransferResponse {
40
40
  success?: boolean;
41
41
  path?: string;
42
+ /** One entry per pasted source, when a selection was moved. */
43
+ paths?: string[];
44
+ /** Entries that could not land, after the rest of the selection did. */
45
+ failures?: { path: string; error: string }[];
42
46
  error?: string;
43
47
  }
44
48
 
@@ -54,8 +58,9 @@ export interface FileClipboard {
54
58
  busy: boolean;
55
59
  /** The reason a paste onto this target would be refused, or null when legal. */
56
60
  refusalFor: (target: ClipboardTarget) => string | null;
57
- copy: (target: ClipboardTarget) => void;
58
- cut: (target: ClipboardTarget) => void;
61
+ /** Put one row, or a whole selection, on the clipboard. */
62
+ copy: (targets: readonly ClipboardTarget[]) => void;
63
+ cut: (targets: readonly ClipboardTarget[]) => void;
59
64
  duplicate: (target: ClipboardTarget) => void;
60
65
  paste: (target: ClipboardTarget) => void;
61
66
  /**
@@ -109,6 +114,12 @@ export function useFileClipboard({ rootPath, repo, onComplete, onError }: FileCl
109
114
  clearsClipRef.current = false;
110
115
  if (data.success) {
111
116
  if (clearClip) setStoredClip(null);
117
+ // A selection can land partly: the entries that did must be reported as
118
+ // done while the ones that did not are named, or the tree re-read looks
119
+ // like it silently dropped a file.
120
+ if (data.failures?.length) {
121
+ onError(`${data.failures.length} item(s) could not be moved: ${data.failures[0]!.error}`);
122
+ }
112
123
  onComplete();
113
124
  } else {
114
125
  onError(data.error || 'The transfer did not complete');
@@ -136,16 +147,34 @@ export function useFileClipboard({ rootPath, repo, onComplete, onError }: FileCl
136
147
  [],
137
148
  );
138
149
 
150
+ /** Post a paste of `sources`, as repeated form fields the route reads back. */
151
+ const postPaste = useCallback((sources: readonly ClipboardTarget[], target: ClipboardTarget, mode: 'copy' | 'cut', clearsClip: boolean) => {
152
+ const formData = new FormData();
153
+ formData.append('actionType', 'paste');
154
+ formData.append('mode', mode);
155
+ formData.append('targetPath', target.path);
156
+ formData.append('targetKind', target.kind);
157
+ // The route stats each source for its kind; a `sourceKind` field would be
158
+ // a second, unchecked answer to the same question.
159
+ for (const source of sources) formData.append('sourcePath', source.path);
160
+ if (repo && repo !== '.') formData.append('repo', repo);
161
+ if (rootPath) formData.append('root', rootPath);
162
+ clearsClipRef.current = clearsClip;
163
+ void fetcher.submit(formData, { method: 'post', action: '/api/fs/action' });
164
+ }, [fetcher, repo, rootPath]);
165
+
139
166
  return {
140
167
  clip,
141
168
  busy: fetcher.state !== 'idle',
142
169
  refusalFor,
143
170
  moveRefusalFor,
144
- copy: (target) => {
145
- setStoredClip({ scope, path: target.path, name: target.name, kind: target.kind, mode: 'copy' });
171
+ copy: (targets) => {
172
+ if (targets.length === 0) return;
173
+ setStoredClip({ scope, entries: targets.map(({ path, name, kind }) => ({ path, name, kind })), mode: 'copy' });
146
174
  },
147
- cut: (target) => {
148
- setStoredClip({ scope, path: target.path, name: target.name, kind: target.kind, mode: 'cut' });
175
+ cut: (targets) => {
176
+ if (targets.length === 0) return;
177
+ setStoredClip({ scope, entries: targets.map(({ path, name, kind }) => ({ path, name, kind })), mode: 'cut' });
149
178
  },
150
179
  duplicate: (target) => {
151
180
  post({ actionType: 'duplicate', path: target.path }, false);
@@ -159,16 +188,7 @@ export function useFileClipboard({ rootPath, repo, onComplete, onError }: FileCl
159
188
  // The same `paste` request a Cut's paste posts: a drag IS a cut, so the
160
189
  // two must not diverge on what moving an entry does. It clears the
161
190
  // clipboard only when the entry it holds was the one moved.
162
- post(
163
- {
164
- actionType: 'paste',
165
- sourcePath: source.path,
166
- mode: 'cut',
167
- targetPath: target.path,
168
- targetKind: target.kind,
169
- },
170
- clip?.path === source.path && clip?.mode === 'cut',
171
- );
191
+ postPaste([source], target, 'cut', Boolean(clip?.mode === 'cut' && clip.entries.some((entry) => entry.path === source.path)));
172
192
  },
173
193
  paste: (target) => {
174
194
  if (!clip) return;
@@ -177,16 +197,7 @@ export function useFileClipboard({ rootPath, repo, onComplete, onError }: FileCl
177
197
  onError(refusal);
178
198
  return;
179
199
  }
180
- post(
181
- {
182
- actionType: 'paste',
183
- sourcePath: clip.path,
184
- mode: clip.mode,
185
- targetPath: target.path,
186
- targetKind: target.kind,
187
- },
188
- clip.mode === 'cut',
189
- );
200
+ postPaste(clip.entries.map(({ path, name, kind }) => ({ path, name, kind })), target, clip.mode, clip.mode === 'cut');
190
201
  },
191
202
  };
192
203
  }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The Files panel's selection arithmetic: what a modifier means, which rows a
8
+ * shift-range covers, and the flat order the range is computed against.
9
+ *
10
+ * The range is the one rule that can be wrong without looking wrong: a range
11
+ * computed from the TREE instead of the DRAWN rows selects collapsed children
12
+ * the user never saw, and a range whose anchor disappeared must not select the
13
+ * whole tree.
14
+ */
15
+
16
+ import { describe, expect, test } from 'bun:test';
17
+ import { rangePaths, selectionGestureOf, visibleEntriesOf, type SelectionNode } from '@/client/hooks/workspace/file-selection';
18
+
19
+ const node = (path: string, type: 'file' | 'folder', children?: SelectionNode[]): SelectionNode => ({
20
+ path,
21
+ name: path.split('/').pop() ?? path,
22
+ type,
23
+ children,
24
+ });
25
+
26
+ describe('selectionGestureOf', () => {
27
+ test('shift ranges, meta or ctrl toggles, otherwise plain', () => {
28
+ expect(selectionGestureOf({ shiftKey: true, metaKey: true, ctrlKey: true })).toBe('range');
29
+ expect(selectionGestureOf({ metaKey: true })).toBe('toggle');
30
+ expect(selectionGestureOf({ ctrlKey: true })).toBe('toggle');
31
+ expect(selectionGestureOf({})).toBe('plain');
32
+ });
33
+ });
34
+
35
+ describe('rangePaths', () => {
36
+ const order = ['a.ts', 'b.ts', 'c.ts', 'd.ts'];
37
+
38
+ test('covers both ends regardless of direction', () => {
39
+ expect(rangePaths(order, 'b.ts', 'd.ts')).toEqual(['b.ts', 'c.ts', 'd.ts']);
40
+ expect(rangePaths(order, 'd.ts', 'b.ts')).toEqual(['b.ts', 'c.ts', 'd.ts']);
41
+ });
42
+
43
+ test('falls back to the target when the anchor is gone from the visible rows', () => {
44
+ // A collapsed folder's anchor is no longer drawn; selecting the rest of the
45
+ // tree because of that would be worse than selecting only the click.
46
+ expect(rangePaths(order, 'gone.ts', 'c.ts')).toEqual(['c.ts']);
47
+ expect(rangePaths(order, null, 'c.ts')).toEqual(['c.ts']);
48
+ });
49
+ });
50
+
51
+ describe('visibleEntriesOf', () => {
52
+ const tree: SelectionNode[] = [
53
+ node('src', 'folder', [node('src/a.ts', 'file'), node('src/deep', 'folder', [node('src/deep/b.ts', 'file')])]),
54
+ node('readme.md', 'file'),
55
+ ];
56
+
57
+ test('an open folder contributes its children immediately after it', () => {
58
+ const entries = visibleEntriesOf(tree, (entry) => entry.type === 'folder');
59
+ expect(entries.map((entry) => entry.path)).toEqual(['src', 'src/a.ts', 'src/deep', 'src/deep/b.ts', 'readme.md']);
60
+ expect(entries.find((entry) => entry.path === 'src/deep/b.ts')?.depth).toBe(2);
61
+ });
62
+
63
+ test('a closed folder contributes only itself', () => {
64
+ const entries = visibleEntriesOf(tree, () => false);
65
+ expect(entries.map((entry) => entry.path)).toEqual(['src', 'readme.md']);
66
+ });
67
+ });
@@ -0,0 +1,156 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The Files panel's MULTI-selection: which rows are marked, and what a click on
8
+ * one means.
9
+ *
10
+ * The panel opens files, so the click has to do two jobs at once — the first
11
+ * click selects AND opens, a shift-click selects a block without opening, and a
12
+ * meta/ctrl-click adds or removes one row. That split lives here rather than in
13
+ * `TreeItem` because the range needs the VISIBLE row order, which is the
14
+ * panel's tree, not the row's own state.
15
+ *
16
+ * The anchor is deliberately sticky: shift-clicking a third time extends from
17
+ * the same row the plain click started at, which is what every file manager
18
+ * does. A plain click moves it; a toggle moves it too, so the next shift range
19
+ * starts where the user last touched.
20
+ */
21
+
22
+ import { useCallback, useMemo, useState } from 'preact/hooks';
23
+
24
+ /** What a click with the platform modifiers means. */
25
+ export type SelectionGesture = 'plain' | 'range' | 'toggle';
26
+
27
+ /** The gesture a click carries: shift ranges, meta/ctrl toggles, else plain. */
28
+ export function selectionGestureOf(event: {
29
+ shiftKey?: boolean;
30
+ metaKey?: boolean;
31
+ ctrlKey?: boolean;
32
+ }): SelectionGesture {
33
+ if (event.shiftKey) return 'range';
34
+ if (event.metaKey || event.ctrlKey) return 'toggle';
35
+ return 'plain';
36
+ }
37
+
38
+ /**
39
+ * The paths between `anchor` and `target` in visible order, inclusive.
40
+ *
41
+ * Empty when either end is not visible: the anchor can survive a folder being
42
+ * collapsed, and a range across rows that are no longer drawn would select
43
+ * entries the user cannot see.
44
+ */
45
+ export function rangePaths(
46
+ order: readonly string[],
47
+ anchor: string | null,
48
+ target: string,
49
+ ): string[] {
50
+ if (!anchor) return [target];
51
+ const from = order.indexOf(anchor);
52
+ const to = order.indexOf(target);
53
+ if (from === -1 || to === -1) return [target];
54
+ const start = Math.min(from, to);
55
+ const end = Math.max(from, to);
56
+ return order.slice(start, end + 1);
57
+ }
58
+
59
+ export interface FileSelection {
60
+ /** The marked paths. Stable identity until the selection changes. */
61
+ selected: ReadonlySet<string>;
62
+ /** How many rows are marked — the menus' labels read it. */
63
+ count: number;
64
+ /** The row a shift-click ranges from. */
65
+ anchor: string | null;
66
+ gestureOf: (event: { shiftKey?: boolean; metaKey?: boolean; ctrlKey?: boolean }) => SelectionGesture;
67
+ /** Apply a gesture to `path`; a plain click replaces the selection. */
68
+ applyGesture: (gesture: SelectionGesture, path: string) => void;
69
+ /** Right-click: keep a selection the row belongs to, else select just it. */
70
+ ensureForContext: (path: string) => void;
71
+ /** Drop the whole selection — a repo switch, or Escape. */
72
+ clear: () => void;
73
+ }
74
+
75
+ export function useFileSelection(order: readonly string[]): FileSelection {
76
+ const [selected, setSelected] = useState<ReadonlySet<string>>(() => new Set());
77
+ const [anchor, setAnchor] = useState<string | null>(null);
78
+
79
+ const applyGesture = useCallback((gesture: SelectionGesture, path: string) => {
80
+ if (gesture === 'range') {
81
+ setSelected(new Set(rangePaths(order, anchor, path)));
82
+ return;
83
+ }
84
+ setAnchor(path);
85
+ if (gesture === 'toggle') {
86
+ setSelected((previous) => {
87
+ const next = new Set(previous);
88
+ if (next.has(path)) next.delete(path);
89
+ else next.add(path);
90
+ return next;
91
+ });
92
+ return;
93
+ }
94
+ setSelected(new Set([path]));
95
+ }, [anchor, order]);
96
+
97
+ const ensureForContext = useCallback((path: string) => {
98
+ setSelected((previous) => {
99
+ if (previous.has(path)) return previous;
100
+ return new Set([path]);
101
+ });
102
+ setAnchor(path);
103
+ }, []);
104
+
105
+ const clear = useCallback(() => {
106
+ setSelected(new Set());
107
+ setAnchor(null);
108
+ }, []);
109
+
110
+ const gestureOf = useCallback(selectionGestureOf, []);
111
+
112
+ return useMemo(
113
+ () => ({ selected, count: selected.size, anchor, gestureOf, applyGesture, ensureForContext, clear }),
114
+ [selected, anchor, gestureOf, applyGesture, ensureForContext, clear],
115
+ );
116
+ }
117
+
118
+ /** A path's place in the visible tree, with the facts a batch action needs. */
119
+ export interface SelectionEntry {
120
+ path: string;
121
+ name: string;
122
+ kind: 'file' | 'folder';
123
+ /** Nesting depth, for the flat order the range is computed against. */
124
+ depth: number;
125
+ }
126
+
127
+ /**
128
+ * The rows the panel is DRAWING, in order — the coordinate space of a
129
+ * shift-range.
130
+ *
131
+ * A folder contributes itself and, when open, its children immediately after
132
+ * it: that is the DOM order the rows are read in. `isOpen` is the panel's own
133
+ * expansion set plus the transient `forceExpanded` a chain row carries.
134
+ */
135
+ export interface SelectionNode {
136
+ path: string;
137
+ name: string;
138
+ type: 'file' | 'folder';
139
+ children?: readonly SelectionNode[];
140
+ forceExpanded?: boolean;
141
+ }
142
+
143
+ export function visibleEntriesOf(
144
+ nodes: readonly SelectionNode[],
145
+ isOpen: (node: SelectionNode) => boolean,
146
+ depth = 0,
147
+ out: SelectionEntry[] = [],
148
+ ): SelectionEntry[] {
149
+ for (const node of nodes) {
150
+ out.push({ path: node.path, name: node.name, kind: node.type === 'folder' ? 'folder' : 'file', depth });
151
+ if (node.type === 'folder' && isOpen(node) && Array.isArray(node.children)) {
152
+ visibleEntriesOf(node.children, isOpen, depth + 1, out);
153
+ }
154
+ }
155
+ return out;
156
+ }