Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

8.4 KB
Raw Blame
// What the chrome draws: the wasm's views, re-read whenever it says
// something changed. The wasm owns the state; this is a copy of what it
// shows, kept in runes so the components follow it.

import { flushSync } from 'svelte';
import { everyClient, handle, type Client } from './client';
import type { AgentsView, NavsView, PluginsView, SidebarView, SnippetsView, StatusView, Strings, TabsView, WebSettings } from './model';

export const views = $state({
  tabs: null as TabsView | null,
  navs: [] as NavsView,
  status: { toast: null, card: null, summary: '' } as StatusView,
  sidebar: {
    rows: [], editing: { kind: 'none' }, space: null, new_project_error: null,
    reveal: { edge: 6, dwell_ms: 150, retreat_ms: 250 }, footer: [], footer_label_min_width: 168,
  } as SidebarView,
  /** The right-hand panel's rows, whether it is on show or not. */
  agents: { rows: [], summary: '', tabs: [], active: 'agents', labeled: true } as AgentsView,
  /** The right panel's Snippets tab; loading until it is first on show. */
  snippets: { state: 'loading', reason: null, rows: [], query: '', empty: '', answers: null, asked: 0, revision: 0 } as SnippetsView,
  /** The plugin list in Settings › Sidebar & Plugins. */
  plugins: { state: 'loading', status: '', rows: [], refused: null, revision: 0 } as PluginsView,
  /** Changes when a plugin's panel is to be painted anew (PluginPanel). */
  panelRevision: 0,
  /** The same, for the panel's extended view. */
  extendedRevision: 0,
  /** The panel on show asks for room beside the right panel for its
      extended view (PluginExtended). */
  panelExtended: false,
  /** The page's own preferences, as the wasm holds them (settings.rs). */
  settings: {
    language: 'system',
    theme: 'dark',
    font: { mode: 'follow' },
    'hover-reveal': true,
    'agents-panel': false,
    'palette-hotkey': 'cmd-k',
    'sidebar-width': 220,
    'scroll-mode': 'smooth',
  } as WebSettings,
  layout: '',
  /** The page's own labels in the active language; empty until attached. */
  strings: {} as Strings,
  /** What the page says before the client exists: the boot, or its failure. */
  boot: 'Loading…',
  bootFailed: false,
  /** Which step of the start the page is on: 0 the client's own code,
      1 the fonts, 2 the connection; the boot screen's bar. */
  bootStage: 0,
  /** The client is attached; the views below are its, not the boot's. */
  ready: false,
});

/** A label by catalogue id; the id itself until the client is attached. */
export function s(id: string): string {
  return views.strings[id] ?? id;
}

/** A label with a number in it: the wasm sends it with `{count}` where the
    number goes (`COUNTED_KEYS` in views.rs). */
export function sCount(id: string, count: number): string {
  return s(id).replace('{count}', String(count));
}

/** The boot's progress, and the probes' one-line results. */
export function setBoot(text: string, failed = false, stage?: number) {
  views.boot = text;
  views.bootFailed = failed;
  if (stage !== undefined) views.bootStage = stage;
}

/** Re-read every view now. Set by `activate`; a no-op before it. */
let refresh: () => void = () => {};

// The views are re-listed every frame something moved and are mostly the
// same string as last time; parsing only what changed keeps the whole
// chrome from being diffed for a cursor blink. Kept for the client on show
// and forgotten when another comes on.
let lastTabs = '';
let lastNavs = '';
let lastStatus = '';
let lastStrings = '';
let lastSidebar = '';
let lastAgents = '';
// The snippets can be long; their view is read only when it moved.
let lastSnippets = -1;
let lastPlugins = -1;
let lastPanel = -1;
let lastExtended = -1;
let lastSettings = '';

function forget() {
  lastTabs = lastNavs = lastStatus = lastStrings = lastSidebar = lastAgents = lastSettings = '';
  lastSnippets = lastPlugins = lastPanel = lastExtended = -1;
}

/** Follow a client: its views are read whenever they change while it is
    the one on show, and `also` hears of every change regardless. */
export function attach(client: Client, also: () => void = () => {}) {
  client.on_change(() => {
    also();
    if (handle.client === client) read();
  });
}

