Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

11.0 KB
Raw Blame
// "Local computer" vs "VM": which computer kwant's browser and shell use.
// Contract: GET/PUT /api/settings/computer (docs/browser-service.md)
import { ClaudeVm, hostSupportsIsolation, parseClaudeVm, putErrorText } from './claudeVm';

export type ComputerMode = 'local' | 'vm';

/** Local mode only: the bots' Chrome runs without a window and shows in the app, unless the
 *  person opened it as a real window (to sign in or solve a CAPTCHA). */
export interface LocalWindow {
  visible: boolean;
  available: boolean;
  reason: string | null;
}

export interface ComputerSettings {
  mode: ComputerMode;
  available: Record<ComputerMode, boolean>;
  reasons: Record<ComputerMode, string | null>;
  window: LocalWindow;
  /** What vm mode runs in containers, as the server reports it ("browser", "kwnt_shell", later "cli_agents"). */
  isolates: string[];
  /** "Run Claude inside the VM"; null on servers without it. */
  claudeVm: ClaudeVm | null;
}

/** What vm mode isolated before the server said so: never the CLI agents. */
export const DEFAULT_VM_ISOLATES = ['browser', 'kwnt_shell'];

const ISOLATED_PARTS: Record<string, string> = { browser: 'browser', kwnt_shell: 'built-in shell' };

/** VM copy built from what it really isolates, so it can't promise more than the server does. */
export function vmCopy(isolates: string[], claudeOn = false): { label: string; hint: string; note: string | null } {
  // The setting, not the old umbrella flag, says whether Claude moved (ui.md decision 5)
  if (claudeOn && isolates.includes('kwnt_shell')) {
    return {
      label: 'VM (browser, shell and Claude isolated)',
      hint: 'The browser, built-in shell and Claude run in Docker containers, away from your files.',
      note: 'Codex and Grok still run on this computer, with their own file and shell tools. They won’t run in a folder Claude used in its sealed box.',
    };
  }
  if (isolates.includes('cli_agents')) {
    return { label: 'VM (isolated)', hint: 'Runs bots in Docker containers, away from your files.', note: null };
  }
  const parts = Object.keys(ISOLATED_PARTS).filter((k) => isolates.includes(k)).map((k) => ISOLATED_PARTS[k]);
  // Without Docker (the compose stack's browser service) there is no executor container.
  if (!isolates.includes('kwnt_shell')) {
    return {
      label: parts.length ? `VM (${parts.join(' and ')} isolated)` : 'VM',
      hint: parts.length === 1 ? `The ${parts[0]} runs in a Docker container, away from your files.` : 'Uses Docker containers.',
      note: "The built-in shell isn't available in this setup. Claude, Codex and Grok use their own shell tools.",
    };
  }
  return {
    label: `VM (${parts.join(' and ')} isolated)`,
    hint: `The ${parts.join(' and ')} run in Docker containers, away from your files.`,
    note: 'Claude, Codex and Grok still run on this computer, with their own file and shell tools.',
  };
}

export interface ModeCopy {
  id: ComputerMode;
  label: string;
  short: string;
  hint: string;
  /** A caveat shown under the hint. */
  note: string | null;
}

export const COMPUTER_MODES: ModeCopy[] = [
  { id: 'local', label: 'Local computer', short: 'Local', hint: 'Uses your Chrome and your files.', note: null },
  { id: 'vm', short: 'VM', ...vmCopy(DEFAULT_VM_ISOLATES) },
];

/** Said under the VM card in Local mode, on hosts where the switch would work, while it's off (state H). */
export const PICK_VM_FOR_CLAUDE = 'Pick VM to run Claude inside it too.';

/** Both options, with the VM copy taken from what this server says it isolates. */
export const computerModes = (s: ComputerSettings | null | undefined): ModeCopy[] => {
  if (!s) return COMPUTER_MODES;
  return COMPUTER_MODES.map((m) => {
    if (m.id !== 'vm') return m;
    const copy = vmCopy(s.isolates, !!s.claudeVm?.on);
    if (s.mode === 'local' && s.claudeVm && !s.claudeVm.on && hostSupportsIsolation(s.claudeVm) && s.isolates.includes('kwnt_shell')) {
      copy.note = copy.note ? `${copy.note} ${PICK_VM_FOR_CLAUDE}` : PICK_VM_FOR_CLAUDE;
    }
    return { ...m, ...copy };
  });
};

export const LOCAL_BROWSER_NOTE = "Your bots' Chrome is open as a window on your computer.";

/** Whether the browser is in its own window instead of the app's live view. */
export const showsLocalWindow = (s: ComputerSettings | null | undefined) => s?.mode === 'local' && s.window.visible;

/** A tab the bot hasn't opened anything in yet. Its frame is an empty page (black on macOS), so the panel shows "Browser idle". */
export const isBlankPage = (url: string | null | undefined) => {
  const u = (url ?? '').trim();
  return u === '' || u === 'about:blank';
};

/** "Ben's" for a bot's browser, "the team's" for the team page. */
export const browserOwnerWord = (botName: string | null | undefined) => (botName?.trim() ? `${botName.trim()}'s` : "the team's");

const capitalise = (t: string) => t.charAt(0).toUpperCase() + t.slice(1);

/**
 * The browser panel's footer, in sentence case: whose browser it is and what it's doing
 * ("Live view of Ben's browser", "Ben's browser is idle", "Ben's browser is off").
 */
