Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

9.0 KB
Raw Blame
/** A Live call as the wire carries it: its state, the record a finished call
 * leaves, and the non-secret Live settings. Types only; `wire.ts` re-exports them. */

/** Which app holds the microphone of a Live call: the desktop app (its own
 * page or a server's page in it), a web browser, or a phone. Self-declared;
 * for display and logs only. The phone apps show a value they do not know as
 * "another device". */
export type LiveClient = "desktop" | "web" | "ios" | "android";
export type LiveCallStatus = "connecting" | "live" | "ending" | "ended";
/** Why a call ended. "signed-out": the sign-in or paired phone that started
 * it was signed out, revoked or unpaired. A client that does not know a
 * reason shows the call's `error` text, or plain "Call ended.". */
export type LiveEndReason =
  | "hung-up" | "idle" | "expired" | "content" | "remote-hangup" | "connection-lost"
  | "sideband-lost" | "deleted" | "shutdown" | "signed-out" | "error";

/** The one Live call a harness runs. Never carries the key or any speech. */
export interface LiveCallState {
  callId: string;
  botId: string;
  threadId: string;
  client: LiveClient;
  voice: string;
  /** epoch ms when the session was created */
  startedAt: number;
  status: LiveCallStatus;
  endReason?: LiveEndReason;
  /** short, user-facing; present when the call ended on a problem */
  error?: string;
}

export interface LiveCodexProgressFrame {
  kind: "live.codex.progress";
  callId: string;
  text: string;
}

/** A line the bot said aloud on a Live call: when it began (epoch ms) and its words. */
export interface LiveSpokenLine {
  at: number;
  text: string;
}

/** One finished Live call, written once when a call that went live ends. Of what was said it carries only the bot's own
 * spoken lines; the person's words stay in the chat as the requests the bot received. */
export interface LiveCallRecord {
  callId: string;
  botId: string;
  client: LiveClient;
  /** epoch ms, = LiveCallState.startedAt */
  startedAt: number;
  /** epoch ms when the call ended */
  endedAt: number;
  /** whole seconds: OpenAI usage seconds when given, else wall clock */
  seconds: number;
  endReason: LiveEndReason;
  /** only when LLM thread titles are on and the engine can generateText */
  title?: string;
  /** what the bot said aloud, in order; absent when the voice session sent no words (Fish, Gemini, ElevenLabs) */
  spoken?: LiveSpokenLine[];
}

/** Who runs a Live call's voice session: OpenAI itself, a GPT-Live deployment on the user's own Azure OpenAI / Foundry
 * resource, a Fish Audio Live 3 agent the harness sets up on the person's Fish Audio account, whose session the client joins with Fish's SDK,
 * the Gemini Live API on the person's Gemini key, which the client reaches with a single-use token the harness creates, or an
 * ElevenLabs agent the harness sets up on the person's ElevenLabs account, which the client reaches with a signed URL, or
 * "custom": an OpenAI-compatible server the person names (a gateway that serves the GPT-Live API), reached like OpenAI. */
export type LiveProvider = "openai" | "azure" | "fish" | "gemini" | "elevenlabs" | "custom" | "codex";

/** A bot can choose GPT-Live or Fish Live 3 while sharing the workspace's provider credentials. */
export type BotLiveOverride = { provider: "openai" } | { provider: "fish"; fishAgentId?: string };

/** `live`: the key saved for Live calls only; `openai`: the OpenAI API key from Settings → API keys, used when there is no `live` key;
 * `fishAudio`: the Fish Audio key from Settings → API keys, the only key a Fish Audio call uses;
 * `gemini`: the Gemini key from Settings → API keys, the only key a Gemini call uses;
 * `elevenlabs`: the person's own ElevenLabs key from Settings → API keys, the only key an ElevenLabs call uses. */
export type LiveKeySource = "live" | "openai" | "fishAudio" | "gemini" | "elevenlabs";

export interface LiveHistoryLimits {
  historyMessages: number;
  historyMessageChars: number;
  historyTotalChars: number;
}

