feat(ai): add custom prompt templates

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-07-22 13:43:39 +08:00
1 parent 01fde8879f
commit 1fb3e2816b
58 files changed
+2381 -502

No files matched your search

+4
View File
@@ -16,6 +16,7 @@ import { useConnectionStore } from "@/stores/connectionStore";
import { useQueryStore } from "@/stores/queryStore"; import { useQueryStore } from "@/stores/queryStore";
import { enforceRightSidebarPanelExclusivity, RIGHT_SIDEBAR_PANEL_IDS, transitionRightSidebarPanels, useSettingsStore, type RightSidebarPanelId, type RightSidebarPanelState } from "@/stores/settingsStore"; import { enforceRightSidebarPanelExclusivity, RIGHT_SIDEBAR_PANEL_IDS, transitionRightSidebarPanels, useSettingsStore, type RightSidebarPanelId, type RightSidebarPanelState } from "@/stores/settingsStore";
import { useSavedSqlStore } from "@/stores/savedSqlStore"; import { useSavedSqlStore } from "@/stores/savedSqlStore";
import { usePromptTemplateStore } from "@/stores/promptTemplateStore";
import { useToast } from "@/composables/useToast"; import { useToast } from "@/composables/useToast";
import { useTheme } from "@/composables/useTheme"; import { useTheme } from "@/composables/useTheme";
import { useAppUpdater } from "@/composables/useAppUpdater"; import { useAppUpdater } from "@/composables/useAppUpdater";
@@ -123,6 +124,7 @@ const connectionStore = useConnectionStore();
const queryStore = useQueryStore(); const queryStore = useQueryStore();
const settingsStore = useSettingsStore(); const settingsStore = useSettingsStore();
const savedSqlStore = useSavedSqlStore(); const savedSqlStore = useSavedSqlStore();
const promptTemplateStore = usePromptTemplateStore();
connectionStore.setBeforeConnectHandler((config) => ensureJdbcxRuntimeDrivers(config, api).then(() => undefined)); connectionStore.setBeforeConnectHandler((config) => ensureJdbcxRuntimeDrivers(config, api).then(() => undefined));
const { message: toastMessage, visible: toastVisible, toast } = useToast(); const { message: toastMessage, visible: toastVisible, toast } = useToast();
const { isDark, themeMode, applyTheme, setThemeMode } = useTheme(); const { isDark, themeMode, applyTheme, setThemeMode } = useTheme();
@@ -1905,6 +1907,8 @@ async function initApp() {
console.log(`[STARTUP] queryStore.initOpenTabs: ${(performance.now() - t0).toFixed(0)}ms`); console.log(`[STARTUP] queryStore.initOpenTabs: ${(performance.now() - t0).toFixed(0)}ms`);
await settingsStore.initDesktopSettings().catch(() => {}); await settingsStore.initDesktopSettings().catch(() => {});
void promptTemplateStore.init();
void Promise.all([initSavedSqlEditorPositions(), savedSqlStore.initFromStorage()]) void Promise.all([initSavedSqlEditorPositions(), savedSqlStore.initFromStorage()])
.then(() => { .then(() => {
console.log(`[STARTUP] savedSqlStore.initFromStorage: ${(performance.now() - t0).toFixed(0)}ms`); console.log(`[STARTUP] savedSqlStore.initFromStorage: ${(performance.now() - t0).toFixed(0)}ms`);
@@ -47,15 +47,17 @@ import { useSettingsStore, AI_PROVIDER_PRESETS, normalizeAiConfig } from "@/stor
import AiProviderLogo from "@/components/icons/AiProviderLogo.vue"; import AiProviderLogo from "@/components/icons/AiProviderLogo.vue";
import { useConnectionStore } from "@/stores/connectionStore"; import { useConnectionStore } from "@/stores/connectionStore";
import { useSavedSqlStore } from "@/stores/savedSqlStore"; import { useSavedSqlStore } from "@/stores/savedSqlStore";
import { usePromptTemplateStore } from "@/stores/promptTemplateStore";
import { connectionIconType } from "@/lib/connection/connectionPresentation"; import { connectionIconType } from "@/lib/connection/connectionPresentation";
import DatabaseIcon from "@/components/icons/DatabaseIcon.vue"; import DatabaseIcon from "@/components/icons/DatabaseIcon.vue";
import ConnectionGroupBadge from "@/components/connection/ConnectionGroupBadge.vue"; import ConnectionGroupBadge from "@/components/connection/ConnectionGroupBadge.vue";
import { useQueryStore } from "@/stores/queryStore"; import { useQueryStore } from "@/stores/queryStore";
import { useToast } from "@/composables/useToast"; import { useToast } from "@/composables/useToast";
import { useNavigationTargets } from "@/composables/useNavigationTargets"; import { useNavigationTargets } from "@/composables/useNavigationTargets";
import { buildAiContext, runAgentStream, isVectorDbType, isValidActionForMode, defaultActionForMode, type AiAction, type AiAssistantMode, type AiSqlFileContext } from "@/lib/ai/ai"; import { buildAiContext, runAgentStream, isVectorDbType, isValidActionForMode, defaultActionForMode, type AiAction, type AiAssistantMode, type AiSqlFileContext, type CustomPromptContext } from "@/lib/ai/ai";
import { orderAiConfigsForDisplay } from "@/lib/ai/aiConfigOrdering"; import { orderAiConfigsForDisplay } from "@/lib/ai/aiConfigOrdering";
import { normalizeClaudeCodeReasoningLevel } from "@/lib/ai/aiModelEffort"; import { normalizeClaudeCodeReasoningLevel } from "@/lib/ai/aiModelEffort";
import { ACTIVE_TEMPLATES_TOTAL_MAX, promptTemplateCharacterCount } from "@/types/promptTemplate";
import type { AgentEvent } from "@/lib/backend/tauri"; import type { AgentEvent } from "@/lib/backend/tauri";
import { buildAiAgentPlan } from "@/lib/ai/aiAgentPlan"; import { buildAiAgentPlan } from "@/lib/ai/aiAgentPlan";
@@ -85,6 +87,7 @@ const { t } = useI18n();
const settings = useSettingsStore(); const settings = useSettingsStore();
const connectionStore = useConnectionStore(); const connectionStore = useConnectionStore();
const savedSqlStore = useSavedSqlStore(); const savedSqlStore = useSavedSqlStore();
const promptTemplateStore = usePromptTemplateStore();
const queryStore = useQueryStore(); const queryStore = useQueryStore();
const { openTableTarget } = useNavigationTargets({ const { openTableTarget } = useNavigationTargets({
showFieldLineageDialog: ref(false), showFieldLineageDialog: ref(false),
@@ -148,6 +151,67 @@ const currentSessionId = ref("");
const conversationId = ref(""); const conversationId = ref("");
const conversations = ref<AiConversation[]>([]); const conversations = ref<AiConversation[]>([]);
const showConversationList = ref(false); const showConversationList = ref(false);
const showTemplateSelector = ref(false);
// Prompt template selection (panel-session scope)
const activeTemplateIds = ref<string[]>([]);
const activeTemplates = computed(() => promptTemplateStore.templates.filter((t) => activeTemplateIds.value.includes(t.id)));
watch(
() => promptTemplateStore.templates,
(templates) => {
const availableIds = new Set(templates.map((template) => template.id));
activeTemplateIds.value = activeTemplateIds.value.filter((id) => availableIds.has(id));
},
);
// Retry store load on selector open if prior init failed (e.g. backend not yet ready at mount)
watch(showTemplateSelector, (open) => {
if (open) void promptTemplateStore.ensureLoaded();
});
// Reset template selection when the user switches to a different connection or database —
// a new database context warrants a fresh selection of scenario templates.
watch(
// Return a stable primitive key: a fresh array literal is never Object.is-equal to the
// previous one, so a getter returning `[id, database]` fires on every dependency
// invalidation (e.g. the 30s backup scheduler replacing connection objects) even when the
// id/database values are unchanged — spuriously clearing the selection mid agent-run.
() => `${props.connection?.id ?? ""}::${props.tab?.database ?? ""}`,
() => {
activeTemplateIds.value = [];
},
);
function toggleTemplateId(id: string) {
if (activeTemplateIds.value.includes(id)) {
activeTemplateIds.value = activeTemplateIds.value.filter((tid) => tid !== id);
} else {
// Check total content limit
const tpl = promptTemplateStore.templates.find((t) => t.id === id);
if (tpl) {
const currentTotal = activeTemplates.value.reduce((sum, template) => sum + promptTemplateCharacterCount(template.content), 0);
if (currentTotal + promptTemplateCharacterCount(tpl.content) > ACTIVE_TEMPLATES_TOTAL_MAX) {
toast(t("ai.templateSelectorTooLong", { max: ACTIVE_TEMPLATES_TOTAL_MAX }), 4000);
return;
}
}
activeTemplateIds.value = [...activeTemplateIds.value, id];
}
}
function deselectAllTemplates() {
activeTemplateIds.value = [];
}
const templateSelectorLabel = computed(() => {
if (!promptTemplateStore.isLoaded) return t("ai.templateSelectorLoading");
const count = activeTemplates.value.length;
if (count === 0) return t("ai.templateSelectorNone");
const name = activeTemplates.value[0].name;
if (count === 1) return name;
return `${name} +${count - 1}`;
});
const promptTextareaRef = ref<HTMLTextAreaElement | null>(null); const promptTextareaRef = ref<HTMLTextAreaElement | null>(null);
const shouldAutoScroll = ref(true); const shouldAutoScroll = ref(true);
const userPausedAutoScroll = ref(false); const userPausedAutoScroll = ref(false);
@@ -1393,6 +1457,21 @@ async function send() {
toast(t("ai.noConfig")); toast(t("ai.noConfig"));
return; return;
} }
// Acquire the send guard before the first async operation so two rapid
// submissions cannot both pass the initial isGenerating check and then
// resume into concurrent agent runs.
isGenerating.value = true;
if (!(await promptTemplateStore.ensureLoaded())) {
isGenerating.value = false;
toast(t("ai.customInstructionsLoadFailed"), 5000);
return;
}
// Snapshot the selected custom prompts at send time so later async context loading
// cannot change the instructions for an already-submitted request.
const customPromptContext: CustomPromptContext = {
globalInstructions: promptTemplateStore.globalInstructions,
activeTemplates: [...activeTemplates.value],
};
const selectedTableMentions = [...selectedMentions.value]; const selectedTableMentions = [...selectedMentions.value];
const selectedSqlFiles = [...selectedSqlFileMentions.value]; const selectedSqlFiles = [...selectedSqlFileMentions.value];
@@ -1417,7 +1496,6 @@ async function send() {
// Agent confirmation cannot grant autonomous writes while the active database is production. // Agent confirmation cannot grant autonomous writes while the active database is production.
const allowWriteSql = requestedMode === "agent" && allowWriteSqlForNextRun && !productionContext.value.active; const allowWriteSql = requestedMode === "agent" && allowWriteSqlForNextRun && !productionContext.value.active;
allowWriteSqlForNextRun = false; allowWriteSqlForNextRun = false;
isGenerating.value = true;
messages.value.push({ role: "assistant", content: "" }); messages.value.push({ role: "assistant", content: "" });
const assistantIdx = messages.value.length - 1; const assistantIdx = messages.value.length - 1;
const sessionId = uuid(); const sessionId = uuid();
@@ -1475,6 +1553,7 @@ async function send() {
scrollToBottom(); scrollToBottom();
}, },
sessionId, sessionId,
customPromptContext,
); );
} catch (e: unknown) { } catch (e: unknown) {
const message = e instanceof Error ? e.message : String(e); const message = e instanceof Error ? e.message : String(e);
@@ -2120,6 +2199,44 @@ async function openExternalUrl(url: string) {
@keydown="onPromptKeydown" @keydown="onPromptKeydown"
/> />
<div class="flex min-w-0 flex-nowrap items-center gap-1.5 overflow-hidden"> <div class="flex min-w-0 flex-nowrap items-center gap-1.5 overflow-hidden">
<!-- Template selector -->
<Popover v-model:open="showTemplateSelector">
<PopoverTrigger as-child>
<button type="button" class="flex shrink-0 items-center gap-1 whitespace-nowrap rounded-[6px] border px-2 py-0.5 text-[11px] text-muted-foreground hover:bg-muted hover:text-foreground">
<FileCode class="h-3 w-3" />
{{ t("ai.templateSelectorLabel", { label: templateSelectorLabel }) }}
<svg class="h-3 w-3 shrink-0 opacity-60" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="m6 9 6 6 6-6" /></svg>
</button>
</PopoverTrigger>
<PopoverContent align="start" class="w-64 gap-0 p-1.5">
<div class="max-h-64 overflow-auto">
<div v-if="!promptTemplateStore.isLoaded" class="px-3 py-4 text-center text-xs text-muted-foreground">
{{ t("ai.templateSelectorLoading") }}
</div>
<div v-else-if="promptTemplateStore.templates.length === 0" class="px-3 py-4 text-center text-xs text-muted-foreground">
{{ t("ai.templateSelectorEmpty") }}
</div>
<template v-else>
<template v-for="tpl in promptTemplateStore.templates" :key="tpl.id">
<button type="button" class="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-xs hover:bg-muted" @click="toggleTemplateId(tpl.id)">
<div class="flex h-4 w-4 shrink-0 items-center justify-center rounded-sm border" :class="activeTemplateIds.includes(tpl.id) ? 'border-primary bg-primary text-primary-foreground' : ''">
<Check v-if="activeTemplateIds.includes(tpl.id)" class="h-3 w-3" />
</div>
<div class="flex-1 truncate text-left">
<div class="font-medium">{{ tpl.name }}</div>
<div class="text-[10px] text-muted-foreground truncate">{{ tpl.content.slice(0, 60) }}</div>
</div>
</button>
</template>
</template>
</div>
<div v-if="promptTemplateStore.isLoaded && promptTemplateStore.templates.length > 0" class="border-t mt-1 pt-1 px-1">
<button type="button" class="flex w-full items-center gap-2 rounded-sm px-2 py-1 text-xs text-muted-foreground hover:bg-muted hover:text-foreground" @click="deselectAllTemplates">
{{ t("ai.templateSelectorDeselectAll") }}
</button>
</div>
</PopoverContent>
</Popover>
<LightDropdown <LightDropdown
v-model="assistantMode" v-model="assistantMode"
:items="assistantModeItems" :items="assistantModeItems"
@@ -113,6 +113,7 @@ import { APP_THEME_PALETTES, type AppThemeAppearance, type AppThemeMode, type Ap
import { editorSettingsDraftChanged, editorSettingsDraftFromSettings, editorSettingsPatchFromDraft, normalizeTableOpenPageSizeDraft, type EditorSettingsDraft } from "@/lib/settings/editorSettingsDraft"; import { editorSettingsDraftChanged, editorSettingsDraftFromSettings, editorSettingsPatchFromDraft, normalizeTableOpenPageSizeDraft, type EditorSettingsDraft } from "@/lib/settings/editorSettingsDraft";
import { useConnectionStore } from "@/stores/connectionStore"; import { useConnectionStore } from "@/stores/connectionStore";
import { useSavedSqlStore } from "@/stores/savedSqlStore"; import { useSavedSqlStore } from "@/stores/savedSqlStore";
import { usePromptTemplateStore } from "@/stores/promptTemplateStore";
import { useTunnelProfileStore } from "@/stores/tunnelProfileStore"; import { useTunnelProfileStore } from "@/stores/tunnelProfileStore";
import { currentLocale, setLocale, type Locale } from "@/i18n"; import { currentLocale, setLocale, type Locale } from "@/i18n";
import { LOCALE_OPTIONS } from "@/lib/app/localeOptions"; import { LOCALE_OPTIONS } from "@/lib/app/localeOptions";
@@ -122,12 +123,15 @@ import { DEFAULT_UI_FONT_FAMILY, SYSTEM_UI_FONT_FAMILY } from "@/lib/app/appFont
import { buildAppSupportInfoRows, formatAppSupportInfoForClipboard, type AppSupportInfoLabels } from "@/lib/app/supportInfo"; import { buildAppSupportInfoRows, formatAppSupportInfoForClipboard, type AppSupportInfoLabels } from "@/lib/app/supportInfo";
import { DateTimePatterns, normalizeSupportedDateTimePattern } from "@/lib/dataGrid/columnFormatter"; import { DateTimePatterns, normalizeSupportedDateTimePattern } from "@/lib/dataGrid/columnFormatter";
import { MAX_RESULT_PAGE_SIZE, MIN_RESULT_PAGE_SIZE } from "@/lib/dataGrid/paginationPageSize"; import { MAX_RESULT_PAGE_SIZE, MIN_RESULT_PAGE_SIZE } from "@/lib/dataGrid/paginationPageSize";
import type { PromptTemplate } from "@/types/promptTemplate";
import { GLOBAL_INSTRUCTIONS_MAX, PROMPT_TEMPLATE_CONTENT_MAX, PROMPT_TEMPLATE_NAME_MAX, promptTemplateCharacterCount } from "@/types/promptTemplate";
const { t } = useI18n(); const { t } = useI18n();
const { toast } = useToast(); const { toast } = useToast();
const settingsStore = useSettingsStore(); const settingsStore = useSettingsStore();
const connectionStore = useConnectionStore(); const connectionStore = useConnectionStore();
const savedSqlStore = useSavedSqlStore(); const savedSqlStore = useSavedSqlStore();
const promptTemplateStore = usePromptTemplateStore();
const tunnelProfileStore = useTunnelProfileStore(); const tunnelProfileStore = useTunnelProfileStore();
const { isDark, themeMode, themePalette, setThemeMode, setThemePalette } = useTheme(); const { isDark, themeMode, themePalette, setThemeMode, setThemePalette } = useTheme();
@@ -1967,9 +1971,15 @@ watch(snippetProvider, (provider) => {
void refreshSnippetTokenStatus(); void refreshSnippetTokenStatus();
}); });
watch(activeSettingsTab, (tab) => { watch(activeSettingsTab, async (tab) => {
if (tab === "mcp" && !mcpStatus.value && !mcpStatusLoading.value) void refreshMcpStatus(); if (tab === "mcp" && !mcpStatus.value && !mcpStatusLoading.value) void refreshMcpStatus();
if (tab === "ai" && aiIsCliProvider.value) void ensureCliMcpStatus(); if (tab === "ai" && aiIsCliProvider.value) void ensureCliMcpStatus();
if (tab === "ai") {
// Await completion so we don't snapshot an empty default when the store
// is still loading its first payload (init via App.vue is fire-and-forget).
await promptTemplateStore.ensureLoaded();
editGlobalInstructions.value = promptTemplateStore.globalInstructions;
}
if (tab === "about" && !appSupportInfo.value) void refreshAppSupportInfo(); if (tab === "about" && !appSupportInfo.value) void refreshAppSupportInfo();
if (tab === "appearance") { if (tab === "appearance") {
checkLayoutDescTruncation(); checkLayoutDescTruncation();
@@ -1977,6 +1987,18 @@ watch(activeSettingsTab, (tab) => {
} }
}); });
// If the store finishes loading while the AI tab is already open (e.g. a retry
// from another entry point succeeded after the tab-switch snapshot saw a failed
// load), backfill the textarea — but never clobber text the user already typed.
watch(
() => promptTemplateStore.isLoaded,
(loaded) => {
if (loaded && activeSettingsTab.value === "ai" && !editGlobalInstructions.value) {
editGlobalInstructions.value = promptTemplateStore.globalInstructions;
}
},
);
onMounted(() => { onMounted(() => {
void refreshWebDavPasswordStatus(); void refreshWebDavPasswordStatus();
checkLayoutDescTruncation(); checkLayoutDescTruncation();
@@ -2026,6 +2048,116 @@ async function changePassword() {
// ---------- AI Settings ---------- // ---------- AI Settings ----------
// Global Custom Instructions
const editGlobalInstructions = ref("");
const globalInstructionsSaving = ref(false);
// Prompt Templates Management
const templateEditing = ref<PromptTemplate | null>(null);
const templateFormOpen = ref(false);
const templateForm = ref<{ name: string; content: string }>({ name: "", content: "" });
const templateFormIsNew = ref(true);
const templateSaving = ref(false);
const templateDeleteConfirm = ref<PromptTemplate | null>(null);
const templateDeleteConfirmOpen = ref(false);
watch(templateDeleteConfirm, (val) => {
templateDeleteConfirmOpen.value = val !== null;
});
watch(templateDeleteConfirmOpen, (val) => {
if (!val) templateDeleteConfirm.value = null;
});
function openNewTemplate() {
templateForm.value = { name: "", content: "" };
templateFormIsNew.value = true;
templateEditing.value = null;
templateFormOpen.value = true;
}
function openEditTemplate(tpl: PromptTemplate) {
templateForm.value = { name: tpl.name, content: tpl.content };
templateFormIsNew.value = false;
templateEditing.value = tpl;
templateFormOpen.value = true;
}
function closeTemplateForm() {
templateForm.value = { name: "", content: "" };
templateEditing.value = null;
templateFormOpen.value = false;
}
function templateNameValid(): boolean {
return templateForm.value.name.trim().length > 0;
}
function templateContentValid(): boolean {
return templateForm.value.content.trim().length > 0;
}
function templateNameTooLong(): boolean {
return promptTemplateCharacterCount(templateForm.value.name) > PROMPT_TEMPLATE_NAME_MAX;
}
function templateContentTooLong(): boolean {
return promptTemplateCharacterCount(templateForm.value.content) > PROMPT_TEMPLATE_CONTENT_MAX;
}
function localNameDuplicate(): boolean {
const name = templateForm.value.name.trim();
if (!name) return false;
const existingId = templateEditing.value?.id ?? "";
return promptTemplateStore.templates.some((t) => t.id !== existingId && t.name.toLowerCase() === name.toLowerCase());
}
function templateSaveDisabled(): boolean {
return templateSaving.value || !templateNameValid() || !templateContentValid() || templateNameTooLong() || templateContentTooLong();
}
async function saveTemplateForm() {
if (templateSaveDisabled()) return;
templateSaving.value = true;
try {
const id = templateEditing.value?.id ?? uuid();
await promptTemplateStore.save(id, templateForm.value.name.trim(), templateForm.value.content.trim());
closeTemplateForm();
toast(t("ai.promptTemplateSaved"));
} catch (e: any) {
toast(e?.message || String(e), 5000);
} finally {
templateSaving.value = false;
}
}
async function confirmDeleteTemplate(tpl: PromptTemplate) {
try {
await promptTemplateStore.remove(tpl.id);
toast(t("ai.promptTemplateDeleted"));
} catch (e: any) {
toast(e?.message || String(e), 5000);
} finally {
templateDeleteConfirm.value = null;
}
}
async function saveGlobalInstructions() {
if (promptTemplateCharacterCount(editGlobalInstructions.value) > GLOBAL_INSTRUCTIONS_MAX) return;
globalInstructionsSaving.value = true;
try {
await promptTemplateStore.saveGlobalInstructions(editGlobalInstructions.value);
toast(t("ai.globalInstructionsSaved"));
} catch (e: any) {
toast(e?.message || String(e), 5000);
} finally {
globalInstructionsSaving.value = false;
}
}
function globalInstructionsTooLong(): boolean {
return promptTemplateCharacterCount(editGlobalInstructions.value) > GLOBAL_INSTRUCTIONS_MAX;
}
// AI Config Delete Confirmation // AI Config Delete Confirmation
const aiDeleteConfirmOpen = ref(false); const aiDeleteConfirmOpen = ref(false);
const aiDeleteConfigId = ref<string | null>(null); const aiDeleteConfigId = ref<string | null>(null);
@@ -4568,6 +4700,92 @@ onUnmounted(cleanupPreviewEditor);
</div> </div>
</div> </div>
<!-- Global Custom Instructions (list mode) -->
<div v-if="aiConfigListMode === 'list'" class="space-y-3">
<Separator />
<div>
<h3 class="text-sm font-medium">{{ t("ai.globalInstructions") }}</h3>
<p class="text-xs text-muted-foreground">{{ t("ai.globalInstructionsDescription") }}</p>
</div>
<textarea v-model="editGlobalInstructions" class="w-full rounded-md border bg-background px-3 py-2 text-xs font-mono resize-y min-h-[80px]" rows="4" :placeholder="t('ai.globalInstructionsDescription')"></textarea>
<div class="flex items-center justify-between">
<span class="text-xs" :class="globalInstructionsTooLong() ? 'text-destructive' : 'text-muted-foreground'">
{{ t("ai.globalInstructionsCharCount", { count: promptTemplateCharacterCount(editGlobalInstructions), max: GLOBAL_INSTRUCTIONS_MAX }) }}
</span>
<Button type="button" size="sm" :disabled="!promptTemplateStore.isLoaded || globalInstructionsTooLong() || globalInstructionsSaving" @click="saveGlobalInstructions">
{{ globalInstructionsSaving ? t("common.processing") : t("ai.globalInstructionsSave") }}
</Button>
</div>
</div>
<!-- Prompt Templates Management (list mode) -->
<div v-if="aiConfigListMode === 'list'" class="space-y-3">
<Separator />
<div class="flex items-center justify-between">
<div>
<h3 class="text-sm font-medium">{{ t("ai.promptTemplates") }}</h3>
<p class="text-xs text-muted-foreground">{{ t("ai.promptTemplatesDescription") }}</p>
</div>
<Button v-if="!templateFormOpen" type="button" size="sm" @click="openNewTemplate">
<Plus class="mr-1 h-3.5 w-3.5" />
{{ t("ai.promptTemplateNew") }}
</Button>
</div>
<!-- Template list -->
<div v-if="!templateFormOpen && promptTemplateStore.templates.length > 0" class="space-y-1.5">
<div v-for="tpl in promptTemplateStore.templates" :key="tpl.id" class="flex items-center justify-between rounded-md border p-3">
<div class="min-w-0 flex-1">
<div class="text-sm font-medium truncate">{{ tpl.name }}</div>
<div class="text-xs text-muted-foreground truncate">{{ tpl.content.slice(0, 100) }}{{ tpl.content.length > 100 ? "..." : "" }}</div>
</div>
<div class="flex items-center gap-1 shrink-0 ml-2">
<Button type="button" size="sm" variant="ghost" @click="openEditTemplate(tpl)">{{ t("common.edit") }}</Button>
<Button type="button" size="sm" variant="ghost" class="text-destructive" @click="templateDeleteConfirm = tpl">{{ t("common.delete") }}</Button>
</div>
</div>
</div>
<div v-if="!templateFormOpen && promptTemplateStore.templates.length === 0" class="rounded-md border border-dashed p-6 text-center">
<p class="text-sm text-muted-foreground">{{ t("ai.promptTemplateNoTemplates") }}</p>
</div>
<!-- Template Edit Form -->
<div v-if="templateFormOpen" class="rounded-md border p-4 space-y-3">
<div class="flex items-center justify-between">
<h4 class="text-sm font-medium">{{ templateFormIsNew ? t("ai.promptTemplateNew") : t("ai.promptTemplateEdit") }}</h4>
<Button type="button" variant="ghost" size="sm" @click="closeTemplateForm">
<X class="h-3.5 w-3.5" />
</Button>
</div>
<div>
<Label class="text-xs">{{ t("ai.promptTemplateName") }}</Label>
<Input v-model="templateForm.name" class="mt-1 h-8 text-xs" :placeholder="t('ai.promptTemplateNamePlaceholder')" />
<div class="flex justify-between mt-0.5">
<p v-if="templateNameTooLong()" class="text-xs text-destructive">{{ t("ai.promptTemplateNameTooLong", { max: PROMPT_TEMPLATE_NAME_MAX }) }}</p>
<p v-else-if="localNameDuplicate()" class="text-xs text-destructive">{{ t("ai.promptTemplateNameExists", { name: templateForm.name.trim() }) }}</p>
<span v-else></span>
<span class="text-xs" :class="templateNameTooLong() ? 'text-destructive' : 'text-muted-foreground'">{{ promptTemplateCharacterCount(templateForm.name) }}/{{ PROMPT_TEMPLATE_NAME_MAX }}</span>
</div>
</div>
<div>
<Label class="text-xs">{{ t("ai.promptTemplateContent") }}</Label>
<textarea v-model="templateForm.content" class="mt-1 w-full rounded-md border bg-background px-3 py-2 text-xs font-mono resize-y min-h-[80px]" rows="4" :placeholder="t('ai.promptTemplateContentPlaceholder')"></textarea>
<div class="flex justify-between mt-0.5">
<p v-if="templateContentTooLong()" class="text-xs text-destructive">{{ t("ai.promptTemplateTooLong", { max: PROMPT_TEMPLATE_CONTENT_MAX }) }}</p>
<span v-else></span>
<span class="text-xs" :class="templateContentTooLong() ? 'text-destructive' : 'text-muted-foreground'">{{ promptTemplateCharacterCount(templateForm.content) }}/{{ PROMPT_TEMPLATE_CONTENT_MAX }}</span>
</div>
</div>
<div class="flex justify-end gap-2">
<Button type="button" variant="outline" size="sm" @click="closeTemplateForm">{{ t("common.cancel") }}</Button>
<Button type="button" size="sm" :disabled="templateSaveDisabled()" @click="saveTemplateForm">
{{ templateSaving ? t("common.processing") : t("common.save") }}
</Button>
</div>
</div>
</div>
<!-- Config Edit View --> <!-- Config Edit View -->
<div v-else class="space-y-4"> <div v-else class="space-y-4">
<div class="flex items-center justify-between"> <div class="flex items-center justify-between">
@@ -5431,6 +5649,13 @@ onUnmounted(cleanupPreviewEditor);
<!-- AI Config Delete Confirmation --> <!-- AI Config Delete Confirmation -->
<DangerConfirmDialog v-model:open="aiDeleteConfirmOpen" :title="t('ai.deleteConfigTitle')" :message="t('ai.deleteConfigConfirm')" :confirm-label="t('common.delete')" @confirm="aiConfirmDeleteConfig" /> <DangerConfirmDialog v-model:open="aiDeleteConfirmOpen" :title="t('ai.deleteConfigTitle')" :message="t('ai.deleteConfigConfirm')" :confirm-label="t('common.delete')" @confirm="aiConfirmDeleteConfig" />
<DangerConfirmDialog
v-model:open="templateDeleteConfirmOpen"
:title="t('ai.promptTemplateDeleteTitle')"
:message="t('ai.promptTemplateDeleteConfirm', { name: templateDeleteConfirm?.name ?? '' })"
:confirm-label="t('common.delete')"
@confirm="templateDeleteConfirm && confirmDeleteTemplate(templateDeleteConfirm)"
/>
</component> </component>
</template> </template>
+34
View File
@@ -1575,6 +1575,40 @@ export default {
editResend: "Resend", editResend: "Resend",
editCancel: "Cancel", editCancel: "Cancel",
cannotDeleteActiveConfig: "Cannot delete the currently active config. Switch to another config first.", cannotDeleteActiveConfig: "Cannot delete the currently active config. Switch to another config first.",
globalInstructions: "Global Custom Instructions",
globalInstructionsDescription: "Automatically applied to all AI conversations.",
globalInstructionsCharCount: "{count}/{max} characters",
globalInstructionsSaved: "Global instructions saved",
globalInstructionsSave: "Save",
globalInstructionsTooLong: "Global instructions too long (max {max} characters)",
customInstructionsLoadFailed: "Failed to load custom AI instructions. Please try again.",
promptTemplates: "Scenario Prompt Templates",
promptTemplatesDescription: "Select one or more templates in the AI assistant to inject scenario-specific conventions.",
promptTemplateNew: "New Template",
promptTemplateEdit: "Edit Template",
promptTemplateName: "Template Name",
promptTemplateNamePlaceholder: "e.g. Production DB rules",
promptTemplateContent: "Template Content",
promptTemplateContentPlaceholder: "Describe the convention or instruction...",
promptTemplateNameEmpty: "Template name cannot be empty",
promptTemplateNameExists: "Template name '{name}' already exists",
promptTemplateContentRequired: "Template content cannot be empty",
promptTemplateDeleteTitle: "Delete Template",
promptTemplateDeleteConfirm: "Delete template '{name}'? This cannot be undone.",
promptTemplateTooLong: "Template content too long (max {max} characters)",
promptTemplateNameTooLong: "Template name too long (max {max} characters)",
promptTemplateSaved: "Template saved",
promptTemplateDeleted: "Template deleted",
promptTemplateNoTemplates: "No templates yet. Create one to get started.",
promptTemplateManage: "Manage Templates",
promptTemplateSavedHint: "Changes are saved automatically.",
templateSelectorNone: "None",
templateSelectorButton: "Templates",
templateSelectorLabel: "Templates: {label}",
templateSelectorDeselectAll: "Deselect All",
templateSelectorLoading: "Loading...",
templateSelectorEmpty: "No templates. Click Manage to add.",
templateSelectorTooLong: "Selected templates exceed the total content limit ({max} characters). Deselect some to continue.",
}, },
contextMenu: { contextMenu: {
openConnection: "Open Connection", openConnection: "Open Connection",
+34
View File
@@ -1449,6 +1449,40 @@ export default withEnglishFallback({
proposalConfirmYes: "Sí, continuar", proposalConfirmYes: "Sí, continuar",
proposalConfirmNo: "No, gracias", proposalConfirmNo: "No, gracias",
proposalConfirmTitle: "Confirmación de acción requerida", proposalConfirmTitle: "Confirmación de acción requerida",
globalInstructions: "Instrucciones globales personalizadas",
globalInstructionsDescription: "Aplicadas automáticamente a todas las conversaciones con IA.",
globalInstructionsCharCount: "{count}/{max} caracteres",
globalInstructionsSaved: "Instrucciones globales guardadas",
globalInstructionsSave: "Guardar",
globalInstructionsTooLong: "Instrucciones globales demasiado largas (máx. {max} caracteres)",
customInstructionsLoadFailed: "Error al cargar las instrucciones personalizadas de IA. Inténtalo de nuevo.",
promptTemplates: "Plantillas de prompt por escenario",
promptTemplatesDescription: "Selecciona una o más plantillas en el asistente de IA para inyectar convenciones específicas del escenario.",
promptTemplateNew: "Nueva plantilla",
promptTemplateEdit: "Editar plantilla",
promptTemplateName: "Nombre de plantilla",
promptTemplateNamePlaceholder: "p. ej. Reglas de BD de producción",
promptTemplateContent: "Contenido de la plantilla",
promptTemplateContentPlaceholder: "Describe la convención o instrucción...",
promptTemplateNameEmpty: "El nombre de la plantilla no puede estar vacío",
promptTemplateNameExists: "El nombre de plantilla '{name}' ya existe",
promptTemplateContentRequired: "El contenido de la plantilla no puede estar vacío",
promptTemplateDeleteTitle: "Eliminar plantilla",
promptTemplateDeleteConfirm: "¿Eliminar plantilla '{name}'? Esta acción no se puede deshacer.",
promptTemplateTooLong: "Contenido de plantilla demasiado largo (máx. {max} caracteres)",
promptTemplateNameTooLong: "Nombre de plantilla demasiado largo (máx. {max} caracteres)",
promptTemplateSaved: "Plantilla guardada",
promptTemplateDeleted: "Plantilla eliminada",
promptTemplateNoTemplates: "Aún no hay plantillas. Crea una para empezar.",
promptTemplateManage: "Gestionar plantillas",
promptTemplateSavedHint: "Los cambios se guardan automáticamente.",
templateSelectorNone: "Ninguna",
templateSelectorButton: "Plantillas",
templateSelectorLabel: "Plantillas: {label}",
templateSelectorDeselectAll: "Deseleccionar todas",
templateSelectorLoading: "Cargando...",
templateSelectorEmpty: "Sin plantillas. Haz clic en Gestionar para añadir.",
templateSelectorTooLong: "Las plantillas seleccionadas superan el límite total de contenido ({max} caracteres). Deselecciona algunas para continuar.",
agentSteps: { agentSteps: {
generated: "SQL generado", generated: "SQL generado",
noSql: "No se encontró SQL", noSql: "No se encontró SQL",
+34
View File
@@ -1414,6 +1414,40 @@ export default withEnglishFallback({
proposalConfirmYes: "Sì, procedi", proposalConfirmYes: "Sì, procedi",
proposalConfirmNo: "No, grazie", proposalConfirmNo: "No, grazie",
proposalConfirmTitle: "Conferma azione richiesta", proposalConfirmTitle: "Conferma azione richiesta",
globalInstructions: "Istruzioni globali personalizzate",
globalInstructionsDescription: "Applicate automaticamente a tutte le conversazioni AI.",
globalInstructionsCharCount: "{count}/{max} caratteri",
globalInstructionsSaved: "Istruzioni globali salvate",
globalInstructionsSave: "Salva",
globalInstructionsTooLong: "Istruzioni globali troppo lunghe (massimo {max} caratteri)",
customInstructionsLoadFailed: "Impossibile caricare le istruzioni AI personalizzate. Riprova.",
promptTemplates: "Modelli di prompt per scenario",
promptTemplatesDescription: "Seleziona uno o più modelli nell'assistente AI per inserire convenzioni specifiche dello scenario.",
promptTemplateNew: "Nuovo modello",
promptTemplateEdit: "Modifica modello",
promptTemplateName: "Nome modello",
promptTemplateNamePlaceholder: "es. Regole DB di produzione",
promptTemplateContent: "Contenuto modello",
promptTemplateContentPlaceholder: "Descrivi la convenzione o l'istruzione...",
promptTemplateNameEmpty: "Il nome del modello non può essere vuoto",
promptTemplateNameExists: "Il nome modello '{name}' esiste già",
promptTemplateContentRequired: "Il contenuto del modello non può essere vuoto",
promptTemplateDeleteTitle: "Elimina modello",
promptTemplateDeleteConfirm: "Eliminare il modello '{name}'? Questa azione non può essere annullata.",
promptTemplateTooLong: "Contenuto modello troppo lungo (massimo {max} caratteri)",
promptTemplateNameTooLong: "Nome modello troppo lungo (massimo {max} caratteri)",
promptTemplateSaved: "Modello salvato",
promptTemplateDeleted: "Modello eliminato",
promptTemplateNoTemplates: "Nessun modello ancora. Creane uno per iniziare.",
promptTemplateManage: "Gestisci modelli",
promptTemplateSavedHint: "Le modifiche vengono salvate automaticamente.",
templateSelectorNone: "Nessuno",
templateSelectorButton: "Modelli",
templateSelectorLabel: "Modelli: {label}",
templateSelectorDeselectAll: "Deseleziona tutto",
templateSelectorLoading: "Caricamento...",
templateSelectorEmpty: "Nessun modello. Clicca Gestisci per aggiungerne.",
templateSelectorTooLong: "I modelli selezionati superano il limite totale di contenuto ({max} caratteri). Deseleziona alcuni per continuare.",
agentSteps: { agentSteps: {
generated: "SQL generato", generated: "SQL generato",
noSql: "Nessun SQL trovato", noSql: "Nessun SQL trovato",
+34
View File
@@ -1453,6 +1453,40 @@ export default withEnglishFallback({
proposalConfirmYes: "はい、続行", proposalConfirmYes: "はい、続行",
proposalConfirmNo: "いいえ", proposalConfirmNo: "いいえ",
proposalConfirmTitle: "操作の確認", proposalConfirmTitle: "操作の確認",
globalInstructions: "グローバルカスタム指示",
globalInstructionsDescription: "すべてのAI会話に自動的に適用されます。",
globalInstructionsCharCount: "{count}/{max} 文字",
globalInstructionsSaved: "グローバル指示を保存しました",
globalInstructionsSave: "保存",
globalInstructionsTooLong: "グローバル指示が長すぎます(最大{max}文字)",
customInstructionsLoadFailed: "カスタムAI指示の読み込みに失敗しました。もう一度お試しください。",
promptTemplates: "シナリオプロンプトテンプレート",
promptTemplatesDescription: "AIアシスタントで1つ以上のテンプレートを選択し、シナリオ固有の規約を注入します。",
promptTemplateNew: "新規テンプレート",
promptTemplateEdit: "テンプレート編集",
promptTemplateName: "テンプレート名",
promptTemplateNamePlaceholder: "例:本番DBルール",
promptTemplateContent: "テンプレート内容",
promptTemplateContentPlaceholder: "規約や指示を記述...",
promptTemplateNameEmpty: "テンプレート名を入力してください",
promptTemplateNameExists: "テンプレート名 '{name}' は既に存在します",
promptTemplateContentRequired: "テンプレート内容を入力してください",
promptTemplateDeleteTitle: "テンプレート削除",
promptTemplateDeleteConfirm: "テンプレート '{name}' を削除しますか?この操作は元に戻せません。",
promptTemplateTooLong: "テンプレート内容が長すぎます(最大{max}文字)",
promptTemplateNameTooLong: "テンプレート名が長すぎます(最大{max}文字)",
promptTemplateSaved: "テンプレートを保存しました",
promptTemplateDeleted: "テンプレートを削除しました",
promptTemplateNoTemplates: "テンプレートがありません。新規作成してください。",
promptTemplateManage: "テンプレート管理",
promptTemplateSavedHint: "変更は自動的に保存されます。",
templateSelectorNone: "なし",
templateSelectorButton: "テンプレート",
templateSelectorLabel: "テンプレート: {label}",
templateSelectorDeselectAll: "すべて解除",
templateSelectorLoading: "読み込み中...",
templateSelectorEmpty: "テンプレートがありません。「管理」をクリックして追加してください。",
templateSelectorTooLong: "選択されたテンプレートの合計が上限({max}文字)を超えています。選択を解除してください。",
agentSteps: { agentSteps: {
generated: "SQL生成完了", generated: "SQL生成完了",
noSql: "SQLが見つかりません", noSql: "SQLが見つかりません",
+34
View File
@@ -1451,6 +1451,40 @@ export default withEnglishFallback({
proposalConfirmYes: "Sim, continuar", proposalConfirmYes: "Sim, continuar",
proposalConfirmNo: "Não, obrigado", proposalConfirmNo: "Não, obrigado",
proposalConfirmTitle: "Confirmação de ação necessária", proposalConfirmTitle: "Confirmação de ação necessária",
globalInstructions: "Instruções globais personalizadas",
globalInstructionsDescription: "Aplicadas automaticamente a todas as conversas com IA.",
globalInstructionsCharCount: "{count}/{max} caracteres",
globalInstructionsSaved: "Instruções globais salvas",
globalInstructionsSave: "Salvar",
globalInstructionsTooLong: "Instruções globais muito longas (máx. {max} caracteres)",
customInstructionsLoadFailed: "Falha ao carregar instruções personalizadas de IA. Tente novamente.",
promptTemplates: "Modelos de prompt por cenário",
promptTemplatesDescription: "Selecione um ou mais modelos no assistente de IA para injetar convenções específicas do cenário.",
promptTemplateNew: "Novo modelo",
promptTemplateEdit: "Editar modelo",
promptTemplateName: "Nome do modelo",
promptTemplateNamePlaceholder: "ex. Regras de BD de produção",
promptTemplateContent: "Conteúdo do modelo",
promptTemplateContentPlaceholder: "Descreva a convenção ou instrução...",
promptTemplateNameEmpty: "O nome do modelo não pode estar vazio",
promptTemplateNameExists: "O nome de modelo '{name}' já existe",
promptTemplateContentRequired: "O conteúdo do modelo não pode estar vazio",
promptTemplateDeleteTitle: "Excluir modelo",
promptTemplateDeleteConfirm: "Excluir modelo '{name}'? Esta ação não pode ser desfeita.",
promptTemplateTooLong: "Conteúdo do modelo muito longo (máx. {max} caracteres)",
promptTemplateNameTooLong: "Nome do modelo muito longo (máx. {max} caracteres)",
promptTemplateSaved: "Modelo salvo",
promptTemplateDeleted: "Modelo excluído",
promptTemplateNoTemplates: "Nenhum modelo ainda. Crie um para começar.",
promptTemplateManage: "Gerenciar modelos",
promptTemplateSavedHint: "As alterações são salvas automaticamente.",
templateSelectorNone: "Nenhum",
templateSelectorButton: "Modelos",
templateSelectorLabel: "Modelos: {label}",
templateSelectorDeselectAll: "Desmarcar todos",
templateSelectorLoading: "Carregando...",
templateSelectorEmpty: "Nenhum modelo. Clique em Gerenciar para adicionar.",
templateSelectorTooLong: "Os modelos selecionados excedem o limite total de conteúdo ({max} caracteres). Desmarque alguns para continuar.",
agentSteps: { agentSteps: {
generated: "SQL gerado", generated: "SQL gerado",
noSql: "Nenhum SQL encontrado", noSql: "Nenhum SQL encontrado",
+34
View File
@@ -1575,6 +1575,40 @@ export default withEnglishFallback({
editResend: "重新发送", editResend: "重新发送",
editCancel: "取消", editCancel: "取消",
cannotDeleteActiveConfig: "无法删除当前活跃的配置,请先切换到其他配置", cannotDeleteActiveConfig: "无法删除当前活跃的配置,请先切换到其他配置",
globalInstructions: "全局自定义指令",
globalInstructionsDescription: "所有 AI 对话自动应用。",
globalInstructionsCharCount: "{count}/{max} 字符",
globalInstructionsSaved: "全局指令已保存",
globalInstructionsSave: "保存",
globalInstructionsTooLong: "全局指令过长(最多 {max} 字符)",
customInstructionsLoadFailed: "自定义 AI 规范加载失败,请稍后重试。",
promptTemplates: "场景 Prompt 模板",
promptTemplatesDescription: "在 AI 助手中选择一个或多个模板,注入场景化的约定规范。",
promptTemplateNew: "新建模板",
promptTemplateEdit: "编辑模板",
promptTemplateName: "模板名称",
promptTemplateNamePlaceholder: "例如:生产库规范",
promptTemplateContent: "模板内容",
promptTemplateContentPlaceholder: "描述约定或指令...",
promptTemplateNameEmpty: "模板名称不能为空",
promptTemplateNameExists: "模板名称“{name}”已存在",
promptTemplateContentRequired: "模板内容不能为空",
promptTemplateDeleteTitle: "删除模板",
promptTemplateDeleteConfirm: "确认删除模板“{name}”?此操作不可撤销。",
promptTemplateTooLong: "模板内容过长(最多 {max} 字符)",
promptTemplateNameTooLong: "模板名称过长(最多 {max} 字符)",
promptTemplateSaved: "模板已保存",
promptTemplateDeleted: "模板已删除",
promptTemplateNoTemplates: "暂无模板,请新建一个。",
promptTemplateManage: "管理模板",
promptTemplateSavedHint: "修改后自动保存。",
templateSelectorNone: "无",
templateSelectorButton: "模板",
templateSelectorLabel: "模板: {label}",
templateSelectorDeselectAll: "全不选",
templateSelectorLoading: "加载中...",
templateSelectorEmpty: "暂无模板,请点击管理添加。",
templateSelectorTooLong: "已选模板内容合计超过限制(最多 {max} 字符),请取消部分勾选。",
}, },
contextMenu: { contextMenu: {
openConnection: "打开连接", openConnection: "打开连接",
+34
View File
@@ -1416,6 +1416,40 @@ export default withEnglishFallback({
proposalConfirmYes: "是,請繼續", proposalConfirmYes: "是,請繼續",
proposalConfirmNo: "不用了", proposalConfirmNo: "不用了",
proposalConfirmTitle: "需要確認是否執行以下操作", proposalConfirmTitle: "需要確認是否執行以下操作",
globalInstructions: "全域自訂指令",
globalInstructionsDescription: "自動套用至所有 AI 對話。",
globalInstructionsCharCount: "{count}/{max} 字元",
globalInstructionsSaved: "全域指令已儲存",
globalInstructionsSave: "儲存",
globalInstructionsTooLong: "全域指令過長(最多 {max} 字元)",
customInstructionsLoadFailed: "無法載入自訂 AI 規範,請稍後重試。",
promptTemplates: "場景 Prompt 範本",
promptTemplatesDescription: "在 AI 助手中選擇一或多個範本,注入場景化的約定規範。",
promptTemplateNew: "新增範本",
promptTemplateEdit: "編輯範本",
promptTemplateName: "範本名稱",
promptTemplateNamePlaceholder: "例如:正式環境資料庫規範",
promptTemplateContent: "範本內容",
promptTemplateContentPlaceholder: "描述約定或指令...",
promptTemplateNameEmpty: "範本名稱不可為空",
promptTemplateNameExists: "範本名稱「{name}」已存在",
promptTemplateContentRequired: "範本內容不可為空",
promptTemplateDeleteTitle: "刪除範本",
promptTemplateDeleteConfirm: "確認刪除範本「{name}」?此操作無法復原。",
promptTemplateTooLong: "範本內容過長(最多 {max} 字元)",
promptTemplateNameTooLong: "範本名稱過長(最多 {max} 字元)",
promptTemplateSaved: "範本已儲存",
promptTemplateDeleted: "範本已刪除",
promptTemplateNoTemplates: "尚無範本,請新增一個。",
promptTemplateManage: "管理範本",
promptTemplateSavedHint: "修改後自動儲存。",
templateSelectorNone: "無",
templateSelectorButton: "範本",
templateSelectorLabel: "範本: {label}",
templateSelectorDeselectAll: "取消全選",
templateSelectorLoading: "載入中...",
templateSelectorEmpty: "尚無範本,請點擊管理以新增。",
templateSelectorTooLong: "已選範本內容合計超過限制(最多 {max} 字元),請取消部分選取。",
agentSteps: { agentSteps: {
generated: "已產生 SQL", generated: "已產生 SQL",
noSql: "未找到 SQL", noSql: "未找到 SQL",
+35 -12
View File
@@ -1,6 +1,7 @@
import type { AiConfig } from "@/stores/settingsStore"; import type { AiConfig } from "@/stores/settingsStore";
import { uuid } from "@/lib/common/utils"; import { uuid } from "@/lib/common/utils";
import type { ColumnInfo, ConnectionConfig, DatabaseType, ForeignKeyInfo, IndexInfo, QueryResult, QueryTab } from "@/types/database"; import type { ColumnInfo, ConnectionConfig, DatabaseType, ForeignKeyInfo, IndexInfo, QueryResult, QueryTab } from "@/types/database";
import type { PromptTemplate } from "@/types/promptTemplate";
import * as api from "@/lib/backend/api"; import * as api from "@/lib/backend/api";
import { currentLocale, type Locale } from "@/i18n"; import { currentLocale, type Locale } from "@/i18n";
import { aiTableMentionKey, type AiTableMention } from "@/lib/ai/aiTableMentions"; import { aiTableMentionKey, type AiTableMention } from "@/lib/ai/aiTableMentions";
@@ -96,9 +97,30 @@ export interface AiRequestInput {
allowWriteSql?: boolean; allowWriteSql?: boolean;
} }
function buildAgentRequest(input: AiRequestInput, history?: api.AiMessage[]): { messages: api.AiMessage[]; systemPrompt: string; taskContract: api.AiTaskContract; maxTokens: number } { export interface CustomPromptContext {
globalInstructions?: string;
activeTemplates?: PromptTemplate[];
}
function buildCustomInstructionLines(custom: CustomPromptContext | undefined, isZh: boolean): string[] {
const global = custom?.globalInstructions?.trim() ?? "";
const templates = (custom?.activeTemplates ?? []).filter((t) => t.content.trim());
if (!global && templates.length === 0) return [];
const parts: string[] = [];
if (global) parts.push(global);
parts.push(...templates.map((t) => `### ${t.name}\n${t.content}`));
return [
isZh
? `## 用户自定义规范(补充性)\n以下为用户定义的规范与模板;上方核心安全及方言规则优先级更高。\n\n${parts.join("\n\n")}`
: `## Custom Instructions (supplementary)\nThe following are user-defined conventions and templates. Core safety and dialect rules above take precedence.\n\n${parts.join("\n\n")}`,
];
}
function buildAgentRequest(input: AiRequestInput, history?: api.AiMessage[], custom?: CustomPromptContext): { messages: api.AiMessage[]; systemPrompt: string; taskContract: api.AiTaskContract; maxTokens: number } {
const isZh = isChineseLocale(currentLocale()); const isZh = isChineseLocale(currentLocale());
const systemPrompt = buildSystemPrompt(input.action, input.context, input.mode); const systemPrompt = buildSystemPrompt(input.action, input.context, input.mode, custom);
const userPrompt = buildUserPrompt(input.action, input.context, input.instruction, isZh); const userPrompt = buildUserPrompt(input.action, input.context, input.instruction, isZh);
const taskContract: api.AiTaskContract = { const taskContract: api.AiTaskContract = {
action: input.action, action: input.action,
@@ -113,8 +135,8 @@ function buildAgentRequest(input: AiRequestInput, history?: api.AiMessage[]): {
return { messages, systemPrompt, taskContract, maxTokens }; return { messages, systemPrompt, taskContract, maxTokens };
} }
export async function runAiAction(input: AiRequestInput, history?: api.AiMessage[]): Promise<string> { export async function runAiAction(input: AiRequestInput, history?: api.AiMessage[], custom?: CustomPromptContext): Promise<string> {
const { messages, systemPrompt, taskContract, maxTokens } = buildAgentRequest(input, history); const { messages, systemPrompt, taskContract, maxTokens } = buildAgentRequest(input, history, custom);
return api.aiComplete({ return api.aiComplete({
config: input.config, config: input.config,
systemPrompt, systemPrompt,
@@ -124,8 +146,8 @@ export async function runAiAction(input: AiRequestInput, history?: api.AiMessage
}); });
} }
export async function runAiStream(input: AiRequestInput, history: api.AiMessage[] | undefined, onDelta: (delta: string) => void, sessionId?: string, onReasoningDelta?: (delta: string) => void): Promise<void> { export async function runAiStream(input: AiRequestInput, history: api.AiMessage[] | undefined, onDelta: (delta: string) => void, sessionId?: string, onReasoningDelta?: (delta: string) => void, custom?: CustomPromptContext): Promise<void> {
const { messages, systemPrompt, taskContract, maxTokens } = buildAgentRequest(input, history); const { messages, systemPrompt, taskContract, maxTokens } = buildAgentRequest(input, history, custom);
const sid = sessionId || uuid(); const sid = sessionId || uuid();
await api.aiStream( await api.aiStream(
@@ -146,8 +168,8 @@ export async function runAiStream(input: AiRequestInput, history: api.AiMessage[
); );
} }
export async function runAgentStream(input: AiRequestInput, history: api.AiMessage[] | undefined, onEvent: (event: AgentEvent) => void, sessionId?: string): Promise<string> { export async function runAgentStream(input: AiRequestInput, history: api.AiMessage[] | undefined, onEvent: (event: AgentEvent) => void, sessionId?: string, custom?: CustomPromptContext): Promise<string> {
const { messages, systemPrompt, taskContract, maxTokens } = buildAgentRequest(input, history); const { messages, systemPrompt, taskContract, maxTokens } = buildAgentRequest(input, history, custom);
const sid = sessionId || uuid(); const sid = sessionId || uuid();
return api.aiAgentStream( return api.aiAgentStream(
@@ -199,9 +221,9 @@ export function extractSql(text: string): string {
return text.trim(); return text.trim();
} }
export function buildSystemPrompt(action: AiAction, context: AiContext, mode: AiAssistantMode = "ask"): string { export function buildSystemPrompt(action: AiAction, context: AiContext, mode: AiAssistantMode = "ask", custom?: CustomPromptContext): string {
if (isVectorDbType(context.databaseType)) { if (isVectorDbType(context.databaseType)) {
return buildVectorSystemPrompt(context, mode); return buildVectorSystemPrompt(context, mode, custom);
} }
const schema = formatSchema(context); const schema = formatSchema(context);
const resultPreview = context.lastResultPreview ? `\nLast result preview:\n${context.lastResultPreview}\n` : ""; const resultPreview = context.lastResultPreview ? `\nLast result preview:\n${context.lastResultPreview}\n` : "";
@@ -211,7 +233,7 @@ export function buildSystemPrompt(action: AiAction, context: AiContext, mode: Ai
const isZh = isChineseLocale(currentLocale()); const isZh = isChineseLocale(currentLocale());
const lines: string[] = [...buildBasePromptLines(isZh), ...buildModePromptLines(mode, isZh), ...buildActionPromptLines(action, isZh)]; const lines: string[] = [...buildBasePromptLines(isZh), ...buildModePromptLines(mode, isZh), ...buildActionPromptLines(action, isZh), ...buildCustomInstructionLines(custom, isZh)];
if (schemaScope === "focused_table") { if (schemaScope === "focused_table") {
lines.push( lines.push(
@@ -271,7 +293,7 @@ function buildBasePromptLines(isZh: boolean): string[] {
]; ];
} }
function buildVectorSystemPrompt(context: AiContext, mode: AiAssistantMode): string { function buildVectorSystemPrompt(context: AiContext, mode: AiAssistantMode, custom?: CustomPromptContext): string {
const isZh = isChineseLocale(currentLocale()); const isZh = isChineseLocale(currentLocale());
const schema = formatSchema(context); const schema = formatSchema(context);
const resultPreview = context.lastResultPreview ? `\nLast result preview:\n${context.lastResultPreview}\n` : ""; const resultPreview = context.lastResultPreview ? `\nLast result preview:\n${context.lastResultPreview}\n` : "";
@@ -281,6 +303,7 @@ function buildVectorSystemPrompt(context: AiContext, mode: AiAssistantMode): str
isZh ? `你是 DBX 内置的向量数据库助手。当前连接的是 ${dbLabel(context.databaseType)} 数据库。用中文回复。` : `You are DBX's vector database assistant. Connected to ${dbLabel(context.databaseType)}. Reply in English.`, isZh ? `你是 DBX 内置的向量数据库助手。当前连接的是 ${dbLabel(context.databaseType)} 数据库。用中文回复。` : `You are DBX's vector database assistant. Connected to ${dbLabel(context.databaseType)}. Reply in English.`,
isZh ? "数据存储在集合(collections)中,每条记录包含唯一标识及可选的元数据负载(payload/metadata)。" : "Data is stored in collections. Each record has a unique identifier and optional metadata payload.", isZh ? "数据存储在集合(collections)中,每条记录包含唯一标识及可选的元数据负载(payload/metadata)。" : "Data is stored in collections. Each record has a unique identifier and optional metadata payload.",
...buildVectorModePromptLines(context, mode, isZh), ...buildVectorModePromptLines(context, mode, isZh),
...buildCustomInstructionLines(custom, isZh),
"", "",
`Database type: ${context.databaseType}`, `Database type: ${context.databaseType}`,
`Connection: ${context.connectionName}`, `Connection: ${context.connectionName}`,
+8
View File
@@ -287,6 +287,13 @@ export const saveAiConversation = forward("saveAiConversation");
export const loadAiConversations = forward("loadAiConversations"); export const loadAiConversations = forward("loadAiConversations");
export const deleteAiConversation = forward("deleteAiConversation"); export const deleteAiConversation = forward("deleteAiConversation");
// Prompt Templates
export const loadPromptTemplates = forward("loadPromptTemplates");
export const savePromptTemplate = forward("savePromptTemplate");
export const deletePromptTemplate = forward("deletePromptTemplate");
export const getAiGlobalCustomInstructions = forward("getAiGlobalCustomInstructions");
export const setAiGlobalCustomInstructions = forward("setAiGlobalCustomInstructions");
// System // System
export const listSystemFonts = forward("listSystemFonts"); export const listSystemFonts = forward("listSystemFonts");
export const listSshConfigHosts = forward("listSshConfigHosts"); export const listSshConfigHosts = forward("listSshConfigHosts");
@@ -527,6 +534,7 @@ export type {
AiModelInfo, AiModelInfo,
AiChatMessage, AiChatMessage,
AiConversation, AiConversation,
PromptTemplate,
AgentDriverInfo, AgentDriverInfo,
DriverStoreUsage, DriverStoreUsage,
DriverStoreUsageItem, DriverStoreUsageItem,
+36
View File
@@ -109,6 +109,7 @@ import type {
DroppedFilePreviewSqlOptions, DroppedFilePreviewSqlOptions,
MongoGridFsFileInfo, MongoGridFsFileInfo,
AppSupportInfo, AppSupportInfo,
PromptTemplate,
} from "@/lib/backend/tauri"; } from "@/lib/backend/tauri";
import type { QueryEditability } from "@/lib/sql/sqlAnalysis"; import type { QueryEditability } from "@/lib/sql/sqlAnalysis";
import { isTerminalTransferProgress } from "@/lib/backend/transferProgress"; import { isTerminalTransferProgress } from "@/lib/backend/transferProgress";
@@ -203,6 +204,16 @@ async function del<T>(url: string): Promise<T> {
return res.json(); return res.json();
} }
async function put<T>(url: string, body: unknown): Promise<T> {
const res = await fetch(apiUrl(url), {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
}
function qs(params: Record<string, string | number | boolean | undefined>): string { function qs(params: Record<string, string | number | boolean | undefined>): string {
const sp = new URLSearchParams(); const sp = new URLSearchParams();
for (const [k, v] of Object.entries(params)) { for (const [k, v] of Object.entries(params)) {
@@ -1403,6 +1414,31 @@ export async function deleteAiConversation(id: string): Promise<void> {
return del(`/api/ai/conversation/${id}`); return del(`/api/ai/conversation/${id}`);
} }
// ---------------------------------------------------------------------------
// Prompt Templates
// ---------------------------------------------------------------------------
export async function loadPromptTemplates(): Promise<PromptTemplate[]> {
return get("/api/prompt-templates");
}
export async function savePromptTemplate(id: string, name: string, content: string): Promise<PromptTemplate> {
return post("/api/prompt-templates", { id, name, content });
}
export async function deletePromptTemplate(id: string): Promise<void> {
return del(`/api/prompt-templates/${encodeURIComponent(id)}`);
}
export async function getAiGlobalCustomInstructions(): Promise<string> {
const result = await get<{ content: string }>("/api/prompt-templates/global-instructions");
return result.content ?? "";
}
export async function setAiGlobalCustomInstructions(content: string): Promise<void> {
return put("/api/prompt-templates/global-instructions", { content });
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// SQL File Execution // SQL File Execution
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
+30
View File
@@ -677,6 +677,36 @@ export async function deleteAiConversation(id: string): Promise<void> {
return invoke("delete_ai_conversation", { id }); return invoke("delete_ai_conversation", { id });
} }
// --- Prompt Templates ---
export interface PromptTemplate {
id: string;
name: string;
content: string;
createdAt: string;
updatedAt: string;
}
export async function loadPromptTemplates(): Promise<PromptTemplate[]> {
return invoke("load_prompt_templates");
}
export async function savePromptTemplate(id: string, name: string, content: string): Promise<PromptTemplate> {
return invoke("save_prompt_template", { id, name, content });
}
export async function deletePromptTemplate(id: string): Promise<void> {
return invoke("delete_prompt_template", { id });
}
export async function getAiGlobalCustomInstructions(): Promise<string> {
return invoke("get_ai_global_custom_instructions");
}
export async function setAiGlobalCustomInstructions(content: string): Promise<void> {
return invoke("set_ai_global_custom_instructions", { content });
}
export async function testConnection(config: ConnectionConfig): Promise<string> { export async function testConnection(config: ConnectionConfig): Promise<string> {
return invoke("test_connection", { config }); return invoke("test_connection", { config });
} }
@@ -0,0 +1,71 @@
import { defineStore } from "pinia";
import { ref } from "vue";
import * as api from "@/lib/backend/api";
import type { PromptTemplate } from "@/types/promptTemplate";
export const usePromptTemplateStore = defineStore("promptTemplate", () => {
const templates = ref<PromptTemplate[]>([]);
const globalInstructions = ref("");
const isLoaded = ref(false);
const isLoading = ref(false);
let loadPromise: Promise<boolean> | null = null;
async function init(): Promise<boolean> {
if (isLoaded.value) return true;
if (loadPromise) return loadPromise;
isLoading.value = true;
loadPromise = (async () => {
try {
const [tpls, gi] = await Promise.all([api.loadPromptTemplates(), api.getAiGlobalCustomInstructions()]);
templates.value = tpls;
globalInstructions.value = gi;
isLoaded.value = true;
return true;
} catch {
// Keep failed initialization retryable, but do not let callers send
// AI requests with an incomplete global-instructions context.
return false;
} finally {
isLoading.value = false;
loadPromise = null;
}
})();
return loadPromise;
}
/** Return whether prompt data is ready; failed initialization remains retryable. */
async function ensureLoaded(): Promise<boolean> {
return init();
}
async function save(id: string, name: string, content: string): Promise<PromptTemplate> {
const saved = await api.savePromptTemplate(id, name, content);
const idx = templates.value.findIndex((t) => t.id === id);
if (idx >= 0) {
templates.value[idx] = saved;
} else {
templates.value.push(saved);
}
// Maintain stable sort order: created_at, then id
templates.value = [...templates.value].sort(sortTemplates);
return saved;
}
async function remove(id: string): Promise<void> {
await api.deletePromptTemplate(id);
templates.value = templates.value.filter((t) => t.id !== id);
}
async function saveGlobalInstructions(content: string): Promise<void> {
const trimmed = content.trim();
await api.setAiGlobalCustomInstructions(trimmed);
globalInstructions.value = trimmed;
}
return { templates, globalInstructions, isLoaded, isLoading, init, ensureLoaded, save, remove, saveGlobalInstructions };
});
function sortTemplates(a: PromptTemplate, b: PromptTemplate): number {
if (a.createdAt !== b.createdAt) return a.createdAt < b.createdAt ? -1 : 1;
return a.id < b.id ? -1 : 1;
}
+17
View File
@@ -0,0 +1,17 @@
export interface PromptTemplate {
id: string;
name: string;
content: string;
createdAt: string;
updatedAt: string;
}
export const PROMPT_TEMPLATE_NAME_MAX = 50;
export const PROMPT_TEMPLATE_CONTENT_MAX = 8000;
export const GLOBAL_INSTRUCTIONS_MAX = 8000;
export const ACTIVE_TEMPLATES_TOTAL_MAX = 16000;
/** Matches Rust str::chars().count() for frontend prompt validation. */
export function promptTemplateCharacterCount(value: string): number {
return Array.from(value).length;
}
+1
View File
@@ -41,6 +41,7 @@ pub mod path_utils;
pub mod plugins; pub mod plugins;
pub mod process; pub mod process;
pub mod production_safety; pub mod production_safety;
pub mod prompt_template;
pub mod query; pub mod query;
pub mod query_cancel; pub mod query_cancel;
pub mod query_execution_sql; pub mod query_execution_sql;
+11
View File
@@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct PromptTemplate {
pub id: String,
pub name: String,
pub content: String,
pub created_at: String,
pub updated_at: String,
}
+405
View File
@@ -19,6 +19,7 @@ use crate::history::{
HistorySearchRequest, HistorySearchResult, MAX_HISTORY, HistorySearchRequest, HistorySearchResult, MAX_HISTORY,
}; };
use crate::models::connection::{ConnectionConfig, DatabaseConnectionInfo, DatabaseType, TransportLayerConfig}; use crate::models::connection::{ConnectionConfig, DatabaseConnectionInfo, DatabaseType, TransportLayerConfig};
use crate::prompt_template::PromptTemplate;
use crate::saved_sql::{SavedSqlFile, SavedSqlFolder, SavedSqlLibrary}; use crate::saved_sql::{SavedSqlFile, SavedSqlFolder, SavedSqlLibrary};
const SSH_TUNNEL_SECRET_PREFIX: &str = "ssh_tunnels."; const SSH_TUNNEL_SECRET_PREFIX: &str = "ssh_tunnels.";
@@ -28,6 +29,7 @@ const APP_STATE_EDITOR_SETTINGS_KEY: &str = "editor_settings";
const APP_STATE_OPEN_TABS_KEY: &str = "open_tabs"; const APP_STATE_OPEN_TABS_KEY: &str = "open_tabs";
const APP_STATE_SAVED_SQL_EDITOR_POSITIONS_KEY: &str = "saved_sql_editor_positions"; const APP_STATE_SAVED_SQL_EDITOR_POSITIONS_KEY: &str = "saved_sql_editor_positions";
const MCP_GLOBAL_POLICY_KEY: &str = "mcp_global_policy"; const MCP_GLOBAL_POLICY_KEY: &str = "mcp_global_policy";
const APP_STATE_AI_GLOBAL_INSTRUCTIONS_KEY: &str = "ai_global_custom_instructions";
const USER_DATA_TABLES: &[&str] = &[ const USER_DATA_TABLES: &[&str] = &[
"connections", "connections",
"connection_secrets", "connection_secrets",
@@ -372,6 +374,13 @@ const SCHEMA_STATEMENTS: &[&str] = &[
config_json TEXT NOT NULL, config_json TEXT NOT NULL,
is_default INTEGER NOT NULL DEFAULT 0 is_default INTEGER NOT NULL DEFAULT 0
)", )",
"CREATE TABLE IF NOT EXISTS prompt_templates (
id TEXT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
content TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL DEFAULT '',
updated_at TEXT NOT NULL DEFAULT ''
)",
]; ];
impl Storage { impl Storage {
@@ -1630,6 +1639,24 @@ impl Storage {
self.load_app_state_value(APP_STATE_SAVED_SQL_EDITOR_POSITIONS_KEY).await self.load_app_state_value(APP_STATE_SAVED_SQL_EDITOR_POSITIONS_KEY).await
} }
pub async fn save_ai_global_custom_instructions(&self, content: &str) -> Result<(), String> {
let trimmed = content.trim();
if trimmed.chars().count() > 8000 {
return Err("global instructions too long (max 8000 chars)".to_string());
}
self.save_app_state_value(APP_STATE_AI_GLOBAL_INSTRUCTIONS_KEY, &serde_json::Value::String(trimmed.to_string()))
.await
}
pub async fn load_ai_global_custom_instructions(&self) -> Result<String, String> {
let value = self.load_app_state_value(APP_STATE_AI_GLOBAL_INSTRUCTIONS_KEY).await?;
Ok(match value {
Some(serde_json::Value::String(s)) => s,
None | Some(serde_json::Value::Null) => String::new(),
other => other.map(|v| v.to_string()).unwrap_or_default(),
})
}
pub async fn load_or_create_local_device_secret(&self) -> Result<String, String> { pub async fn load_or_create_local_device_secret(&self) -> Result<String, String> {
let mut settings = self.load_app_settings_json().await?; let mut settings = self.load_app_settings_json().await?;
if let Some(secret) = settings.get("local_device_secret").and_then(|value| value.as_str()) { if let Some(secret) = settings.get("local_device_secret").and_then(|value| value.as_str()) {
@@ -1772,6 +1799,108 @@ impl Storage {
}) })
.await .await
} }
// Prompt Templates
pub async fn load_prompt_templates(&self) -> Result<Vec<PromptTemplate>, String> {
self.with_conn(|conn| {
let mut stmt = conn
.prepare(
"SELECT id, name, content, created_at, updated_at \
FROM prompt_templates ORDER BY created_at, id",
)
.map_err(|e| e.to_string())?;
let rows = stmt
.query_map([], |row| {
Ok(PromptTemplate {
id: row.get(0)?,
name: row.get(1)?,
content: row.get(2)?,
created_at: row.get(3)?,
updated_at: row.get(4)?,
})
})
.map_err(|e| e.to_string())?;
rows.collect::<Result<Vec<_>, _>>().map_err(|e| e.to_string())
})
.await
}
pub async fn save_prompt_template(&self, id: &str, name: &str, content: &str) -> Result<PromptTemplate, String> {
let id = id.to_string();
let name = name.trim().to_string();
let content = content.to_string();
// Validation
if name.is_empty() {
return Err("template name cannot be empty".to_string());
}
if name.chars().count() > 50 {
return Err("template name too long (max 50 chars)".to_string());
}
if content.chars().count() > 8000 {
return Err("template content too long (max 8000 chars)".to_string());
}
let now = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
self.with_conn(move |conn| {
// Case-insensitive duplicate name check (Unicode-aware).
// SQLite LOWER() is ASCII-only, so we compare in Rust where
// str::to_lowercase() handles full Unicode case folding.
let name_lower = name.to_lowercase();
let mut stmt = conn
.prepare("SELECT name FROM prompt_templates WHERE id != ?1")
.map_err(|e| e.to_string())?;
let duplicate = stmt
.query_map(params![id], |row| row.get::<_, String>(0))
.map_err(|e| e.to_string())?
.filter_map(|r| r.ok())
.any(|existing| existing.to_lowercase() == name_lower);
if duplicate {
return Err("duplicate template name".to_string());
}
// Check if row exists to decide INSERT vs UPDATE
let existing_created_at: Option<String> = conn
.query_row("SELECT created_at FROM prompt_templates WHERE id = ?1", params![id], |row| row.get(0))
.optional()
.map_err(|e| e.to_string())?;
if let Some(created_at) = existing_created_at {
// UPDATE — preserve created_at
conn.execute(
"UPDATE prompt_templates SET name = ?1, content = ?2, updated_at = ?3 WHERE id = ?4",
params![name, content, now, id],
)
.map_err(|e| e.to_string())?;
Ok(PromptTemplate { id, name, content, created_at, updated_at: now })
} else {
// INSERT
conn.execute(
"INSERT INTO prompt_templates (id, name, content, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5)",
params![id, name, content, now, now],
)
.map_err(|e| e.to_string())?;
Ok(PromptTemplate { id, name, content, created_at: now.clone(), updated_at: now })
}
})
.await
}
pub async fn delete_prompt_template(&self, id: &str) -> Result<(), String> {
let id = id.to_string();
self.with_conn(move |conn| {
let rows =
conn.execute("DELETE FROM prompt_templates WHERE id = ?1", params![id]).map_err(|e| e.to_string())?;
if rows == 0 {
Err("template not found".to_string())
} else {
Ok(())
}
})
.await
}
} }
// Connections // Connections
@@ -4701,4 +4830,280 @@ mod tests {
std::fs::remove_file(&db).ok(); std::fs::remove_file(&db).ok();
} }
// --- Prompt Templates ---
#[tokio::test]
async fn prompt_template_save_new_creates_timestamps() {
let db = temp_db_path("pt-save-new");
let storage = Storage::open(&db).await.unwrap();
let result = storage.save_prompt_template("t1", "Production Rules", "SELECT 1").await.unwrap();
assert_eq!(result.id, "t1");
assert_eq!(result.name, "Production Rules");
assert_eq!(result.content, "SELECT 1");
assert!(!result.created_at.is_empty());
assert_eq!(result.created_at, result.updated_at);
// Verify it's persisted in load
let templates = storage.load_prompt_templates().await.unwrap();
assert_eq!(templates.len(), 1);
assert_eq!(templates[0].id, "t1");
assert_eq!(templates[0].created_at, result.created_at);
assert_eq!(templates[0].updated_at, result.updated_at);
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn prompt_template_save_update_preserves_created_at() {
let db = temp_db_path("pt-save-update");
let storage = Storage::open(&db).await.unwrap();
let first = storage.save_prompt_template("t1", "Original Name", "Original content").await.unwrap();
// Ensure some time passes so updated_at changes
std::thread::sleep(std::time::Duration::from_millis(1100));
let second = storage.save_prompt_template("t1", "Updated Name", "Updated content").await.unwrap();
assert_eq!(second.id, "t1");
assert_eq!(second.name, "Updated Name");
assert_eq!(second.content, "Updated content");
assert_eq!(second.created_at, first.created_at, "created_at must be preserved on update");
assert_ne!(second.updated_at, first.updated_at, "updated_at must change on update");
// Verify only one row exists
let templates = storage.load_prompt_templates().await.unwrap();
assert_eq!(templates.len(), 1);
assert_eq!(templates[0].name, "Updated Name");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn prompt_template_save_rejects_blank_name() {
let db = temp_db_path("pt-name-blank");
let storage = Storage::open(&db).await.unwrap();
let err = storage.save_prompt_template("t1", "", "content").await.unwrap_err();
assert!(err.contains("cannot be empty"), "expected 'cannot be empty', got: {err}");
let err = storage.save_prompt_template("t1", " ", "content").await.unwrap_err();
assert!(err.contains("cannot be empty"), "expected 'cannot be empty', got: {err}");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn prompt_template_save_rejects_long_name() {
let db = temp_db_path("pt-name-long");
let storage = Storage::open(&db).await.unwrap();
let long_name = "a".repeat(51);
let err = storage.save_prompt_template("t1", &long_name, "content").await.unwrap_err();
assert!(err.contains("too long") && err.contains("50"), "expected too long (max 50), got: {err}");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn prompt_template_accepts_multi_byte_characters_within_char_limit() {
let db = temp_db_path("pt-multibyte-name");
let storage = Storage::open(&db).await.unwrap();
// 25 Chinese characters = 75 bytes but only 25 chars — should be allowed under 50 char limit
let name25 = "数".repeat(25); // 25 chars, 75 bytes
assert_eq!(name25.chars().count(), 25);
assert!(name25.len() > 50); // byte length exceeds 50
let result = storage.save_prompt_template("t1", &name25, "content").await.unwrap();
assert_eq!(result.name, name25);
// 51 Chinese characters = 153 bytes — should be rejected (51 chars > 50)
let name51 = "数".repeat(51);
assert_eq!(name51.chars().count(), 51);
let err = storage.save_prompt_template("t2", &name51, "content").await.unwrap_err();
assert!(err.contains("too long") && err.contains("50"), "expected too long (max 50), got: {err}");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn prompt_template_save_rejects_long_content() {
let db = temp_db_path("pt-content-long");
let storage = Storage::open(&db).await.unwrap();
let long_content = "a".repeat(8001);
let err = storage.save_prompt_template("t1", "Valid Name", &long_content).await.unwrap_err();
assert!(err.contains("too long") && err.contains("8000"), "expected too long (max 8000), got: {err}");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn prompt_template_save_rejects_duplicate_name_case_insensitive() {
let db = temp_db_path("pt-dup-name");
let storage = Storage::open(&db).await.unwrap();
storage.save_prompt_template("t1", "Production Rules", "content 1").await.unwrap();
// Same name, different id → should fail
let err = storage.save_prompt_template("t2", "production rules", "content 2").await.unwrap_err();
assert!(err.contains("duplicate"), "expected 'duplicate', got: {err}");
// Same name, same id → should update (not fail)
let update = storage.save_prompt_template("t1", "Production Rules", "updated").await.unwrap();
assert_eq!(update.id, "t1");
assert_eq!(update.content, "updated");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn prompt_template_save_rejects_duplicate_name_unicode_case_folding() {
let db = temp_db_path("pt-dup-unicode");
let storage = Storage::open(&db).await.unwrap();
// SQLite LOWER() is ASCII-only (U+00C4 'Ä' → no change), but Rust
// str::to_lowercase() does full Unicode case folding (Ä → ä).
// Both directions must detect the duplicate.
storage.save_prompt_template("t1", "Ä规则", "content-upper").await.unwrap();
let err = storage.save_prompt_template("t2", "ä规则", "content-lower").await.unwrap_err();
assert!(err.contains("duplicate"), "expected 'duplicate', got: {err}");
// Reverse: lower-case first, upper-case second.
let db = temp_db_path("pt-dup-unicode-2");
let storage = Storage::open(&db).await.unwrap();
storage.save_prompt_template("t1", "ä规则", "content-lower").await.unwrap();
let err = storage.save_prompt_template("t2", "Ä规则", "content-upper").await.unwrap_err();
assert!(err.contains("duplicate"), "expected 'duplicate', got: {err}");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn prompt_template_load_order_is_stable() {
let db = temp_db_path("pt-load-order");
let storage = Storage::open(&db).await.unwrap();
// Insert in reverse order of created_at by sleeping between inserts
storage.save_prompt_template("a", "Template A", "a").await.unwrap();
std::thread::sleep(std::time::Duration::from_millis(1100));
storage.save_prompt_template("b", "Template B", "b").await.unwrap();
std::thread::sleep(std::time::Duration::from_millis(1100));
storage.save_prompt_template("c", "Template C", "c").await.unwrap();
let templates = storage.load_prompt_templates().await.unwrap();
assert_eq!(templates.len(), 3);
// Order should be by created_at ascending: A first, C last
assert_eq!(templates[0].id, "a");
assert_eq!(templates[1].id, "b");
assert_eq!(templates[2].id, "c");
// Insert with same created_at — tie-break by id
// We insert d right after c without delay
storage.save_prompt_template("d", "Template D", "d").await.unwrap();
let templates = storage.load_prompt_templates().await.unwrap();
assert_eq!(templates.len(), 4);
assert_eq!(templates[3].id, "d");
// Second load should give same order
let templates2 = storage.load_prompt_templates().await.unwrap();
assert_eq!(templates, templates2);
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn prompt_template_delete_unknown_id_errors() {
let db = temp_db_path("pt-delete-unknown");
let storage = Storage::open(&db).await.unwrap();
let err = storage.delete_prompt_template("nonexistent").await.unwrap_err();
assert!(err.contains("not found"), "expected 'not found', got: {err}");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn prompt_template_delete_existing_removes() {
let db = temp_db_path("pt-delete-existing");
let storage = Storage::open(&db).await.unwrap();
storage.save_prompt_template("t1", "Template", "content").await.unwrap();
assert_eq!(storage.load_prompt_templates().await.unwrap().len(), 1);
storage.delete_prompt_template("t1").await.unwrap();
assert!(storage.load_prompt_templates().await.unwrap().is_empty());
std::fs::remove_file(&db).ok();
}
// --- Global Custom Instructions ---
#[tokio::test]
async fn global_instructions_set_get_roundtrip() {
let db = temp_db_path("gi-roundtrip");
let storage = Storage::open(&db).await.unwrap();
let instructions = "Amounts are in cents. Always filter by date range.";
storage.save_ai_global_custom_instructions(instructions).await.unwrap();
let loaded = storage.load_ai_global_custom_instructions().await.unwrap();
assert_eq!(loaded, instructions);
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn global_instructions_defaults_to_empty() {
let db = temp_db_path("gi-default");
let storage = Storage::open(&db).await.unwrap();
let loaded = storage.load_ai_global_custom_instructions().await.unwrap();
assert_eq!(loaded, "");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn global_instructions_rejects_too_long() {
let db = temp_db_path("gi-too-long");
let storage = Storage::open(&db).await.unwrap();
let long = "a".repeat(8001);
let err = storage.save_ai_global_custom_instructions(&long).await.unwrap_err();
assert!(err.contains("too long") && err.contains("8000"), "expected too long (max 8000), got: {err}");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn global_instructions_empty_string_clears() {
let db = temp_db_path("gi-clear");
let storage = Storage::open(&db).await.unwrap();
storage.save_ai_global_custom_instructions("Some instructions").await.unwrap();
assert_eq!(storage.load_ai_global_custom_instructions().await.unwrap(), "Some instructions");
// Empty string (trimmed) is allowed — equivalent to clear
storage.save_ai_global_custom_instructions("").await.unwrap();
assert_eq!(storage.load_ai_global_custom_instructions().await.unwrap(), "");
std::fs::remove_file(&db).ok();
}
#[tokio::test]
async fn global_instructions_whitespace_only_trims() {
let db = temp_db_path("gi-whitespace");
let storage = Storage::open(&db).await.unwrap();
storage.save_ai_global_custom_instructions(" \n \t ").await.unwrap();
let loaded = storage.load_ai_global_custom_instructions().await.unwrap();
assert_eq!(loaded, "");
std::fs::remove_file(&db).ok();
}
} }
+13 -6
View File
@@ -1,32 +1,39 @@
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::response::{IntoResponse, Response}; use axum::response::{IntoResponse, Response};
pub struct AppError(pub String); pub struct AppError {
pub message: String,
pub status: StatusCode,
}
impl AppError { impl AppError {
pub fn internal(msg: impl Into<String>) -> Self { pub fn internal(msg: impl Into<String>) -> Self {
AppError(msg.into()) AppError { message: msg.into(), status: StatusCode::INTERNAL_SERVER_ERROR }
} }
pub fn bad_request(msg: impl Into<String>) -> Self { pub fn bad_request(msg: impl Into<String>) -> Self {
AppError(msg.into()) AppError { message: msg.into(), status: StatusCode::BAD_REQUEST }
}
pub fn not_found(msg: impl Into<String>) -> Self {
AppError { message: msg.into(), status: StatusCode::NOT_FOUND }
} }
} }
impl IntoResponse for AppError { impl IntoResponse for AppError {
fn into_response(self) -> Response { fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, self.0).into_response() (self.status, self.message).into_response()
} }
} }
impl From<String> for AppError { impl From<String> for AppError {
fn from(s: String) -> Self { fn from(s: String) -> Self {
AppError(s) AppError { message: s, status: StatusCode::INTERNAL_SERVER_ERROR }
} }
} }
impl From<&str> for AppError { impl From<&str> for AppError {
fn from(s: &str) -> Self { fn from(s: &str) -> Self {
AppError(s.to_string()) AppError { message: s.to_string(), status: StatusCode::INTERNAL_SERVER_ERROR }
} }
} }
+10
View File
@@ -517,6 +517,16 @@ async fn main() {
.route("/ai/cancel-stream", post(routes::ai::ai_cancel_stream)) .route("/ai/cancel-stream", post(routes::ai::ai_cancel_stream))
.route("/ai/test-connection", post(routes::ai::ai_test_connection)) .route("/ai/test-connection", post(routes::ai::ai_test_connection))
.route("/ai/models", post(routes::ai::ai_list_models)) .route("/ai/models", post(routes::ai::ai_list_models))
// Prompt templates
.route(
"/prompt-templates",
get(routes::prompt_template::load_prompt_templates).post(routes::prompt_template::save_prompt_template),
)
.route("/prompt-templates/{id}", delete(routes::prompt_template::delete_prompt_template))
.route(
"/prompt-templates/global-instructions",
get(routes::prompt_template::get_global_instructions).put(routes::prompt_template::set_global_instructions),
)
// Transfer // Transfer
.route("/transfer/start", post(routes::transfer::start_transfer)) .route("/transfer/start", post(routes::transfer::start_transfer))
.route("/transfer/ownership-preview", post(routes::transfer::preview_transfer_ownership)) .route("/transfer/ownership-preview", post(routes::transfer::preview_transfer_ownership))
+32 -32
View File
@@ -64,7 +64,7 @@ pub async fn get_driver_store_usage(State(state): State<Arc<WebState>>) -> Resul
pub async fn clear_driver_download_cache( pub async fn clear_driver_download_cache(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
clear_agent_download_cache(&state.app.agent_manager).map_err(AppError)?; clear_agent_download_cache(&state.app.agent_manager).map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
@@ -84,7 +84,7 @@ pub async fn stop_driver_runtime(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<DriverRuntimeRequest>, Json(req): Json<DriverRuntimeRequest>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
dbx_core::driver_runtime::stop_driver_runtime(&state.app, &req.runtime_id).await.map_err(AppError)?; dbx_core::driver_runtime::stop_driver_runtime(&state.app, &req.runtime_id).await.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
@@ -92,7 +92,7 @@ pub async fn restart_driver_runtime(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<DriverRuntimeRequest>, Json(req): Json<DriverRuntimeRequest>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
dbx_core::driver_runtime::restart_driver_runtime(&state.app, &req.runtime_id).await.map_err(AppError)?; dbx_core::driver_runtime::restart_driver_runtime(&state.app, &req.runtime_id).await.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
@@ -100,32 +100,32 @@ pub async fn install_agent(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<AgentTypeRequest>, Json(req): Json<AgentTypeRequest>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&req.db_type)).await.map_err(AppError)?; ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&req.db_type)).await.map_err(AppError::from)?;
let tx = progress_sender(&state, "global").await; let tx = progress_sender(&state, "global").await;
install_agent_driver(&state.app.agent_manager, &req.db_type, |event| send_progress_event(&tx, event)) install_agent_driver(&state.app.agent_manager, &req.db_type, |event| send_progress_event(&tx, event))
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
pub async fn upgrade_all_agents(State(state): State<Arc<WebState>>) -> Result<Json<serde_json::Value>, AppError> { pub async fn upgrade_all_agents(State(state): State<Arc<WebState>>) -> Result<Json<serde_json::Value>, AppError> {
let registry = fetch_registry().await.map_err(AppError)?; let registry = fetch_registry().await.map_err(AppError::from)?;
let agents = build_agent_list(&state.app.agent_manager, Some(&registry)); let agents = build_agent_list(&state.app.agent_manager, Some(&registry));
let updatable: Vec<String> = let updatable: Vec<String> =
agents.iter().filter(|agent| agent.update_available).map(|agent| agent.db_type.clone()).collect(); agents.iter().filter(|agent| agent.update_available).map(|agent| agent.db_type.clone()).collect();
ensure_no_agent_update_blockers(&state.app, &updatable).await.map_err(AppError)?; ensure_no_agent_update_blockers(&state.app, &updatable).await.map_err(AppError::from)?;
let tx = progress_sender(&state, "global").await; let tx = progress_sender(&state, "global").await;
let result = upgrade_all_agent_drivers(&state.app.agent_manager, |event| send_progress_event(&tx, event)) let result = upgrade_all_agent_drivers(&state.app.agent_manager, |event| send_progress_event(&tx, event))
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|err| AppError(err.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|err| AppError::from(err.to_string()))?))
} }
pub async fn uninstall_agent( pub async fn uninstall_agent(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<AgentTypeRequest>, Json(req): Json<AgentTypeRequest>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
uninstall_agent_driver(&state.app.agent_manager, &req.db_type).await.map_err(AppError)?; uninstall_agent_driver(&state.app.agent_manager, &req.db_type).await.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
@@ -143,7 +143,7 @@ pub async fn set_agent_java_runtime_config(
let mut config = req.config; let mut config = req.config;
if config.mode == JavaRuntimeMode::Custom || config.mode == JavaRuntimeMode::System { if config.mode == JavaRuntimeMode::Custom || config.mode == JavaRuntimeMode::System {
let candidate_state = AgentState { java_runtime: config.clone(), ..am.load_state() }; let candidate_state = AgentState { java_runtime: config.clone(), ..am.load_state() };
let resolved = am.resolve_java_runtime(&candidate_state, DEFAULT_JRE_KEY).map_err(AppError)?; let resolved = am.resolve_java_runtime(&candidate_state, DEFAULT_JRE_KEY).map_err(AppError::from)?;
if config.mode == JavaRuntimeMode::Custom { if config.mode == JavaRuntimeMode::Custom {
config.custom_java_path = Some(resolved.to_string_lossy().to_string()); config.custom_java_path = Some(resolved.to_string_lossy().to_string());
} }
@@ -154,7 +154,7 @@ pub async fn set_agent_java_runtime_config(
let mut local_state = am.load_state(); let mut local_state = am.load_state();
local_state.java_runtime = config.clone(); local_state.java_runtime = config.clone();
am.save_state(&local_state).map_err(AppError)?; am.save_state(&local_state).map_err(AppError::from)?;
am.stop_daemons().await; am.stop_daemons().await;
Ok(Json(config)) Ok(Json(config))
} }
@@ -169,29 +169,29 @@ pub async fn import_agents_from_zip(
mut multipart: Multipart, mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let tmp_dir = state.data_dir.join("tmp"); let tmp_dir = state.data_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir).map_err(|err| AppError(err.to_string()))?; std::fs::create_dir_all(&tmp_dir).map_err(|err| AppError::from(err.to_string()))?;
if let Some(field) = multipart.next_field().await.map_err(|err| AppError(err.to_string()))? { if let Some(field) = multipart.next_field().await.map_err(|err| AppError::from(err.to_string()))? {
let file_name = field.file_name().unwrap_or("offline-drivers.zip").to_string(); let file_name = field.file_name().unwrap_or("offline-drivers.zip").to_string();
if !file_name.to_ascii_lowercase().ends_with(".zip") { if !file_name.to_ascii_lowercase().ends_with(".zip") {
return Err(AppError("Offline driver package must be a .zip file".to_string())); return Err(AppError::from("Offline driver package must be a .zip file".to_string()));
} }
let zip_path = tmp_dir.join(format!("agent-offline-{}.zip", uuid::Uuid::new_v4())); let zip_path = tmp_dir.join(format!("agent-offline-{}.zip", uuid::Uuid::new_v4()));
let tx = progress_sender(&state, "global").await; let tx = progress_sender(&state, "global").await;
let result = async { let result = async {
let mut upload = tokio::fs::File::create(&zip_path).await.map_err(|err| AppError(err.to_string()))?; let mut upload = tokio::fs::File::create(&zip_path).await.map_err(|err| AppError::from(err.to_string()))?;
let mut field = field; let mut field = field;
while let Some(chunk) = field.chunk().await.map_err(|err| AppError(err.to_string()))? { while let Some(chunk) = field.chunk().await.map_err(|err| AppError::from(err.to_string()))? {
upload.write_all(&chunk).await.map_err(|err| AppError(err.to_string()))?; upload.write_all(&chunk).await.map_err(|err| AppError::from(err.to_string()))?;
} }
upload.flush().await.map_err(|err| AppError(err.to_string()))?; upload.flush().await.map_err(|err| AppError::from(err.to_string()))?;
drop(upload); drop(upload);
let plan = inspect_offline_zip(&zip_path).map_err(AppError)?; let plan = inspect_offline_zip(&zip_path).map_err(AppError::from)?;
ensure_no_offline_import_blockers(&state.app, &plan).await.map_err(AppError)?; ensure_no_offline_import_blockers(&state.app, &plan).await.map_err(AppError::from)?;
import_agents_from_zip_core(&state.app.agent_manager, &zip_path, |event| send_progress_event(&tx, event)) import_agents_from_zip_core(&state.app.agent_manager, &zip_path, |event| send_progress_event(&tx, event))
.map_err(AppError) .map_err(AppError::from)
} }
.await; .await;
let _ = std::fs::remove_file(&zip_path); let _ = std::fs::remove_file(&zip_path);
@@ -201,7 +201,7 @@ pub async fn import_agents_from_zip(
return Ok(Json(serde_json::json!({ "count": result.drivers_installed.len() as u32 }))); return Ok(Json(serde_json::json!({ "count": result.drivers_installed.len() as u32 })));
} }
Err(AppError("No file uploaded".to_string())) Err(AppError::from("No file uploaded".to_string()))
} }
pub async fn import_agent_driver_file( pub async fn import_agent_driver_file(
@@ -215,28 +215,28 @@ pub async fn import_agent_driver_file(
while let Ok(Some(field)) = multipart.next_field().await { while let Ok(Some(field)) = multipart.next_field().await {
let name = field.name().unwrap_or("").to_string(); let name = field.name().unwrap_or("").to_string();
if name == "dbType" { if name == "dbType" {
db_type = Some(field.text().await.map_err(|e| AppError(e.to_string()))?); db_type = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?);
} else if name == "file" { } else if name == "file" {
driver_name = field.file_name().unwrap_or("agent").to_string(); driver_name = field.file_name().unwrap_or("agent").to_string();
driver_data = Some(field.bytes().await.map_err(|e| AppError(e.to_string()))?.to_vec()); driver_data = Some(field.bytes().await.map_err(|e| AppError::from(e.to_string()))?.to_vec());
} }
} }
let db_type = db_type.ok_or_else(|| AppError("Missing dbType field".to_string()))?; let db_type = db_type.ok_or_else(|| AppError::from("Missing dbType field".to_string()))?;
let data = driver_data.ok_or_else(|| AppError("No file uploaded".to_string()))?; let data = driver_data.ok_or_else(|| AppError::from("No file uploaded".to_string()))?;
let temp_dir = state.app.plugins.root_dir().join("agent_upload_tmp"); let temp_dir = state.app.plugins.root_dir().join("agent_upload_tmp");
std::fs::create_dir_all(&temp_dir).map_err(|e| AppError(e.to_string()))?; std::fs::create_dir_all(&temp_dir).map_err(|e| AppError::from(e.to_string()))?;
let suffix = std::path::Path::new(&driver_name) let suffix = std::path::Path::new(&driver_name)
.extension() .extension()
.and_then(|extension| extension.to_str()) .and_then(|extension| extension.to_str())
.map(|extension| format!(".{extension}")) .map(|extension| format!(".{extension}"))
.unwrap_or_default(); .unwrap_or_default();
let tmp_path = temp_dir.join(format!("agent-{}{}", uuid::Uuid::new_v4(), suffix)); let tmp_path = temp_dir.join(format!("agent-{}{}", uuid::Uuid::new_v4(), suffix));
std::fs::write(&tmp_path, &data).map_err(|e| AppError(e.to_string()))?; std::fs::write(&tmp_path, &data).map_err(|e| AppError::from(e.to_string()))?;
let result = async { let result = async {
ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&db_type)).await.map_err(AppError)?; ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&db_type)).await.map_err(AppError::from)?;
import_agent_driver(&state.app.agent_manager, &db_type, &tmp_path).map_err(AppError::from) import_agent_driver(&state.app.agent_manager, &db_type, &tmp_path).map_err(AppError::from)
} }
.await; .await;
@@ -254,7 +254,7 @@ pub async fn reinstall_jre(
send_progress_event(&tx, event); send_progress_event(&tx, event);
}) })
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
@@ -263,7 +263,7 @@ pub async fn uninstall_jre(
Json(req): Json<JreRequest>, Json(req): Json<JreRequest>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let key = req.jre_key.as_deref().unwrap_or(DEFAULT_JRE_KEY); let key = req.jre_key.as_deref().unwrap_or(DEFAULT_JRE_KEY);
uninstall_agent_jre(&state.app.agent_manager, key).await.map_err(AppError)?; uninstall_agent_jre(&state.app.agent_manager, key).await.map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
+16 -16
View File
@@ -113,12 +113,12 @@ pub async fn save_ai_config(
Json(body): Json<SaveAiConfigRequest>, Json(body): Json<SaveAiConfigRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
reject_web_unsupported_ai_provider(&body.config)?; reject_web_unsupported_ai_provider(&body.config)?;
state.app.storage.save_ai_config(&body.config).await.map_err(AppError)?; state.app.storage.save_ai_config(&body.config).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
pub async fn load_ai_config(State(state): State<Arc<WebState>>) -> Result<Json<Option<AiConfig>>, AppError> { pub async fn load_ai_config(State(state): State<Arc<WebState>>) -> Result<Json<Option<AiConfig>>, AppError> {
let config = state.app.storage.load_ai_config().await.map_err(AppError)?; let config = state.app.storage.load_ai_config().await.map_err(AppError::from)?;
Ok(Json(config)) Ok(Json(config))
} }
@@ -130,14 +130,14 @@ pub async fn save_ai_provider_config(
let mut config = body.config; let mut config = body.config;
config.provider = parsed_provider; config.provider = parsed_provider;
reject_web_unsupported_ai_provider(&config)?; reject_web_unsupported_ai_provider(&config)?;
state.app.storage.save_ai_provider_config(&body.provider, &config).await.map_err(AppError)?; state.app.storage.save_ai_provider_config(&body.provider, &config).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
pub async fn load_ai_provider_configs( pub async fn load_ai_provider_configs(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
) -> Result<Json<HashMap<String, AiConfig>>, AppError> { ) -> Result<Json<HashMap<String, AiConfig>>, AppError> {
let configs = state.app.storage.load_ai_provider_configs().await.map_err(AppError)?; let configs = state.app.storage.load_ai_provider_configs().await.map_err(AppError::from)?;
Ok(Json(configs)) Ok(Json(configs))
} }
@@ -158,12 +158,12 @@ pub async fn save_ai_configs(
for item in &body.configs { for item in &body.configs {
reject_web_unsupported_ai_provider(&item.config)?; reject_web_unsupported_ai_provider(&item.config)?;
} }
state.app.storage.save_ai_configs(&body.configs).await.map_err(AppError)?; state.app.storage.save_ai_configs(&body.configs).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
pub async fn load_ai_configs(State(state): State<Arc<WebState>>) -> Result<Json<Vec<AiConfigItem>>, AppError> { pub async fn load_ai_configs(State(state): State<Arc<WebState>>) -> Result<Json<Vec<AiConfigItem>>, AppError> {
let configs = state.app.storage.load_ai_configs().await.map_err(AppError)?; let configs = state.app.storage.load_ai_configs().await.map_err(AppError::from)?;
Ok(Json(configs)) Ok(Json(configs))
} }
@@ -177,7 +177,7 @@ pub async fn set_default_ai_config(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<SetDefaultAiConfigRequest>, Json(body): Json<SetDefaultAiConfigRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.set_default_ai_config(&body.config_id).await.map_err(AppError)?; state.app.storage.set_default_ai_config(&body.config_id).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -192,7 +192,7 @@ pub async fn save_ai_config_item(
Json(body): Json<SaveAiConfigItemRequest>, Json(body): Json<SaveAiConfigItemRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
reject_web_unsupported_ai_provider(&body.config.config)?; reject_web_unsupported_ai_provider(&body.config.config)?;
state.app.storage.save_ai_config_item(&body.config).await.map_err(AppError)?; state.app.storage.save_ai_config_item(&body.config).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -200,7 +200,7 @@ pub async fn delete_ai_config(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Path(config_id): Path<String>, Path(config_id): Path<String>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.delete_ai_config(&config_id).await.map_err(AppError)?; state.app.storage.delete_ai_config(&config_id).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -212,12 +212,12 @@ pub async fn save_ai_conversation(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<SaveAiConversationRequest>, Json(body): Json<SaveAiConversationRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.save_ai_conversation(&body.conversation).await.map_err(AppError)?; state.app.storage.save_ai_conversation(&body.conversation).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
pub async fn load_ai_conversations(State(state): State<Arc<WebState>>) -> Result<Json<Vec<AiConversation>>, AppError> { pub async fn load_ai_conversations(State(state): State<Arc<WebState>>) -> Result<Json<Vec<AiConversation>>, AppError> {
let conversations = state.app.storage.load_ai_conversations().await.map_err(AppError)?; let conversations = state.app.storage.load_ai_conversations().await.map_err(AppError::from)?;
Ok(Json(conversations)) Ok(Json(conversations))
} }
@@ -225,7 +225,7 @@ pub async fn delete_ai_conversation(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Path(id): Path<String>, Path(id): Path<String>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.delete_ai_conversation(&id).await.map_err(AppError)?; state.app.storage.delete_ai_conversation(&id).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -235,7 +235,7 @@ pub async fn delete_ai_conversation(
pub async fn ai_complete(Json(body): Json<AiCompleteRequest>) -> Result<Json<String>, AppError> { pub async fn ai_complete(Json(body): Json<AiCompleteRequest>) -> Result<Json<String>, AppError> {
reject_web_unsupported_ai_provider(&body.request.config)?; reject_web_unsupported_ai_provider(&body.request.config)?;
let result = dbx_core::ai::complete(&body.request).await.map_err(AppError)?; let result = dbx_core::ai::complete(&body.request).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -247,13 +247,13 @@ pub async fn ai_test_connection(
Json(body): Json<AiTestConnectionRequest>, Json(body): Json<AiTestConnectionRequest>,
) -> Result<Json<AiTestConnectionResult>, AppError> { ) -> Result<Json<AiTestConnectionResult>, AppError> {
reject_web_unsupported_ai_provider(&body.config)?; reject_web_unsupported_ai_provider(&body.config)?;
let result = dbx_core::ai::test_connection_core(&body.config).await.map_err(AppError)?; let result = dbx_core::ai::test_connection_core(&body.config).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
pub async fn ai_list_models(Json(body): Json<AiListModelsRequest>) -> Result<Json<Vec<AiModelInfo>>, AppError> { pub async fn ai_list_models(Json(body): Json<AiListModelsRequest>) -> Result<Json<Vec<AiModelInfo>>, AppError> {
reject_web_unsupported_ai_provider(&body.config)?; reject_web_unsupported_ai_provider(&body.config)?;
let result = dbx_core::ai::list_models_core(&body.config).await.map_err(AppError)?; let result = dbx_core::ai::list_models_core(&body.config).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -323,7 +323,7 @@ pub async fn ai_agent_stream(
let (tx, rx) = tokio::sync::broadcast::channel::<String>(256); let (tx, rx) = tokio::sync::broadcast::channel::<String>(256);
let parsed_db_type: DatabaseType = serde_json::from_str(&format!("\"{}\"", body.db_type)) let parsed_db_type: DatabaseType = serde_json::from_str(&format!("\"{}\"", body.db_type))
.map_err(|_| AppError(format!("Unknown database type: {}", body.db_type)))?; .map_err(|_| AppError::from(format!("Unknown database type: {}", body.db_type)))?;
let production_database = state let production_database = state
.app .app
.configs .configs
+5 -5
View File
@@ -39,7 +39,7 @@ pub struct DecryptConfigRequest {
} }
pub async fn load_pinned_tree_node_ids(State(state): State<Arc<WebState>>) -> Result<Json<Vec<String>>, AppError> { pub async fn load_pinned_tree_node_ids(State(state): State<Arc<WebState>>) -> Result<Json<Vec<String>>, AppError> {
let ids = state.app.storage.load_pinned_tree_node_ids().await.map_err(AppError)?; let ids = state.app.storage.load_pinned_tree_node_ids().await.map_err(AppError::from)?;
Ok(Json(ids)) Ok(Json(ids))
} }
@@ -47,26 +47,26 @@ pub async fn save_pinned_tree_node_ids(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<SavePinnedTreeNodeIdsRequest>, Json(body): Json<SavePinnedTreeNodeIdsRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.save_pinned_tree_node_ids(&body.ids).await.map_err(AppError)?; state.app.storage.save_pinned_tree_node_ids(&body.ids).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
pub async fn load_mcp_global_policy( pub async fn load_mcp_global_policy(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
) -> Result<Json<McpGlobalPolicyState>, AppError> { ) -> Result<Json<McpGlobalPolicyState>, AppError> {
state.app.storage.load_mcp_global_policy().await.map(Json).map_err(AppError) state.app.storage.load_mcp_global_policy().await.map(Json).map_err(AppError::from)
} }
pub async fn save_mcp_global_policy( pub async fn save_mcp_global_policy(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(policy): Json<McpGlobalPolicy>, Json(policy): Json<McpGlobalPolicy>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.save_mcp_global_policy(&policy).await.map_err(AppError)?; state.app.storage.save_mcp_global_policy(&policy).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
pub async fn decrypt_config(Json(body): Json<DecryptConfigRequest>) -> Result<Json<String>, AppError> { pub async fn decrypt_config(Json(body): Json<DecryptConfigRequest>) -> Result<Json<String>, AppError> {
decrypt_config_payload(&body.payload, &body.passphrase).map(Json).map_err(AppError) decrypt_config_payload(&body.payload, &body.passphrase).map(Json).map_err(AppError::from)
} }
fn decrypt_config_payload(payload: &EncryptedConfigPayload, passphrase: &str) -> Result<String, String> { fn decrypt_config_payload(payload: &EncryptedConfigPayload, passphrase: &str) -> Result<String, String> {
+27 -27
View File
@@ -102,8 +102,8 @@ pub async fn webdav_sync_test(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(mut req): Json<WebDavConfigRequest>, Json(mut req): Json<WebDavConfigRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError)?; resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
WebDavClient::new(req.config).test().await.map_err(AppError)?; WebDavClient::new(req.config).test().await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -111,14 +111,14 @@ pub async fn webdav_password_status(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<WebDavConfigRequest>, Json(req): Json<WebDavConfigRequest>,
) -> Result<Json<WebDavPasswordStatus>, AppError> { ) -> Result<Json<WebDavPasswordStatus>, AppError> {
webdav_saved_password_status(&state.app.storage, &req.config).await.map(Json).map_err(AppError) webdav_saved_password_status(&state.app.storage, &req.config).await.map(Json).map_err(AppError::from)
} }
pub async fn save_webdav_saved_password( pub async fn save_webdav_saved_password(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<SaveWebDavPasswordRequest>, Json(req): Json<SaveWebDavPasswordRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
save_webdav_password(&state.app.storage, &req.config, &req.password).await.map_err(AppError)?; save_webdav_password(&state.app.storage, &req.config, &req.password).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -126,14 +126,14 @@ pub async fn forget_webdav_saved_password(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<WebDavConfigRequest>, Json(req): Json<WebDavConfigRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
forget_webdav_password(&state.app.storage, &req.config).await.map_err(AppError)?; forget_webdav_password(&state.app.storage, &req.config).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
pub async fn webdav_sync_secrets_status( pub async fn webdav_sync_secrets_status(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
) -> Result<Json<WebDavSyncSecretsStatus>, AppError> { ) -> Result<Json<WebDavSyncSecretsStatus>, AppError> {
core_webdav_sync_secrets_status(&state.app.storage).await.map(Json).map_err(AppError) core_webdav_sync_secrets_status(&state.app.storage).await.map(Json).map_err(AppError::from)
} }
pub async fn save_webdav_sync_secrets_preference( pub async fn save_webdav_sync_secrets_preference(
@@ -142,12 +142,12 @@ pub async fn save_webdav_sync_secrets_preference(
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
core_save_webdav_sync_secrets_preference(&state.app.storage, req.enabled, req.passphrase.as_deref()) core_save_webdav_sync_secrets_preference(&state.app.storage, req.enabled, req.passphrase.as_deref())
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
pub async fn forget_webdav_sync_secrets_passphrase(State(state): State<Arc<WebState>>) -> Result<Json<()>, AppError> { pub async fn forget_webdav_sync_secrets_passphrase(State(state): State<Arc<WebState>>) -> Result<Json<()>, AppError> {
core_forget_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError)?; core_forget_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -155,7 +155,7 @@ pub async fn webdav_sync_upload(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(mut req): Json<WebDavUploadRequest>, Json(mut req): Json<WebDavUploadRequest>,
) -> Result<Json<WebDavSyncSummary>, AppError> { ) -> Result<Json<WebDavSyncSummary>, AppError> {
resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError)?; resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
let snapshot = build_sync_snapshot_with_saved_secrets( let snapshot = build_sync_snapshot_with_saved_secrets(
&state.app.storage, &state.app.storage,
env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_VERSION"),
@@ -163,21 +163,21 @@ pub async fn webdav_sync_upload(
req.secrets_passphrase.as_deref(), req.secrets_passphrase.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
WebDavClient::new(req.config).put_snapshot(&snapshot).await.map(Json).map_err(AppError) WebDavClient::new(req.config).put_snapshot(&snapshot).await.map(Json).map_err(AppError::from)
} }
pub async fn webdav_sync_download( pub async fn webdav_sync_download(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(mut req): Json<WebDavDownloadRequest>, Json(mut req): Json<WebDavDownloadRequest>,
) -> Result<Json<WebDavDownloadResult>, AppError> { ) -> Result<Json<WebDavDownloadResult>, AppError> {
resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError)?; resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
let (snapshot, summary) = WebDavClient::new(req.config).get_snapshot().await.map_err(AppError)?; let (snapshot, summary) = WebDavClient::new(req.config).get_snapshot().await.map_err(AppError::from)?;
let explicit_passphrase = req.secrets_passphrase.as_deref().map(str::trim).filter(|value| !value.is_empty()); let explicit_passphrase = req.secrets_passphrase.as_deref().map(str::trim).filter(|value| !value.is_empty());
let saved_passphrase = if explicit_passphrase.is_some() { let saved_passphrase = if explicit_passphrase.is_some() {
None None
} else { } else {
resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError)? resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError::from)?
}; };
let apply_summary = apply_sync_snapshot( let apply_summary = apply_sync_snapshot(
&state.app.storage, &state.app.storage,
@@ -185,7 +185,7 @@ pub async fn webdav_sync_download(
ApplySnapshotOptions { secrets_passphrase: explicit_passphrase.or(saved_passphrase.as_deref()) }, ApplySnapshotOptions { secrets_passphrase: explicit_passphrase.or(saved_passphrase.as_deref()) },
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(WebDavDownloadResult { Ok(Json(WebDavDownloadResult {
summary, summary,
editor_settings: snapshot.editor_settings, editor_settings: snapshot.editor_settings,
@@ -198,8 +198,8 @@ pub async fn snippet_sync_test(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(mut req): Json<SnippetConfigRequest>, Json(mut req): Json<SnippetConfigRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError)?; resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
SnippetSyncClient::new(req.config).test().await.map_err(AppError)?; SnippetSyncClient::new(req.config).test().await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -207,14 +207,14 @@ pub async fn snippet_token_status(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<SnippetConfigRequest>, Json(req): Json<SnippetConfigRequest>,
) -> Result<Json<SnippetTokenStatus>, AppError> { ) -> Result<Json<SnippetTokenStatus>, AppError> {
snippet_saved_token_status(&state.app.storage, &req.config).await.map(Json).map_err(AppError) snippet_saved_token_status(&state.app.storage, &req.config).await.map(Json).map_err(AppError::from)
} }
pub async fn save_snippet_saved_token( pub async fn save_snippet_saved_token(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<SaveSnippetTokenRequest>, Json(req): Json<SaveSnippetTokenRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
save_snippet_token(&state.app.storage, &req.config, &req.token).await.map_err(AppError)?; save_snippet_token(&state.app.storage, &req.config, &req.token).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -222,7 +222,7 @@ pub async fn forget_snippet_saved_token(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<SnippetConfigRequest>, Json(req): Json<SnippetConfigRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
forget_snippet_token(&state.app.storage, &req.config).await.map_err(AppError)?; forget_snippet_token(&state.app.storage, &req.config).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -230,7 +230,7 @@ pub async fn snippet_sync_upload(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(mut req): Json<SnippetUploadRequest>, Json(mut req): Json<SnippetUploadRequest>,
) -> Result<Json<SnippetSyncSummary>, AppError> { ) -> Result<Json<SnippetSyncSummary>, AppError> {
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError)?; resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
let snapshot = build_sync_snapshot_with_saved_secrets( let snapshot = build_sync_snapshot_with_saved_secrets(
&state.app.storage, &state.app.storage,
env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_VERSION"),
@@ -238,21 +238,21 @@ pub async fn snippet_sync_upload(
req.secrets_passphrase.as_deref(), req.secrets_passphrase.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
SnippetSyncClient::new(req.config).put_snapshot(&snapshot).await.map(Json).map_err(AppError) SnippetSyncClient::new(req.config).put_snapshot(&snapshot).await.map(Json).map_err(AppError::from)
} }
pub async fn snippet_sync_download( pub async fn snippet_sync_download(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(mut req): Json<SnippetDownloadRequest>, Json(mut req): Json<SnippetDownloadRequest>,
) -> Result<Json<SnippetDownloadResult>, AppError> { ) -> Result<Json<SnippetDownloadResult>, AppError> {
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError)?; resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
let (snapshot, summary) = SnippetSyncClient::new(req.config).get_snapshot().await.map_err(AppError)?; let (snapshot, summary) = SnippetSyncClient::new(req.config).get_snapshot().await.map_err(AppError::from)?;
let explicit_passphrase = req.secrets_passphrase.as_deref().map(str::trim).filter(|value| !value.is_empty()); let explicit_passphrase = req.secrets_passphrase.as_deref().map(str::trim).filter(|value| !value.is_empty());
let saved_passphrase = if explicit_passphrase.is_some() { let saved_passphrase = if explicit_passphrase.is_some() {
None None
} else { } else {
resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError)? resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError::from)?
}; };
let apply_summary = apply_sync_snapshot( let apply_summary = apply_sync_snapshot(
&state.app.storage, &state.app.storage,
@@ -260,7 +260,7 @@ pub async fn snippet_sync_download(
ApplySnapshotOptions { secrets_passphrase: explicit_passphrase.or(saved_passphrase.as_deref()) }, ApplySnapshotOptions { secrets_passphrase: explicit_passphrase.or(saved_passphrase.as_deref()) },
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(SnippetDownloadResult { Ok(Json(SnippetDownloadResult {
summary, summary,
editor_settings: snapshot.editor_settings, editor_settings: snapshot.editor_settings,
+29 -24
View File
@@ -111,14 +111,14 @@ pub async fn test_connection(
run_temporary_connection_test(&state.app, body.config, false) run_temporary_connection_test(&state.app, body.config, false)
.await .await
.map(|result| Json(result.message)) .map(|result| Json(result.message))
.map_err(AppError) .map_err(AppError::from)
} }
pub async fn test_connection_with_info( pub async fn test_connection_with_info(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<ConnectRequest>, Json(body): Json<ConnectRequest>,
) -> Result<Json<ConnectionTestResult>, AppError> { ) -> Result<Json<ConnectionTestResult>, AppError> {
run_temporary_connection_test(&state.app, body.config, true).await.map(Json).map_err(AppError) run_temporary_connection_test(&state.app, body.config, true).await.map(Json).map_err(AppError::from)
} }
pub async fn connect_db( pub async fn connect_db(
@@ -132,7 +132,7 @@ pub async fn connect_db(
&config.password, &config.password,
!config.attached_databases.is_empty(), !config.attached_databases.is_empty(),
) )
.map_err(AppError)?; .map_err(AppError::from)?;
} }
let app = &state.app; let app = &state.app;
let connection_id = config.id.clone(); let connection_id = config.id.clone();
@@ -142,7 +142,7 @@ pub async fn connect_db(
app.reset_connection_transport_for_config(&connection_id, &config).await; app.reset_connection_transport_for_config(&connection_id, &config).await;
app.configs.write().await.insert(connection_id.clone(), config.clone()); app.configs.write().await.insert(connection_id.clone(), config.clone());
app.get_or_create_pool_for_connection_attempt(&connection_id, None, attempt).await.map_err(AppError)?; app.get_or_create_pool_for_connection_attempt(&connection_id, None, attempt).await.map_err(AppError::from)?;
Ok(Json(connection_id)) Ok(Json(connection_id))
} }
@@ -151,7 +151,12 @@ pub async fn connected_database_info(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<ConnectionIdentifierQuoteRequest>, Json(body): Json<ConnectionIdentifierQuoteRequest>,
) -> Result<Json<Option<DatabaseConnectionInfo>>, AppError> { ) -> Result<Json<Option<DatabaseConnectionInfo>>, AppError> {
state.app.connection_database_info(&body.connection_id, body.database.as_deref()).await.map(Json).map_err(AppError) state
.app
.connection_database_info(&body.connection_id, body.database.as_deref())
.await
.map(Json)
.map_err(AppError::from)
} }
pub async fn save_connection_database_info( pub async fn save_connection_database_info(
@@ -163,7 +168,7 @@ pub async fn save_connection_database_info(
.save_connection_database_info(&body.connection_id, body.database_info) .save_connection_database_info(&body.connection_id, body.database_info)
.await .await
.map(|_| Json(())) .map(|_| Json(()))
.map_err(AppError) .map_err(AppError::from)
} }
pub async fn connection_final_proxy_port( pub async fn connection_final_proxy_port(
@@ -172,7 +177,7 @@ pub async fn connection_final_proxy_port(
) -> Result<Json<u16>, AppError> { ) -> Result<Json<u16>, AppError> {
let runtime_config = body.config.canonicalized(); let runtime_config = body.config.canonicalized();
if !runtime_config.has_effective_transport_layers() { if !runtime_config.has_effective_transport_layers() {
return Err(AppError("Connection has no configured transport layers".to_string())); return Err(AppError::from("Connection has no configured transport layers".to_string()));
} }
if runtime_config.db_type == dbx_core::models::connection::DatabaseType::Sqlite { if runtime_config.db_type == dbx_core::models::connection::DatabaseType::Sqlite {
dbx_core::db::sqlite::validate_persistent_attachments( dbx_core::db::sqlite::validate_persistent_attachments(
@@ -180,7 +185,7 @@ pub async fn connection_final_proxy_port(
&runtime_config.password, &runtime_config.password,
!runtime_config.attached_databases.is_empty(), !runtime_config.attached_databases.is_empty(),
) )
.map_err(AppError)?; .map_err(AppError::from)?;
} }
let app = &state.app; let app = &state.app;
@@ -188,7 +193,7 @@ pub async fn connection_final_proxy_port(
let db_config = dbx_core::connection::metadata_connection_config(&runtime_config); let db_config = dbx_core::connection::metadata_connection_config(&runtime_config);
app.configs.write().await.insert(connection_id.clone(), runtime_config); app.configs.write().await.insert(connection_id.clone(), runtime_config);
let (_, port) = app.connection_host_port(&connection_id, &db_config).await.map_err(AppError)?; let (_, port) = app.connection_host_port(&connection_id, &db_config).await.map_err(AppError::from)?;
Ok(Json(port)) Ok(Json(port))
} }
@@ -224,7 +229,7 @@ pub async fn check_connection_health(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<DisconnectRequest>, Json(body): Json<DisconnectRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.check_connection_health(&body.connection_id).await.map_err(AppError)?; state.app.check_connection_health(&body.connection_id).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -237,7 +242,7 @@ pub async fn connection_identifier_quote(
.connection_identifier_quote(&body.connection_id, body.database.as_deref()) .connection_identifier_quote(&body.connection_id, body.database.as_deref())
.await .await
.map(Json) .map(Json)
.map_err(AppError) .map_err(AppError::from)
} }
pub async fn close_database_connection( pub async fn close_database_connection(
@@ -246,7 +251,7 @@ pub async fn close_database_connection(
) -> Result<Json<bool>, AppError> { ) -> Result<Json<bool>, AppError> {
let database = body.database.trim(); let database = body.database.trim();
let database = if database.is_empty() { None } else { Some(database) }; let database = if database.is_empty() { None } else { Some(database) };
state.app.close_database_pool(&body.connection_id, database).await.map(Json).map_err(AppError) state.app.close_database_pool(&body.connection_id, database).await.map(Json).map_err(AppError::from)
} }
pub async fn save_connections( pub async fn save_connections(
@@ -260,10 +265,10 @@ pub async fn save_connections(
&config.password, &config.password,
!config.attached_databases.is_empty(), !config.attached_databases.is_empty(),
) )
.map_err(AppError)?; .map_err(AppError::from)?;
} }
} }
state.app.storage.save_connections(&body.configs).await.map_err(AppError)?; state.app.storage.save_connections(&body.configs).await.map_err(AppError::from)?;
let sync = sync_connection_configs(&state, &body.configs).await; let sync = sync_connection_configs(&state, &body.configs).await;
remove_connection_pools_for_connection_ids(&state, &sync.connection_pool_ids_to_drop).await; remove_connection_pools_for_connection_ids(&state, &sync.connection_pool_ids_to_drop).await;
drop_nacos_adapters_for_connection_ids(&state, &sync.nacos_adapter_ids_to_drop).await; drop_nacos_adapters_for_connection_ids(&state, &sync.nacos_adapter_ids_to_drop).await;
@@ -275,7 +280,7 @@ pub async fn mcp_add_connection(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<McpAddConnectionRequest>, Json(body): Json<McpAddConnectionRequest>,
) -> Result<Json<ConnectionConfig>, AppError> { ) -> Result<Json<ConnectionConfig>, AppError> {
let saved = state.app.storage.add_connection_for_mcp(body.config).await.map_err(AppError)?; let saved = state.app.storage.add_connection_for_mcp(body.config).await.map_err(AppError::from)?;
state.app.configs.write().await.insert(saved.id.clone(), saved.clone()); state.app.configs.write().await.insert(saved.id.clone(), saved.clone());
Ok(Json(saved)) Ok(Json(saved))
} }
@@ -285,7 +290,7 @@ pub async fn mcp_remove_connection(
Json(body): Json<McpRemoveConnectionRequest>, Json(body): Json<McpRemoveConnectionRequest>,
) -> Result<Json<bool>, AppError> { ) -> Result<Json<bool>, AppError> {
let connection_id = body.connection_id; let connection_id = body.connection_id;
let removed = state.app.storage.remove_connection_for_mcp(&connection_id).await.map_err(AppError)?; let removed = state.app.storage.remove_connection_for_mcp(&connection_id).await.map_err(AppError::from)?;
if removed { if removed {
state.app.configs.write().await.remove(&connection_id); state.app.configs.write().await.remove(&connection_id);
state.app.remove_connection_pools_detached(&connection_id).await; state.app.remove_connection_pools_detached(&connection_id).await;
@@ -297,7 +302,7 @@ pub async fn mcp_remove_connection(
} }
pub async fn load_connections(State(state): State<Arc<WebState>>) -> Result<Json<Vec<ConnectionConfig>>, AppError> { pub async fn load_connections(State(state): State<Arc<WebState>>) -> Result<Json<Vec<ConnectionConfig>>, AppError> {
let configs = state.app.storage.load_connections().await.map_err(AppError)?; let configs = state.app.storage.load_connections().await.map_err(AppError::from)?;
let sync = sync_connection_configs(&state, &configs).await; let sync = sync_connection_configs(&state, &configs).await;
remove_connection_pools_for_connection_ids(&state, &sync.connection_pool_ids_to_drop).await; remove_connection_pools_for_connection_ids(&state, &sync.connection_pool_ids_to_drop).await;
drop_nacos_adapters_for_connection_ids(&state, &sync.nacos_adapter_ids_to_drop).await; drop_nacos_adapters_for_connection_ids(&state, &sync.nacos_adapter_ids_to_drop).await;
@@ -507,13 +512,13 @@ mod tests {
Json(ConnectRequest { config: config.clone(), client_attempt: None }), Json(ConnectRequest { config: config.clone(), client_attempt: None }),
) )
.await .await
.unwrap_or_else(|error| panic!("{}", error.0)); .unwrap_or_else(|error| panic!("{}", error.message));
assert_eq!(legacy.0, "Connection successful"); assert_eq!(legacy.0, "Connection successful");
let detailed = let detailed =
test_connection_with_info(State(state.clone()), Json(ConnectRequest { config, client_attempt: None })) test_connection_with_info(State(state.clone()), Json(ConnectRequest { config, client_attempt: None }))
.await .await
.unwrap_or_else(|error| panic!("{}", error.0)); .unwrap_or_else(|error| panic!("{}", error.message));
assert_eq!(detailed.0.message, "Connection successful"); assert_eq!(detailed.0.message, "Connection successful");
assert_eq!(detailed.0.database_info, None); assert_eq!(detailed.0.database_info, None);
assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_"))); assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_")));
@@ -545,7 +550,7 @@ mod tests {
connect_db(State(state.clone()), Json(ConnectRequest { config: invalid.clone(), client_attempt: None })) connect_db(State(state.clone()), Json(ConnectRequest { config: invalid.clone(), client_attempt: None }))
.await .await
.unwrap_err(); .unwrap_err();
assert!(connect_error.0.contains("in-memory main database"), "{}", connect_error.0); assert!(connect_error.message.contains("in-memory main database"), "{}", connect_error.message);
invalid.transport_layers.push(TransportLayerConfig::Proxy(ProxyTunnelConfig { invalid.transport_layers.push(TransportLayerConfig::Proxy(ProxyTunnelConfig {
id: "proxy".to_string(), id: "proxy".to_string(),
@@ -565,7 +570,7 @@ mod tests {
) )
.await .await
.unwrap_err(); .unwrap_err();
assert!(proxy_error.0.contains("in-memory main database"), "{}", proxy_error.0); assert!(proxy_error.message.contains("in-memory main database"), "{}", proxy_error.message);
assert!(state.app.connections.read().await.contains_key(&initial.id)); assert!(state.app.connections.read().await.contains_key(&initial.id));
assert_eq!(state.app.configs.read().await.get(&initial.id), Some(&initial)); assert_eq!(state.app.configs.read().await.get(&initial.id), Some(&initial));
@@ -681,7 +686,7 @@ mod tests {
Json(McpRemoveConnectionRequest { connection_id: removed.id.clone() }), Json(McpRemoveConnectionRequest { connection_id: removed.id.clone() }),
) )
.await .await
.unwrap_or_else(|error| panic!("{}", error.0)); .unwrap_or_else(|error| panic!("{}", error.message));
assert!(removed_result.0); assert!(removed_result.0);
assert_eq!(state.app.storage.load_connections().await.unwrap()[0].id, kept.id); assert_eq!(state.app.storage.load_connections().await.unwrap()[0].id, kept.id);
@@ -691,7 +696,7 @@ mod tests {
) )
.await .await
.unwrap_err(); .unwrap_err();
assert!(scope_error.0.starts_with("CONNECTION_OUT_OF_SCOPE:")); assert!(scope_error.message.starts_with("CONNECTION_OUT_OF_SCOPE:"));
state state
.app .app
@@ -709,7 +714,7 @@ mod tests {
) )
.await .await
.unwrap_err(); .unwrap_err();
assert!(read_only_error.0.starts_with("MCP_READ_ONLY:")); assert!(read_only_error.message.starts_with("MCP_READ_ONLY:"));
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
} }
+9 -3
View File
@@ -8,21 +8,27 @@ use crate::state::WebState;
pub async fn prepare_data_compare( pub async fn prepare_data_compare(
Json(options): Json<dbx_core::data_compare::DataComparePreparationOptions>, Json(options): Json<dbx_core::data_compare::DataComparePreparationOptions>,
) -> Result<Json<dbx_core::data_compare::DataComparePreparation>, AppError> { ) -> Result<Json<dbx_core::data_compare::DataComparePreparation>, AppError> {
dbx_core::data_compare::prepare_data_compare(options).map(Json).map_err(AppError) dbx_core::data_compare::prepare_data_compare(options).map(Json).map_err(AppError::from)
} }
pub async fn prepare_data_compare_from_tables( pub async fn prepare_data_compare_from_tables(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(options): Json<dbx_core::data_compare::DataCompareFromTablesOptions>, Json(options): Json<dbx_core::data_compare::DataCompareFromTablesOptions>,
) -> Result<Json<dbx_core::data_compare::DataCompareFromTablesPreparation>, AppError> { ) -> Result<Json<dbx_core::data_compare::DataCompareFromTablesPreparation>, AppError> {
dbx_core::data_compare::prepare_data_compare_from_tables(&state.app, options).await.map(Json).map_err(AppError) dbx_core::data_compare::prepare_data_compare_from_tables(&state.app, options)
.await
.map(Json)
.map_err(AppError::from)
} }
pub async fn prepare_data_compare_missing_target( pub async fn prepare_data_compare_missing_target(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(options): Json<dbx_core::data_compare::DataCompareMissingTargetOptions>, Json(options): Json<dbx_core::data_compare::DataCompareMissingTargetOptions>,
) -> Result<Json<dbx_core::data_compare::DataCompareFromTablesPreparation>, AppError> { ) -> Result<Json<dbx_core::data_compare::DataCompareFromTablesPreparation>, AppError> {
dbx_core::data_compare::prepare_data_compare_missing_target(&state.app, options).await.map(Json).map_err(AppError) dbx_core::data_compare::prepare_data_compare_missing_target(&state.app, options)
.await
.map(Json)
.map_err(AppError::from)
} }
pub async fn build_data_compare_sync_plan( pub async fn build_data_compare_sync_plan(
+5 -5
View File
@@ -40,7 +40,7 @@ pub async fn start_database_export(
// CWD, unreachable by the browser). The file is served back via the download // CWD, unreachable by the browser). The file is served back via the download
// endpoint after the export completes. // endpoint after the export completes.
let tmp_dir = state.data_dir.join("tmp"); let tmp_dir = state.data_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?; std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
let tmp_file = tmp_dir.join(format!("database_export_{export_id}.sql")); let tmp_file = tmp_dir.join(format!("database_export_{export_id}.sql"));
let tmp_file_path = tmp_file.to_string_lossy().to_string(); let tmp_file_path = tmp_file.to_string_lossy().to_string();
req.file_path = tmp_file_path.clone(); req.file_path = tmp_file_path.clone();
@@ -128,7 +128,7 @@ pub async fn database_export_progress(
Path(export_id): Path<String>, Path(export_id): Path<String>,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> { ) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> {
let channels = state.sse_channels.read().await; let channels = state.sse_channels.read().await;
let tx = channels.get(&export_id).ok_or_else(|| AppError("Export not found".to_string()))?; let tx = channels.get(&export_id).ok_or_else(|| AppError::from("Export not found".to_string()))?;
let rx = tx.subscribe(); let rx = tx.subscribe();
drop(channels); drop(channels);
Ok(crate::sse::sse_from_channel(rx)) Ok(crate::sse::sse_from_channel(rx))
@@ -151,9 +151,9 @@ pub async fn database_export_download(
.write() .write()
.await .await
.remove(&export_id) .remove(&export_id)
.ok_or_else(|| AppError("Export file not found".to_string()))?; .ok_or_else(|| AppError::from("Export file not found".to_string()))?;
let data = tokio::fs::read(&file_path).await.map_err(|e| AppError(e.to_string()))?; let data = tokio::fs::read(&file_path).await.map_err(|e| AppError::from(e.to_string()))?;
// Clean up temp file // Clean up temp file
let _ = tokio::fs::remove_file(&file_path).await; let _ = tokio::fs::remove_file(&file_path).await;
@@ -162,5 +162,5 @@ pub async fn database_export_download(
.header(header::CONTENT_TYPE, "application/sql; charset=utf-8") .header(header::CONTENT_TYPE, "application/sql; charset=utf-8")
.header(header::CONTENT_DISPOSITION, format!("attachment; filename=\"{download_filename}\"")) .header(header::CONTENT_DISPOSITION, format!("attachment; filename=\"{download_filename}\""))
.body(Body::from(data)) .body(Body::from(data))
.map_err(|e| AppError(e.to_string())) .map_err(|e| AppError::from(e.to_string()))
} }
+29 -28
View File
@@ -20,11 +20,11 @@ where
let token = query.token(); let token = query.token();
tokio::select! { tokio::select! {
biased; biased;
_ = token.cancelled() => Err(AppError(dbx_core::query::canceled_error())), _ = token.cancelled() => Err(AppError::from(dbx_core::query::canceled_error())),
result = future => result.map_err(AppError), result = future => result.map_err(AppError::from),
} }
} else { } else {
future.await.map_err(AppError) future.await.map_err(AppError::from)
} }
} }
@@ -34,7 +34,7 @@ async fn ensure_writable(
action: &str, action: &str,
) -> Result<(), AppError> { ) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
return Err(AppError(format!( return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.", "Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
name, action name, action
))); )));
@@ -149,7 +149,8 @@ pub async fn list_databases(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<DocumentListDatabasesRequest>, Json(req): Json<DocumentListDatabasesRequest>,
) -> Result<Json<Vec<String>>, AppError> { ) -> Result<Json<Vec<String>>, AppError> {
let result = dbx_core::document_ops::list_databases_core(&state.app, &req.connection_id).await.map_err(AppError)?; let result =
dbx_core::document_ops::list_databases_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -159,7 +160,7 @@ pub async fn list_collections(
) -> Result<Json<Vec<dbx_core::document_ops::CollectionInfo>>, AppError> { ) -> Result<Json<Vec<dbx_core::document_ops::CollectionInfo>>, AppError> {
let result = dbx_core::document_ops::list_collections_core(&state.app, &req.connection_id, &req.database) let result = dbx_core::document_ops::list_collections_core(&state.app, &req.connection_id, &req.database)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -200,7 +201,7 @@ pub async fn insert_document(
req.routing.as_deref(), req.routing.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -219,7 +220,7 @@ pub async fn update_document(
req.routing.as_deref(), req.routing.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -237,7 +238,7 @@ pub async fn delete_document(
req.routing.as_deref(), req.routing.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -254,7 +255,7 @@ pub async fn list_gridfs_files(
req.sort.as_deref(), req.sort.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -270,7 +271,7 @@ pub async fn list_gridfs_buckets(
req.sort.as_deref(), req.sort.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -281,7 +282,7 @@ pub async fn create_gridfs_bucket(
ensure_writable(&state.app, &req.connection_id, "Create GridFS bucket").await?; ensure_writable(&state.app, &req.connection_id, "Create GridFS bucket").await?;
dbx_core::document_ops::create_gridfs_bucket_core(&state.app, &req.connection_id, &req.database, &req.bucket) dbx_core::document_ops::create_gridfs_bucket_core(&state.app, &req.connection_id, &req.database, &req.bucket)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -292,7 +293,7 @@ pub async fn delete_gridfs_bucket(
ensure_writable(&state.app, &req.connection_id, "Delete GridFS bucket").await?; ensure_writable(&state.app, &req.connection_id, "Delete GridFS bucket").await?;
dbx_core::document_ops::delete_gridfs_bucket_core(&state.app, &req.connection_id, &req.database, &req.bucket) dbx_core::document_ops::delete_gridfs_bucket_core(&state.app, &req.connection_id, &req.database, &req.bucket)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -308,7 +309,7 @@ pub async fn download_gridfs_file(
&req.file_id, &req.file_id,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -323,14 +324,14 @@ pub async fn upload_gridfs_file(
let mut content_type: Option<String> = None; let mut content_type: Option<String> = None;
let mut file_bytes: Option<Vec<u8>> = None; let mut file_bytes: Option<Vec<u8>> = None;
while let Some(field) = multipart.next_field().await.map_err(|e| AppError(e.to_string()))? { while let Some(field) = multipart.next_field().await.map_err(|e| AppError::from(e.to_string()))? {
let name = field.name().unwrap_or_default().to_string(); let name = field.name().unwrap_or_default().to_string();
match name.as_str() { match name.as_str() {
"connectionId" => connection_id = Some(field.text().await.map_err(|e| AppError(e.to_string()))?), "connectionId" => connection_id = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?),
"database" => database = Some(field.text().await.map_err(|e| AppError(e.to_string()))?), "database" => database = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?),
"bucket" => bucket = Some(field.text().await.map_err(|e| AppError(e.to_string()))?), "bucket" => bucket = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?),
"fileName" => file_name = Some(field.text().await.map_err(|e| AppError(e.to_string()))?), "fileName" => file_name = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?),
"contentType" => content_type = Some(field.text().await.map_err(|e| AppError(e.to_string()))?), "contentType" => content_type = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?),
"file" => { "file" => {
if file_name.is_none() { if file_name.is_none() {
file_name = field.file_name().map(str::to_string); file_name = field.file_name().map(str::to_string);
@@ -338,7 +339,7 @@ pub async fn upload_gridfs_file(
if content_type.is_none() { if content_type.is_none() {
content_type = field.content_type().map(str::to_string); content_type = field.content_type().map(str::to_string);
} }
file_bytes = Some(field.bytes().await.map_err(|e| AppError(e.to_string()))?.to_vec()); file_bytes = Some(field.bytes().await.map_err(|e| AppError::from(e.to_string()))?.to_vec());
} }
_ => { _ => {
let _ = field.bytes().await; let _ = field.bytes().await;
@@ -346,11 +347,11 @@ pub async fn upload_gridfs_file(
} }
} }
let connection_id = connection_id.ok_or_else(|| AppError("Missing connectionId".to_string()))?; let connection_id = connection_id.ok_or_else(|| AppError::from("Missing connectionId".to_string()))?;
let database = database.ok_or_else(|| AppError("Missing database".to_string()))?; let database = database.ok_or_else(|| AppError::from("Missing database".to_string()))?;
let bucket = bucket.ok_or_else(|| AppError("Missing bucket".to_string()))?; let bucket = bucket.ok_or_else(|| AppError::from("Missing bucket".to_string()))?;
let file_name = file_name.ok_or_else(|| AppError("Missing fileName".to_string()))?; let file_name = file_name.ok_or_else(|| AppError::from("Missing fileName".to_string()))?;
let file_bytes = file_bytes.ok_or_else(|| AppError("No file uploaded".to_string()))?; let file_bytes = file_bytes.ok_or_else(|| AppError::from("No file uploaded".to_string()))?;
ensure_writable(&state.app, &connection_id, "Upload GridFS file").await?; ensure_writable(&state.app, &connection_id, "Upload GridFS file").await?;
let result = dbx_core::document_ops::upload_gridfs_file_core( let result = dbx_core::document_ops::upload_gridfs_file_core(
@@ -363,7 +364,7 @@ pub async fn upload_gridfs_file(
content_type.as_deref(), content_type.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -380,6 +381,6 @@ pub async fn delete_gridfs_file(
&req.file_id, &req.file_id,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
+6 -5
View File
@@ -14,7 +14,7 @@ async fn ensure_writable(
action: &str, action: &str,
) -> Result<(), AppError> { ) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
return Err(AppError(format!( return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.", "Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
name, action name, action
))); )));
@@ -59,7 +59,7 @@ pub async fn list_prefix(
req.continuation.as_deref(), req.continuation.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -67,7 +67,8 @@ pub async fn get(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<EtcdKeyRequest>, Json(req): Json<EtcdKeyRequest>,
) -> Result<Json<dbx_core::agent_kv::KvGetResponse>, AppError> { ) -> Result<Json<dbx_core::agent_kv::KvGetResponse>, AppError> {
let result = dbx_core::agent_kv::kv_get_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError)?; let result =
dbx_core::agent_kv::kv_get_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -78,7 +79,7 @@ pub async fn put(
ensure_writable(&state.app, &req.connection_id, "Put").await?; ensure_writable(&state.app, &req.connection_id, "Put").await?;
let result = dbx_core::agent_kv::kv_put_core(&state.app, &req.connection_id, &req.key, req.value, req.lease) let result = dbx_core::agent_kv::kv_put_core(&state.app, &req.connection_id, &req.key, req.value, req.lease)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -88,6 +89,6 @@ pub async fn delete(
) -> Result<Json<dbx_core::agent_kv::KvDeleteResponse>, AppError> { ) -> Result<Json<dbx_core::agent_kv::KvDeleteResponse>, AppError> {
ensure_writable(&state.app, &req.connection_id, "Delete").await?; ensure_writable(&state.app, &req.connection_id, "Delete").await?;
let result = let result =
dbx_core::agent_kv::kv_delete_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError)?; dbx_core::agent_kv::kv_delete_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
+7 -6
View File
@@ -25,7 +25,7 @@ pub async fn save_history(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<SaveHistoryRequest>, Json(body): Json<SaveHistoryRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.save_history_entry(&body.entry).await.map_err(AppError)?; state.app.storage.save_history_entry(&body.entry).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -35,7 +35,8 @@ pub async fn load_history(
) -> Result<Json<Vec<HistoryEntry>>, AppError> { ) -> Result<Json<Vec<HistoryEntry>>, AppError> {
let limit = q.limit.unwrap_or(100); let limit = q.limit.unwrap_or(100);
let offset = q.offset.unwrap_or(0); let offset = q.offset.unwrap_or(0);
let entries = state.app.storage.load_history_entries(limit, offset, q.activity_kind).await.map_err(AppError)?; let entries =
state.app.storage.load_history_entries(limit, offset, q.activity_kind).await.map_err(AppError::from)?;
Ok(Json(entries)) Ok(Json(entries))
} }
@@ -43,17 +44,17 @@ pub async fn search_history(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(request): Json<HistorySearchRequest>, Json(request): Json<HistorySearchRequest>,
) -> Result<Json<HistorySearchResult>, AppError> { ) -> Result<Json<HistorySearchResult>, AppError> {
state.app.storage.search_history_entries(request).await.map(Json).map_err(AppError) state.app.storage.search_history_entries(request).await.map(Json).map_err(AppError::from)
} }
pub async fn load_history_connection_options( pub async fn load_history_connection_options(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
) -> Result<Json<Vec<HistoryConnectionOption>>, AppError> { ) -> Result<Json<Vec<HistoryConnectionOption>>, AppError> {
state.app.storage.load_history_connection_options().await.map(Json).map_err(AppError) state.app.storage.load_history_connection_options().await.map(Json).map_err(AppError::from)
} }
pub async fn clear_history(State(state): State<Arc<WebState>>) -> Result<Json<()>, AppError> { pub async fn clear_history(State(state): State<Arc<WebState>>) -> Result<Json<()>, AppError> {
state.app.storage.clear_history().await.map_err(AppError)?; state.app.storage.clear_history().await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -61,6 +62,6 @@ pub async fn delete_history_entry(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Path(id): Path<String>, Path(id): Path<String>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.delete_history_entry(&id).await.map_err(AppError)?; state.app.storage.delete_history_entry(&id).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
+2 -2
View File
@@ -17,11 +17,11 @@ pub async fn save_sidebar_layout(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<SaveLayoutRequest>, Json(body): Json<SaveLayoutRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.save_sidebar_layout(&body.layout).await.map_err(AppError)?; state.app.storage.save_sidebar_layout(&body.layout).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
pub async fn load_sidebar_layout(State(state): State<Arc<WebState>>) -> Result<Json<serde_json::Value>, AppError> { pub async fn load_sidebar_layout(State(state): State<Arc<WebState>>) -> Result<Json<serde_json::Value>, AppError> {
let layout = state.app.storage.load_sidebar_layout().await.map_err(AppError)?; let layout = state.app.storage.load_sidebar_layout().await.map_err(AppError::from)?;
Ok(Json(layout.unwrap_or(serde_json::json!(null)))) Ok(Json(layout.unwrap_or(serde_json::json!(null))))
} }
+18 -14
View File
@@ -228,12 +228,12 @@ fn mongo_field_predicate_is_exists_true(value: &serde_json::Value) -> bool {
} }
async fn load_policy(state: &Arc<WebState>) -> Result<McpGlobalPolicy, AppError> { async fn load_policy(state: &Arc<WebState>) -> Result<McpGlobalPolicy, AppError> {
state.app.storage.load_mcp_global_policy().await.map(|state| state.policy()).map_err(AppError) state.app.storage.load_mcp_global_policy().await.map(|state| state.policy()).map_err(AppError::from)
} }
fn ensure_allowed(policy: &McpGlobalPolicy, connection_id: &str) -> Result<(), AppError> { fn ensure_allowed(policy: &McpGlobalPolicy, connection_id: &str) -> Result<(), AppError> {
if policy.allowed_connection_ids.as_ref().is_some_and(|allowed| !allowed.iter().any(|id| id == connection_id)) { if policy.allowed_connection_ids.as_ref().is_some_and(|allowed| !allowed.iter().any(|id| id == connection_id)) {
return Err(AppError(format!( return Err(AppError::from(format!(
"CONNECTION_OUT_OF_SCOPE: connection '{connection_id}' is not allowed by DBX MCP settings" "CONNECTION_OUT_OF_SCOPE: connection '{connection_id}' is not allowed by DBX MCP settings"
))); )));
} }
@@ -241,7 +241,7 @@ fn ensure_allowed(policy: &McpGlobalPolicy, connection_id: &str) -> Result<(), A
} }
fn connection_read_only_error(message: impl Into<String>) -> AppError { fn connection_read_only_error(message: impl Into<String>) -> AppError {
AppError(format!("CONNECTION_READ_ONLY: {}", message.into())) AppError::from(format!("CONNECTION_READ_ONLY: {}", message.into()))
} }
async fn load_connection(state: &Arc<WebState>, connection_id: &str) -> Result<ConnectionConfig, AppError> { async fn load_connection(state: &Arc<WebState>, connection_id: &str) -> Result<ConnectionConfig, AppError> {
@@ -250,10 +250,10 @@ async fn load_connection(state: &Arc<WebState>, connection_id: &str) -> Result<C
.storage .storage
.load_connections() .load_connections()
.await .await
.map_err(AppError)? .map_err(AppError::from)?
.into_iter() .into_iter()
.find(|config| config.id == connection_id) .find(|config| config.id == connection_id)
.ok_or_else(|| AppError(format!("Connection with id '{connection_id}' not found"))) .ok_or_else(|| AppError::from(format!("Connection with id '{connection_id}' not found")))
} }
pub async fn ensure_scope(state: &Arc<WebState>, headers: &HeaderMap, connection_id: &str) -> Result<(), AppError> { pub async fn ensure_scope(state: &Arc<WebState>, headers: &HeaderMap, connection_id: &str) -> Result<(), AppError> {
@@ -295,7 +295,7 @@ pub async fn ensure_mongo_pipeline_target(
} }
let config = load_connection(state, connection_id).await?; let config = load_connection(state, connection_id).await?;
if dbx_core::production_safety::mongo_pipeline_targets_production_database(&config, database, pipeline_json) { if dbx_core::production_safety::mongo_pipeline_targets_production_database(&config, database, pipeline_json) {
return Err(AppError( return Err(AppError::from(
"PRODUCTION_DATABASE_READ_ONLY: MongoDB aggregate write targeting production scope is blocked.".to_string(), "PRODUCTION_DATABASE_READ_ONLY: MongoDB aggregate write targeting production scope is blocked.".to_string(),
)); ));
} }
@@ -316,10 +316,12 @@ async fn ensure_write_with_risk(
let policy = load_policy(state).await?; let policy = load_policy(state).await?;
ensure_allowed(&policy, connection_id)?; ensure_allowed(&policy, connection_id)?;
if policy.read_only { if policy.read_only {
return Err(AppError(format!("MCP_READ_ONLY: DBX MCP read-only mode is enabled. {action} blocked."))); return Err(AppError::from(format!("MCP_READ_ONLY: DBX MCP read-only mode is enabled. {action} blocked.")));
} }
if dangerous && !policy.allow_dangerous_sql { if dangerous && !policy.allow_dangerous_sql {
return Err(AppError(format!("SQL_BLOCKED: High-risk operation '{action}' is disabled in DBX MCP settings."))); return Err(AppError::from(format!(
"SQL_BLOCKED: High-risk operation '{action}' is disabled in DBX MCP settings."
)));
} }
let config = load_connection(state, connection_id).await?; let config = load_connection(state, connection_id).await?;
if config.read_only { if config.read_only {
@@ -329,7 +331,7 @@ async fn ensure_write_with_risk(
))); )));
} }
if dbx_core::production_safety::is_production_database(&config, database) { if dbx_core::production_safety::is_production_database(&config, database) {
return Err(AppError(format!( return Err(AppError::from(format!(
"PRODUCTION_DATABASE_READ_ONLY: {action} blocked for production database '{database}'." "PRODUCTION_DATABASE_READ_ONLY: {action} blocked for production database '{database}'."
))); )));
} }
@@ -350,21 +352,23 @@ pub async fn ensure_sql(
ensure_allowed(&policy, connection_id)?; ensure_allowed(&policy, connection_id)?;
let config = load_connection(state, connection_id).await?; let config = load_connection(state, connection_id).await?;
if dbx_core::sql_risk::mcp_sql_has_forbidden_database_switch(sql, config.db_type) { if dbx_core::sql_risk::mcp_sql_has_forbidden_database_switch(sql, config.db_type) {
return Err(AppError("SQL_BLOCKED: MCP does not allow USE or persistent database switching.".to_string())); return Err(AppError::from(
"SQL_BLOCKED: MCP does not allow USE or persistent database switching.".to_string(),
));
} }
let is_write = dbx_core::query_execution_sql::is_write_sql_for_database(sql, config.db_type); let is_write = dbx_core::query_execution_sql::is_write_sql_for_database(sql, config.db_type);
if policy.read_only && is_write { if policy.read_only && is_write {
return Err(AppError("MCP_READ_ONLY: DBX MCP read-only mode is enabled. SQL write blocked.".to_string())); return Err(AppError::from("MCP_READ_ONLY: DBX MCP read-only mode is enabled. SQL write blocked.".to_string()));
} }
if !policy.allow_dangerous_sql && dbx_core::sql_risk::is_dangerous_sql_for_database(sql, config.db_type) { if !policy.allow_dangerous_sql && dbx_core::sql_risk::is_dangerous_sql_for_database(sql, config.db_type) {
return Err(AppError("SQL_BLOCKED: High-risk SQL is disabled in DBX MCP settings.".to_string())); return Err(AppError::from("SQL_BLOCKED: High-risk SQL is disabled in DBX MCP settings.".to_string()));
} }
if config.read_only { if config.read_only {
dbx_core::query_execution_sql::check_read_only(sql, &config.name, config.db_type) dbx_core::query_execution_sql::check_read_only(sql, &config.name, config.db_type)
.map_err(connection_read_only_error)?; .map_err(connection_read_only_error)?;
} }
if is_write && dbx_core::production_safety::targets_production_database(&config, database, sql) { if is_write && dbx_core::production_safety::targets_production_database(&config, database, sql) {
return Err(AppError( return Err(AppError::from(
"PRODUCTION_DATABASE_READ_ONLY: SQL write targeting production scope is blocked.".to_string(), "PRODUCTION_DATABASE_READ_ONLY: SQL write targeting production scope is blocked.".to_string(),
)); ));
} }
@@ -377,7 +381,7 @@ mod tests {
#[test] #[test]
fn connection_read_only_errors_use_the_stable_mcp_code() { fn connection_read_only_errors_use_the_stable_mcp_code() {
assert_eq!(connection_read_only_error("write blocked").0, "CONNECTION_READ_ONLY: write blocked"); assert_eq!(connection_read_only_error("write blocked").message, "CONNECTION_READ_ONLY: write blocked");
} }
#[test] #[test]
+1
View File
@@ -16,6 +16,7 @@ pub mod mongo;
pub mod mq; pub mod mq;
pub mod nacos; pub mod nacos;
pub mod plugins; pub mod plugins;
pub mod prompt_template;
pub mod query; pub mod query;
pub mod query_result_export; pub mod query_result_export;
pub mod redis; pub mod redis;
+37 -37
View File
@@ -18,7 +18,7 @@ pub struct MongoParseShellCommandRequest {
pub async fn parse_shell_command( pub async fn parse_shell_command(
Json(req): Json<MongoParseShellCommandRequest>, Json(req): Json<MongoParseShellCommandRequest>,
) -> Result<Json<dbx_core::mongo_shell::MongoCommand>, AppError> { ) -> Result<Json<dbx_core::mongo_shell::MongoCommand>, AppError> {
dbx_core::mongo_shell::parse(&req.source).map(Json).map_err(AppError) dbx_core::mongo_shell::parse(&req.source).map(Json).map_err(AppError::from)
} }
async fn run_cancellable<T, F>(state: &Arc<WebState>, execution_id: Option<String>, future: F) -> Result<T, AppError> async fn run_cancellable<T, F>(state: &Arc<WebState>, execution_id: Option<String>, future: F) -> Result<T, AppError>
@@ -33,11 +33,11 @@ where
let token = query.token(); let token = query.token();
tokio::select! { tokio::select! {
biased; biased;
_ = token.cancelled() => Err(AppError(dbx_core::query::canceled_error())), _ = token.cancelled() => Err(AppError::from(dbx_core::query::canceled_error())),
result = future => result.map_err(AppError), result = future => result.map_err(AppError::from),
} }
} else { } else {
future.await.map_err(AppError) future.await.map_err(AppError::from)
} }
} }
@@ -48,7 +48,7 @@ async fn ensure_writable(
action: &str, action: &str,
) -> Result<(), AppError> { ) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
return Err(AppError(format!( return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.", "Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
name, action name, action
))); )));
@@ -284,7 +284,7 @@ pub async fn list_databases(
) -> Result<Json<Vec<String>>, AppError> { ) -> Result<Json<Vec<String>>, AppError> {
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?; super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
let result = let result =
dbx_core::mongo_ops::mongo_list_databases_core(&state.app, &req.connection_id).await.map_err(AppError)?; dbx_core::mongo_ops::mongo_list_databases_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -296,7 +296,7 @@ pub async fn list_collections(
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?; super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
let result = dbx_core::mongo_ops::mongo_list_collections_core(&state.app, &req.connection_id, &req.database) let result = dbx_core::mongo_ops::mongo_list_collections_core(&state.app, &req.connection_id, &req.database)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -319,7 +319,7 @@ pub async fn vector_collection_detail(
&req.collection, &req.collection,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -330,7 +330,7 @@ pub async fn create_database(
ensure_writable(&state.app, &req.connection_id, "Create database").await?; ensure_writable(&state.app, &req.connection_id, "Create database").await?;
dbx_core::mongo_ops::mongo_create_database_core(&state.app, &req.connection_id, &req.database) dbx_core::mongo_ops::mongo_create_database_core(&state.app, &req.connection_id, &req.database)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
@@ -341,7 +341,7 @@ pub async fn drop_database(
ensure_writable(&state.app, &req.connection_id, "Drop database").await?; ensure_writable(&state.app, &req.connection_id, "Drop database").await?;
dbx_core::mongo_ops::mongo_drop_database_core(&state.app, &req.connection_id, &req.database) dbx_core::mongo_ops::mongo_drop_database_core(&state.app, &req.connection_id, &req.database)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
@@ -355,7 +355,7 @@ pub async fn drop_collection(
ensure_writable(&state.app, &req.connection_id, "Drop collection").await?; ensure_writable(&state.app, &req.connection_id, "Drop collection").await?;
dbx_core::mongo_ops::mongo_drop_collection_core(&state.app, &req.connection_id, &req.database, &req.collection) dbx_core::mongo_ops::mongo_drop_collection_core(&state.app, &req.connection_id, &req.database, &req.collection)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
@@ -375,7 +375,7 @@ pub async fn rename_collection(
&req.new_name, &req.new_name,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "ok": true }))) Ok(Json(serde_json::json!({ "ok": true })))
} }
@@ -401,7 +401,7 @@ pub async fn find_documents(
), ),
) )
.await?; .await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn find_one( pub async fn find_one(
@@ -422,7 +422,7 @@ pub async fn find_one(
), ),
) )
.await?; .await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn count_documents( pub async fn count_documents(
@@ -459,7 +459,7 @@ pub async fn server_version(
dbx_core::mongo_ops::mongo_server_version_core(&state.app, &req.connection_id, &req.database), dbx_core::mongo_ops::mongo_server_version_core(&state.app, &req.connection_id, &req.database),
) )
.await?; .await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn collection_stats( pub async fn collection_stats(
@@ -480,7 +480,7 @@ pub async fn collection_stats(
), ),
) )
.await?; .await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn aggregate_documents( pub async fn aggregate_documents(
@@ -521,7 +521,7 @@ pub async fn aggregate_documents(
), ),
) )
.await?; .await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn distinct( pub async fn distinct(
@@ -543,7 +543,7 @@ pub async fn distinct(
), ),
) )
.await?; .await?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn create_index( pub async fn create_index(
@@ -563,7 +563,7 @@ pub async fn create_index(
req.options_json.as_deref(), req.options_json.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "name": name }))) Ok(Json(serde_json::json!({ "name": name })))
} }
@@ -584,8 +584,8 @@ pub async fn drop_indexes(
req.single, req.single,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn insert_document( pub async fn insert_document(
@@ -602,7 +602,7 @@ pub async fn insert_document(
None, None,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -621,7 +621,7 @@ pub async fn insert_documents(
&req.docs_json, &req.docs_json,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "affected_rows": result }))) Ok(Json(serde_json::json!({ "affected_rows": result })))
} }
@@ -640,7 +640,7 @@ pub async fn update_document(
req.routing.as_deref(), req.routing.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -667,7 +667,7 @@ pub async fn update_documents(
req.options_json.as_deref(), req.options_json.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "affected_rows": result }))) Ok(Json(serde_json::json!({ "affected_rows": result })))
} }
@@ -689,8 +689,8 @@ pub async fn find_one_and_update(
req.options_json.as_deref(), req.options_json.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn find_one_and_replace( pub async fn find_one_and_replace(
@@ -711,8 +711,8 @@ pub async fn find_one_and_replace(
req.options_json.as_deref(), req.options_json.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn find_one_and_delete( pub async fn find_one_and_delete(
@@ -732,8 +732,8 @@ pub async fn find_one_and_delete(
req.options_json.as_deref(), req.options_json.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn delete_document( pub async fn delete_document(
@@ -750,7 +750,7 @@ pub async fn delete_document(
req.routing.as_deref(), req.routing.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -775,7 +775,7 @@ pub async fn delete_documents(
req.many, req.many,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "affected_rows": result }))) Ok(Json(serde_json::json!({ "affected_rows": result })))
} }
@@ -873,12 +873,12 @@ mod tests {
let revoked = ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Update") let revoked = ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Update")
.await .await
.unwrap_err(); .unwrap_err();
assert!(revoked.0.starts_with("MCP_READ_ONLY:"), "{}", revoked.0); assert!(revoked.message.starts_with("MCP_READ_ONLY:"), "{}", revoked.message);
state.app.storage.save_mcp_global_policy(&writable_policy).await.unwrap(); state.app.storage.save_mcp_global_policy(&writable_policy).await.unwrap();
let empty_filter = let empty_filter =
ensure_find_one_write_policy(&state, &headers, &connection.id, "app", "{}", "Delete").await.unwrap_err(); ensure_find_one_write_policy(&state, &headers, &connection.id, "app", "{}", "Delete").await.unwrap_err();
assert!(empty_filter.0.starts_with("SQL_BLOCKED:"), "{}", empty_filter.0); assert!(empty_filter.message.starts_with("SQL_BLOCKED:"), "{}", empty_filter.message);
state state
.app .app
@@ -893,7 +893,7 @@ mod tests {
ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Update") ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Update")
.await .await
.unwrap_err(); .unwrap_err();
assert!(production.0.starts_with("PRODUCTION_DATABASE_READ_ONLY:"), "{}", production.0); assert!(production.message.starts_with("PRODUCTION_DATABASE_READ_ONLY:"), "{}", production.message);
state.app.storage.save_connections(std::slice::from_ref(&connection)).await.unwrap(); state.app.storage.save_connections(std::slice::from_ref(&connection)).await.unwrap();
state state
@@ -909,7 +909,7 @@ mod tests {
let allowlist = ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Delete") let allowlist = ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Delete")
.await .await
.unwrap_err(); .unwrap_err();
assert!(allowlist.0.starts_with("CONNECTION_OUT_OF_SCOPE:"), "{}", allowlist.0); assert!(allowlist.message.starts_with("CONNECTION_OUT_OF_SCOPE:"), "{}", allowlist.message);
drop(state); drop(state);
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
+65 -57
View File
@@ -298,7 +298,7 @@ pub(crate) struct RawRequestReq {
async fn ensure_writable(app: &dbx_core::connection::AppState, conn_id: &str, action: &str) -> Result<(), AppError> { async fn ensure_writable(app: &dbx_core::connection::AppState, conn_id: &str, action: &str) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, conn_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(app, conn_id).await {
return Err(AppError(format!("Read-only connection '{name}'. {action} is blocked."))); return Err(AppError::from(format!("Read-only connection '{name}'. {action} is blocked.")));
} }
Ok(()) Ok(())
} }
@@ -310,7 +310,7 @@ pub async fn test_connection(
Json(req): Json<ConnReq>, Json(req): Json<ConnReq>,
) -> Result<Json<dbx_core::mq::MqClusterInfo>, AppError> { ) -> Result<Json<dbx_core::mq::MqClusterInfo>, AppError> {
let result = let result =
dbx_core::mq::service::mq_test_connection_core(&state.app, &req.connection_id).await.map_err(AppError)?; dbx_core::mq::service::mq_test_connection_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -318,7 +318,8 @@ pub async fn list_tenants(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<ConnReq>, Json(req): Json<ConnReq>,
) -> Result<Json<Vec<dbx_core::mq::TenantInfo>>, AppError> { ) -> Result<Json<Vec<dbx_core::mq::TenantInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_tenants_core(&state.app, &req.connection_id).await.map_err(AppError)?; let result =
dbx_core::mq::service::mq_list_tenants_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -326,8 +327,9 @@ pub async fn get_tenant(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<TenantReq>, Json(req): Json<TenantReq>,
) -> Result<Json<dbx_core::mq::TenantInfo>, AppError> { ) -> Result<Json<dbx_core::mq::TenantInfo>, AppError> {
let result = let result = dbx_core::mq::service::mq_get_tenant_core(&state.app, &req.connection_id, &req.name)
dbx_core::mq::service::mq_get_tenant_core(&state.app, &req.connection_id, &req.name).await.map_err(AppError)?; .await
.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -338,7 +340,7 @@ pub async fn create_tenant(
ensure_writable(&state.app, &req.connection_id, "Create tenant").await?; ensure_writable(&state.app, &req.connection_id, "Create tenant").await?;
dbx_core::mq::service::mq_create_tenant_core(&state.app, &req.connection_id, &req.name, req.config) dbx_core::mq::service::mq_create_tenant_core(&state.app, &req.connection_id, &req.name, req.config)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -349,7 +351,7 @@ pub async fn update_tenant(
ensure_writable(&state.app, &req.connection_id, "Update tenant").await?; ensure_writable(&state.app, &req.connection_id, "Update tenant").await?;
dbx_core::mq::service::mq_update_tenant_core(&state.app, &req.connection_id, &req.name, req.config) dbx_core::mq::service::mq_update_tenant_core(&state.app, &req.connection_id, &req.name, req.config)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -360,7 +362,7 @@ pub async fn delete_tenant(
ensure_writable(&state.app, &req.connection_id, "Delete tenant").await?; ensure_writable(&state.app, &req.connection_id, "Delete tenant").await?;
dbx_core::mq::service::mq_delete_tenant_core(&state.app, &req.connection_id, &req.name, req.force) dbx_core::mq::service::mq_delete_tenant_core(&state.app, &req.connection_id, &req.name, req.force)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -370,7 +372,7 @@ pub async fn list_namespaces(
) -> Result<Json<Vec<dbx_core::mq::NamespaceInfo>>, AppError> { ) -> Result<Json<Vec<dbx_core::mq::NamespaceInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_namespaces_core(&state.app, &req.connection_id, &req.tenant) let result = dbx_core::mq::service::mq_list_namespaces_core(&state.app, &req.connection_id, &req.tenant)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -381,7 +383,7 @@ pub async fn create_namespace(
ensure_writable(&state.app, &req.connection_id, "Create namespace").await?; ensure_writable(&state.app, &req.connection_id, "Create namespace").await?;
dbx_core::mq::service::mq_create_namespace_core(&state.app, &req.connection_id, req.ns, req.config) dbx_core::mq::service::mq_create_namespace_core(&state.app, &req.connection_id, req.ns, req.config)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -392,7 +394,7 @@ pub async fn delete_namespace(
ensure_writable(&state.app, &req.connection_id, "Delete namespace").await?; ensure_writable(&state.app, &req.connection_id, "Delete namespace").await?;
dbx_core::mq::service::mq_delete_namespace_core(&state.app, &req.connection_id, req.ns, req.force) dbx_core::mq::service::mq_delete_namespace_core(&state.app, &req.connection_id, req.ns, req.force)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -402,7 +404,7 @@ pub async fn get_namespace_policies(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_get_namespace_policies_core(&state.app, &req.connection_id, req.ns) let result = dbx_core::mq::service::mq_get_namespace_policies_core(&state.app, &req.connection_id, req.ns)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -412,7 +414,7 @@ pub async fn list_topics(
) -> Result<Json<Vec<dbx_core::mq::TopicInfo>>, AppError> { ) -> Result<Json<Vec<dbx_core::mq::TopicInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_topics_core(&state.app, &req.connection_id, req.ns, req.opts) let result = dbx_core::mq::service::mq_list_topics_core(&state.app, &req.connection_id, req.ns, req.opts)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -423,7 +425,7 @@ pub async fn create_topic(
ensure_writable(&state.app, &req.connection_id, "Create topic").await?; ensure_writable(&state.app, &req.connection_id, "Create topic").await?;
dbx_core::mq::service::mq_create_topic_core(&state.app, &req.connection_id, req.topic, req.partitions) dbx_core::mq::service::mq_create_topic_core(&state.app, &req.connection_id, req.topic, req.partitions)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -434,7 +436,7 @@ pub async fn delete_topic(
ensure_writable(&state.app, &req.connection_id, "Delete topic").await?; ensure_writable(&state.app, &req.connection_id, "Delete topic").await?;
dbx_core::mq::service::mq_delete_topic_core(&state.app, &req.connection_id, req.topic, req.force) dbx_core::mq::service::mq_delete_topic_core(&state.app, &req.connection_id, req.topic, req.force)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -445,7 +447,7 @@ pub async fn update_partitions(
ensure_writable(&state.app, &req.connection_id, "Update partitions").await?; ensure_writable(&state.app, &req.connection_id, "Update partitions").await?;
dbx_core::mq::service::mq_update_partitions_core(&state.app, &req.connection_id, req.topic, req.partitions) dbx_core::mq::service::mq_update_partitions_core(&state.app, &req.connection_id, req.topic, req.partitions)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -455,7 +457,7 @@ pub async fn get_topic_stats(
) -> Result<Json<dbx_core::mq::TopicStats>, AppError> { ) -> Result<Json<dbx_core::mq::TopicStats>, AppError> {
let result = dbx_core::mq::service::mq_get_topic_stats_core(&state.app, &req.connection_id, req.topic) let result = dbx_core::mq::service::mq_get_topic_stats_core(&state.app, &req.connection_id, req.topic)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -465,7 +467,7 @@ pub async fn get_topic_internal_stats(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_get_topic_internal_stats_core(&state.app, &req.connection_id, req.topic) let result = dbx_core::mq::service::mq_get_topic_internal_stats_core(&state.app, &req.connection_id, req.topic)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -475,7 +477,7 @@ pub async fn list_subscriptions(
) -> Result<Json<Vec<dbx_core::mq::SubscriptionInfo>>, AppError> { ) -> Result<Json<Vec<dbx_core::mq::SubscriptionInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_subscriptions_core(&state.app, &req.connection_id, req.topic) let result = dbx_core::mq::service::mq_list_subscriptions_core(&state.app, &req.connection_id, req.topic)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -486,7 +488,7 @@ pub async fn create_subscription(
ensure_writable(&state.app, &req.connection_id, "Create subscription").await?; ensure_writable(&state.app, &req.connection_id, "Create subscription").await?;
dbx_core::mq::service::mq_create_subscription_core(&state.app, &req.connection_id, req.topic, req.sub, req.pos) dbx_core::mq::service::mq_create_subscription_core(&state.app, &req.connection_id, req.topic, req.sub, req.pos)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -497,7 +499,7 @@ pub async fn delete_subscription(
ensure_writable(&state.app, &req.connection_id, "Delete subscription").await?; ensure_writable(&state.app, &req.connection_id, "Delete subscription").await?;
dbx_core::mq::service::mq_delete_subscription_core(&state.app, &req.connection_id, req.topic, req.sub, req.force) dbx_core::mq::service::mq_delete_subscription_core(&state.app, &req.connection_id, req.topic, req.sub, req.force)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -508,7 +510,7 @@ pub async fn skip_messages(
ensure_writable(&state.app, &req.connection_id, "Skip messages").await?; ensure_writable(&state.app, &req.connection_id, "Skip messages").await?;
dbx_core::mq::service::mq_skip_messages_core(&state.app, &req.connection_id, req.topic, req.sub, req.count) dbx_core::mq::service::mq_skip_messages_core(&state.app, &req.connection_id, req.topic, req.sub, req.count)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -519,7 +521,7 @@ pub async fn reset_cursor(
ensure_writable(&state.app, &req.connection_id, "Reset cursor").await?; ensure_writable(&state.app, &req.connection_id, "Reset cursor").await?;
dbx_core::mq::service::mq_reset_cursor_core(&state.app, &req.connection_id, req.topic, req.sub, req.pos) dbx_core::mq::service::mq_reset_cursor_core(&state.app, &req.connection_id, req.topic, req.sub, req.pos)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -530,7 +532,7 @@ pub async fn clear_backlog(
ensure_writable(&state.app, &req.connection_id, "Clear backlog").await?; ensure_writable(&state.app, &req.connection_id, "Clear backlog").await?;
dbx_core::mq::service::mq_clear_backlog_core(&state.app, &req.connection_id, req.topic, req.sub) dbx_core::mq::service::mq_clear_backlog_core(&state.app, &req.connection_id, req.topic, req.sub)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -540,7 +542,7 @@ pub async fn get_consumer_group_config(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_get_consumer_group_config_core(&state.app, &req.connection_id, req.group_id) let result = dbx_core::mq::service::mq_get_consumer_group_config_core(&state.app, &req.connection_id, req.group_id)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -556,7 +558,7 @@ pub async fn alter_consumer_group_config(
req.config, req.config,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -573,7 +575,7 @@ pub async fn peek_messages(
req.options, req.options,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -590,7 +592,7 @@ pub async fn expire_messages(
req.expire_seconds, req.expire_seconds,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -600,7 +602,7 @@ pub async fn list_producers(
) -> Result<Json<Vec<dbx_core::mq::ProducerInfo>>, AppError> { ) -> Result<Json<Vec<dbx_core::mq::ProducerInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_producers_core(&state.app, &req.connection_id, req.topic) let result = dbx_core::mq::service::mq_list_producers_core(&state.app, &req.connection_id, req.topic)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -610,13 +612,15 @@ pub async fn list_consumers(
) -> Result<Json<Vec<dbx_core::mq::ConsumerInfo>>, AppError> { ) -> Result<Json<Vec<dbx_core::mq::ConsumerInfo>>, AppError> {
let result = dbx_core::mq::service::mq_list_consumers_core(&state.app, &req.connection_id, req.topic, req.sub) let result = dbx_core::mq::service::mq_list_consumers_core(&state.app, &req.connection_id, req.topic, req.sub)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
pub async fn unload_topic(State(state): State<Arc<WebState>>, Json(req): Json<TopicReq>) -> Result<Json<()>, AppError> { pub async fn unload_topic(State(state): State<Arc<WebState>>, Json(req): Json<TopicReq>) -> Result<Json<()>, AppError> {
ensure_writable(&state.app, &req.connection_id, "Unload topic").await?; ensure_writable(&state.app, &req.connection_id, "Unload topic").await?;
dbx_core::mq::service::mq_unload_topic_core(&state.app, &req.connection_id, req.topic).await.map_err(AppError)?; dbx_core::mq::service::mq_unload_topic_core(&state.app, &req.connection_id, req.topic)
.await
.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -627,7 +631,7 @@ pub async fn set_publish_rate(
ensure_writable(&state.app, &req.connection_id, "Set publish rate").await?; ensure_writable(&state.app, &req.connection_id, "Set publish rate").await?;
dbx_core::mq::service::mq_set_publish_rate_core(&state.app, &req.connection_id, req.scope, req.rate) dbx_core::mq::service::mq_set_publish_rate_core(&state.app, &req.connection_id, req.scope, req.rate)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -638,7 +642,7 @@ pub async fn set_dispatch_rate(
ensure_writable(&state.app, &req.connection_id, "Set dispatch rate").await?; ensure_writable(&state.app, &req.connection_id, "Set dispatch rate").await?;
dbx_core::mq::service::mq_set_dispatch_rate_core(&state.app, &req.connection_id, req.scope, req.rate) dbx_core::mq::service::mq_set_dispatch_rate_core(&state.app, &req.connection_id, req.scope, req.rate)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -649,7 +653,7 @@ pub async fn set_subscribe_rate(
ensure_writable(&state.app, &req.connection_id, "Set subscribe rate").await?; ensure_writable(&state.app, &req.connection_id, "Set subscribe rate").await?;
dbx_core::mq::service::mq_set_subscribe_rate_core(&state.app, &req.connection_id, req.scope, req.rate) dbx_core::mq::service::mq_set_subscribe_rate_core(&state.app, &req.connection_id, req.scope, req.rate)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -660,7 +664,7 @@ pub async fn set_backlog_quota(
ensure_writable(&state.app, &req.connection_id, "Set backlog quota").await?; ensure_writable(&state.app, &req.connection_id, "Set backlog quota").await?;
dbx_core::mq::service::mq_set_backlog_quota_core(&state.app, &req.connection_id, req.scope, req.quota) dbx_core::mq::service::mq_set_backlog_quota_core(&state.app, &req.connection_id, req.scope, req.quota)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -671,7 +675,7 @@ pub async fn set_retention(
ensure_writable(&state.app, &req.connection_id, "Set retention").await?; ensure_writable(&state.app, &req.connection_id, "Set retention").await?;
dbx_core::mq::service::mq_set_retention_core(&state.app, &req.connection_id, req.scope, req.retention) dbx_core::mq::service::mq_set_retention_core(&state.app, &req.connection_id, req.scope, req.retention)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -681,7 +685,7 @@ pub async fn get_effective_policies(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_get_effective_policies_core(&state.app, &req.connection_id, req.scope) let result = dbx_core::mq::service::mq_get_effective_policies_core(&state.app, &req.connection_id, req.scope)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -692,7 +696,7 @@ pub async fn grant_permission(
ensure_writable(&state.app, &req.connection_id, "Grant permission").await?; ensure_writable(&state.app, &req.connection_id, "Grant permission").await?;
dbx_core::mq::service::mq_grant_permission_core(&state.app, &req.connection_id, req.scope, req.role, req.actions) dbx_core::mq::service::mq_grant_permission_core(&state.app, &req.connection_id, req.scope, req.role, req.actions)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -703,7 +707,7 @@ pub async fn revoke_permission(
ensure_writable(&state.app, &req.connection_id, "Revoke permission").await?; ensure_writable(&state.app, &req.connection_id, "Revoke permission").await?;
dbx_core::mq::service::mq_revoke_permission_core(&state.app, &req.connection_id, req.scope, req.role) dbx_core::mq::service::mq_revoke_permission_core(&state.app, &req.connection_id, req.scope, req.role)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -713,7 +717,7 @@ pub async fn list_permissions(
) -> Result<Json<dbx_core::mq::PermissionMap>, AppError> { ) -> Result<Json<dbx_core::mq::PermissionMap>, AppError> {
let result = dbx_core::mq::service::mq_list_permissions_core(&state.app, &req.connection_id, req.scope) let result = dbx_core::mq::service::mq_list_permissions_core(&state.app, &req.connection_id, req.scope)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -722,8 +726,9 @@ pub async fn issue_token(
Json(req): Json<IssueTokenReq>, Json(req): Json<IssueTokenReq>,
) -> Result<Json<dbx_core::mq::MqIssuedToken>, AppError> { ) -> Result<Json<dbx_core::mq::MqIssuedToken>, AppError> {
ensure_writable(&state.app, &req.connection_id, "Issue MQ token").await?; ensure_writable(&state.app, &req.connection_id, "Issue MQ token").await?;
let result = let result = dbx_core::mq::service::mq_issue_token_core(&state.app, &req.connection_id, req.req)
dbx_core::mq::service::mq_issue_token_core(&state.app, &req.connection_id, req.req).await.map_err(AppError)?; .await
.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -733,7 +738,7 @@ pub async fn list_token_records(
) -> Result<Json<Vec<dbx_core::mq::MqTokenRecord>>, AppError> { ) -> Result<Json<Vec<dbx_core::mq::MqTokenRecord>>, AppError> {
let result = dbx_core::mq::service::mq_list_token_records_core(&state.app, &req.connection_id, req.subject) let result = dbx_core::mq::service::mq_list_token_records_core(&state.app, &req.connection_id, req.subject)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -743,7 +748,7 @@ pub async fn get_backlog(
) -> Result<Json<dbx_core::mq::BacklogStats>, AppError> { ) -> Result<Json<dbx_core::mq::BacklogStats>, AppError> {
let result = dbx_core::mq::service::mq_get_backlog_core(&state.app, &req.connection_id, req.topic, req.sub) let result = dbx_core::mq::service::mq_get_backlog_core(&state.app, &req.connection_id, req.topic, req.sub)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -757,8 +762,9 @@ pub async fn get_cluster_info(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<ClusterInfoReq>, Json(req): Json<ClusterInfoReq>,
) -> Result<Json<dbx_core::mq::ClusterInfo>, AppError> { ) -> Result<Json<dbx_core::mq::ClusterInfo>, AppError> {
let result = let result = dbx_core::mq::service::mq_get_cluster_info_core(&state.app, &req.connection_id)
dbx_core::mq::service::mq_get_cluster_info_core(&state.app, &req.connection_id).await.map_err(AppError)?; .await
.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -768,7 +774,7 @@ pub async fn get_topic_route(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_get_topic_route_core(&state.app, &req.connection_id, req.topic) let result = dbx_core::mq::service::mq_get_topic_route_core(&state.app, &req.connection_id, req.topic)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -787,7 +793,7 @@ pub async fn alter_topic_config(
ensure_writable(&state.app, &req.connection_id, "Alter topic config").await?; ensure_writable(&state.app, &req.connection_id, "Alter topic config").await?;
dbx_core::mq::service::mq_alter_topic_config_core(&state.app, &req.connection_id, req.topic, req.configs) dbx_core::mq::service::mq_alter_topic_config_core(&state.app, &req.connection_id, req.topic, req.configs)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -798,7 +804,7 @@ pub async fn skip_topic_accumulation(
ensure_writable(&state.app, &req.connection_id, "Skip topic accumulation").await?; ensure_writable(&state.app, &req.connection_id, "Skip topic accumulation").await?;
let result = dbx_core::mq::service::mq_skip_topic_accumulation_core(&state.app, &req.connection_id, req.topic) let result = dbx_core::mq::service::mq_skip_topic_accumulation_core(&state.app, &req.connection_id, req.topic)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -816,7 +822,7 @@ pub async fn view_message(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::mq::service::mq_view_message_core(&state.app, &req.connection_id, req.topic, req.msg_id) let result = dbx_core::mq::service::mq_view_message_core(&state.app, &req.connection_id, req.topic, req.msg_id)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -845,7 +851,7 @@ pub async fn query_messages_by_key(
req.max_num, req.max_num,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -872,7 +878,7 @@ pub async fn query_messages_by_topic(
req.max_num, req.max_num,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -891,7 +897,7 @@ pub async fn query_message_trace(
let result = let result =
dbx_core::mq::service::mq_query_message_trace_core(&state.app, &req.connection_id, req.msg_id, req.trace_topic) dbx_core::mq::service::mq_query_message_trace_core(&state.app, &req.connection_id, req.msg_id, req.trace_topic)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -902,8 +908,9 @@ pub async fn raw_request(
if req.req.is_mutating() { if req.req.is_mutating() {
ensure_writable(&state.app, &req.connection_id, "MQ admin write").await?; ensure_writable(&state.app, &req.connection_id, "MQ admin write").await?;
} }
let result = let result = dbx_core::mq::service::mq_raw_request_core(&state.app, &req.connection_id, req.req)
dbx_core::mq::service::mq_raw_request_core(&state.app, &req.connection_id, req.req).await.map_err(AppError)?; .await
.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -920,7 +927,8 @@ pub async fn send_message(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<SendMessageReq>, Json(req): Json<SendMessageReq>,
) -> Result<Json<dbx_core::mq::SendMessageResponse>, AppError> { ) -> Result<Json<dbx_core::mq::SendMessageResponse>, AppError> {
let result = let result = dbx_core::mq::service::mq_send_message_core(&state.app, &req.connection_id, req.req)
dbx_core::mq::service::mq_send_message_core(&state.app, &req.connection_id, req.req).await.map_err(AppError)?; .await
.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
+19 -17
View File
@@ -100,8 +100,9 @@ pub async fn test_connection(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<ConnReq>, Json(req): Json<ConnReq>,
) -> Result<Json<dbx_core::nacos::NacosConnectionInfo>, AppError> { ) -> Result<Json<dbx_core::nacos::NacosConnectionInfo>, AppError> {
let result = let result = dbx_core::nacos::service::nacos_test_connection_core(&state.app, &req.connection_id)
dbx_core::nacos::service::nacos_test_connection_core(&state.app, &req.connection_id).await.map_err(AppError)?; .await
.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -109,8 +110,9 @@ pub async fn list_namespaces(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<ConnReq>, Json(req): Json<ConnReq>,
) -> Result<Json<Vec<dbx_core::nacos::NacosNamespaceInfo>>, AppError> { ) -> Result<Json<Vec<dbx_core::nacos::NacosNamespaceInfo>>, AppError> {
let result = let result = dbx_core::nacos::service::nacos_list_namespaces_core(&state.app, &req.connection_id)
dbx_core::nacos::service::nacos_list_namespaces_core(&state.app, &req.connection_id).await.map_err(AppError)?; .await
.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -120,7 +122,7 @@ pub async fn create_namespace(
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_create_namespace_core(&state.app, &req.connection_id, req.req) dbx_core::nacos::service::nacos_create_namespace_core(&state.app, &req.connection_id, req.req)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -130,7 +132,7 @@ pub async fn update_namespace(
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_update_namespace_core(&state.app, &req.connection_id, req.req) dbx_core::nacos::service::nacos_update_namespace_core(&state.app, &req.connection_id, req.req)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -140,7 +142,7 @@ pub async fn list_configs(
) -> Result<Json<dbx_core::nacos::NacosConfigList>, AppError> { ) -> Result<Json<dbx_core::nacos::NacosConfigList>, AppError> {
let result = dbx_core::nacos::service::nacos_list_configs_core(&state.app, &req.connection_id, req.query) let result = dbx_core::nacos::service::nacos_list_configs_core(&state.app, &req.connection_id, req.query)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -150,7 +152,7 @@ pub async fn get_config(
) -> Result<Json<dbx_core::nacos::NacosConfigItem>, AppError> { ) -> Result<Json<dbx_core::nacos::NacosConfigItem>, AppError> {
let result = dbx_core::nacos::service::nacos_get_config_core(&state.app, &req.connection_id, req.key) let result = dbx_core::nacos::service::nacos_get_config_core(&state.app, &req.connection_id, req.key)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -160,7 +162,7 @@ pub async fn publish_config(
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_publish_config_core(&state.app, &req.connection_id, req.req) dbx_core::nacos::service::nacos_publish_config_core(&state.app, &req.connection_id, req.req)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -170,7 +172,7 @@ pub async fn delete_config(
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_delete_config_core(&state.app, &req.connection_id, req.key) dbx_core::nacos::service::nacos_delete_config_core(&state.app, &req.connection_id, req.key)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -180,7 +182,7 @@ pub async fn list_config_history(
) -> Result<Json<dbx_core::nacos::NacosConfigHistoryList>, AppError> { ) -> Result<Json<dbx_core::nacos::NacosConfigHistoryList>, AppError> {
let result = dbx_core::nacos::service::nacos_list_config_history_core(&state.app, &req.connection_id, req.query) let result = dbx_core::nacos::service::nacos_list_config_history_core(&state.app, &req.connection_id, req.query)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -190,7 +192,7 @@ pub async fn get_config_history(
) -> Result<Json<dbx_core::nacos::NacosConfigItem>, AppError> { ) -> Result<Json<dbx_core::nacos::NacosConfigItem>, AppError> {
let result = dbx_core::nacos::service::nacos_get_config_history_core(&state.app, &req.connection_id, req.key) let result = dbx_core::nacos::service::nacos_get_config_history_core(&state.app, &req.connection_id, req.key)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -200,7 +202,7 @@ pub async fn rollback_config(
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_rollback_config_core(&state.app, &req.connection_id, req.req) dbx_core::nacos::service::nacos_rollback_config_core(&state.app, &req.connection_id, req.req)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -210,7 +212,7 @@ pub async fn list_services(
) -> Result<Json<dbx_core::nacos::NacosServiceList>, AppError> { ) -> Result<Json<dbx_core::nacos::NacosServiceList>, AppError> {
let result = dbx_core::nacos::service::nacos_list_services_core(&state.app, &req.connection_id, req.query) let result = dbx_core::nacos::service::nacos_list_services_core(&state.app, &req.connection_id, req.query)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -220,7 +222,7 @@ pub async fn list_instances(
) -> Result<Json<Vec<dbx_core::nacos::NacosInstanceInfo>>, AppError> { ) -> Result<Json<Vec<dbx_core::nacos::NacosInstanceInfo>>, AppError> {
let result = dbx_core::nacos::service::nacos_list_instances_core(&state.app, &req.connection_id, req.query) let result = dbx_core::nacos::service::nacos_list_instances_core(&state.app, &req.connection_id, req.query)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -230,7 +232,7 @@ pub async fn update_instance(
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
dbx_core::nacos::service::nacos_update_instance_core(&state.app, &req.connection_id, req.req) dbx_core::nacos::service::nacos_update_instance_core(&state.app, &req.connection_id, req.req)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -240,6 +242,6 @@ pub async fn raw_request(
) -> Result<Json<dbx_core::nacos::NacosRawResponse>, AppError> { ) -> Result<Json<dbx_core::nacos::NacosRawResponse>, AppError> {
let result = dbx_core::nacos::service::nacos_raw_request_core(&state.app, &req.connection_id, req.req) let result = dbx_core::nacos::service::nacos_raw_request_core(&state.app, &req.connection_id, req.req)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
+1 -1
View File
@@ -8,5 +8,5 @@ use crate::error::AppError;
use crate::state::WebState; use crate::state::WebState;
pub async fn list_plugins(State(state): State<Arc<WebState>>) -> Result<Json<Vec<InstalledPlugin>>, AppError> { pub async fn list_plugins(State(state): State<Arc<WebState>>) -> Result<Json<Vec<InstalledPlugin>>, AppError> {
state.app.plugins.list_installed().map(Json).map_err(AppError) state.app.plugins.list_installed().map(Json).map_err(AppError::from)
} }
@@ -0,0 +1,97 @@
use std::sync::Arc;
use axum::extract::{Path, State};
use axum::Json;
use serde::{Deserialize, Serialize};
use dbx_core::prompt_template::PromptTemplate;
use crate::error::AppError;
use crate::state::WebState;
// ---------------------------------------------------------------------------
// Request types
// ---------------------------------------------------------------------------
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SavePromptTemplateRequest {
pub id: String,
pub name: String,
pub content: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SetGlobalInstructionsRequest {
pub content: String,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetGlobalInstructionsResponse {
pub content: String,
}
// ---------------------------------------------------------------------------
// Prompt Templates CRUD
// ---------------------------------------------------------------------------
pub async fn load_prompt_templates(State(state): State<Arc<WebState>>) -> Result<Json<Vec<PromptTemplate>>, AppError> {
let templates = state.app.storage.load_prompt_templates().await.map_err(AppError::from)?;
Ok(Json(templates))
}
pub async fn save_prompt_template(
State(state): State<Arc<WebState>>,
Json(body): Json<SavePromptTemplateRequest>,
) -> Result<Json<PromptTemplate>, AppError> {
let result = state.app.storage.save_prompt_template(&body.id, &body.name, &body.content).await.map_err(|e| {
// Map validation errors to 400
if e.contains("too long") || e.contains("cannot be empty") || e.contains("duplicate") {
AppError::bad_request(e)
} else {
AppError::from(e)
}
})?;
Ok(Json(result))
}
pub async fn delete_prompt_template(
State(state): State<Arc<WebState>>,
Path(id): Path<String>,
) -> Result<Json<()>, AppError> {
state.app.storage.delete_prompt_template(&id).await.map_err(|e| {
if e.contains("not found") {
AppError::not_found(e)
} else {
AppError::from(e)
}
})?;
Ok(Json(()))
}
// ---------------------------------------------------------------------------
// Global Custom Instructions
// ---------------------------------------------------------------------------
pub async fn get_global_instructions(
State(state): State<Arc<WebState>>,
) -> Result<Json<GetGlobalInstructionsResponse>, AppError> {
let content = state.app.storage.load_ai_global_custom_instructions().await.map_err(AppError::from)?;
Ok(Json(GetGlobalInstructionsResponse { content }))
}
pub async fn set_global_instructions(
State(state): State<Arc<WebState>>,
Json(body): Json<SetGlobalInstructionsRequest>,
) -> Result<Json<()>, AppError> {
state.app.storage.save_ai_global_custom_instructions(&body.content).await.map_err(|e| {
if e.contains("too long") {
AppError::bad_request(e)
} else {
AppError::from(e)
}
})?;
Ok(Json(()))
}
+24 -22
View File
@@ -353,7 +353,7 @@ pub async fn execute_query(
}, },
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
drop(registered); drop(registered);
Ok(Json(result)) Ok(Json(result))
@@ -396,7 +396,7 @@ pub async fn execute_multi(
}, },
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
drop(registered); drop(registered);
Ok(Json(result)) Ok(Json(result))
@@ -420,7 +420,7 @@ pub async fn execute_batch(
req.timeout_secs, req.timeout_secs,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -445,7 +445,7 @@ pub async fn close_query_session(
req.client_session_id.as_deref(), req.client_session_id.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!(closed))) Ok(Json(serde_json::json!(closed)))
} }
@@ -459,7 +459,7 @@ pub async fn close_client_connection_session(
.app .app
.close_client_session_pool(&req.connection_id, database, &req.client_session_id) .close_client_session_pool(&req.connection_id, database, &req.client_session_id)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!(closed))) Ok(Json(serde_json::json!(closed)))
} }
@@ -485,7 +485,7 @@ pub async fn execute_script(
None, None,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -503,7 +503,7 @@ pub async fn execute_in_transaction(
req.schema.as_deref(), req.schema.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -511,7 +511,7 @@ pub async fn execute_in_transaction(
pub async fn analyze_sql_references( pub async fn analyze_sql_references(
Json(req): Json<AnalyzeSqlReferencesRequest>, Json(req): Json<AnalyzeSqlReferencesRequest>,
) -> Result<Json<dbx_core::sql_analysis::SqlReferenceAnalysis>, AppError> { ) -> Result<Json<dbx_core::sql_analysis::SqlReferenceAnalysis>, AppError> {
dbx_core::sql_analysis::analyze_sql_references(&req.sql, req.dialect.as_deref()).map(Json).map_err(AppError) dbx_core::sql_analysis::analyze_sql_references(&req.sql, req.dialect.as_deref()).map(Json).map_err(AppError::from)
} }
pub async fn find_statement_at_cursor(Json(req): Json<FindStatementAtCursorRequest>) -> Json<String> { pub async fn find_statement_at_cursor(Json(req): Json<FindStatementAtCursorRequest>) -> Json<String> {
@@ -571,7 +571,7 @@ pub async fn get_explain_info(
req.mode.as_deref(), req.mode.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(plan)) Ok(Json(plan))
} }
@@ -600,13 +600,13 @@ pub async fn build_search_result_where(Json(req): Json<BuildSearchResultWhereReq
} }
pub async fn build_rename_object_sql(Json(req): Json<BuildRenameObjectSqlRequest>) -> Result<Json<String>, AppError> { pub async fn build_rename_object_sql(Json(req): Json<BuildRenameObjectSqlRequest>) -> Result<Json<String>, AppError> {
dbx_core::db_admin_sql::build_rename_object_sql(req.options).map(Json).map_err(AppError) dbx_core::db_admin_sql::build_rename_object_sql(req.options).map(Json).map_err(AppError::from)
} }
pub async fn build_create_database_sql( pub async fn build_create_database_sql(
Json(req): Json<BuildCreateDatabaseSqlRequest>, Json(req): Json<BuildCreateDatabaseSqlRequest>,
) -> Result<Json<String>, AppError> { ) -> Result<Json<String>, AppError> {
dbx_core::db_admin_sql::build_create_database_sql(req.options).map(Json).map_err(AppError) dbx_core::db_admin_sql::build_create_database_sql(req.options).map(Json).map_err(AppError::from)
} }
#[cfg(feature = "duckdb-bundled")] #[cfg(feature = "duckdb-bundled")]
@@ -629,7 +629,7 @@ pub async fn build_drop_table_sql(Json(req): Json<BuildTableAdminSqlRequest>) ->
pub async fn build_drop_table_child_object_sql( pub async fn build_drop_table_child_object_sql(
Json(req): Json<BuildDropTableChildObjectSqlRequest>, Json(req): Json<BuildDropTableChildObjectSqlRequest>,
) -> Result<Json<String>, AppError> { ) -> Result<Json<String>, AppError> {
dbx_core::db_admin_sql::build_drop_table_child_object_sql(req.options).map(Json).map_err(AppError) dbx_core::db_admin_sql::build_drop_table_child_object_sql(req.options).map(Json).map_err(AppError::from)
} }
pub async fn build_empty_table_sql(Json(req): Json<BuildTableAdminSqlRequest>) -> Json<String> { pub async fn build_empty_table_sql(Json(req): Json<BuildTableAdminSqlRequest>) -> Json<String> {
@@ -645,13 +645,13 @@ pub async fn build_drop_database_sql(Json(req): Json<BuildDatabaseNameSqlRequest
} }
pub async fn build_create_schema_sql(Json(req): Json<BuildSchemaNameSqlRequest>) -> Result<Json<String>, AppError> { pub async fn build_create_schema_sql(Json(req): Json<BuildSchemaNameSqlRequest>) -> Result<Json<String>, AppError> {
dbx_core::db_admin_sql::build_create_schema_sql(req.options).map(Json).map_err(AppError) dbx_core::db_admin_sql::build_create_schema_sql(req.options).map(Json).map_err(AppError::from)
} }
pub async fn build_update_database_properties_sql( pub async fn build_update_database_properties_sql(
Json(req): Json<BuildDatabasePropertyEditSqlRequest>, Json(req): Json<BuildDatabasePropertyEditSqlRequest>,
) -> Result<Json<String>, AppError> { ) -> Result<Json<String>, AppError> {
dbx_core::db_admin_sql::build_update_database_properties_sql(req.options).map(Json).map_err(AppError) dbx_core::db_admin_sql::build_update_database_properties_sql(req.options).map(Json).map_err(AppError::from)
} }
pub async fn build_drop_schema_sql(Json(req): Json<BuildSchemaNameSqlRequest>) -> Json<String> { pub async fn build_drop_schema_sql(Json(req): Json<BuildSchemaNameSqlRequest>) -> Json<String> {
@@ -671,13 +671,13 @@ pub async fn build_copy_table_data_sql(Json(req): Json<BuildCopyTableDataSqlRequ
pub async fn build_executable_object_source_statements( pub async fn build_executable_object_source_statements(
Json(req): Json<BuildExecutableObjectSourceRequest>, Json(req): Json<BuildExecutableObjectSourceRequest>,
) -> Result<Json<Vec<String>>, AppError> { ) -> Result<Json<Vec<String>>, AppError> {
dbx_core::object_source_sql::build_executable_object_source_statements(req.input).map(Json).map_err(AppError) dbx_core::object_source_sql::build_executable_object_source_statements(req.input).map(Json).map_err(AppError::from)
} }
pub async fn build_executable_object_source_sql( pub async fn build_executable_object_source_sql(
Json(req): Json<BuildExecutableObjectSourceRequest>, Json(req): Json<BuildExecutableObjectSourceRequest>,
) -> Result<Json<String>, AppError> { ) -> Result<Json<String>, AppError> {
dbx_core::object_source_sql::build_executable_object_source_sql(req.input).map(Json).map_err(AppError) dbx_core::object_source_sql::build_executable_object_source_sql(req.input).map(Json).map_err(AppError::from)
} }
pub async fn build_editable_object_source(Json(req): Json<BuildExecutableObjectSourceRequest>) -> Json<String> { pub async fn build_editable_object_source(Json(req): Json<BuildExecutableObjectSourceRequest>) -> Json<String> {
@@ -687,7 +687,9 @@ pub async fn build_editable_object_source(Json(req): Json<BuildExecutableObjectS
pub async fn build_routine_rename_object_source_statements( pub async fn build_routine_rename_object_source_statements(
Json(req): Json<BuildRoutineRenameObjectSourceRequest>, Json(req): Json<BuildRoutineRenameObjectSourceRequest>,
) -> Result<Json<Vec<String>>, AppError> { ) -> Result<Json<Vec<String>>, AppError> {
dbx_core::object_source_sql::build_routine_rename_object_source_statements(req.input).map(Json).map_err(AppError) dbx_core::object_source_sql::build_routine_rename_object_source_statements(req.input)
.map(Json)
.map_err(AppError::from)
} }
pub async fn build_view_ddl_sql(Json(req): Json<BuildViewDdlRequest>) -> Json<String> { pub async fn build_view_ddl_sql(Json(req): Json<BuildViewDdlRequest>) -> Json<String> {
@@ -712,7 +714,7 @@ pub async fn preview_sqlite_table_structure_change(
) )
.await .await
.map(Json) .map(Json)
.map_err(AppError) .map_err(AppError::from)
} }
pub async fn apply_sqlite_table_structure_change( pub async fn apply_sqlite_table_structure_change(
@@ -728,7 +730,7 @@ pub async fn apply_sqlite_table_structure_change(
) )
.await .await
.map(Json) .map(Json)
.map_err(AppError) .map_err(AppError::from)
} }
pub async fn build_create_table_sql( pub async fn build_create_table_sql(
@@ -802,11 +804,11 @@ pub async fn build_hive_table_properties_sql(Json(req): Json<BuildHiveTablePrope
pub async fn build_export_insert_statements( pub async fn build_export_insert_statements(
Json(req): Json<BuildExportInsertStatementsRequest>, Json(req): Json<BuildExportInsertStatementsRequest>,
) -> Result<Json<Vec<String>>, AppError> { ) -> Result<Json<Vec<String>>, AppError> {
dbx_core::database_export::build_export_insert_statements(req.options).map(Json).map_err(AppError) dbx_core::database_export::build_export_insert_statements(req.options).map(Json).map_err(AppError::from)
} }
pub async fn build_export_sql_insert(Json(req): Json<BuildExportSqlInsertRequest>) -> Result<Json<String>, AppError> { pub async fn build_export_sql_insert(Json(req): Json<BuildExportSqlInsertRequest>) -> Result<Json<String>, AppError> {
dbx_core::database_export::build_export_sql_insert(req.options).map(Json).map_err(AppError) dbx_core::database_export::build_export_sql_insert(req.options).map(Json).map_err(AppError::from)
} }
pub async fn build_database_sql_export( pub async fn build_database_sql_export(
@@ -841,5 +843,5 @@ pub async fn build_database_sql_export(
} }
} }
} }
dbx_core::database_export::build_database_sql_export(options).map(Json).map_err(AppError) dbx_core::database_export::build_database_sql_export(options).map(Json).map_err(AppError::from)
} }
@@ -43,10 +43,10 @@ pub async fn start_query_result_export(
let ext = match req.format.as_str() { let ext = match req.format.as_str() {
"csv" => "csv", "csv" => "csv",
"xlsx" => "xlsx", "xlsx" => "xlsx",
_ => return Err(AppError(format!("Unsupported query result export format: {}", req.format))), _ => return Err(AppError::from(format!("Unsupported query result export format: {}", req.format))),
}; };
let tmp_dir = state.data_dir.join("tmp"); let tmp_dir = state.data_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?; std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
let tmp_file = tmp_dir.join(format!("query_result_export_{export_id}.{ext}")); let tmp_file = tmp_dir.join(format!("query_result_export_{export_id}.{ext}"));
let file_path = tmp_file.to_string_lossy().to_string(); let file_path = tmp_file.to_string_lossy().to_string();
req.file_path = file_path.clone(); req.file_path = file_path.clone();
@@ -146,15 +146,15 @@ pub async fn query_result_export_download(
.write() .write()
.await .await
.remove(&export_id) .remove(&export_id)
.ok_or_else(|| AppError("Export file not found".to_string()))?; .ok_or_else(|| AppError::from("Export file not found".to_string()))?;
let data = tokio::fs::read(&file_path).await.map_err(|e| AppError(e.to_string()))?; let data = tokio::fs::read(&file_path).await.map_err(|e| AppError::from(e.to_string()))?;
let _ = tokio::fs::remove_file(&file_path).await; let _ = tokio::fs::remove_file(&file_path).await;
let (content_type, file_ext) = match format.as_str() { let (content_type, file_ext) = match format.as_str() {
"csv" => ("text/csv; charset=utf-8", "csv"), "csv" => ("text/csv; charset=utf-8", "csv"),
"xlsx" => ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"), "xlsx" => ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"),
_ => return Err(AppError(format!("Unknown format: {format}"))), _ => return Err(AppError::from(format!("Unknown format: {format}"))),
}; };
let filename = format!("query_result_export_{export_id}.{file_ext}"); let filename = format!("query_result_export_{export_id}.{file_ext}");
@@ -164,5 +164,5 @@ pub async fn query_result_export_download(
.header(header::CONTENT_TYPE, content_type) .header(header::CONTENT_TYPE, content_type)
.header(header::CONTENT_DISPOSITION, format!("attachment; filename=\"{filename}\"")) .header(header::CONTENT_DISPOSITION, format!("attachment; filename=\"{filename}\""))
.body(Body::from(data)) .body(Body::from(data))
.map_err(|e| AppError(e.to_string())) .map_err(|e| AppError::from(e.to_string()))
} }
+39 -38
View File
@@ -15,7 +15,7 @@ async fn ensure_writable(
action: &str, action: &str,
) -> Result<(), AppError> { ) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
return Err(AppError(format!( return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.", "Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
name, action name, action
))); )));
@@ -215,8 +215,8 @@ pub async fn list_databases(
Json(req): Json<RedisConnectionRequest>, Json(req): Json<RedisConnectionRequest>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = let result =
dbx_core::redis_ops::redis_list_databases_core(&state.app, &req.connection_id).await.map_err(AppError)?; dbx_core::redis_ops::redis_list_databases_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn scan_keys( pub async fn scan_keys(
@@ -232,8 +232,8 @@ pub async fn scan_keys(
req.count, req.count,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn scan_keys_batch( pub async fn scan_keys_batch(
@@ -251,8 +251,8 @@ pub async fn scan_keys_batch(
req.include_types.unwrap_or(true), req.include_types.unwrap_or(true),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn scan_values( pub async fn scan_values(
@@ -270,8 +270,8 @@ pub async fn scan_values(
req.count, req.count,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn get_value( pub async fn get_value(
@@ -280,8 +280,8 @@ pub async fn get_value(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::redis_ops::redis_get_value_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw) let result = dbx_core::redis_ops::redis_get_value_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn load_more( pub async fn load_more(
@@ -299,8 +299,8 @@ pub async fn load_more(
req.filter.as_deref(), req.filter.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn set_string( pub async fn set_string(
@@ -317,7 +317,7 @@ pub async fn set_string(
req.ttl, req.ttl,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -328,7 +328,7 @@ pub async fn delete_key(
ensure_writable(&state.app, &req.connection_id, "Delete key").await?; ensure_writable(&state.app, &req.connection_id, "Delete key").await?;
dbx_core::redis_ops::redis_delete_key_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw) dbx_core::redis_ops::redis_delete_key_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -348,7 +348,7 @@ pub async fn hash_set(
req.ttl, req.ttl,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -359,7 +359,7 @@ pub async fn hash_del(
ensure_writable(&state.app, &req.connection_id, "HDEL").await?; ensure_writable(&state.app, &req.connection_id, "HDEL").await?;
dbx_core::redis_ops::redis_hash_del_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, &req.field) dbx_core::redis_ops::redis_hash_del_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, &req.field)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -378,7 +378,7 @@ pub async fn list_push(
req.ttl, req.ttl,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -391,7 +391,7 @@ pub async fn list_set(
let value = req.value.as_deref().unwrap_or(""); let value = req.value.as_deref().unwrap_or("");
dbx_core::redis_ops::redis_list_set_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, index, value) dbx_core::redis_ops::redis_list_set_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, index, value)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -403,7 +403,7 @@ pub async fn list_remove(
let index = req.index.unwrap_or(0); let index = req.index.unwrap_or(0);
dbx_core::redis_ops::redis_list_remove_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, index) dbx_core::redis_ops::redis_list_remove_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, index)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -421,7 +421,7 @@ pub async fn set_add(
req.ttl, req.ttl,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -432,7 +432,7 @@ pub async fn set_remove(
ensure_writable(&state.app, &req.connection_id, "SREM").await?; ensure_writable(&state.app, &req.connection_id, "SREM").await?;
dbx_core::redis_ops::redis_set_remove_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, &req.member) dbx_core::redis_ops::redis_set_remove_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw, &req.member)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -447,7 +447,7 @@ pub async fn zadd(State(state): State<Arc<WebState>>, Json(req): Json<RedisZaddR
req.ttl, req.ttl,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -465,7 +465,7 @@ pub async fn stream_add(
req.ttl, req.ttl,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -482,7 +482,7 @@ pub async fn json_set(
req.ttl, req.ttl,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -492,7 +492,7 @@ pub async fn check_json_module(
) -> Result<Json<bool>, AppError> { ) -> Result<Json<bool>, AppError> {
let result = dbx_core::redis_ops::redis_check_json_module_in_db_core(&state.app, &req.connection_id, req.db) let result = dbx_core::redis_ops::redis_check_json_module_in_db_core(&state.app, &req.connection_id, req.db)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -504,7 +504,7 @@ pub async fn delete_keys(
let result = let result =
dbx_core::redis_ops::redis_delete_keys_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raws) dbx_core::redis_ops::redis_delete_keys_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raws)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -513,7 +513,7 @@ pub async fn flush_db(
Json(req): Json<RedisDbRequest>, Json(req): Json<RedisDbRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
ensure_writable(&state.app, &req.connection_id, "FLUSHDB").await?; ensure_writable(&state.app, &req.connection_id, "FLUSHDB").await?;
dbx_core::redis_ops::redis_flush_db_core(&state.app, &req.connection_id, req.db).await.map_err(AppError)?; dbx_core::redis_ops::redis_flush_db_core(&state.app, &req.connection_id, req.db).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -524,7 +524,7 @@ pub async fn execute_command(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?; super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
let argv = dbx_core::db::redis_driver::parse_command_argv(&req.command) let argv = dbx_core::db::redis_driver::parse_command_argv(&req.command)
.map_err(|error| AppError(format!("Invalid Redis command: {error}")))?; .map_err(|error| AppError::from(format!("Invalid Redis command: {error}")))?;
let cmd_name = argv[0].to_ascii_uppercase(); let cmd_name = argv[0].to_ascii_uppercase();
let safety = dbx_core::db::redis_driver::classify_command(&cmd_name); let safety = dbx_core::db::redis_driver::classify_command(&cmd_name);
let is_mcp_request = super::mcp_policy::is_mcp_request(&headers); let is_mcp_request = super::mcp_policy::is_mcp_request(&headers);
@@ -554,7 +554,7 @@ pub async fn execute_command(
// In read-only mode, only allow safe read commands // In read-only mode, only allow safe read commands
if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.connection_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.connection_id).await {
if safety != dbx_core::db::redis_driver::RedisCommandSafety::Allowed { if safety != dbx_core::db::redis_driver::RedisCommandSafety::Allowed {
return Err(AppError(format!( return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. Command '{}' blocked.", "Read-only mode: connection '{}' has read-only protection enabled. Command '{}' blocked.",
name, cmd_name name, cmd_name
))); )));
@@ -568,8 +568,8 @@ pub async fn execute_command(
if is_mcp_request { centrally_approved_high_risk } else { req.skip_safety_check.unwrap_or(false) }, if is_mcp_request { centrally_approved_high_risk } else { req.skip_safety_check.unwrap_or(false) },
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn publish_message( pub async fn publish_message(
@@ -580,7 +580,7 @@ pub async fn publish_message(
let count = let count =
dbx_core::redis_ops::redis_publish_core(&state.app, &req.connection_id, req.db, &req.channel, &req.message) dbx_core::redis_ops::redis_publish_core(&state.app, &req.connection_id, req.db, &req.channel, &req.message)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::json!({ "subscribers": count }))) Ok(Json(serde_json::json!({ "subscribers": count })))
} }
@@ -596,15 +596,16 @@ pub async fn slowlog_get(
req.node_port, req.node_port,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn cluster_master_nodes( pub async fn cluster_master_nodes(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<ClusterNodesRequest>, Json(req): Json<ClusterNodesRequest>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = let result = dbx_core::redis_ops::redis_cluster_master_nodes_core(&state.app, &req.connection_id)
dbx_core::redis_ops::redis_cluster_master_nodes_core(&state.app, &req.connection_id).await.map_err(AppError)?; .await
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
+6 -6
View File
@@ -8,7 +8,7 @@ use crate::error::AppError;
use crate::state::WebState; use crate::state::WebState;
pub async fn load_saved_sql_library(State(state): State<Arc<WebState>>) -> Result<Json<SavedSqlLibrary>, AppError> { pub async fn load_saved_sql_library(State(state): State<Arc<WebState>>) -> Result<Json<SavedSqlLibrary>, AppError> {
let library = state.app.storage.load_saved_sql_library_summary().await.map_err(AppError)?; let library = state.app.storage.load_saved_sql_library_summary().await.map_err(AppError::from)?;
Ok(Json(library)) Ok(Json(library))
} }
@@ -16,7 +16,7 @@ pub async fn load_saved_sql_file(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Path(id): Path<String>, Path(id): Path<String>,
) -> Result<Json<Option<SavedSqlFile>>, AppError> { ) -> Result<Json<Option<SavedSqlFile>>, AppError> {
let file = state.app.storage.load_saved_sql_file(&id).await.map_err(AppError)?; let file = state.app.storage.load_saved_sql_file(&id).await.map_err(AppError::from)?;
Ok(Json(file)) Ok(Json(file))
} }
@@ -24,7 +24,7 @@ pub async fn save_saved_sql_folder(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(folder): Json<SavedSqlFolder>, Json(folder): Json<SavedSqlFolder>,
) -> Result<Json<SavedSqlFolder>, AppError> { ) -> Result<Json<SavedSqlFolder>, AppError> {
state.app.storage.save_saved_sql_folder(&folder).await.map_err(AppError)?; state.app.storage.save_saved_sql_folder(&folder).await.map_err(AppError::from)?;
Ok(Json(folder)) Ok(Json(folder))
} }
@@ -32,7 +32,7 @@ pub async fn delete_saved_sql_folder(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Path(id): Path<String>, Path(id): Path<String>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.delete_saved_sql_folder(&id).await.map_err(AppError)?; state.app.storage.delete_saved_sql_folder(&id).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -40,7 +40,7 @@ pub async fn save_saved_sql_file(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(file): Json<SavedSqlFile>, Json(file): Json<SavedSqlFile>,
) -> Result<Json<SavedSqlFile>, AppError> { ) -> Result<Json<SavedSqlFile>, AppError> {
state.app.storage.save_saved_sql_file(&file).await.map_err(AppError)?; state.app.storage.save_saved_sql_file(&file).await.map_err(AppError::from)?;
Ok(Json(file)) Ok(Json(file))
} }
@@ -48,6 +48,6 @@ pub async fn delete_saved_sql_file(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Path(id): Path<String>, Path(id): Path<String>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.delete_saved_sql_file(&id).await.map_err(AppError)?; state.app.storage.delete_saved_sql_file(&id).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
+65 -60
View File
@@ -29,8 +29,8 @@ pub async fn list_databases(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Query(q): Query<SchemaQuery>, Query(q): Query<SchemaQuery>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::schema::list_databases_core(&state.app, &q.connection_id).await.map_err(AppError)?; let result = dbx_core::schema::list_databases_core(&state.app, &q.connection_id).await.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
/// Resolve a non-internal catalog for dispatch to the Doris multi-catalog path. /// Resolve a non-internal catalog for dispatch to the Doris multi-catalog path.
@@ -42,8 +42,9 @@ pub async fn list_doris_catalogs(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Query(q): Query<SchemaQuery>, Query(q): Query<SchemaQuery>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = dbx_core::schema::list_doris_catalogs_core(&state.app, &q.connection_id).await.map_err(AppError)?; let result =
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) dbx_core::schema::list_doris_catalogs_core(&state.app, &q.connection_id).await.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_doris_catalog_databases( pub async fn list_doris_catalog_databases(
@@ -53,17 +54,18 @@ pub async fn list_doris_catalog_databases(
let catalog = q.catalog.as_deref().unwrap_or("internal"); let catalog = q.catalog.as_deref().unwrap_or("internal");
let result = dbx_core::schema::list_doris_catalog_databases_core(&state.app, &q.connection_id, catalog) let result = dbx_core::schema::list_doris_catalog_databases_core(&state.app, &q.connection_id, catalog)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_sqlserver_linked_servers( pub async fn list_sqlserver_linked_servers(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Query(q): Query<SchemaQuery>, Query(q): Query<SchemaQuery>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let result = let result = dbx_core::schema::list_sqlserver_linked_servers_core(&state.app, &q.connection_id)
dbx_core::schema::list_sqlserver_linked_servers_core(&state.app, &q.connection_id).await.map_err(AppError)?; .await
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_sqlserver_linked_server_catalogs( pub async fn list_sqlserver_linked_server_catalogs(
@@ -73,8 +75,8 @@ pub async fn list_sqlserver_linked_server_catalogs(
let server = q.server.as_deref().unwrap_or(""); let server = q.server.as_deref().unwrap_or("");
let result = dbx_core::schema::list_sqlserver_linked_server_catalogs_core(&state.app, &q.connection_id, server) let result = dbx_core::schema::list_sqlserver_linked_server_catalogs_core(&state.app, &q.connection_id, server)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_sqlserver_linked_server_schemas( pub async fn list_sqlserver_linked_server_schemas(
@@ -86,7 +88,7 @@ pub async fn list_sqlserver_linked_server_schemas(
let result = let result =
dbx_core::schema::list_sqlserver_linked_server_schemas_core(&state.app, &q.connection_id, server, catalog) dbx_core::schema::list_sqlserver_linked_server_schemas_core(&state.app, &q.connection_id, server, catalog)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -108,8 +110,8 @@ pub async fn list_sqlserver_linked_server_tables(
q.offset, q.offset,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn get_sqlserver_column_metadata( pub async fn get_sqlserver_column_metadata(
@@ -122,8 +124,8 @@ pub async fn get_sqlserver_column_metadata(
let result = let result =
dbx_core::schema::get_sqlserver_column_metadata_core(&state.app, &q.connection_id, database, schema, table) dbx_core::schema::get_sqlserver_column_metadata_core(&state.app, &q.connection_id, database, schema, table)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_schemas( pub async fn list_schemas(
@@ -138,7 +140,7 @@ pub async fn list_schemas(
q.apply_visible_filter.unwrap_or(false), q.apply_visible_filter.unwrap_or(false),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -163,7 +165,7 @@ pub async fn list_tables(
object_types.as_deref(), object_types.as_deref(),
) )
.await .await
.map_err(AppError)? .map_err(AppError::from)?
} else { } else {
dbx_core::schema::list_tables_core( dbx_core::schema::list_tables_core(
&state.app, &state.app,
@@ -176,9 +178,9 @@ pub async fn list_tables(
object_types.as_deref(), object_types.as_deref(),
) )
.await .await
.map_err(AppError)? .map_err(AppError::from)?
}; };
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_objects( pub async fn list_objects(
@@ -202,7 +204,7 @@ pub async fn list_objects(
object_types.as_deref(), object_types.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
tables tables
.into_iter() .into_iter()
.map(|table| dbx_core::db::ObjectInfo { .map(|table| dbx_core::db::ObjectInfo {
@@ -230,9 +232,9 @@ pub async fn list_objects(
object_types.as_deref(), object_types.as_deref(),
) )
.await .await
.map_err(AppError)? .map_err(AppError::from)?
}; };
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_object_statistics( pub async fn list_object_statistics(
@@ -243,8 +245,8 @@ pub async fn list_object_statistics(
let schema = q.schema.as_deref().unwrap_or(""); let schema = q.schema.as_deref().unwrap_or("");
let result = dbx_core::schema::list_object_statistics_core(&state.app, &q.connection_id, database, schema) let result = dbx_core::schema::list_object_statistics_core(&state.app, &q.connection_id, database, schema)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_completion_objects( pub async fn list_completion_objects(
@@ -255,15 +257,16 @@ pub async fn list_completion_objects(
let schema = q.schema.as_deref().unwrap_or(""); let schema = q.schema.as_deref().unwrap_or("");
let result = dbx_core::schema::list_completion_objects_core(&state.app, &q.connection_id, database, schema) let result = dbx_core::schema::list_completion_objects_core(&state.app, &q.connection_id, database, schema)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn completion_assistant_search( pub async fn completion_assistant_search(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(request): Json<dbx_core::db::CompletionAssistantRequest>, Json(request): Json<dbx_core::db::CompletionAssistantRequest>,
) -> Result<Json<dbx_core::db::CompletionAssistantResponse>, AppError> { ) -> Result<Json<dbx_core::db::CompletionAssistantResponse>, AppError> {
let result = dbx_core::schema::completion_assistant_search_core(&state.app, request).await.map_err(AppError)?; let result =
dbx_core::schema::completion_assistant_search_core(&state.app, request).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -274,7 +277,7 @@ pub async fn get_object_source(
let database = q.database.as_deref().unwrap_or(""); let database = q.database.as_deref().unwrap_or("");
let schema = q.schema.as_deref().unwrap_or(""); let schema = q.schema.as_deref().unwrap_or("");
let name = q.table.as_deref().unwrap_or(""); let name = q.table.as_deref().unwrap_or("");
let object_type = q.object_type.ok_or_else(|| AppError("Missing object_type".to_string()))?; let object_type = q.object_type.ok_or_else(|| AppError::from("Missing object_type".to_string()))?;
let result = dbx_core::schema::get_object_source_core( let result = dbx_core::schema::get_object_source_core(
&state.app, &state.app,
&q.connection_id, &q.connection_id,
@@ -285,7 +288,7 @@ pub async fn get_object_source(
q.signature.as_deref(), q.signature.as_deref(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -299,7 +302,7 @@ pub async fn list_columns(
let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await { let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await {
dbx_core::schema::get_doris_catalog_columns_core(&state.app, &q.connection_id, &catalog, database, table) dbx_core::schema::get_doris_catalog_columns_core(&state.app, &q.connection_id, &catalog, database, table)
.await .await
.map_err(AppError)? .map_err(AppError::from)?
} else { } else {
dbx_core::schema::get_columns_core_for_session( dbx_core::schema::get_columns_core_for_session(
&state.app, &state.app,
@@ -310,9 +313,9 @@ pub async fn list_columns(
q.client_session_id.as_deref(), q.client_session_id.as_deref(),
) )
.await .await
.map_err(AppError)? .map_err(AppError::from)?
}; };
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_data_types( pub async fn list_data_types(
@@ -321,8 +324,8 @@ pub async fn list_data_types(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let database = q.database.as_deref().unwrap_or(""); let database = q.database.as_deref().unwrap_or("");
let result = let result =
dbx_core::schema::list_data_types_core(&state.app, &q.connection_id, database).await.map_err(AppError)?; dbx_core::schema::list_data_types_core(&state.app, &q.connection_id, database).await.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_indexes( pub async fn list_indexes(
@@ -335,13 +338,13 @@ pub async fn list_indexes(
let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await { let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await {
dbx_core::schema::list_doris_catalog_indexes_core(&state.app, &q.connection_id, &catalog, database, table) dbx_core::schema::list_doris_catalog_indexes_core(&state.app, &q.connection_id, &catalog, database, table)
.await .await
.map_err(AppError)? .map_err(AppError::from)?
} else { } else {
dbx_core::schema::list_indexes_core(&state.app, &q.connection_id, database, schema, table) dbx_core::schema::list_indexes_core(&state.app, &q.connection_id, database, schema, table)
.await .await
.map_err(AppError)? .map_err(AppError::from)?
}; };
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_foreign_keys( pub async fn list_foreign_keys(
@@ -354,13 +357,13 @@ pub async fn list_foreign_keys(
let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await { let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await {
dbx_core::schema::list_doris_catalog_foreign_keys_core(&state.app, &q.connection_id, &catalog, database, table) dbx_core::schema::list_doris_catalog_foreign_keys_core(&state.app, &q.connection_id, &catalog, database, table)
.await .await
.map_err(AppError)? .map_err(AppError::from)?
} else { } else {
dbx_core::schema::list_foreign_keys_core(&state.app, &q.connection_id, database, schema, table) dbx_core::schema::list_foreign_keys_core(&state.app, &q.connection_id, database, schema, table)
.await .await
.map_err(AppError)? .map_err(AppError::from)?
}; };
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_triggers( pub async fn list_triggers(
@@ -373,13 +376,13 @@ pub async fn list_triggers(
let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await { let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await {
dbx_core::schema::list_doris_catalog_triggers_core(&state.app, &q.connection_id, &catalog, database, table) dbx_core::schema::list_doris_catalog_triggers_core(&state.app, &q.connection_id, &catalog, database, table)
.await .await
.map_err(AppError)? .map_err(AppError::from)?
} else { } else {
dbx_core::schema::list_triggers_core(&state.app, &q.connection_id, database, schema, table) dbx_core::schema::list_triggers_core(&state.app, &q.connection_id, database, schema, table)
.await .await
.map_err(AppError)? .map_err(AppError::from)?
}; };
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn get_ddl( pub async fn get_ddl(
@@ -392,11 +395,11 @@ pub async fn get_ddl(
let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await { let result = if let Some(catalog) = external_doris_catalog(&state, &q.connection_id, q.catalog.as_deref()).await {
dbx_core::schema::get_doris_catalog_table_ddl_core(&state.app, &q.connection_id, &catalog, database, table) dbx_core::schema::get_doris_catalog_table_ddl_core(&state.app, &q.connection_id, &catalog, database, table)
.await .await
.map_err(AppError)? .map_err(AppError::from)?
} else { } else {
dbx_core::schema::get_table_ddl_core(&state.app, &q.connection_id, database, schema, table, q.object_type) dbx_core::schema::get_table_ddl_core(&state.app, &q.connection_id, database, schema, table, q.object_type)
.await .await
.map_err(AppError)? .map_err(AppError::from)?
}; };
Ok(Json(result)) Ok(Json(result))
} }
@@ -409,8 +412,8 @@ pub async fn list_functions(
let schema = q.schema.as_deref().unwrap_or(""); let schema = q.schema.as_deref().unwrap_or("");
let result = dbx_core::schema::list_functions_core(&state.app, &q.connection_id, database, schema) let result = dbx_core::schema::list_functions_core(&state.app, &q.connection_id, database, schema)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@@ -435,8 +438,8 @@ pub async fn list_sequences(
q.with_last_values.unwrap_or(false), q.with_last_values.unwrap_or(false),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_rules( pub async fn list_rules(
@@ -445,9 +448,10 @@ pub async fn list_rules(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let database = q.database.as_deref().unwrap_or(""); let database = q.database.as_deref().unwrap_or("");
let schema = q.schema.as_deref().unwrap_or(""); let schema = q.schema.as_deref().unwrap_or("");
let result = let result = dbx_core::schema::list_rules_core(&state.app, &q.connection_id, database, schema)
dbx_core::schema::list_rules_core(&state.app, &q.connection_id, database, schema).await.map_err(AppError)?; .await
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_owners( pub async fn list_owners(
@@ -456,9 +460,10 @@ pub async fn list_owners(
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let database = q.database.as_deref().unwrap_or(""); let database = q.database.as_deref().unwrap_or("");
let schema = q.schema.as_deref().unwrap_or(""); let schema = q.schema.as_deref().unwrap_or("");
let result = let result = dbx_core::schema::list_owners_core(&state.app, &q.connection_id, database, schema)
dbx_core::schema::list_owners_core(&state.app, &q.connection_id, database, schema).await.map_err(AppError)?; .await
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_extensions( pub async fn list_extensions(
@@ -469,8 +474,8 @@ pub async fn list_extensions(
let schema = q.schema.as_deref().unwrap_or(""); let schema = q.schema.as_deref().unwrap_or("");
let result = dbx_core::schema::list_extensions_core(&state.app, &q.connection_id, database, schema) let result = dbx_core::schema::list_extensions_core(&state.app, &q.connection_id, database, schema)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
pub async fn list_available_extensions( pub async fn list_available_extensions(
@@ -480,6 +485,6 @@ pub async fn list_available_extensions(
let database = q.database.as_deref().unwrap_or(""); let database = q.database.as_deref().unwrap_or("");
let result = dbx_core::schema::list_available_extensions_core(&state.app, &q.connection_id, database) let result = dbx_core::schema::list_available_extensions_core(&state.app, &q.connection_id, database)
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
} }
+3 -3
View File
@@ -28,7 +28,7 @@ pub async fn save_schema_cache(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<SaveSchemaCacheRequest>, Json(body): Json<SaveSchemaCacheRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.save_schema_cache(&body.cache_key, &body.payload).await.map_err(AppError)?; state.app.storage.save_schema_cache(&body.cache_key, &body.payload).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
@@ -36,7 +36,7 @@ pub async fn load_schema_cache(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Query(query): Query<SchemaCacheKeyQuery>, Query(query): Query<SchemaCacheKeyQuery>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let payload = state.app.storage.load_schema_cache(&query.cache_key).await.map_err(AppError)?; let payload = state.app.storage.load_schema_cache(&query.cache_key).await.map_err(AppError::from)?;
Ok(Json(payload.unwrap_or(serde_json::json!(null)))) Ok(Json(payload.unwrap_or(serde_json::json!(null))))
} }
@@ -44,6 +44,6 @@ pub async fn delete_schema_cache_prefix(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Query(query): Query<SchemaCachePrefixQuery>, Query(query): Query<SchemaCachePrefixQuery>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.delete_schema_cache_prefix(&query.prefix).await.map_err(AppError)?; state.app.storage.delete_schema_cache_prefix(&query.prefix).await.map_err(AppError::from)?;
Ok(Json(())) Ok(Json(()))
} }
+15 -15
View File
@@ -35,17 +35,17 @@ pub async fn preview_sql_file(
mut multipart: Multipart, mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let tmp_dir = state.data_dir.join("tmp"); let tmp_dir = state.data_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?; std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
if let Some(field) = multipart.next_field().await.map_err(|e| AppError(e.to_string()))? { if let Some(field) = multipart.next_field().await.map_err(|e| AppError::from(e.to_string()))? {
let file_name = field.file_name().unwrap_or("upload.sql").to_string(); let file_name = field.file_name().unwrap_or("upload.sql").to_string();
let data = field.bytes().await.map_err(|e| AppError(e.to_string()))?; let data = field.bytes().await.map_err(|e| AppError::from(e.to_string()))?;
let file_path = safe_uploaded_sql_path(&tmp_dir, &file_name)?; let file_path = safe_uploaded_sql_path(&tmp_dir, &file_name)?;
std::fs::write(&file_path, &data).map_err(|e| AppError(e.to_string()))?; std::fs::write(&file_path, &data).map_err(|e| AppError::from(e.to_string()))?;
let size_bytes = data.len() as u64; let size_bytes = data.len() as u64;
let content = sql::decode_sql_file_bytes(&data).map_err(AppError)?; let content = sql::decode_sql_file_bytes(&data).map_err(AppError::from)?;
let preview: String = content.chars().take(20_000).collect(); let preview: String = content.chars().take(20_000).collect();
return Ok(Json(serde_json::json!({ return Ok(Json(serde_json::json!({
@@ -57,7 +57,7 @@ pub async fn preview_sql_file(
}))); })));
} }
Err(AppError("No file uploaded".to_string())) Err(AppError::from("No file uploaded".to_string()))
} }
pub async fn execute_sql_file( pub async fn execute_sql_file(
@@ -68,7 +68,7 @@ pub async fn execute_sql_file(
// Fast-fail: reject early if the connection is read-only (individual statements are also checked in do_execute) // Fast-fail: reject early if the connection is read-only (individual statements are also checked in do_execute)
if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.connection_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.connection_id).await {
return Err(AppError(format!( return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. SQL file execution blocked.", "Read-only mode: connection '{}' has read-only protection enabled. SQL file execution blocked.",
name name
))); )));
@@ -81,7 +81,7 @@ pub async fn execute_sql_file(
{ {
let mut executions = state.sql_file_executions.write().await; let mut executions = state.sql_file_executions.write().await;
if executions.contains_key(&execution_id) { if executions.contains_key(&execution_id) {
return Err(AppError(format!("SQL file execution '{execution_id}' already exists"))); return Err(AppError::from(format!("SQL file execution '{execution_id}' already exists")));
} }
executions.insert(execution_id.clone(), token.clone()); executions.insert(execution_id.clone(), token.clone());
} }
@@ -162,7 +162,7 @@ async fn cleanup_sql_file_execution(state: &WebState, execution_id: &str) {
fn safe_uploaded_sql_path(tmp_dir: &Path, file_name: &str) -> Result<PathBuf, AppError> { fn safe_uploaded_sql_path(tmp_dir: &Path, file_name: &str) -> Result<PathBuf, AppError> {
let base_name = file_name.rsplit(['/', '\\']).find(|part| !part.is_empty()).unwrap_or("upload.sql").trim(); let base_name = file_name.rsplit(['/', '\\']).find(|part| !part.is_empty()).unwrap_or("upload.sql").trim();
if base_name.is_empty() || base_name == "." || base_name == ".." { if base_name.is_empty() || base_name == "." || base_name == ".." {
return Err(AppError("Invalid SQL file name".to_string())); return Err(AppError::from("Invalid SQL file name".to_string()));
} }
Ok(tmp_dir.join(base_name)) Ok(tmp_dir.join(base_name))
} }
@@ -170,13 +170,13 @@ fn safe_uploaded_sql_path(tmp_dir: &Path, file_name: &str) -> Result<PathBuf, Ap
fn validated_uploaded_sql_path(data_dir: &Path, file_path: &str) -> Result<PathBuf, AppError> { fn validated_uploaded_sql_path(data_dir: &Path, file_path: &str) -> Result<PathBuf, AppError> {
let path = PathBuf::from(file_path); let path = PathBuf::from(file_path);
if !path.is_absolute() { if !path.is_absolute() {
return Err(AppError("File path must be absolute".to_string())); return Err(AppError::from("File path must be absolute".to_string()));
} }
let tmp_dir = data_dir.join("tmp").canonicalize().map_err(|e| AppError(e.to_string()))?; let tmp_dir = data_dir.join("tmp").canonicalize().map_err(|e| AppError::from(e.to_string()))?;
let canonical_path = path.canonicalize().map_err(|e| AppError(e.to_string()))?; let canonical_path = path.canonicalize().map_err(|e| AppError::from(e.to_string()))?;
if !canonical_path.starts_with(&tmp_dir) { if !canonical_path.starts_with(&tmp_dir) {
return Err(AppError("File path must be inside the uploaded SQL directory".to_string())); return Err(AppError::from("File path must be inside the uploaded SQL directory".to_string()));
} }
Ok(canonical_path) Ok(canonical_path)
} }
@@ -186,7 +186,7 @@ pub async fn sql_file_progress(
AxumPath(execution_id): AxumPath<String>, AxumPath(execution_id): AxumPath<String>,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> { ) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> {
let channels = state.sse_channels.read().await; let channels = state.sse_channels.read().await;
let tx = channels.get(&execution_id).ok_or_else(|| AppError("Execution not found".to_string()))?; let tx = channels.get(&execution_id).ok_or_else(|| AppError::from("Execution not found".to_string()))?;
let rx = tx.subscribe(); let rx = tx.subscribe();
drop(channels); drop(channels);
Ok(crate::sse::sse_from_lossy_channel(rx)) Ok(crate::sse::sse_from_lossy_channel(rx))
@@ -216,7 +216,7 @@ mod tests {
let path = match safe_uploaded_sql_path(&tmp_dir, "../outside.sql") { let path = match safe_uploaded_sql_path(&tmp_dir, "../outside.sql") {
Ok(path) => path, Ok(path) => path,
Err(error) => panic!("{}", error.0), Err(error) => panic!("{}", error.message),
}; };
assert_eq!(path, tmp_dir.join("outside.sql")); assert_eq!(path, tmp_dir.join("outside.sql"));
+6 -6
View File
@@ -39,14 +39,14 @@ pub async fn start_table_export(
// Generate temp file path for web export output // Generate temp file path for web export output
let tmp_dir = state.data_dir.join("tmp"); let tmp_dir = state.data_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?; std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
let ext = match req.format.as_str() { let ext = match req.format.as_str() {
"csv" => "csv", "csv" => "csv",
"xlsx" => "xlsx", "xlsx" => "xlsx",
"json" => "json", "json" => "json",
"markdown" | "md" => "md", "markdown" | "md" => "md",
"sql" => "sql", "sql" => "sql",
_ => return Err(AppError(format!("Unsupported export format: {}", req.format))), _ => return Err(AppError::from(format!("Unsupported export format: {}", req.format))),
}; };
let tmp_file = tmp_dir.join(format!("table_export_{export_id}.{ext}")); let tmp_file = tmp_dir.join(format!("table_export_{export_id}.{ext}"));
let file_path = tmp_file.to_string_lossy().to_string(); let file_path = tmp_file.to_string_lossy().to_string();
@@ -132,9 +132,9 @@ pub async fn table_export_download(
.write() .write()
.await .await
.remove(&export_id) .remove(&export_id)
.ok_or_else(|| AppError("Export file not found".to_string()))?; .ok_or_else(|| AppError::from("Export file not found".to_string()))?;
let data = tokio::fs::read(&file_path).await.map_err(|e| AppError(e.to_string()))?; let data = tokio::fs::read(&file_path).await.map_err(|e| AppError::from(e.to_string()))?;
// Clean up temp file // Clean up temp file
let _ = tokio::fs::remove_file(&file_path).await; let _ = tokio::fs::remove_file(&file_path).await;
@@ -144,7 +144,7 @@ pub async fn table_export_download(
"json" => ("application/json; charset=utf-8", "json"), "json" => ("application/json; charset=utf-8", "json"),
"markdown" | "md" => ("text/markdown; charset=utf-8", "md"), "markdown" | "md" => ("text/markdown; charset=utf-8", "md"),
"sql" => ("application/sql; charset=utf-8", "sql"), "sql" => ("application/sql; charset=utf-8", "sql"),
_ => return Err(AppError(format!("Unknown format: {format}"))), _ => return Err(AppError::from(format!("Unknown format: {format}"))),
}; };
let filename = format!("table_export_{export_id}.{file_ext}"); let filename = format!("table_export_{export_id}.{file_ext}");
@@ -154,5 +154,5 @@ pub async fn table_export_download(
.header(header::CONTENT_TYPE, content_type) .header(header::CONTENT_TYPE, content_type)
.header(header::CONTENT_DISPOSITION, format!("attachment; filename=\"{filename}\"")) .header(header::CONTENT_DISPOSITION, format!("attachment; filename=\"{filename}\""))
.body(Body::from(data)) .body(Body::from(data))
.map_err(|e| AppError(e.to_string())) .map_err(|e| AppError::from(e.to_string()))
} }
+23 -23
View File
@@ -37,7 +37,7 @@ pub async fn preview_import(
mut multipart: Multipart, mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let tmp_dir = import_upload_dir(&state.data_dir); let tmp_dir = import_upload_dir(&state.data_dir);
std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?; std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError::from(e.to_string()))?;
cleanup_expired_import_uploads(&tmp_dir, Duration::from_secs(24 * 60 * 60)); cleanup_expired_import_uploads(&tmp_dir, Duration::from_secs(24 * 60 * 60));
let mut uploaded_file: Option<(String, PathBuf)> = None; let mut uploaded_file: Option<(String, PathBuf)> = None;
@@ -51,14 +51,14 @@ pub async fn preview_import(
Ok(None) => break, Ok(None) => break,
Err(error) => { Err(error) => {
cleanup_pending_upload(&uploaded_file).await; cleanup_pending_upload(&uploaded_file).await;
return Err(AppError(error.to_string())); return Err(AppError::from(error.to_string()));
} }
}; };
let name = field.name().unwrap_or_default().to_string(); let name = field.name().unwrap_or_default().to_string();
if name == "file" { if name == "file" {
if uploaded_file.is_some() { if uploaded_file.is_some() {
cleanup_pending_upload(&uploaded_file).await; cleanup_pending_upload(&uploaded_file).await;
return Err(AppError("Only one import file may be uploaded".to_string())); return Err(AppError::from("Only one import file may be uploaded".to_string()));
} }
let file_name = field.file_name().unwrap_or("upload.csv").to_string(); let file_name = field.file_name().unwrap_or("upload.csv").to_string();
let source_ref = uuid::Uuid::new_v4().to_string(); let source_ref = uuid::Uuid::new_v4().to_string();
@@ -73,7 +73,7 @@ pub async fn preview_import(
Ok(value) => value, Ok(value) => value,
Err(error) => { Err(error) => {
cleanup_pending_upload(&uploaded_file).await; cleanup_pending_upload(&uploaded_file).await;
return Err(AppError(error.to_string())); return Err(AppError::from(error.to_string()));
} }
}; };
match name.as_str() { match name.as_str() {
@@ -82,7 +82,7 @@ pub async fn preview_import(
Ok(source_format) => Some(source_format), Ok(source_format) => Some(source_format),
Err(error) => { Err(error) => {
cleanup_pending_upload(&uploaded_file).await; cleanup_pending_upload(&uploaded_file).await;
return Err(AppError(error.to_string())); return Err(AppError::from(error.to_string()));
} }
}; };
} }
@@ -91,7 +91,7 @@ pub async fn preview_import(
Ok(parse_options) => parse_options, Ok(parse_options) => parse_options,
Err(error) => { Err(error) => {
cleanup_pending_upload(&uploaded_file).await; cleanup_pending_upload(&uploaded_file).await;
return Err(AppError(error.to_string())); return Err(AppError::from(error.to_string()));
} }
}; };
} }
@@ -117,20 +117,20 @@ pub async fn preview_import(
Ok(preview) => preview, Ok(preview) => preview,
Err(error) => { Err(error) => {
cleanup_uploaded_import_path(&file_path).await; cleanup_uploaded_import_path(&file_path).await;
return Err(AppError(error)); return Err(AppError::from(error));
} }
}; };
let preview = match serde_json::to_value(preview) { let preview = match serde_json::to_value(preview) {
Ok(preview) => preview, Ok(preview) => preview,
Err(error) => { Err(error) => {
cleanup_uploaded_import_path(&file_path).await; cleanup_uploaded_import_path(&file_path).await;
return Err(AppError(error.to_string())); return Err(AppError::from(error.to_string()));
} }
}; };
return Ok(Json(preview)); return Ok(Json(preview));
} }
Err(AppError("No file uploaded".to_string())) Err(AppError::from("No file uploaded".to_string()))
} }
async fn write_import_upload(field: axum::extract::multipart::Field<'_>, file_path: &StdPath) -> Result<(), AppError> { async fn write_import_upload(field: axum::extract::multipart::Field<'_>, file_path: &StdPath) -> Result<(), AppError> {
@@ -146,22 +146,22 @@ where
S: Stream<Item = Result<Bytes, E>> + Unpin, S: Stream<Item = Result<Bytes, E>> + Unpin,
E: std::fmt::Display, E: std::fmt::Display,
{ {
let mut upload = tokio::fs::File::create(file_path).await.map_err(|error| AppError(error.to_string()))?; let mut upload = tokio::fs::File::create(file_path).await.map_err(|error| AppError::from(error.to_string()))?;
let mut uploaded_bytes = 0usize; let mut uploaded_bytes = 0usize;
// Stream uploads to disk so valid large CSV files don't require a second full-size in-memory copy. // Stream uploads to disk so valid large CSV files don't require a second full-size in-memory copy.
let result = async { let result = async {
while let Some(chunk) = chunks.next().await { while let Some(chunk) = chunks.next().await {
let chunk = chunk.map_err(|error| AppError(error.to_string()))?; let chunk = chunk.map_err(|error| AppError::from(error.to_string()))?;
uploaded_bytes = uploaded_bytes.saturating_add(chunk.len()); uploaded_bytes = uploaded_bytes.saturating_add(chunk.len());
if uploaded_bytes > max_upload_bytes { if uploaded_bytes > max_upload_bytes {
return Err(AppError(format!( return Err(AppError::from(format!(
"File too large: {uploaded_bytes} bytes received (max {max_upload_bytes} bytes)" "File too large: {uploaded_bytes} bytes received (max {max_upload_bytes} bytes)"
))); )));
} }
upload.write_all(&chunk).await.map_err(|error| AppError(error.to_string()))?; upload.write_all(&chunk).await.map_err(|error| AppError::from(error.to_string()))?;
} }
upload.flush().await.map_err(|error| AppError(error.to_string())) upload.flush().await.map_err(|error| AppError::from(error.to_string()))
} }
.await; .await;
drop(upload); drop(upload);
@@ -183,7 +183,7 @@ pub async fn execute_import(
// Reject import early if the connection is read-only // Reject import early if the connection is read-only
if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.connection_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.connection_id).await {
cleanup_uploaded_import_source(&req.file_path).await; cleanup_uploaded_import_source(&req.file_path).await;
return Err(AppError(format!( return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. Import blocked.", "Read-only mode: connection '{}' has read-only protection enabled. Import blocked.",
name name
))); )));
@@ -287,7 +287,7 @@ pub async fn import_progress(
Path(import_id): Path<String>, Path(import_id): Path<String>,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> { ) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> {
let channels = state.sse_channels.read().await; let channels = state.sse_channels.read().await;
let tx = channels.get(&import_id).ok_or_else(|| AppError("Import not found".to_string()))?; let tx = channels.get(&import_id).ok_or_else(|| AppError::from("Import not found".to_string()))?;
let rx = tx.subscribe(); let rx = tx.subscribe();
drop(channels); drop(channels);
Ok(crate::sse::sse_from_channel(rx)) Ok(crate::sse::sse_from_channel(rx))
@@ -308,7 +308,7 @@ fn import_upload_dir(data_dir: &StdPath) -> PathBuf {
fn safe_uploaded_import_path(tmp_dir: &StdPath, file_name: &str, source_ref: &str) -> Result<PathBuf, AppError> { fn safe_uploaded_import_path(tmp_dir: &StdPath, file_name: &str, source_ref: &str) -> Result<PathBuf, AppError> {
let base_name = file_name.rsplit(['/', '\\']).find(|part| !part.is_empty()).unwrap_or("upload.csv").trim(); let base_name = file_name.rsplit(['/', '\\']).find(|part| !part.is_empty()).unwrap_or("upload.csv").trim();
if base_name.is_empty() || base_name == "." || base_name == ".." { if base_name.is_empty() || base_name == "." || base_name == ".." {
return Err(AppError("Invalid import file name".to_string())); return Err(AppError::from("Invalid import file name".to_string()));
} }
Ok(tmp_dir.join(format!("{source_ref}-{base_name}"))) Ok(tmp_dir.join(format!("{source_ref}-{base_name}")))
} }
@@ -316,14 +316,14 @@ fn safe_uploaded_import_path(tmp_dir: &StdPath, file_name: &str, source_ref: &st
fn validated_uploaded_import_path(data_dir: &StdPath, file_path: &str) -> Result<PathBuf, AppError> { fn validated_uploaded_import_path(data_dir: &StdPath, file_path: &str) -> Result<PathBuf, AppError> {
let path = PathBuf::from(file_path); let path = PathBuf::from(file_path);
if !path.is_absolute() { if !path.is_absolute() {
return Err(AppError("Import source path must be absolute".to_string())); return Err(AppError::from("Import source path must be absolute".to_string()));
} }
let tmp_dir = import_upload_dir(data_dir).canonicalize().map_err(|e| AppError(e.to_string()))?; let tmp_dir = import_upload_dir(data_dir).canonicalize().map_err(|e| AppError::from(e.to_string()))?;
let canonical_path = let canonical_path =
path.canonicalize().map_err(|e| AppError(format!("Import source is no longer available: {e}")))?; path.canonicalize().map_err(|e| AppError::from(format!("Import source is no longer available: {e}")))?;
if !canonical_path.starts_with(&tmp_dir) { if !canonical_path.starts_with(&tmp_dir) {
return Err(AppError("Import source must be inside the uploaded import directory".to_string())); return Err(AppError::from("Import source must be inside the uploaded import directory".to_string()));
} }
Ok(canonical_path) Ok(canonical_path)
} }
@@ -387,7 +387,7 @@ mod tests {
let error = write_import_upload_stream(chunks, &file_path, 4).await.unwrap_err(); let error = write_import_upload_stream(chunks, &file_path, 4).await.unwrap_err();
assert!(error.0.contains("File too large")); assert!(error.message.contains("File too large"));
assert!(!file_path.exists()); assert!(!file_path.exists());
} }
@@ -398,7 +398,7 @@ mod tests {
let error = write_import_upload_stream(chunks, &file_path, 8).await.unwrap_err(); let error = write_import_upload_stream(chunks, &file_path, 8).await.unwrap_err();
assert_eq!(error.0, "multipart stream failed"); assert_eq!(error.message, "multipart stream failed");
assert!(!file_path.exists()); assert!(!file_path.exists());
} }
} }
+18 -12
View File
@@ -33,11 +33,11 @@ pub async fn start_transfer(
Json(body): Json<StartTransferRequest>, Json(body): Json<StartTransferRequest>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let req = body.request; let req = body.request;
transfer::validate_transfer_target_table_names(&req).map_err(AppError)?; transfer::validate_transfer_target_table_names(&req).map_err(AppError::from)?;
// Reject transfer early if the target connection is read-only // Reject transfer early if the target connection is read-only
if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.target_connection_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.target_connection_id).await {
return Err(AppError(format!( return Err(AppError::from(format!(
"Read-only mode: target connection '{}' has read-only protection enabled. Transfer blocked.", "Read-only mode: target connection '{}' has read-only protection enabled. Transfer blocked.",
name name
))); )));
@@ -353,13 +353,19 @@ pub async fn preview_transfer_ownership(
Json(body): Json<PreviewTransferOwnershipRequest>, Json(body): Json<PreviewTransferOwnershipRequest>,
) -> Result<Json<dbx_core::transfer::TransferOwnershipPreview>, AppError> { ) -> Result<Json<dbx_core::transfer::TransferOwnershipPreview>, AppError> {
let req = body.request; let req = body.request;
transfer::validate_transfer_target_table_names(&req).map_err(AppError)?; transfer::validate_transfer_target_table_names(&req).map_err(AppError::from)?;
let source_db_type = transfer::get_db_type(&state.app, &req.source_connection_id).await.map_err(AppError)?; let source_db_type = transfer::get_db_type(&state.app, &req.source_connection_id).await.map_err(AppError::from)?;
let target_db_type = transfer::get_db_type(&state.app, &req.target_connection_id).await.map_err(AppError)?; let target_db_type = transfer::get_db_type(&state.app, &req.target_connection_id).await.map_err(AppError::from)?;
let source_pool_key = let source_pool_key = state
state.app.get_or_create_pool(&req.source_connection_id, Some(&req.source_database)).await.map_err(AppError)?; .app
let target_pool_key = .get_or_create_pool(&req.source_connection_id, Some(&req.source_database))
state.app.get_or_create_pool(&req.target_connection_id, Some(&req.target_database)).await.map_err(AppError)?; .await
.map_err(AppError::from)?;
let target_pool_key = state
.app
.get_or_create_pool(&req.target_connection_id, Some(&req.target_database))
.await
.map_err(AppError::from)?;
let preview = transfer::preview_transfer_ownership( let preview = transfer::preview_transfer_ownership(
&state.app, &state.app,
&req, &req,
@@ -369,7 +375,7 @@ pub async fn preview_transfer_ownership(
&target_pool_key, &target_pool_key,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(preview)) Ok(Json(preview))
} }
@@ -378,7 +384,7 @@ pub async fn transfer_progress(
Path(transfer_id): Path<String>, Path(transfer_id): Path<String>,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> { ) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> {
let channels = state.sse_channels.read().await; let channels = state.sse_channels.read().await;
let tx = channels.get(&transfer_id).ok_or_else(|| AppError("Transfer not found".to_string()))?; let tx = channels.get(&transfer_id).ok_or_else(|| AppError::from("Transfer not found".to_string()))?;
let rx = tx.subscribe(); let rx = tx.subscribe();
drop(channels); drop(channels);
Ok(crate::sse::sse_from_channel(rx)) Ok(crate::sse::sse_from_channel(rx))
@@ -416,5 +422,5 @@ pub async fn sort_tables_by_fk_dependency(
) )
.await .await
.map(Json) .map(Json)
.map_err(AppError) .map_err(AppError::from)
} }
+3 -3
View File
@@ -17,19 +17,19 @@ pub struct SaveTunnelProfilesRequest {
pub async fn load_tunnel_profiles( pub async fn load_tunnel_profiles(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
) -> Result<Json<Vec<TransportLayerConfig>>, AppError> { ) -> Result<Json<Vec<TransportLayerConfig>>, AppError> {
state.app.storage.load_tunnel_profiles().await.map(Json).map_err(AppError) state.app.storage.load_tunnel_profiles().await.map(Json).map_err(AppError::from)
} }
pub async fn save_tunnel_profiles( pub async fn save_tunnel_profiles(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(body): Json<SaveTunnelProfilesRequest>, Json(body): Json<SaveTunnelProfilesRequest>,
) -> Result<Json<()>, AppError> { ) -> Result<Json<()>, AppError> {
state.app.storage.save_tunnel_profiles(&body.profiles).await.map(Json).map_err(AppError) state.app.storage.save_tunnel_profiles(&body.profiles).await.map(Json).map_err(AppError::from)
} }
pub async fn test_tunnel_profile( pub async fn test_tunnel_profile(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(profile): Json<TransportLayerConfig>, Json(profile): Json<TransportLayerConfig>,
) -> Result<Json<String>, AppError> { ) -> Result<Json<String>, AppError> {
state.app.test_tunnel_profile(&profile).await.map(Json).map_err(AppError) state.app.test_tunnel_profile(&profile).await.map(Json).map_err(AppError::from)
} }
+4 -3
View File
@@ -17,9 +17,10 @@ pub struct UpdateCheckParams {
pub async fn check_for_updates(Query(params): Query<UpdateCheckParams>) -> Result<Json<serde_json::Value>, AppError> { pub async fn check_for_updates(Query(params): Query<UpdateCheckParams>) -> Result<Json<serde_json::Value>, AppError> {
let locale = params.locale.unwrap_or_else(|| "zh-CN".to_string()); let locale = params.locale.unwrap_or_else(|| "zh-CN".to_string());
let release = update::fetch_latest_release(&locale, params.source.unwrap_or_default()).await.map_err(AppError)?; let release =
update::fetch_latest_release(&locale, params.source.unwrap_or_default()).await.map_err(AppError::from)?;
let info = update::build_update_info(release, env!("CARGO_PKG_VERSION")); let info = update::build_update_info(release, env!("CARGO_PKG_VERSION"));
Ok(Json(serde_json::to_value(info).map_err(|e| AppError(e.to_string()))?)) Ok(Json(serde_json::to_value(info).map_err(|e| AppError::from(e.to_string()))?))
} }
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
@@ -32,6 +33,6 @@ pub async fn fetch_changelog(
Query(params): Query<ChangelogParams>, Query(params): Query<ChangelogParams>,
) -> Result<Json<changelog::ChangelogData>, AppError> { ) -> Result<Json<changelog::ChangelogData>, AppError> {
let lang = params.lang.unwrap_or_else(|| "en".to_string()); let lang = params.lang.unwrap_or_else(|| "en".to_string());
let data = changelog::fetch_changelog(&lang).await.map_err(AppError)?; let data = changelog::fetch_changelog(&lang).await.map_err(AppError::from)?;
Ok(Json(data)) Ok(Json(data))
} }
+6 -5
View File
@@ -13,7 +13,7 @@ async fn ensure_writable(
action: &str, action: &str,
) -> Result<(), AppError> { ) -> Result<(), AppError> {
if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
return Err(AppError(format!( return Err(AppError::from(format!(
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.", "Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
name, action name, action
))); )));
@@ -60,7 +60,7 @@ pub async fn list_prefix(
req.recursive, req.recursive,
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -68,7 +68,8 @@ pub async fn get(
State(state): State<Arc<WebState>>, State(state): State<Arc<WebState>>,
Json(req): Json<ZooKeeperKeyRequest>, Json(req): Json<ZooKeeperKeyRequest>,
) -> Result<Json<dbx_core::agent_kv::KvGetResponse>, AppError> { ) -> Result<Json<dbx_core::agent_kv::KvGetResponse>, AppError> {
let result = dbx_core::agent_kv::kv_get_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError)?; let result =
dbx_core::agent_kv::kv_get_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -85,7 +86,7 @@ pub async fn put(
req.options.unwrap_or_default(), req.options.unwrap_or_default(),
) )
.await .await
.map_err(AppError)?; .map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
@@ -95,6 +96,6 @@ pub async fn delete(
) -> Result<Json<dbx_core::agent_kv::KvDeleteResponse>, AppError> { ) -> Result<Json<dbx_core::agent_kv::KvDeleteResponse>, AppError> {
ensure_writable(&state.app, &req.connection_id, "Delete").await?; ensure_writable(&state.app, &req.connection_id, "Delete").await?;
let result = let result =
dbx_core::agent_kv::kv_delete_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError)?; dbx_core::agent_kv::kv_delete_core(&state.app, &req.connection_id, &req.key).await.map_err(AppError::from)?;
Ok(Json(result)) Ok(Json(result))
} }
+112
View File
@@ -0,0 +1,112 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { test } from "vitest";
import { parse } from "vue/compiler-sfc";
// Regression guard for the F2 finding: AiAssistant.send() must acquire the
// synchronous send guard (isGenerating.value = true) BEFORE its first await
// (promptTemplateStore.ensureLoaded()). Otherwise two rapid submissions can
// both pass the initial isGenerating check, await the deferred store load, and
// resume into two concurrent agent runs.
//
// send() is an internal <script setup> function (not exported / not exposed),
// and this repo has no component-mount test harness (@vue/test-utils is not a
// dependency; zero specs call mount()). So — like aiMessageLayout.test.ts — we
// assert the invariant against the SFC source: the guard is set synchronously
// before any suspension point, which is exactly what makes the check-then-set
// sequence atomic against a concurrent caller.
const aiAssistantPath = "apps/desktop/src/components/editor/AiAssistant.vue";
const source = readFileSync(aiAssistantPath, "utf8");
function scriptSetupContent(): string {
const { descriptor, errors } = parse(source, { filename: aiAssistantPath });
assert.deepEqual(errors, []);
assert.ok(descriptor.scriptSetup, "AiAssistant.vue should use <script setup>");
return descriptor.scriptSetup!.content;
}
// Extract a function body by brace-matching from a signature anchor, so the
// assertions below are scoped to send() and immune to unrelated edits elsewhere
// in the (very large) component.
function functionBody(script: string, signature: string): string {
const sigIndex = script.indexOf(signature);
assert.notEqual(sigIndex, -1, `expected to find ${signature}`);
assert.equal(
script.indexOf(signature, sigIndex + signature.length),
-1,
`${signature} should be unique`,
);
const openBrace = script.indexOf("{", sigIndex + signature.length);
assert.notEqual(openBrace, -1, "function body should open with a brace");
let depth = 0;
for (let i = openBrace; i < script.length; i++) {
const ch = script[i];
if (ch === "{") depth++;
else if (ch === "}") {
depth--;
if (depth === 0) return script.slice(openBrace + 1, i);
}
}
throw new Error("unbalanced braces while extracting function body");
}
test("send() acquires the guard synchronously before the first await", () => {
const body = functionBody(scriptSetupContent(), "async function send()");
const entryCheck = body.search(/\|\|\s*isGenerating\.value\)\s*return/);
const guardSet = body.indexOf("isGenerating.value = true");
const firstAwait = body.indexOf("await");
assert.notEqual(entryCheck, -1, "send() should early-return when isGenerating is already set");
assert.notEqual(guardSet, -1, "send() should set the isGenerating guard");
assert.notEqual(firstAwait, -1, "send() should contain an await");
// The entry check runs first, then the guard is set, then (and only then) the
// first await suspends. Because nothing awaits between the check and the set,
// a second concurrent send() cannot slip past the check before the guard flips.
assert.ok(entryCheck < guardSet, "isGenerating check must precede the guard set");
assert.ok(guardSet < firstAwait, "guard must be set before the first await (no await between check and set)");
// The first suspension point is specifically the prompt-template load — the
// await the reviewer flagged as happening before the guard existed.
const ensureLoadedAwait = body.indexOf("await promptTemplateStore.ensureLoaded()");
assert.notEqual(ensureLoadedAwait, -1, "send() should await promptTemplateStore.ensureLoaded()");
assert.equal(firstAwait, ensureLoadedAwait, "ensureLoaded() must be the first await in send()");
});
test("send() never leaks the guard on an early return after acquiring it", () => {
const body = functionBody(scriptSetupContent(), "async function send()");
const guardSet = body.indexOf("isGenerating.value = true");
// Every `return` that appears after the guard is acquired must first reset the
// guard (there is one such path: the ensureLoaded() failure branch). A bare
// `return` after the guard without a reset would strand isGenerating at true
// and permanently block all future sends.
const tail = body.slice(guardSet);
const returnsAfterGuard = [...tail.matchAll(/\breturn\b/g)];
assert.ok(returnsAfterGuard.length >= 1, "expected the ensureLoaded failure branch to early-return");
for (const match of returnsAfterGuard) {
const preceding = tail.slice(0, match.index);
assert.ok(
preceding.includes("isGenerating.value = false"),
"any early return after the guard is acquired must reset isGenerating first",
);
}
});
test("send() snapshots custom prompts before deferred AI context loading", () => {
const body = functionBody(scriptSetupContent(), "async function send()");
const ensureLoadedAwait = body.indexOf("await promptTemplateStore.ensureLoaded()");
const snapshot = body.indexOf("const customPromptContext: CustomPromptContext");
const sqlFileLoad = body.indexOf("await loadReferencedSqlFiles");
const aiContextLoad = body.indexOf("await buildAiContext");
assert.notEqual(snapshot, -1, "send() should snapshot the custom prompt context");
assert.ok(ensureLoadedAwait < snapshot, "the snapshot must be taken after templates finish loading");
assert.ok(snapshot < sqlFileLoad, "SQL file loading must not delay the custom prompt snapshot");
assert.ok(snapshot < aiContextLoad, "AI context loading must not delay the custom prompt snapshot");
assert.match(body.slice(snapshot, sqlFileLoad), /activeTemplates:\s*\[\.\.\.activeTemplates\.value\]/);
});
+236
View File
@@ -0,0 +1,236 @@
import { strict as assert } from "node:assert";
import { test } from "vitest";
import type { AiContext } from "../../apps/desktop/src/lib/ai/ai.ts";
import { promptTemplateCharacterCount, type PromptTemplate } from "../../apps/desktop/src/types/promptTemplate";
// ---------------------------------------------------------------------------
// Setup (mirrors existing aiPrompt.test.ts setup)
// ---------------------------------------------------------------------------
class MemoryStorage {
private values = new Map<string, string>();
getItem(key: string): string | null {
return this.values.get(key) ?? null;
}
setItem(key: string, value: string) {
this.values.set(key, value);
}
removeItem(key: string) {
this.values.delete(key);
}
clear() {
this.values.clear();
}
}
const localStorage = new MemoryStorage();
localStorage.setItem("dbx-locale", "en");
Object.defineProperty(globalThis, "localStorage", {
value: localStorage,
configurable: true,
});
const { buildSystemPrompt, isVectorDbType } = await import("../../apps/desktop/src/lib/ai/ai.ts");
// Test via buildSystemPrompt and its public API.
function context(overrides: Partial<AiContext> = {}): AiContext {
return {
connectionId: "conn-1",
connectionName: "prod-analytics",
databaseType: "postgres",
database: "app",
currentSql: "",
tables: [
{
schema: "public",
name: "orders",
tableType: "TABLE",
columns: [
{ name: "id", data_type: "uuid", is_nullable: false, is_primary_key: true },
{ name: "user_id", data_type: "uuid", is_nullable: false },
{ name: "total", data_type: "numeric", is_nullable: false },
],
indexes: [{ name: "idx_orders_user_id", columns: ["user_id"], is_unique: false, is_primary: false }],
foreignKeys: [{ column: "user_id", ref_table: "users", ref_column: "id" }],
},
],
sqlFiles: [],
truncated: false,
...overrides,
};
}
function makeTemplate(id: string, name: string, content: string): PromptTemplate {
return { id, name, content, createdAt: "2026-01-01T00:00:00Z", updatedAt: "2026-01-01T00:00:00Z" };
}
test("prompt template character count matches Rust Unicode scalar counting", () => {
assert.strictEqual(promptTemplateCharacterCount("A😀中"), 3);
assert.strictEqual(promptTemplateCharacterCount("😀".repeat(8000)), 8000);
});
// ---------------------------------------------------------------------------
// Baseline (no custom context) — byte-identical
// ---------------------------------------------------------------------------
test("buildSystemPrompt without custom = byte-identical baseline", () => {
const baseline = buildSystemPrompt("general", context(), "ask");
const withUndefined = buildSystemPrompt("general", context(), "ask", undefined);
const withEmpty = buildSystemPrompt("general", context(), "ask", {});
assert.strictEqual(withUndefined, baseline);
assert.strictEqual(withEmpty, baseline);
});
test("buildSystemPrompt without custom (agent mode) = byte-identical baseline", () => {
const baseline = buildSystemPrompt("generate", context(), "agent");
const withUndefined = buildSystemPrompt("generate", context(), "agent", undefined);
assert.strictEqual(withUndefined, baseline);
});
// ---------------------------------------------------------------------------
// Global instructions only
// ---------------------------------------------------------------------------
test("global instructions only — injected in relational path", () => {
const prompt = buildSystemPrompt("general", context(), "ask", {
globalInstructions: "Always use UTC timestamps.",
});
assert.match(prompt, /Custom Instructions \(supplementary\)/);
assert.match(prompt, /Always use UTC timestamps/);
// Should NOT have template heading
assert.doesNotMatch(prompt, /### /);
});
test("global instructions only — injected in vector path (qdrant)", () => {
const prompt = buildSystemPrompt("general", context({ databaseType: "qdrant" }), "ask", {
globalInstructions: "Use vector search for similarity.",
});
assert.match(prompt, /Custom Instructions \(supplementary\)/);
assert.match(prompt, /Use vector search for similarity/);
});
// ---------------------------------------------------------------------------
// Templates only
// ---------------------------------------------------------------------------
test("templates only — injected in relational path", () => {
const prompt = buildSystemPrompt("general", context(), "ask", {
activeTemplates: [
makeTemplate("t1", "Production Rules", "Always filter by tenant_id."),
],
});
assert.match(prompt, /Custom Instructions \(supplementary\)/);
assert.match(prompt, /### Production Rules/);
assert.match(prompt, /Always filter by tenant_id/);
});
test("templates only — injected in vector path (qdrant)", () => {
const prompt = buildSystemPrompt("general", context({ databaseType: "qdrant" }), "ask", {
activeTemplates: [
makeTemplate("t1", "Vector Conventions", "Collections must have metadata."),
],
});
assert.match(prompt, /Custom Instructions \(supplementary\)/);
assert.match(prompt, /### Vector Conventions/);
assert.match(prompt, /Collections must have metadata/);
});
// ---------------------------------------------------------------------------
// Both global instructions and templates
// ---------------------------------------------------------------------------
test("global + templates — correct order: global first, then templates", () => {
const prompt = buildSystemPrompt("general", context(), "ask", {
globalInstructions: "GLOBAL_RULE",
activeTemplates: [
makeTemplate("t1", "Template1", "T1_CONTENT"),
makeTemplate("t2", "Template2", "T2_CONTENT"),
],
});
// Section header appears
assert.match(prompt, /Custom Instructions \(supplementary\)/);
// Global must appear before template sections
const customStart = prompt.indexOf("## Custom Instructions");
const globalPos = prompt.indexOf("GLOBAL_RULE", customStart);
const t1Pos = prompt.indexOf("### Template1", customStart);
const t2Pos = prompt.indexOf("### Template2", customStart);
assert.ok(globalPos < t1Pos, "global instructions must appear before Template1");
assert.ok(t1Pos < t2Pos, "Template1 must appear before Template2");
});
// ---------------------------------------------------------------------------
// Whitespace-only content stripped
// ---------------------------------------------------------------------------
test("whitespace-only template content not injected", () => {
const prompt = buildSystemPrompt("general", context(), "ask", {
activeTemplates: [
makeTemplate("t1", "Empty Template", " \n \t "),
makeTemplate("t2", "Valid Template", "Do X and Y."),
],
});
assert.match(prompt, /### Valid Template/);
assert.doesNotMatch(prompt, /### Empty Template/);
assert.match(prompt, /Do X and Y/);
});
test("whitespace-only global instructions not injected", () => {
const baseline = buildSystemPrompt("general", context(), "ask");
const withBlankGlobal = buildSystemPrompt("general", context(), "ask", {
globalInstructions: " \n \t ",
});
assert.strictEqual(withBlankGlobal, baseline);
});
// ---------------------------------------------------------------------------
// Vector DB types all covered
// ---------------------------------------------------------------------------
test("vector db type (chromadb) includes injected section", () => {
const prompt = buildSystemPrompt("general", context({ databaseType: "chromadb" }), "ask", {
globalInstructions: "Chromadb instructions.",
});
assert.match(prompt, /Custom Instructions \(supplementary\)/);
assert.match(prompt, /Chromadb instructions/);
});
test("vector db type (milvus) includes injected section", () => {
const prompt = buildSystemPrompt("general", context({ databaseType: "milvus" }), "ask", {
activeTemplates: [makeTemplate("t1", "Milvus Rules", "Use partition key.")],
});
assert.match(prompt, /Custom Instructions \(supplementary\)/);
assert.match(prompt, /### Milvus Rules/);
});
// ---------------------------------------------------------------------------
// Agent mode also injects
// ---------------------------------------------------------------------------
test("agent mode injects custom instructions", () => {
const prompt = buildSystemPrompt("query", context(), "agent", {
globalInstructions: "AGENT_RULE",
});
assert.match(prompt, /Custom Instructions \(supplementary\)/);
assert.match(prompt, /AGENT_RULE/);
});
// ---------------------------------------------------------------------------
// Empty custom context equals no custom context (byte-identical)
// ---------------------------------------------------------------------------
test("custom with all empty fields equals no custom", () => {
const baseline = buildSystemPrompt("general", context(), "ask");
const withEmpty = buildSystemPrompt("general", context(), "ask", {
globalInstructions: "",
activeTemplates: [],
});
assert.strictEqual(withEmpty, baseline);
});
+144
View File
@@ -0,0 +1,144 @@
import assert from "node:assert/strict";
import { beforeEach, test, vi } from "vitest";
import { createPinia, setActivePinia } from "pinia";
import type { PromptTemplate } from "../../apps/desktop/src/types/promptTemplate";
const apiMock = vi.hoisted(() => ({
getAiGlobalCustomInstructions: vi.fn(),
loadPromptTemplates: vi.fn(),
}));
vi.mock("@/lib/backend/api", () => apiMock);
import { usePromptTemplateStore } from "../../apps/desktop/src/stores/promptTemplateStore.ts";
const template: PromptTemplate = {
id: "production-rules",
name: "Production Rules",
content: "Use tenant_id filters.",
createdAt: "2026-01-01T00:00:00Z",
updatedAt: "2026-01-01T00:00:00Z",
};
beforeEach(() => {
setActivePinia(createPinia());
apiMock.loadPromptTemplates.mockReset();
apiMock.getAiGlobalCustomInstructions.mockReset();
});
test("concurrent prompt initialization waits for one complete load", async () => {
let resolveTemplates!: (value: PromptTemplate[]) => void;
let resolveGlobalInstructions!: (value: string) => void;
const templates = new Promise<PromptTemplate[]>((resolve) => {
resolveTemplates = resolve;
});
const globalInstructions = new Promise<string>((resolve) => {
resolveGlobalInstructions = resolve;
});
apiMock.loadPromptTemplates.mockReturnValueOnce(templates);
apiMock.getAiGlobalCustomInstructions.mockReturnValueOnce(globalInstructions);
const store = usePromptTemplateStore();
const initialLoad = store.init();
const sendLoad = store.ensureLoaded();
assert.equal(apiMock.loadPromptTemplates.mock.calls.length, 1);
assert.equal(apiMock.getAiGlobalCustomInstructions.mock.calls.length, 1);
resolveTemplates([template]);
resolveGlobalInstructions("Always use read-only SQL first.");
assert.deepEqual(await Promise.all([initialLoad, sendLoad]), [true, true]);
assert.deepEqual(store.templates, [template]);
assert.equal(store.globalInstructions, "Always use read-only SQL first.");
});
test("failed prompt initialization remains retryable", async () => {
apiMock.loadPromptTemplates.mockRejectedValueOnce(new Error("backend unavailable"));
apiMock.getAiGlobalCustomInstructions.mockResolvedValueOnce("stale instruction");
apiMock.loadPromptTemplates.mockResolvedValueOnce([template]);
apiMock.getAiGlobalCustomInstructions.mockResolvedValueOnce("Recovered instruction");
const store = usePromptTemplateStore();
assert.equal(await store.ensureLoaded(), false);
assert.equal(store.isLoaded, false);
assert.equal(await store.ensureLoaded(), true);
assert.equal(store.globalInstructions, "Recovered instruction");
assert.deepEqual(store.templates, [template]);
});
test("delayed load populates globalInstructions after async init resolves", async () => {
// Simulates EditorSettingsDialog's AI-tab flow: a fire-and-forget init()
// has been issued elsewhere, and the consumer must await ensureLoaded()
// before reading globalInstructions — otherwise it sees the empty default.
let resolveTemplates!: (value: PromptTemplate[]) => void;
let resolveGlobalInstructions!: (value: string) => void;
const templates = new Promise<PromptTemplate[]>((resolve) => {
resolveTemplates = resolve;
});
const globalInstructions = new Promise<string>((resolve) => {
resolveGlobalInstructions = resolve;
});
apiMock.loadPromptTemplates.mockReturnValueOnce(templates);
apiMock.getAiGlobalCustomInstructions.mockReturnValueOnce(globalInstructions);
const store = usePromptTemplateStore();
// Kick off init (simulates App.vue's fire-and-forget) but don't await yet
const initPromise = store.init();
// Consumer awaits ensureLoaded before reading globalInstructions (fix for #1)
const sendPromise = store.ensureLoaded().then((ok) => {
if (!ok) throw new Error("ensureLoaded should succeed");
return store.globalInstructions;
});
// Init hasn't resolved yet — globalInstructions should still be empty default
assert.equal(store.globalInstructions, "");
resolveTemplates([template]);
resolveGlobalInstructions("Use Unicode-aware comparisons.");
await initPromise;
const instructions = await sendPromise;
assert.equal(instructions, "Use Unicode-aware comparisons.");
assert.equal(store.globalInstructions, "Use Unicode-aware comparisons.");
assert.equal(store.isLoaded, true);
});
test("concurrent ensureLoaded calls with deferred init do not race", async () => {
// Two rapid callers both call ensureLoaded() before init resolves.
// Both must see the same resolved state without spawning duplicate loads
// or seeing inconsistent globalInstructions values.
let resolveTemplates!: (value: PromptTemplate[]) => void;
let resolveGlobalInstructions!: (value: string) => void;
const templates = new Promise<PromptTemplate[]>((resolve) => {
resolveTemplates = resolve;
});
const globalInstructions = new Promise<string>((resolve) => {
resolveGlobalInstructions = resolve;
});
apiMock.loadPromptTemplates.mockReturnValueOnce(templates);
apiMock.getAiGlobalCustomInstructions.mockReturnValueOnce(globalInstructions);
const store = usePromptTemplateStore();
// Two concurrent callers (simulating two rapid send() calls)
const callerA = store.ensureLoaded().then((ok) => (ok ? store.globalInstructions : "FAIL_A"));
const callerB = store.ensureLoaded().then((ok) => (ok ? store.globalInstructions : "FAIL_B"));
// Only one API call pair
assert.equal(apiMock.loadPromptTemplates.mock.calls.length, 1);
assert.equal(apiMock.getAiGlobalCustomInstructions.mock.calls.length, 1);
resolveTemplates([template]);
resolveGlobalInstructions("Concurrent-safe value.");
const [resultA, resultB] = await Promise.all([callerA, callerB]);
assert.equal(resultA, "Concurrent-safe value.");
assert.equal(resultB, "Concurrent-safe value.");
assert.equal(store.isLoaded, true);
});
+1
View File
@@ -25,6 +25,7 @@ pub mod mongo_cmd;
pub mod mq_cmd; pub mod mq_cmd;
pub mod nacos_cmd; pub mod nacos_cmd;
pub mod plugins; pub mod plugins;
pub mod prompt_template;
pub mod query; pub mod query;
pub mod query_cancel; pub mod query_cancel;
pub mod query_result_export; pub mod query_result_export;
+36
View File
@@ -0,0 +1,36 @@
use std::sync::Arc;
use tauri::State;
use dbx_core::prompt_template::PromptTemplate;
use super::connection::AppState;
#[tauri::command]
pub async fn load_prompt_templates(state: State<'_, Arc<AppState>>) -> Result<Vec<PromptTemplate>, String> {
state.storage.load_prompt_templates().await
}
#[tauri::command]
pub async fn save_prompt_template(
state: State<'_, Arc<AppState>>,
id: String,
name: String,
content: String,
) -> Result<PromptTemplate, String> {
state.storage.save_prompt_template(&id, &name, &content).await
}
#[tauri::command]
pub async fn delete_prompt_template(state: State<'_, Arc<AppState>>, id: String) -> Result<(), String> {
state.storage.delete_prompt_template(&id).await
}
#[tauri::command]
pub async fn get_ai_global_custom_instructions(state: State<'_, Arc<AppState>>) -> Result<String, String> {
state.storage.load_ai_global_custom_instructions().await
}
#[tauri::command]
pub async fn set_ai_global_custom_instructions(state: State<'_, Arc<AppState>>, content: String) -> Result<(), String> {
state.storage.save_ai_global_custom_instructions(&content).await
}
+5
View File
@@ -1073,6 +1073,11 @@ pub fn run() {
commands::ai_multi_config::set_default_ai_config, commands::ai_multi_config::set_default_ai_config,
commands::ai_multi_config::save_ai_config_item, commands::ai_multi_config::save_ai_config_item,
commands::ai_multi_config::delete_ai_config, commands::ai_multi_config::delete_ai_config,
commands::prompt_template::load_prompt_templates,
commands::prompt_template::save_prompt_template,
commands::prompt_template::delete_prompt_template,
commands::prompt_template::get_ai_global_custom_instructions,
commands::prompt_template::set_ai_global_custom_instructions,
commands::app_settings::load_desktop_settings, commands::app_settings::load_desktop_settings,
commands::app_settings::save_desktop_settings, commands::app_settings::save_desktop_settings,
commands::app_settings::complete_app_close, commands::app_settings::complete_app_close,