/** Put a client's views on show: the chrome draws it from here on. */
export function activate(client: Client) {
  handle.client = client;
  forget();
  refresh = read;
  views.ready = true;
  read();
}

function read() {
  const client = handle.client;
  if (!client) return;
  {
    const strings = client.strings();
    if (strings !== lastStrings) {
      lastStrings = strings;
      views.strings = JSON.parse(strings) as Strings;
      // Its words are in the language too.
      lastSnippets = -1;
      lastPlugins = -1;
    }
    const tabs = client.tabs();
    if (tabs !== lastTabs) {
      lastTabs = tabs;
      views.tabs = JSON.parse(tabs) as TabsView;
    }
    const navs = client.navs();
    if (navs !== lastNavs) {
      lastNavs = navs;
      views.navs = JSON.parse(navs) as NavsView;
    }
    const status = client.status();
    if (status !== lastStatus) {
      lastStatus = status;
      views.status = JSON.parse(status) as StatusView;
    }
    const sidebar = client.sidebar();
    if (sidebar !== lastSidebar) {
      lastSidebar = sidebar;
      views.sidebar = JSON.parse(sidebar) as SidebarView;
    }
    const agents = client.agents();
    if (agents !== lastAgents) {
      lastAgents = agents;
      views.agents = JSON.parse(agents) as AgentsView;
    }
    const snippets = client.snippets_revision();
    if (snippets !== lastSnippets) {
      lastSnippets = snippets;
      views.snippets = JSON.parse(client.snippets()) as SnippetsView;
    }
    const plugins = client.plugins_revision();
    if (plugins !== lastPlugins) {
      lastPlugins = plugins;
      views.plugins = JSON.parse(client.plugins()) as PluginsView;
    }
    const panel = client.plugin_panel_revision(false);
    if (panel !== lastPanel) {
      lastPanel = panel;
      views.panelRevision = panel;
    }
    const extended = client.plugin_panel_revision(true);
    if (extended !== lastExtended) {
      lastExtended = extended;
      views.extendedRevision = extended;
    }
    views.panelExtended = client.plugin_panel_extended();
    const settings = client.settings();
    if (settings !== lastSettings) {
      lastSettings = settings;
      views.settings = JSON.parse(settings) as WebSettings;
    }
    views.layout = client.layout();
  }
}

/** Re-read every view and put it on show now, for a caller that went to
    the wasm itself and wants what it changed drawn before it returns. */
export function refreshViews() {
  refresh();
  flushSync();
}

/** Switch the interface language; returns the locale it resolved to. */
export function setLocale(preference: string): string {
  if (!handle.client) return '';
  const languages = Array.from(navigator.languages ?? [navigator.language]);
  for (const other of everyClient()) if (other !== handle.client) other.set_locale(preference, languages);
  const code = handle.client.set_locale(preference, languages);
  refresh();
  flushSync();
  return code;
}

/** A click on the tab row or a pane's bar, delivered to the wasm. */
export function chromeClick(action: string, pane: number | null = null, tab: number | null = null): boolean {
  if (!handle.client) return false;
  const handled = handle.client.chrome_click(action, pane, tab);
  if (handled) {
    // What the click changed is on show before the handler returns, as it
    // was when the wasm wrote the markup itself: the client's own notice
    // is a frame away, and a two-press close is read back straight after
    // the press that asked for it.
    refresh();
    flushSync();
  }
  return handled;
}

/** A click in the sidebar, delivered to the wasm. */
export function sideClick(kind: string, id: string | null = null, flag: boolean | null = null): boolean {
  if (!handle.client) return false;
  const handled = handle.client.side_click(kind, id, flag);
  // As for the chrome: what the click changed is on show before the handler
  // returns, since the client's own notice is a frame away and a two-press
  // delete is read back straight after the press that asked for it.
  if (handled) {
    refresh();
    flushSync();
  }
  return handled;
}

/** Enter or Escape in the sidebar's inline input, with what was typed. */
export function sideKey(key: 'Enter' | 'Escape', value: string) {
  if (!handle.client) return;
  handle.client.side_key(key, value);
  refresh();
  flushSync();
}