import { useState } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { AlertTriangle, CheckCircle2, CircleSlash, PackagePlus, PlayCircle, Sparkles, XCircle } from "lucide-react";
import type { ToolExampleSmokeResult } from "@paperclipai/shared";
import { queryKeys } from "@/lib/queryKeys";
import { toolsApi } from "@/api/tools";
import { ApiError } from "@/api/client";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent } from "@/components/ui/card";
import { useToast } from "@/context/ToastContext";
import { EmptyState } from "@/components/EmptyState";
import {
CapabilityBadges,
DecisionBadge,
ErrorState,
LoadingState,
RelativeTime,
RiskBadge,
ToolsPageHeader,
} from "./shared";
function errorMessage(error: unknown) {
return error instanceof ApiError ? error.message : error instanceof Error ? error.message : String(error);
}
/** A smoke check that exercised (and tripped) the runtime-error / fails-closed path. */
function isRuntimeErrorCheck(check: ToolExampleSmokeResult["checks"][number]): boolean {
const haystacks = [check.name, check.decision ?? "", check.reasonCode ?? ""];
return haystacks.some((h) => /runtime[_-]?error/i.test(String(h)));
}
function SmokeResult({ result }: { result: ToolExampleSmokeResult }) {
return (
{result.ok ? (
) : (
)}
Smoke {result.ok ? "passed" : "failed"}
{result.actor.actorType}
{result.checks.map((check) => (
{check.ok ? (
) : (
)}
{check.name.replaceAll("_", " ")}
{check.toolName ? {check.toolName} : null}
{check.decision ? : null}
{check.reasonCode ? {check.reasonCode} : null}
))}
);
}
export function ExamplesTab({ companyId }: { companyId: string }) {
const qc = useQueryClient();
const { pushToast } = useToast();
const [smokeResults, setSmokeResults] = useState>({});
const [smokeRanAt, setSmokeRanAt] = useState>({});
const examples = useQuery({
queryKey: queryKeys.tools.examples(companyId),
queryFn: () => toolsApi.listExamples(companyId),
});
const invalidateToolState = () => {
qc.invalidateQueries({ queryKey: queryKeys.tools.examples(companyId) });
qc.invalidateQueries({ queryKey: queryKeys.tools.applications(companyId) });
qc.invalidateQueries({ queryKey: queryKeys.tools.connections(companyId) });
qc.invalidateQueries({ queryKey: queryKeys.tools.runtimeSlots(companyId) });
qc.invalidateQueries({ queryKey: queryKeys.tools.runtimeHealth(companyId) });
qc.invalidateQueries({ queryKey: queryKeys.tools.audit(companyId, 100) });
};
const install = useMutation({
mutationFn: (exampleId: string) => toolsApi.installExample(companyId, exampleId),
onSuccess: (result) => {
invalidateToolState();
pushToast({
title: result.created ? "Example installed" : "Example already installed",
body: `${result.profileEntries.length} read-only tool grant(s) active`,
tone: "success",
});
},
onError: (error) =>
pushToast({
title: "Install failed",
body: errorMessage(error),
tone: "error",
}),
});
const smoke = useMutation({
mutationFn: (exampleId: string) => toolsApi.smokeExample(companyId, exampleId),
onSuccess: (result) => {
setSmokeResults((current) => ({ ...current, [result.exampleId]: result }));
setSmokeRanAt((current) => ({ ...current, [result.exampleId]: Date.now() }));
invalidateToolState();
pushToast({
title: result.ok ? "Smoke passed" : "Smoke failed",
body: `${result.checks.filter((check) => check.ok).length}/${result.checks.length} checks passed`,
tone: result.ok ? "success" : "error",
});
},
onError: (error) =>
pushToast({
title: "Smoke failed",
body: errorMessage(error),
tone: "error",
}),
});
if (examples.isLoading) return ;
if (examples.error) return examples.refetch()} />;
const list = examples.data?.examples ?? [];
return (
{list.length === 0 ? (
) : (
{list.map((example) => {
const installed = example.install.installed;
const result = smokeResults[example.id];
const ranAt = smokeRanAt[example.id];
const hasRuntimeError = result ? result.checks.some(isRuntimeErrorCheck) : false;
return (
{example.title}
{installed ? "installed" : "available"}
{example.fixture.transport}
{result ? (
{result.ok ? "smoke pass" : "smoke fail"}
) : null}
{example.description}
install.mutate(example.id)}
>
{hasRuntimeError ? "Install anyway" : installed ? "Repair" : "Install"}
smoke.mutate(example.id)}
>
Smoke
{hasRuntimeError ? (
A smoke check hit a runtime-error path — the stdio
slot died and the gateway failed closed (the agent saw no partial output). The supervisor
restarts automatically; installing is intentionally friction-y until the slot is healthy.
) : example.install.reason ? (
{example.install.reason}
) : null}
Fixture tools
{example.fixture.tools.map((tool) => (
{tool.name}
))}
Safe default profile
{example.safeDefaultProfile.name}
Allows {example.safeDefaultProfile.allowedToolNames.join(", ")}
{result ? : null}
{ranAt ? (
<>
Last smoke
>
) : (
"Not smoked this session"
)}
install.mutate(example.id)}
>
{hasRuntimeError ? "Install anyway" : installed ? "Repair" : "Install"}
);
})}
)}
);
}