2026-02-11 17:55:52 +05:30
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { makeAssistantToolUI } from "@assistant-ui/react";
|
2026-02-11 18:50:57 +05:30
|
|
|
import { useAtomValue, useSetAtom } from "jotai";
|
2026-02-12 03:19:38 +05:30
|
|
|
import { Dot, FileTextIcon } from "lucide-react";
|
2026-02-11 22:07:31 +05:30
|
|
|
import { useParams, usePathname } from "next/navigation";
|
2026-02-11 18:50:57 +05:30
|
|
|
import { useEffect, useState } from "react";
|
2026-02-11 17:55:52 +05:30
|
|
|
import { z } from "zod";
|
2026-02-13 02:43:26 +05:30
|
|
|
import { openReportPanelAtom, reportPanelAtom } from "@/atoms/chat/report-panel.atom";
|
2026-02-13 01:34:43 -08:00
|
|
|
import { TextShimmerLoader } from "@/components/prompt-kit/loader";
|
2026-02-11 17:55:52 +05:30
|
|
|
import { baseApiService } from "@/lib/apis/base-api.service";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Zod schemas for runtime validation
|
|
|
|
|
*/
|
|
|
|
|
const GenerateReportArgsSchema = z.object({
|
|
|
|
|
topic: z.string(),
|
|
|
|
|
source_content: z.string(),
|
|
|
|
|
report_style: z.string().nullish(),
|
|
|
|
|
user_instructions: z.string().nullish(),
|
2026-02-12 03:19:38 +05:30
|
|
|
parent_report_id: z.number().nullish(),
|
2026-02-11 17:55:52 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const GenerateReportResultSchema = z.object({
|
|
|
|
|
status: z.enum(["ready", "failed"]),
|
|
|
|
|
report_id: z.number().nullish(),
|
|
|
|
|
title: z.string().nullish(),
|
|
|
|
|
word_count: z.number().nullish(),
|
|
|
|
|
message: z.string().nullish(),
|
|
|
|
|
error: z.string().nullish(),
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-11 18:50:57 +05:30
|
|
|
const ReportMetadataResponseSchema = z.object({
|
2026-02-11 17:55:52 +05:30
|
|
|
id: z.number(),
|
|
|
|
|
title: z.string(),
|
|
|
|
|
report_metadata: z
|
|
|
|
|
.object({
|
2026-02-11 20:04:36 +05:30
|
|
|
status: z.enum(["ready", "failed"]).nullish(),
|
|
|
|
|
error_message: z.string().nullish(),
|
2026-02-11 17:55:52 +05:30
|
|
|
word_count: z.number().nullish(),
|
|
|
|
|
section_count: z.number().nullish(),
|
|
|
|
|
})
|
|
|
|
|
.nullish(),
|
2026-02-12 03:19:38 +05:30
|
|
|
report_group_id: z.number().nullish(),
|
|
|
|
|
versions: z
|
|
|
|
|
.array(
|
|
|
|
|
z.object({
|
|
|
|
|
id: z.number(),
|
|
|
|
|
created_at: z.string().nullish(),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.nullish(),
|
2026-02-11 17:55:52 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Types derived from Zod schemas
|
|
|
|
|
*/
|
|
|
|
|
type GenerateReportArgs = z.infer<typeof GenerateReportArgsSchema>;
|
|
|
|
|
type GenerateReportResult = z.infer<typeof GenerateReportResultSchema>;
|
|
|
|
|
|
2026-02-11 18:50:57 +05:30
|
|
|
/**
|
|
|
|
|
* Loading state component shown while report is being generated.
|
2026-02-11 20:04:36 +05:30
|
|
|
* Matches the compact card layout of the completed ReportCard.
|
2026-02-11 17:55:52 +05:30
|
|
|
*/
|
|
|
|
|
function ReportGeneratingState({ topic }: { topic: string }) {
|
|
|
|
|
return (
|
2026-02-11 18:50:57 +05:30
|
|
|
<div className="my-4 overflow-hidden rounded-xl border bg-card">
|
2026-02-11 22:48:33 +05:30
|
|
|
<div className="flex w-full items-center gap-2 sm:gap-3 bg-muted/30 px-4 py-5 sm:px-6 sm:py-6">
|
|
|
|
|
<div className="flex size-8 sm:size-12 shrink-0 items-center justify-center rounded-lg bg-primary/10">
|
|
|
|
|
<FileTextIcon className="size-4 sm:size-6 text-primary" />
|
2026-02-11 20:04:36 +05:30
|
|
|
</div>
|
2026-02-11 18:50:57 +05:30
|
|
|
<div className="min-w-0 flex-1">
|
2026-02-11 22:48:33 +05:30
|
|
|
<h3 className="font-semibold text-foreground text-sm sm:text-base leading-tight line-clamp-2">
|
2026-02-11 17:55:52 +05:30
|
|
|
{topic}
|
|
|
|
|
</h3>
|
2026-02-11 21:39:22 +05:30
|
|
|
<TextShimmerLoader text="Putting things together" size="sm" />
|
2026-02-11 17:55:52 +05:30
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Error state component shown when report generation fails
|
|
|
|
|
*/
|
|
|
|
|
function ReportErrorState({ title, error }: { title: string; error: string }) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="my-4 overflow-hidden rounded-xl border bg-card">
|
2026-02-11 22:48:33 +05:30
|
|
|
<div className="flex items-center gap-2 sm:gap-3 bg-muted/30 px-4 py-5 sm:px-6 sm:py-6">
|
|
|
|
|
<div className="flex size-8 sm:size-12 shrink-0 items-center justify-center rounded-lg bg-muted/60">
|
|
|
|
|
<FileTextIcon className="size-4 sm:size-6 text-muted-foreground" />
|
2026-02-11 17:55:52 +05:30
|
|
|
</div>
|
|
|
|
|
<div className="min-w-0 flex-1">
|
2026-02-11 22:48:33 +05:30
|
|
|
<h3 className="font-semibold text-muted-foreground text-sm sm:text-base leading-tight line-clamp-2">
|
2026-02-11 17:55:52 +05:30
|
|
|
{title}
|
|
|
|
|
</h3>
|
2026-02-13 02:43:26 +05:30
|
|
|
<p className="text-muted-foreground/60 text-[11px] sm:text-xs mt-0.5 truncate">{error}</p>
|
2026-02-11 17:55:52 +05:30
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-11 18:50:57 +05:30
|
|
|
* Compact report card shown inline in the chat.
|
|
|
|
|
* Clicking it opens the report in the right-side panel (desktop) or Vaul drawer (mobile).
|
2026-02-11 17:55:52 +05:30
|
|
|
*/
|
2026-02-11 18:50:57 +05:30
|
|
|
function ReportCard({
|
2026-02-11 17:55:52 +05:30
|
|
|
reportId,
|
|
|
|
|
title,
|
|
|
|
|
wordCount,
|
2026-02-11 22:07:31 +05:30
|
|
|
shareToken,
|
2026-02-11 17:55:52 +05:30
|
|
|
}: {
|
|
|
|
|
reportId: number;
|
|
|
|
|
title: string;
|
|
|
|
|
wordCount?: number;
|
2026-02-11 22:07:31 +05:30
|
|
|
/** When set, uses public endpoint for fetching report data */
|
|
|
|
|
shareToken?: string | null;
|
2026-02-11 17:55:52 +05:30
|
|
|
}) {
|
2026-02-11 18:50:57 +05:30
|
|
|
const openPanel = useSetAtom(openReportPanelAtom);
|
|
|
|
|
const panelState = useAtomValue(reportPanelAtom);
|
|
|
|
|
const [metadata, setMetadata] = useState<{
|
|
|
|
|
title: string;
|
|
|
|
|
wordCount: number | null;
|
2026-02-12 03:19:38 +05:30
|
|
|
versionLabel: string | null;
|
|
|
|
|
}>({ title, wordCount: wordCount ?? null, versionLabel: null });
|
2026-02-11 17:55:52 +05:30
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
2026-02-11 20:04:36 +05:30
|
|
|
const [error, setError] = useState<string | null>(null);
|
2026-02-11 17:55:52 +05:30
|
|
|
|
2026-02-12 03:19:38 +05:30
|
|
|
// Fetch lightweight metadata (title + counts + version info)
|
2026-02-11 17:55:52 +05:30
|
|
|
useEffect(() => {
|
2026-02-11 18:50:57 +05:30
|
|
|
let cancelled = false;
|
|
|
|
|
const fetchMetadata = async () => {
|
2026-02-11 17:55:52 +05:30
|
|
|
setIsLoading(true);
|
2026-02-11 20:04:36 +05:30
|
|
|
setError(null);
|
2026-02-11 17:55:52 +05:30
|
|
|
try {
|
2026-02-11 22:07:31 +05:30
|
|
|
const url = shareToken
|
|
|
|
|
? `/api/v1/public/${shareToken}/reports/${reportId}/content`
|
|
|
|
|
: `/api/v1/reports/${reportId}/content`;
|
|
|
|
|
const rawData = await baseApiService.get<unknown>(url);
|
2026-02-11 18:50:57 +05:30
|
|
|
if (cancelled) return;
|
|
|
|
|
const parsed = ReportMetadataResponseSchema.safeParse(rawData);
|
2026-02-11 17:55:52 +05:30
|
|
|
if (parsed.success) {
|
2026-02-11 20:04:36 +05:30
|
|
|
// Check if report was marked as failed in metadata
|
|
|
|
|
if (parsed.data.report_metadata?.status === "failed") {
|
2026-02-13 02:43:26 +05:30
|
|
|
setError(parsed.data.report_metadata?.error_message || "Report generation failed");
|
2026-02-11 20:04:36 +05:30
|
|
|
} else {
|
2026-02-12 03:19:38 +05:30
|
|
|
// Determine version label from versions array
|
|
|
|
|
let versionLabel: string | null = null;
|
|
|
|
|
const versions = parsed.data.versions;
|
|
|
|
|
if (versions && versions.length > 1) {
|
|
|
|
|
const idx = versions.findIndex((v) => v.id === reportId);
|
|
|
|
|
if (idx >= 0) {
|
|
|
|
|
versionLabel = `version ${idx + 1}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-11 20:04:36 +05:30
|
|
|
setMetadata({
|
|
|
|
|
title: parsed.data.title || title,
|
2026-02-13 02:43:26 +05:30
|
|
|
wordCount: parsed.data.report_metadata?.word_count ?? wordCount ?? null,
|
2026-02-12 03:19:38 +05:30
|
|
|
versionLabel,
|
2026-02-11 20:04:36 +05:30
|
|
|
});
|
|
|
|
|
}
|
2026-02-11 17:55:52 +05:30
|
|
|
}
|
2026-02-11 18:50:57 +05:30
|
|
|
} catch {
|
2026-02-11 20:04:36 +05:30
|
|
|
if (!cancelled) setError("No report found");
|
2026-02-11 17:55:52 +05:30
|
|
|
} finally {
|
2026-02-11 18:50:57 +05:30
|
|
|
if (!cancelled) setIsLoading(false);
|
2026-02-11 17:55:52 +05:30
|
|
|
}
|
|
|
|
|
};
|
2026-02-11 18:50:57 +05:30
|
|
|
fetchMetadata();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
2026-02-11 22:07:31 +05:30
|
|
|
}, [reportId, title, wordCount, shareToken]);
|
2026-02-11 17:55:52 +05:30
|
|
|
|
2026-02-11 20:04:36 +05:30
|
|
|
// Show non-clickable error card for any error (failed status, not found, etc.)
|
|
|
|
|
if (!isLoading && error) {
|
|
|
|
|
return <ReportErrorState title={title} error={error} />;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-11 18:50:57 +05:30
|
|
|
const isActive = panelState.isOpen && panelState.reportId === reportId;
|
2026-02-11 17:55:52 +05:30
|
|
|
|
2026-02-11 18:50:57 +05:30
|
|
|
const handleOpen = () => {
|
|
|
|
|
openPanel({
|
|
|
|
|
reportId,
|
|
|
|
|
title: metadata.title,
|
|
|
|
|
wordCount: metadata.wordCount ?? undefined,
|
2026-02-11 22:07:31 +05:30
|
|
|
shareToken,
|
2026-02-11 18:50:57 +05:30
|
|
|
});
|
|
|
|
|
};
|
2026-02-11 17:55:52 +05:30
|
|
|
|
|
|
|
|
return (
|
2026-02-11 18:50:57 +05:30
|
|
|
<div
|
2026-02-12 03:49:47 +05:30
|
|
|
className={`my-4 overflow-hidden rounded-xl border bg-card transition-colors ${isActive ? "ring-1 ring-primary/50" : ""}`}
|
2026-02-11 18:50:57 +05:30
|
|
|
>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={handleOpen}
|
2026-02-11 22:48:33 +05:30
|
|
|
className="flex w-full items-center gap-2 sm:gap-3 bg-muted/30 px-4 py-5 sm:px-6 sm:py-6 text-left transition-colors hover:bg-muted/50 focus:outline-none focus-visible:outline-none"
|
2026-02-11 18:50:57 +05:30
|
|
|
>
|
2026-02-11 22:48:33 +05:30
|
|
|
<div className="flex size-8 sm:size-12 shrink-0 items-center justify-center rounded-lg bg-primary/10">
|
|
|
|
|
<FileTextIcon className="size-4 sm:size-6 text-primary" />
|
2026-02-11 17:55:52 +05:30
|
|
|
</div>
|
2026-02-11 18:50:57 +05:30
|
|
|
<div className="min-w-0 flex-1">
|
2026-02-11 22:48:33 +05:30
|
|
|
<h3 className="font-semibold text-foreground text-sm sm:text-base leading-tight line-clamp-2">
|
2026-02-11 18:50:57 +05:30
|
|
|
{isLoading ? title : metadata.title}
|
|
|
|
|
</h3>
|
|
|
|
|
<p className="text-muted-foreground text-[10px] sm:text-xs mt-0.5">
|
|
|
|
|
{isLoading ? (
|
|
|
|
|
<span className="inline-block h-3 w-24 rounded bg-muted/60 animate-pulse" />
|
2026-02-11 17:55:52 +05:30
|
|
|
) : (
|
2026-02-11 18:50:57 +05:30
|
|
|
<>
|
2026-02-13 02:43:26 +05:30
|
|
|
{metadata.wordCount != null && `${metadata.wordCount.toLocaleString()} words`}
|
|
|
|
|
{metadata.wordCount != null && metadata.versionLabel && (
|
|
|
|
|
<Dot className="inline size-4" />
|
|
|
|
|
)}
|
2026-02-12 03:19:38 +05:30
|
|
|
{metadata.versionLabel}
|
2026-02-11 18:50:57 +05:30
|
|
|
</>
|
2026-02-11 17:55:52 +05:30
|
|
|
)}
|
2026-02-11 18:50:57 +05:30
|
|
|
</p>
|
2026-02-11 17:55:52 +05:30
|
|
|
</div>
|
2026-02-11 18:50:57 +05:30
|
|
|
</button>
|
2026-02-11 17:55:52 +05:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate Report Tool UI Component
|
|
|
|
|
*
|
|
|
|
|
* This component is registered with assistant-ui to render custom UI
|
|
|
|
|
* when the generate_report tool is called by the agent.
|
|
|
|
|
*
|
|
|
|
|
* Unlike podcast (which uses polling), the report is generated inline
|
|
|
|
|
* and the result contains status: "ready" immediately.
|
|
|
|
|
*/
|
2026-02-13 02:43:26 +05:30
|
|
|
export const GenerateReportToolUI = makeAssistantToolUI<GenerateReportArgs, GenerateReportResult>({
|
2026-02-11 17:55:52 +05:30
|
|
|
toolName: "generate_report",
|
|
|
|
|
render: function GenerateReportUI({ args, result, status }) {
|
2026-02-11 22:07:31 +05:30
|
|
|
const params = useParams();
|
|
|
|
|
const pathname = usePathname();
|
|
|
|
|
const isPublicRoute = pathname?.startsWith("/public/");
|
|
|
|
|
const shareToken = isPublicRoute && typeof params?.token === "string" ? params.token : null;
|
|
|
|
|
|
2026-02-11 17:55:52 +05:30
|
|
|
const topic = args.topic || "Report";
|
|
|
|
|
|
|
|
|
|
// Loading state - tool is still running (LLM generating report)
|
|
|
|
|
if (status.type === "running" || status.type === "requires-action") {
|
|
|
|
|
return <ReportGeneratingState topic={topic} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Incomplete/cancelled state
|
|
|
|
|
if (status.type === "incomplete") {
|
|
|
|
|
if (status.reason === "cancelled") {
|
|
|
|
|
return (
|
|
|
|
|
<div className="my-4 rounded-xl border border-muted p-3 sm:p-4 text-muted-foreground">
|
|
|
|
|
<p className="flex items-center gap-1.5 sm:gap-2 text-xs sm:text-sm">
|
|
|
|
|
<FileTextIcon className="size-3.5 sm:size-4" />
|
|
|
|
|
<span className="line-through">Report generation cancelled</span>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (status.reason === "error") {
|
|
|
|
|
return (
|
|
|
|
|
<ReportErrorState
|
|
|
|
|
title={topic}
|
|
|
|
|
error={typeof status.error === "string" ? status.error : "An error occurred"}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No result yet
|
|
|
|
|
if (!result) {
|
|
|
|
|
return <ReportGeneratingState topic={topic} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Failed result
|
|
|
|
|
if (result.status === "failed") {
|
2026-02-13 02:43:26 +05:30
|
|
|
return (
|
|
|
|
|
<ReportErrorState
|
|
|
|
|
title={result.title || topic}
|
|
|
|
|
error={result.error || "Generation failed"}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2026-02-11 17:55:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ready with report_id
|
|
|
|
|
if (result.status === "ready" && result.report_id) {
|
|
|
|
|
return (
|
2026-02-11 18:50:57 +05:30
|
|
|
<ReportCard
|
2026-02-11 17:55:52 +05:30
|
|
|
reportId={result.report_id}
|
|
|
|
|
title={result.title || topic}
|
|
|
|
|
wordCount={result.word_count ?? undefined}
|
2026-02-11 22:07:31 +05:30
|
|
|
shareToken={shareToken}
|
2026-02-11 17:55:52 +05:30
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback - missing required data
|
|
|
|
|
return <ReportErrorState title={topic} error="Missing report ID" />;
|
|
|
|
|
},
|
|
|
|
|
});
|