@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
57 lines • 1.99 kB
JavaScript
/**
* Messages on one kernel client's MessagePort to the main-thread WebView service. The eval
* kernel's worker side (`senpi-codemode/src/kernels/js/worker-webview-client.js`) is plain
* JavaScript and mirrors these shapes; keep the two in sync.
*/
export const WEBVIEW_METHODS = [
"navigate",
"evaluate",
"screenshot",
"click",
"type",
"press",
"scroll",
"scrollTo",
"resize",
"goBack",
"goForward",
"reload",
"cdp",
];
function isRecord(value) {
return typeof value === "object" && value !== null;
}
function isMethod(value) {
return WEBVIEW_METHODS.some((method) => method === value);
}
/** Parses one message from a kernel worker; anything else is dropped at this boundary. */
export function parseClientMessage(value) {
if (!isRecord(value))
return undefined;
const { kind, id, viewId } = value;
if (kind === "close-all")
return typeof id === "number" ? { kind, id } : undefined;
if (typeof viewId !== "string")
return undefined;
if (kind === "close")
return { kind, viewId };
if (kind === "listen")
return typeof value.event === "string" ? { kind, viewId, event: value.event } : undefined;
if (typeof id !== "number")
return undefined;
if (kind === "create") {
const options = isRecord(value.options) ? value.options : {};
return { kind, id, viewId, options, captureConsole: value.captureConsole === true };
}
if (kind === "call" && isMethod(value.method) && Array.isArray(value.args)) {
return { kind, id, viewId, method: value.method, args: value.args };
}
return undefined;
}
export function wireError(error) {
if (!(error instanceof Error))
return { name: "Error", message: String(error) };
const code = Reflect.get(error, "code");
return { name: error.name, message: error.message, ...(typeof code === "string" ? { code } : {}) };
}
//# sourceMappingURL=webview-wire.js.map