@kabmo-labs/kabmo 0.17.0 → 0.18.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.
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +538 -457
- package/dist/client/static/js/index-hp75vsv9.css +1 -0
- package/dist/client/static/js/{index-rerjq5th.js → index-tc09dtca.js} +823 -814
- package/package.json +1 -1
- package/src/client/components/common/MarkdownRenderer.tsx +136 -89
- package/src/client/components/common/anchored-menu/index.test.tsx +141 -0
- package/src/client/components/common/anchored-menu/index.tsx +143 -0
- package/src/client/components/common/markdown-review/ReviewPopover.tsx +144 -0
- package/src/client/components/common/markdown-review/ReviewSummary.tsx +108 -0
- package/src/client/components/common/markdown-review/index.tsx +129 -0
- package/src/client/components/common/server-control/Panel.tsx +33 -23
- package/src/client/components/common/server-control/index.test.tsx +11 -5
- package/src/client/components/common/server-control/index.tsx +10 -8
- package/src/client/components/layout/session-sidebar/CategoryHeader.tsx +42 -21
- package/src/client/components/layout/session-sidebar/CategoryHeader.worktree.test.tsx +36 -17
- package/src/client/components/layout/session-sidebar/CategoryItem.tsx +8 -7
- package/src/client/components/layout/session-sidebar/about-modal/NotesDialog.tsx +94 -0
- package/src/client/components/layout/session-sidebar/about-modal/VersionLine.test.tsx +68 -0
- package/src/client/components/layout/session-sidebar/about-modal/VersionLine.tsx +36 -0
- package/src/client/components/layout/session-sidebar/about-modal/index.tsx +57 -34
- package/src/client/components/layout/session-sidebar/modern/WorkspaceHeader.tsx +28 -10
- package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +7 -6
- package/src/client/components/layout/session-sidebar/modern/workspace-header.test.tsx +1 -1
- package/src/client/components/layout/update-popup/ReleaseNotes.tsx +8 -3
- package/src/client/components/layout/update-popup/index.tsx +5 -1
- package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +13 -15
- package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +23 -1
- package/src/client/components/mobile/mobile-right-sidebar/MissingFileBanner.tsx +30 -0
- package/src/client/components/mobile/mobile-right-sidebar/PreviewScroller.tsx +18 -2
- package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +22 -67
- package/src/client/components/mobile/mobile-session-sidebar/ItemMenu.tsx +110 -0
- package/src/client/components/settings/SettingsSidebarList.tsx +20 -0
- package/src/client/components/settings/categories/AgentSettings.tsx +23 -50
- package/src/client/components/settings/categories/CommandSettings.tsx +2 -2
- package/src/client/components/settings/categories/McpSettings.tsx +4 -21
- package/src/client/components/settings/categories/OmpSettings.tsx +147 -194
- package/src/client/components/settings/categories/SkillSettings.tsx +2 -2
- package/src/client/components/settings/categories/TokenUsageSettings.tsx +211 -229
- package/src/client/components/settings/categories/agent-settings/DetailPane.tsx +229 -237
- package/src/client/components/settings/categories/agent-settings/ModelPicker.tsx +71 -0
- package/src/client/components/settings/categories/agent-settings/SidebarList.tsx +9 -2
- package/src/client/components/settings/categories/agent-settings/levels.ts +38 -0
- package/src/client/components/settings/categories/behavior-settings/Editor.tsx +27 -7
- package/src/client/components/settings/categories/chat-settings/ToolRenderSection.tsx +23 -5
- package/src/client/components/settings/categories/command-settings/DetailPane.tsx +75 -33
- package/src/client/components/settings/categories/command-settings/SidebarList.tsx +7 -0
- package/src/client/components/settings/categories/mcp-settings/DetailPane.tsx +91 -76
- package/src/client/components/settings/categories/mcp-settings/EnvVarsSection.tsx +83 -0
- package/src/client/components/settings/categories/mcp-settings/ImportModal.tsx +26 -13
- package/src/client/components/settings/categories/mcp-settings/SidebarList.tsx +20 -0
- package/src/client/components/settings/categories/omp-settings/ConfigTabs.tsx +97 -36
- package/src/client/components/settings/categories/omp-settings/EngineHeader.tsx +151 -0
- package/src/client/components/settings/categories/omp-settings/ExtensionsPanel.tsx +68 -32
- package/src/client/components/settings/categories/omp-settings/OrderedChips.tsx +119 -0
- package/src/client/components/settings/categories/omp-settings/PresetBar.tsx +14 -7
- package/src/client/components/settings/categories/omp-settings/RoleEditor.tsx +15 -6
- package/src/client/components/settings/categories/omp-settings/RowEditor.tsx +165 -226
- package/src/client/components/settings/categories/omp-settings/types.ts +28 -0
- package/src/client/components/settings/categories/skill-settings/DetailHeader.tsx +35 -7
- package/src/client/components/settings/categories/skill-settings/DetailPane.tsx +34 -14
- package/src/client/components/settings/categories/skill-settings/SidebarList.tsx +21 -0
- package/src/client/components/settings/categories/skill-settings/pane.test.tsx +15 -6
- package/src/client/components/settings/categories/token-usage-settings/Breakdown.tsx +242 -102
- package/src/client/components/settings/categories/token-usage-settings/Chart.tsx +130 -56
- package/src/client/components/settings/categories/token-usage-settings/Controls.tsx +135 -0
- package/src/client/components/settings/categories/token-usage-settings/MetricsGrid.tsx +55 -33
- package/src/client/components/settings/categories/token-usage-settings/Provenance.tsx +59 -0
- package/src/client/components/settings/categories/token-usage-settings/panel.test.tsx +239 -0
- package/src/client/components/workspace/browser-panel/Screencast.test.ts +1 -0
- package/src/client/components/workspace/browser-panel/Screencast.tsx +17 -22
- package/src/client/components/workspace/browser-panel/TabStrip.test.tsx +158 -0
- package/src/client/components/workspace/browser-panel/TabStrip.tsx +90 -0
- package/src/client/components/workspace/browser-panel/index.tsx +50 -4
- package/src/client/components/workspace/browser-toolbar/index.tsx +41 -1
- package/src/client/components/workspace/chat-timeline/ThinkingSection.tsx +3 -1
- package/src/client/components/workspace/chat-timeline/ToolCallingSection.tsx +2 -0
- package/src/client/components/workspace/chat-timeline/block-chrome.test.tsx +13 -2
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.test.ts +88 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.tsx +24 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.test.ts +34 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.tsx +5 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ArtifactChip.tsx +7 -6
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Tui.test.tsx +237 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Tui.tsx +191 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/styles.test.tsx +1 -1
- package/src/client/components/workspace/diff-panel/MergeDiffView.tsx +21 -7
- package/src/client/components/workspace/diff-panel/changes.test.ts +51 -0
- package/src/client/components/workspace/diff-panel/changes.ts +28 -12
- package/src/client/components/workspace/diff-panel/merge.test.ts +28 -0
- package/src/client/components/workspace/diff-panel/merge.ts +21 -1
- package/src/client/components/workspace/editor/Body.tsx +7 -0
- package/src/client/components/workspace/editor/FileDocument.tsx +15 -21
- package/src/client/components/workspace/editor/FilePane.tsx +12 -0
- package/src/client/components/workspace/editor/PreviewPane.tsx +70 -0
- package/src/client/components/workspace/editor/Toolbar.tsx +17 -1
- package/src/client/components/workspace/editor/index.tsx +25 -22
- package/src/client/components/workspace/editor/toolbar-clusters.test.ts +14 -0
- package/src/client/components/workspace/editor/toolbar-clusters.tsx +12 -86
- package/src/client/components/workspace/editor/toolbar-toggle.tsx +53 -0
- package/src/client/components/workspace/editor/toolbar-write-clusters.tsx +112 -0
- package/src/client/components/workspace/git-panel/commit-modal.test.ts +5 -3
- package/src/client/components/workspace/user-browser-panel/AddressBar.tsx +5 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateBar.test.tsx +201 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateBar.tsx +112 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateLayer.test.tsx +225 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateLayer.tsx +217 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateTarget.tsx +99 -0
- package/src/client/components/workspace/user-browser-panel/Frame.tsx +30 -1
- package/src/client/components/workspace/user-browser-panel/index.tsx +81 -2
- package/src/client/data/agent-data/builtin.ts +0 -10
- package/src/client/data/agent-data/orchestrator.ts +0 -45
- package/src/client/data/agent-data/specialist.ts +0 -100
- package/src/client/data/mock/token-usage.ts +170 -120
- package/src/client/data/settings/omp-schema.json +33 -1
- package/src/client/hooks/browser/use-annotate.test.ts +143 -0
- package/src/client/hooks/browser/use-annotate.ts +98 -0
- package/src/client/hooks/browser/use-close-tab.test.ts +148 -0
- package/src/client/hooks/browser/use-close-tab.ts +67 -0
- package/src/client/hooks/browser/use-screencast.ts +13 -1
- package/src/client/hooks/chat/composer/browser-context.test.ts +117 -0
- package/src/client/hooks/chat/composer/browser-context.ts +40 -16
- package/src/client/hooks/chat/composer/items.test.ts +1 -1
- package/src/client/hooks/chat/composer/review-context.ts +38 -0
- package/src/client/hooks/chat/composer/use-draft.test.ts +132 -0
- package/src/client/hooks/chat/composer/use-draft.ts +31 -2
- package/src/client/hooks/chat/timeline/row-window.test.ts +15 -2
- package/src/client/hooks/editor/use-document-annotations.ts +124 -0
- package/src/client/hooks/editor/use-editor-panel-ui.ts +52 -0
- package/src/client/hooks/editor/use-file-review-mode.ts +35 -0
- package/src/client/hooks/editor/use-preview-review.ts +148 -0
- package/src/client/hooks/settings/agents.ts +149 -0
- package/src/client/hooks/settings/crud-list.test.ts +28 -6
- package/src/client/hooks/settings/crud-list.ts +96 -57
- package/src/client/hooks/settings/omp-config.ts +164 -0
- package/src/client/hooks/settings/useTokenUsage.ts +125 -0
- package/src/client/hooks/settings/write-feedback.test.ts +104 -0
- package/src/client/hooks/settings/write-feedback.ts +63 -0
- package/src/client/hooks/ui/markdown-hydration.ts +125 -0
- package/src/client/hooks/ui/updates/index.test.ts +100 -1
- package/src/client/hooks/ui/updates/index.ts +50 -0
- package/src/client/hooks/ui/updates/notes.test.ts +138 -0
- package/src/client/hooks/ui/updates/notes.ts +57 -0
- package/src/client/lib/browser/element-at.test.ts +229 -0
- package/src/client/lib/browser/element-at.ts +139 -0
- package/src/client/lib/review/client.ts +73 -0
- package/src/client/lib/router/search-params.ts +42 -20
- package/src/client/tailwind.css +65 -0
- package/src/server/lib/btw/lifecycle.server.ts +2 -2
- package/src/server/lib/db/schema.ts +7 -0
- package/src/server/lib/db/seed-terminal.test.ts +4 -0
- package/src/server/lib/omp/config/agent-definitions.test.ts +273 -0
- package/src/server/lib/omp/config/agents.ts +231 -61
- package/src/server/lib/omp/config/definitions.test.ts +9 -105
- package/src/server/lib/omp/config/extensions.ts +16 -7
- package/src/server/lib/omp/config/omp-config-files.test.ts +27 -0
- package/src/server/lib/omp/rpc/constants.test.ts +11 -0
- package/src/server/lib/omp/rpc/constants.ts +15 -0
- package/src/server/lib/omp/rpc/process.ts +11 -14
- package/src/server/lib/omp/rpc/session-commands.ts +6 -3
- package/src/server/lib/omp/rpc/utility-agent-dir.test.ts +134 -0
- package/src/server/lib/omp/rpc/utility-agent-dir.ts +138 -0
- package/src/server/lib/omp/rpc/utility.ts +12 -0
- package/src/server/lib/review/schema.server.ts +45 -0
- package/src/server/lib/review/store.server.ts +138 -0
- package/src/server/lib/terminal/attach-replay.test.ts +132 -0
- package/src/server/lib/terminal/output.test.ts +166 -0
- package/src/server/lib/terminal/output.ts +104 -0
- package/src/server/lib/terminal/runtime.server.ts +5 -0
- package/src/server/lib/terminal/send-result.test.ts +33 -0
- package/src/server/lib/terminal/send-result.ts +32 -0
- package/src/server/lib/terminal/session.server.ts +45 -11
- package/src/server/lib/terminal/store-liveness.test.ts +4 -0
- package/src/server/lib/updates/active-notes.test.ts +149 -0
- package/src/server/lib/updates/active-notes.ts +75 -0
- package/src/server/lib/updates/changelog-file.ts +42 -11
- package/src/server/lib/updates/changelog.test.ts +15 -1
- package/src/server/lib/updates/omp.ts +42 -15
- package/src/server/lib/updates/release-url.ts +27 -1
- package/src/server/lib/usage/index-parse.test.ts +200 -0
- package/src/server/lib/usage/index-parse.ts +178 -0
- package/src/server/lib/usage/index-query.server.ts +248 -0
- package/src/server/lib/usage/index-scan.server.ts +343 -0
- package/src/server/lib/usage/index-schema.server.ts +82 -0
- package/src/server/lib/usage/index.test.ts +234 -0
- package/src/server/lib/usage/providers.server.ts +4 -53
- package/src/server/routes/browser/index.ts +3 -0
- package/src/server/routes/browser/stream.ts +31 -2
- package/src/server/routes/browser/tabs.test.ts +94 -0
- package/src/server/routes/browser/tabs.ts +85 -0
- package/src/server/routes/index.ts +2 -0
- package/src/server/routes/review/bindings.ts +11 -0
- package/src/server/routes/review/index.ts +127 -0
- package/src/server/routes/review/routes.test.ts +191 -0
- package/src/server/routes/settings/agents.test.ts +180 -0
- package/src/server/routes/settings/agents.ts +95 -102
- package/src/server/routes/settings/mcp.test.ts +135 -0
- package/src/server/routes/settings/mcp.ts +14 -6
- package/src/server/routes/settings/settings-store-routes.test.ts +6 -113
- package/src/server/routes/telemetry/tokens.test.ts +145 -0
- package/src/server/routes/telemetry/tokens.ts +70 -149
- package/src/server/routes/terminal/ws.ts +6 -5
- package/src/server/routes/updates/index.ts +2 -0
- package/src/server/routes/updates/notes.ts +34 -0
- package/src/server/routes/updates/routes.test.ts +49 -0
- package/src/shared/lib/browser/annotate.test.ts +283 -0
- package/src/shared/lib/browser/annotate.ts +231 -0
- package/src/shared/lib/browser/browser-viewer.test.ts +26 -4
- package/src/shared/lib/browser/frame-gate.test.ts +91 -0
- package/src/shared/lib/browser/frame-gate.ts +69 -0
- package/src/shared/lib/browser/frame-params.test.ts +43 -0
- package/src/shared/lib/browser/frame-params.ts +60 -0
- package/src/shared/lib/browser/frame-pipeline.test.ts +132 -0
- package/src/shared/lib/browser/frame-pipeline.ts +80 -0
- package/src/shared/lib/browser/page-context.ts +60 -4
- package/src/shared/lib/browser/snapshot.test.ts +115 -0
- package/src/shared/lib/browser/snapshot.ts +112 -0
- package/src/shared/lib/browser/tab-close.test.ts +74 -0
- package/src/shared/lib/browser/tab-close.ts +77 -0
- package/src/shared/lib/browser/viewer.ts +45 -26
- package/src/shared/lib/chat/timeline/block-chrome.ts +19 -0
- package/src/shared/lib/chat/timeline/tool-render-mode.ts +17 -4
- package/src/shared/lib/chat/tool/artifact.ts +55 -20
- package/src/shared/lib/chat/tool/bash-facts.ts +96 -0
- package/src/shared/lib/chat/tool/minimized.test.ts +98 -0
- package/src/shared/lib/chat/tool/minimized.ts +64 -0
- package/src/shared/lib/chat/tool/summary.test.ts +28 -8
- package/src/shared/lib/chat/tool/summary.ts +3 -32
- package/src/shared/lib/markdown/blocks/anchor.ts +63 -0
- package/src/shared/lib/markdown/blocks/blocks.test.ts +141 -0
- package/src/shared/lib/markdown/blocks/renderer.ts +120 -0
- package/src/shared/lib/markdown/blocks/scan.ts +100 -0
- package/src/shared/lib/markdown/blocks/types.ts +62 -0
- package/src/shared/lib/markdown/marked.ts +58 -147
- package/src/shared/lib/markdown/math-extensions.ts +168 -0
- package/src/shared/lib/markdown/review/prompt.ts +77 -0
- package/src/shared/lib/markdown/review/reconcile.ts +55 -0
- package/src/shared/lib/markdown/review/review.test.ts +96 -0
- package/src/shared/lib/markdown/xml-sections.test.ts +140 -0
- package/src/shared/lib/markdown/xml-sections.ts +139 -0
- package/src/shared/lib/models/provider/label.ts +57 -0
- package/src/shared/lib/omp/config/agent-file.test.ts +54 -0
- package/src/shared/lib/omp/config/agent-file.ts +43 -0
- package/src/shared/lib/omp/config/row-value.test.ts +95 -0
- package/src/shared/lib/omp/config/row-value.ts +97 -0
- package/src/shared/lib/omp/config/schema.test.ts +31 -0
- package/src/shared/lib/omp/config/schema.ts +6 -0
- package/src/shared/lib/omp/rpc/frame.test.ts +48 -47
- package/src/shared/lib/omp/rpc/frame.ts +49 -40
- package/src/shared/lib/ui/menu-anchor.test.ts +69 -0
- package/src/shared/lib/ui/menu-anchor.ts +89 -0
- package/src/shared/lib/usage/breakdown.test.ts +197 -0
- package/src/shared/lib/usage/breakdown.ts +241 -0
- package/src/shared/lib/usage/range.test.ts +204 -0
- package/src/shared/lib/usage/range.ts +240 -0
- package/src/shared/lib/usage/view.test.ts +85 -0
- package/src/shared/lib/usage/view.ts +74 -0
- package/src/shared/types/browser.ts +78 -0
- package/src/shared/types/index.ts +1 -0
- package/src/shared/types/settings/agent.ts +28 -6
- package/src/shared/types/settings/state.ts +4 -3
- package/src/shared/types/settings/write.ts +6 -0
- package/src/shared/types/tokenUsage.ts +128 -21
- package/src/shared/types/updates.ts +20 -0
- package/dist/client/static/js/index-3ve31rzx.css +0 -1
- package/src/server/lib/omp/session/usage/aggregate-shaping.test.ts +0 -149
- package/src/server/lib/omp/session/usage/aggregate.test.ts +0 -243
- package/src/server/lib/omp/session/usage/aggregate.ts +0 -330
- package/src/server/lib/omp/session/usage/shaping.ts +0 -130
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Native omp config writes for the OMP Engine panel: the live snapshot, the
|
|
8
|
+
* per-key save/reset state, and the "saved <key>" flash.
|
|
9
|
+
*
|
|
10
|
+
* Extracted from `OmpSettings` for the same reason `useModelRoles` was: it is a
|
|
11
|
+
* self-contained store with three verbs and its own error surface, and the panel
|
|
12
|
+
* was over the per-file ceiling. The load is still the panel's single
|
|
13
|
+
* `Promise.all`, so `applyEntries` adopts its half of that payload.
|
|
14
|
+
*
|
|
15
|
+
* Every write keeps the server's OWN sentence. `omp config set` refuses a stale
|
|
16
|
+
* key with "Unknown setting: <key>", a bad value with "Invalid value: …", and a
|
|
17
|
+
* timeout with nothing at all — a bare "save failed" badge made all three read
|
|
18
|
+
* the same.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
|
|
22
|
+
import type { ConfigEntry } from '@/server/lib/omp/config/config-cli';
|
|
23
|
+
|
|
24
|
+
export interface OmpConfigWrites {
|
|
25
|
+
entries: Record<string, ConfigEntry>;
|
|
26
|
+
/** Keys whose write is in flight. */
|
|
27
|
+
savingKeys: Set<string>;
|
|
28
|
+
/** Keys with a local draft the server does not hold. */
|
|
29
|
+
dirtyKeys: Set<string>;
|
|
30
|
+
/** Key → the refusal's sentence. */
|
|
31
|
+
saveErrors: Map<string, string>;
|
|
32
|
+
/** The key whose write just landed (cleared after a short flash). */
|
|
33
|
+
savedKey: string | null;
|
|
34
|
+
/** Adopt the `GET /api/settings/omp-config` entries. */
|
|
35
|
+
applyEntries: (entries: Record<string, ConfigEntry>) => void;
|
|
36
|
+
commitValue: (key: string, value: unknown) => Promise<boolean>;
|
|
37
|
+
resetKey: (key: string) => Promise<boolean>;
|
|
38
|
+
setDirty: (key: string, dirty: boolean) => void;
|
|
39
|
+
/** Flash a badge for a write that does not own a config row (roles, extensions). */
|
|
40
|
+
flashSaved: (key: string) => void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const SAVED_FLASH_MS = 2000;
|
|
44
|
+
|
|
45
|
+
interface WriteAnswer {
|
|
46
|
+
ok: boolean;
|
|
47
|
+
value?: unknown;
|
|
48
|
+
error?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** POST one config write; never throws for a refusal. */
|
|
52
|
+
async function postConfig(body: unknown): Promise<WriteAnswer> {
|
|
53
|
+
const res = await fetch('/api/settings/omp-config', {
|
|
54
|
+
method: 'POST',
|
|
55
|
+
headers: { 'Content-Type': 'application/json' },
|
|
56
|
+
body: JSON.stringify(body),
|
|
57
|
+
});
|
|
58
|
+
const data: unknown = await res.json().catch(() => null);
|
|
59
|
+
if (typeof data !== 'object' || data === null) {
|
|
60
|
+
return { ok: false, error: `The config route answered ${res.status} without a body` };
|
|
61
|
+
}
|
|
62
|
+
const ok = 'success' in data && data.success === true;
|
|
63
|
+
const error = 'error' in data && typeof data.error === 'string' ? data.error : undefined;
|
|
64
|
+
const value = 'value' in data ? data.value : undefined;
|
|
65
|
+
return { ok, ...(error ? { error } : {}), ...(value === undefined ? {} : { value }) };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function useOmpConfigWrites(): OmpConfigWrites {
|
|
69
|
+
const [entries, setEntries] = useState<Record<string, ConfigEntry>>({});
|
|
70
|
+
const [savingKeys, setSavingKeys] = useState<Set<string>>(new Set());
|
|
71
|
+
const [dirtyKeys, setDirtyKeys] = useState<Set<string>>(new Set());
|
|
72
|
+
const [saveErrors, setSaveErrors] = useState<Map<string, string>>(new Map());
|
|
73
|
+
const [savedKey, setSavedKey] = useState<string | null>(null);
|
|
74
|
+
const mounted = useRef(true);
|
|
75
|
+
|
|
76
|
+
useEffect(() => () => { mounted.current = false; }, []);
|
|
77
|
+
|
|
78
|
+
const applyEntries = useCallback((next: Record<string, ConfigEntry>) => {
|
|
79
|
+
setEntries(next);
|
|
80
|
+
}, []);
|
|
81
|
+
|
|
82
|
+
const flashSaved = useCallback((key: string) => {
|
|
83
|
+
setSavedKey(key);
|
|
84
|
+
setTimeout(() => setSavedKey((current) => (current === key ? null : current)), SAVED_FLASH_MS);
|
|
85
|
+
}, []);
|
|
86
|
+
|
|
87
|
+
/** Apply one write's outcome to the row state. */
|
|
88
|
+
const applyWriteResult = useCallback((key: string, outcome: WriteAnswer) => {
|
|
89
|
+
if (!mounted.current) return;
|
|
90
|
+
setSavingKeys((prev) => {
|
|
91
|
+
const next = new Set(prev);
|
|
92
|
+
next.delete(key);
|
|
93
|
+
return next;
|
|
94
|
+
});
|
|
95
|
+
setDirtyKeys((prev) => {
|
|
96
|
+
const next = new Set(prev);
|
|
97
|
+
next.delete(key);
|
|
98
|
+
return next;
|
|
99
|
+
});
|
|
100
|
+
if (outcome.ok) {
|
|
101
|
+
setSaveErrors((prev) => {
|
|
102
|
+
if (!prev.has(key)) return prev;
|
|
103
|
+
const next = new Map(prev);
|
|
104
|
+
next.delete(key);
|
|
105
|
+
return next;
|
|
106
|
+
});
|
|
107
|
+
if (outcome.value !== undefined) {
|
|
108
|
+
setEntries((prev) => ({ ...prev, [key]: { ...prev[key], value: outcome.value } }));
|
|
109
|
+
}
|
|
110
|
+
flashSaved(key);
|
|
111
|
+
} else {
|
|
112
|
+
setSaveErrors((prev) => new Map(prev).set(key, outcome.error ?? 'omp rejected the write'));
|
|
113
|
+
}
|
|
114
|
+
}, [flashSaved]);
|
|
115
|
+
|
|
116
|
+
const runWrite = useCallback(async (key: string, body: unknown): Promise<boolean> => {
|
|
117
|
+
setSavingKeys((prev) => new Set(prev).add(key));
|
|
118
|
+
setDirtyKeys((prev) => {
|
|
119
|
+
const next = new Set(prev);
|
|
120
|
+
next.delete(key);
|
|
121
|
+
return next;
|
|
122
|
+
});
|
|
123
|
+
try {
|
|
124
|
+
const outcome = await postConfig(body);
|
|
125
|
+
applyWriteResult(key, outcome);
|
|
126
|
+
return outcome.ok;
|
|
127
|
+
} catch (err) {
|
|
128
|
+
console.error('Failed to write omp config:', err);
|
|
129
|
+
applyWriteResult(key, { ok: false, error: err instanceof Error ? err.message : String(err) });
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
}, [applyWriteResult]);
|
|
133
|
+
|
|
134
|
+
const commitValue = useCallback(
|
|
135
|
+
(key: string, value: unknown) => runWrite(key, { key, value }),
|
|
136
|
+
[runWrite],
|
|
137
|
+
);
|
|
138
|
+
const resetKey = useCallback(
|
|
139
|
+
(key: string) => runWrite(key, { key, action: 'reset' }),
|
|
140
|
+
[runWrite],
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const setDirty = useCallback((key: string, dirty: boolean) => {
|
|
144
|
+
setDirtyKeys((prev) => {
|
|
145
|
+
const next = new Set(prev);
|
|
146
|
+
if (dirty) next.add(key);
|
|
147
|
+
else next.delete(key);
|
|
148
|
+
return next;
|
|
149
|
+
});
|
|
150
|
+
}, []);
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
entries,
|
|
154
|
+
savingKeys,
|
|
155
|
+
dirtyKeys,
|
|
156
|
+
saveErrors,
|
|
157
|
+
savedKey,
|
|
158
|
+
applyEntries,
|
|
159
|
+
commitValue,
|
|
160
|
+
resetKey,
|
|
161
|
+
setDirty,
|
|
162
|
+
flashSaved,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Loads the Token Usage payload and keeps it fresh while the index catches up.
|
|
8
|
+
*
|
|
9
|
+
* Three behaviours the panel depends on:
|
|
10
|
+
*
|
|
11
|
+
* - **One request per period.** Cadence and breakdown tab live in the
|
|
12
|
+
* component; changing them re-folds the payload client-side.
|
|
13
|
+
* - **Stale-while-revalidate.** A range change keeps the previous numbers on
|
|
14
|
+
* screen (`refreshing`) instead of blanking the panel, and the first load is
|
|
15
|
+
* the only case that shows a skeleton.
|
|
16
|
+
* - **Abort.** A fast user clicking ranges would otherwise render whichever
|
|
17
|
+
* response happened to arrive last.
|
|
18
|
+
*
|
|
19
|
+
* While `index.state` is `building` the hook polls: the indexer answers with
|
|
20
|
+
* the data it already has, so the panel draws immediately and the footer says
|
|
21
|
+
* "indexing…" until the next poll reports `ready`.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
|
|
25
|
+
import type { TimeRangeType, TokenUsagePayload } from '@/shared/types';
|
|
26
|
+
|
|
27
|
+
const POLL_MS = 1_500;
|
|
28
|
+
|
|
29
|
+
export interface TokenUsageState {
|
|
30
|
+
payload: TokenUsagePayload | null;
|
|
31
|
+
error: string | null;
|
|
32
|
+
/** True only until the first payload arrives. */
|
|
33
|
+
loading: boolean;
|
|
34
|
+
/** A request is in flight while older data is on screen. */
|
|
35
|
+
refreshing: boolean;
|
|
36
|
+
reload: (force?: boolean) => void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function useTokenUsage(range: TimeRangeType, custom: { from: string; to: string }): TokenUsageState {
|
|
40
|
+
const [payload, setPayload] = useState<TokenUsagePayload | null>(null);
|
|
41
|
+
const [error, setError] = useState<string | null>(null);
|
|
42
|
+
const [loading, setLoading] = useState(true);
|
|
43
|
+
const [refreshing, setRefreshing] = useState(false);
|
|
44
|
+
const abortRef = useRef<AbortController | null>(null);
|
|
45
|
+
const payloadRef = useRef<TokenUsagePayload | null>(null);
|
|
46
|
+
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
47
|
+
const forceRef = useRef(false);
|
|
48
|
+
|
|
49
|
+
const clearTimer = () => {
|
|
50
|
+
if (timerRef.current !== null) {
|
|
51
|
+
clearTimeout(timerRef.current);
|
|
52
|
+
timerRef.current = null;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const request = useCallback(
|
|
57
|
+
(): string => {
|
|
58
|
+
const params = new URLSearchParams({ range });
|
|
59
|
+
if (range === 'custom') {
|
|
60
|
+
params.set('customFrom', custom.from);
|
|
61
|
+
params.set('customTo', custom.to);
|
|
62
|
+
}
|
|
63
|
+
return `/api/telemetry/tokens?${params.toString()}${forceRef.current ? '&refresh=1' : ''}`;
|
|
64
|
+
},
|
|
65
|
+
[range, custom.from, custom.to],
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const load = useCallback(
|
|
69
|
+
async (force: boolean): Promise<void> => {
|
|
70
|
+
abortRef.current?.abort();
|
|
71
|
+
const controller = new AbortController();
|
|
72
|
+
abortRef.current = controller;
|
|
73
|
+
forceRef.current = force;
|
|
74
|
+
setRefreshing(true);
|
|
75
|
+
try {
|
|
76
|
+
const response = await fetch(request(), {
|
|
77
|
+
signal: controller.signal,
|
|
78
|
+
method: force ? 'POST' : 'GET',
|
|
79
|
+
});
|
|
80
|
+
const body = (await response.json()) as TokenUsagePayload & { error?: string };
|
|
81
|
+
if (!response.ok || body.error) throw new Error(body.error ?? `Token usage failed (${response.status})`);
|
|
82
|
+
payloadRef.current = body;
|
|
83
|
+
setPayload(body);
|
|
84
|
+
setError(null);
|
|
85
|
+
setLoading(false);
|
|
86
|
+
clearTimer();
|
|
87
|
+
if (body.index.state === 'building') {
|
|
88
|
+
timerRef.current = setTimeout(() => {
|
|
89
|
+
forceRef.current = false;
|
|
90
|
+
void load(false);
|
|
91
|
+
}, POLL_MS);
|
|
92
|
+
}
|
|
93
|
+
} catch (caught) {
|
|
94
|
+
if (controller.signal.aborted) return;
|
|
95
|
+
setError(caught instanceof Error ? caught.message : 'Failed to load token usage');
|
|
96
|
+
setLoading(false);
|
|
97
|
+
} finally {
|
|
98
|
+
if (!controller.signal.aborted) setRefreshing(false);
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
[request],
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
// Only the FIRST load has nothing to show. A range change keeps the
|
|
106
|
+
// previous numbers on screen with an "updating…" hint instead of blanking
|
|
107
|
+
// the panel the user is reading.
|
|
108
|
+
setLoading((current) => current && payloadRef.current === null);
|
|
109
|
+
void load(false);
|
|
110
|
+
return () => {
|
|
111
|
+
abortRef.current?.abort();
|
|
112
|
+
clearTimer();
|
|
113
|
+
};
|
|
114
|
+
}, [load]);
|
|
115
|
+
|
|
116
|
+
const reload = useCallback(
|
|
117
|
+
(force = false) => {
|
|
118
|
+
clearTimer();
|
|
119
|
+
void load(force);
|
|
120
|
+
},
|
|
121
|
+
[load],
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
return { payload, error, loading, refreshing, reload };
|
|
125
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The write-feedback contract every settings pane now shares.
|
|
8
|
+
*
|
|
9
|
+
* This exists because the panes used to paint "Saved successfully" the moment
|
|
10
|
+
* the button was pressed — before the request answered — so a refused write (a
|
|
11
|
+
* duplicate preset name, a read-only native file, a validator rejecting the
|
|
12
|
+
* draft) showed success over a row that had not changed. The badge is now driven
|
|
13
|
+
* by the outcome and nothing else, and a refusal keeps the server's sentence.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { afterAll, afterEach, beforeAll, 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 { useWriteFeedback, type WriteFeedback } from '@/client/hooks/settings/write-feedback';
|
|
21
|
+
import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
|
|
22
|
+
import type { WriteOutcome } from '@/shared/types';
|
|
23
|
+
|
|
24
|
+
let container: HTMLElement | null = null;
|
|
25
|
+
let feedback: WriteFeedback | null = null;
|
|
26
|
+
|
|
27
|
+
function Probe() {
|
|
28
|
+
feedback = useWriteFeedback();
|
|
29
|
+
return h('div', null, feedback.saving ? 'saving' : feedback.saved ? 'saved' : 'idle');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
beforeAll(() => {
|
|
33
|
+
installDomGlobals(new Window({ url: 'http://localhost' }));
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
afterEach(() => {
|
|
37
|
+
if (container) render(null, container);
|
|
38
|
+
container = null;
|
|
39
|
+
feedback = null;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
afterAll(() => {
|
|
43
|
+
restoreDomGlobals();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
async function mount(): Promise<void> {
|
|
47
|
+
container = document.body.appendChild(document.createElement('div'));
|
|
48
|
+
await act(async () => { render(h(Probe, {}), container as HTMLElement); });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Run one write and drain its promise chain plus the resulting renders. */
|
|
52
|
+
async function run(action: () => Promise<WriteOutcome>): Promise<boolean> {
|
|
53
|
+
let ok = false;
|
|
54
|
+
await act(async () => {
|
|
55
|
+
const pending = feedback!.run(action);
|
|
56
|
+
ok = await pending;
|
|
57
|
+
});
|
|
58
|
+
return ok;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe('useWriteFeedback', () => {
|
|
62
|
+
test('a landed write flashes saved instead of trusting the click', async () => {
|
|
63
|
+
await mount();
|
|
64
|
+
expect(await run(async () => ({ ok: true }))).toBe(true);
|
|
65
|
+
expect(feedback?.saved).toBe(true);
|
|
66
|
+
expect(feedback?.error).toBeNull();
|
|
67
|
+
expect(feedback?.saving).toBe(false);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('a refusal keeps the server’s sentence and shows no success', async () => {
|
|
71
|
+
await mount();
|
|
72
|
+
expect(await run(async () => ({ ok: false, error: 'A preset named “cheap” already exists' }))).toBe(false);
|
|
73
|
+
expect(feedback?.saved).toBe(false);
|
|
74
|
+
expect(feedback?.error).toBe('A preset named “cheap” already exists');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('a refusal without a sentence still says something actionable', async () => {
|
|
78
|
+
await mount();
|
|
79
|
+
await run(async () => ({ ok: false }));
|
|
80
|
+
expect(feedback?.error).toBe('The server refused the write.');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('a thrown write is reported rather than escaping to the console', async () => {
|
|
84
|
+
await mount();
|
|
85
|
+
expect(await run(async () => { throw new Error('network down'); })).toBe(false);
|
|
86
|
+
expect(feedback?.error).toBe('network down');
|
|
87
|
+
expect(feedback?.saving).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('a later success clears the previous refusal', async () => {
|
|
91
|
+
await mount();
|
|
92
|
+
await run(async () => ({ ok: false, error: 'first refusal' }));
|
|
93
|
+
await run(async () => ({ ok: true }));
|
|
94
|
+
expect(feedback?.error).toBeNull();
|
|
95
|
+
expect(feedback?.saved).toBe(true);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('dismissError clears the sentence without clearing a success', async () => {
|
|
99
|
+
await mount();
|
|
100
|
+
await run(async () => ({ ok: false, error: 'nope' }));
|
|
101
|
+
await act(async () => { feedback?.dismissError(); });
|
|
102
|
+
expect(feedback?.error).toBeNull();
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Write feedback for a settings pane: whether the last save/delete is still in
|
|
8
|
+
* flight, whether it landed, and the server's own sentence when it did not.
|
|
9
|
+
*
|
|
10
|
+
* The panes used to show "Saved successfully" the instant the button was
|
|
11
|
+
* pressed, before the request had answered — so a refused write (a duplicate
|
|
12
|
+
* agent name, a read-only native file, a validator rejecting the draft) showed
|
|
13
|
+
* a success badge over a row that had not changed. The outcome of the write is
|
|
14
|
+
* now the only thing that sets the badge.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { useCallback, useRef, useState } from 'preact/hooks';
|
|
18
|
+
import type { WriteOutcome } from '@/shared/types';
|
|
19
|
+
|
|
20
|
+
export interface WriteFeedback {
|
|
21
|
+
saving: boolean;
|
|
22
|
+
saved: boolean;
|
|
23
|
+
/** The last refusal's sentence; stays until the next write or dismissal. */
|
|
24
|
+
error: string | null;
|
|
25
|
+
dismissError: () => void;
|
|
26
|
+
/** Run one write and report its real outcome. Resolves `true` when it landed. */
|
|
27
|
+
run: (action: () => Promise<WriteOutcome>) => Promise<boolean>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const SAVED_FLASH_MS = 2000;
|
|
31
|
+
|
|
32
|
+
export function useWriteFeedback(): WriteFeedback {
|
|
33
|
+
const [saving, setSaving] = useState(false);
|
|
34
|
+
const [saved, setSaved] = useState(false);
|
|
35
|
+
const [error, setError] = useState<string | null>(null);
|
|
36
|
+
const flash = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
37
|
+
|
|
38
|
+
const dismissError = useCallback(() => setError(null), []);
|
|
39
|
+
|
|
40
|
+
const run = useCallback(async (action: () => Promise<WriteOutcome>): Promise<boolean> => {
|
|
41
|
+
setSaving(true);
|
|
42
|
+
setError(null);
|
|
43
|
+
clearTimeout(flash.current);
|
|
44
|
+
setSaved(false);
|
|
45
|
+
try {
|
|
46
|
+
const outcome = await action();
|
|
47
|
+
if (!outcome.ok) {
|
|
48
|
+
setError(outcome.error ?? 'The server refused the write.');
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
setSaved(true);
|
|
52
|
+
flash.current = setTimeout(() => setSaved(false), SAVED_FLASH_MS);
|
|
53
|
+
return true;
|
|
54
|
+
} catch (err) {
|
|
55
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
56
|
+
return false;
|
|
57
|
+
} finally {
|
|
58
|
+
setSaving(false);
|
|
59
|
+
}
|
|
60
|
+
}, []);
|
|
61
|
+
|
|
62
|
+
return { saving, saved, error, dismissError, run };
|
|
63
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The two post-mount hydration passes a rendered markdown document needs.
|
|
8
|
+
*
|
|
9
|
+
* Extracted from `MarkdownRenderer` for the per-file ceiling, and they belong in
|
|
10
|
+
* a hook because they are not rendering: both observe the container and swap
|
|
11
|
+
* placeholders for real content after the HTML string landed.
|
|
12
|
+
*
|
|
13
|
+
* ## Why an observer, in both cases
|
|
14
|
+
*
|
|
15
|
+
* The container's `innerHTML` can be replaced with an IDENTICAL string — the
|
|
16
|
+
* chat timeline re-renders a message, a preview re-renders after a save — and
|
|
17
|
+
* the effect does not re-run for an unchanged dependency. New placeholders would
|
|
18
|
+
* then sit unhydrated forever. Each hydrator writes only inside the placeholder
|
|
19
|
+
* elements it found, so it never re-triggers its own observer.
|
|
20
|
+
*
|
|
21
|
+
* ## KaTeX is lazy, mermaid is not
|
|
22
|
+
*
|
|
23
|
+
* `marked.ts` emits a `math-pending` span and `katex.ts` swaps it once ~522 kB
|
|
24
|
+
* of JS arrives; a message with no math must never pay for that fetch. Mermaid
|
|
25
|
+
* bakes the palette into its SVG, so every theme write has to re-render it — the
|
|
26
|
+
* trigger is the `data-theme` ATTRIBUTE rather than the `omp:theme-changed`
|
|
27
|
+
* event, because the navbar toggle and the settings modal are both writers and
|
|
28
|
+
* observing the attribute catches all of them.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import type { RefObject } from 'preact';
|
|
32
|
+
import { useEffect, useLayoutEffect } from 'preact/hooks';
|
|
33
|
+
|
|
34
|
+
import { hydrateMathBlocks } from '@/shared/lib/markdown/katex';
|
|
35
|
+
import { hydrateMermaidBlocks } from '@/shared/lib/markdown/mermaid';
|
|
36
|
+
|
|
37
|
+
const useIsomorphicLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
|
38
|
+
|
|
39
|
+
/** Replace `math-pending` placeholders for real KaTeX markup, when there are any. */
|
|
40
|
+
export function useMathHydration(container: RefObject<HTMLElement>, html: string, active: boolean): void {
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (!active || typeof window === 'undefined') return;
|
|
43
|
+
const element = container.current;
|
|
44
|
+
if (!element) return;
|
|
45
|
+
|
|
46
|
+
let cancelled = false;
|
|
47
|
+
const hydrate = async () => {
|
|
48
|
+
if (!element.isConnected) return;
|
|
49
|
+
await hydrateMathBlocks(element);
|
|
50
|
+
if (cancelled) return;
|
|
51
|
+
};
|
|
52
|
+
void hydrate();
|
|
53
|
+
const observer = new MutationObserver(() => {
|
|
54
|
+
if (element.isConnected) void hydrateMathBlocks(element);
|
|
55
|
+
});
|
|
56
|
+
observer.observe(element, { childList: true });
|
|
57
|
+
|
|
58
|
+
return () => {
|
|
59
|
+
cancelled = true;
|
|
60
|
+
observer.disconnect();
|
|
61
|
+
};
|
|
62
|
+
}, [container, active, html]);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface MermaidHydrationOptions {
|
|
66
|
+
container: RefObject<HTMLElement>;
|
|
67
|
+
html: string;
|
|
68
|
+
active: boolean;
|
|
69
|
+
/** The block the open diagram viewer was read from, so a theme write can re-read it. */
|
|
70
|
+
diagramBlock: RefObject<HTMLElement | null>;
|
|
71
|
+
/**
|
|
72
|
+
* The block the viewer is showing, re-rendered for the new palette.
|
|
73
|
+
*
|
|
74
|
+
* The caller reads it (`readDiagramBlock`) because the snapshot type belongs
|
|
75
|
+
* to the viewer, not to hydration — this module never needs to know what a
|
|
76
|
+
* diagram IS.
|
|
77
|
+
*/
|
|
78
|
+
onDiagramBlock: (block: HTMLElement) => void;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Draw every `mermaid-block` placeholder, and re-draw on a palette change. */
|
|
82
|
+
export function useMermaidHydration({
|
|
83
|
+
container,
|
|
84
|
+
html,
|
|
85
|
+
active,
|
|
86
|
+
diagramBlock,
|
|
87
|
+
onDiagramBlock,
|
|
88
|
+
}: MermaidHydrationOptions): void {
|
|
89
|
+
useIsomorphicLayoutEffect(() => {
|
|
90
|
+
if (!active || typeof window === 'undefined') return;
|
|
91
|
+
const element = container.current;
|
|
92
|
+
if (!element) return;
|
|
93
|
+
|
|
94
|
+
let timer: number | undefined;
|
|
95
|
+
const hydrate = async () => {
|
|
96
|
+
if (!element.isConnected) return;
|
|
97
|
+
await hydrateMermaidBlocks(element);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
void hydrate();
|
|
101
|
+
const observer = new MutationObserver(() => {
|
|
102
|
+
if (element.isConnected) void hydrateMermaidBlocks(element, { cachedOnly: true });
|
|
103
|
+
window.clearTimeout(timer);
|
|
104
|
+
timer = window.setTimeout(() => void hydrate(), 120);
|
|
105
|
+
});
|
|
106
|
+
observer.observe(element, { childList: true });
|
|
107
|
+
|
|
108
|
+
const rehydrateForTheme = async () => {
|
|
109
|
+
await hydrate();
|
|
110
|
+
const block = diagramBlock.current;
|
|
111
|
+
// A timeline re-render can replace the block under an open viewer; keep
|
|
112
|
+
// the last snapshot then rather than closing the viewer on the user.
|
|
113
|
+
if (!block?.isConnected) return;
|
|
114
|
+
onDiagramBlock(block);
|
|
115
|
+
};
|
|
116
|
+
const themeObserver = new MutationObserver(() => void rehydrateForTheme());
|
|
117
|
+
themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
|
|
118
|
+
|
|
119
|
+
return () => {
|
|
120
|
+
window.clearTimeout(timer);
|
|
121
|
+
observer.disconnect();
|
|
122
|
+
themeObserver.disconnect();
|
|
123
|
+
};
|
|
124
|
+
}, [container, html, active, diagramBlock, onDiagramBlock]);
|
|
125
|
+
}
|
|
@@ -21,7 +21,12 @@ import { Window } from 'happy-dom';
|
|
|
21
21
|
import { h, render } from 'preact';
|
|
22
22
|
import { act } from 'preact/test-utils';
|
|
23
23
|
|
|
24
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
isUpdateApplyResult,
|
|
26
|
+
UPDATE_RECHECK_MIN_GAP_MS,
|
|
27
|
+
useUpdates,
|
|
28
|
+
type UseUpdatesResult,
|
|
29
|
+
} from '@/client/hooks/ui/updates';
|
|
25
30
|
import type { RestartWatchDeps } from '@/client/hooks/ui/updates/restart';
|
|
26
31
|
import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
|
|
27
32
|
|
|
@@ -197,6 +202,100 @@ describe('an update that restarts its own server', () => {
|
|
|
197
202
|
});
|
|
198
203
|
});
|
|
199
204
|
|
|
205
|
+
describe('a console left open across a release', () => {
|
|
206
|
+
/** Count the checks a render has issued. */
|
|
207
|
+
const checks = (harness: Harness): number =>
|
|
208
|
+
harness.requests.filter((url) => url.includes('/api/updates/check')).length;
|
|
209
|
+
|
|
210
|
+
/** Move `Date.now` past the re-check floor for the duration of `run`. */
|
|
211
|
+
async function pastCooldown(run: () => Promise<void> | void): Promise<void> {
|
|
212
|
+
const realNow = Date.now;
|
|
213
|
+
const origin = realNow();
|
|
214
|
+
Date.now = () => origin + UPDATE_RECHECK_MIN_GAP_MS + 1;
|
|
215
|
+
try {
|
|
216
|
+
await run();
|
|
217
|
+
} finally {
|
|
218
|
+
Date.now = realNow;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
test('the tab becoming visible again re-reads the status', async () => {
|
|
223
|
+
const harness = await mount(
|
|
224
|
+
() => Response.json(checkPayload()),
|
|
225
|
+
() => ({ pid: 100, startedAt: '2026-10-15T00:00:00.000Z', uptime: 900 }),
|
|
226
|
+
);
|
|
227
|
+
expect(checks(harness)).toBe(1);
|
|
228
|
+
|
|
229
|
+
await pastCooldown(async () => {
|
|
230
|
+
win.document.dispatchEvent(new win.Event('visibilitychange'));
|
|
231
|
+
await act(async () => {});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
expect(checks(harness)).toBe(2);
|
|
235
|
+
expect(harness.updates.info?.kabmo.latest).toBe('2.0.0');
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test('the interval re-reads a console that never left the screen', async () => {
|
|
239
|
+
let tick: (() => void) | null = null;
|
|
240
|
+
const realSetInterval = win.setInterval;
|
|
241
|
+
const realClearInterval = win.clearInterval;
|
|
242
|
+
win.setInterval = ((handler: Parameters<typeof win.setInterval>[0]) => {
|
|
243
|
+
tick = handler as () => void;
|
|
244
|
+
return 1;
|
|
245
|
+
}) as unknown as typeof win.setInterval;
|
|
246
|
+
win.clearInterval = (() => undefined) as typeof win.clearInterval;
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
const harness = await mount(
|
|
250
|
+
() => Response.json(checkPayload()),
|
|
251
|
+
() => ({ pid: 100, startedAt: '2026-10-15T00:00:00.000Z', uptime: 900 }),
|
|
252
|
+
);
|
|
253
|
+
expect(checks(harness)).toBe(1);
|
|
254
|
+
|
|
255
|
+
await pastCooldown(async () => {
|
|
256
|
+
tick?.();
|
|
257
|
+
await act(async () => {});
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
expect(checks(harness)).toBe(2);
|
|
261
|
+
} finally {
|
|
262
|
+
win.setInterval = realSetInterval;
|
|
263
|
+
win.clearInterval = realClearInterval;
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test('a re-check inside the cooldown does not read npm again', async () => {
|
|
268
|
+
const harness = await mount(
|
|
269
|
+
() => Response.json(checkPayload()),
|
|
270
|
+
() => ({ pid: 100, startedAt: '2026-10-15T00:00:00.000Z', uptime: 900 }),
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
// `focus` and `visibilitychange` both fire on the same return to the app.
|
|
274
|
+
win.document.dispatchEvent(new win.Event('visibilitychange'));
|
|
275
|
+
win.dispatchEvent(new win.Event('focus'));
|
|
276
|
+
await act(async () => {});
|
|
277
|
+
|
|
278
|
+
expect(checks(harness)).toBe(1);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test('an unmounted console stops listening', async () => {
|
|
282
|
+
const harness = await mount(
|
|
283
|
+
() => Response.json(checkPayload()),
|
|
284
|
+
() => ({ pid: 100, startedAt: '2026-10-15T00:00:00.000Z', uptime: 900 }),
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
act(() => render(null, container));
|
|
288
|
+
|
|
289
|
+
await pastCooldown(async () => {
|
|
290
|
+
win.document.dispatchEvent(new win.Event('visibilitychange'));
|
|
291
|
+
win.dispatchEvent(new win.Event('focus'));
|
|
292
|
+
await act(async () => {});
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
expect(checks(harness)).toBe(1);
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
200
299
|
describe('an update that does not restart', () => {
|
|
201
300
|
test('re-checks instead of reloading', async () => {
|
|
202
301
|
const harness = await mount(
|