Skip to content

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

18.9 KB
Raw Blame
// Fish Audio Live 3 calls. The Fish key never leaves the harness: the client gets only Fish's single-use session token,
// and Fish has no server sideband, so the client relays what was said and the harness answers through FishRelay.
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
import type { LiveFishRelayCommand, LiveFishRelayEvent, LiveFishSessionToken, LiveHistoryLimits, LiveProvider } from "../shared/wire.ts";
import type { LiveSocket } from "./live-call-controller.ts";
import { liveInstructions, LiveSessionError, selectLiveHistory, type LiveBot, type LiveEndpoint, type LiveHistoryMessage } from "./live-call.ts";
import type { LiveConfig } from "./live-config.ts";

const FISH_BASE = "https://api.fish.audio";
const CREATE_TIMEOUT_MS = 20_000;
const END_TIMEOUT_MS = 5_000;
const SETUP_TIMEOUT_MS = 8_000;
/** Fish agent ids as the dashboard and `GET /v1/agent/agents` show them. */
export const FISH_AGENT_ID = /^[\w-]{1,128}$/;
export const FISH_DELEGATE_TOOL = "delegate";
/** What a "fish" call reports as its voice: the voice is set on the Fish agent, not per call. */
export const FISH_AGENT_VOICE = "agent";
export const FISH_UPDATE_PREFIX = "[OpenMausBot]";
const MAX_QUEUED_COMMANDS = 50;
export const RELAY_POLL_MS = 15_000;

/** Only a loopback http URL is accepted, read at call time. */
export function fishLiveBaseUrl(env: NodeJS.ProcessEnv = process.env): string {
  const override = env.OMB_FISH_LIVE_URL?.trim() ?? "";
  return /^http:\/\/127\.0\.0\.1:\d{1,5}$/.test(override) ? override : FISH_BASE;
}

/** `model` is the agent id, "" while no usable one is saved. */
export function fishLiveEndpoint(agentId: string | null | undefined, env: NodeJS.ProcessEnv = process.env): LiveEndpoint {
  const id = agentId?.trim() ?? "";
  return { provider: "fish", apiBase: `${fishLiveBaseUrl(env)}/v1/agent`, model: FISH_AGENT_ID.test(id) ? id : "" };
}

/** Which Fish Audio account an agent was set up on, without keeping its key. */
export function fishKeyPrint(key: string): string {
  return createHash("sha256").update(key.trim()).digest("hex").slice(0, 16);
}

/** The saved agent, while it belongs to the account of `key` or was set by the operator (no `fishAgentFor`). */
export function usableFishAgent(live: LiveConfig | undefined, key: string): string | null {
  const id = live?.fishAgentId?.trim() ?? "";
  if (!FISH_AGENT_ID.test(id)) return null;
  return live?.fishAgentFor === undefined || (key.trim() !== "" && live.fishAgentFor === fishKeyPrint(key)) ? id : null;
}

/** The `voice.speaking_language` codes Fish Audio agents take. */
const FISH_SPEAKING_LANGUAGES = new Set([
  "en", "ja", "zh", "ko", "es", "fr", "de", "pt", "it", "nl", "ar", "hi", "id", "tr", "ru", "bg", "sr", "hr", "cs", "sk", "pl", "uk", "ro",
  "hu", "el", "sv", "da", "no", "fi", "et", "lv", "lt", "ca", "he", "fa", "ur", "kk", "ka", "hy", "bn", "ta", "te", "kn", "mr", "gu", "pa",
  "ne", "th", "vi", "ms", "tl", "af",
]);

/** The language a Fish agent speaks and listens for with the app in `language` (a locale such as "vi" or "pt-br"):
 * its base code when Fish Audio speaks it, else English. */
export function fishSpeakingLanguage(language: string | undefined): string {
  const base = language?.trim().toLowerCase().split(/[-_]/)[0] ?? "";
  return FISH_SPEAKING_LANGUAGES.has(base) ? base : "en";
}

