"use client";
import { useQueryClient } from "@tanstack/react-query";
import { useAtom, useAtomValue } from "jotai";
import { Activity, RefreshCcw } from "lucide-react";
import { useCallback } from "react";
import { actionLogSheetAtom } from "@/atoms/agent/action-log-sheet.atom";
import { agentFlagsAtom } from "@/atoms/agent/agent-flags-query.atom";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import {
Sheet,
SheetContent,
SheetDescription,
SheetHeader,
SheetTitle,
} from "@/components/ui/sheet";
import { Skeleton } from "@/components/ui/skeleton";
import { agentActionsQueryKey, useAgentActionsQuery } from "@/hooks/use-agent-actions-query";
import { ActionLogItem } from "./action-log-item";
function EmptyState() {
return (
No actions logged yet
Once the agent calls a tool in this thread, it will show up here. From the log you can
inspect arguments and revert reversible actions.
);
}
function DisabledState() {
return (
Action log is disabled
This deployment hasn't enabled the agent action log. An admin can flip
SURFSENSE_ENABLE_ACTION_LOG
.
);
}
const SKELETON_KEYS = ["s1", "s2", "s3", "s4"] as const;
function LoadingState() {
return (
{SKELETON_KEYS.map((key) => (
))}
);
}
export function ActionLogSheet() {
const [state, setState] = useAtom(actionLogSheetAtom);
const queryClient = useQueryClient();
const { data: flags } = useAtomValue(agentFlagsAtom);
const actionLogEnabled = !!flags?.enable_action_log && !flags?.disable_new_agent_stack;
const revertEnabled = !!flags?.enable_revert_route && !flags?.disable_new_agent_stack;
const threadId = state.threadId;
const { data, items, isLoading, isFetching, isError, error, refetch } = useAgentActionsQuery(
threadId,
{ enabled: state.open && actionLogEnabled }
);
const handleRevertSuccess = useCallback(() => {
if (threadId !== null) {
queryClient.invalidateQueries({ queryKey: agentActionsQueryKey(threadId) });
}
}, [queryClient, threadId]);
return (
setState((s) => ({ ...s, open }))}>
Agent actions
{data?.total !== undefined && data.total > 0 && (
{data.total}
)}
refetch()}
disabled={isFetching || !actionLogEnabled}
className="size-8 p-0"
aria-label="Refresh action log"
>
Audit trail of every tool call the agent made in this thread.
{revertEnabled
? " Reversible actions can be undone in place."
: " Reverts are read-only on this deployment."}
{!actionLogEnabled ? (
) : threadId === null ? (
) : isLoading ? (
) : isError ? (
Failed to load actions
{error instanceof Error ? error.message : "Unknown error"}
refetch()}>
Try again
) : items.length === 0 ? (
) : (
{items.map((action) => (
))}
{data?.has_more && (
Showing {items.length} of {data.total}. Older actions are paginated.
)}
)}
);
}