Files
kandev/apps/web/components/task/simple/components/run-error-entry.tsx
T
2026-08-16 17:41:43 +01:00

129 lines
4.8 KiB
TypeScript

"use client";
import { useState } from "react";
import {
IconAlertTriangle,
IconChevronDown,
IconPlayerPlay,
IconRefresh,
} from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@kandev/ui/collapsible";
import { useAppStore } from "@/components/state-provider";
import { selectOfficeAgentProfiles } from "@/lib/state/slices/office/selectors";
import { getWebSocketClient } from "@/lib/ws/connection";
import { formatRelativeTime } from "@/lib/utils";
import { AgentAvatar } from "@/app/office/components/agent-avatar";
import { RemediationLink } from "@/components/task/remediation-link";
import type { RunError } from "@/app/office/tasks/[id]/types";
import { ManagedRuntimeNpmRunError } from "./managed-runtime-npm-run-error";
import { useTranslation } from "react-i18next";
type RunErrorEntryProps = {
taskId: string;
error: RunError;
};
/**
* Top-level chat entry rendered when an office session is in FAILED
* state. Replaces the legacy red action-message banner: shows a short
* generic header, a Show details collapsible exposing the verbatim
* raw payload (for bug reports), and the Resume / Start fresh
* buttons. Click handlers wire to the existing `session.recover` WS
* request so the recovery semantics are unchanged.
*/
export function RunErrorEntry({ taskId, error }: RunErrorEntryProps) {
const { t } = useTranslation();
const agentName = useAppStore((s) =>
error.agentProfileId
? (selectOfficeAgentProfiles(s).find((a) => a.id === error.agentProfileId)?.name ??
t("task:agent"))
: t("task:agent"),
);
const [showDetails, setShowDetails] = useState(false);
const handleRecover = async (action: "resume" | "fresh_start" | "runtime_retry") => {
const client = getWebSocketClient();
if (!client) return;
try {
await client.request("session.recover", {
task_id: taskId,
session_id: error.sessionId,
action,
});
} catch {
// No-op — the chat will reflect any subsequent state via WS.
}
};
if (error.failureCode === "managed_runtime_npm_resolution") {
return (
<ManagedRuntimeNpmRunError
error={error}
agentName={agentName}
onRetry={() => handleRecover("runtime_retry")}
/>
);
}
return (
<div className="flex gap-3 py-3 border-b border-border/50">
<AgentAvatar name={agentName} size="md" />
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 flex-wrap">
<span className="font-medium text-sm">{agentName}</span>
<span className="inline-flex items-center gap-1 text-xs text-red-600 dark:text-red-400">
<IconAlertTriangle className="h-3.5 w-3.5" />
{t("task:stoppedWithAnError")}
</span>
<span className="text-xs text-muted-foreground">
{formatRelativeTime(error.failedAt)}
</span>
</div>
<p className="mt-1 text-sm text-muted-foreground">{t("task:theAgentStoppedWithAnError")}</p>
{error.rawPayload && (
<Collapsible open={showDetails} onOpenChange={setShowDetails} className="mt-2">
<CollapsibleTrigger className="flex items-center gap-1 text-xs text-muted-foreground cursor-pointer hover:text-foreground transition-colors">
<IconChevronDown
className={`h-3.5 w-3.5 transition-transform ${showDetails ? "rotate-180" : ""}`}
/>
{t("task:showDetails")}
</CollapsibleTrigger>
<CollapsibleContent>
<pre
className="mt-1 text-[11px] font-mono text-muted-foreground bg-muted/50 rounded p-2 overflow-auto max-h-[300px] whitespace-pre-wrap break-words"
data-testid="run-error-raw-payload"
>
{error.rawPayload}
</pre>
</CollapsibleContent>
</Collapsible>
)}
<div className="mt-2 flex items-center gap-2 flex-wrap">
<RemediationLink url={error.remediationUrl} />
<Button
variant="outline"
size="sm"
className="h-7 text-xs cursor-pointer gap-1.5"
onClick={() => handleRecover("resume")}
data-testid="run-error-resume-button"
>
<IconRefresh className="h-3 w-3" />
{t("task:resumeSession")}
</Button>
<Button
variant="outline"
size="sm"
className="h-7 text-xs cursor-pointer gap-1.5"
onClick={() => handleRecover("fresh_start")}
data-testid="run-error-fresh-button"
>
<IconPlayerPlay className="h-3 w-3" />
{t("task:startFreshSession")}
</Button>
</div>
</div>
</div>
);
}