Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

8.0 KB
Raw Blame
/* ═══════════════════════════════════════════════════════════════
   A conversation surface: Chat and Code each have their own. Their
   sessions, messages, live stream, agent state and pending approvals are
   separate stores, fed by separate event streams, so nothing one does
   shows up in the other.
   ═══════════════════════════════════════════════════════════════ */

import { createContext, useContext } from "react";
import { create, type StoreApi, type UseBoundStore } from "zustand";
import type { ChatMessage, PendingPrompt, ResearchPlanPrompt, ResearchSnapshot, ThoughtTrace } from "~/api/types";
import type { AgentState } from "./store";
import { newTrace } from "./trace";

export type Surface = "chat" | "code";

export interface ConversationState {
  surface: Surface;
  activeId: string | null;
  messages: ChatMessage[];
  streaming: string;
  loading: boolean;
  agent: AgentState;
  step: { total: number; current: number; tool: string } | null;
  prompts: PendingPrompt[];
  /** The current turn's thought process, streaming. */
  live: ThoughtTrace | null;
  /** The stop button was pressed and the runtime hasn't confirmed yet. */
  stopping: boolean;
  /**
   * This chat's message is waiting in line: every slot the runtime runs
   * chats in is taken. Cleared when this chat's turn starts.
   */
  waiting: { position: number; busyWith?: string; busySessionId?: string } | null;
  setWaiting(waiting: ConversationState["waiting"]): void;
  setStopping(stopping: boolean): void;
  /** Videos and songs being made in the background for this chat: placeholders until they arrive. */
  pendingMedia: import("~/api/types").PendingMedia[];
  setPendingMedia(update: (list: import("~/api/types").PendingMedia[]) => import("~/api/types").PendingMedia[]): void;
  /** An effort picked before this chat existed: applied when its thread is created. */
  pendingEffort?: import("~/api/types").ReasoningChoice;
  setPendingEffort(level: import("~/api/types").ReasoningChoice | undefined): void;
  /** Text to put in the box (an undone message); the composer takes it once. */
  draft: string | null;
  setDraft(draft: string | null): void;
  /** The conversation's plan checklist (update_plan), if Mercury made one. */
  /** The plan checklist; `state` once the turn that worked on it ended without finishing it. */
  plan: { steps: Array<{ label: string; status: "pending" | "active" | "done" }>; updatedAt: number; state?: "paused" | "stopped" } | null;
  setPlan(plan: ConversationState["plan"]): void;
  /**
   * Research in this conversation: switched on for the next message
   * (`armed`), and the run going now or the last one.
   */
  research: { armed: boolean; planFirst?: boolean; topic?: string | null; run: ResearchSnapshot | null } | null;
  setResearch(update: (r: NonNullable<ConversationState["research"]>) => ConversationState["research"]): void;
  /** A research plan waiting for Start. */
  researchPlan: ResearchPlanPrompt | null;
  setResearchPlan(plan: ResearchPlanPrompt | null): void;
  /** A note about the running turn that isn't its answer ("provider stalled, retrying"). */
  notice: { message: string; level: "info" | "warning"; at: number } | null;
  setNotice(notice: ConversationState["notice"]): void;
  setLive(update: (live: ThoughtTrace) => ThoughtTrace): void;
  clearLive(): void;
  setActive(id: string | null): void;
  setMessages(messages: ChatMessage[]): void;
  addMessage(message: ChatMessage): void;
  appendStreaming(text: string): void;
  clearStreaming(): void;
  setLoading(loading: boolean): void;
  setAgent(agent: AgentState): void;
  setStep(step: ConversationState["step"]): void;
  addPrompt(prompt: PendingPrompt): void;
  removePrompt(id: string): void;
}

export type ConversationStore = UseBoundStore<StoreApi<ConversationState>>;

