feat(plugins): add AI tools and consent-gated data access

This commit is contained in:
t8y2 committed 2026-09-25 01:56:26 +08:00
1 parent 1bc6f70cd8
commit 26a015cd86
71 files changed
+4613 -129

No files matched your search

@@ -12,6 +12,8 @@ import i18n from "@/i18n";
import { TooltipProvider } from "@/components/ui/tooltip";
import AiAssistant from "@/components/editor/AiAssistant.vue";
import { useSettingsStore } from "@/stores/settingsStore";
import { useConnectionStore } from "@/stores/connectionStore";
import type { ConnectionConfig } from "@/types/database";
vi.mock("@/lib/backend/api", async (importOriginal) => {
const actual = await importOriginal<Record<string, unknown>>();
@@ -22,6 +24,7 @@ vi.mock("@/lib/backend/api", async (importOriginal) => {
loadAiRuns: empty,
readUserSkills: empty,
loadAiConfigs: empty,
listPlugins: empty,
loadPromptTemplates: empty,
getAiGlobalCustomInstructions: () => Promise.resolve(""),
saveAiChatSelection: () => Promise.resolve(),
@@ -35,15 +38,16 @@ afterEach(() => {
while (cleanups.length) cleanups.pop()?.();
});
async function mountPanel(aiConfigLoaded: boolean): Promise<unknown[]> {
async function mountPanel(aiConfigLoaded: boolean, connection?: ConnectionConfig) {
const pinia = createPinia();
const errors: unknown[] = [];
const app = createApp({ render: () => h(TooltipProvider, () => h(AiAssistant)) });
const app = createApp({ render: () => h(TooltipProvider, () => h(AiAssistant, { connection })) });
app.use(pinia);
app.use(i18n);
app.config.errorHandler = (error) => errors.push(error);
app.config.warnHandler = () => {};
useSettingsStore(pinia).isAiConfigLoaded = aiConfigLoaded;
if (connection) useConnectionStore(pinia).connections = [connection];
const container = document.createElement("div");
document.body.append(container);
app.mount(container);
@@ -53,15 +57,32 @@ async function mountPanel(aiConfigLoaded: boolean): Promise<unknown[]> {
});
// Let mount-time loads settle so their failures surface here too.
await new Promise((resolve) => setTimeout(resolve, 20));
return errors;
return { errors, container };
}
describe("AiAssistant mount", () => {
it("opens when the AI config was loaded before the panel mounted", async () => {
expect((await mountPanel(true)).map(String)).toEqual([]);
expect((await mountPanel(true)).errors.map(String)).toEqual([]);
});
it("opens while the AI config is still loading", async () => {
expect((await mountPanel(false)).map(String)).toEqual([]);
expect((await mountPanel(false)).errors.map(String)).toEqual([]);
});
it.each(["plugin", "etcd"] as const)("hides database and schema selectors for %s connections", async (dbType) => {
const { errors, container } = await mountPanel(true, { id: "connection", name: "Connection", db_type: dbType, plugin_id: dbType === "plugin" ? "sample.plugin" : undefined, host: "localhost", port: 22, username: "", password: "" });
expect(errors.map(String)).toEqual([]);
const row = container.querySelector("[data-ai-composer-context-row]");
expect(row?.textContent).toContain("Connection");
expect(row?.textContent).not.toContain(i18n.global.t("editor.selectDatabase"));
expect(row?.classList.contains("ai-prompt-context-row--schema")).toBe(false);
});
it("keeps database and schema selectors for PostgreSQL", async () => {
const { errors, container } = await mountPanel(true, { id: "postgres", name: "PostgreSQL", db_type: "postgres", host: "localhost", port: 5432, username: "", password: "" });
expect(errors.map(String)).toEqual([]);
const row = container.querySelector("[data-ai-composer-context-row]");
expect(row?.textContent).toContain(i18n.global.t("editor.selectDatabase"));
expect(row?.classList.contains("ai-prompt-context-row--schema")).toBe(true);
});
});
@@ -0,0 +1,55 @@
// @vitest-environment happy-dom
import { createApp, nextTick, reactive } from "vue";
import { afterEach, describe, expect, it, vi } from "vitest";
import i18n from "@/i18n";
import AiToolApprovalCard from "../AiToolApprovalCard.vue";
import type { AiAgentStepApproval } from "@/lib/ai/aiAgentStepPresentation";
const cleanups: Array<() => void> = [];
function mountCard() {
const approval = reactive<AiAgentStepApproval>({ approvalId: "approval", sessionId: "session", pluginName: "Plugin", pluginTool: "write", connectionName: "Connection", args: { target: "orders" }, expiresAtMs: Date.now() + 1000, status: "pending" });
const onResolve = vi.fn();
const container = document.createElement("div");
document.body.append(container);
const app = createApp(AiToolApprovalCard, { approval, onResolve });
app.use(i18n);
app.mount(container);
cleanups.push(() => {
app.unmount();
container.remove();
});
return { approval, onResolve, container };
}
afterEach(() => {
while (cleanups.length) cleanups.pop()?.();
vi.useRealTimers();
});
describe("AiToolApprovalCard", () => {
it("expires without another backend event and removes its answer buttons", async () => {
vi.useFakeTimers();
const { container, onResolve } = mountCard();
expect(container.querySelectorAll("button")).toHaveLength(2);
await vi.advanceTimersByTimeAsync(1001);
await nextTick();
expect(container.querySelectorAll("button")).toHaveLength(0);
expect(container.textContent).toContain(i18n.global.t("ai.toolApproval.status.expired"));
expect(onResolve).not.toHaveBeenCalled();
});
it("forwards one-time approval and disables both buttons while submitting", async () => {
const { container, approval, onResolve } = mountCard();
container.querySelector("button")?.click();
expect(onResolve).toHaveBeenCalledWith(true);
approval.status = "submitting";
await nextTick();
expect([...container.querySelectorAll("button")].every((button) => button.disabled)).toBe(true);
approval.status = "denied";
await nextTick();
expect(container.querySelectorAll("button")).toHaveLength(0);
expect(container.textContent).toContain(i18n.global.t("ai.toolApproval.status.denied"));
});
});