feat(plugins): run declared commands from plugin workbench UI
This commit is contained in:
1 parent
7bc85dfcb0
commit
21102f4810
10 files changed
+190
-7
No files matched your search
@@ -77,6 +77,26 @@ export function interpolatePluginAiRecommendation(item: PluginAiRecommendation,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared `{{path}}` interpolation (same prototype-safe resolution as
|
||||
* recommendations) for non-recommendation templates such as command
|
||||
* `instance_key` scoping. Returns null when any placeholder cannot be
|
||||
* resolved to a scalar — callers decide the fallback (recommendations drop
|
||||
* the item; instance keys fall back to the literal template).
|
||||
*/
|
||||
export function interpolatePluginTemplate(value: string, context: Record<string, unknown>): string | null {
|
||||
let unresolved = false;
|
||||
const interpolated = value.replace(PLACEHOLDER, (_match, path: string) => {
|
||||
const resolved = contextValueText(readContextPath(context, path));
|
||||
if (resolved === undefined) {
|
||||
unresolved = true;
|
||||
return "";
|
||||
}
|
||||
return resolved;
|
||||
});
|
||||
return unresolved ? null : interpolated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve, order, and cap a list of recommendations for a context.
|
||||
* The input is never mutated and the result contains fresh objects.
|
||||
|
||||
@@ -225,4 +225,51 @@ describe("plugin command registry (PR-A4)", () => {
|
||||
expect(registry.findCommandTargetingWorkbench("io.dbx.ssh", "other.workbench")).toBeUndefined();
|
||||
expect(registry.findCommandTargetingWorkbench("other.plugin", "io.dbx.ssh.workbench")).toBeUndefined();
|
||||
});
|
||||
|
||||
// §4.1 instance_key placeholder scoping: one panel instance per resolved
|
||||
// connection, re-activation per key, literal fallback when unresolvable.
|
||||
it("scopes panel instances by instance_key placeholders from the execution context", () => {
|
||||
const registry = createFrontendPluginRegistry([
|
||||
installedPlugin("io.dbx.ldap", [
|
||||
{ type: "workbench", id: "io.dbx.ldap.logpanel", label: "Logs" },
|
||||
{ type: "command", id: "open-conn-logs", label: "Connection logs", action: { type: "open-workbench", workbench: "io.dbx.ldap.logpanel", presentation: "panel", reuse: "singleton", instance_key: "logs:{{connectionId}}" } },
|
||||
] as unknown as InstalledPlugin["manifest"]["contributions"]),
|
||||
]);
|
||||
const openPluginWorkbench = vi.fn();
|
||||
const { entries, activeEntryId } = usePluginBottomDock();
|
||||
|
||||
executePluginCommand(registry, { openPluginWorkbench } as never, "io.dbx.ldap", "open-conn-logs", { connectionId: "conn-a" });
|
||||
executePluginCommand(registry, { openPluginWorkbench } as never, "io.dbx.ldap", "open-conn-logs", { connectionId: "conn-b" });
|
||||
expect(entries.value.map((entry) => entry.instanceKey)).toEqual(["logs:conn-a", "logs:conn-b"]);
|
||||
|
||||
// the same connection re-activates its own instance; caller-only extras do not fork it
|
||||
executePluginCommand(registry, { openPluginWorkbench } as never, "io.dbx.ldap", "open-conn-logs", { connectionId: "conn-a", caller: "only" });
|
||||
expect(entries.value).toHaveLength(2);
|
||||
expect(activeEntryId.value).toBe(entries.value[0]!.id);
|
||||
|
||||
// unresolvable template (no context) falls back to the literal key
|
||||
executePluginCommand(registry, { openPluginWorkbench } as never, "io.dbx.ldap", "open-conn-logs");
|
||||
expect(entries.value).toHaveLength(3);
|
||||
expect(entries.value[2]!.instanceKey).toBe("logs:{{connectionId}}");
|
||||
expect(openPluginWorkbench).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("merges caller context over the command context and keeps reserved fields host-owned", () => {
|
||||
const registry = createFrontendPluginRegistry([
|
||||
installedPlugin("io.dbx.ldap", [
|
||||
{ type: "workbench", id: "io.dbx.ldap.logpanel", label: "Logs" },
|
||||
{ type: "command", id: "open-logs", label: "Logs", action: { type: "open-workbench", workbench: "io.dbx.ldap.logpanel", presentation: "panel", context: { plugin: { mode: "logs" }, from: "command" } } },
|
||||
] as unknown as InstalledPlugin["manifest"]["contributions"]),
|
||||
]);
|
||||
const { entries } = usePluginBottomDock();
|
||||
executePluginCommand(registry, { openPluginWorkbench: vi.fn() } as never, "io.dbx.ldap", "open-logs", { plugin: { mode: "override" }, connectionId: "c1", workbenchId: "plugin-forged", restored: true, surface: "tab" });
|
||||
const entry = entries.value[0]!;
|
||||
expect(entry.context.plugin).toEqual({ mode: "override" });
|
||||
expect(entry.context.from).toBe("command");
|
||||
expect(entry.context.connectionId).toBe("c1");
|
||||
// §11: reserved identity fields are host-authored — forged values never pass.
|
||||
expect(entry.context.workbenchId).toBe(entry.id);
|
||||
expect(entry.context.restored).toBe(false);
|
||||
expect(entry.context.surface).toBe("panel");
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,7 @@ import * as api from "@/lib/backend/api";
|
||||
import { uuid } from "@/lib/common/utils";
|
||||
import { activatePluginDockEntry, addPluginDockEntry, findReusableDockEntry } from "@/lib/plugins/pluginBottomDock";
|
||||
import { createFrontendPluginRegistry, evaluatePluginCommandConditions, type FrontendPluginRegistry } from "@/lib/plugins/frontendPlugin";
|
||||
import { interpolatePluginTemplate } from "@/lib/plugins/pluginAiRecommendations";
|
||||
import { useQueryStore } from "@/stores/queryStore";
|
||||
import type { PluginCommandContribution, PluginConditionContextKeys } from "@/types/database";
|
||||
|
||||
@@ -31,7 +32,7 @@ export interface PluginCommandExecutionResult {
|
||||
* §5.4 re-validate enablement right before execution (against the current context snapshot; the surface key follows
|
||||
* presentation).
|
||||
*/
|
||||
export function executePluginCommand(registry: FrontendPluginRegistry, queryStore: ReturnType<typeof useQueryStore>, pluginId: string, commandId: string): PluginCommandExecutionResult {
|
||||
export function executePluginCommand(registry: FrontendPluginRegistry, queryStore: ReturnType<typeof useQueryStore>, pluginId: string, commandId: string, callerContext?: Record<string, unknown>): PluginCommandExecutionResult {
|
||||
const command = registry.findCommand(pluginId, commandId)?.contribution;
|
||||
if (!command) return { error: `Unknown command '${pluginId}.${commandId}'` };
|
||||
const action = command.action;
|
||||
@@ -44,15 +45,28 @@ export function executePluginCommand(registry: FrontendPluginRegistry, queryStor
|
||||
if (!evaluatePluginCommandConditions(command.enablement?.all, commandContextKeys)) {
|
||||
return { error: `Command '${pluginId}.${commandId}' is disabled by enablement` };
|
||||
}
|
||||
// Execution context: command-declared payload first, caller context (bridge
|
||||
// executeCommand) over it; reserved identity fields (§11) are stripped from
|
||||
// the caller — the host owns workbenchId/restored/surface on every surface.
|
||||
const callerPayload = callerContext && typeof callerContext === "object" && !Array.isArray(callerContext) ? { ...callerContext } : {};
|
||||
delete callerPayload.workbenchId;
|
||||
delete callerPayload.restored;
|
||||
delete callerPayload.surface;
|
||||
const executionContext = { ...(action.context ?? {}), ...callerPayload };
|
||||
// presentation: panel -> adds an entry to the global bottom dock (§8.3); tab (default)
|
||||
// -> opens a workbench tab.
|
||||
if (action.presentation === "panel") {
|
||||
// §4.1: singleton (default) re-activates the existing panel instance
|
||||
// (reuse key = pluginId + commandId + presentation + instance_key);
|
||||
// reuse:"new" always spawns one. The dock "+" menu bypasses this path by
|
||||
// design — it is the explicit multi-open affordance.
|
||||
// design — it is the explicit multi-open affordance. `instance_key`
|
||||
// supports the shared `{{path}}` placeholders resolved against the
|
||||
// execution context, so `logs:{{connectionId}}` scopes one panel instance
|
||||
// per connection; an unresolvable template falls back to its literal (a
|
||||
// manifest without placeholders behaves exactly as before).
|
||||
const instanceKey = action.instance_key ? (interpolatePluginTemplate(action.instance_key, executionContext) ?? action.instance_key) : undefined;
|
||||
if (action.reuse !== "new") {
|
||||
const existing = findReusableDockEntry(pluginId, command.id, action.instance_key);
|
||||
const existing = findReusableDockEntry(pluginId, command.id, instanceKey);
|
||||
if (existing) {
|
||||
activatePluginDockEntry(existing.id);
|
||||
return {};
|
||||
@@ -63,17 +77,17 @@ export function executePluginCommand(registry: FrontendPluginRegistry, queryStor
|
||||
workbenchContributionId: action.workbench,
|
||||
kind: "command",
|
||||
commandId: command.id,
|
||||
instanceKey: action.instance_key,
|
||||
instanceKey,
|
||||
title: command.label,
|
||||
icon: command.icon,
|
||||
commandContext: action.context ?? {},
|
||||
commandContext: executionContext,
|
||||
});
|
||||
return {};
|
||||
}
|
||||
// Host-authored context: rebuilt from action.context — plugin-supplied reserved fields
|
||||
// Host-authored context: built from the execution context — plugin-supplied reserved fields
|
||||
// (workbenchId/restored/surface) never pass through; the workbenchId is generated by the host.
|
||||
const context = {
|
||||
...(action.context ?? {}),
|
||||
...executionContext,
|
||||
workbenchId: uuid(),
|
||||
restored: false,
|
||||
surface: "tab",
|
||||
|
||||
@@ -437,6 +437,68 @@ describe("PluginHostBridge", () => {
|
||||
expect(openWorkbench).toHaveBeenNthCalledWith(2, "sample", "sample.other", undefined, { forceNew: false });
|
||||
});
|
||||
|
||||
it("routes host.executeCommand to the surface handler scoped to the calling plugin", async () => {
|
||||
const messages: unknown[] = [];
|
||||
const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window;
|
||||
const executeCommand = vi.fn(() => ({ error: "Command 'sample.open-panel' is disabled by enablement" }));
|
||||
const bridge = new PluginHostBridge(plugin(["host.workbench"]), workbench, {}, () => target, {
|
||||
invoke: vi.fn(),
|
||||
notify: vi.fn(),
|
||||
sendBinary: vi.fn(),
|
||||
readAsset: vi.fn(),
|
||||
executeCommand,
|
||||
});
|
||||
bridge.handleWindowMessage({
|
||||
source: target,
|
||||
data: { source: "dbx-plugin", version: 1, type: "request", id: "3", method: "host.executeCommand", params: { commandId: "open-panel", context: { connectionId: "c1" } } },
|
||||
} as MessageEvent);
|
||||
await vi.waitFor(() => expect(messages).toHaveLength(1));
|
||||
expect(executeCommand).toHaveBeenCalledWith("sample", "open-panel", { connectionId: "c1" });
|
||||
// Expected business outcomes resolve as a result (no rejection) so webviews
|
||||
// can surface a notice instead of catching.
|
||||
expect(messages[0]).toMatchObject({ id: "3", result: { error: "Command 'sample.open-panel' is disabled by enablement" } });
|
||||
});
|
||||
|
||||
it("normalizes a void executeCommand handler to an empty result object", async () => {
|
||||
const messages: unknown[] = [];
|
||||
const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window;
|
||||
const executeCommand = vi.fn();
|
||||
const bridge = new PluginHostBridge(plugin(["host.workbench"]), workbench, {}, () => target, {
|
||||
invoke: vi.fn(),
|
||||
notify: vi.fn(),
|
||||
sendBinary: vi.fn(),
|
||||
readAsset: vi.fn(),
|
||||
executeCommand,
|
||||
});
|
||||
bridge.handleWindowMessage({
|
||||
source: target,
|
||||
data: { source: "dbx-plugin", version: 1, type: "request", id: "4", method: "host.executeCommand", params: { commandId: "open-panel" } },
|
||||
} as MessageEvent);
|
||||
await vi.waitFor(() => expect(messages).toHaveLength(1));
|
||||
expect(executeCommand).toHaveBeenCalledWith("sample", "open-panel", undefined);
|
||||
expect(messages[0]).toMatchObject({ id: "4", result: {} });
|
||||
});
|
||||
|
||||
it("host.executeCommand requires the host.workbench permission", async () => {
|
||||
const messages: unknown[] = [];
|
||||
const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window;
|
||||
const executeCommand = vi.fn();
|
||||
const bridge = new PluginHostBridge(plugin(), workbench, {}, () => target, {
|
||||
invoke: vi.fn(),
|
||||
notify: vi.fn(),
|
||||
sendBinary: vi.fn(),
|
||||
readAsset: vi.fn(),
|
||||
executeCommand,
|
||||
});
|
||||
bridge.handleWindowMessage({
|
||||
source: target,
|
||||
data: { source: "dbx-plugin", version: 1, type: "request", id: "5", method: "host.executeCommand", params: { commandId: "open-panel" } },
|
||||
} as MessageEvent);
|
||||
await vi.waitFor(() => expect(messages).toHaveLength(1));
|
||||
expect(messages[0]).toMatchObject({ id: "5", error: "Plugin has not declared permission 'host.workbench'" });
|
||||
expect(executeCommand).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reopens a plugin connection through host.reopenConnection", async () => {
|
||||
const messages: unknown[] = [];
|
||||
const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window;
|
||||
|
||||
@@ -162,6 +162,17 @@ export interface PluginHostBridgeApi {
|
||||
openAiConversation?(request: AiPluginConversationRequest): Promise<void>;
|
||||
setAiRecommendations?(update: PluginAiRecommendationHostUpdate): void;
|
||||
openWorkbench?(pluginId: string, contributionId: string, context?: PluginWorkbenchContext, options?: { forceNew?: boolean }): Promise<void> | void;
|
||||
/**
|
||||
* §4/§5 command execution asked from a plugin webview: the same registry
|
||||
* path as menu execution — enablement (§5.4), §4.1 singleton reuse,
|
||||
* host-authored context — scoped to the calling plugin's own declared
|
||||
* commands. The optional context merges over the command-declared context
|
||||
* (caller wins; reserved identity fields are host-owned) and feeds
|
||||
* `instance_key` `{{path}}` placeholders, so per-connection instances are
|
||||
* one `logs:{{connectionId}}` declaration away. Resolves `{ error }` for
|
||||
* expected business outcomes; only bridge or permission failures reject.
|
||||
*/
|
||||
executeCommand?(pluginId: string, commandId: string, context?: Record<string, unknown>): Promise<{ error?: string } | null | void> | { error?: string } | null | void;
|
||||
openFilesystem?(pluginId: string, providerId: string, context?: PluginWorkbenchContext): Promise<void> | void;
|
||||
/** Explicit user-triggered reconnect of an owned plugin connection (full flow, interactive password prompt allowed). */
|
||||
reopenConnection?(pluginId: string, connectionId: string): Promise<void>;
|
||||
@@ -663,6 +674,12 @@ export class PluginHostBridge {
|
||||
await this.api.openWorkbench(this.plugin.manifest.id, requireProtocolName(input.contributionId, "workbench contribution"), isRecord(input.context) ? input.context : undefined, { forceNew: input.forceNew === true });
|
||||
return null;
|
||||
}
|
||||
if (method === "host.executeCommand") {
|
||||
this.requirePermission("host.workbench");
|
||||
if (!this.api.executeCommand) throw new Error("Host command execution is unavailable");
|
||||
const input = requireRecord(params, "host.executeCommand params");
|
||||
return (await this.api.executeCommand(this.plugin.manifest.id, requireProtocolName(input.commandId, "command id"), isRecord(input.context) ? input.context : undefined)) ?? {};
|
||||
}
|
||||
if (method === "host.reopenConnection") {
|
||||
if (!this.api.reopenConnection) throw new Error("Connection reopen is unavailable");
|
||||
const input = requireRecord(params, "host.reopenConnection params");
|
||||
@@ -1209,6 +1226,15 @@ export function pluginSdkSource(initialTheme?: PluginBridgeTheme): string {
|
||||
return URL.createObjectURL(new Blob([decode(asset.dataBase64)], { type: asset.contentType }));
|
||||
},
|
||||
openWorkbench: (contributionId, childContext, options) => request('host.openWorkbench', { contributionId, context: childContext, forceNew: !!(options && options.forceNew) }),
|
||||
// §4/§5 command execution from a webview — the same registry path a menu
|
||||
// placement takes (enablement, §4.1 reuse, host-authored context),
|
||||
// scoped to the plugin's own declared commands. The optional context
|
||||
// merges over the command context and scopes instance_key placeholders
|
||||
// (e.g. one panel per connection). Resolves { error } for expected
|
||||
// business outcomes so callers can surface a notice without try/catch.
|
||||
// NOTE: this block is the sandbox bootstrap template source — comments
|
||||
// here must not contain backticks or dollar-brace interpolation.
|
||||
executeCommand: (commandId, context) => request('host.executeCommand', { commandId, context }),
|
||||
openFilesystem: (providerId, childContext) => request('host.openFilesystem', { providerId, context: childContext }),
|
||||
reopenConnection: (connectionId) => request('host.reopenConnection', { connectionId }),
|
||||
// Estimated plans only: mode must be sent explicitly so a plugin states
|
||||
|
||||
Reference in new issue
Block a user