sailfork / t3code-github / apps/ desktop/ src/ ipc/ methods/ window.ts

import {
  ContextMenuItemSchema,
  DesktopAppBrandingSchema,
  DesktopEnvironmentBootstrapSchema,
  DesktopMediaReadSchema,
  DesktopThemeSchema,
  EDITORS,
  EditorId,
  PickedThemeFileSchema,
  PickFolderOptionsSchema,
  PRIMARY_LOCAL_ENVIRONMENT_ID,
  REMOTE_CAPABLE_EDITOR_IDS,
  SystemSettingsPaneSchema,
  type DesktopEnvironmentBootstrap,
  type PickedThemeFile,
} from "@t3tools/contracts";
import { PROJECT_FAVICON_EXTENSIONS } from "@t3tools/shared/projectFavicon";
import { resolveEditorCommand } from "@t3tools/shared/editor";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { isRelayTracingEnabled } from "@t3tools/shared/otelEnvironment";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";

import * as DesktopBackendConfiguration from "../../backend/DesktopBackendConfiguration.ts";
import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
import * as DesktopLocalEnvironmentAuth from "../../backend/DesktopLocalEnvironmentAuth.ts";
import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
import * as DesktopManagedPolicy from "../../app/DesktopManagedPolicy.ts";
import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts";
import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
import * as ElectronApp from "../../electron/ElectronApp.ts";
import * as ElectronDialog from "../../electron/ElectronDialog.ts";
import * as ElectronMenu from "../../electron/ElectronMenu.ts";
import * as ElectronShell from "../../electron/ElectronShell.ts";
import * as ElectronTheme from "../../electron/ElectronTheme.ts";
import * as ElectronWindow from "../../electron/ElectronWindow.ts";
import * as Electron from "electron";
import * as MacPermissions from "../../permissions/MacPermissions.ts";
import { safariPermissionCheck } from "../../preview/BrowserImport/SafariPermission.ts";
import * as IpcChannels from "../channels.ts";
import * as DesktopIpc from "../DesktopIpc.ts";
import {
  extractDistroFromUncPath,
  resolveWslPickFolderDefaultPath,
  wslUncPathToLinuxPath,
} from "../../wsl/wslPathParsing.ts";

const ContextMenuPosition = Schema.Struct({
  x: Schema.Number,
  y: Schema.Number,
});

const ContextMenuInput = Schema.Struct({
  items: Schema.Array(ContextMenuItemSchema),
  position: Schema.optionalKey(ContextMenuPosition),
});

function toWebSocketBaseUrl(httpBaseUrl: URL): string {
  const url = new URL(httpBaseUrl.href);
  url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
  return url.href;
}

export const getAppBranding = DesktopIpc.makeSyncIpcMethod({
  channel: IpcChannels.GET_APP_BRANDING_CHANNEL,
  result: Schema.NullOr(DesktopAppBrandingSchema),
  handler: Effect.fn("desktop.ipc.window.getAppBranding")(function* () {
    const environment = yield* DesktopEnvironment.DesktopEnvironment;
    return environment.branding;
  }),
});

/**
 * Relay traces leave the machine, so a policy with `telemetry: false`, or one that cannot be read,
 * stops them. Read once here: a sync IPC handler cannot wait on the filesystem.
 */
export const installRelayTelemetryEnabled = Effect.fn("desktop.ipc.window.installRelayTelemetry")(
  function* () {
    const ipc = yield* DesktopIpc.DesktopIpc;
    const enabled =
      isRelayTracingEnabled(yield* HostProcess.Environment) &&
      (yield* (yield* DesktopManagedPolicy.DesktopManagedPolicy).read.pipe(
        Effect.map((found) => found?.document.limits?.telemetry !== false),
        Effect.orElseSucceed(() => false),
      ));
    yield* ipc.handleSync(
      DesktopIpc.makeSyncIpcMethod({
        channel: IpcChannels.GET_RELAY_TELEMETRY_ENABLED_CHANNEL,
        result: Schema.Boolean,
        handler: () => Effect.succeed(enabled),
      }),
    );
  },
);