/** Non-secret Live settings, as GET /api/config and PATCH /api/live/settings report them. */
export interface LiveSettings {
  /** A Live call has everything it needs: a key (`keySource` says which; never the key itself), and for Azure a valid endpoint and deployment. */
  configured: boolean;
  keySource?: LiveKeySource;
  /** Absent means "openai". With "azure" or "custom" only the `live` key is used, with "fish" only the Fish Audio key, with "gemini" only the
   * Gemini key and with "elevenlabs" only the ElevenLabs key, never the OpenAI API key. */
  provider?: LiveProvider;
  /** The Azure resource endpoint as saved, e.g. `https://my-resource.openai.azure.com`. Not a secret. */
  azureEndpoint?: string;
  /** The Azure deployment of gpt-live-1; empty means "gpt-live-1". */
  azureDeployment?: string;
  /** The custom endpoint as saved, e.g. `https://gateway.example.com/v1`. Not a secret. */
  customEndpoint?: string;
  /** The model a custom endpoint's calls use; empty means "gpt-live-1". */
  customModel?: string;
  voice: string;
  /** The Gemini Live voice for "gemini" calls; absent means Gemini's default. */
  geminiVoice?: string;
  /** The Gemini Live model for "gemini" calls; absent means Gemini's default. */
  geminiModel?: string;
  readTypedReplies: boolean;
  idleMinutes: number;
  /** Opts direct OpenAI GPT-Live WebRTC calls into speech-wake standby; absent or false keeps active inference. */
  standby?: boolean;
  /** Whole seconds before standby, from 5 to 60; defaults to 15 when enabled. */
  standbyIdleSeconds?: number;
  historyMessages?: number;
  historyMessageChars?: number;
  historyTotalChars?: number;
}

/** The single-use token Fish Audio returns for one agent session (Fish API v1 `LiveKitSessionToken`), passed verbatim to
 * `AgentSession.start({ sessionToken })` of `@fishaudio/agent-client` 1.x. Holds no API key. */
export interface LiveFishSessionToken {
  transport: "livekit";
  session_id: string;
  /** ISO time by which the client must join */
  expires_at: string;
  max_duration_seconds: number;
  livekit_url: string;
  token: string;
}

/** A "fish" call's transport, answered only to the client that started the call: Fish's session token, and the token
 * that client presents on POST /api/live/relay, the call's only route for what it heard and for the bot's replies. */
export interface LiveFishTransport {
  type: "fish";
  session: LiveFishSessionToken;
  relayToken: string;
}

/** A Gemini Live session the client opens itself: `token` is a single-use Gemini ephemeral token (`auth_tokens/…`) whose
 * setup (model, instructions, tools) the harness locked when it created it, so the client's own setup is ignored. Holds no API key. */
export interface LiveGeminiSession {
  /** The harness's own id for the call's relay; Gemini has none before the client connects. */
  session_id: string;
  token: string;
  /** The BidiGenerateContentConstrained WebSocket, without the token. */
  ws_url: string;
  /** e.g. `models/gemini-3.8-live` */
  model: string;
  /** ISO time by which the client must connect */
  expires_at: string;
}

/** A "gemini" call's transport, answered only to the client that started the call; it relays as a "fish" call does. */
export interface LiveGeminiTransport {
  type: "gemini";
  session: LiveGeminiSession;
  relayToken: string;
}

/** An ElevenLabs agent conversation the client opens itself: `signed_url` is a short-lived WebSocket URL ElevenLabs signed for
 * the harness's agent, and `instructions` the system prompt the client sends as the conversation's prompt override. Holds no
 * API key. */
export interface LiveElevenLabsSession {
  /** The harness's own id for the call's relay; ElevenLabs names the conversation only once the client connects. */
  session_id: string;
  signed_url: string;
  instructions: string;
  /** The bot's own ElevenLabs voice, sent as the conversation's voice override; absent when the call keeps the agent's voice. */
  voice_id?: string;
  /** ISO time by which the client must connect */
  expires_at: string;
}

/** An "elevenlabs" call's transport, answered only to the client that started the call; it relays as a "fish" call does. */
export interface LiveElevenLabsTransport {
  type: "elevenlabs";
  session: LiveElevenLabsSession;
  relayToken: string;
}

/** What the client of a "fish", "gemini" or "elevenlabs" call reports on POST /api/live/relay. Times are ms since the client joined the session. */
export type LiveFishRelayEvent =
  | { type: "connected" }
  | { type: "heard"; text: string; startMs: number; endMs: number }
  | { type: "spoke"; startMs: number; endMs: number }
  | { type: "delegate"; id: string; atMs: number }
  | { type: "ended"; reason: string }
  | { type: "error"; code: string };

/** What the harness hands the client of a relayed ("fish", "gemini" or "elevenlabs") call: words for the agent to speak or know (`kind` as on GPT-Live), or the end of the call. */
export type LiveFishRelayCommand =
  | { type: "append"; kind: "instructions" | "thinking" | "commentary"; content: string; delegationId: string | null }
  | { type: "close" };