feat(ai): add plugin recommendations and agent context

This commit is contained in:
二丫讲梵 authored and t8y2 committed 2026-09-27 12:04:09 +08:00
1 parent 1b6017b9c5
commit e6edd02bbe
43 files changed
+1210 -118

No files matched your search

@@ -90,6 +90,7 @@ import {
type AiAction,
type AiActionSelection,
type AiAssistantMode,
type AiContext,
type AiContextTarget,
type AiCsvFileContext,
type AiTextAttachmentEncoding,
@@ -187,7 +188,8 @@ import { buildAiConversationSearchIndex, filterAiConversationSearchIndex } from
import AiAttachmentCard from "@/components/editor/AiAttachmentCard.vue";
import AiToolApprovalCard from "@/components/editor/AiToolApprovalCard.vue";
import { resolveAiMessageCopyText } from "@/lib/ai/aiMessageCopy";
import { buildPluginAiRequest, pluginContextFromMessages, pluginContextText, streamPluginAiConversation, type AiPluginContext, type AiPluginConversationRequest } from "@/lib/ai/aiPluginConversation";
import { buildPluginAiRequest, createPluginAiConversation, pluginComposerConnectionLabel, pluginContextConnectionId, pluginContextFromMessages, pluginContextText, streamPluginAiConversation, type AiPluginContext, type AiPluginConversationRequest } from "@/lib/ai/aiPluginConversation";
import type { PluginAiRecommendationHostUpdate } from "@/lib/plugins/pluginHostBridge";
const { t } = useI18n();
const AiChartRenderer = defineAsyncComponent({
@@ -291,6 +293,7 @@ const props = defineProps<{
tab?: QueryTab;
connection?: ConnectionConfig;
maximized?: boolean;
pluginRecommendations?: PluginAiRecommendationHostUpdate;
}>();
// Every AI-initiated action carries the *target* it must run against: the
@@ -356,6 +359,27 @@ const boundConnectionId = computed(() => conversationBinding.value.connectionId)
const boundConnection = computed(() => (boundConnectionId.value ? connectionStore.getConfig(boundConnectionId.value) : undefined));
const boundDatabase = computed(() => conversationBinding.value.database);
const boundSchema = computed(() => conversationBinding.value.schema);
const pluginContextConnection = computed(() => {
const contextConnectionId = pluginContext.value ? pluginContextConnectionId(pluginContext.value) : undefined;
return boundConnection.value ?? (contextConnectionId ? connectionStore.getConfig(contextConnectionId) : undefined);
});
const pluginComposerConnectionName = computed(() => {
const context = pluginContext.value;
if (!context) return "";
return pluginComposerConnectionLabel(context, pluginContextConnection.value?.name, activeConversation.value?.connectionName);
});
function canUsePluginAgentMode(context = pluginContext.value, connection = boundConnection.value): boolean {
return !context || !!connection;
}
watch(
[pluginContext, boundConnection],
([context, connection]) => {
if (context && !connection && assistantMode.value === "agent") assistantMode.value = "ask";
},
{ immediate: true },
);
// `immediate` runs this during setup whenever the AI config finished loading
// before the panel mounted (the usual case: the app loads it at startup), and
@@ -1663,6 +1687,7 @@ function openCodeSnapshot(seg: { content: string; lang: string }) {
}
const showActionButtons = computed(() => {
if (pluginContext.value) return false;
if (!boundConnection.value) return true;
return !isVectorDbType(boundConnection.value.db_type);
});
@@ -1677,6 +1702,7 @@ const modeActionTriggerLabel = computed(() => {
});
function switchModeActionTab(mode: "ask" | "agent") {
if (mode === "agent" && !canUsePluginAgentMode()) return;
activeAction.value = resolveDefaultActionSelection(mode);
if (assistantMode.value !== mode) {
// Set the mode after the action so the tab label and picker stay aligned.
@@ -3245,9 +3271,9 @@ async function send() {
// database. When it still matches, dropping it would silently discard what
// the user attached alongside the affirmative.
const confirmationRetargets = !!confirmationTarget && !sameConversationBinding(runBinding, conversationBinding.value);
const connection = runPluginContext ? undefined : runBinding.connectionId ? connectionStore.getConfig(runBinding.connectionId) : undefined;
const connection = runBinding.connectionId ? connectionStore.getConfig(runBinding.connectionId) : undefined;
const tab = runPluginContext ? undefined : aiContextTargetFor(runBinding, props.tab);
const runSourceName = runPluginContext?.pluginName ?? connection?.name ?? "";
const runSourceName = connection?.name ?? runPluginContext?.pluginName ?? "";
if (!runPluginContext && (!connection || !tab)) {
clearPendingWriteGrant();
return;
@@ -3467,7 +3493,7 @@ async function send() {
if (autoSendVisible) scrollToBottom({ force: true });
const requestedSelection: AiActionSelection = auto ? auto.action : activeAction.value;
const requestedMode: AiAssistantMode = runPluginContext ? "ask" : auto ? auto.mode : assistantMode.value;
const requestedMode: AiAssistantMode = auto ? auto.mode : assistantMode.value;
// A confirmed-write turn (the ✅ reply, or the segment that resumes an
// `awaiting_write_confirmation` run) is a continuation of the pending proposal,
// not a new user request: its reply text is component copy, so the Auto router
@@ -3482,8 +3508,8 @@ async function send() {
// router entirely, so their behavior is unchanged (#9118).
let requestedAction: AiAction;
if (runPluginContext) {
// A plugin conversation carries its own data snapshot: the Auto router must
// not classify the prompt, and the task contract stays host-owned.
// Plugin requests use the general action; the Agent decides which live
// plugin tools are needed from their advertised definitions.
requestedAction = "general";
} else if (!isAutoActionSelection(requestedSelection)) {
requestedAction = requestedSelection;
@@ -3741,7 +3767,36 @@ async function send() {
}
if (runIsVisible()) scrollToBottom();
};
if (runPluginContext) {
if (runPluginContext && requestedMode === "agent" && connection) {
if (!generationCanContinue()) return;
if (runIsVisible()) generationStatus.value = { ...generationStatus.value, phase: "waiting_model" };
const context: AiContext = {
connectionId: connection.id,
connectionName: connection.name,
databaseType: "plugin",
database: "",
currentSql: "",
tables: [],
sqlFiles: [],
truncated: false,
};
const instruction = [text, `Plugin: ${runPluginContext.pluginName}`, `Recommendation context: ${runPluginContext.title}`, "Use the connected plugin tools to retrieve the current state before answering."].filter(Boolean).join("\n\n");
await runAgentStream(
{
config: activeConfig,
action: requestedAction,
mode: requestedMode,
instruction,
taskContractUserRequest: text,
context,
inlineImages: imageAttachments.map(({ mediaType, data }) => ({ mediaType, data })),
},
history,
onEvent,
sessionId,
customPromptContext,
);
} else if (runPluginContext) {
if (!generationCanContinue()) return;
if (runIsVisible()) generationStatus.value = { ...generationStatus.value, phase: "waiting_model" };
const request = buildPluginAiRequest(
@@ -4725,7 +4780,7 @@ function selectConversation(conv: AiConversation) {
const activeRun = backgroundAiRunsEnabled ? desktopAiRun<ChatMessage>(conv.id) : undefined;
messages.value = activeRun?.messages ?? chatMessagesFromConversation(conv);
if (pluginContext.value) {
assistantMode.value = "ask";
assistantMode.value = pluginContext.value.mode === "agent" && !boundConnection.value ? "ask" : (pluginContext.value.mode ?? "ask");
activeAction.value = "general";
}
unreadConversations.delete(conv.id);
@@ -5266,12 +5321,21 @@ function openPluginConversation(request: AiPluginConversationRequest) {
defaultModeInitialized = true;
startNewChat();
draftPluginContext.value = request.context;
assistantMode.value = "ask";
const pluginConnectionId = pluginContextConnectionId(request.context);
const pluginConnection = pluginConnectionId ? connectionStore.getConfig(pluginConnectionId) : undefined;
draftBinding.value = pluginConnection ? { connectionId: pluginConnection.id, connectionName: pluginConnection.name, database: "", schema: undefined } : null;
assistantMode.value = request.mode === "agent" && !pluginConnection ? "ask" : (request.mode ?? request.context.mode ?? "ask");
activeAction.value = "general";
setPrompt(request.prompt, true);
if (request.send) void send();
}
function sendPluginRecommendation(item: { id: string; label: string; prompt: string }) {
const update = props.pluginRecommendations;
if (!update || !update.items.some((candidate) => candidate.id === item.id)) return;
openPluginConversation(createPluginAiConversation({ id: update.pluginId, name: update.pluginName }, { title: item.label, prompt: item.prompt, context: update.context, send: true, mode: "agent" }));
}
function setPrompt(text: string, fromPlugin = false) {
if (!fromPlugin && pluginContext.value) startNewChat();
prompt.value = text;
@@ -5496,10 +5560,34 @@ async function openExternalUrl(url: string) {
<div v-if="messages.length === 0" class="flex-1 min-h-0 flex flex-col items-center justify-center text-center text-muted-foreground">
<Bot class="h-10 w-10 mb-3 opacity-30" />
<p class="text-sm">{{ t(pluginContext ? "ai.pluginWelcome" : "ai.welcome") }}</p>
<div v-if="pluginRecommendations?.items.length" class="mt-5 flex max-w-[95%] flex-wrap justify-center gap-2">
<button
v-for="recommendation in pluginRecommendations.items"
:key="recommendation.id"
type="button"
class="rounded-full border border-border/80 bg-background px-3 py-1.5 text-xs text-muted-foreground transition-colors hover:border-primary/50 hover:bg-primary/5 hover:text-foreground"
:title="recommendation.prompt"
@click="sendPluginRecommendation(recommendation)"
>
{{ recommendation.label }}
</button>
</div>
</div>
<div v-else class="relative min-h-0 flex-1">
<ScrollArea ref="scrollRef" class="ai-message-scroll h-full overflow-hidden">
<div v-else class="relative flex min-h-0 flex-1 flex-col">
<ScrollArea ref="scrollRef" class="ai-message-scroll min-h-0 flex-1 overflow-hidden">
<div class="flex flex-col gap-3 p-3">
<div v-if="pluginRecommendations?.items.length" class="flex shrink-0 flex-wrap justify-center gap-2 border-b border-border/60 px-3 py-2" data-ai-plugin-recommendations>
<button
v-for="recommendation in pluginRecommendations.items"
:key="`history:${recommendation.id}`"
type="button"
class="rounded-full border border-border/80 bg-background px-3 py-1.5 text-xs text-muted-foreground transition-colors hover:border-primary/50 hover:bg-primary/5 hover:text-foreground"
:title="recommendation.prompt"
@click="sendPluginRecommendation(recommendation)"
>
{{ recommendation.label }}
</button>
</div>
<template v-for="(msg, i) in visibleMessages" :key="i">
<div v-if="awayUpdatesBaselineIndex >= 0 && i === awayUpdatesBaselineIndex" class="mb-1 flex items-center gap-2 py-0.5" role="separator" :aria-label="t('ai.awayUpdatesDivider')">
<span class="h-px flex-1 bg-primary/25" />
@@ -5906,11 +5994,12 @@ async function openExternalUrl(url: string) {
<div class="resize-handle" @mousedown="startResize"></div>
<div class="px-2 pb-2 pt-1">
<div data-ai-composer-context-row :class="['ai-prompt-context-row mb-1 flex min-w-0 items-center gap-x-1 text-xs text-foreground/80', showAiSchemaSelector && 'ai-prompt-context-row--schema', compactContextControls && 'ai-prompt-context-row--compact']">
<details v-if="pluginContext" class="min-w-0 flex-1" data-ai-plugin-context>
<summary class="cursor-pointer truncate">{{ pluginContext.pluginName }} · {{ pluginContext.title }}</summary>
<pre class="max-h-56 overflow-auto whitespace-pre-wrap break-all p-2 text-[11px]">{{ pluginContextText(pluginContext) }}</pre>
</details>
<template v-else-if="connectionStore.connections.length">
<div v-if="pluginContext && !connectionStore.connections.length" class="flex min-w-0 max-w-[14rem] items-center gap-1" data-ai-plugin-context :title="pluginContext.title">
<ConnectionIcon v-if="pluginContextConnection" :connection="pluginContextConnection" class="h-3 w-3 shrink-0" />
<Server v-else class="h-3 w-3 shrink-0" />
<span class="truncate">{{ pluginComposerConnectionName }}</span>
</div>
<template v-if="connectionStore.connections.length">
<ConnectionIcon v-if="boundConnection" :connection="boundConnection" class="h-3 w-3 shrink-0" />
<Server v-else class="h-3 w-3 shrink-0" />
<ConnectionTreeSelect
@@ -6272,12 +6361,7 @@ async function openExternalUrl(url: string) {
</TooltipContent>
</Tooltip>
<!-- Combined mode + action selector -->
<span v-if="pluginContext" class="ai-mode-static-trigger flex shrink-0 items-center gap-1 text-xs text-muted-foreground" :title="t('ai.modes.ask')">
<MessageSquarePlus class="h-3 w-3" aria-hidden="true" />
<span class="ai-mode-action-label" aria-hidden="true">{{ t("ai.modes.ask") }}</span>
<span class="sr-only">{{ t("ai.modes.ask") }}</span>
</span>
<Popover v-else v-model:open="modeActionOpen">
<Popover v-model:open="modeActionOpen">
<PopoverTrigger as-child>
<button type="button" class="ai-mode-action-trigger flex shrink-0 items-center gap-1 whitespace-nowrap rounded-[6px] border px-2 py-0.5 text-[11px] text-muted-foreground hover:bg-muted hover:text-foreground" :aria-label="modeActionTriggerLabel" :title="modeActionTriggerLabel">
<component :is="modeIcon" class="h-3 w-3" />
@@ -31,6 +31,34 @@ function bodyOf(fnSignature: string): string {
}
describe("AI conversation owns its connection binding (#9902)", () => {
it("renders plugin recommendation chips and sends the selected snapshot immediately", () => {
expect(source).toContain("pluginRecommendations?: PluginAiRecommendationHostUpdate");
expect(source).toContain('v-for="recommendation in pluginRecommendations.items"');
expect(source).toContain('@click="sendPluginRecommendation(recommendation)"');
expect(source).toContain("`history:${recommendation.id}`");
const recommendationStart = source.indexOf("function sendPluginRecommendation(");
expect(source.slice(recommendationStart, recommendationStart + 900)).toContain("send: true");
expect(source.slice(recommendationStart, recommendationStart + 900)).toContain("context: update.context");
});
it("keeps the connection selector available after a plugin recommendation is selected", () => {
const contextStart = source.lastIndexOf("data-ai-composer-context-row");
const contextEnd = source.indexOf("data-ai-composer-actions", contextStart);
const contextMarkup = source.slice(contextStart, contextEnd);
expect(contextMarkup).toContain('v-if="pluginContext && !connectionStore.connections.length"');
expect(contextMarkup).toContain("data-ai-plugin-context");
expect(contextMarkup).toContain('v-if="connectionStore.connections.length"');
expect(contextMarkup).toContain("<ConnectionTreeSelect");
expect(contextMarkup).toContain('@update:model-value="(v) => changeConnection(v)"');
});
it("does not expose Agent mode for a plugin conversation without a connection", () => {
expect(source).toContain("function canUsePluginAgentMode");
expect(source).toContain('if (mode === "agent" && !canUsePluginAgentMode()) return;');
expect(source).toContain('request.mode === "agent" && !pluginConnection ? "ask"');
});
it("rebinding the conversation never rewrites the editor tab or the global active connection", () => {
const body = bodyOf("async function changeConnection(connectionId: string)");
@@ -14,6 +14,7 @@ import AiAssistant from "@/components/editor/AiAssistant.vue";
import { useSettingsStore } from "@/stores/settingsStore";
import { useConnectionStore } from "@/stores/connectionStore";
import type { ConnectionConfig } from "@/types/database";
import type { PluginAiRecommendationHostUpdate } from "@/lib/plugins/pluginHostBridge";
vi.mock("@/lib/backend/api", async (importOriginal) => {
const actual = await importOriginal<Record<string, unknown>>();
@@ -38,10 +39,10 @@ afterEach(() => {
while (cleanups.length) cleanups.pop()?.();
});
async function mountPanel(aiConfigLoaded: boolean, connection?: ConnectionConfig, configureSettings?: (settings: ReturnType<typeof useSettingsStore>) => void) {
async function mountPanel(aiConfigLoaded: boolean, connection?: ConnectionConfig, configureSettings?: (settings: ReturnType<typeof useSettingsStore>) => void, pluginRecommendations?: PluginAiRecommendationHostUpdate) {
const pinia = createPinia();
const errors: unknown[] = [];
const app = createApp({ render: () => h(TooltipProvider, () => h(AiAssistant, { connection })) });
const app = createApp({ render: () => h(TooltipProvider, () => h(AiAssistant, { connection, pluginRecommendations })) });
app.use(pinia);
app.use(i18n);
app.config.errorHandler = (error) => errors.push(error);
@@ -63,6 +64,40 @@ async function mountPanel(aiConfigLoaded: boolean, connection?: ConnectionConfig
}
describe("AiAssistant mount", () => {
it("keeps the real Agent mode and an interactive picker after clicking a plugin recommendation", async () => {
const { container, errors } = await mountPanel(
true,
{ id: "plugin-connection", name: "Sample", db_type: "plugin", plugin_id: "sample.plugin", host: "localhost", port: 22, username: "", password: "" },
(settings) => {
settings.defaultAiMode = "agent";
},
{
pluginId: "sample.plugin",
pluginName: "Sample",
contributionId: "workbench",
workbenchId: "cluster",
context: { connectionId: "plugin-connection" },
items: [{ id: "overview", label: "Inspect cluster", prompt: "Inspect cluster" }],
},
);
expect(container.querySelector(".ai-mode-action-trigger")?.textContent).toContain(i18n.global.t("ai.modes.agent"));
container.querySelector<HTMLButtonElement>('button[title="Inspect cluster"]')!.click();
await new Promise((resolve) => setTimeout(resolve, 20));
const trigger = container.querySelector<HTMLButtonElement>(".ai-mode-action-trigger");
expect(trigger).not.toBeNull();
expect(trigger?.textContent).toContain(i18n.global.t("ai.modes.agent"));
expect(container.querySelector(".ai-mode-static-trigger")).toBeNull();
trigger!.click();
await new Promise((resolve) => setTimeout(resolve, 20));
expect(trigger?.getAttribute("aria-expanded")).toBe("true");
const askButton = Array.from(document.querySelectorAll<HTMLButtonElement>('[role="dialog"] button')).find((button) => button.textContent?.trim() === i18n.global.t("ai.modes.ask"));
expect(askButton).toBeDefined();
askButton!.click();
await new Promise((resolve) => setTimeout(resolve, 20));
expect(trigger?.textContent).toContain(i18n.global.t("ai.modes.ask"));
expect(errors.map(String)).toEqual([]);
});
it("opens when the AI config was loaded before the panel mounted", async () => {
expect((await mountPanel(true)).errors.map(String)).toEqual([]);
});
@@ -16,6 +16,7 @@ import {
type PluginSaveFileRequest,
type PluginSaveFileResult,
type PluginWorkbenchContext,
type PluginAiRecommendationHostUpdate,
} from "@/lib/plugins/pluginHostBridge";
import { getCachedPluginUiHtml, getOrLoadPluginUiHtml } from "@/lib/plugins/pluginUiHtmlCache";
import { buildPluginEditorAppearance } from "@/lib/plugins/pluginAppearance";
@@ -42,6 +43,7 @@ const emit = defineEmits<{
openWorkbench: [pluginId: string, contributionId: string, context?: PluginWorkbenchContext, options?: { forceNew?: boolean }];
openFilesystem: [pluginId: string, providerId: string, context?: PluginWorkbenchContext];
closeTab: [];
recommendations: [update: PluginAiRecommendationHostUpdate];
}>();
const { t, locale: appLocale } = useI18n();
@@ -381,6 +383,7 @@ function createBridge() {
sendBinary: api.sendPluginBinary,
readAsset: api.readPluginUiAsset,
openAiConversation,
setAiRecommendations: (update) => emit("recommendations", update),
openWorkbench: async (pluginId, contributionId, context, options) => emit("openWorkbench", pluginId, contributionId, context, options),
openFilesystem: async (pluginId, providerId, context) => emit("openFilesystem", pluginId, providerId, context),
reopenConnection: (pluginId, connectionId) => useConnectionStore().reopenPluginConnection(connectionId, pluginId),
@@ -5,7 +5,7 @@ import PluginWorkbenchHost from "@/components/plugins/PluginWorkbenchHost.vue";
import PluginRefreshState from "@/components/plugins/PluginRefreshState.vue";
import * as api from "@/lib/backend/api";
import { createFrontendPluginRegistry } from "@/lib/plugins/frontendPlugin";
import type { PluginWorkbenchContext } from "@/lib/plugins/pluginHostBridge";
import type { PluginAiRecommendationHostUpdate, PluginWorkbenchContext } from "@/lib/plugins/pluginHostBridge";
import type { InstalledPlugin } from "@/types/database";
import { useQueryStore } from "@/stores/queryStore";
import { useI18n } from "vue-i18n";
@@ -21,6 +21,7 @@ const props = defineProps<{
const { t, locale: appLocale } = useI18n();
const emit = defineEmits<{
closeTab: [];
recommendations: [update: PluginAiRecommendationHostUpdate];
}>();
const connectionStore = useConnectionStore();
const queryStore = useQueryStore();
@@ -129,6 +130,6 @@ defineExpose({ refresh });
<AlertTriangle class="mt-0.5 size-4 shrink-0" />
<span>{{ error || t("pluginPlatform.workbenchUnavailableFallback") }}</span>
</div>
<PluginWorkbenchHost v-else class="min-h-0 flex-1" :plugin="entry.plugin" :contribution="entry.contribution" :context="hostContext" @open-workbench="openWorkbench" @open-filesystem="openFilesystem" @close-tab="emit('closeTab')" />
<PluginWorkbenchHost v-else class="min-h-0 flex-1" :plugin="entry.plugin" :contribution="entry.contribution" :context="hostContext" @open-workbench="openWorkbench" @open-filesystem="openFilesystem" @close-tab="emit('closeTab')" @recommendations="emit('recommendations', $event)" />
</div>
</template>