feat(ai): drag scoped table references into chat

This commit is contained in:
Allen authored and GitHub committed 2026-08-21 13:03:14 +08:00
1 parent cc78d4f528
commit 09b326558f
6 files changed
+197 -4

No files matched your search

@@ -0,0 +1,67 @@
import { describe, expect, it } from "vitest";
import { aiTableMentionFromTableReference } from "@/lib/ai/aiTableReferenceDrop";
import { createTableReferencePayload } from "@/lib/editor/queryEditorTableDrop";
describe("ai table reference drop", () => {
const context = { connectionId: "conn-1", database: "app-db" };
it("maps a table payload to a table mention", () => {
const payload = createTableReferencePayload({
connectionId: "conn-1",
database: "app-db",
schema: "public",
tableName: "users",
databaseType: "postgres",
});
expect(aiTableMentionFromTableReference(payload, context)).toEqual({
raw: "@public.users",
schema: "public",
table: "users",
});
});
it("quotes mention parts that are not simple identifiers", () => {
const payload = createTableReferencePayload({
connectionId: "conn-1",
database: "app-db",
tableName: "order items",
databaseType: "mysql",
});
expect(aiTableMentionFromTableReference(payload, context)).toEqual({
raw: '@"order items"',
schema: undefined,
table: "order items",
});
});
it("ignores database references", () => {
const payload = createTableReferencePayload({
connectionId: "conn-1",
database: "app-db",
referenceType: "database",
databaseType: "mysql",
});
expect(aiTableMentionFromTableReference(payload, context)).toBeNull();
});
it("ignores column references", () => {
const payload = createTableReferencePayload({
connectionId: "conn-1",
database: "app-db",
schema: "public",
tableName: "users",
columnName: "email",
databaseType: "postgres",
});
expect(aiTableMentionFromTableReference(payload, context)).toBeNull();
});
it("ignores empty payloads", () => {
expect(aiTableMentionFromTableReference(null, context)).toBeNull();
expect(aiTableMentionFromTableReference(undefined, context)).toBeNull();
});
});
@@ -0,0 +1,43 @@
import { formatAiTableMention, type AiTableMention } from "@/lib/ai/aiTableMentions";
import type { QueryEditorTableReferenceDropDetail, QueryEditorTableReferencePayload } from "@/lib/editor/queryEditorTableDrop";
/** Selector marking the AI assistant panel root as a table-reference drop target. */
export const AI_ASSISTANT_TABLE_DROP_ROOT_SELECTOR = "[data-ai-assistant-root]";
export interface AiTableReferenceDropContext {
connectionId?: string;
database?: string;
}
export interface AiTableReferenceDropHandlerOptions {
context: AiTableReferenceDropContext;
assistantRoot: Element | null | undefined;
elementFromPoint: (x: number, y: number) => Element | null;
onMention: (mention: AiTableMention, payload: QueryEditorTableReferencePayload) => void;
}
/**
* Maps a sidebar table-reference drag payload to an AI table mention chip.
* Only plain table/view references become mentions; database and column
* references are not representable as table mentions and return null.
*/
export function aiTableMentionFromTableReference(payload: QueryEditorTableReferencePayload | null | undefined, context: AiTableReferenceDropContext): AiTableMention | null {
if (!payload || payload.referenceType === "database" || payload.columnName) return null;
if (!context.connectionId || context.database == null || payload.connectionId !== context.connectionId || payload.database !== context.database) return null;
const table = payload.tableName;
if (!table) return null;
const schema = payload.schema;
return { raw: formatAiTableMention(schema, table), schema, table };
}
export function handleAiTableReferenceDropEvent(event: Event, options: AiTableReferenceDropHandlerOptions): boolean {
if (!(event instanceof CustomEvent)) return false;
const detail = event.detail as QueryEditorTableReferenceDropDetail | undefined;
if (!detail?.payload) return false;
const target = options.elementFromPoint(detail.clientX, detail.clientY);
if (!target || !options.assistantRoot?.contains(target)) return false;
const mention = aiTableMentionFromTableReference(detail.payload, options.context);
if (!mention) return false;
options.onMention(mention, detail.payload);
return true;
}
@@ -24,7 +24,16 @@ export interface QueryEditorTableReferenceDropDetail {
let activeTableReferencePayload: QueryEditorTableReferencePayload | null = null;
export function createTableReferencePayload(options: { connectionId?: string; database?: string; schema?: string; tableName?: string; columnName?: string; referenceType?: "database" | "table" | "column"; databaseType?: DatabaseType; driverProfile?: string }): QueryEditorTableReferencePayload | null {
export function createTableReferencePayload(options: {
connectionId?: string;
database?: string;
schema?: string;
tableName?: string;
columnName?: string;
referenceType?: "database" | "table" | "column";
databaseType?: DatabaseType;
driverProfile?: string;
}): QueryEditorTableReferencePayload | null {
if (!options.connectionId || options.database == null) return null;
const referenceType = options.referenceType ?? (options.columnName ? "column" : "table");
if (referenceType !== "database" && !options.tableName) return null;