feat(skin): bridge trusted plugin actions

This commit is contained in:
illusia committed 2026-10-10 15:53:50 +08:00
1 parent 1aa29158ef
commit 35cb6ad6df
13 files changed
+276 -45

No files matched your search

@@ -1,6 +1,7 @@
import type { SkinRuntimePlugin } from "../../../src/shared/skin-runtime-plugins.js";
import type { SkinExtensionUiInput } from "../../../src/shared/skin-extension-ui-input.js";
import type { SkinPluginDocument } from "../../../src/shared/skin-plugin.js";
import { parseSkinExtensionUiActionRequest } from "../../../src/shared/skin-extension-ui.js";
import type { SkinExtensionUiInput, SkinExtensionUiEventInput } from "../../../src/shared/skin-extension-ui-input.js";
import type { SkinPluginActionDefinition, SkinPluginDocument } from "../../../src/shared/skin-plugin.js";
import {
createSkinExtensionWasmUiLoopPrototype,
type SkinExtensionWasmUiLoopRunHandle,
@@ -13,10 +14,22 @@ export class SkinRuntimePluginSessionError extends Error {
}
}
const HOST_ACTION_EVENT_NAMES = new Set(["click", "dblclick", "submit", "keydown", "keyup", "contextmenu", "pointerdown", "pointerup", "drop"]);
function assertExplicitUserActivation(event: SkinExtensionUiEventInput): void {
if (!HOST_ACTION_EVENT_NAMES.has(event.eventName)) {
throw new SkinRuntimePluginSessionError("SKIN_RUNTIME_PLUGIN_ACTION_EVENT_INVALID", "Host actions require an explicit user activation event.");
}
if ((event.eventName === "keydown" || event.eventName === "keyup") && !["Enter", " ", "Escape"].includes(event.key ?? "")) {
throw new SkinRuntimePluginSessionError("SKIN_RUNTIME_PLUGIN_ACTION_KEY_INVALID", "Host actions may only use Enter, Space, or Escape key events.");
}
}
export interface SkinRuntimePluginSessionOptions {
plugin: SkinRuntimePlugin;
createRun(input: SkinExtensionUiInput): SkinExtensionWasmUiLoopRunHandle;
onOutput?(output: string, document: SkinPluginDocument): void | Promise<void>;
onAction?(action: SkinPluginActionDefinition, event: SkinExtensionUiEventInput): void | Promise<void>;
onError?(error: Error): void;
}
@@ -25,13 +38,24 @@ export function createSkinRuntimePluginSession(options: SkinRuntimePluginSession
let output: string | null = null;
const loop = createSkinExtensionWasmUiLoopPrototype({
createRun: options.createRun,
onOutput: async (nextOutput, document) => {
onOutput: async (nextOutput, document, input) => {
if (document.components.some((component) => component.page !== options.plugin.page)) {
throw new SkinRuntimePluginSessionError("SKIN_RUNTIME_PLUGIN_PAGE_MISMATCH", "Generated UI must stay on the page declared by its plugin.");
}
if (options.plugin.mode === "surface" && document.components.length !== 1) {
throw new SkinRuntimePluginSessionError("SKIN_RUNTIME_PLUGIN_SURFACE_OUTPUT_INVALID", "A surface plugin must generate exactly one root component.");
}
const action = parseSkinExtensionUiActionRequest(nextOutput, options.plugin.permissions);
if (action) {
if (!input.event) {
throw new SkinRuntimePluginSessionError("SKIN_RUNTIME_PLUGIN_ACTION_WITHOUT_EVENT", "A host action can only follow a trusted UI event.");
}
assertExplicitUserActivation(input.event);
if (!options.onAction) {
throw new SkinRuntimePluginSessionError("SKIN_RUNTIME_PLUGIN_ACTION_UNAVAILABLE", "The host action bridge is unavailable.");
}
await options.onAction(action, input.event);
}
output = nextOutput;
await options.onOutput?.(nextOutput, document);
},