import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { Loader2, Repeat } from "lucide-react"; import type { SuggestRoutineInteraction } from "@paperclipai/shared"; import { agentsApi } from "@/api/agents"; import { describeError } from "@/api/errors"; import { routineSuggestionsApi } from "@/api/routine-suggestions"; import { AgentAvatar } from "@/components/AgentAvatar"; import { Button } from "@/components/ui/button"; import { queryKeys } from "@/lib/queryKeys"; import { Link } from "@/lib/router"; const DEFAULT_OFFER = "This looks like something that comes up regularly. Want me to do it on a schedule?"; function localTimeZone(): string { try { return Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC"; } catch { return "UTC"; } } /** * The agent's offer, after it finishes a task, to repeat that task as a * routine. Shown inline in the task conversation right after the completion * message, not above the composer: it is an optional follow-up, not an input * the agent waits on. Declined and withdrawn offers leave no card. */ export function RoutineSuggestionCard({ interaction }: { interaction: SuggestRoutineInteraction }) { const queryClient = useQueryClient(); const { payload, result } = interaction; const agentId = interaction.createdByAgentId ?? payload.assigneeAgentId; const agentQuery = useQuery({ queryKey: [...queryKeys.agents.detail(agentId), interaction.companyId], queryFn: () => agentsApi.get(agentId, interaction.companyId), enabled: interaction.status === "pending", staleTime: 60_000, }); const settle = async (updated: SuggestRoutineInteraction) => { queryClient.setQueriesData( { queryKey: ["issues", "interactions"] }, (current) => current?.map((candidate) => (candidate.id === updated.id ? updated : candidate)), ); // Task routes key interaction caches by UUID or identifier; prefix // invalidation reaches whichever one is mounted. await Promise.all([ queryClient.invalidateQueries({ queryKey: ["issues", "interactions"] }), queryClient.invalidateQueries({ queryKey: ["routines"] }), ]); }; const accept = useMutation({ mutationFn: () => routineSuggestionsApi.accept(interaction.issueId, interaction.id, localTimeZone()), onSuccess: (response) => settle(response.interaction), }); const decline = useMutation({ mutationFn: () => routineSuggestionsApi.decline(interaction.issueId, interaction.id), onSuccess: (updated) => settle(updated), }); if (interaction.status === "accepted") { return (
{result?.routineId ? ( Routine set up:{" "} {result.routineTitle ?? payload.title} {" "}· {result.cadenceLabel ?? payload.cadenceLabel} ) : ( Setting up the routine… )}
); } if (interaction.status !== "pending") return null; const agentName = agentQuery.data?.name ?? "Your agent"; const busy = accept.isPending || decline.isPending; const error = accept.error ?? decline.error; return (

{agentName}

{payload.message ?? DEFAULT_OFFER}

{payload.title}

{payload.cadenceLabel}

{error ? (

{describeError(error).body}

) : null}
); }