/** Fish API v1: a Live 3 agent speaking `language` named for `product`, with the client tool `delegate` and a system prompt
 * each session may override (`createFishLiveSession` sends one), published so sessions can start. Returns its id. */
export async function setUpFishAgent(key: string, endpoint: LiveEndpoint, product: string, language: string, fetchImpl: typeof fetch = fetch): Promise<string> {
  const call = (method: string, path: string, body: unknown) => fishSetupRequest(fetchImpl, key, method, `${endpoint.apiBase}${path}`, body);
  const tool = await call("POST", "/tools", {
    name: FISH_DELEGATE_TOOL,
    tool_type: "client",
    description: "Hands the user's request to the assistant that does the work: looking things up, using apps and files, running tasks.",
    parameters: { type: "object", properties: { request: { type: "string", description: "What the user asked for, in their own words." } }, required: ["request"] },
    expects_response: true,
    timeout_seconds: 30,
  });
  const toolId = typeof tool.tool_id === "string" ? tool.tool_id : "";
  if (!toolId) throw new LiveSessionError(FISH_UNEXPECTED, 502);
  const agent = await call("POST", "/agents", {
    name: `${product} Live calls`.slice(0, 120),
    config: { voice: { engine: "live3", speaking_language: language }, tools: { enabled: true, tool_ids: [toolId] } },
  });
  const agentId = typeof agent.agent_id === "string" && FISH_AGENT_ID.test(agent.agent_id) ? agent.agent_id : "";
  if (!agentId) throw new LiveSessionError(FISH_UNEXPECTED, 502);
  await call("PATCH", `/agents/${encodeURIComponent(agentId)}`, { overrides_allowed: ["system_prompt"] });
  await call("POST", `/agents/${encodeURIComponent(agentId)}/publish`, {});
  return agentId;
}

/** Fish API v1: the agent speaks and listens for `language` from its next session on (a draft config change, published). */
export async function setFishAgentLanguage(key: string, endpoint: LiveEndpoint, agentId: string, language: string, fetchImpl: typeof fetch = fetch): Promise<void> {
  const agent = `${endpoint.apiBase}/agents/${encodeURIComponent(agentId)}`;
  await fishSetupRequest(fetchImpl, key, "PATCH", `${agent}/config`, { voice: { speaking_language: language } });
  await fishSetupRequest(fetchImpl, key, "POST", `${agent}/publish`, {});
}

const FISH_UNEXPECTED = "Fish Audio returned an unexpected answer. Try the call again.";

async function fishSetupRequest(fetchImpl: typeof fetch, key: string, method: string, url: string, body: unknown): Promise<Record<string, unknown>> {
  let response: Response;
  try {
    response = await fetchImpl(url, {
      method,
      headers: { authorization: `Bearer ${key.trim()}`, "content-type": "application/json" },
      redirect: "error",
      body: JSON.stringify(body),
      signal: AbortSignal.timeout(SETUP_TIMEOUT_MS),
    });
  } catch (error) {
    const timedOut = error instanceof Error && (error.name === "TimeoutError" || error.name === "AbortError");
    throw new LiveSessionError(timedOut ? "Fish Audio did not answer in time. Try the call again." : "Could not reach Fish Audio. Check the internet connection.", 502);
  }
  if (!response.ok) {
    await response.body?.cancel().catch(() => undefined);
    throw new LiveSessionError(fishErrorMessage(response.status), response.status === 429 ? 429 : 502);
  }
  const answer = await response.json().catch(() => null) as unknown;
  return answer && typeof answer === "object" ? answer as Record<string, unknown> : {};
}

/** The Fish agent's system prompt for this call: the GPT-Live instructions, how this agent delegates, and the recent chat. */
export interface FishInstructionsOptions {
  cloudHome?: boolean;
  historyLimits?: Partial<LiveHistoryLimits>;
  provider?: LiveProvider;
}

