refactor(webui): centralize native runtime access (#4769)

This commit is contained in:
chengyongru
2026-07-14 15:00:30 +08:00
committed by GitHub
parent b7048cf76a
commit 6c9e3a2cc3
5 changed files with 95 additions and 33 deletions
+28 -13
View File
@@ -62,7 +62,7 @@ declare global {
}
}
export function getHostApi(): NanobotHostApi | null {
function getHostApi(): NanobotHostApi | null {
if (typeof window === "undefined") return null;
return window.nanobotHost ?? null;
}
@@ -88,13 +88,27 @@ export function createRuntimeHost(
surface,
capabilities: mergedCapabilities,
socketFactory: bridge ? createHostWebSocket : undefined,
pickFolder: api?.pickFolder,
restartEngine: api?.restartEngine,
openLogs: api?.openLogs,
exportDiagnostics: api?.exportDiagnostics,
pickFolder: api ? () => api.pickFolder() : undefined,
restartEngine: api ? () => api.restartEngine() : undefined,
openLogs: api ? () => api.openLogs() : undefined,
exportDiagnostics: api ? () => api.exportDiagnostics() : undefined,
};
}
export function getRuntimeHost(
surface?: string | null,
capabilities?: Partial<RuntimeCapabilities> | null,
): RuntimeHost {
const api = getHostApi();
const runtimeSurface =
surface == null ? (api ? "native" : "browser") : toRuntimeSurface(surface);
return createRuntimeHost(runtimeSurface, capabilities);
}
export function isNativeRuntime(surface?: string | null): boolean {
return getHostApi() !== null || toRuntimeSurface(surface) === "native";
}
export function createHostWebSocket(url: string): WebSocket {
const api = getHostSocketBridge();
if (!api) {
@@ -105,19 +119,20 @@ export function createHostWebSocket(url: string): WebSocket {
function getHostSocketBridge(): HostSocketBridge | null {
const api = getHostApi();
const { closeSocket, onSocketEvent, openSocket, sendSocket } = api ?? {};
if (
!api?.openSocket
|| !api.sendSocket
|| !api.closeSocket
|| !api.onSocketEvent
!openSocket
|| !sendSocket
|| !closeSocket
|| !onSocketEvent
) {
return null;
}
return {
closeSocket: api.closeSocket,
onSocketEvent: api.onSocketEvent,
openSocket: api.openSocket,
sendSocket: api.sendSocket,
closeSocket: (id) => closeSocket.call(api, id),
onSocketEvent: (listener) => onSocketEvent.call(api, listener),
openSocket: (url) => openSocket.call(api, url),
sendSocket: (id, data) => sendSocket.call(api, id, data),
};
}