Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

3.7 KB
Raw Blame
import { HostProcessPlatform } from "@akeru/shared/hostProcess";
import { isCommandAvailable } from "@akeru/shared/shell";
import * as Effect from "effect/Effect";
import * as Encoding from "effect/Encoding";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import {
  type ProcessLaunch,
  POWERSHELL_ARGUMENTS_PREFIX,
  DETACHED_IGNORE_STDIO_OPTIONS,
  readBrowserLaunchEnv,
} from "./externalLauncherTypes.ts";

export const resolveAvailableCommand = Effect.fn("externalLauncher.resolveAvailableCommand")(
  function* (
    commands: ReadonlyArray<string>,
    env: NodeJS.ProcessEnv,
  ): Effect.fn.Return<Option.Option<string>, never, FileSystem.FileSystem | Path.Path> {
    for (const command of commands) {
      if (yield* isCommandAvailable(command, { env })) {
        return Option.some(command);
      }
    }

    return Option.none();
  },
);

export function encodeUtf16LeBase64(input: string): string {
  const bytes = new Uint8Array(input.length * 2);

  for (let index = 0; index < input.length; index += 1) {
    const code = input.charCodeAt(index);
    bytes[index * 2] = code & 0xff;
    bytes[index * 2 + 1] = code >>> 8;
  }

  return Encoding.encodeBase64(bytes);
}

export function escapePowerShellStringLiteral(input: string): string {
  return `'${input.replaceAll("'", "''")}'`;
}

export function resolvePowerShellPath(env: NodeJS.ProcessEnv = {}): string {
  return `${env.SYSTEMROOT || env.windir || String.raw`C:\Windows`}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
}

export function resolveWslPowerShellPath(): string {
  return "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe";
}

// File reveals from WSL resolve PowerShell through the interop PATH rather
// than the fixed /mnt/c mount: the automount root is configurable, and a
// PATH-resolved command keeps the advertised capability aligned with the
// availability check `launchEditor` performs before spawning.
export const WSL_POWERSHELL_COMMAND = "powershell.exe";

export function shouldUseWindowsHostFromWsl(
  platform: NodeJS.Platform,
  env: NodeJS.ProcessEnv = {},
): boolean {
  return (
    platform === "linux" &&
    (env.WSL_DISTRO_NAME !== undefined || env.WSL_INTEROP !== undefined) &&
    env.SSH_CONNECTION === undefined &&
    env.SSH_TTY === undefined &&
    env.container === undefined
  );
}

export function resolveWindowsBrowserLaunch(target: string, command: string): ProcessLaunch {
  const encodedCommand = encodeUtf16LeBase64(
    `$ProgressPreference = 'SilentlyContinue'; Start ${escapePowerShellStringLiteral(target)}`,
  );

  return {
    command,
    args: [...POWERSHELL_ARGUMENTS_PREFIX, encodedCommand],
    options: {
      detached: true,
      shell: false,
      stdin: "ignore",
      stdout: "ignore",
      stderr: "ignore",
    },
  };
}

export function buildBrowserLaunch(
  target: string,
  platform: NodeJS.Platform,
  env: NodeJS.ProcessEnv = {},
): ProcessLaunch {
  if (platform === "darwin") {
    return {
      command: "open",
      args: [target],
      options: DETACHED_IGNORE_STDIO_OPTIONS,
    };
  }

  if (platform === "win32") {
    return resolveWindowsBrowserLaunch(target, resolvePowerShellPath(env));
  }

  if (shouldUseWindowsHostFromWsl(platform, env)) {
    return resolveWindowsBrowserLaunch(target, resolveWslPowerShellPath());
  }

  return {
    command: "xdg-open",
    args: [target],
    options: DETACHED_IGNORE_STDIO_OPTIONS,
  };
}

export const resolveBrowserLaunch = Effect.fn("externalLauncher.resolveBrowserLaunch")(function* (
  target: string,
) {
  const platform = yield* HostProcessPlatform;
  const env = yield* readBrowserLaunchEnv;

  return buildBrowserLaunch(target, platform, env);
});