export function browserFooterLabel(
  s: ComputerSettings | null | undefined,
  ready: boolean,
  who: { botName?: string | null; browsing?: boolean } = {},
): string {
  const whose = browserOwnerWord(who.botName);
  if (s?.mode === 'local' && s.window.visible) return capitalise(`${whose} browser is open in a Chrome window`);
  if (!ready) return capitalise(`${whose} browser is off`);
  return who.browsing ? `Live view of ${whose} browser` : capitalise(`${whose} browser is idle`);
}

/** Fired on window after a successful switch so every open control shows the new mode. */
export const COMPUTER_MODE_EVENT = 'kwnt:computer-mode';

const isMode = (v: unknown): v is ComputerMode => v === 'local' || v === 'vm';

/** The response, or null when it isn't the expected shape. */
export function parseComputerSettings(data: unknown): ComputerSettings | null {
  if (!data || typeof data !== 'object') return null;
  const d = data as Record<string, any>;
  if (!isMode(d.mode) || !d.available || typeof d.available !== 'object') return null;
  const reason = (m: ComputerMode) => (typeof d.reasons?.[m] === 'string' && d.reasons[m].trim() ? d.reasons[m] : null);
  const w = d.window && typeof d.window === 'object' ? d.window : {};
  return {
    mode: d.mode,
    available: { local: d.available.local === true, vm: d.available.vm === true },
    reasons: { local: reason('local'), vm: reason('vm') },
    window: {
      visible: w.visible === true,
      available: w.available === true,
      reason: typeof w.reason === 'string' && w.reason.trim() ? w.reason : null,
    },
    // Older servers don't send it; they isolated the same things.
    isolates: Array.isArray(d.isolates) ? d.isolates.filter((x: unknown): x is string => typeof x === 'string') : DEFAULT_VM_ISOLATES,
    claudeVm: parseClaudeVm(d),
  };
}

export const otherMode = (m: ComputerMode): ComputerMode => (m === 'local' ? 'vm' : 'local');

export const modeLabel = (m: ComputerMode, s?: ComputerSettings | null) => computerModes(s).find((x) => x.id === m)!.label;

/** Why `mode` can't be picked right now, or null when it can. */
export function unavailableReason(settings: ComputerSettings, mode: ComputerMode): string | null {
  if (settings.available[mode]) return null;
  return settings.reasons[mode] ?? `${modeLabel(mode, settings)} isn't available on this computer.`;
}

export interface SwitchState {
  settings: ComputerSettings | null;
  /** The mode being saved; shown as selected until the server answers. */
  pending: ComputerMode | null;
  error: string | null;
}

export type SwitchAction =
  | { type: 'loaded'; settings: ComputerSettings }
  | { type: 'start'; mode: ComputerMode }
  | { type: 'saved'; settings: ComputerSettings }
  | { type: 'failed'; error: string }
  | { type: 'dismiss' };

export const initialSwitchState: SwitchState = { settings: null, pending: null, error: null };

export function switchReducer(state: SwitchState, action: SwitchAction): SwitchState {
  switch (action.type) {
    case 'loaded':
      return state.pending ? state : { ...state, settings: action.settings };
    case 'start':
      return { ...state, pending: action.mode, error: null };
    case 'saved':
      return { settings: action.settings, pending: null, error: null };
    case 'failed':
      // Dropping `pending` puts the control back on the saved mode.
      return { ...state, pending: null, error: action.error };
    case 'dismiss':
      return { ...state, error: null };
  }
}

/** The mode a control should show as selected. */
export const shownMode = (s: SwitchState): ComputerMode | null => s.pending ?? s.settings?.mode ?? null;

/** null when this server has no computer setting (older build) or it couldn't be read. */
export async function fetchComputerSettings(): Promise<ComputerSettings | null> {
  const res = await fetch('/api/settings/computer', { cache: 'no-store' });
  if (!res.ok) return null;
  return parseComputerSettings(await res.json().catch(() => null));
}

type SaveResult = { ok: true; settings: ComputerSettings } | { ok: false; error: string };

async function putSettings(path: string, body: unknown, failed: string): Promise<SaveResult> {
  let res: Response;
  try {
    res = await fetch(path, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(body),
    });
  } catch {
    return { ok: false, error: 'Could not reach the server.' };
  }
  const data = await res.json().catch(() => null);
  if (!res.ok) {
    const msg = data && typeof data.error === 'string' && data.error.trim() ? data.error : `${failed} (HTTP ${res.status}).`;
    return { ok: false, error: msg };
  }
  const settings = parseComputerSettings(data);
  return settings ? { ok: true, settings } : { ok: false, error: 'The server sent an unexpected answer.' };
}

export const saveComputerMode = (mode: ComputerMode) => putSettings('/api/settings/computer', { mode }, 'Switching failed');

/** Opens the bots' Chrome as a window (`true`) or puts it back in the app (`false`). */
/** `untilSignedIn`: the window is for a sign-in, and comes back into the app once it is over. */
export const saveLocalWindow = (visible: boolean, untilSignedIn = false) =>
  putSettings(
    '/api/settings/computer/window',
    untilSignedIn ? { visible, until_signed_in: true } : { visible },
    visible ? 'Could not open the window' : 'Could not bring the browser back',
  );

/** Turns "Run Claude inside the VM" on or off. A 400's code is put in people's words (fix 5). */
export async function saveIsolateClaude(on: boolean): Promise<SaveResult> {
  let res: Response;
  try {
    res = await fetch('/api/settings/computer', {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ isolate_claude: on }),
    });
  } catch {
    return { ok: false, error: 'Could not reach the server.' };
  }
  const data = await res.json().catch(() => null);
  if (!res.ok) return { ok: false, error: putErrorText(res.status, data) };
  const settings = parseComputerSettings(data);
  return settings ? { ok: true, settings } : { ok: false, error: 'The server sent an unexpected answer.' };
}