25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(ai): bound schema context and result preview to prevent oversized AI payloads (#2496)
Abeautifulsnowd6ef9f140aaa
diff --git a/apps/desktop/src/lib/ai.ts b/apps/desktop/src/lib/ai.tsindex c47dd41b09..c33a4b5aa4 100644--- a/apps/desktop/src/lib/ai.ts+++ b/apps/desktop/src/lib/ai.ts@@ -349,9 +349,11 @@ function formatSchema(context: AiContext): string {.join("\n\n");}-export async function buildAiContext(tab: QueryTab, connection: ConnectionConfig, options: { maxTables?: number; maxColumnsPerTable?: number; mentionedTables?: AiTableMention[] } = {}): Promise<AiContext> {+export async function buildAiContext(tab: QueryTab, connection: ConnectionConfig, options: { maxTables?: number; maxColumnsPerTable?: number; maxIndexesPerTable?: number; maxFksPerTable?: number; mentionedTables?: AiTableMention[] } = {}): Promise<AiContext> {const maxTables = options.maxTables ?? 50;const maxColumnsPerTable = options.maxColumnsPerTable ?? 40;+ const maxIndexesPerTable = options.maxIndexesPerTable ?? 10;+ const maxFksPerTable = options.maxFksPerTable ?? 10;const databaseType = aiDatabaseTypeForConnection(connection);const tables: AiSchemaTable[] = [];const tableKeys = new Set<string>();@@ -371,8 +373,8 @@ export async function buildAiContext(tab: QueryTab, connection: ConnectionConfigtableType: "TABLE",comment: tableComment,columns: tab.tableMeta.columns.slice(0, maxColumnsPerTable),- indexes,- foreignKeys,+ indexes: indexes.slice(0, maxIndexesPerTable),+ foreignKeys: foreignKeys.slice(0, maxFksPerTable),});tableKeys.add(aiTableMentionKey(tab.tableMeta.schema, tName));truncated = tab.tableMeta.columns.length > maxColumnsPerTable;@@ -381,7 +383,7 @@ export async function buildAiContext(tab: QueryTab, connection: ConnectionConfigfor (const mention of options.mentionedTables ?? []) {const key = aiTableMentionKey(mention.schema, mention.table);if (tableKeys.has(key)) continue;- const entry = await loadMentionedTableContext(tab, connection, mention, maxColumnsPerTable).catch(() => undefined);+ const entry = await loadMentionedTableContext(tab, connection, mention, maxColumnsPerTable, maxIndexesPerTable, maxFksPerTable).catch(() => undefined);if (!entry) continue;tableKeys.add(aiTableMentionKey(entry.schema, entry.name));tables.push(entry);@@ -443,8 +445,8 @@ export async function buildAiContext(tab: QueryTab, connection: ConnectionConfigtableType: table.table_type,comment: table.comment,columns: columns.slice(0, maxColumnsPerTable),- indexes,- foreignKeys,+ indexes: indexes.slice(0, maxIndexesPerTable),+ foreignKeys: foreignKeys.slice(0, maxFksPerTable),_truncatedCols: columns.length > maxColumnsPerTable,})),),@@ -479,7 +481,7 @@ export async function buildAiContext(tab: QueryTab, connection: ConnectionConfig};}-async function loadMentionedTableContext(tab: QueryTab, connection: ConnectionConfig, mention: AiTableMention, maxColumnsPerTable: number): Promise<AiSchemaTable | undefined> {+async function loadMentionedTableContext(tab: QueryTab, connection: ConnectionConfig, mention: AiTableMention, maxColumnsPerTable: number, maxIndexesPerTable: number, maxFksPerTable: number): Promise<AiSchemaTable | undefined> {const databaseType = aiDatabaseTypeForConnection(connection);const schema = await resolveMentionedTableSchema(tab, connection, mention);const [columns, indexes, foreignKeys, tableComment] = await Promise.all([@@ -494,8 +496,8 @@ async function loadMentionedTableContext(tab: QueryTab, connection: ConnectionCotableType: "TABLE",comment: tableComment,columns: columns.slice(0, maxColumnsPerTable),- indexes,- foreignKeys,+ indexes: indexes.slice(0, maxIndexesPerTable),+ foreignKeys: foreignKeys.slice(0, maxFksPerTable),};}@@ -548,8 +550,15 @@ function extractLastError(result?: QueryResult): string | undefined {function formatResultPreview(result?: QueryResult): string | undefined {if (!result || result.columns.includes("Error") || !result.rows.length) return undefined;+ const MAX_VALUE_CHARS = 200;const rows = result.rows.slice(0, 5).map((row) => {- return result.columns.map((column, index) => `${column}=${JSON.stringify(row[index] ?? null)}`).join(", ");+ return result.columns+ .map((column, index) => {+ const raw = JSON.stringify(row[index] ?? null);+ const val = raw.length > MAX_VALUE_CHARS ? raw.slice(0, MAX_VALUE_CHARS) + "…" : raw;+ return `${column}=${val}`;+ })+ .join(", ");});return rows.join("\n");}