export const getSystemLocale = DesktopIpc.makeSyncIpcMethod({
  channel: IpcChannels.GET_SYSTEM_LOCALE_CHANNEL,
  result: Schema.String,
  handler: Effect.fn("desktop.ipc.window.getSystemLocale")(function* () {
    const electronApp = yield* ElectronApp.ElectronApp;
    return yield* electronApp.systemLocale;
  }),
});

export const getWindowFullscreenState = DesktopIpc.makeSyncIpcMethod({
  channel: IpcChannels.GET_WINDOW_FULLSCREEN_STATE_CHANNEL,
  result: Schema.Boolean,
  handler: Effect.fn("desktop.ipc.window.getWindowFullscreenState")(function* () {
    const electronWindow = yield* ElectronWindow.ElectronWindow;
    const window = yield* electronWindow.currentMainOrFirst;
    return Option.isSome(window) && window.value.isFullScreen();
  }),
});

export const getLocalEnvironmentBootstraps = DesktopIpc.makeSyncIpcMethod({
  channel: IpcChannels.GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL,
  result: Schema.Array(DesktopEnvironmentBootstrapSchema),
  handler: Effect.fn("desktop.ipc.window.getLocalEnvironmentBootstraps")(function* () {
    const pool = yield* DesktopBackendPool.DesktopBackendPool;
    const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
    const instances = yield* pool.list;
    const bootstraps: DesktopEnvironmentBootstrap[] = [];
    for (const instance of instances) {
      const isPrimary = instance.id === PRIMARY_LOCAL_ENVIRONMENT_ID;
      const config = yield* instance.currentConfig;
      const snapshot = yield* instance.snapshot;
      if (Option.isNone(config) || Option.isSome(config.value.preflightFailure)) {
        const fatalPreflight =
          Option.isSome(config) &&
          Option.isSome(config.value.preflightFailure) &&
          config.value.preflightFailure.value.fatal;
        const stoppedPreflight =
          Option.isSome(config) &&
          Option.isSome(config.value.preflightFailure) &&
          (!snapshot.desiredRunning || !snapshot.restartScheduled);
        if (isPrimary || fatalPreflight || stoppedPreflight) continue;
        bootstraps.push({
          id: instance.id,
          label: yield* instance.label,
          runningDistro: null,
          httpBaseUrl: null,
          wsBaseUrl: null,
        });
        continue;
      }
      const { bootstrap, httpBaseUrl } = config.value;
      const runningDistro = config.value.runningDistro ?? null;
      bootstraps.push({
        id: instance.id,
        label: runningDistro === null ? yield* instance.label : `WSL (${runningDistro})`,
        runningDistro,
        httpBaseUrl: httpBaseUrl.href,
        wsBaseUrl: toWebSocketBaseUrl(httpBaseUrl),
        // Use the current window's derived token; the launch config's token may be stale.
        ...(bootstrap.desktopBootstrapSecret
          ? { bootstrapToken: yield* configuration.currentBootstrapToken }
          : bootstrap.desktopBootstrapToken
            ? { bootstrapToken: bootstrap.desktopBootstrapToken }
            : {}),
      });
    }
    return bootstraps;
  }),
});

function extractWslDistroFromEnvironmentId(envId: string): string | null {
  if (!envId.startsWith(DesktopWslBackend.WSL_INSTANCE_ID_PREFIX)) {
    return null;
  }
  const suffix = envId.slice(DesktopWslBackend.WSL_INSTANCE_ID_PREFIX.length);
  return suffix === "default" || suffix.length === 0 ? null : suffix;
}

export const getLocalEnvironmentBearerToken = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.GET_LOCAL_ENVIRONMENT_BEARER_TOKEN_CHANNEL,
  payload: Schema.Void,
  result: Schema.String,
  handler: Effect.fn("desktop.ipc.window.getLocalEnvironmentBearerToken")(function* () {
    const localAuth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth;
    return yield* localAuth.getBearerToken;
  }),
});