export function fishInstructions(
  bot: LiveBot,
  history: LiveHistoryMessage[],
  { cloudHome = false, historyLimits, provider = "fish" }: FishInstructionsOptions = {},
): string {
  const lines = [
    liveInstructions(bot, { cloudHome }),
    "",
    `To delegate, call the \`${FISH_DELEGATE_TOOL}\` tool with the user's request in their own words. Its result is what you learned or did; speak it naturally and briefly.`,
    `A user message that starts with ${FISH_UPDATE_PREFIX} is not the user speaking: it is a later result or a status note from your work. Speak it to the user naturally, in the first person, without reading the prefix.`,
    "Do not end the call yourself. When the user asks to hang up, delegate it.",
  ];
  const recent = selectLiveHistory(history, historyLimits, provider);
  if (recent.length) {
    lines.push("", "The conversation so far, most recent last:");
    for (const message of recent) lines.push(`${message.role === "user" ? "User" : "You"}: ${message.text}`);
  }
  return lines.join("\n");
}

export interface CreateFishSessionInput {
  key: string;
  endpoint: LiveEndpoint;
  bot: LiveBot;
  history: LiveHistoryMessage[];
  cloudHome: boolean;
  historyLimits?: Partial<LiveHistoryLimits>;
  fetchImpl?: typeof fetch;
  timeoutMs?: number;
}

/** Fish API v1 `POST /v1/agent/sessions`. Errors carry a status and a message fit for the user: never the key, never Fish's raw
 * body; `fishStatus` is Fish's own status. */
export async function createFishLiveSession(input: CreateFishSessionInput): Promise<LiveFishSessionToken> {
  const key = input.key.trim();
  if (!key) throw new LiveSessionError("Add a Fish Audio API key in Settings → AI → Other services to use Live calls.", 409);
  const body = {
    agent_id: input.endpoint.model,
    // Fish refuses an override the agent's `overrides_allowed` does not list (400).
    overrides: {
      system_prompt: fishInstructions(input.bot, input.history, {
        cloudHome: input.cloudHome,
        historyLimits: input.historyLimits,
        provider: "fish",
      }),
    },
  };
  let response: Response;
  try {
    response = await (input.fetchImpl ?? fetch)(`${input.endpoint.apiBase}/sessions`, {
      method: "POST",
      headers: { authorization: `Bearer ${key}`, "content-type": "application/json" },
      redirect: "error",
      body: JSON.stringify(body),
      signal: AbortSignal.timeout(input.timeoutMs ?? CREATE_TIMEOUT_MS),
    });
  } catch (error) {
    const timedOut = error instanceof Error && (error.name === "TimeoutError" || error.name === "AbortError");
    throw new LiveSessionError(timedOut ? "Fish Audio did not answer in time. Try the call again." : "Could not reach Fish Audio. Check the internet connection.", 502);
  }
  if (!response.ok) {
    await response.body?.cancel().catch(() => undefined);
    throw Object.assign(new LiveSessionError(fishErrorMessage(response.status), response.status === 429 ? 429 : 502), { fishStatus: response.status });
  }
  const token = await response.json().catch(() => null) as Partial<LiveFishSessionToken> | null;
  if (!token || token.transport !== "livekit" || typeof token.session_id !== "string" || !token.session_id
    || typeof token.token !== "string" || !token.token || typeof token.livekit_url !== "string" || !/^(wss|https):\/\//.test(token.livekit_url)) {
    throw new LiveSessionError(FISH_UNEXPECTED, 502);
  }
  return {
    transport: "livekit",
    session_id: token.session_id,
    expires_at: typeof token.expires_at === "string" ? token.expires_at : "",
    max_duration_seconds: Number(token.max_duration_seconds) || 0,
    livekit_url: token.livekit_url,
    token: token.token,
  };
}

