Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

7.8 KB
Raw Blame
import { useId, useState, type ReactNode } from "react";
import { ArrowDownLeft, ArrowUpRight, ChevronRight, Clock3, Inbox, Users } from "lucide-react";
import { CodingThreadActivity } from "@/components/agentic/threads/CodingThreadLink";
import { codingThreadOf } from "@/components/agentic/threads/openCodingThread";
import { ChatMarkdown } from "@/components/agentchat/ChatMarkdown";
import { useOpenPairConversation } from "@/components/agentchat/PairConversation";
import type { InternalMessageItem, TeamEvent } from "@/components/agentchat/reduce";
import { useT } from "@/i18n";
import { NEUTRAL_ASSISTANT_NAME } from "@/lib/assistantNameCache";
import { societyDisplayName } from "@/lib/societyDisplayName";
import { useEventStore } from "@/store/events";
import { cn } from "@/lib/utils";
import { AgentSwatch } from "../AgentSwatch";
import type { SocietyAgent } from "../data";
export { routineTask } from "./routineExecution";
export { CodingThreadActivity };

/** A quiet event in the conversation; details remain keyboard accessible. */
export function ChatActivity({ label, icon, children, failed = false }: {
  label: ReactNode; icon?: ReactNode; children: ReactNode; failed?: boolean;
}) {
  const [open, setOpen] = useState(false);
  const id = useId();
  return <div className="my-3 w-full min-w-0 text-xs text-muted-foreground">
    <button type="button" aria-expanded={open} aria-controls={id} onClick={() => setOpen(!open)}
      className={cn("mx-auto flex max-w-[min(100%,36rem)] items-center justify-center gap-2 rounded-md px-2 py-1.5 text-left hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring", failed && "text-destructive")}>
      {icon}<span className="min-w-0 truncate">{label}</span>
      <ChevronRight aria-hidden className={cn("h-3 w-3 shrink-0 transition-transform", open && "rotate-90")} />
    </button>
    {open ? <div id={id} className="mx-auto mt-2 max-w-xl rounded-xl bg-secondary px-4 py-3 text-sm leading-relaxed [overflow-wrap:anywhere]">{children}</div> : null}
  </div>;
}

/** The framed assignment header the backend writes (``frame_assignment``). */
const ASSIGNMENT_HEADER = /^\[assignment from ([^\]\r\n]+)\]\r?\n/;

/**
 * Work Jarvis or a teammate handed this agent, as its one chat records it:
 * ``{sender, task}``, or null for an ordinary message from the person.
 */
export function assignmentOf(text: string): { sender: string; task: string } | null {
  const match = ASSIGNMENT_HEADER.exec(text);
  if (!match) return null;
  const body = text.slice(match[0].length);
  // The task ends where the handoff instructions the backend appends begin.
  const end = body.search(/\r?\nWhen you are done, end with a handoff:/);
  return { sender: match[1].trim(), task: (end >= 0 ? body.slice(0, end) : body).trim() };
}

/** A delegated task in the agent's one chat: folded, sender first, task inside. */
export function DelegationActivity({ sender, task, roster }: {
  sender: string; task: string; roster: SocietyAgent[];
}) {
  const t = useT();
  const assistantName = useEventStore((s) => s.assistantName);
  const participant = roster.find((agent) => agent.agentId === sender);
  const name = participant
    ? societyDisplayName(participant, assistantName)
    : sender === "jarvis" ? (assistantName || NEUTRAL_ASSISTANT_NAME) : sender;
  return <ChatActivity icon={<Inbox aria-hidden className="h-3.5 w-3.5 shrink-0" />}
    label={<span className="inline-flex max-w-full items-center gap-2">
      <span>{t("society.chat.assignment_from").replace("{0}", "")}</span>
      {participant ? <AgentSwatch agent={participant} size={18} /> : null}
      <span className="truncate">{name}</span>
      <span className="truncate">· {task.split(/\r?\n/)[0]}</span>
    </span>}>
    <ChatMarkdown text={task} />
  </ChatActivity>;
}

/** What the engine shows for a turn that brings results: "<goal>: 2 results". */
const TEAM_RESULTS = /^(.*): (\d+) results?$/s;

