Skip to content

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

8.9 KB
Raw Blame
import type { LiveHistoryLimits } from "../shared/wire.ts";
import { createCodexLiveSession, type LiveCodexCli } from "./live-codex.ts";
import { productName } from "./brand.ts";
import type { LiveCallDeps } from "./live-call-controller.ts";
import { openLiveSocket, relayedProvider } from "./live-call-controller.ts";
import { createLiveSession, liveAttachUrl, LiveSessionError, type LiveBot, type LiveEndpoint, type LiveHistoryMessage } from "./live-call.ts";
import { codexLiveCallRefusal, elevenLabsLiveCallRefusal, fishLiveCallRefusal, geminiLiveCallRefusal } from "./live-access.ts";
import type { LiveConfig } from "./live-config.ts";
import { createFishLiveSession, endFishLiveSession, fishKeyPrint, FishRelays, fishSpeakingLanguage, setFishAgentLanguage, setUpFishAgent } from "./live-fish.ts";
import { createElevenLabsLiveSession, setUpElevenLabsAgent } from "./live-elevenlabs.ts";
import { createGeminiLiveSession } from "./live-gemini.ts";
import type { LiveRouteDeps } from "./routes/live.ts";

export interface LiveSessionWiringInput {
  bot(botId: string): LiveBot;
  history(threadId: string): LiveHistoryMessage[];
  cloudHome: boolean;
  othersOnLoopback(): boolean;
  /** Saves the Fish Audio or ElevenLabs agent the harness set up, with the account it belongs to. */
  saveAgent(agent: Required<Pick<LiveConfig, "fishAgentId" | "fishAgentFor" | "fishAgentLanguage">> | Required<Pick<LiveConfig, "elevenlabsAgentId" | "elevenlabsAgentFor">>): void;
  /** The app's language, which a Fish Audio agent speaks; English when unset. */
  language?(): string | undefined;
  codex?(): LiveCodexCli | null;
  /** The language the saved Fish Audio agent was last set to speak (`LiveConfig.fishAgentLanguage`). */
  fishAgentLanguage?(): string | undefined;
  /** The name a person reads, which names the agent on their account; the brand's own by default. */
  productName?(): string;
  historyLimits?(): Partial<LiveHistoryLimits> | undefined;
  env?: NodeJS.ProcessEnv;
  fetchImpl?: typeof fetch;
}

/** How a Live call's session starts and is followed, per provider: GPT-Live's WebRTC session and sideband on OpenAI or
 * Azure, or a Fish Audio agent, Gemini Live session or ElevenLabs agent conversation followed through its client's relay. */
