feat(history): add unified MCP execution history
This commit is contained in:
39 files changed
+1261
-87
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { useHistoryRetentionSetting } from "../useHistoryRetentionSetting";
|
||||
|
||||
vi.mock("@/lib/backend/api", () => ({
|
||||
loadHistoryRetentionLimit: vi.fn(),
|
||||
saveHistoryRetentionLimit: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("injected MCP history retention endpoints", () => {
|
||||
it("loads, saves unlimited, and resets through the independent endpoints", async () => {
|
||||
const loadMcp = vi.fn().mockResolvedValue(5000);
|
||||
const saveMcp = vi.fn().mockResolvedValue(undefined);
|
||||
const setting = useHistoryRetentionSetting(loadMcp, saveMcp);
|
||||
await setting.load();
|
||||
expect(loadMcp).toHaveBeenCalledOnce();
|
||||
expect(setting.draft.value).toBe(5000);
|
||||
expect(setting.changed.value).toBe(false);
|
||||
setting.draft.value = 0;
|
||||
await setting.save();
|
||||
expect(saveMcp).toHaveBeenLastCalledWith(0);
|
||||
expect(setting.changed.value).toBe(false);
|
||||
setting.reset();
|
||||
expect(setting.draft.value).toBe(1000);
|
||||
expect(setting.changed.value).toBe(true);
|
||||
await setting.save();
|
||||
expect(saveMcp).toHaveBeenLastCalledWith(1000);
|
||||
});
|
||||
|
||||
it("does not overwrite the server setting after a failed load", async () => {
|
||||
const saveMcp = vi.fn();
|
||||
const setting = useHistoryRetentionSetting(vi.fn().mockRejectedValue(new Error("offline")), saveMcp);
|
||||
await setting.load();
|
||||
expect(setting.loadError.value).toBe("offline");
|
||||
setting.draft.value = 200;
|
||||
await setting.save();
|
||||
expect(saveMcp).not.toHaveBeenCalled();
|
||||
expect(setting.loaded.value).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps failed saves dirty so they can be retried", async () => {
|
||||
const saveMcp = vi.fn().mockRejectedValueOnce(new Error("write failed")).mockResolvedValue(undefined);
|
||||
const setting = useHistoryRetentionSetting(vi.fn().mockResolvedValue(1000), saveMcp);
|
||||
await setting.load();
|
||||
setting.draft.value = 200;
|
||||
await expect(setting.save()).rejects.toThrow("write failed");
|
||||
expect(setting.saving.value).toBe(false);
|
||||
expect(setting.changed.value).toBe(true);
|
||||
await setting.save();
|
||||
expect(setting.changed.value).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -64,6 +64,41 @@ describe("usePanelResize", () => {
|
||||
expect(localStorage.getItem("dbx-ai-panel-width")).toBe("1060");
|
||||
});
|
||||
|
||||
it.each([0, 360])("lets history use the editor width with a %ipx AI sibling", (aiWidth) => {
|
||||
const editor = document.createElement("div");
|
||||
editor.setAttribute("data-editor-content", "");
|
||||
const ai = document.createElement("div");
|
||||
const panel = document.createElement("div");
|
||||
const handle = document.createElement("div");
|
||||
panel.append(handle);
|
||||
document.body.append(editor, ai, panel);
|
||||
vi.spyOn(editor, "getBoundingClientRect").mockReturnValue(rect(300, 900));
|
||||
vi.spyOn(ai, "getBoundingClientRect").mockReturnValue(rect(1200, aiWidth));
|
||||
vi.spyOn(panel, "getBoundingClientRect").mockReturnValue(rect(1200 + aiWidth, 288));
|
||||
|
||||
const { historyWidth, startHistoryResize } = usePanelResize();
|
||||
handle.addEventListener("pointerdown", startHistoryResize);
|
||||
handle.dispatchEvent(pointerEvent("pointerdown", 1200 + aiWidth));
|
||||
document.dispatchEvent(pointerEvent("pointermove", -1000));
|
||||
expect(panel.style.width).toBe("1188px");
|
||||
document.dispatchEvent(pointerEvent("pointerup", -1000));
|
||||
expect(historyWidth.value).toBe(1188);
|
||||
expect(localStorage.getItem("dbx-history-width")).toBeNull();
|
||||
|
||||
vi.spyOn(panel, "getBoundingClientRect").mockReturnValue(rect(300 + aiWidth, 1188));
|
||||
vi.spyOn(editor, "getBoundingClientRect").mockReturnValue(rect(300, 0));
|
||||
handle.dispatchEvent(pointerEvent("pointerdown", 300 + aiWidth));
|
||||
document.dispatchEvent(pointerEvent("pointermove", 500 + aiWidth));
|
||||
document.dispatchEvent(pointerEvent("pointerup", 500 + aiWidth));
|
||||
expect(historyWidth.value).toBe(988);
|
||||
});
|
||||
|
||||
it("starts at 60% on every application launch even when a legacy width is stored", () => {
|
||||
localStorage.setItem("dbx-history-width", String(window.innerWidth));
|
||||
const { historyWidth } = usePanelResize();
|
||||
expect(historyWidth.value).toBe(Math.round(window.innerWidth * 0.6));
|
||||
});
|
||||
|
||||
it("resizes from the flex-shrunk width after a wide panel is restored in a narrow window", () => {
|
||||
localStorage.setItem("dbx-ai-panel-width", "1060");
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ export function isHistoryRetentionLimit(value: unknown): value is number {
|
||||
}
|
||||
|
||||
/** A server-owned setting: browser localStorage cannot configure MCP retention. */
|
||||
export function useHistoryRetentionSetting() {
|
||||
export function useHistoryRetentionSetting(loadLimit: () => Promise<number> = loadHistoryRetentionLimit, saveLimit: (limit: number) => Promise<void> = saveHistoryRetentionLimit) {
|
||||
const draft = ref(DEFAULT_HISTORY_RETENTION_LIMIT);
|
||||
const persisted = ref(DEFAULT_HISTORY_RETENTION_LIMIT);
|
||||
const loaded = ref(false);
|
||||
@@ -26,7 +26,7 @@ export function useHistoryRetentionSetting() {
|
||||
loading.value = true;
|
||||
loadError.value = "";
|
||||
try {
|
||||
const value = await loadHistoryRetentionLimit();
|
||||
const value = await loadLimit();
|
||||
if (!isHistoryRetentionLimit(value)) throw new Error("Invalid query history retention limit");
|
||||
if (request !== generation) return;
|
||||
draft.value = persisted.value = value;
|
||||
@@ -44,7 +44,7 @@ export function useHistoryRetentionSetting() {
|
||||
const value = draft.value;
|
||||
saving.value = true;
|
||||
try {
|
||||
await saveHistoryRetentionLimit(value);
|
||||
await saveLimit(value);
|
||||
persisted.value = value;
|
||||
} finally {
|
||||
saving.value = false;
|
||||
|
||||
@@ -14,6 +14,11 @@ function restoredPanelWidth(storageKey: string, fallback: number): number {
|
||||
return Math.max(PANEL_MIN_WIDTH, Number(safeLocalStorageGet(storageKey)) || fallback);
|
||||
}
|
||||
|
||||
function restoredHistoryPanelWidth(): number {
|
||||
const fallback = Math.round(window.innerWidth * 0.6);
|
||||
return Math.max(PANEL_MIN_WIDTH, fallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefer the layout-owning panel root over the handle's immediate parent.
|
||||
* Vertical tab handles live inside a nested `.app-tab-bar`, while width is
|
||||
@@ -41,10 +46,21 @@ function availableAiPanelMaxWidth(handle: HTMLElement | null) {
|
||||
return Math.max(PANEL_MIN_WIDTH, panelRect.width + panelRect.left - contentRect.left);
|
||||
}
|
||||
|
||||
// History may have another auxiliary panel (including a hidden AI panel)
|
||||
// before it. Only borrow space from the editor, never from that sibling.
|
||||
function availableHistoryPanelMaxWidth(handle: HTMLElement | null) {
|
||||
const panel = resolveResizePanel(handle);
|
||||
const editor = panel?.parentElement?.querySelector<HTMLElement>(":scope > [data-editor-content]");
|
||||
if (!panel || !editor) return availableAiPanelMaxWidth(handle);
|
||||
return Math.max(PANEL_MIN_WIDTH, panel.getBoundingClientRect().width + editor.getBoundingClientRect().width);
|
||||
}
|
||||
|
||||
export function usePanelResize() {
|
||||
const sidebarWidth = ref(restoredPanelWidth("dbx-sidebar-width", 260));
|
||||
const aiPanelWidth = ref(restoredPanelWidth("dbx-ai-panel-width", 360));
|
||||
const historyWidth = ref(restoredPanelWidth("dbx-history-width", 288));
|
||||
// History opens at a comfortable content-area width; users can still resize
|
||||
// it or switch to the explicit full-width mode from its toolbar.
|
||||
const historyWidth = ref(restoredHistoryPanelWidth());
|
||||
const sqlLibraryWidth = ref(restoredPanelWidth("dbx-sql-library-width", 288));
|
||||
const sqlFilePanelWidth = ref(restoredPanelWidth("dbx-sql-file-panel-width", 288));
|
||||
const tabBarWidth = ref(restoredPanelWidth("dbx-tab-bar-width", 240));
|
||||
@@ -108,7 +124,9 @@ export function usePanelResize() {
|
||||
endPanelResize();
|
||||
resizeOverlay.remove();
|
||||
widthRef.value = currentWidth;
|
||||
safeLocalStorageSet(storageKey, String(widthRef.value));
|
||||
// History deliberately starts at 60% on every application launch;
|
||||
// resizing remains available for the current session only.
|
||||
if (storageKey !== "dbx-history-width") safeLocalStorageSet(storageKey, String(widthRef.value));
|
||||
};
|
||||
|
||||
document.addEventListener("pointermove", onPointerMove);
|
||||
@@ -120,7 +138,7 @@ export function usePanelResize() {
|
||||
|
||||
const startSidebarResize = startPanelResize(sidebarWidth, "dbx-sidebar-width", "right");
|
||||
const startAiPanelResize = startPanelResize(aiPanelWidth, "dbx-ai-panel-width", "left", availableAiPanelMaxWidth);
|
||||
const startHistoryResize = startPanelResize(historyWidth, "dbx-history-width", "left");
|
||||
const startHistoryResize = startPanelResize(historyWidth, "dbx-history-width", "left", availableHistoryPanelMaxWidth);
|
||||
const startSqlLibraryResize = startPanelResize(sqlLibraryWidth, "dbx-sql-library-width", "left");
|
||||
const startSqlFilePanelResize = startPanelResize(sqlFilePanelWidth, "dbx-sql-file-panel-width", "left");
|
||||
const startLeftTabBarResize = startPanelResize(tabBarWidth, "dbx-tab-bar-width", "right");
|
||||
|
||||
Reference in new issue
Block a user