/** Fish API v1 `POST /v1/agent/sessions/{id}/end`; best effort, never throws. */
export async function endFishLiveSession(sessionId: string, key: string, endpoint: LiveEndpoint, fetchImpl: typeof fetch = fetch): Promise<boolean> {
  try {
    const response = await fetchImpl(`${endpoint.apiBase}/sessions/${encodeURIComponent(sessionId)}/end`, {
      method: "POST",
      headers: { authorization: `Bearer ${key.trim()}` },
      redirect: "error",
      signal: AbortSignal.timeout(END_TIMEOUT_MS),
    });
    await response.body?.cancel().catch(() => undefined);
    return response.ok;
  } catch {
    return false;
  }
}

export function fishErrorMessage(status: number): string {
  switch (status) {
    case 400: return "Fish Audio rejected the call settings (HTTP 400).";
    case 401: return "Fish Audio rejected the API key. Check the Fish Audio key in Settings → AI → Other services.";
    case 402: return "The Fish Audio account has no credit for agent calls.";
    case 403: return "This Fish Audio key may not start agent calls.";
    case 404: return "Fish Audio could not find the agent for Live calls. Try the call again.";
    case 409: return "The Fish Audio agent for Live calls is not ready yet. Try the call again in a moment.";
    case 422: return "Fish Audio could not use the call request (HTTP 422).";
    case 429: return "Fish Audio is limiting agent sessions right now. Try again in a moment.";
    default: return status >= 500 ? "Fish Audio had a problem starting the call. Try again in a moment." : `Fish Audio could not start the call (HTTP ${status}).`;
  }
}

const SOCKET = { connecting: 0, open: 1, closed: 3 } as const;
/** What the client of a fish call says it heard ends a call this way; anything else is the call's own ending. */
const CLIENT_END_REASONS: Record<string, string> = {
  agent_hangup: "remote_hangup",
  conversation_timeout: "expired",
  connection_lost: "connection_lost",
};

/** The controller's sideband for a "fish" call. The client's reports arrive through `receive` and are read exactly as
 * GPT-Live's sideband events are; the controller's commands queue for the client's `poll`. Everything the client sends is
 * the caller's own claim, so a fish call is held only by someone fishLiveCallRefusal admits (live-access.ts). */
export class FishRelay implements LiveSocket {
  readyState: number = SOCKET.connecting;
  onopen: ((event: unknown) => void) | null = null;
  onmessage: ((event: { data: unknown }) => void) | null = null;
  onclose: ((event: unknown) => void) | null = null;
  onerror: ((event: unknown) => void) | null = null;
  readonly sessionId: string;
  private readonly token: Buffer;
  private readonly endSession: () => Promise<unknown>;
  private readonly forget: () => void;
  private queue: LiveFishRelayCommand[] = [];
  private waiter: (() => void) | null = null;
  private ended: Promise<unknown> | null = null;
  private received = 0;

  constructor(input: { sessionId: string; token: string; endSession: () => Promise<unknown>; forget: () => void }) {
    this.sessionId = input.sessionId;
    this.token = Buffer.from(input.token);
    this.endSession = input.endSession;
    this.forget = input.forget;
  }

  holds(token: string): boolean {
    const offered = Buffer.from(token);
    return offered.length === this.token.length && timingSafeEqual(offered, this.token);
  }

  send(data: string): void {
    if (this.readyState === SOCKET.closed) throw new Error("relay closed");
    let event: Record<string, unknown>;
    try {
      event = JSON.parse(data) as Record<string, unknown>;
    } catch {
      return;
    }
    const kind = /^session\.(instructions|thinking|commentary)\.append$/.exec(String(event.type))?.[1] as "instructions" | "thinking" | "commentary" | undefined;
    if (kind) {
      if (typeof event.content !== "string" || !event.content) return;
      this.push({ type: "append", kind, content: event.content, delegationId: typeof event.delegation_id === "string" ? event.delegation_id : null });
      return;
    }
    if (event.type === "session.close") {
      this.push({ type: "close" });
      void this.end().then(() => this.deliver({ type: "session.closed" }));
    }
  }

  close(): void {
    if (this.readyState === SOCKET.closed) return;
    this.readyState = SOCKET.closed;
    this.queue.push({ type: "close" });
    this.wake();
    this.forget();
    void this.end();
  }