/**
 * A turn the team engine gave a team lead: members' results arrived, a report
 * is due, or a goal the person resumed goes on. Nobody typed it, so it is a
 * quiet line in the conversation and never a message bubble.
 */
export function TeamEventLine({ event, text }: { event: TeamEvent; text: string }) {
  const t = useT();
  const results = event === "update" ? TEAM_RESULTS.exec(text) : null;
  const label = results ? t("society.lead.event_update").replace("{0}", results[2]) : t(`society.lead.event_${event}`).replace("{0}", "");
  const goal = (results ? results[1] : text).split(/\r?\n/)[0];
  return <p data-testid="team-event-line" data-event={event}
    className="mx-auto my-1 flex max-w-[min(100%,36rem)] items-center justify-center gap-2 px-2 py-1 text-xs text-muted-foreground">
    <Users aria-hidden className="h-3.5 w-3.5 shrink-0" />
    <span className="shrink-0">{label.trim()}</span>
    {goal ? <span className="min-w-0 truncate">· {goal}</span> : null}
  </p>;
}

export function RoutineActivity({ task, original, onOpen }: { task: string; original: string; onOpen?: () => void }) {
  const t = useT();
  if (onOpen) return <button type="button" onClick={onOpen}
    className="mx-auto my-3 flex max-w-[min(100%,36rem)] items-center gap-2 rounded-md px-2 py-1.5 text-left text-xs text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
    <Clock3 aria-hidden className="h-3.5 w-3.5 shrink-0" /><span className="truncate">{t("society.chat.routine_check")} · {task.split(/\r?\n/)[0]}</span><ChevronRight aria-hidden className="h-3 w-3 shrink-0" />
  </button>;
  return <ChatActivity label={<>{t("society.chat.routine_check")} · {task.split(/\r?\n/)[0]}</>} icon={<Clock3 aria-hidden className="h-3.5 w-3.5 shrink-0" />}>
    <ChatMarkdown text={task} />
    <details className="mt-2 text-xs"><summary className="cursor-pointer">{t("society.chat.activity_details")}</summary><p className="mt-2 whitespace-pre-wrap">{original}</p></details>
  </ChatActivity>;
}

export function AgentMessageActivity({ item, roster }: { item: InternalMessageItem; roster: SocietyAgent[] }) {
  const t = useT();
  const codingThread = codingThreadOf(item.message.sender_id);
  const openPair = useOpenPairConversation();
  const assistantName = useEventStore((s) => s.assistantName);
  const outgoing = Boolean(item.outgoing);
  const id = item.outgoing?.recipientId ?? item.message.sender_id;
  const participant = roster.find(agent => agent.agentId === id);
  const name = participant ? societyDisplayName(participant, assistantName) : (item.outgoing?.recipientName || item.message.sender_name);
  const status = item.message.status;
  const failed = status === "failed";
  const canOpenPair = Boolean(openPair && id && id !== "user" && item.message.sender_kind !== "user");
  const label = t(failed ? "society.chat.message_failed" : outgoing ? "society.chat.message_to" : "society.chat.message_from");
  if (codingThread) return <CodingThreadActivity label={item.message.text} threadId={codingThread} failed={failed} />;
  return <ChatActivity failed={failed} icon={outgoing ? <ArrowUpRight aria-hidden className="h-3.5 w-3.5 shrink-0" /> : <ArrowDownLeft aria-hidden className="h-3.5 w-3.5 shrink-0" />}
    label={<span className="inline-flex max-w-full items-center gap-2"><span>{label}</span>{participant ? <AgentSwatch agent={participant} size={18} /> : null}<span className="truncate">{name}</span>{status === "queued" ? <span>· {t("agent_chat.delivery_queued")}</span> : null}</span>}>
    <ChatMarkdown text={item.message.text} />
    {item.message.error ? <p role="alert" className="mt-2 text-destructive">{item.message.error}</p> : null}
    {canOpenPair ? <button type="button" onClick={() => openPair?.({ id, name })} className="mt-3 rounded-sm text-xs underline underline-offset-4 focus-visible:ring-2 focus-visible:ring-ring">{t("agent_chat.pair_open")}</button> : null}
  </ChatActivity>;
}