export const pickFolder = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.PICK_FOLDER_CHANNEL,
  payload: Schema.UndefinedOr(PickFolderOptionsSchema),
  result: Schema.NullOr(Schema.String),
  handler: Effect.fn("desktop.ipc.window.pickFolder")(function* (options) {
    const dialog = yield* ElectronDialog.ElectronDialog;
    const electronWindow = yield* ElectronWindow.ElectronWindow;
    const environment = yield* DesktopEnvironment.DesktopEnvironment;
    const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
    const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment;
    const settings = yield* appSettings.get;
    // A picked path only means something to a backend on this machine.
    if (!settings.localEnvironmentEnabled) {
      return null;
    }
    const targetId = options?.targetEnvironmentId;
    const wslDistroFromTarget =
      targetId !== undefined && targetId.startsWith(DesktopWslBackend.WSL_INSTANCE_ID_PREFIX)
        ? extractWslDistroFromEnvironmentId(targetId)
        : null;
    const useWsl =
      targetId !== undefined &&
      targetId !== PRIMARY_LOCAL_ENVIRONMENT_ID &&
      targetId.startsWith(DesktopWslBackend.WSL_INSTANCE_ID_PREFIX);
    // Match the orchestrator's persisted distro fallback for wsl:default.
    const wslDistro = useWsl ? (wslDistroFromTarget ?? settings.wslDistro) : null;
    const defaultPath = useWsl
      ? Option.fromNullishOr(
          resolveWslPickFolderDefaultPath(
            options,
            { distro: wslDistro },
            yield* wslEnvironment.listDistros,
            Option.getOrNull(yield* wslEnvironment.getUserHome(wslDistro)),
          ),
        )
      : environment.resolvePickFolderDefaultPath(options);
    const selectedPath = yield* dialog.pickFolder({
      owner: yield* electronWindow.focusedMainOrFirst,
      defaultPath,
    });
    if (Option.isNone(selectedPath)) {
      return null;
    }
    if (!useWsl) {
      return selectedPath.value;
    }

    const linuxUncPath = wslUncPathToLinuxPath(selectedPath.value);
    if (linuxUncPath !== null) {
      return linuxUncPath;
    }

    const converted = yield* wslEnvironment.windowsToWslPath(
      extractDistroFromUncPath(selectedPath.value) ?? wslDistro,
      selectedPath.value,
    );
    return Option.getOrElse(converted, () => selectedPath.value);
  }),
});

export const pickProjectFavicon = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.PICK_PROJECT_FAVICON_CHANNEL,
  payload: Schema.UndefinedOr(Schema.String),
  result: Schema.NullOr(Schema.String),
  handler: Effect.fn("desktop.ipc.window.pickProjectFavicon")(function* (initialPath) {
    const dialog = yield* ElectronDialog.ElectronDialog;
    const electronWindow = yield* ElectronWindow.ElectronWindow;
    const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
    if (!(yield* appSettings.get).localEnvironmentEnabled) {
      return null;
    }
    const paths = yield* dialog.pickFiles({
      owner: yield* electronWindow.focusedMainOrFirst,
      defaultPath: Option.fromNullishOr(initialPath),
      multiple: false,
      filters: [
        {
          name: "Images",
          extensions: PROJECT_FAVICON_EXTENSIONS.map((extension) => extension.slice(1)),
        },
      ],
    });
    return paths[0] ?? null;
  }),
});

export const setTheme = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.SET_THEME_CHANNEL,
  payload: DesktopThemeSchema,
  result: Schema.Void,
  handler: Effect.fn("desktop.ipc.window.setTheme")(function* (theme) {
    const electronTheme = yield* ElectronTheme.ElectronTheme;
    yield* electronTheme.setSource(theme);
  }),
});