export function liveSessionWiring(input: LiveSessionWiringInput): {
  calls: Pick<LiveCallDeps, "codexRefusal" | "fishRefusal" | "geminiRefusal" | "elevenlabsRefusal" | "createSession" | "openSocket" | "attachUrl">;
  fishRelay: LiveRouteDeps["fishRelay"];
} {
  const relays = new FishRelays();
  const relayed = (endpoint: LiveEndpoint) => relayedProvider(endpoint.provider) !== null;
  const env = input.env ?? process.env;
  const fetchImpl = input.fetchImpl ?? fetch;

  const operatorAgent = () => Boolean(env.OMB_FISH_LIVE_AGENT_ID?.trim());

  const setUpAgent = async (key: string, endpoint: LiveEndpoint, language: string): Promise<LiveEndpoint> => {
    const fishAgentId = await setUpFishAgent(key, endpoint, (input.productName ?? productName)(), language, fetchImpl);
    input.saveAgent({ fishAgentId, fishAgentFor: fishKeyPrint(key), fishAgentLanguage: language });
    return { ...endpoint, model: fishAgentId };
  };

  // Moves the saved agent the harness set up to the app's language; if Fish refuses, the call goes ahead in the old one.
  // One change at a time, even past a cancelled call start, so a slower earlier start never saves over a later one.
  let languageChange: Promise<void> = Promise.resolve();
  const speakLanguage = (key: string, endpoint: LiveEndpoint, language: string): Promise<void> => {
    const change = languageChange.then(async () => {
      if (operatorAgent() || (input.fishAgentLanguage?.() ?? "en") === language) return;
      try {
        await setFishAgentLanguage(key, endpoint, endpoint.model, language, fetchImpl);
      } catch {
        return;
      }
      input.saveAgent({ fishAgentId: endpoint.model, fishAgentFor: fishKeyPrint(key), fishAgentLanguage: language });
    });
    languageChange = change.catch(() => undefined);
    return change;
  };

  const startFish = async (key: string, endpoint: LiveEndpoint, botId: string, threadId: string) => {
    const session = { key, bot: input.bot(botId), history: input.history(threadId), cloudHome: input.cloudHome, historyLimits: input.historyLimits?.(), fetchImpl };
    if (endpoint.fixedAgent) return { agent: endpoint, token: await createFishLiveSession({ ...session, endpoint }) };
    const language = fishSpeakingLanguage(input.language?.());
    if (endpoint.model) await speakLanguage(key, endpoint, language);
    let agent = endpoint.model ? endpoint : await setUpAgent(key, endpoint, language);
    try {
      return { agent, token: await createFishLiveSession({ ...session, endpoint: agent }) };
    } catch (error) {
      // An agent removed on Fish Audio is set up again, unless the operator chose it.
      const gone = (error as { fishStatus?: number }).fishStatus === 404 && endpoint.model !== "" && !operatorAgent();
      if (!gone) throw error;
      agent = await setUpAgent(key, endpoint, language);
      return { agent, token: await createFishLiveSession({ ...session, endpoint: agent }) };
    }
  };

  const setUpElevenLabs = async (key: string, endpoint: LiveEndpoint): Promise<LiveEndpoint> => {
    const elevenlabsAgentId = await setUpElevenLabsAgent(key, endpoint, (input.productName ?? productName)(), fetchImpl);
    input.saveAgent({ elevenlabsAgentId, elevenlabsAgentFor: fishKeyPrint(key) });
    return { ...endpoint, model: elevenlabsAgentId };
  };

  const startElevenLabs = async (key: string, endpoint: LiveEndpoint, botId: string, threadId: string) => {
    const session = { key, bot: input.bot(botId), history: input.history(threadId), cloudHome: input.cloudHome, historyLimits: input.historyLimits?.(), fetchImpl };
    try {
      return await createElevenLabsLiveSession({ ...session, endpoint: endpoint.model ? endpoint : await setUpElevenLabs(key, endpoint) });
    } catch (error) {
      // An agent removed on ElevenLabs is set up again.
      if ((error as { elevenlabsStatus?: number }).elevenlabsStatus !== 404 || !endpoint.model) throw error;
      return createElevenLabsLiveSession({ ...session, endpoint: await setUpElevenLabs(key, endpoint) });
    }
  };

  return {
    fishRelay: (sessionId, token) => relays.find(sessionId, token),
    calls: {
      codexRefusal: (auth, client) => codexLiveCallRefusal(auth, Boolean(input.codex?.()), client),
      fishRefusal: (auth) => fishLiveCallRefusal(auth, { othersOnLoopback: input.othersOnLoopback() }),
      geminiRefusal: (auth) => geminiLiveCallRefusal(auth, { othersOnLoopback: input.othersOnLoopback() }),
      elevenlabsRefusal: (auth) => elevenLabsLiveCallRefusal(auth, { othersOnLoopback: input.othersOnLoopback() }),
      createSession: async ({ key, endpoint, sdp, botId, threadId, voice, standby, standbyIdleSeconds }) => {
        const historyLimits = input.historyLimits?.();
        if (endpoint.provider === "codex") {
          const config = input.codex?.();
          if (!config) throw new LiveSessionError("Set up the local Codex CLI in Settings → AI.", 503);
          return createCodexLiveSession({ ...config, sdp, voice });
        }
        if (endpoint.provider === "gemini") {
          const session = await createGeminiLiveSession({ key, endpoint, voice, bot: input.bot(botId), history: input.history(threadId), cloudHome: input.cloudHome, historyLimits, fetchImpl });
          // Gemini has nothing to end: the session ends when the client closes it, and the token is single use.
          const { token } = relays.open(session.session_id, async () => undefined);
          return { sessionId: session.session_id, gemini: { type: "gemini", session, relayToken: token } };
        }
        if (endpoint.provider === "elevenlabs") {
          const session = await startElevenLabs(key, endpoint, botId, threadId);
          // ElevenLabs has nothing to end either: the conversation ends when the client closes it.
          const { token } = relays.open(session.session_id, async () => undefined);
          return { sessionId: session.session_id, elevenlabs: { type: "elevenlabs", session, relayToken: token } };
        }
        if (endpoint.provider !== "fish") {
          return createLiveSession({ key, endpoint, sdp, voice, standby, standbyIdleSeconds, bot: input.bot(botId), history: input.history(threadId), cloudHome: input.cloudHome, historyLimits, fetchImpl });
        }
        const { agent, token: session } = await startFish(key, endpoint, botId, threadId);
        const { token } = relays.open(session.session_id, () => endFishLiveSession(session.session_id, key, agent, fetchImpl));
        return { sessionId: session.session_id, fish: { type: "fish", session, relayToken: token } };
      },
      openSocket: (url, key, endpoint) => relayed(endpoint) ? relays.socket(url) : openLiveSocket(url, key, endpoint),
      attachUrl: (sessionId, endpoint) => relayed(endpoint) ? sessionId : liveAttachUrl(sessionId, endpoint),
    },
  };
}