Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

5.1 KB
Raw Blame
import type { AccountState, CallError, CodyncBridge, HostSnapshot, UpdateState } from '@shared/ipc'
import { DEMO_UNAVAILABLE, FakeHost } from './fake-host'
import { COMPUTER_ID, HOST_NAME } from './story'
import { version } from '../../../package.json'

// `window.codync` for the browser: a running local host served by `FakeHost`, everything else
// signed out, unavailable or a no-op. Imported before the renderer so module-level reads see it.

const host = new FakeHost(version)
const none = () => () => {}
const unavailable = () => Promise.reject(new Error(DEMO_UNAVAILABLE))

const snapshot: HostSnapshot = {
  state: { kind: 'running' },
  local: { computerId: COMPUTER_ID, baseURL: 'http://demo.invalid', token: 'demo' },
  dev: false,
  logPath: '',
}
const account: AccountState = { ready: true, configured: false, busy: false, error: null, user: null, cloudURL: null }
const updates: UpdateState = {
  supported: false, checking: false, availableVersion: null, downloadPercent: null, staged: false, installing: false,
  autoCheck: false, autoDownload: false, lastCheck: null, error: null,
}

const bridge: CodyncBridge = {
  platform: 'darwin',
  appName: 'Codync',
  appScheme: 'codync',
  hostPort: 19222,
  appVersion: version,
  computerName: HOST_NAME,
  openedAtLogin: false,
  // Opens the group at launch, like the website's screenshot.
  debugOpen: 'Ship room',
  host: {
    snapshot: async () => snapshot,
    onChange: none,
    install() {},
    restart() {},
    async call(_baseURL, _token, method, body) {
      await new Promise((r) => setTimeout(r, 30))
      try {
        return await host.call(method, (body ?? {}) as Record<string, unknown>)
      } catch (e) {
        throw (e as CallError).status ? e : ({ status: 500, message: String(e) } satisfies CallError)
      }
    },
    stream(url, _token, onData) {
      const since = Number(new URL(url).searchParams.get('since') ?? 0)
      return host.subscribe(since, onData)
    },
  },
  account: {
    state: async () => account,
    onChange: none,
    signIn: unavailable,
    signOut: async () => {},
    deleteAccount: async () => false,
    token: unavailable,
  },
  speech: { available: false, locales: () => [], start() {}, stop() {}, onEvent: none, shortcut: async () => 'off', setShortcut: async () => 'off', setCalling() {} },
  ssh: {
    state: async () => ({ profiles: [], status: {}, attachments: [], needsLinger: [] }),
    onChange: none,
    save: async () => DEMO_UNAVAILABLE,
    remove() {},
    connect() {},
    disconnect() {},
    trustHostKey() {},
    install() {},
    chooseKey: async () => null,
    configHosts: async () => ({ access: 'denied', hosts: [] }),
    setConfigAccess: async () => ({ access: 'denied', hosts: [] }),
  },
  island: {
    supported: false,
    enabled: async () => false,
    setEnabled: async () => false,
    setSummary() {},
    state: async () => ({ screen: { notch: null }, summary: { bots: [], permissions: [], usage: [] } }),
    onSummary: none,
    onScreen: none,
    onHover: none,
    setShape() {},
    setFocusable() {},
    setKeys() {},
    onKey: none,
    onVoiceKey: none,
    respond() {},
    send() {},
    openBot() {},
    newBot() {},
    openApp() {},
  },
  cloud: { request: async () => ({ ok: false, error: { status: 503, code: 'demo', message: DEMO_UNAVAILABLE } }) },
  updates: { state: async () => updates, onChange: none, check() {}, install() {}, setAutoCheck() {}, setAutoDownload() {} },
  artifacts: { show: async () => {}, openInBrowser: unavailable },
  files: { begin: unavailable, write: async () => {}, finish: async () => {}, cancel: async () => {} },
  app: {
    reportError() {},
    openExternal: (url) => void window.open(url, '_blank', 'noopener'),
    copy: (text) => void navigator.clipboard?.writeText(text).catch(() => {}),
    pickFiles: async () => [],
    pickExport: async () => null,
    readClipboardFiles: async () => [],
    setTraySummary() {},
    showWindow() {},
    onCommand: none,
    resetAllData: async () => {},
    setLaunchAtLogin: async () => false,
    appearance: async () => 'system',
    setAppearance: async (value) => value,
    showInDock: async () => true,
    setShowInDock: async () => true,
    launchAtLogin: async () => false,
    openSettings() {},
    authenticate: unavailable,
    screenAgentState: async () => ({ available: false, needsApproval: false, registered: false }),
    setScreenAgent: unavailable,
    syncScreenAgent: async () => ({ needsApproval: false }),
  },
}

// Every visit starts from the story: no mirror cache or drafts from a previous visit, onboarding done,
// no GitHub star ask over the demo.
try {
  for (const key of Object.keys(localStorage)) if (key.startsWith('codync-')) localStorage.removeItem(key)
  localStorage.setItem('macAccountOnboardingCompleted', 'true')
  localStorage.setItem('computerAccessSetup', JSON.stringify('later'))
  localStorage.setItem('githubStarAsk', JSON.stringify({ kind: 'done' }))
} catch {}

// The dark palette everywhere (vite.demo.config.ts does the same for the CSS).
const matchMedia = window.matchMedia.bind(window)
window.matchMedia = (query) => matchMedia(query.includes('prefers-color-scheme: dark') ? 'all' : query)

window.codync = bridge