Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

2.4 KB
Raw Blame
// Keep the conversation switch in sync with the brain and other windows.
import type { ChatPeer } from "@pibot/client-core";
import type { ConversationYolo } from "@pibot/protocol";
import { useCallback, useEffect, useState } from "react";

/**
 * The YOLO switch of one conversation (DM topic or group conversation). `state` is null
 * while unknown or when the conversation cannot have one (a draft, a routine's topic), in which
 * case no control is shown. Other windows follow through `conversations.yolo.changed`.
 */
export function useConversationYolo(peer: ChatPeer | null | undefined, conversationId?: string) {
  const [state, setState] = useState<ConversationYolo | null>(null);
  useEffect(() => {
    setState(null);
    if (!peer || !conversationId) return;
    let active = true;
    const load = () =>
      peer.call("conversations.yolo.get", { conversationId }).then(
        (value) => {
          if (active) setState(value);
        },
        () => {
          // Unavailable here (routine topic, archived, older brain): show no switch.
          if (active) setState(null);
        },
      );
    const offs = [
      peer.on("conversations.yolo.changed", (event) => {
        if (event.conversationId !== conversationId) return;
        setState((current) => (current ? { ...current, ...event } : current));
      }),
      // A restarted brain may have answered changes this window missed.
      peer.on("brain.status", (event) => {
        if (event.state === "ready") void load();
      }),
    ];
    void load();
    return () => {
      active = false;
      for (const off of offs) off();
    };
  }, [peer, conversationId]);
  const set = useCallback(
    async (enabled: boolean, signal: AbortSignal, acknowledged = false) => {
      if (!peer || !conversationId) return undefined;
      const value =
        enabled && window.pibot.platform === "win32"
          ? await peer.call(
              "conversations.yolo.enableOnWindows",
              { conversationId, ...(acknowledged ? { acknowledged: true } : {}) },
              { signal },
            )
          : await peer.call("conversations.yolo.set", { conversationId, enabled }, { signal });
      if (!signal.aborted)
        setState((current) => (current?.conversationId === conversationId ? value : current));
      return value;
    },
    [peer, conversationId],
  );
  return { state: state?.conversationId === conversationId ? state : null, set };
}