From f1f4518b6780373acb57e5eb7fbe300d61f394c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E4=B8=AB=E8=AE=B2=E6=A2=B5?= Date: Fri, 3 Jul 2026 14:17:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai-table-mentions):=20=E5=A2=9E=E5=8A=A0AI?= =?UTF-8?q?=E8=A1=A8=E6=A0=BC=E6=8F=90=E5=8F=8A=E5=80=99=E9=80=89=E9=A1=B9?= =?UTF-8?q?=E7=9A=84=E8=BF=87=E6=BB=A4=E5=92=8C=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20(#2502)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入AI表格提及候选项的限制常量 - 实现过滤候选项的函数,支持根据表名过滤 - 添加单元测试以验证提及解析和候选项过滤的功能 --- .../src/components/editor/AiAssistant.vue | 67 +++++++++++++------ .../src/lib/__tests__/aiTableMentions.spec.ts | 45 +++++++++++++ apps/desktop/src/lib/aiTableMentions.ts | 8 +++ 3 files changed, 101 insertions(+), 19 deletions(-) create mode 100644 apps/desktop/src/lib/__tests__/aiTableMentions.spec.ts diff --git a/apps/desktop/src/components/editor/AiAssistant.vue b/apps/desktop/src/components/editor/AiAssistant.vue index 60fc7ea9d9..8631fb6f80 100644 --- a/apps/desktop/src/components/editor/AiAssistant.vue +++ b/apps/desktop/src/components/editor/AiAssistant.vue @@ -35,7 +35,7 @@ import { isSchemaAware } from "@/lib/databaseCapabilities"; import ExplainPlanViewer from "@/components/explain/ExplainPlanViewer.vue"; import { parseExplainResult, type ParsedExplainPlan } from "@/lib/explainPlan"; import { copyToClipboard } from "@/lib/clipboard"; -import { formatAiTableMention, parseAiTableMentions, type AiTableMention } from "@/lib/aiTableMentions"; +import { AI_TABLE_MENTION_CANDIDATE_LIMIT, AI_TABLE_MENTION_SCHEMA_LIMIT, filterAiTableMentionCandidates, formatAiTableMention, parseAiTableMentions, type AiTableMention } from "@/lib/aiTableMentions"; import { isAiPromptImeCompositionEvent, shouldSubmitAiPromptOnKeydown } from "@/lib/aiPromptKeyboard"; import { looksLikeActionProposal, containsChinese } from "@/lib/aiProposalDetect"; @@ -184,6 +184,7 @@ const mentionStart = ref(0); const mentionSelectedIndex = ref(0); const mentionCandidates = ref([]); const mentionCache = ref>({}); +const mentionListRef = ref(null); const selectedMentions = ref([]); let mentionTimer: ReturnType | undefined; let mentionRequestId = 0; @@ -574,30 +575,30 @@ async function loadMentionCandidates(query: string) { const schemas = mentionSchemaOrder(await listSchemas(props.tab.connectionId, props.tab.database)); const filteredSchemas = schemaPrefix ? schemas.filter((schema) => schema.toLowerCase().includes(schemaPrefix.toLowerCase())) : schemas; const results = await Promise.all( - filteredSchemas.slice(0, 8).map(async (schema) => { - const tables = await listTables(props.tab!.connectionId, props.tab!.database, schema, tableFilter || undefined, 20); - return filterMentionCandidates( + filteredSchemas.slice(0, AI_TABLE_MENTION_SCHEMA_LIMIT).map(async (schema) => { + const tables = await listTables(props.tab!.connectionId, props.tab!.database, schema, tableFilter || undefined, AI_TABLE_MENTION_CANDIDATE_LIMIT); + return filterAiTableMentionCandidates( tables.map((table) => mentionCandidateFromTable(table, schema)), tableFilter, - 20, + AI_TABLE_MENTION_CANDIDATE_LIMIT, ); }), ); - candidates = results.flat(); + candidates = filterAiTableMentionCandidates(results.flat(), "", AI_TABLE_MENTION_CANDIDATE_LIMIT); } else { const schema = props.tab.database || props.connection.database || "main"; - const tables = await listTables(props.tab.connectionId, props.tab.database, schema, tableFilter || undefined, 40); - candidates = filterMentionCandidates( + const tables = await listTables(props.tab.connectionId, props.tab.database, schema, tableFilter || undefined, AI_TABLE_MENTION_CANDIDATE_LIMIT); + candidates = filterAiTableMentionCandidates( tables.map((table) => mentionCandidateFromTable(table)), tableFilter, - 40, + AI_TABLE_MENTION_CANDIDATE_LIMIT, ); } if (requestId !== mentionRequestId) return; - mentionCache.value[key] = candidates.slice(0, 40); + mentionCache.value[key] = candidates.slice(0, AI_TABLE_MENTION_CANDIDATE_LIMIT); mentionCandidates.value = mentionCache.value[key]; - mentionSelectedIndex.value = 0; + setMentionSelectedIndex(0); } catch (e: unknown) { if (requestId !== mentionRequestId) return; const message = e instanceof Error ? e.message : String(e); @@ -636,9 +637,31 @@ function formatMentionTableType(tableType: string) { return t("ai.tableMentionTypes.table"); } -function filterMentionCandidates(candidates: AiMentionCandidate[], tableFilter: string, limit: number): AiMentionCandidate[] { - const normalizedFilter = tableFilter.toLowerCase(); - return candidates.filter((candidate) => !normalizedFilter || candidate.name.toLowerCase().includes(normalizedFilter)).slice(0, limit); +function setMentionSelectedIndex(index: number, keepVisible = true) { + mentionSelectedIndex.value = Math.max(0, Math.min(index, Math.max(mentionCandidates.value.length - 1, 0))); + if (keepVisible) scrollMentionSelectedIntoView(); +} + +function scrollMentionSelectedIntoView() { + nextTick(() => { + const list = mentionListRef.value; + if (!list) return; + const item = list.querySelector(`[data-mention-index="${mentionSelectedIndex.value}"]`); + if (!item) return; + + const listRect = list.getBoundingClientRect(); + const itemRect = item.getBoundingClientRect(); + const itemTop = itemRect.top - listRect.top + list.scrollTop; + const itemBottom = itemTop + itemRect.height; + const visibleTop = list.scrollTop; + const visibleBottom = visibleTop + list.clientHeight; + + if (itemTop < visibleTop) { + list.scrollTop = itemTop; + } else if (itemBottom > visibleBottom) { + list.scrollTop = itemBottom - list.clientHeight; + } + }); } function refreshMentionState() { @@ -656,6 +679,11 @@ function refreshMentionState() { }, 120); } +function onPromptKeyup(event: KeyboardEvent) { + if (["ArrowDown", "ArrowUp", "Enter", "Tab", "Escape"].includes(event.key)) return; + refreshMentionState(); +} + function insertMention(candidate: AiMentionCandidate) { const textarea = promptTextareaRef.value; const cursor = textarea?.selectionStart ?? prompt.value.length; @@ -677,12 +705,12 @@ function onPromptKeydown(event: KeyboardEvent) { if (mentionOpen.value) { if (event.key === "ArrowDown") { event.preventDefault(); - mentionSelectedIndex.value = Math.min(mentionSelectedIndex.value + 1, Math.max(mentionCandidates.value.length - 1, 0)); + setMentionSelectedIndex(mentionSelectedIndex.value + 1); return; } if (event.key === "ArrowUp") { event.preventDefault(); - mentionSelectedIndex.value = Math.max(mentionSelectedIndex.value - 1, 0); + setMentionSelectedIndex(mentionSelectedIndex.value - 1); return; } if ((event.key === "Enter" || event.key === "Tab") && mentionCandidates.value[mentionSelectedIndex.value]) { @@ -1291,15 +1319,16 @@ async function openExternalUrl(url: string) {
{{ t("ai.tableMentionEmpty") }}
-
+