"use client";
import { useCallback, useEffect } from "react";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { IconPlus, IconRobot } from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip";
import { useAppStore } from "@/components/state-provider";
import { useInOffice } from "@/hooks/use-in-office";
import { useOfficeRefetch } from "@/hooks/use-office-refetch";
import { listAgentProfiles } from "@/lib/api/domains/office-api";
import { cn } from "@/lib/utils";
import type { AgentProfile } from "@/lib/state/slices/office/types";
import { selectActiveSessionsForAgent } from "@/lib/state/slices/session/selectors";
import { AgentAvatar } from "@/app/office/components/agent-avatar";
import { AgentStatusDot } from "@/app/office/agents/components/agent-status-dot";
import { LiveAgentIndicator } from "@/app/office/agents/components/live-agent-indicator";
import {
APP_SIDEBAR_SECTION_IDS,
SIDEBAR_ITEM_ACTIVE,
SIDEBAR_ITEM_INACTIVE,
} from "../app-sidebar-constants";
import { AppSidebarSection } from "../app-sidebar-section";
type AgentsSectionProps = {
collapsed: boolean;
};
export function AgentsSection({ collapsed }: AgentsSectionProps) {
const router = useRouter();
const inOffice = useInOffice();
const agents = useAppStore((s) => s.office.agentProfiles);
const workspaceId = useAppStore((s) => s.workspaces.activeId);
const setOfficeAgentProfiles = useAppStore((s) => s.setOfficeAgentProfiles);
const refetchAgents = useCallback(async () => {
if (!workspaceId || !inOffice) return;
const res = await listAgentProfiles(workspaceId).catch(() => ({ agents: [] }));
setOfficeAgentProfiles(res.agents ?? []);
}, [workspaceId, inOffice, setOfficeAgentProfiles]);
useEffect(() => {
refetchAgents();
}, [refetchAgents]);
useOfficeRefetch("agents", refetchAgents);
if (!inOffice) return null;
const headerAction = (
Add agent
);
return (
);
}
function AgentRow({ agent }: { agent: AgentProfile }) {
const pathname = usePathname();
const href = `/office/agents/${agent.id}`;
const isActive = pathname === href;
const liveCount = useAppStore((s) => selectActiveSessionsForAgent(s, agent.id));
const errorCount = useAppStore((s) =>
s.office.inboxItems.reduce((acc, item) => {
if (item.type !== "agent_run_failed") return acc;
const payloadAgent =
typeof item.payload?.agent_profile_id === "string" ? item.payload.agent_profile_id : "";
return payloadAgent === agent.id ? acc + 1 : acc;
}, 0),
);
const isAutoPaused = (agent.pauseReason ?? "").startsWith("Auto-paused:");
return (
{agent.name}
{isAutoPaused ? (
paused
) : null}
{!isAutoPaused && errorCount > 0 ? (
{errorCount} error{errorCount === 1 ? "" : "s"}
) : null}
{liveCount > 0 && }
{liveCount === 0 && !isAutoPaused && errorCount === 0 && (
)}
);
}