feat(skin): add validated runtime UI output bridge

This commit is contained in:
illusia committed 2026-10-10 15:53:49 +08:00
1 parent 7047d71678
commit d137b41b5c
16 files changed
+443 -155

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
import {
parseSkinPluginDocument,
type SkinPluginAction,
type SkinPluginDocument,
type SkinPluginNode,
} from "./skin-plugin.js";
export const SKIN_EXTENSION_UI_OUTPUT_LIMIT_BYTES = 16 * 1024;
const LOCAL_UI_ACTIONS = new Set<SkinPluginAction>(["ui.setState", "ui.toggleState"]);
export class SkinExtensionUiOutputError extends Error {
constructor(readonly code: string, message: string) {
super(message);
this.name = "SkinExtensionUiOutputError";
}
}
function fail(code: string, message: string): never {
throw new SkinExtensionUiOutputError(code, message);
}
function assertLocalUiNode(node: SkinPluginNode): void {
if (node.widget) fail("SKIN_EXTENSION_UI_WIDGET_UNSUPPORTED", "Extension UI output cannot embed privileged host widgets.");
node.children?.forEach(assertLocalUiNode);
}
/** Parses Wasm output into the existing safe component tree, with no added host capabilities. */
export function parseSkinExtensionUiOutput(source: string): SkinPluginDocument {
if (typeof source !== "string" || !source.length) {
fail("SKIN_EXTENSION_UI_OUTPUT_INVALID", "Extension UI output must be a non-empty JSON string.");
}
if (new TextEncoder().encode(source).byteLength > SKIN_EXTENSION_UI_OUTPUT_LIMIT_BYTES) {
fail("SKIN_EXTENSION_UI_OUTPUT_SIZE", "Extension UI output exceeds the 16 KiB UTF-8 limit.");
}
let input: unknown;
try { input = JSON.parse(source); }
catch { fail("SKIN_EXTENSION_UI_OUTPUT_JSON", "Extension UI output must contain valid JSON."); }
let document: SkinPluginDocument;
try { document = parseSkinPluginDocument(input); }
catch { fail("SKIN_EXTENSION_UI_OUTPUT_SCHEMA", "Extension UI output does not match the safe component schema."); }
if (document.schemaVersion !== 3) {
fail("SKIN_EXTENSION_UI_SCHEMA_UNSUPPORTED", "Extension UI output requires components schema version 3.");
}
for (const component of document.components) {
if (component.page === "demo" || component.mode === "surface" || component.permissions.length > 0) {
fail("SKIN_EXTENSION_UI_CAPABILITY_UNSUPPORTED", "Extension UI output cannot request skin permissions or replace a page.");
}
if (Object.values(component.actions).some((action) => !LOCAL_UI_ACTIONS.has(action.type))) {
fail("SKIN_EXTENSION_UI_ACTION_UNSUPPORTED", "Extension UI output can only update its own local UI state.");
}
assertLocalUiNode(component.root);
}
return document;
}
+10 -6
View File
@@ -132,14 +132,18 @@ test("host widget references stay allowlisted and cannot be used on the demo pag
errorCode(() => parseSkinPluginDocument(demoWidget), "SKIN_PLUGIN_WIDGET_INVALID");
});
test("KAAK v3 example uses only validated public session data and an explicit join action", async () => {
test("KAAK v3 example splits validated channel and member data by component", async () => {
const source = await readFile(new URL("../../docs/examples/kaak-voice/components.json", import.meta.url), "utf8");
const parsed = parseSkinPluginJson(source);
const sidebar = parsed.components[0];
assert.equal(sidebar.id, "kaak-room-sidebar");
assert.deepEqual(sidebar.permissions, ["session.channels.read", "session.members.read", "session.channel.join"]);
assert.equal(sidebar.actions["join-channel"].type, "voice.joinChannel");
assert.equal(sidebar.root.children?.[1].children?.[0].repeat?.path, "session.channels");
const channelSidebar = parsed.components.find((component) => component.id === "kaak-channel-sidebar");
const memberSidebar = parsed.components.find((component) => component.id === "kaak-members-sidebar");
assert.ok(channelSidebar);
assert.ok(memberSidebar);
assert.deepEqual(channelSidebar.permissions, ["session.channels.read", "session.channel.join"]);
assert.equal(channelSidebar.actions["join-channel"].type, "voice.joinChannel");
assert.equal(channelSidebar.root.children?.[1].children?.[0].repeat?.path, "session.channels");
assert.deepEqual(memberSidebar.permissions, ["session.members.read"]);
assert.equal(memberSidebar.root.children?.[2].children?.[0].repeat?.path, "session.members");
});
test("skin plugin binds form controls only to declared local state", () => {