  /** The client's reports, in order. Before the client has joined only `connected`, `ended` and `error` count. */
  receive(events: LiveFishRelayEvent[], from = this.received): void {
    // A batch the client retries after a lost answer starts at events already taken; those are skipped.
    const fresh = events.slice(Math.max(0, this.received - from));
    this.received = Math.max(this.received, from + events.length);
    for (const event of fresh) {
      if (this.readyState === SOCKET.closed) return;
      if (event.type === "connected") {
        if (this.readyState !== SOCKET.connecting) continue;
        this.readyState = SOCKET.open;
        this.onopen?.({});
        continue;
      }
      if (event.type === "ended") {
        this.deliver({ type: "session.closed", ...(CLIENT_END_REASONS[event.reason] ? { reason: CLIENT_END_REASONS[event.reason] } : {}) });
        continue;
      }
      if (event.type === "error") {
        this.deliver({ type: "error", error: { code: event.code } });
        continue;
      }
      if (this.readyState !== SOCKET.open) continue;
      if (event.type === "heard") {
        // GPT-Live's deltas are fragments glued as sent; a whole Fish utterance needs its own space.
        this.deliver({ type: "session.input_transcript.delta", delta: `${event.text} `, start_ms: event.startMs, end_ms: event.endMs });
      } else if (event.type === "spoke") {
        this.deliver({ type: "session.output_transcript.delta", start_ms: event.startMs, end_ms: event.endMs });
      } else if (event.type === "delegate") {
        this.deliver({ type: "session.delegation.created", delegation: { id: event.id, target: "client" }, offset_ms: event.atMs });
      }
    }
  }

  /** The queued commands, waiting up to `waitMs` for one. A newer poll answers an older one with nothing. */
  async poll(waitMs: number): Promise<{ commands: LiveFishRelayCommand[]; closed: boolean }> {
    if (!this.queue.length && this.readyState !== SOCKET.closed && waitMs > 0) {
      this.wake();
      await new Promise<void>((resolve) => {
        const done = () => {
          clearTimeout(timer);
          if (this.waiter === done) this.waiter = null;
          resolve();
        };
        const timer = setTimeout(done, waitMs);
        timer.unref?.();
        this.waiter = done;
      });
    }
    const commands = this.queue.splice(0);
    return { commands, closed: this.readyState === SOCKET.closed };
  }

  private push(command: LiveFishRelayCommand): void {
    this.queue.push(command);
    if (this.queue.length > MAX_QUEUED_COMMANDS) this.queue.splice(0, this.queue.length - MAX_QUEUED_COMMANDS);
    this.wake();
  }

  private wake(): void {
    const waiter = this.waiter;
    this.waiter = null;
    waiter?.();
  }

  private deliver(event: Record<string, unknown>): void {
    if (this.readyState === SOCKET.closed) return;
    this.onmessage?.({ data: JSON.stringify(event) });
  }

  private end(): Promise<unknown> {
    this.ended ??= this.endSession().catch(() => undefined);
    return this.ended;
  }
}

export class FishRelays {
  private readonly relays = new Map<string, FishRelay>();

  /** A relay for a session just created; its token goes only to the client that started the call. */
  open(sessionId: string, endSession: () => Promise<unknown>): { relay: FishRelay; token: string } {
    const token = randomBytes(32).toString("base64url");
    this.relays.get(sessionId)?.close();
    const relay = new FishRelay({ sessionId, token, endSession, forget: () => {
      if (this.relays.get(sessionId) === relay) this.relays.delete(sessionId);
    } });
    this.relays.set(sessionId, relay);
    return { relay, token };
  }

  socket(sessionId: string): FishRelay {
    const relay = this.relays.get(sessionId);
    if (!relay) throw new Error("no relay for that session");
    return relay;
  }

  /** null unless the token is this session's. */
  find(sessionId: string, token: string): FishRelay | null {
    const relay = this.relays.get(sessionId);
    return relay?.holds(token) ? relay : null;
  }
}