'use client' import { useState, useEffect } from 'react' import { X, CheckCircle2, XCircle, Loader2, Clock, ChevronDown, Wrench, Trash2 } from 'lucide-react' import { formatDistanceToNow } from 'date-fns' import { fr } from 'date-fns/locale/fr' import { enUS } from 'date-fns/locale/en-US' import { toast } from 'sonner' import { useLanguage } from '@/lib/i18n' interface AgentRunLogProps { agentId: string agentName: string onClose: () => void } interface Action { id: string status: string result?: string | null log?: string | null input?: string | null toolLog?: string | null tokensUsed?: number | null createdAt: string | Date } interface ToolLogStep { step: number text?: string toolCalls?: Array<{ toolName: string; args: any }> toolResults?: Array<{ toolName: string; preview?: string }> } const statusKeys: Record = { success: 'agents.status.success', failure: 'agents.status.failure', running: 'agents.status.running', pending: 'agents.status.pending', } export function AgentRunLog({ agentId, agentName, onClose }: AgentRunLogProps) { const { t, language } = useLanguage() const [actions, setActions] = useState([]) const [loading, setLoading] = useState(true) const [isDeleting, setIsDeleting] = useState(false) const dateLocale = language === 'fr' ? fr : enUS const loadActions = async () => { setLoading(true) try { const { getAgentActions } = await import('@/app/actions/agent-actions') const data = await getAgentActions(agentId) setActions(data) } catch { // Silent fail } finally { setLoading(false) } } useEffect(() => { loadActions() }, [agentId]) const handleClearHistory = async () => { if (!confirm(t('agents.runLog.clearConfirm'))) return setIsDeleting(true) try { const { deleteAgentHistory } = await import('@/app/actions/agent-actions') await deleteAgentHistory(agentId) setActions([]) toast.success(t('agents.runLog.cleared')) } catch { toast.error(t('agents.toasts.deleteError')) } finally { setIsDeleting(false) } } return (
e.stopPropagation()} >

{t('agents.runLog.title')}

{agentName}

{actions.length > 0 && ( )}
{loading && (
)} {!loading && actions.length === 0 && (

{t('agents.runLog.noHistory')}

)} {actions.map(action => { let toolSteps: ToolLogStep[] = [] try { toolSteps = action.toolLog ? JSON.parse(action.toolLog) : [] } catch {} return (
{action.status === 'success' && } {action.status === 'failure' && } {action.status === 'running' && } {action.status === 'pending' && }
{t(statusKeys[action.status] || action.status)} {formatDistanceToNow(new Date(action.createdAt), { addSuffix: true, locale: dateLocale })}
{action.log && (

{action.log}

)}
{toolSteps.length > 0 && (
{t('agents.runLog.toolTrace', { count: toolSteps.length })}
{toolSteps.map((step, i) => (
{t('agents.runLog.step', { num: step.step })} {step.toolCalls && step.toolCalls.length > 0 && (
{step.toolCalls.map((tc, j) => (
{tc.toolName} {JSON.stringify(tc.args ?? {}).substring(0, 80)}
))}
)}
))}
)}
) })}
) }