mark electron renderer for codex bridge
This commit is contained in:
parent
31a8e07a5b
commit
a54e847c8e
5 changed files with 82 additions and 17 deletions
|
|
@ -33,4 +33,8 @@ const bridge = {
|
|||
}
|
||||
};
|
||||
|
||||
contextBridge.exposeInMainWorld("islandflowDesktopRuntime", {
|
||||
shell: "electron",
|
||||
app: "islandflow"
|
||||
});
|
||||
contextBridge.exposeInMainWorld("islandflowDesktop", bridge);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { describe, expect, it } from "bun:test";
|
|||
|
||||
import {
|
||||
createUnavailableState,
|
||||
detectDesktopRuntimeMarker,
|
||||
detectDesktopShell,
|
||||
resolveDesktopAiRuntime,
|
||||
} from "./desktop-ai";
|
||||
|
|
@ -18,6 +19,22 @@ import {
|
|||
} from "./desktop-ai-panels";
|
||||
|
||||
describe("desktop ai runtime detection", () => {
|
||||
it("recognizes the explicit desktop preload marker before the bridge is available", () => {
|
||||
const runtime = resolveDesktopAiRuntime({
|
||||
islandflowDesktopRuntime: {
|
||||
app: "islandflow",
|
||||
shell: "electron",
|
||||
},
|
||||
navigator: {
|
||||
userAgent: "Mozilla/5.0 Safari/537.36",
|
||||
},
|
||||
});
|
||||
|
||||
expect(runtime.shellAvailable).toBe(true);
|
||||
expect(runtime.bridgeAvailable).toBe(false);
|
||||
expect(runtime.bridge).toBeNull();
|
||||
});
|
||||
|
||||
it("recognizes Electron user agents before the bridge is available", () => {
|
||||
const runtime = resolveDesktopAiRuntime({
|
||||
navigator: {
|
||||
|
|
@ -102,8 +119,20 @@ describe("desktop action copy", () => {
|
|||
});
|
||||
|
||||
describe("desktop shell detection", () => {
|
||||
it("matches the explicit preload runtime marker", () => {
|
||||
expect(
|
||||
detectDesktopRuntimeMarker({ app: "islandflow", shell: "electron" }),
|
||||
).toBe(true);
|
||||
expect(
|
||||
detectDesktopRuntimeMarker({ app: "islandflow", shell: "browser" }),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("matches Electron signatures", () => {
|
||||
expect(detectDesktopShell("Mozilla/5.0 Electron/39.0.0")).toBe(true);
|
||||
expect(detectDesktopShell("Mozilla/5.0 IslandflowDesktop/0.1.0")).toBe(
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
detectDesktopShell("Mozilla/5.0 Chrome/136.0.0.0 Safari/537.36"),
|
||||
).toBe(false);
|
||||
|
|
|
|||
|
|
@ -38,6 +38,10 @@ type DesktopAiRuntime = {
|
|||
|
||||
declare global {
|
||||
interface Window {
|
||||
islandflowDesktopRuntime?: {
|
||||
app?: string | null;
|
||||
shell?: string | null;
|
||||
};
|
||||
islandflowDesktop?: DesktopAiBridge;
|
||||
}
|
||||
}
|
||||
|
|
@ -59,14 +63,28 @@ type DesktopAiContextValue = {
|
|||
|
||||
const BRIDGE_POLL_INTERVAL_MS = 250;
|
||||
const BRIDGE_POLL_MAX_ATTEMPTS = 20;
|
||||
const ELECTRON_USER_AGENT_PATTERN = /\bElectron\/\S+/i;
|
||||
const ELECTRON_USER_AGENT_PATTERN = /\b(?:Electron|IslandflowDesktop)\/\S+/i;
|
||||
|
||||
export const detectDesktopShell = (userAgent: string | null | undefined): boolean =>
|
||||
Boolean(userAgent && ELECTRON_USER_AGENT_PATTERN.test(userAgent));
|
||||
|
||||
export const detectDesktopRuntimeMarker = (
|
||||
runtime:
|
||||
| {
|
||||
app?: string | null;
|
||||
shell?: string | null;
|
||||
}
|
||||
| null
|
||||
| undefined
|
||||
): boolean => runtime?.app === "islandflow" && runtime.shell === "electron";
|
||||
|
||||
export const resolveDesktopAiRuntime = (
|
||||
value:
|
||||
| {
|
||||
islandflowDesktopRuntime?: {
|
||||
app?: string | null;
|
||||
shell?: string | null;
|
||||
};
|
||||
islandflowDesktop?: DesktopAiBridge;
|
||||
navigator?: { userAgent?: string | null };
|
||||
}
|
||||
|
|
@ -75,7 +93,10 @@ export const resolveDesktopAiRuntime = (
|
|||
): DesktopAiRuntime => {
|
||||
const bridge = value?.islandflowDesktop?.ai ? value.islandflowDesktop : null;
|
||||
const bridgeAvailable = Boolean(bridge?.ai);
|
||||
const shellAvailable = bridgeAvailable || detectDesktopShell(value?.navigator?.userAgent);
|
||||
const shellAvailable =
|
||||
bridgeAvailable ||
|
||||
detectDesktopRuntimeMarker(value?.islandflowDesktopRuntime) ||
|
||||
detectDesktopShell(value?.navigator?.userAgent);
|
||||
|
||||
return {
|
||||
shellAvailable,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue