feat(history): add unified MCP execution history

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-26 22:18:38 +08:00
1 parent dd82d948a9
commit 5c0ce7e4dc
39 files changed
+1261 -87

No files matched your search

@@ -1,4 +1,5 @@
<script setup lang="ts">
import { cleanupMcpHistoryRetention, loadMcpHistoryRetentionLimit, saveMcpHistoryRetentionLimit } from "@/lib/backend/api";
import { HISTORY_RETENTION_LIMITS, useHistoryRetentionSetting } from "@/composables/useHistoryRetentionSetting";
import { ref, watch, shallowRef, computed, onMounted, onUnmounted, nextTick } from "vue";
import type { Ref } from "vue";
@@ -305,7 +306,10 @@ const { t, locale } = useI18n();
const { toast } = useToast();
const settingsStore = useSettingsStore();
const historyRetention = useHistoryRetentionSetting();
const mcpHistoryRetention = useHistoryRetentionSetting(loadMcpHistoryRetentionLimit, saveMcpHistoryRetentionLimit);
const { draft: editHistoryRetentionLimit, loaded: historyRetentionLoaded, loading: historyRetentionLoading, saving: historyRetentionSaving, loadError: historyRetentionLoadError } = historyRetention;
const { draft: editMcpHistoryRetentionLimit, loaded: mcpHistoryRetentionLoaded, loading: mcpHistoryRetentionLoading, saving: mcpHistoryRetentionSaving, loadError: mcpHistoryRetentionLoadError } = mcpHistoryRetention;
const mcpHistoryCleanupLoading = ref(false);
const connectionStore = useConnectionStore();
const hasSqlServerConnection = computed(() => connectionStore.connections.some((connection) => effectiveDatabaseTypeForConnection(connection) === "sqlserver"));
const savedSqlStore = useSavedSqlStore();
@@ -1908,6 +1912,7 @@ watch(
if (open) {
syncEditorSettingsDraftFromStore();
void historyRetention.load();
void mcpHistoryRetention.load();
editShowTrayIcon.value = settingsStore.desktopSettings.show_tray_icon;
editQuitOnClose.value = settingsStore.desktopSettings.quit_on_close;
editIconTheme.value = settingsStore.desktopSettings.icon_theme;
@@ -1917,6 +1922,7 @@ watch(
editSidebarTablePageSize.value = settingsStore.desktopSettings.sidebar_table_page_size ?? DEFAULT_SIDEBAR_TABLE_PAGE_SIZE;
} else {
historyRetention.discard();
mcpHistoryRetention.discard();
clearThemePaletteOptionPreview();
clearUiFontOptionPreview();
restoreLocaleOptionPreview();
@@ -2116,11 +2122,25 @@ const queryResultRowLimitDraftTouched = computed(
normalizeTableOpenPageSizeDraft(editPageSize.value) !== normalizeTableOpenPageSizeDraft(editEditorSettingsBase.value.pageSize),
);
const hasBlockingQueryResultRowLimit = computed(() => queryResultRowLimitViolated.value && queryResultRowLimitDraftTouched.value);
const hasApplyBlocker = computed(() => historyRetention.invalid.value || historyRetentionSaving.value || hasBlockingShortcutConflicts.value || hasBlockingFormatterConfig.value || hasBlockingQueryResultRowLimit.value);
const hasApplyBlocker = computed(() => historyRetention.invalid.value || mcpHistoryRetention.invalid.value || historyRetentionSaving.value || mcpHistoryRetentionSaving.value || hasBlockingShortcutConflicts.value || hasBlockingFormatterConfig.value || hasBlockingQueryResultRowLimit.value);
async function cleanupMcpHistory() {
if (mcpHistoryCleanupLoading.value) return;
mcpHistoryCleanupLoading.value = true;
try {
const deleted = await cleanupMcpHistoryRetention();
toast(t("settings.mcpHistoryCleanupCompleted", { count: deleted }));
} catch (error) {
toast(t("settings.mcpHistoryCleanupFailed", { error: error instanceof Error ? error.message : String(error) }), 5000);
} finally {
mcpHistoryCleanupLoading.value = false;
}
}
function hasChanges(): boolean {
return (
historyRetention.changed.value ||
mcpHistoryRetention.changed.value ||
hasImportedSettingsPendingApply.value ||
hasEditorDraftChanges.value ||
editShowTrayIcon.value !== settingsStore.desktopSettings.show_tray_icon ||
@@ -2137,6 +2157,7 @@ function hasChanges(): boolean {
async function persistSettings() {
if (hasApplyBlocker.value) return;
await historyRetention.save();
await mcpHistoryRetention.save();
const editorSettingsPatch = editorSettingsPatchFromDraft(currentEditorSettingsDraft(), editEditorSettingsBase.value);
const sidebarObjectDisplayChanged = editorSettingsPatch.sidebarObjectDisplay !== undefined && editorSettingsPatch.sidebarObjectDisplay !== settingsStore.editorSettings.sidebarObjectDisplay;
const sidebarTablePageSizeChanged = editSidebarTablePageSize.value !== (settingsStore.desktopSettings.sidebar_table_page_size ?? DEFAULT_SIDEBAR_TABLE_PAGE_SIZE);
@@ -2316,6 +2337,7 @@ function resetDefaultsForTab(tab: SettingsCategory) {
editToolbarItems.value = { ...DEFAULT_EDITOR_SETTINGS.toolbarItems };
} else if (tab === "data") {
historyRetention.reset();
mcpHistoryRetention.reset();
editShowColumnCommentsInHeader.value = DEFAULT_EDITOR_SETTINGS.showColumnCommentsInHeader;
editShowColumnTypesInHeader.value = DEFAULT_EDITOR_SETTINGS.showColumnTypesInHeader;
editShowColumnHeaderTooltips.value = DEFAULT_EDITOR_SETTINGS.showColumnHeaderTooltips;
@@ -2377,6 +2399,7 @@ function resetDefaultsForTab(tab: SettingsCategory) {
function resetAllDefaults() {
historyRetention.reset();
mcpHistoryRetention.reset();
// Same contract as resetDefaultsForTab: a full reset also exits any
// in-progress shortcut capture (#9066).
editingShortcutId.value = null;
@@ -7681,11 +7704,13 @@ onUnmounted(() => {
<!-- Data Tab -->
<section v-else-if="activeSettingsTab === 'data'" data-settings-search-id="data" :class="['flex flex-col gap-5 py-2', settingsSearchTargetClass('data')]">
<div data-settings-search-id="history-retention" :class="['space-y-2', settingsSearchTargetClass('history-retention')]">
<Label for="history-retention-limit">{{ t("settings.historyRetentionLimit") }}</Label>
<p class="text-xs text-muted-foreground">{{ t("settings.historyRetentionDescription") }}</p>
<div data-settings-search-id="history-retention" :class="['flex items-center justify-between gap-4 rounded-md border bg-muted/20 px-3 py-2', settingsSearchTargetClass('history-retention')]">
<div class="min-w-0 space-y-1">
<Label for="history-retention-limit">{{ t("settings.historyRetentionLimit") }}</Label>
<p class="text-xs text-muted-foreground">{{ t("settings.historyRetentionDescription") }}</p>
</div>
<Select :model-value="String(editHistoryRetentionLimit)" :disabled="!historyRetentionLoaded || historyRetentionSaving" @update:model-value="(value) => (editHistoryRetentionLimit = Number(value))">
<SelectTrigger id="history-retention-limit" class="w-40"><SelectValue /></SelectTrigger>
<SelectTrigger id="history-retention-limit" class="w-40 shrink-0"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem v-for="limit in HISTORY_RETENTION_LIMITS" :key="limit" :value="String(limit)">{{ limit === 0 ? t("settings.historyRetentionUnlimited") : String(limit) }}</SelectItem>
</SelectContent>
@@ -7696,6 +7721,28 @@ onUnmounted(() => {
<Button type="button" variant="outline" size="sm" @click="historyRetention.load">{{ t("common.retry") }}</Button>
</div>
</div>
<div data-settings-search-id="mcp-history-retention" :class="['flex items-center justify-between gap-4 rounded-md border bg-muted/20 px-3 py-2', settingsSearchTargetClass('mcp-history-retention')]">
<div class="min-w-0 space-y-1">
<Label for="mcp-history-retention-limit">{{ t("settings.mcpHistoryRetentionLimit") }}</Label>
<p class="text-xs text-muted-foreground">{{ t("settings.mcpHistoryRetentionDescription") }}</p>
</div>
<div class="flex shrink-0 items-center gap-2">
<Select :model-value="String(editMcpHistoryRetentionLimit)" :disabled="!mcpHistoryRetentionLoaded || mcpHistoryRetentionSaving" @update:model-value="(value) => (editMcpHistoryRetentionLimit = Number(value))">
<SelectTrigger id="mcp-history-retention-limit" class="w-40"><SelectValue /></SelectTrigger>
<SelectContent
><SelectItem v-for="limit in HISTORY_RETENTION_LIMITS" :key="limit" :value="String(limit)">{{ limit === 0 ? t("settings.historyRetentionUnlimited") : String(limit) }}</SelectItem></SelectContent
>
</Select>
<p v-if="mcpHistoryRetentionLoading" class="text-xs text-muted-foreground">{{ t("common.loading") }}</p>
<div v-if="mcpHistoryRetentionLoadError" class="flex items-center gap-2 text-xs text-destructive" role="alert">
<span>{{ t("settings.historyRetentionLoadFailed", { error: mcpHistoryRetentionLoadError }) }}</span>
<Button type="button" variant="outline" size="sm" @click="mcpHistoryRetention.load">{{ t("common.retry") }}</Button>
</div>
<Button type="button" variant="outline" size="sm" :disabled="mcpHistoryCleanupLoading || !mcpHistoryRetentionLoaded" @click="cleanupMcpHistory">
{{ mcpHistoryCleanupLoading ? t("common.loading") : t("settings.mcpHistoryCleanup") }}
</Button>
</div>
</div>
<div id="data-grid-toolbar-layout" data-settings-search-id="data-grid-toolbar-layout" :class="['settings-item flex items-center justify-between gap-4 rounded-md border bg-muted/20 px-3 py-2', settingsSearchTargetClass('data-grid-toolbar-layout')]">
<div class="min-w-0 space-y-1">
<Label for="data-grid-toolbar-layout-select">{{ t("settings.dataGridToolbarLayout") }}</Label>
@@ -2,7 +2,7 @@
import { ref, computed, nextTick, onBeforeUnmount, onMounted, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useSqlHighlighter } from "@/composables/useSqlHighlighter";
import { CalendarClock, Check, Copy, Database, ListFilter, LoaderCircle, Minus, RotateCcw, Search, Sparkles, Trash2, X } from "@lucide/vue";
import { CalendarClock, Check, Copy, Database, ListFilter, LoaderCircle, Maximize2, Minimize2, Minus, RefreshCw, RotateCcw, Search, Sparkles, Trash2, X } from "@lucide/vue";
import { RecycleScroller } from "vue-virtual-scroller";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";
@@ -34,18 +34,24 @@ const connectionStore = useConnectionStore();
const props = defineProps<{
currentConnectionId?: string;
currentDatabase?: string;
maximized?: boolean;
}>();
const emit = defineEmits<{
restore: [sql: string, entry: HistoryEntry];
analyzeAi: [entry: HistoryEntry];
close: [];
toggleMaximize: [];
}>();
type HistoryFilter = "all" | "query" | "data_change" | "schema_change" | "failed";
type HistoryFilter = "all" | "query" | "data_change" | "schema_change" | "failed" | "mcp_success";
type HistorySource = "sql" | "mcp";
const searchText = ref("");
const mcpToolName = ref("");
const mcpToolOptions = ref<string[]>([]);
const activeFilter = ref<HistoryFilter>("all");
const activeSource = ref<HistorySource>("sql");
const dateRange = ref<HistoryDateRange>({ startDate: "", endDate: "" });
const dateRangeDraft = ref<HistoryDateRange>({ startDate: "", endDate: "" });
const dateRangeOpen = ref(false);
@@ -71,7 +77,9 @@ const filtersScrollable = ref(false);
let filterScrollResizeObserver: ResizeObserver | null = null;
let searchDebounceTimer: ReturnType<typeof setTimeout> | null = null;
const filters: HistoryFilter[] = ["all", "query", "data_change", "schema_change", "failed"];
const filters = computed<HistoryFilter[]>(() => (activeSource.value === "mcp" ? ["all", "failed", "mcp_success"] : ["all", "query", "data_change", "schema_change", "failed"]));
const mcpQuickTools = computed(() => mcpToolOptions.value);
const canClearHistory = computed(() => store.total > 0);
const hasDateFilter = computed(() => hasHistoryDateRange(dateRange.value));
const dateRangeDraftValid = computed(() => historyDateRangeIsValid(dateRangeDraft.value));
const dateRangeSummary = computed(() => {
@@ -95,6 +103,10 @@ function activityKind(entry: HistoryEntry) {
return resolveHistoryActivityKind(entry);
}
function isMcpEntry(entry: HistoryEntry) {
return entry.source === "mcp" || historyEntrySource(entry) === "MCP";
}
function restore(entry: HistoryEntry) {
emit("restore", entry.sql, entry);
selectedEntry.value = null;
@@ -123,13 +135,13 @@ function executeDelete() {
}
function confirmClearHistory() {
if (store.total > 0 || store.connectionOptions.length > 0) {
if (canClearHistory.value) {
showClearConfirm.value = true;
}
}
function executeClear() {
store.clear();
void store.clear(activeSource.value);
showClearConfirm.value = false;
}
@@ -149,11 +161,31 @@ function truncateSql(sql: string): string {
return line.length > 120 ? line.slice(0, 120) + "..." : line;
}
function formatHistoryJson(value?: string | null): string {
if (!value) return "-";
try {
return JSON.stringify(JSON.parse(value), null, 2);
} catch {
return value;
}
}
function isLegacyResponse(value?: string | null): boolean {
try {
const parsed = JSON.parse(value || "null");
return !!parsed && typeof parsed.content_blocks === "number" && !("content" in parsed);
} catch {
return false;
}
}
function entryTitle(entry: HistoryEntry) {
if (isMcpEntry(entry)) return entry.mcp_tool_name || entry.operation || "MCP call";
return entry.target || entry.operation || truncateSql(entry.sql);
}
function entrySubtitle(entry: HistoryEntry) {
if (isMcpEntry(entry)) return truncateSql(entry.mcp_request_json || entry.target || entry.operation || "-");
if (activityKind(entry) === "query") return truncateSql(entry.sql);
return truncateSql(entry.sql || entry.target || entry.operation || "");
}
@@ -266,11 +298,13 @@ function buildHistorySearchRequest(): HistorySearchRequest {
search_text: searchText.value,
connections: selectedConnections.value.map((connection) => ({ ...connection })),
databases: selectedDatabases.value.map((database) => ({ ...database })),
activity_kind: activeFilter.value !== "all" && activeFilter.value !== "failed" ? activeFilter.value : undefined,
success: activeFilter.value === "failed" ? false : undefined,
activity_kind: activeFilter.value !== "all" && activeFilter.value !== "failed" && activeFilter.value !== "mcp_success" ? activeFilter.value : undefined,
success: activeFilter.value === "failed" ? false : activeFilter.value === "mcp_success" ? true : undefined,
started_at: localDateBoundary(dateRange.value.startDate, false),
ended_at: localDateBoundary(dateRange.value.endDate, true),
limit: 100,
source: activeSource.value,
mcp_tool_name: activeSource.value === "mcp" ? mcpToolName.value.trim() || undefined : undefined,
};
}
@@ -282,6 +316,10 @@ async function runHistorySearch() {
}
}
function refreshHistory() {
void runHistorySearch();
}
// Debounce all filter changes through one timer to avoid redundant history scans.
function scheduleHistorySearch() {
if (searchDebounceTimer) clearTimeout(searchDebounceTimer);
@@ -362,6 +400,7 @@ function detailsRows(entry: HistoryEntry) {
[t("history.detail.rollback"), canRollbackHistoryEntry(entry) ? t("history.rollbackAvailable") : t("history.rollbackUnavailable")],
[t("history.detail.status"), entry.success ? t("history.success") : t("history.failed")],
];
if (entry.source === "mcp" || historyEntrySource(entry) === "MCP") rows.splice(1, 0, [t("history.detail.mcpTool"), entry.mcp_tool_name || entry.operation || "-"]);
if (entry.error) rows.push([t("history.detail.error"), entry.error]);
return rows;
}
@@ -477,6 +516,7 @@ async function closeHistory() {
}
function getHistoryMenuItems(entry: HistoryEntry): ContextMenuItem[] {
const isMcp = isMcpEntry(entry);
return [
{
label: t("history.viewDetails"),
@@ -484,22 +524,35 @@ function getHistoryMenuItems(entry: HistoryEntry): ContextMenuItem[] {
selectedEntry.value = entry;
},
},
{ label: t("history.restore"), action: () => restore(entry) },
{ label: t("history.analyzeWithAi"), action: () => emit("analyzeAi", entry), icon: Sparkles },
{ label: t("history.copy"), action: () => copyText(entry.sql) },
...(canRollbackHistoryEntry(entry) ? [{ label: t("history.rollback"), action: () => rollback(entry) }] : []),
...(isMcp
? []
: [
{ label: t("history.restore"), action: () => restore(entry) },
{ label: t("history.analyzeWithAi"), action: () => emit("analyzeAi", entry), icon: Sparkles },
{ label: t("history.copy"), action: () => copyText(entry.sql) },
...(canRollbackHistoryEntry(entry) ? [{ label: t("history.rollback"), action: () => rollback(entry) }] : []),
]),
{ label: t("history.delete"), action: () => confirmDeleteEntry(entry.id), variant: "destructive" as const },
];
}
watch(activeSource, () => {
void nextTick(updateFilterScrollability);
});
watch(
() => filters.map((filter) => filterLabel(filter)).join("\0"),
() => {
void nextTick(updateFilterScrollability);
() => store.entries,
(entries) => {
const known = new Set(mcpToolOptions.value);
for (const entry of entries) {
if (isMcpEntry(entry) && entry.mcp_tool_name) known.add(entry.mcp_tool_name);
}
mcpToolOptions.value = [...known].sort().slice(0, 50);
},
{ deep: true },
);
watch([searchText, activeFilter, () => dateRange.value.startDate, () => dateRange.value.endDate, () => selectedConnections.value.map(connectionKey).join("\0"), () => selectedDatabases.value.map(databaseKey).join("\0")], scheduleHistorySearch);
watch([searchText, mcpToolName, activeSource, activeFilter, () => dateRange.value.startDate, () => dateRange.value.endDate, () => selectedConnections.value.map(connectionKey).join("\0"), () => selectedDatabases.value.map(databaseKey).join("\0")], scheduleHistorySearch);
onMounted(() => {
store.setHistoryPanelActive(true);
@@ -530,9 +583,16 @@ onBeforeUnmount(() => {
<span class="text-xs font-medium">{{ t("history.title") }}</span>
<span v-if="store.total > 0" class="text-[10px] text-muted-foreground">{{ store.total }}</span>
<span class="flex-1" />
<Button v-if="store.total > 0 || store.connectionOptions.length > 0" variant="ghost" size="icon" class="h-5 w-5" @click="confirmClearHistory">
<Button v-if="canClearHistory" variant="ghost" size="icon" class="h-5 w-5" @click="confirmClearHistory">
<Trash2 class="h-3 w-3" />
</Button>
<Button variant="ghost" size="icon" class="h-5 w-5" :disabled="store.loading" :title="t('history.refresh')" :aria-label="t('history.refresh')" @click="refreshHistory">
<RefreshCw class="h-3 w-3" :class="{ 'animate-spin': store.loading }" />
</Button>
<Button variant="ghost" size="icon" class="h-5 w-5" :title="t(maximized ? 'diff.restore' : 'diff.maximize')" :aria-label="t(maximized ? 'diff.restore' : 'diff.maximize')" :aria-pressed="!!maximized" @click="emit('toggleMaximize')">
<Minimize2 v-if="maximized" class="h-3 w-3" />
<Maximize2 v-else class="h-3 w-3" />
</Button>
<Button variant="ghost" size="icon" class="h-5 w-5" :aria-label="t('common.close')" :disabled="resolvingTransaction || (isRollingBack && !!pendingRollback)" @click="closeHistory">
<X class="h-3 w-3" />
</Button>
@@ -545,14 +605,42 @@ onBeforeUnmount(() => {
</div>
<div class="border-b shrink-0">
<div class="flex gap-1 px-2 pt-2">
<button
v-for="source in ['sql', 'mcp'] as HistorySource[]"
:key="source"
type="button"
class="h-7 rounded border px-3 text-xs"
:class="activeSource === source ? 'border-primary bg-primary text-primary-foreground' : 'bg-background'"
@click="
activeSource = source;
activeFilter = 'all';
"
>
{{ t(`history.sources.${source}`) }}
</button>
</div>
<div ref="filterScrollRef" class="history-filter-scroll flex gap-1 px-2 pt-2" :class="{ 'history-filter-scroll--scrollable': filtersScrollable }">
<button v-for="filter in filters" :key="filter" type="button" class="h-6 shrink-0 rounded border px-2 text-xs" :class="activeFilter === filter ? 'border-primary bg-primary text-primary-foreground' : 'bg-background'" @click="activeFilter = filter">
{{ filterLabel(filter) }}
</button>
</div>
<div class="relative flex items-center px-2 py-1">
<Search class="absolute left-3 w-3 h-3 text-muted-foreground pointer-events-none" />
<input data-history-search v-model="searchText" autocapitalize="off" autocorrect="off" spellcheck="false" class="flex-1 h-5 text-xs bg-transparent border rounded pl-5 pr-1 outline-none placeholder:text-muted-foreground" :placeholder="t('history.search')" />
<select v-if="activeSource === 'mcp'" data-mcp-tool-search v-model="mcpToolName" class="mr-1 h-5 max-w-52 rounded border bg-background px-1 text-xs outline-none" :title="t('history.mcp.toolSearch')">
<option value="">{{ t("history.mcp.allTools") }}</option>
<option v-for="tool in mcpQuickTools" :key="tool" :value="tool">{{ tool }}</option>
</select>
<Search v-if="activeSource !== 'mcp'" class="absolute left-3 w-3 h-3 text-muted-foreground pointer-events-none" />
<input
data-history-search
v-model="searchText"
autocapitalize="off"
autocorrect="off"
spellcheck="false"
class="flex-1 h-5 text-xs bg-transparent border rounded pr-1 outline-none placeholder:text-muted-foreground"
:class="activeSource === 'mcp' ? 'pl-1' : 'pl-5'"
:placeholder="t('history.search')"
/>
<Popover :open="dateRangeOpen" @update:open="setDateRangeOpen">
<PopoverTrigger as-child>
<button
@@ -688,28 +776,30 @@ onBeforeUnmount(() => {
<RecycleScroller v-if="shouldVirtualizeHistory(store.entries.length)" class="min-h-0 flex-1" :items="store.entries" :item-size="HISTORY_ROW_HEIGHT" :buffer="HISTORY_SCROLL_BUFFER" :skip-hover="true" key-field="id">
<template #default="{ item: entry }">
<CustomContextMenu :items="getHistoryMenuItems(entry)" v-slot="{ onContextMenu, isOpen }">
<div class="h-[72px] cursor-pointer select-none border-b border-border/50 px-3 py-2 text-xs" :class="isOpen || selectedEntry?.id === entry.id ? 'bg-accent text-accent-foreground' : 'hover:bg-accent/40'" @click="selectedEntry = entry" @contextmenu="onContextMenu">
<div class="mb-0.5 flex items-center gap-1">
<span class="inline-flex h-5 w-9 shrink-0 items-center justify-center rounded border px-1 text-[10px] leading-none text-muted-foreground">
{{ kindShortLabel(entry) }}
</span>
<span class="truncate font-medium">{{ entryTitle(entry) }}</span>
<span v-if="historyEntrySource(entry)" class="inline-flex h-5 shrink-0 items-center rounded border border-primary/30 bg-primary/5 px-1 text-[10px] font-medium text-primary">
{{ historyEntrySource(entry) }}
</span>
<span class="ml-auto shrink-0 text-muted-foreground">{{ formatTime(entry.executed_at) }}</span>
</div>
<div class="truncate font-mono text-muted-foreground">{{ entrySubtitle(entry) }}</div>
<div class="mt-0.5 flex items-center gap-2">
<span class="inline-flex min-w-0 items-center gap-1 text-muted-foreground">
<Database class="h-3 w-3 shrink-0" />
<span class="truncate">
{{ entry.connection_name }}<template v-if="entry.database"> / {{ entry.database }}</template>
<div class="h-[80px] cursor-pointer select-none border-b border-border/50 px-2 py-1.5 text-xs" :class="isOpen || selectedEntry?.id === entry.id ? 'bg-accent text-accent-foreground' : ''" @click="selectedEntry = entry" @contextmenu="onContextMenu">
<div class="h-full rounded-md border bg-card px-2 py-1.5 shadow-sm transition-colors hover:border-primary/30 hover:bg-accent/40">
<div class="mb-0.5 flex items-center gap-1">
<span class="inline-flex h-5 w-9 shrink-0 items-center justify-center overflow-hidden rounded border px-1 text-[10px] leading-none text-muted-foreground">
{{ isMcpEntry(entry) ? "MCP" : kindShortLabel(entry) }}
</span>
</span>
<span class="ml-auto shrink-0" :class="entry.success ? 'text-green-500' : 'text-red-500'">
{{ entry.success ? formatQueryDuration(entry.execution_time_ms) : t("history.failed") }}
</span>
<span class="truncate font-medium">{{ entryTitle(entry) }}</span>
<span v-if="historyEntrySource(entry) && !isMcpEntry(entry)" class="inline-flex h-5 shrink-0 items-center rounded border border-primary/30 bg-primary/5 px-1 text-[10px] font-medium text-primary">
{{ historyEntrySource(entry) }}
</span>
<span class="ml-auto shrink-0 text-muted-foreground">{{ formatTime(entry.executed_at) }}</span>
</div>
<div class="truncate font-mono text-muted-foreground">{{ entrySubtitle(entry) }}</div>
<div class="mt-0.5 flex items-center gap-2">
<span class="inline-flex min-w-0 items-center gap-1 text-muted-foreground">
<Database class="h-3 w-3 shrink-0" />
<span class="truncate">
{{ entry.connection_name || entry.target || t("history.scope.unknownConnection") }}<template v-if="entry.database"> / {{ entry.database }}</template>
</span>
</span>
<span class="ml-auto shrink-0" :class="entry.success ? 'text-green-500' : 'text-red-500'">
{{ entry.success ? formatQueryDuration(entry.execution_time_ms) : t("history.failed") }}
</span>
</div>
</div>
</div>
</CustomContextMenu>
@@ -744,7 +834,24 @@ onBeforeUnmount(() => {
<div class="min-w-0 break-words">{{ value }}</div>
</template>
</div>
<div>
<div v-if="selectedEntry.source === 'mcp' || historyEntrySource(selectedEntry) === 'MCP'" class="space-y-2">
<div class="mb-1 flex items-center justify-between">
<div class="text-sm font-medium">{{ t("history.mcp.request") }}</div>
<Button variant="ghost" size="icon" class="h-7 w-7" :title="t('history.copy')" :aria-label="t('history.copy')" @click="copyText(selectedEntry.mcp_request_json || selectedEntry.sql || selectedEntry.details_json || '-')">
<Copy class="h-3.5 w-3.5" />
</Button>
</div>
<pre class="max-h-64 overflow-auto rounded border bg-muted/30 p-3 text-xs">{{ formatHistoryJson(selectedEntry.mcp_request_json || selectedEntry.sql || selectedEntry.details_json) }}</pre>
<div class="mb-1 flex items-center justify-between">
<div class="text-sm font-medium">{{ t("history.mcp.response") }}</div>
<Button variant="ghost" size="icon" class="h-7 w-7" :title="t('history.copy')" :aria-label="t('history.copy')" @click="copyText(selectedEntry.mcp_response_json || '-')">
<Copy class="h-3.5 w-3.5" />
</Button>
</div>
<p v-if="isLegacyResponse(selectedEntry.mcp_response_json)" class="text-xs text-muted-foreground">{{ t("history.mcp.legacyResponse") }}</p>
<pre class="max-h-[min(50vh,32rem)] overflow-auto rounded border bg-muted/30 p-3 text-xs">{{ formatHistoryJson(selectedEntry.mcp_response_json) }}</pre>
</div>
<div v-else>
<div class="mb-1 flex items-center justify-between">
<div class="text-sm font-medium">SQL</div>
<Button variant="ghost" size="sm" class="h-7" @click="copyText(selectedEntry.sql)">
@@ -766,11 +873,11 @@ onBeforeUnmount(() => {
</div>
</div>
<DialogFooter>
<Button variant="outline" @click="selectedEntry && emit('analyzeAi', selectedEntry)">
<Button v-if="selectedEntry && selectedEntry.source !== 'mcp' && historyEntrySource(selectedEntry) !== 'MCP'" variant="outline" @click="selectedEntry && emit('analyzeAi', selectedEntry)">
<Sparkles class="h-4 w-4" />
{{ t("history.analyzeWithAi") }}
</Button>
<Button variant="outline" @click="selectedEntry && restore(selectedEntry)">{{ t("history.restore") }}</Button>
<Button v-if="selectedEntry && selectedEntry.source !== 'mcp' && historyEntrySource(selectedEntry) !== 'MCP'" variant="outline" @click="selectedEntry && restore(selectedEntry)">{{ t("history.restore") }}</Button>
<label v-if="selectedEntry && canRollbackHistoryEntry(selectedEntry) && canUseManualRollback" class="flex items-center gap-2 text-xs" :title="t('history.manualRollbackHint')">
<input v-model="manualRollback" type="checkbox" :disabled="rollbackLocked" />
{{ t("toolbar.manualTransaction") }}
@@ -801,7 +908,7 @@ onBeforeUnmount(() => {
<DialogHeader>
<DialogTitle>{{ t("history.clear") }}</DialogTitle>
</DialogHeader>
<p class="text-sm text-muted-foreground">{{ t("history.confirmClear") }}</p>
<p class="text-sm text-muted-foreground">{{ t("history.confirmClearSource", { source: t(`history.sources.${activeSource}`) }) }}</p>
<DialogFooter>
<Button variant="outline" @click="showClearConfirm = false">{{ t("dangerDialog.cancel") }}</Button>
<Button variant="destructive" @click="executeClear">{{ t("dangerDialog.confirm") }}</Button>
@@ -111,6 +111,12 @@ vi.mock("@/lib/backend/api", async (importOriginal) => {
hoisted.saves.push(limit);
hoisted.limit = limit;
},
loadMcpHistoryRetentionLimit: async () => hoisted.limit,
saveMcpHistoryRetentionLimit: async (limit: number) => {
hoisted.saves.push(limit);
hoisted.limit = limit;
},
cleanupMcpHistoryRetention: async () => 0,
};
});
@@ -5,6 +5,7 @@ import QueryHistory from "../QueryHistory.vue";
const mocks = vi.hoisted(() => ({
add: vi.fn(),
search: vi.fn(),
beginManualTransaction: vi.fn(),
executeInManualTransaction: vi.fn(),
commitManualTransaction: vi.fn(),
@@ -40,7 +41,7 @@ vi.mock("@/stores/historyStore", () => ({
nextCursor: null,
setHistoryPanelActive: vi.fn(),
loadConnectionOptions: vi.fn().mockResolvedValue(undefined),
search: vi.fn().mockResolvedValue(undefined),
search: mocks.search,
add: mocks.add,
}),
}));
@@ -229,3 +230,22 @@ describe("QueryHistory manual rollback SQL", () => {
expect(mocks.beginManualTransaction).not.toHaveBeenCalled();
});
});
describe("QueryHistory source filters", () => {
it("searches MCP independently and resets success when switching back to SQL", async () => {
const root = document.createElement("div");
document.body.append(root);
app = createApp(QueryHistory);
app.mount(root);
await nextTick();
button("history.sources.mcp").click();
await vi.waitFor(() => expect(mocks.search).toHaveBeenLastCalledWith(expect.objectContaining({ source: "mcp", activity_kind: undefined, success: undefined })));
expect(document.body.textContent).not.toContain("history.filters.schema_change");
button("history.filters.mcp_success").click();
await vi.waitFor(() => expect(mocks.search).toHaveBeenLastCalledWith(expect.objectContaining({ source: "mcp", success: true, activity_kind: undefined })));
button("history.sources.sql").click();
await vi.waitFor(() => expect(mocks.search).toHaveBeenLastCalledWith(expect.objectContaining({ source: "sql", success: undefined, activity_kind: undefined })));
expect(document.body.textContent).toContain("history.filters.schema_change");
expect(document.body.textContent).not.toContain("history.sources.all");
});
});