function makeConversation(surface: Surface): ConversationStore {
  return create<ConversationState>((set) => ({
    surface,
    activeId: null,
    messages: [],
    streaming: "",
    loading: false,
    waiting: null,
    setWaiting: (waiting) => set({ waiting }),
    agent: "idle",
    step: null,
    prompts: [],
    live: null,
    stopping: false,
    setStopping: (stopping) => set({ stopping }),
    draft: null,
    setDraft: (draft) => set({ draft }),
    pendingMedia: [],
    setPendingMedia: (update) => set((s) => ({ pendingMedia: update(s.pendingMedia) })),
    pendingEffort: undefined,
    setPendingEffort: (pendingEffort) => set({ pendingEffort }),
    plan: null,
    setPlan: (plan) => set({ plan }),
    research: null,
    setResearch: (update) => set((s) => ({ research: update(s.research ?? { armed: false, run: null }) })),
    researchPlan: null,
    setResearchPlan: (researchPlan) => set({ researchPlan }),
    notice: null,
    setNotice: (notice) => set({ notice }),
    setLive: (update) => set((s) => ({ live: update(s.live ?? newTrace()) })),
    clearLive: () => set({ live: null }),
    setActive: (activeId) => set({ activeId, prompts: [], step: null, agent: "idle", live: null, stopping: false, plan: null, research: null, researchPlan: null, notice: null, pendingEffort: undefined, pendingMedia: [] }),
    setMessages: (messages) => set({ messages }),
    addMessage: (message) => set((s) => ({ messages: [...s.messages, message] })),
    appendStreaming: (text) => set((s) => ({ streaming: s.streaming + text })),
    clearStreaming: () => set({ streaming: "" }),
    setLoading: (loading) => set({ loading }),
    setAgent: (agent) => set({ agent }),
    setStep: (step) => set({ step }),
    addPrompt: (prompt) => set((s) => ({ prompts: [...s.prompts.filter((p) => p.id !== prompt.id), prompt], agent: "needs-you" })),
    removePrompt: (id) => set((s) => {
      const prompts = s.prompts.filter((p) => p.id !== id);
      return { prompts, agent: prompts.length === 0 && s.agent === "needs-you" ? "working" : s.agent };
    }),
  }));
}

export const conversations: Record<Surface, ConversationStore> = {
  chat: makeConversation("chat"),
  code: makeConversation("code"),
};

export const ConversationContext = createContext<ConversationStore>(conversations.chat);

/** The conversation of the surface this component is rendered in. */
export function useConversation<T>(selector: (s: ConversationState) => T): T {
  return useContext(ConversationContext)(selector);
}

export function useConversationStore(): ConversationStore {
  return useContext(ConversationContext);
}

/** The list a conversation shows in: Code's (a Code session) or Chat's. */
export function listOf(thread: { id: string; title?: string | null }, codeIds: Set<string>): Surface {
  return codeIds.has(thread.id) || (thread.title ?? "").startsWith("Code · ") ? "code" : "chat";
}

/* ── Code sessions: which runtime sessions belong to Code, and their folder ── */

export interface CodeSession {
  id: string;
  folder: string | null;
  createdAt: string;
}

interface CodeSessionsState {
  scope: string;
  sessions: CodeSession[];
  load(scope: string): void;
  upsert(session: CodeSession): void;
  remove(id: string): void;
}

const keyFor = (scope: string) => `mercury.code.sessions.v1:${scope}`;

export const useCodeSessions = create<CodeSessionsState>((set, get) => ({
  scope: "",
  sessions: [],
  load(scope) {
    let sessions: CodeSession[] = [];
    try { sessions = JSON.parse(localStorage.getItem(keyFor(scope)) ?? "[]") as CodeSession[]; } catch { sessions = []; }
    set({ scope, sessions: Array.isArray(sessions) ? sessions : [] });
  },
  upsert(session) {
    const sessions = [session, ...get().sessions.filter((s) => s.id !== session.id)];
    try { localStorage.setItem(keyFor(get().scope), JSON.stringify(sessions)); } catch { /* ignore */ }
    set({ sessions });
  },
  remove(id) {
    const sessions = get().sessions.filter((s) => s.id !== id);
    try { localStorage.setItem(keyFor(get().scope), JSON.stringify(sessions)); } catch { /* ignore */ }
    set({ sessions });
  },
}));