Skip to content

armgit / dbx

Public Imported from GitHub
0

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。

fix(ai): include schema comments in prompt context

t8y266fc5e2b0524
diff --git a/apps/desktop/src/lib/ai.ts b/apps/desktop/src/lib/ai.ts
index a704495f0d..37cfb5afef 100644
--- a/apps/desktop/src/lib/ai.ts
+++ b/apps/desktop/src/lib/ai.ts
@@ -26,6 +26,7 @@ export interface AiSchemaTable {
schema?: string;
name: string;
tableType: string;
+ comment?: string | null;
columns: ColumnInfo[];
indexes?: IndexInfo[];
foreignKeys?: ForeignKeyInfo[];
@@ -187,6 +188,9 @@ function buildBasePromptLines(isZh: boolean): string[] {
isZh
? "下面的 Schema 上下文已包含表、列、索引和外键信息,直接使用即可。不要查询 information_schema 或系统表来获取结构信息。"
: "The schema context below already contains tables, columns, indexes, and foreign keys — use it directly. Do NOT query information_schema or system tables.",
+ isZh
+ ? "表注释和列注释是语义别名;当用户用中文业务名描述表或字段时,优先根据注释匹配真实表名和字段名。"
+ : "Table and column comments are semantic aliases; when the user describes tables or fields by business names, prefer matching those comments to the real table and column names.",
isZh
? "当用户要求分析或查看某个表时,生成 SELECT 查询获取数据,而不是查询元数据。"
: "When the user asks to 'analyze' or 'look at' a table, generate a SELECT query to retrieve data, not a metadata query.",
@@ -242,6 +246,8 @@ function formatSchema(context: AiContext): string {
.map((table) => {
const name = table.schema ? `${table.schema}.${table.name}` : table.name;
const lines: string[] = [`${name} (${table.tableType})`];
+ const tableComment = table.comment?.trim();
+ if (tableComment) lines.push(` Comment: ${tableComment}`);
for (const column of table.columns) {
const flags = [
@@ -252,7 +258,10 @@ function formatSchema(context: AiContext): string {
]
.filter(Boolean)
.join(", ");
- lines.push(` - ${column.name}: ${column.data_type}${flags ? ` (${flags})` : ""}`);
+ const columnComment = column.comment?.trim();
+ lines.push(
+ ` - ${column.name}: ${column.data_type}${flags ? ` (${flags})` : ""}${columnComment ? ` -- ${columnComment}` : ""}`,
+ );
}
if (table.indexes?.length) {
@@ -292,10 +301,12 @@ export async function buildAiContext(
api.listIndexes(tab.connectionId, tab.database, s, tName).catch(() => [] as IndexInfo[]),
api.listForeignKeys(tab.connectionId, tab.database, s, tName).catch(() => [] as ForeignKeyInfo[]),
]);
+ const tableComment = await loadTableComment(tab.connectionId, tab.database, s, tName).catch(() => undefined);
tables.push({
schema: tab.tableMeta.schema,
name: tName,
tableType: "TABLE",
+ comment: tableComment,
columns: tab.tableMeta.columns.slice(0, maxColumnsPerTable),
indexes,
foreignKeys,
@@ -333,6 +344,7 @@ export async function buildAiContext(
schema: schema === tab.database && !isSchemaAware(connection.db_type) ? undefined : schema,
name: table.name,
tableType: table.table_type,
+ comment: table.comment,
columns: columns.slice(0, maxColumnsPerTable),
indexes,
foreignKeys,
@@ -375,21 +387,33 @@ async function loadMentionedTableContext(
maxColumnsPerTable: number,
): Promise<AiSchemaTable | undefined> {
const schema = await resolveMentionedTableSchema(tab, connection, mention);
- const [columns, indexes, foreignKeys] = await Promise.all([
+ const [columns, indexes, foreignKeys, tableComment] = await Promise.all([
api.getColumns(tab.connectionId, tab.database, schema, mention.table),
api.listIndexes(tab.connectionId, tab.database, schema, mention.table).catch(() => [] as IndexInfo[]),
api.listForeignKeys(tab.connectionId, tab.database, schema, mention.table).catch(() => [] as ForeignKeyInfo[]),
+ loadTableComment(tab.connectionId, tab.database, schema, mention.table).catch(() => undefined),
]);
return {
schema: schema === tab.database && !isSchemaAware(connection.db_type) ? undefined : schema,
name: mention.table,
tableType: "TABLE",
+ comment: tableComment,
columns: columns.slice(0, maxColumnsPerTable),
indexes,
foreignKeys,
};
}
+async function loadTableComment(
+ connectionId: string,
+ database: string,
+ schema: string,
+ tableName: string,
+): Promise<string | undefined> {
+ const tables = await api.listTables(connectionId, database, schema, tableName, 10);
+ return tables.find((table) => table.name.toLowerCase() === tableName.toLowerCase())?.comment?.trim() || undefined;
+}
+
async function resolveMentionedTableSchema(
tab: QueryTab,
connection: ConnectionConfig,
diff --git a/packages/app-tests/aiSchemaComments.test.ts b/packages/app-tests/aiSchemaComments.test.ts
new file mode 100644
index 0000000000..c45c72bf4e
--- /dev/null
+++ b/packages/app-tests/aiSchemaComments.test.ts
@@ -0,0 +1,48 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { buildSystemPrompt, type AiContext } from "../../apps/desktop/src/lib/ai.ts";
+
+const BASE_CONTEXT: AiContext = {
+ connectionName: "local",
+ databaseType: "mysql",
+ database: "app",
+ currentSql: "",
+ tables: [
+ {
+ name: "users",
+ tableType: "BASE TABLE",
+ comment: "用户表",
+ columns: [
+ {
+ name: "id",
+ data_type: "bigint",
+ is_nullable: false,
+ column_default: null,
+ is_primary_key: true,
+ extra: null,
+ comment: "用户ID",
+ },
+ {
+ name: "nickname",
+ data_type: "varchar(64)",
+ is_nullable: true,
+ column_default: null,
+ is_primary_key: false,
+ extra: null,
+ comment: "用户昵称",
+ },
+ ],
+ },
+ ],
+ truncated: false,
+};
+
+test("AI schema context includes table and column comments", () => {
+ const prompt = buildSystemPrompt("generate", BASE_CONTEXT);
+
+ assert.match(prompt, /users \(BASE TABLE\)/);
+ assert.match(prompt, /Comment: 用户表/);
+ assert.match(prompt, /- id: bigint \(PK, NOT NULL\) -- 用户ID/);
+ assert.match(prompt, /- nickname: varchar\(64\) \(nullable\) -- 用户昵称/);
+});