Files
faceclaw/app/ui/shell/worker-window.ts
T

326 lines
12 KiB
TypeScript

import { GrayImage } from "../../graphics/image";
import { windowIcon } from "./chrome-layer";
import { type IconName } from "../../graphics/icons";
import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/tool-registry";
import { shell, type ShellWindow } from "./shell";
/**
* Messages between the shell (main thread) and an app worker. One worker
* hosts one app, which may have several windows; messages are routed by
* windowId. Everything crossing this boundary is small JSON; pixels go
* worker→Java directly.
*/
export type WorkerAppMessage =
| { type: "open-window"; windowId: string; surfaceId: string; viewport: { width: number; height: number } }
| { type: "close-window"; windowId: string }
| { type: "input"; windowId: string; event: unknown; frameId: number; focused: boolean }
| { type: "text-input"; windowId: string; text: string }
| { type: "render"; windowId: string; focused: boolean }
| { type: "foreground"; windowId: string; foreground: boolean; focused: boolean }
| { type: "screen"; on: boolean }
/** Assistant tool invocation aimed at a window; reply with tool-result. */
| { type: "tool-call"; callId: string; windowId: string; name: string; args: unknown };
export type WorkerAppReply =
| { type: "yield-focus"; windowId: string }
| {
/** Foreground and focus one of the app's existing windows. */
type: "focus-window";
windowId: string;
}
| {
/** App-initiated window (e.g. a terminal view opened from the hub list). */
type: "open-window-request";
windowId: string;
title: string;
iconLetter: string;
icon?: IconName;
focus?: boolean;
}
| { type: "set-title"; windowId: string; title: string }
| { type: "set-attention"; windowId: string; attention: boolean }
| {
/** Window-menu pick: open the shell's voice dialog aimed at this window. */
type: "start-voice-input";
windowId: string;
}
| {
/** Window-menu pick: close this window (the shell owns the close path). */
type: "close-window-request";
windowId: string;
}
| {
/** Open or focus the Settings app, optionally jumping to a section. */
type: "open-settings";
section?: string;
}
| {
/**
* Set or clear the app's top-bar tray icon. Pixels ride the JSON
* postMessage roundtrip — acceptable because tray icons are small and
* infrequently updated.
*/
type: "set-tray-icon";
icon: { width: number; height: number; pixels: number[] } | null;
}
| {
/**
* Declare (replacing the prior set) the assistant tools this window
* contributes. Names are unprefixed; the registry adds `app.<appId>.`.
*/
type: "set-tools";
windowId: string;
tools: ToolSpec[];
}
| {
/** Result of a tool-call, matched to the request by callId. */
type: "tool-result";
callId: string;
result: ToolResult;
};
export type WorkerWindowSpec = {
/** Unique across the shell; namespace with the appId (e.g. "terminal:view:3"). */
windowId: string;
title: string;
iconLetter: string;
/** Lucide icon name for the sidebar indicator; falls back to iconLetter. */
icon?: IconName;
/** Foreground and focus the window once its surface exists. */
focus?: boolean;
};
export type WorkerAppHostOptions = {
appId: string;
worker: Worker;
viewport: { width: number; height: number };
/** Create/refresh a window surface on the compositor (no-op when disconnected). */
configureSurface: (surfaceId: string, visible: boolean) => Promise<void>;
setSurfaceVisible: (surfaceId: string, visible: boolean) => void;
removeSurface: (surfaceId: string) => void;
requestShellRender: () => void;
/** Open or focus the Settings app, optionally selecting a section. */
openSettings: (section?: string) => void;
};
/**
* Owns the Worker for one app and adapts its windows to the shell's window
* interface: forwards input and lifecycle over postMessage, relays worker
* requests (yield-focus, new windows, attention flags) back to the shell,
* and manages compositor surfaces for the app's windows. The worker submits
* frames straight to the Java compositor, so no pixels cross this boundary.
*/
/** A tool-call awaiting its worker reply; also its own leak-safety timeout. */
type PendingToolCall = {
windowId: string;
resolve: (result: ToolResult) => void;
timer: ReturnType<typeof setTimeout>;
};
/**
* How long the host waits for a worker's tool-result before giving up. The
* registry applies its own (usually shorter) per-tool timeout; this is the
* backstop that also frees the pending-call entry if the worker never replies.
*/
const TOOL_CALL_HOST_TIMEOUT_MS = 15_000;
export class WorkerAppHost {
private readonly openWindows = new Set<string>();
private readonly pendingToolCalls = new Map<string, PendingToolCall>();
private nextCallSerial = 1;
constructor(private readonly options: WorkerAppHostOptions) {
options.worker.onmessage = (event: MessageEvent) => {
const message = event.data as WorkerAppReply | undefined;
if (!message) return;
switch (message.type) {
case "yield-focus":
// Only the focused window's yield is meaningful.
if (shell.foregroundWindow()?.windowId === message.windowId) {
shell.yieldFocusToSidebar();
}
break;
case "focus-window":
if (this.openWindows.has(message.windowId)) {
shell.focusWindow(message.windowId);
this.options.requestShellRender();
}
break;
case "open-window-request":
this.openWindow({
windowId: message.windowId,
title: message.title,
iconLetter: message.iconLetter,
icon: message.icon,
focus: message.focus,
});
break;
case "set-attention":
shell.setWindowAttention(message.windowId, message.attention);
break;
case "start-voice-input":
// Menus only open on the focused window, but re-check foreground:
// the reply crosses a thread boundary and focus may have moved.
if (shell.foregroundWindow()?.windowId === message.windowId) {
shell.startVoiceInput();
}
break;
case "close-window-request":
if (this.openWindows.has(message.windowId)) {
shell.closeWindow(message.windowId);
this.options.requestShellRender();
}
break;
case "open-settings":
this.options.openSettings(message.section);
break;
case "set-tray-icon": {
let icon: GrayImage | null = null;
if (message.icon) {
icon = new GrayImage(message.icon.width, message.icon.height, 0);
icon.pixels.set(message.icon.pixels.slice(0, icon.pixels.length));
}
shell.setTrayIcon(this.options.appId, icon);
break;
}
case "set-title":
// Titles are informational for now (sidebar shows icons only).
break;
case "set-tools":
// Only a window we actually have open may contribute tools.
if (this.openWindows.has(message.windowId)) {
toolRegistry.setAppTools({
windowId: message.windowId,
appId: this.options.appId,
specs: message.tools,
invoke: (toolName, args) => this.callWindowTool(message.windowId, toolName, args),
isForeground: () => shell.foregroundWindow()?.windowId === message.windowId,
});
}
break;
case "tool-result": {
const pending = this.pendingToolCalls.get(message.callId);
if (pending) {
clearTimeout(pending.timer);
this.pendingToolCalls.delete(message.callId);
pending.resolve(message.result);
}
break;
}
}
};
options.worker.onerror = (error) => {
console.error(`worker app ${options.appId} error: ${JSON.stringify(error)}`);
};
}
windowCount(): number {
return this.openWindows.size;
}
/** Open a window of this app and register it with the shell. */
openWindow(spec: WorkerWindowSpec): ShellWindow {
const surfaceId = `window:${spec.windowId}`;
this.openWindows.add(spec.windowId);
this.post({
type: "open-window",
windowId: spec.windowId,
surfaceId,
viewport: this.options.viewport,
});
const window: ShellWindow = {
appId: this.options.appId,
windowId: spec.windowId,
title: spec.title,
surfaceId,
closeable: true,
close: () => {
this.openWindows.delete(spec.windowId);
// Withdraw this window's tools and fail any in-flight calls to it.
toolRegistry.removeAppTools(spec.windowId);
this.failPendingToolCallsFor(spec.windowId);
this.post({ type: "close-window", windowId: spec.windowId });
this.options.removeSurface(surfaceId);
},
drawIcon: windowIcon(spec.icon, spec.iconLetter),
handleInput: (event, frameId) => {
this.post({
type: "input",
windowId: spec.windowId,
event,
frameId,
focused: shell.isWindowFocused(spec.windowId),
});
},
requestRender: () => {
this.post({ type: "render", windowId: spec.windowId, focused: shell.isWindowFocused(spec.windowId) });
},
receiveTextInput: (text) => {
this.post({ type: "text-input", windowId: spec.windowId, text });
},
setForeground: (foreground) => {
this.options.setSurfaceVisible(surfaceId, foreground);
if (foreground) {
shell.setWindowAttention(spec.windowId, false);
}
this.post({
type: "foreground",
windowId: spec.windowId,
foreground,
focused: shell.isWindowFocused(spec.windowId),
});
},
setScreenOn: (on) => {
// Screen state is per-app, but sending per-window keeps the protocol
// uniform; the worker treats it globally.
this.post({ type: "screen", on });
},
};
shell.registerWindow(window);
// Configure the surface before any foregrounding so the worker's first
// frame has somewhere to land.
void this.options
.configureSurface(surfaceId, false)
.then(() => {
if (spec.focus) {
shell.focusWindow(spec.windowId);
}
this.options.requestShellRender();
})
.catch((error) => {
console.error(`surface setup for ${spec.windowId} failed: ${error}`);
});
return window;
}
/**
* Post a tool-call to the worker and resolve when its tool-result comes back.
* Resolves with a tool error on timeout so a hung worker yields a tool error
* rather than a stuck assistant turn.
*/
private callWindowTool(windowId: string, toolName: string, args: unknown): Promise<ToolResult> {
return new Promise<ToolResult>((resolve) => {
const callId = `${this.options.appId}:${this.nextCallSerial++}`;
const timer = setTimeout(() => {
this.pendingToolCalls.delete(callId);
resolve({ ok: false, error: `App ${this.options.appId} did not respond to ${toolName}` });
}, TOOL_CALL_HOST_TIMEOUT_MS);
this.pendingToolCalls.set(callId, { windowId, resolve, timer });
this.post({ type: "tool-call", callId, windowId, name: toolName, args });
});
}
private failPendingToolCallsFor(windowId: string): void {
for (const [callId, pending] of this.pendingToolCalls) {
if (pending.windowId !== windowId) continue;
clearTimeout(pending.timer);
this.pendingToolCalls.delete(callId);
pending.resolve({ ok: false, error: "The target window was closed" });
}
}
private post(message: WorkerAppMessage): void {
this.options.worker.postMessage(message);
}
}