feat(kaak): refine interaction feedback and permissions

This commit is contained in:
illusia committed 2026-10-11 01:31:44 +08:00
1 parent bdd0c3473c
commit 83902e0fb5
19 files changed
+236 -54

No files matched your search

+2 -2
View File
@@ -1,6 +1,6 @@
import {
SKIN_PLUGIN_HOST_WIDGET_PERMISSIONS,
parseSkinPluginDocument,
skinPluginWidgetPermissions,
type SkinPluginComponent,
type SkinPluginDocument,
type SkinPluginNode,
@@ -30,7 +30,7 @@ export function editableSkinPluginDocument(input: unknown): SkinPluginDocument {
const components = parsed.components.map((component) => {
const permissions = new Set(component.permissions);
const visit = (node: SkinPluginNode) => {
if (node.widget) SKIN_PLUGIN_HOST_WIDGET_PERMISSIONS[node.widget]?.forEach((permission) => permissions.add(permission));
if (node.widget) skinPluginWidgetPermissions(node.widget, node.options).forEach((permission) => permissions.add(permission));
node.children?.forEach(visit);
};
visit(component.root);
+38 -1
View File
@@ -175,6 +175,43 @@ test("schema v3 member cards require a separate permission to display cached pro
assert.equal(parseSkinPluginDocument(document).components[0].root.children?.[0].widget, "voice.member-cards");
});
test("schema v3 trusted widgets can omit screen-share UI and request only the permissions they use", () => {
const document = validDocument();
document.schemaVersion = 3;
document.components[0].actions = {};
document.components[0].permissions = ["session.members.read", "session.memberAvatars.read"];
document.components[0].root = { tag: "main", children: [{ widget: "voice.member-cards", options: { screenShare: false } }] };
assert.deepEqual(parseSkinPluginDocument(document).components[0].root.children?.[0]?.options, { screenShare: false });
const memberCardsDefault = structuredClone(document);
memberCardsDefault.components[0].root.children![0] = { widget: "voice.member-cards" };
errorCode(() => parseSkinPluginDocument(memberCardsDefault), "SKIN_PLUGIN_PERMISSION_MISSING");
const performanceWithoutShare = structuredClone(document);
performanceWithoutShare.components[0].permissions = [];
performanceWithoutShare.components[0].root.children![0] = { widget: "voice.performance-panel", options: { screenShare: false } };
assert.equal(parseSkinPluginDocument(performanceWithoutShare).components[0].root.children?.[0]?.widget, "voice.performance-panel");
const performanceWithShare = structuredClone(performanceWithoutShare);
performanceWithShare.components[0].root.children![0] = { widget: "voice.performance-panel", options: { screenShare: true } };
errorCode(() => parseSkinPluginDocument(performanceWithShare), "SKIN_PLUGIN_PERMISSION_MISSING");
performanceWithShare.components[0].permissions = ["voice.screenShare.read"];
assert.equal(parseSkinPluginDocument(performanceWithShare).components[0].root.children?.[0]?.widget, "voice.performance-panel");
const invalidOption = structuredClone(document);
invalidOption.components[0].root.children![0] = { widget: "voice.member-cards", options: { screenShare: "false" } as never };
errorCode(() => parseSkinPluginDocument(invalidOption), "SKIN_PLUGIN_WIDGET_OPTIONS_INVALID");
const legacyOption = structuredClone(document);
legacyOption.schemaVersion = 2;
legacyOption.components[0].root.children![0] = { widget: "voice.member-cards", options: { screenShare: false } };
errorCode(() => parseSkinPluginDocument(legacyOption), "SKIN_PLUGIN_SCHEMA_UNSUPPORTED");
const misplacedOption = structuredClone(document);
misplacedOption.components[0].root.children![0] = { tag: "div", options: { screenShare: false } } as never;
errorCode(() => parseSkinPluginDocument(misplacedOption), "SKIN_PLUGIN_WIDGET_OPTIONS_INVALID");
});
test("KAAK v3 example declares the connection home and complete voice workspace", async () => {
const source = await readFile(new URL("../../docs/examples/kaak-voice/components.json", import.meta.url), "utf8");
const parsed = parseSkinPluginJson(source);
@@ -205,7 +242,7 @@ test("KAAK v3 example declares the connection home and complete voice workspace"
visit(workspace.root);
assert.ok(nodes.some((node) => node.widget === "voice.audio-controls"));
assert.ok(nodes.some((node) => node.widget === "voice.member-cards"));
assert.ok(nodes.some((node) => node.widget === "voice.screen-share-start"));
assert.ok(!nodes.some((node) => node.widget === "voice.screen-share-start"), "KOOK clone excludes screen sharing");
assert.ok(nodes.some((node) => node.repeat?.path === "session.channels" && node.children?.[0].events?.dblclick === "join-channel"));
assert.ok(!nodes.some((node) => node.repeat?.path === "session.members"), "member imagery stays inside the trusted host widget");
assert.ok(nodes.some((node) => node.tag === "input" && node.bindValue === "message"));
+32 -4
View File
@@ -56,6 +56,10 @@ export const SKIN_PLUGIN_HOST_WIDGETS = Object.freeze([
"voice.disconnect-control",
] as const);
export type SkinPluginHostWidget = typeof SKIN_PLUGIN_HOST_WIDGETS[number];
export interface SkinPluginWidgetOptions {
/** Hide screen-share UI and its stream details for widgets that support it. */
screenShare?: boolean;
}
const widgetPermissions = (...permissions: SkinPluginPermission[]): readonly SkinPluginPermission[] => Object.freeze(permissions);
export const SKIN_PLUGIN_HOST_WIDGET_PERMISSIONS: Partial<Record<SkinPluginHostWidget, readonly SkinPluginPermission[]>> = Object.freeze({
"voice.audio-controls": widgetPermissions("audio.status.read", "audio.microphone.control", "audio.output.control"),
@@ -67,11 +71,21 @@ export const SKIN_PLUGIN_HOST_WIDGET_PERMISSIONS: Partial<Record<SkinPluginHostW
"voice.screen-share-start": widgetPermissions("voice.screenShare.control"),
"voice.whisper-controls": widgetPermissions("voice.whisper.status.read", "voice.whisper.control"),
});
export function skinPluginWidgetPermissions(
widget: SkinPluginHostWidget,
options?: SkinPluginWidgetOptions,
): readonly SkinPluginPermission[] {
if (widget === "voice.performance-panel") return options?.screenShare === true ? widgetPermissions("voice.screenShare.read") : [];
const permissions = SKIN_PLUGIN_HOST_WIDGET_PERMISSIONS[widget] ?? [];
if (options?.screenShare !== false || widget !== "voice.member-cards") return permissions;
return permissions.filter((permission) => permission !== "voice.screenShare.read" && permission !== "voice.screenShare.control");
}
export function missingSkinPluginWidgetPermission(
widget: SkinPluginHostWidget,
permissions: readonly SkinPluginPermission[],
options?: SkinPluginWidgetOptions,
): SkinPluginPermission | undefined {
return SKIN_PLUGIN_HOST_WIDGET_PERMISSIONS[widget]?.find((permission) => !permissions.includes(permission));
return skinPluginWidgetPermissions(widget, options).find((permission) => !permissions.includes(permission));
}
export type SkinPluginEvent =
| "click" | "dblclick" | "change" | "input" | "submit" | "keydown"
@@ -91,6 +105,7 @@ export interface SkinPluginActionDefinition {
export interface SkinPluginNode {
widget?: SkinPluginHostWidget;
options?: SkinPluginWidgetOptions;
tag?: string;
text?: string;
part?: string;
@@ -381,23 +396,36 @@ export function parseSkinPluginDocument(input: unknown): SkinPluginDocument {
componentNodes += 1;
if (componentNodes > (inputSchemaVersion >= 3 ? SKIN_PLUGIN_NODE_LIMIT : 256)) fail("SKIN_PLUGIN_COMPLEXITY_LIMIT", "A component contains too many template nodes.");
if (depth > (inputSchemaVersion >= 3 ? SKIN_PLUGIN_TREE_DEPTH_LIMIT : 16) || !isRecord(rawNode)) fail("SKIN_PLUGIN_TREE_INVALID", "A component tree is too deep or contains an invalid node.");
onlyKeys(rawNode, new Set(["widget", "tag", "text", "part", "className", "attributes", "asset", "bindValue", "repeat", "when", "events", "children"]), "Component node");
onlyKeys(rawNode, new Set(["widget", "options", "tag", "text", "part", "className", "attributes", "asset", "bindValue", "repeat", "when", "events", "children"]), "Component node");
if (inputSchemaVersion === 1 && rawNode.widget !== undefined) fail("SKIN_PLUGIN_SCHEMA_UNSUPPORTED", "Host widgets require components schema version 2.");
const nodeKinds = Number(rawNode.widget !== undefined) + Number(rawNode.tag !== undefined) + Number(rawNode.text !== undefined);
if (nodeKinds !== 1) fail("SKIN_PLUGIN_TREE_INVALID", "Each component node must define exactly one of widget, tag, or text.");
if (rawNode.options !== undefined && rawNode.widget === undefined) fail("SKIN_PLUGIN_WIDGET_OPTIONS_INVALID", "Only host widgets may declare widget options.");
const node: SkinPluginNode = {};
if (rawNode.widget !== undefined) {
if (typeof rawNode.widget !== "string" || !HOST_WIDGETS.has(rawNode.widget)) fail("SKIN_PLUGIN_WIDGET_INVALID", "The component requests an unsupported host widget.");
if (page === "demo") fail("SKIN_PLUGIN_WIDGET_INVALID", "Host widgets are available only on the home and voice client pages.");
if (rawNode.options !== undefined && inputSchemaVersion < 3) fail("SKIN_PLUGIN_SCHEMA_UNSUPPORTED", "Host widget options require components schema version 3.");
// Preserve existing schema v2 packages; newly authored v3 widgets must declare every
// permission needed by the trusted host UI they embed. Screen-share start was already gated.
let options: SkinPluginWidgetOptions | undefined;
if (rawNode.options !== undefined) {
if (!isRecord(rawNode.options)
|| (rawNode.widget !== "voice.member-cards" && rawNode.widget !== "voice.performance-panel")
|| Object.keys(rawNode.options).some((key) => key !== "screenShare")
|| (rawNode.options.screenShare !== undefined && typeof rawNode.options.screenShare !== "boolean")) {
fail("SKIN_PLUGIN_WIDGET_OPTIONS_INVALID", "This host widget uses unsupported options.");
}
options = { ...(rawNode.options.screenShare !== undefined ? { screenShare: rawNode.options.screenShare } : {}) };
}
const requiredPermission = inputSchemaVersion >= 3 || rawNode.widget === "voice.screen-share-start"
? missingSkinPluginWidgetPermission(rawNode.widget as SkinPluginHostWidget, permissions)
? missingSkinPluginWidgetPermission(rawNode.widget as SkinPluginHostWidget, permissions, options)
: undefined;
if (requiredPermission) fail("SKIN_PLUGIN_PERMISSION_MISSING", `The ${rawNode.widget} widget requires ${requiredPermission}.`);
const widgetOnlyKeys = new Set(["widget", "part", "className", "when"]);
const widgetOnlyKeys = new Set(["widget", "options", "part", "className", "when"]);
onlyKeys(rawNode, widgetOnlyKeys, "Host widget node");
node.widget = rawNode.widget as SkinPluginHostWidget;
if (options) node.options = options;
}
if (rawNode.tag !== undefined) {
if (typeof rawNode.tag !== "string" || !ELEMENTS.has(rawNode.tag)) fail("SKIN_PLUGIN_ELEMENT_INVALID", "The component uses an unsupported HTML element.");