export const showContextMenu = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.CONTEXT_MENU_CHANNEL,
  payload: ContextMenuInput,
  result: Schema.NullOr(Schema.String),
  handler: Effect.fn("desktop.ipc.window.showContextMenu")(function* (input) {
    const electronMenu = yield* ElectronMenu.ElectronMenu;
    const electronWindow = yield* ElectronWindow.ElectronWindow;
    const window = yield* electronWindow.focusedMainOrFirst;
    if (Option.isNone(window)) {
      return null;
    }

    const selectedItemId = yield* electronMenu.showContextMenu({
      window: window.value,
      items: input.items,
      position: Option.fromNullishOr(input.position),
    });
    return Option.getOrNull(selectedItemId);
  }),
});

export const openExternal = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.OPEN_EXTERNAL_CHANNEL,
  payload: Schema.String,
  result: Schema.Boolean,
  handler: Effect.fn("desktop.ipc.window.openExternal")(function* (url) {
    const shell = yield* ElectronShell.ElectronShell;
    return yield* shell.openExternal(url);
  }),
});

const READ_MEDIA_MAX_BYTES = 256 * 1024 * 1024;

// No URL field: media URLs can carry signed query tokens.
class MediaReadError extends Schema.TaggedError<MediaReadError>()("MediaReadError", {
  message: Schema.String,
  cause: Schema.Defect(),
}) {}

// The renderer can display media it cannot fetch(); reading here keeps Save and Copy working for it.
export const readMedia = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.READ_MEDIA_CHANNEL,
  payload: Schema.String,
  result: Schema.NullOr(DesktopMediaReadSchema),
  handler: Effect.fn("desktop.ipc.window.readMedia")(function* (url, event) {
    const window = yield* (yield* ElectronWindow.ElectronWindow).main;
    if (
      event === undefined ||
      Option.isNone(window) ||
      window.value.webContents.id !== event.sender.id ||
      !/^https?:$/.test(URL.parse(url)?.protocol ?? "")
    ) {
      return null;
    }
    const session = window.value.webContents.session;
    return yield* Effect.tryPromise({
      try: async () => {
        // Like an <img>, send the session's cookies.
        const response = await session.fetch(url, { credentials: "include" });
        const mimeType = response.headers.get("content-type") ?? "";
        if (!response.ok || !response.body) {
          await response.body?.cancel();
          return { status: response.status, mimeType, data: new Uint8Array() };
        }
        // The whole body crosses IPC, so stop before a huge file can exhaust main-process memory.
        const chunks: Uint8Array[] = [];
        let size = 0;
        for await (const chunk of response.body) {
          size += chunk.byteLength;
          if (size > READ_MEDIA_MAX_BYTES) {
            return { status: 413, mimeType, data: new Uint8Array() };
          }
          chunks.push(chunk);
        }
        return { status: response.status, mimeType, data: Buffer.concat(chunks) };
      },
      catch: (cause) => new MediaReadError({ message: "Could not read media.", cause }),
    }).pipe(Effect.orElseSucceed(() => null));
  }),
});

export const openSystemSettings = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.OPEN_SYSTEM_SETTINGS_CHANNEL,
  payload: SystemSettingsPaneSchema,
  result: Schema.Boolean,
  handler: Effect.fn("desktop.ipc.window.openSystemSettings")(function* (pane) {
    const shell = yield* ElectronShell.ElectronShell;
    const environment = yield* DesktopEnvironment.DesktopEnvironment;
    if (environment.platform !== "darwin") return false;
    const owner = Electron.BrowserWindow.getFocusedWindow();
    const opened = yield* shell.openSystemSettings(pane);
    if (opened && environment.isPackaged) {
      const permissions = yield* MacPermissions.MacPermissions;
      const isGranted = yield* safariPermissionCheck;
      yield* permissions.showHelper(pane, owner, isGranted);
    }
    return opened;
  }),
});

