Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
// SPDX-License-Identifier: AGPL-3.0-only
import type { Citation } from "@/components/assistant-ui/citation-utils";
import { DocumentSourcesGroup } from "@/components/assistant-ui/rag-sources";
import {
type SourceData,
SourcesGroup,
} from "@/components/assistant-ui/sources";
import { MarkdownPreview } from "@/components/markdown/markdown-preview";
import { Button } from "@/components/ui/button";
import { Spinner } from "@/components/ui/spinner";
import { cn } from "@/lib/utils";
import { useAuiState } from "@assistant-ui/react";
import { Telescope02Icon, Alert02Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { type ReactElement, useEffect } from "react";
import {
ensureResearchRunFollowed,
ingestResearchUpdate,
openResearchRun,
runningResearchActivityTitle,
useResearchRunStore,
} from "../stores/research-run-store";
import type { ResearchMessageMetadata } from "../types/research";
import { researchReplyOwnsRun } from "../utils/research-run-binding";
import { researchCitation } from "../utils/research-citations";
import { researchStatusLabel } from "./research-activity-panel";
import { Tick02Icon } from "@/lib/tick-icon";
export function ResearchMessage(): ReactElement | null {
const metadata = useAuiState(
({ message }) =>
(message.metadata as { custom?: ResearchMessageMetadata } | undefined)
?.custom ?? {},
);
const fallbackText = useAuiState(({ message }) =>
message.parts
.filter((part) => part.type === "text")
.map((part) => part.text)
.join("\n"),
);
const messageId = useAuiState(({ message }) => message.id);
const runId = metadata.researchRunId ?? metadata.researchRun?.id ?? "";
const session = useResearchRunStore((state) => state.sessions[runId]);
const initialRun = metadata.researchRun;
const ownsRun = researchReplyOwnsRun(
session?.run?.assistantMessageId,
messageId,
);
useEffect(() => {
if (!runId || !ownsRun) {
return;
}
if (initialRun) {
ingestResearchUpdate(initialRun);
}
if (!session?.following) {
ensureResearchRunFollowed(runId, initialRun);
}
}, [runId, initialRun, ownsRun, session?.following]);
const run = ownsRun ? (session?.run ?? metadata.researchRun) : undefined;
if (!run) {
if (fallbackText.trim()) {
return (
<MarkdownPreview
markdown={fallbackText}
className="max-h-none overflow-visible border-0 bg-transparent p-0 text-ui-15p5"
/>
);
}
if (!ownsRun) {
return null;
}
return (
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<Spinner /> Loading research…
</div>
);
}
// failed reports include their own notice; avoid repeating it here.
if ((run.status === "completed" || run.status === "failed") && run.report) {
const failed = run.status === "failed";
const sources: SourceData[] = run.sources.map((source) => ({
id: String(source.id ?? source.url),
url: source.url,
title: source.title || source.url,
description: source.snippet ?? undefined,
}));
const documentSources: Citation[] = [];
const mcpSources: Citation[] = [];
(run.documentSources ?? []).forEach((source, index) => {
(source.kind === "mcp" ? mcpSources : documentSources).push(
researchCitation(source, index),
);
});
const documentCount = new Set(
[...documentSources, ...mcpSources].map(
(source) => source.documentId ?? source.filename,
),
).size;
const sourceCount = sources.length + documentCount;
return (
<div className="min-w-0">
<button
type="button"
onClick={() => openResearchRun(run)}
className="mb-3 flex items-center gap-2 rounded-full text-sm text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
>
<span className="flex size-5 items-center justify-center rounded-full bg-primary/10 text-primary">
{failed ? (
<HugeiconsIcon icon={Alert02Icon} className="size-3" />
) : (
<HugeiconsIcon icon={Tick02Icon} className="size-3" />
)}
</span>
<span>
{failed
? `Deep research failed · ${sourceCount} sources`
: `Deep research completed · ${sourceCount} sources`}
</span>
<span className="text-primary">View activity</span>
</button>
<MarkdownPreview
markdown={run.report}
className="max-h-none overflow-visible border-0 bg-transparent p-0 text-ui-15p5"
defer={true}
/>
<SourcesGroup sources={sources} allowRemoteIcons={false} />
<DocumentSourcesGroup sources={documentSources} />
<DocumentSourcesGroup sources={mcpSources} label="MCP Sources" />
</div>
);
}
const failed = run.status === "failed";
const cancelled = run.status === "cancelled";
const needsApproval = run.status === "awaiting_approval";
// name the model call so long silent phases do not appear stalled.
const liveDetail =
runningResearchActivityTitle(session?.activities) ??
run.plan?.title ??
"Building a rigorous research plan…";
return (
<div
className={cn(
"rounded-2xl border border-border/70 bg-card/65 p-4",
needsApproval && "border-amber-500/25 bg-amber-500/[0.035]",
failed && "border-destructive/25 bg-destructive/[0.025]",
)}
>
<div className="flex items-start gap-3">
<span
className={cn(
"mt-0.5 flex size-8 shrink-0 items-center justify-center rounded-sm bg-primary/10 text-primary",
failed && "bg-destructive/10 text-destructive",
)}
>
{failed ? (
<HugeiconsIcon icon={Alert02Icon} className="size-4" />
) : cancelled ? (
<HugeiconsIcon icon={Telescope02Icon} className="size-4" />
) : (
<Spinner className="size-4" />
)}
</span>
<div className="min-w-0 flex-1">
<p className="font-heading text-sm font-medium">
{failed
? "Research could not be completed"
: cancelled
? "Research stopped"
: needsApproval
? "Your research plan is ready"
: researchStatusLabel(run.status)}
</p>
<p className="mt-1 text-ui-12p5 leading-relaxed text-muted-foreground">
{session?.error
? session.error
: failed
? run.error
: needsApproval
? "Review the approach before the agent starts gathering evidence."
: cancelled
? "The activity gathered so far is still available."
: liveDetail}
</p>
<Button
size="sm"
variant={needsApproval ? "dark" : "outline"}
className="mt-3"
onClick={() => openResearchRun(run)}
>
{needsApproval ? "Review plan" : "View activity"}
</Button>
</div>
</div>
</div>
);
}