@kabmo-labs/kabmo 0.13.0 → 0.14.1
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.
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +319 -319
- package/dist/client/static/js/index-bnrrw242.css +1 -0
- package/dist/client/static/js/{index-vzb1qz51.js → index-w6jx0h0p.js} +780 -780
- package/marketplace/plugins/command-runner/package.json +2 -2
- package/marketplace/plugins/command-runner/src/server/ring.test.ts +41 -0
- package/marketplace/plugins/command-runner/src/server/run-types.ts +6 -0
- package/marketplace/plugins/command-runner/src/server/runner-harness.ts +69 -0
- package/marketplace/plugins/command-runner/src/server/runner-path.test.ts +127 -0
- package/marketplace/plugins/command-runner/src/server/runner.test.ts +2 -72
- package/marketplace/plugins/command-runner/src/server/runner.ts +31 -3
- package/package.json +2 -2
- package/src/client/components/common/batch-selection/BatchActionBar.test.ts +42 -5
- package/src/client/components/common/batch-selection/BatchActionBar.tsx +57 -14
- package/src/client/components/common/code-surface/extensions.ts +7 -1
- package/src/client/components/common/code-surface/fold-placeholder.ts +43 -0
- package/src/client/components/common/session-row/StatusSlot.tsx +133 -0
- package/src/client/components/common/session-row/TrailingSlot.tsx +84 -0
- package/src/client/components/common/session-row/index.tsx +215 -0
- package/src/client/components/common/session-row/session-row.test.tsx +154 -0
- package/src/client/components/layout/session-sidebar/ClassicView.tsx +121 -0
- package/src/client/components/layout/session-sidebar/SessionsView.tsx +99 -112
- package/src/client/components/layout/session-sidebar/Toolbar.tsx +2 -2
- package/src/client/components/layout/session-sidebar/modern/ActionRow.tsx +149 -0
- package/src/client/components/layout/session-sidebar/modern/FilterChips.tsx +150 -0
- package/src/client/components/layout/session-sidebar/modern/SearchField.tsx +75 -0
- package/src/client/components/layout/session-sidebar/modern/Skeleton.tsx +56 -0
- package/src/client/components/layout/session-sidebar/modern/WorkspaceHeader.tsx +193 -0
- package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +333 -0
- package/src/client/components/layout/session-sidebar/modern/index.tsx +147 -0
- package/src/client/components/layout/session-sidebar/modern/workspace-header.test.tsx +111 -0
- package/src/client/components/layout/session-sidebar/variant-props.ts +73 -0
- package/src/client/components/mobile/SessionSidebar.tsx +137 -146
- package/src/client/components/mobile/mobile-session-sidebar/ClassicDrawer.tsx +140 -0
- package/src/client/components/mobile/mobile-session-sidebar/Toolbar.tsx +3 -3
- package/src/client/components/mobile/mobile-session-sidebar/use-drawer.ts +104 -0
- package/src/client/components/settings/Modal.tsx +5 -27
- package/src/client/components/settings/Sidebar.tsx +1 -1
- package/src/client/components/settings/apply-side-effects.test.ts +102 -0
- package/src/client/components/settings/apply-side-effects.ts +90 -0
- package/src/client/components/settings/categories/AppearanceSettings.tsx +11 -1
- package/src/client/components/settings/categories/ChatSettings.test.tsx +121 -0
- package/src/client/components/settings/categories/ChatSettings.tsx +80 -15
- package/src/client/components/settings/categories/appearance-settings/SidebarStyleSection.tsx +157 -0
- package/src/client/components/settings/categories/chat-settings/ComposerStyleSection.tsx +177 -0
- package/src/client/components/settings/categories/chat-settings/LayoutSection.tsx +1 -1
- package/src/client/components/settings/categories/chat-settings/SidebarList.tsx +73 -0
- package/src/client/components/settings/categories/chat-settings/ToolRenderSection.tsx +144 -0
- package/src/client/components/settings/categories/chat-settings/groups.test.ts +42 -0
- package/src/client/components/settings/categories/chat-settings/groups.ts +81 -0
- package/src/client/components/settings/categories/usage-settings/usage-and-picker.test.ts +1 -68
- package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +7 -0
- package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +3 -0
- package/src/client/components/workspace/chat-timeline/MessageItem.tsx +15 -5
- package/src/client/components/workspace/chat-timeline/MessageList.tsx +90 -41
- package/src/client/components/workspace/chat-timeline/SystemNotice.tsx +13 -6
- package/src/client/components/workspace/chat-timeline/ThinkingSection.test.tsx +117 -0
- package/src/client/components/workspace/chat-timeline/ThinkingSection.tsx +117 -75
- package/src/client/components/workspace/chat-timeline/ToolCallingSection.test.ts +28 -8
- package/src/client/components/workspace/chat-timeline/ToolCallingSection.tsx +48 -4
- package/src/client/components/workspace/chat-timeline/TurnRail.tsx +64 -0
- package/src/client/components/workspace/chat-timeline/block-chrome.test.tsx +178 -0
- package/src/client/components/workspace/chat-timeline/chat-input/AttachmentToolbar.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/chat-input/ComposerNotice.tsx +50 -0
- package/src/client/components/workspace/chat-timeline/chat-input/ComposerRows.tsx +178 -0
- package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +102 -122
- package/src/client/components/workspace/chat-timeline/chat-input/props.ts +7 -0
- package/src/client/components/workspace/chat-timeline/chat-input/styles/index.tsx +111 -0
- package/src/client/components/workspace/chat-timeline/chat-input/styles.test.tsx +181 -0
- package/src/client/components/workspace/chat-timeline/index.tsx +3 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/FrameControl.tsx +1 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/approval-gate.test.tsx +31 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/index.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/index.tsx +2 -2
- package/src/client/components/workspace/chat-timeline/tool-renderers/plugin-renderers.ts +6 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ToolBody.tsx +98 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ToolCardShell.tsx +3 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/status-glyph.tsx +56 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/toggle-label.ts +19 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/tool-view.ts +132 -0
- package/src/client/components/workspace/chat-timeline/{ToolCallCard.plugin.test.ts → tool-renderers/styles/Card.plugin.test.ts} +3 -3
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Card.tsx +60 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Compact.tsx +89 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Minimal.tsx +72 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Trace.tsx +84 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/props.ts +22 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/styles.test.tsx +153 -0
- package/src/client/components/workspace/diff-panel/folding.ts +6 -1
- package/src/client/components/workspace/editor/viewers/SvgPreview.tsx +16 -2
- package/src/client/components/workspace/model-dropdown/Item.plugin.test.ts +86 -12
- package/src/client/components/workspace/model-dropdown/Item.tsx +56 -19
- package/src/client/components/workspace/model-dropdown/Panel.tsx +21 -13
- package/src/client/components/workspace/model-dropdown/Section.test.tsx +114 -4
- package/src/client/components/workspace/model-dropdown/Section.tsx +21 -9
- package/src/client/components/workspace/model-dropdown/groups.test.ts +164 -1
- package/src/client/components/workspace/model-dropdown/groups.ts +94 -17
- package/src/client/components/workspace/model-dropdown/index.tsx +27 -19
- package/src/client/data/settings/omp-schema.json +151 -6
- package/src/client/hooks/chat/composer/style.ts +66 -0
- package/src/client/hooks/chat/timeline/row-window-reveal.ts +130 -0
- package/src/client/hooks/chat/timeline/row-window.test.ts +35 -0
- package/src/client/hooks/chat/timeline/row-window.ts +59 -133
- package/src/client/hooks/chat/timeline/thinking-open.ts +67 -0
- package/src/client/hooks/chat/timeline/tool-render-mode.ts +65 -0
- package/src/client/hooks/settings/providers/login.ts +11 -2
- package/src/client/hooks/ui/window-event.ts +20 -4
- package/src/client/hooks/workspace/batch-archive-run.ts +47 -0
- package/src/client/hooks/workspace/batch-archive.test.ts +59 -0
- package/src/client/hooks/workspace/batch-archive.ts +78 -0
- package/src/client/hooks/workspace/sidebar-batch.ts +95 -0
- package/src/client/hooks/workspace/sidebar-style.ts +66 -0
- package/src/client/tailwind.css +111 -0
- package/src/server/index.ts +10 -134
- package/src/server/lib/lifecycle/after-listen.ts +178 -0
- package/src/server/lib/lifecycle/rejection-guard.test.ts +79 -0
- package/src/server/lib/lifecycle/rejection-guard.ts +74 -0
- package/src/server/lib/lifecycle/tls.test.ts +7 -7
- package/src/server/lib/omp/rpc/frame-fold-frames.test.ts +131 -0
- package/src/server/lib/omp/rpc/frame-fold-host.test-util.ts +46 -0
- package/src/server/lib/omp/rpc/frame-fold.test.ts +1 -100
- package/src/server/lib/omp/rpc/frame-fold.ts +13 -0
- package/src/server/lib/plugins/background.server.ts +17 -1
- package/src/server/lib/plugins/background.test.ts +33 -0
- package/src/server/routes/sessions/batch-archive.test.ts +113 -0
- package/src/server/routes/sessions/batch-archive.ts +80 -0
- package/src/server/routes/sessions/index.ts +4 -0
- package/src/shared/lib/chat/composer/style.test.ts +126 -0
- package/src/shared/lib/chat/composer/style.ts +171 -0
- package/src/shared/lib/chat/composer/tui-only.test.ts +3 -0
- package/src/shared/lib/chat/composer/tui-only.ts +5 -0
- package/src/shared/lib/chat/timeline/block-chrome.ts +129 -0
- package/src/shared/lib/chat/timeline/tool-render-mode.test.ts +51 -0
- package/src/shared/lib/chat/timeline/tool-render-mode.ts +74 -0
- package/src/shared/lib/chat/timeline/trace-groups.test.ts +132 -0
- package/src/shared/lib/chat/timeline/trace-groups.ts +95 -0
- package/src/shared/lib/settings/defaults.ts +6 -0
- package/src/shared/lib/workspace/session-focus.test.ts +93 -0
- package/src/shared/lib/workspace/session-focus.ts +71 -0
- package/src/shared/lib/workspace/session-state/store.ts +5 -0
- package/src/shared/lib/workspace/sidebar-filter-row.test.ts +73 -0
- package/src/shared/lib/workspace/sidebar-filter-row.ts +86 -0
- package/src/shared/lib/workspace/sidebar-style.test.ts +58 -0
- package/src/shared/lib/workspace/sidebar-style.ts +86 -0
- package/src/shared/types/omp/agent.ts +6 -0
- package/src/shared/types/settings/state.ts +31 -0
- package/dist/client/static/js/index-68w8h33q.css +0 -1
- package/src/client/components/workspace/chat-timeline/ToolCallCard.tsx +0 -194
|
@@ -30,26 +30,9 @@
|
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
32
|
import type { RefObject } from 'preact/compat';
|
|
33
|
-
import {
|
|
34
|
-
import { layoutTimelineRows, rowIndex,
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* How long a reveal keeps correcting after it first moves the viewport.
|
|
38
|
-
*
|
|
39
|
-
* Wall-clock rather than a frame count: the rows it just mounted are measured on
|
|
40
|
-
* later commits (a tool card's output arrives through a deferred highlight, an
|
|
41
|
-
* image decodes), and every measurement above the target moves it. Measured
|
|
42
|
-
* here, a fixed short window left a jump to the newest turn 300 px short of the
|
|
43
|
-
* tail — outside the follow-mode threshold, so the stream stopped following
|
|
44
|
-
* after the jump.
|
|
45
|
-
*/
|
|
46
|
-
const REVEAL_SETTLE_MS = 3000;
|
|
47
|
-
|
|
48
|
-
/** Consecutive still frames that end a reveal early — the layout has settled. */
|
|
49
|
-
const REVEAL_STILL_FRAMES = 10;
|
|
50
|
-
|
|
51
|
-
/** How long a reveal waits for a row the pager is still bringing in. */
|
|
52
|
-
const REVEAL_DEADLINE_MS = 3000;
|
|
33
|
+
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'preact/hooks';
|
|
34
|
+
import { layoutTimelineRows, rowIndex, timelineWindow } from '@/shared/lib/chat/timeline/row-window';
|
|
35
|
+
import { createRevealRow, writeScrollTop } from '@/client/hooks/chat/timeline/row-window-reveal';
|
|
53
36
|
|
|
54
37
|
/** A measurement is only adopted when it differs by more than this. */
|
|
55
38
|
const MEASURE_EPSILON_PX = 0.5;
|
|
@@ -58,23 +41,6 @@ const MEASURE_EPSILON_PX = 0.5;
|
|
|
58
41
|
* `FOLLOW_BOTTOM_THRESHOLD_PX` in `scroll.ts`: below it, follow mode is on. */
|
|
59
42
|
const TAIL_ANCHOR_PX = 100;
|
|
60
43
|
|
|
61
|
-
/**
|
|
62
|
-
* Write a scroll position that is meant to LAND, not to animate.
|
|
63
|
-
*
|
|
64
|
-
* The timeline container carries Tailwind's `scroll-smooth`, and CSS
|
|
65
|
-
* `scroll-behavior` applies to a `scrollTop` ASSIGNMENT too (CSSOM-View:
|
|
66
|
-
* the setter scrolls with `behavior: auto`, which resolves to the element's
|
|
67
|
-
* computed `scroll-behavior`). So `el.scrollTop = target` started a smooth
|
|
68
|
-
* animation, the reveal's correction loop re-targeted it every frame, and the
|
|
69
|
-
* jump was left mid-flight wherever the loop stopped — measured on a
|
|
70
|
-
* 240-message session: the trace walked 9 804 → 6 504 px and stayed there.
|
|
71
|
-
* `behavior: 'instant'` is the one value CSS cannot override.
|
|
72
|
-
*/
|
|
73
|
-
function writeScrollTop(el: HTMLElement, top: number): void {
|
|
74
|
-
if (Math.abs(el.scrollTop - top) <= 1) return;
|
|
75
|
-
el.scrollTo({ top, behavior: 'instant' });
|
|
76
|
-
}
|
|
77
|
-
|
|
78
44
|
export interface TimelineRowWindow {
|
|
79
45
|
/** First mounted row index (inclusive). */
|
|
80
46
|
start: number;
|
|
@@ -126,12 +92,6 @@ export function useTimelineRowWindow({ ids, scrollRef, scrollNode, enabled = tru
|
|
|
126
92
|
const [viewport, setViewport] = useState(0);
|
|
127
93
|
// The scroll container. STATE, not the ref: see `scrollNode` in the deps.
|
|
128
94
|
const element = scrollNode;
|
|
129
|
-
// The row at the top of the viewport, captured before a re-layout so the
|
|
130
|
-
// reader's position can be restored after it. `scrollTop` is recorded with it
|
|
131
|
-
// so a restore can tell that SOMETHING ELSE moved the viewport in between —
|
|
132
|
-
// the opening jump to the tail, a rail jump, the follow pin — and stand down
|
|
133
|
-
// instead of writing the reader's old position back over it.
|
|
134
|
-
const anchorRef = useRef<{ id: string; offset: number; scrollTop: number; tail: boolean } | null>(null);
|
|
135
95
|
/**
|
|
136
96
|
* The viewport belongs to the BOTTOM until the reader scrolls away.
|
|
137
97
|
*
|
|
@@ -157,6 +117,12 @@ export function useTimelineRowWindow({ ids, scrollRef, scrollNode, enabled = tru
|
|
|
157
117
|
viewportRef.current = viewport;
|
|
158
118
|
// Bumped per reveal so a superseded one stops writing.
|
|
159
119
|
const revealGenerationRef = useRef(0);
|
|
120
|
+
/**
|
|
121
|
+
* A reveal is flying: it re-writes its target every frame until it settles,
|
|
122
|
+
* so a measurement must not correct the position underneath it (the two
|
|
123
|
+
* would trade the viewport). See `adopt`.
|
|
124
|
+
*/
|
|
125
|
+
const revealingRef = useRef(false);
|
|
160
126
|
|
|
161
127
|
const range = useMemo(
|
|
162
128
|
() => (enabled ? timelineWindow(layout, scrollTop, viewport) : { start: 0, end: ids.length, padTop: 0, padBottom: 0 }),
|
|
@@ -220,26 +186,48 @@ export function useTimelineRowWindow({ ids, scrollRef, scrollNode, enabled = tru
|
|
|
220
186
|
return changed;
|
|
221
187
|
};
|
|
222
188
|
const adopt = (): void => {
|
|
189
|
+
// Which row the reader is looking at, under the geometry they are still
|
|
190
|
+
// seeing: `el.scrollTop` has not moved yet (a browser does not adjust it
|
|
191
|
+
// when content above grows), so the OLD layout is the right one to read
|
|
192
|
+
// the anchor from.
|
|
223
193
|
const current = timelineWindow(layout, el.scrollTop, el.clientHeight, 0);
|
|
224
194
|
const anchorId = ids[current.start];
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
195
|
+
// A reader parked at the tail follows the TAIL, not the row that happened
|
|
196
|
+
// to be at the top of the viewport: the rows below it are estimates until
|
|
197
|
+
// they mount, so a row anchor would leave the viewport short of the
|
|
198
|
+
// bottom — measured at 775 px on an opened session, which is past the
|
|
199
|
+
// 100 px threshold that decides whether the stream keeps following.
|
|
200
|
+
// Measured against the PREVIOUS layout, not the DOM: this effect runs
|
|
201
|
+
// after the content already grew, so the DOM's own distance is the
|
|
202
|
+
// growth itself. A reader who was at the bottom before a row was measured
|
|
203
|
+
// taller must stay at the bottom — measured against the DOM they were
|
|
204
|
+
// left 2 080 px short, i.e. outside follow mode.
|
|
205
|
+
const tail = tailPinRef.current
|
|
206
|
+
|| layout.total - scrollTop - (viewport || el.clientHeight) <= TAIL_ANCHOR_PX;
|
|
207
|
+
const offset = anchorId ? el.scrollTop - layout.offsets[current.start] : 0;
|
|
208
|
+
|
|
209
|
+
// The correction is written HERE, against the new layout, not deferred to
|
|
210
|
+
// an effect that runs after this commit. Deferring it is what made an
|
|
211
|
+
// expanded thinking block snap shut: the restore effect for the SAME
|
|
212
|
+
// commit still holds the pre-measurement layout, so it consumed the
|
|
213
|
+
// anchor and wrote the reader's old offset; the next commit then had no
|
|
214
|
+
// anchor left, `scrollTop` was still stale, and the mounted WINDOW moved
|
|
215
|
+
// one row — unmounting and remounting the rows at its edge (measured in
|
|
216
|
+
// the browser: three rows removed and re-added per expand), which throws
|
|
217
|
+
// away every block's local state.
|
|
218
|
+
const next = layoutTimelineRows(ids, measuredRef.current);
|
|
219
|
+
const target = tail || !anchorId
|
|
220
|
+
? Math.max(0, el.scrollHeight - el.clientHeight)
|
|
221
|
+
: next.offsets[rowIndex(ids, anchorId)] + offset;
|
|
222
|
+
tailPinRef.current = tail;
|
|
223
|
+
// A reveal OWNS the viewport until it settles: it re-writes its target
|
|
224
|
+
// every frame, so a correction here would be written back over it and the
|
|
225
|
+
// two would trade the viewport (measured before this guard existed: the
|
|
226
|
+
// reveal and the restore alternated `0` and its target for the whole
|
|
227
|
+
// animation). The measurement is still adopted; only the position waits.
|
|
228
|
+
if (!revealingRef.current) {
|
|
229
|
+
setScrollTop(target);
|
|
230
|
+
writeScrollTop(el, target);
|
|
243
231
|
}
|
|
244
232
|
setVersion((value) => value + 1);
|
|
245
233
|
};
|
|
@@ -266,80 +254,18 @@ export function useTimelineRowWindow({ ids, scrollRef, scrollNode, enabled = tru
|
|
|
266
254
|
|
|
267
255
|
// Restore the reading position after the layout moved under it.
|
|
268
256
|
useLayoutEffect(() => {
|
|
269
|
-
const anchor = anchorRef.current;
|
|
270
|
-
if (!anchor) return;
|
|
271
|
-
anchorRef.current = null;
|
|
272
257
|
const el = element;
|
|
273
|
-
if (!el) return;
|
|
274
|
-
// The
|
|
275
|
-
//
|
|
276
|
-
//
|
|
277
|
-
//
|
|
278
|
-
//
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
// The bottom edge is the anchor. `writeScrollTop`, because the container
|
|
282
|
-
// carries `scroll-smooth`: a bare `scrollTop` assignment animates, and a
|
|
283
|
-
// correction that animates is a correction that arrives late.
|
|
284
|
-
writeScrollTop(el, Math.max(0, el.scrollHeight - el.clientHeight));
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
|
-
const index = rowIndex(ids, anchor.id);
|
|
288
|
-
if (index < 0) return;
|
|
289
|
-
const target = layout.offsets[index] + anchor.offset;
|
|
290
|
-
writeScrollTop(el, target);
|
|
291
|
-
}, [layout, ids, element]);
|
|
258
|
+
if (!el || !tailPinRef.current) return;
|
|
259
|
+
// The tail pin outlives a commit: the rows above it are estimates until
|
|
260
|
+
// they mount, so every measurement can move the bottom by its own error.
|
|
261
|
+
// The bottom edge is the anchor. `writeScrollTop`, because the container
|
|
262
|
+
// carries `scroll-smooth`: a bare `scrollTop` assignment animates, and a
|
|
263
|
+
// correction that animates is a correction that arrives late.
|
|
264
|
+
writeScrollTop(el, Math.max(0, el.scrollHeight - el.clientHeight));
|
|
265
|
+
}, [layout, element]);
|
|
292
266
|
|
|
293
|
-
const revealRow =
|
|
294
|
-
(
|
|
295
|
-
const el = scrollRef.current;
|
|
296
|
-
if (!el) return Promise.resolve();
|
|
297
|
-
const deadline = Date.now() + REVEAL_DEADLINE_MS;
|
|
298
|
-
const settleBy = Date.now() + REVEAL_SETTLE_MS;
|
|
299
|
-
// Supersedes an earlier reveal: two rail clicks in a row must land on the
|
|
300
|
-
// SECOND turn, not race each other to the first one's position.
|
|
301
|
-
const generation = (revealGenerationRef.current += 1);
|
|
302
|
-
return new Promise<void>((resolve) => {
|
|
303
|
-
let stillFrames = 0;
|
|
304
|
-
let lastTop = -1;
|
|
305
|
-
const step = (): void => {
|
|
306
|
-
if (generation !== revealGenerationRef.current) {
|
|
307
|
-
resolve();
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
|
-
const rows = idsRef.current;
|
|
311
|
-
const index = rowIndex(rows, id);
|
|
312
|
-
if (index < 0) {
|
|
313
|
-
// The pager may still be bringing the row in; keep asking until the
|
|
314
|
-
// deadline rather than scrolling to a row that does not exist yet.
|
|
315
|
-
if (Date.now() < deadline) requestAnimationFrame(step);
|
|
316
|
-
else resolve();
|
|
317
|
-
return;
|
|
318
|
-
}
|
|
319
|
-
const current = layoutRef.current;
|
|
320
|
-
const view = el.clientHeight || viewportRef.current;
|
|
321
|
-
// The newest turn is the TAIL, not the centre of the last row: that
|
|
322
|
-
// is what the reader means by jumping there, and it is what lets
|
|
323
|
-
// follow mode re-engage (the scroll handler's threshold is 100 px).
|
|
324
|
-
tailPinRef.current = index === rows.length - 1;
|
|
325
|
-
const target = tailPinRef.current
|
|
326
|
-
? Math.max(0, el.scrollHeight - view)
|
|
327
|
-
: rowScrollOffset(current, index, view);
|
|
328
|
-
writeScrollTop(el, target);
|
|
329
|
-
// A row that was just mounted is measured on a later commit; the
|
|
330
|
-
// corrections land on the MEASURED position rather than the estimate
|
|
331
|
-
// the first write used.
|
|
332
|
-
stillFrames = Math.abs(el.scrollTop - lastTop) <= 1 ? stillFrames + 1 : 0;
|
|
333
|
-
lastTop = el.scrollTop;
|
|
334
|
-
if (stillFrames >= REVEAL_STILL_FRAMES || Date.now() >= settleBy) {
|
|
335
|
-
resolve();
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
|
-
requestAnimationFrame(step);
|
|
339
|
-
};
|
|
340
|
-
requestAnimationFrame(step);
|
|
341
|
-
});
|
|
342
|
-
},
|
|
267
|
+
const revealRow = useMemo(
|
|
268
|
+
() => createRevealRow({ scrollRef, idsRef, layoutRef, viewportRef, tailPinRef, revealingRef, generationRef: revealGenerationRef }),
|
|
343
269
|
[scrollRef],
|
|
344
270
|
);
|
|
345
271
|
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Whether a thinking block is open, remembered per session and per message.
|
|
8
|
+
*
|
|
9
|
+
* The accordion kept this in a plain `useState`, and that was survivable while
|
|
10
|
+
* every row stayed mounted for the life of the session. It is not survivable
|
|
11
|
+
* now: `MessageList` mounts a WINDOW of rows (`useTimelineRowWindow`), so a
|
|
12
|
+
* block is unmounted whenever the window moves past it — which an expand does
|
|
13
|
+
* by itself, because the row it opens grows and the reader parked at the tail
|
|
14
|
+
* follows the new bottom. Measured in the browser on a real session: the click
|
|
15
|
+
* opened the body, the next measurement moved the viewport, the row unmounted
|
|
16
|
+
* and remounted with a fresh `useState(false)`, and the block was closed again
|
|
17
|
+
* — "sometimes it works" being exactly the case where enough content sat below
|
|
18
|
+
* the row to keep it mounted.
|
|
19
|
+
*
|
|
20
|
+
* The tool cards already answer this by persisting their openness per session
|
|
21
|
+
* (`useToolOpenState`), so this is the same rule for the same reason, not a
|
|
22
|
+
* second convention: a block's open state belongs to the conversation.
|
|
23
|
+
*
|
|
24
|
+
* Only a choice the reader MADE is stored. A generating run opens by default,
|
|
25
|
+
* and writing that default per block would put a row in the session blob for
|
|
26
|
+
* every thought the model produced.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { useEffect, useState } from 'preact/hooks';
|
|
30
|
+
import { useSessionState } from '@/client/hooks/workspace/session-state';
|
|
31
|
+
|
|
32
|
+
/** Session slot holding `{ [messageId]: open }` for the blocks the reader toggled. */
|
|
33
|
+
export const THINKING_OPEN_STATE_KEY = 'chat.thinkingOpen';
|
|
34
|
+
|
|
35
|
+
export interface ThinkingOpenState {
|
|
36
|
+
isOpen: boolean;
|
|
37
|
+
toggle: () => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* `messageId` is optional so a block mounted outside a session (a spec, a
|
|
42
|
+
* preview) still toggles — it just cannot remember the choice.
|
|
43
|
+
*/
|
|
44
|
+
export function useThinkingOpenState(
|
|
45
|
+
messageId: string | undefined,
|
|
46
|
+
defaultOpen: boolean,
|
|
47
|
+
isGenerating: boolean,
|
|
48
|
+
): ThinkingOpenState {
|
|
49
|
+
const [stored, setStored] = useSessionState<Record<string, boolean>>(THINKING_OPEN_STATE_KEY, {});
|
|
50
|
+
const [local, setLocal] = useState(defaultOpen || isGenerating);
|
|
51
|
+
const choice = messageId ? stored[messageId] : undefined;
|
|
52
|
+
|
|
53
|
+
// A run that starts streaming opens itself, as it always has. This is the
|
|
54
|
+
// local half on purpose: the stored half is the reader's own choice, and it
|
|
55
|
+
// must not be overwritten by a default.
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (isGenerating) setLocal(true);
|
|
58
|
+
}, [isGenerating]);
|
|
59
|
+
|
|
60
|
+
const isOpen = choice ?? local;
|
|
61
|
+
const toggle = (): void => {
|
|
62
|
+
const next = !isOpen;
|
|
63
|
+
setLocal(next);
|
|
64
|
+
if (messageId) setStored((prev) => ({ ...prev, [messageId]: next }));
|
|
65
|
+
};
|
|
66
|
+
return { isOpen, toggle };
|
|
67
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The one reader/writer of the transcript's tool-render style.
|
|
8
|
+
*
|
|
9
|
+
* Unlike the chat column's width (an attribute CSS reads), the style picks a
|
|
10
|
+
* Preact renderer, so something in JavaScript must re-render when it changes.
|
|
11
|
+
* The settings modal therefore calls `applyToolRenderMode` when the value
|
|
12
|
+
* moves, which announces `omp:tool-render-changed`; `useToolRenderMode` is the
|
|
13
|
+
* single subscriber, and `ToolCallingSection` is the only consumer.
|
|
14
|
+
*
|
|
15
|
+
* The initial value comes from the primed settings snapshot
|
|
16
|
+
* (`primeKabmoSettings` in `main.tsx`), so a mounted timeline draws the stored
|
|
17
|
+
* style on its first render — no flash of the default.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { useState } from 'preact/hooks';
|
|
21
|
+
import { readKabmoSetting } from '@/shared/lib/settings/client';
|
|
22
|
+
import { useKabmoEvent } from '@/client/hooks/ui/window-event';
|
|
23
|
+
import type { SettingsState } from '@/shared/types';
|
|
24
|
+
import {
|
|
25
|
+
resolveToolRenderMode,
|
|
26
|
+
type ToolRenderMode,
|
|
27
|
+
} from '@/shared/lib/chat/timeline/tool-render-mode';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* `app_settings` key holding the style (in the `kabmo_settings` blob).
|
|
31
|
+
*
|
|
32
|
+
* `satisfies keyof SettingsState` because the key and the field name must be
|
|
33
|
+
* the same string for persistence to work at all — renaming the field without
|
|
34
|
+
* this line would leave the setting silently unsaved.
|
|
35
|
+
*/
|
|
36
|
+
export const TOOL_RENDER_SETTING_KEY = 'toolRenderStyle' satisfies keyof SettingsState;
|
|
37
|
+
|
|
38
|
+
/** Window event carrying the resolved mode as `detail`. */
|
|
39
|
+
export const TOOL_RENDER_CHANGED_EVENT = 'omp:tool-render-changed';
|
|
40
|
+
|
|
41
|
+
/** The stored style, resolved against what this build can draw. */
|
|
42
|
+
export function currentToolRenderMode(appSettings?: Record<string, any>): ToolRenderMode {
|
|
43
|
+
return resolveToolRenderMode(readKabmoSetting<unknown>(TOOL_RENDER_SETTING_KEY, appSettings));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Announce a new style. Called by the settings modal when the value changes —
|
|
48
|
+
* the persisted copy is written by the modal's own `writeKabmoSettings`, so
|
|
49
|
+
* this function only has to make the open timeline re-render.
|
|
50
|
+
*/
|
|
51
|
+
export function applyToolRenderMode(mode: unknown): void {
|
|
52
|
+
if (typeof window === 'undefined') return;
|
|
53
|
+
window.dispatchEvent(
|
|
54
|
+
new CustomEvent(TOOL_RENDER_CHANGED_EVENT, { detail: resolveToolRenderMode(mode) }),
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The style the transcript should draw, kept live across settings edits. */
|
|
59
|
+
export function useToolRenderMode(): ToolRenderMode {
|
|
60
|
+
const [mode, setMode] = useState<ToolRenderMode>(() => currentToolRenderMode());
|
|
61
|
+
useKabmoEvent(TOOL_RENDER_CHANGED_EVENT, (event) => {
|
|
62
|
+
setMode(resolveToolRenderMode((event as CustomEvent<unknown>).detail));
|
|
63
|
+
});
|
|
64
|
+
return mode;
|
|
65
|
+
}
|
|
@@ -32,6 +32,10 @@ export interface LoginUiFrame {
|
|
|
32
32
|
launchUrl?: string;
|
|
33
33
|
instructions?: string;
|
|
34
34
|
placeholder?: string;
|
|
35
|
+
/** omp 18.14.0: `ctx.ui.input({ secret: true })`. Provider login rejects
|
|
36
|
+
* secret prompts server-side, so a secret frame here is an unrelated
|
|
37
|
+
* extension dialog and must not be taken for the OAuth code field. */
|
|
38
|
+
secret?: boolean;
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
export type LoginStatus = 'connecting' | 'waiting' | 'success' | 'failed';
|
|
@@ -184,6 +188,7 @@ export function useProviderLogin({ isOpen, providerId, onAuthenticated }: UsePro
|
|
|
184
188
|
...(typeof frame.launchUrl === 'string' ? { launchUrl: frame.launchUrl } : {}),
|
|
185
189
|
...(typeof frame.instructions === 'string' ? { instructions: frame.instructions } : {}),
|
|
186
190
|
...(typeof frame.placeholder === 'string' ? { placeholder: frame.placeholder } : {}),
|
|
191
|
+
...(typeof frame.secret === 'boolean' ? { secret: frame.secret } : {}),
|
|
187
192
|
};
|
|
188
193
|
const method = ui.method;
|
|
189
194
|
if (method === 'open_url') {
|
|
@@ -206,7 +211,7 @@ export function useProviderLogin({ isOpen, providerId, onAuthenticated }: UsePro
|
|
|
206
211
|
}
|
|
207
212
|
continue;
|
|
208
213
|
}
|
|
209
|
-
if (method === 'input') {
|
|
214
|
+
if (method === 'input' && ui.secret !== true) {
|
|
210
215
|
setState((prev) => ({ ...prev, status: 'waiting', prompt: ui }));
|
|
211
216
|
continue;
|
|
212
217
|
}
|
|
@@ -222,7 +227,11 @@ export function useProviderLogin({ isOpen, providerId, onAuthenticated }: UsePro
|
|
|
222
227
|
respond({ id: ui.id, accepted: true });
|
|
223
228
|
continue;
|
|
224
229
|
}
|
|
225
|
-
// Any other dialog method has no answer this dialog can give.
|
|
230
|
+
// Any other dialog method has no answer this dialog can give. A
|
|
231
|
+
// `secret` input lands here on purpose: omp's provider login
|
|
232
|
+
// refuses secret prompts server-side, so one arriving on this
|
|
233
|
+
// stream belongs to an unrelated extension and reading it as the
|
|
234
|
+
// OAuth code would post a password to the wrong dialog.
|
|
226
235
|
respond({ id: ui.id, cancelled: true });
|
|
227
236
|
continue;
|
|
228
237
|
}
|
|
@@ -8,8 +8,21 @@ import { useEffect, useRef } from 'preact/hooks';
|
|
|
8
8
|
|
|
9
9
|
type EventTargetLike = Window | Document;
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* The global to subscribe on, or null when this environment has none.
|
|
13
|
+
*
|
|
14
|
+
* Read through `globalThis` rather than as a bare identifier: `window` is a
|
|
15
|
+
* ReferenceError where it does not exist, and a component that subscribes during
|
|
16
|
+
* render would then throw instead of rendering. That is not hypothetical — a
|
|
17
|
+
* suite that unmounts its DOM globals between files made `useToolRenderMode`
|
|
18
|
+
* fail with `window is not defined` inside an unrelated render.
|
|
19
|
+
*/
|
|
20
|
+
function globalTarget(name: 'window' | 'document'): EventTargetLike | null {
|
|
21
|
+
return (globalThis as unknown as Record<string, EventTargetLike | undefined>)[name] ?? null;
|
|
22
|
+
}
|
|
23
|
+
|
|
11
24
|
function useEventListener<E extends Event>(
|
|
12
|
-
target: EventTargetLike,
|
|
25
|
+
target: EventTargetLike | null,
|
|
13
26
|
type: string,
|
|
14
27
|
handler: (event: E) => void,
|
|
15
28
|
options?: AddEventListenerOptions | boolean,
|
|
@@ -18,6 +31,9 @@ function useEventListener<E extends Event>(
|
|
|
18
31
|
handlerRef.current = handler;
|
|
19
32
|
|
|
20
33
|
useEffect(() => {
|
|
34
|
+
// Nothing to subscribe on: an effect without a target is a no-op rather
|
|
35
|
+
// than a crash, so a render in a DOM-less environment still mounts.
|
|
36
|
+
if (!target) return;
|
|
21
37
|
const listener = (event: Event) => handlerRef.current(event as E);
|
|
22
38
|
target.addEventListener(type, listener, options);
|
|
23
39
|
return () => target.removeEventListener(type, listener, options);
|
|
@@ -30,7 +46,7 @@ export function useWindowEvent<K extends keyof WindowEventMap>(
|
|
|
30
46
|
handler: (event: WindowEventMap[K]) => void,
|
|
31
47
|
options?: AddEventListenerOptions | boolean,
|
|
32
48
|
) {
|
|
33
|
-
useEventListener<WindowEventMap[K]>(window, type, handler, options);
|
|
49
|
+
useEventListener<WindowEventMap[K]>(globalTarget('window'), type, handler, options);
|
|
34
50
|
}
|
|
35
51
|
|
|
36
52
|
/** Subscribe a `document` listener; the latest handler is always invoked. */
|
|
@@ -39,7 +55,7 @@ export function useDocumentEvent<K extends keyof DocumentEventMap>(
|
|
|
39
55
|
handler: (event: DocumentEventMap[K]) => void,
|
|
40
56
|
options?: AddEventListenerOptions | boolean,
|
|
41
57
|
) {
|
|
42
|
-
useEventListener<DocumentEventMap[K]>(document, type, handler, options);
|
|
58
|
+
useEventListener<DocumentEventMap[K]>(globalTarget('document'), type, handler, options);
|
|
43
59
|
}
|
|
44
60
|
|
|
45
61
|
/** Subscribe to a custom `omp:*` window event. */
|
|
@@ -48,7 +64,7 @@ export function useKabmoEvent(
|
|
|
48
64
|
handler: (event: Event) => void,
|
|
49
65
|
options?: AddEventListenerOptions | boolean,
|
|
50
66
|
) {
|
|
51
|
-
useEventListener<Event>(window, name, handler, options);
|
|
67
|
+
useEventListener<Event>(globalTarget('window'), name, handler, options);
|
|
52
68
|
}
|
|
53
69
|
|
|
54
70
|
/** @deprecated Use `useKabmoEvent`. */
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The state behind a batch archive, shared by both sidebars.
|
|
8
|
+
*
|
|
9
|
+
* Archive is reversible, so there is no confirmation dialog and no pending set
|
|
10
|
+
* to hold: the bar's Archive acts on the live selection and the run only has to
|
|
11
|
+
* report in-flight state (the button spins) and republish the sidebar ONCE,
|
|
12
|
+
* from the server's own signal, after any row actually moved. The caller
|
|
13
|
+
* decides what to say about the settled outcome, because the toast stack is
|
|
14
|
+
* per-layout.
|
|
15
|
+
*
|
|
16
|
+
* Workspaces have no archive state — archive is a session noun — so this is
|
|
17
|
+
* session-mode only.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { useCallback, useState } from 'preact/hooks';
|
|
21
|
+
import {
|
|
22
|
+
archiveSessionsBatch,
|
|
23
|
+
type BatchArchiveOutcome,
|
|
24
|
+
} from '@/client/hooks/workspace/batch-archive';
|
|
25
|
+
|
|
26
|
+
export interface BatchArchiveRun {
|
|
27
|
+
isArchiving: boolean;
|
|
28
|
+
/** Archive these ids. Resolves with the settled outcome (null when nothing ran). */
|
|
29
|
+
archive: (ids: readonly string[]) => Promise<BatchArchiveOutcome | null>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function useBatchArchiveRun(refresh: () => void): BatchArchiveRun {
|
|
33
|
+
const [isArchiving, setIsArchiving] = useState(false);
|
|
34
|
+
|
|
35
|
+
const archive = useCallback(async (ids: readonly string[]): Promise<BatchArchiveOutcome | null> => {
|
|
36
|
+
if (ids.length === 0 || isArchiving) return null;
|
|
37
|
+
setIsArchiving(true);
|
|
38
|
+
const result = await archiveSessionsBatch(ids);
|
|
39
|
+
// One refresh for the whole batch: the server emits `sidebar-structure`
|
|
40
|
+
// once, and only when something actually archived.
|
|
41
|
+
if (result.archived.length > 0) refresh();
|
|
42
|
+
setIsArchiving(false);
|
|
43
|
+
return result;
|
|
44
|
+
}, [isArchiving, refresh]);
|
|
45
|
+
|
|
46
|
+
return { isArchiving, archive };
|
|
47
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The archive summary's contract.
|
|
8
|
+
*
|
|
9
|
+
* A batch archive has no dialog, so this sentence is the ONLY report the user
|
|
10
|
+
* gets: it must name both what was archived and what was refused (a bare count
|
|
11
|
+
* cannot be reconciled against the list), and it must choose the right tone so
|
|
12
|
+
* a partial failure is not dressed as success.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { describe, expect, test } from 'bun:test';
|
|
16
|
+
import { archiveOutcomeMessage, type BatchArchiveOutcome } from '@/client/hooks/workspace/batch-archive';
|
|
17
|
+
|
|
18
|
+
function outcome(partial: Partial<BatchArchiveOutcome>): BatchArchiveOutcome {
|
|
19
|
+
return { archived: [], refused: [], ...partial };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe('archiveOutcomeMessage', () => {
|
|
23
|
+
test('nothing changed reports nothing', () => {
|
|
24
|
+
expect(archiveOutcomeMessage(outcome({}))).toBeNull();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('a clean run is a success and counts the sessions', () => {
|
|
28
|
+
expect(archiveOutcomeMessage(outcome({ archived: ['a', 'b', 'c'] })))
|
|
29
|
+
.toEqual({ message: 'Archived 3 sessions.', tone: 'success' });
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('one archived session is singular', () => {
|
|
33
|
+
expect(archiveOutcomeMessage(outcome({ archived: ['a'] })))
|
|
34
|
+
.toEqual({ message: 'Archived 1 session.', tone: 'success' });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('a partial refusal names both counts and is not dressed as success', () => {
|
|
38
|
+
const report = archiveOutcomeMessage(outcome({
|
|
39
|
+
archived: ['a', 'b'],
|
|
40
|
+
refused: [{ id: 'new-1', error: 'This chat has not been created yet.' }],
|
|
41
|
+
}));
|
|
42
|
+
expect(report?.message).toContain('Archived 2 sessions');
|
|
43
|
+
expect(report?.message).toContain('1 refused');
|
|
44
|
+
expect(report?.tone).toBe('error');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('an all-refused run is an error and names the refusal count', () => {
|
|
48
|
+
const report = archiveOutcomeMessage(outcome({
|
|
49
|
+
refused: [{ id: 'new-1', error: 'This chat has not been created yet.' }],
|
|
50
|
+
}));
|
|
51
|
+
expect(report?.message).toBe('1 session could not be archived.');
|
|
52
|
+
expect(report?.tone).toBe('error');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('a request-level failure reports the server reason', () => {
|
|
56
|
+
expect(archiveOutcomeMessage(outcome({ error: 'Could not archive (HTTP 500).' })))
|
|
57
|
+
.toEqual({ message: 'Could not archive (HTTP 500).', tone: 'error' });
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The batch archive verb, and the sentence a finished run reports.
|
|
8
|
+
*
|
|
9
|
+
* Archive is the reversible sibling of delete, so it has no confirmation
|
|
10
|
+
* dialog: the bar's Archive acts on the selection and the outcome is summarised
|
|
11
|
+
* as one toast. The server still answers per-id verdicts — a pending `new-…`
|
|
12
|
+
* chat cannot be archived — so the summary must name both what was archived and
|
|
13
|
+
* what was refused rather than reporting a bare count the user cannot reconcile
|
|
14
|
+
* against the list.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export interface BatchArchiveRefusal {
|
|
18
|
+
id: string;
|
|
19
|
+
error: string;
|
|
20
|
+
code?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface BatchArchiveOutcome {
|
|
24
|
+
archived: string[];
|
|
25
|
+
refused: BatchArchiveRefusal[];
|
|
26
|
+
/** Set only when the request itself failed (network, 4xx, 5xx). */
|
|
27
|
+
error?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const EMPTY: BatchArchiveOutcome = { archived: [], refused: [] };
|
|
31
|
+
|
|
32
|
+
async function postBatch(url: string, ids: readonly string[]): Promise<BatchArchiveOutcome> {
|
|
33
|
+
try {
|
|
34
|
+
const res = await fetch(url, {
|
|
35
|
+
method: 'POST',
|
|
36
|
+
headers: { 'content-type': 'application/json' },
|
|
37
|
+
body: JSON.stringify({ ids }),
|
|
38
|
+
});
|
|
39
|
+
const body = (await res.json().catch(() => null)) as Partial<BatchArchiveOutcome> | null;
|
|
40
|
+
if (!res.ok) {
|
|
41
|
+
return { ...EMPTY, error: body?.error || `Could not archive (HTTP ${res.status}).` };
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
archived: Array.isArray(body?.archived) ? body.archived : [],
|
|
45
|
+
refused: Array.isArray(body?.refused) ? body.refused : [],
|
|
46
|
+
};
|
|
47
|
+
} catch (error) {
|
|
48
|
+
return { ...EMPTY, error: error instanceof Error ? error.message : 'Could not reach the server.' };
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Archive sessions in one request. Pending `new-…` ids are refused by the server. */
|
|
53
|
+
export function archiveSessionsBatch(ids: readonly string[]): Promise<BatchArchiveOutcome> {
|
|
54
|
+
return postBatch('/api/sessions/batch-archive', ids);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The one line a finished archive run reports, for a toast. Null when the run
|
|
59
|
+
* changed nothing and has nothing to say.
|
|
60
|
+
*/
|
|
61
|
+
export function archiveOutcomeMessage(
|
|
62
|
+
outcome: BatchArchiveOutcome,
|
|
63
|
+
): { message: string; tone: 'success' | 'error' } | null {
|
|
64
|
+
if (outcome.error) return { message: outcome.error, tone: 'error' };
|
|
65
|
+
const archived = outcome.archived.length;
|
|
66
|
+
const refused = outcome.refused.length;
|
|
67
|
+
if (archived === 0 && refused === 0) return null;
|
|
68
|
+
if (refused === 0) {
|
|
69
|
+
return { message: `Archived ${archived} ${archived === 1 ? 'session' : 'sessions'}.`, tone: 'success' };
|
|
70
|
+
}
|
|
71
|
+
if (archived === 0) {
|
|
72
|
+
return { message: `${refused} ${refused === 1 ? 'session' : 'sessions'} could not be archived.`, tone: 'error' };
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
message: `Archived ${archived} ${archived === 1 ? 'session' : 'sessions'}; ${refused} refused.`,
|
|
76
|
+
tone: 'error',
|
|
77
|
+
};
|
|
78
|
+
}
|