export const probeRemoteEditors = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL,
  payload: Schema.Undefined,
  result: Schema.Array(EditorId),
  handler: Effect.fn("desktop.ipc.window.probeRemoteEditors")(function* () {
    const available: Array<EditorId> = [];
    const env = yield* HostProcess.Environment;
    for (const editorId of REMOTE_CAPABLE_EDITOR_IDS) {
      const editor = EDITORS.find((editor) => editor.id === editorId);
      if (editor && Option.isSome(yield* resolveEditorCommand(editor, env))) {
        available.push(editorId);
      }
    }
    return available;
  }),
});

export const pasteAsText = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.PASTE_AS_TEXT_CHANNEL,
  payload: Schema.Undefined,
  result: Schema.Void,
  handler: Effect.fn("desktop.ipc.window.pasteAsText")(function* (_input, event) {
    const electronWindow = yield* ElectronWindow.ElectronWindow;
    const window = yield* electronWindow.main;
    if (
      event === undefined ||
      Option.isNone(window) ||
      window.value.isDestroyed() ||
      window.value.webContents.id !== event.sender.id
    ) {
      return;
    }
    const focused = Electron.webContents.getFocusedWebContents();
    if (
      focused &&
      !focused.isDestroyed() &&
      Electron.BrowserWindow.fromWebContents(focused) === window.value
    ) {
      focused.paste();
    }
  }),
});

export const revealWindow = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.REVEAL_WINDOW_CHANNEL,
  payload: Schema.Undefined,
  result: Schema.Void,
  handler: Effect.fn("desktop.ipc.window.revealWindow")(function* (_input, event) {
    const electronWindow = yield* ElectronWindow.ElectronWindow;
    const window = yield* electronWindow.main;
    if (
      event === undefined ||
      Option.isNone(window) ||
      window.value.webContents.id !== event.sender.id
    ) {
      return;
    }
    yield* electronWindow.reveal(window.value);
  }),
});

const PICKED_THEME_FILE_MAX_BYTES = 256 * 1024;

export const pickThemeFiles = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.PICK_THEME_FILES_CHANNEL,
  payload: Schema.Undefined,
  result: Schema.NullOr(Schema.Array(PickedThemeFileSchema)),
  handler: Effect.fn("desktop.ipc.window.pickThemeFiles")(function* () {
    const dialog = yield* ElectronDialog.ElectronDialog;
    const electronWindow = yield* ElectronWindow.ElectronWindow;
    const fileSystem = yield* FileSystem.FileSystem;
    const path = yield* Path.Path;
    const extensionsDir = path.join(yield* HostProcess.HomeDirectory, ".vscode", "extensions");
    const defaultPath = yield* fileSystem
      .exists(extensionsDir)
      .pipe(Effect.orElseSucceed(() => false));
    const paths = yield* dialog.pickFiles({
      owner: yield* electronWindow.focusedMainOrFirst,
      defaultPath: defaultPath ? Option.some(extensionsDir) : Option.none(),
      filters: [{ name: "JSON", extensions: ["json"] }],
      multiple: true,
    });
    if (paths.length === 0) {
      return null;
    }
    return yield* Effect.forEach(paths, (filePath) => {
      const name = path.basename(filePath);
      return Effect.gen(function* () {
        const info = yield* fileSystem.stat(filePath);
        const size = Number(info.size);
        if (size > PICKED_THEME_FILE_MAX_BYTES) {
          return { name, size, text: "" } satisfies PickedThemeFile;
        }
        const text = yield* fileSystem.readFileString(filePath);
        return { name, size, text } satisfies PickedThemeFile;
      }).pipe(Effect.orElseSucceed((): PickedThemeFile => ({ name, size: 0, text: "" })));
    });
  }),
});

export const checkSystemPermission = DesktopIpc.makeIpcMethod({
  channel: IpcChannels.CHECK_SYSTEM_PERMISSION_CHANNEL,
  payload: SystemSettingsPaneSchema,
  result: Schema.Boolean,
  handler: Effect.fn("desktop.ipc.window.checkSystemPermission")(function* () {
    const environment = yield* DesktopEnvironment.DesktopEnvironment;
    if (environment.platform !== "darwin") return false;
    const check = yield* safariPermissionCheck;
    return yield* Effect.promise(check);
  }),
});