2026-05-17 17:04:57 +08:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
import { MessageBubble } from "@/components/MessageBubble";
|
2026-05-30 23:45:26 +08:00
|
|
|
import { AgentActivityCluster } from "@/components/thread/AgentActivityCluster";
|
|
|
|
|
import { normalizeActivityTimeline, type TurnUnit } from "@/lib/activity-timeline";
|
2026-05-24 13:38:37 +08:00
|
|
|
import type { CliAppInfo, McpPresetInfo, UIMessage } from "@/lib/types";
|
2026-04-18 18:51:53 +00:00
|
|
|
|
|
|
|
|
interface ThreadMessagesProps {
|
|
|
|
|
messages: UIMessage[];
|
2026-05-30 23:45:26 +08:00
|
|
|
/** When true, agent turn still in flight — keeps activity timeline expanded. */
|
2026-05-16 01:14:11 +08:00
|
|
|
isStreaming?: boolean;
|
2026-05-17 17:04:57 +08:00
|
|
|
hiddenMessageCount?: number;
|
|
|
|
|
onLoadEarlier?: () => void;
|
2026-05-22 22:25:12 +08:00
|
|
|
cliApps?: CliAppInfo[];
|
2026-05-24 13:38:37 +08:00
|
|
|
mcpPresets?: McpPresetInfo[];
|
2026-06-06 19:49:33 +08:00
|
|
|
onOpenFilePreview?: (path: string) => void;
|
2026-04-18 18:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-30 23:45:26 +08:00
|
|
|
export type DisplayUnit = TurnUnit;
|
2026-05-16 01:14:11 +08:00
|
|
|
|
|
|
|
|
/** True when this unit index is the last assistant text slice before the next user message (or end of thread). */
|
|
|
|
|
export function isFinalAssistantSliceBeforeNextUser(
|
|
|
|
|
units: DisplayUnit[],
|
|
|
|
|
index: number,
|
|
|
|
|
): boolean {
|
|
|
|
|
const u = units[index];
|
2026-05-30 23:45:26 +08:00
|
|
|
if (u.type !== "message" || u.message.role !== "assistant") return true;
|
2026-05-16 01:14:11 +08:00
|
|
|
for (let j = index + 1; j < units.length; j++) {
|
|
|
|
|
const v = units[j];
|
2026-05-30 23:45:26 +08:00
|
|
|
if (v.type === "message" && v.message.role === "user") break;
|
2026-05-16 01:14:11 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 19:49:33 +08:00
|
|
|
export function buildDisplayUnits(
|
|
|
|
|
messages: UIMessage[],
|
|
|
|
|
isStreaming = false,
|
|
|
|
|
): DisplayUnit[] {
|
|
|
|
|
return normalizeActivityTimeline(messages, {
|
|
|
|
|
preserveTrailingActivity: isStreaming,
|
|
|
|
|
});
|
2026-05-17 17:11:38 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 17:04:57 +08:00
|
|
|
export function assistantCopyFlags(units: DisplayUnit[]): boolean[] {
|
|
|
|
|
const flags = new Array<boolean>(units.length).fill(true);
|
|
|
|
|
let hasLaterUnitBeforeUser = false;
|
|
|
|
|
for (let i = units.length - 1; i >= 0; i -= 1) {
|
|
|
|
|
const unit = units[i];
|
2026-05-30 23:45:26 +08:00
|
|
|
if (unit.type === "message" && unit.message.role === "user") {
|
2026-05-17 17:04:57 +08:00
|
|
|
hasLaterUnitBeforeUser = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-05-30 23:45:26 +08:00
|
|
|
if (unit.type === "message" && unit.message.role === "assistant") {
|
2026-05-17 17:04:57 +08:00
|
|
|
flags[i] = !hasLaterUnitBeforeUser;
|
|
|
|
|
}
|
|
|
|
|
hasLaterUnitBeforeUser = true;
|
|
|
|
|
}
|
|
|
|
|
return flags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ThreadMessages({
|
|
|
|
|
messages,
|
|
|
|
|
isStreaming = false,
|
|
|
|
|
hiddenMessageCount = 0,
|
|
|
|
|
onLoadEarlier,
|
2026-05-22 22:25:12 +08:00
|
|
|
cliApps = [],
|
2026-05-24 13:38:37 +08:00
|
|
|
mcpPresets = [],
|
2026-06-06 19:49:33 +08:00
|
|
|
onOpenFilePreview,
|
2026-05-17 17:04:57 +08:00
|
|
|
}: ThreadMessagesProps) {
|
|
|
|
|
const { t } = useTranslation();
|
2026-06-06 19:49:33 +08:00
|
|
|
const units = useMemo(() => buildDisplayUnits(messages, isStreaming), [isStreaming, messages]);
|
2026-05-17 17:04:57 +08:00
|
|
|
const copyFlags = useMemo(() => assistantCopyFlags(units), [units]);
|
2026-05-30 23:45:26 +08:00
|
|
|
const liveActivityClusterIndices = useMemo(
|
|
|
|
|
() => isStreaming ? currentActivityClusterIndices(units) : new Set<number>(),
|
2026-05-17 23:52:14 +08:00
|
|
|
[isStreaming, units],
|
|
|
|
|
);
|
2026-05-16 01:14:11 +08:00
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
return (
|
2026-05-13 08:05:34 +00:00
|
|
|
<div className="flex w-full flex-col">
|
2026-05-17 17:04:57 +08:00
|
|
|
{hiddenMessageCount > 0 && onLoadEarlier ? (
|
|
|
|
|
<div className="mb-4 flex justify-center">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={onLoadEarlier}
|
|
|
|
|
className="rounded-full border border-border/60 bg-background/85 px-3 py-1.5 text-xs font-medium text-muted-foreground shadow-sm transition-colors hover:bg-muted/55 hover:text-foreground"
|
|
|
|
|
>
|
|
|
|
|
{t("thread.loadEarlier", {
|
|
|
|
|
count: hiddenMessageCount,
|
|
|
|
|
defaultValue: "Load earlier messages",
|
|
|
|
|
})}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2026-05-16 01:14:11 +08:00
|
|
|
{units.map((unit, index) => {
|
|
|
|
|
const prev = units[index - 1];
|
|
|
|
|
const marginTop =
|
|
|
|
|
index > 0
|
|
|
|
|
? marginAfterPrevUnit(prev)
|
|
|
|
|
: "";
|
|
|
|
|
const next = units[index + 1];
|
|
|
|
|
const hasBodyBelow =
|
2026-05-30 23:45:26 +08:00
|
|
|
unit.type === "activity"
|
|
|
|
|
&& next?.type === "message"
|
2026-05-16 01:14:11 +08:00
|
|
|
&& next.message.role === "assistant";
|
|
|
|
|
|
2026-06-02 15:19:10 +08:00
|
|
|
const userPromptId =
|
|
|
|
|
unit.type === "message" && unit.message.role === "user"
|
|
|
|
|
? unit.message.id
|
|
|
|
|
: undefined;
|
|
|
|
|
|
2026-05-13 08:05:34 +00:00
|
|
|
return (
|
2026-06-02 15:19:10 +08:00
|
|
|
<div
|
|
|
|
|
key={unitKey(unit, index)}
|
|
|
|
|
className={marginTop}
|
|
|
|
|
data-user-prompt-id={userPromptId}
|
|
|
|
|
>
|
2026-05-30 23:45:26 +08:00
|
|
|
{unit.type === "activity" ? (
|
2026-05-16 01:14:11 +08:00
|
|
|
<AgentActivityCluster
|
|
|
|
|
messages={unit.messages}
|
2026-05-30 23:45:26 +08:00
|
|
|
isTurnStreaming={liveActivityClusterIndices.has(index)}
|
2026-05-16 01:14:11 +08:00
|
|
|
hasBodyBelow={hasBodyBelow}
|
2026-05-30 23:45:26 +08:00
|
|
|
turnLatencyMs={unit.turnLatencyMs}
|
2026-05-22 22:25:12 +08:00
|
|
|
cliApps={cliApps}
|
2026-05-24 13:38:37 +08:00
|
|
|
mcpPresets={mcpPresets}
|
2026-06-06 19:49:33 +08:00
|
|
|
onOpenFilePreview={onOpenFilePreview}
|
2026-05-16 01:14:11 +08:00
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<MessageBubble
|
|
|
|
|
message={unit.message}
|
|
|
|
|
showAssistantCopyAction={
|
|
|
|
|
unit.message.role === "assistant"
|
2026-05-17 17:04:57 +08:00
|
|
|
? copyFlags[index]
|
2026-05-16 01:14:11 +08:00
|
|
|
: true
|
|
|
|
|
}
|
2026-05-22 22:25:12 +08:00
|
|
|
cliApps={cliApps}
|
2026-05-24 13:38:37 +08:00
|
|
|
mcpPresets={mcpPresets}
|
2026-06-06 19:49:33 +08:00
|
|
|
onOpenFilePreview={onOpenFilePreview}
|
2026-05-16 01:14:11 +08:00
|
|
|
/>
|
|
|
|
|
)}
|
2026-05-13 08:05:34 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2026-04-18 18:51:53 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-05-13 08:05:34 +00:00
|
|
|
|
2026-05-30 23:45:26 +08:00
|
|
|
function currentActivityClusterIndices(units: DisplayUnit[]): Set<number> {
|
|
|
|
|
const indices = new Set<number>();
|
|
|
|
|
let markedCurrentActivity = false;
|
2026-05-29 03:42:53 +08:00
|
|
|
for (let i = units.length - 1; i >= 0; i -= 1) {
|
|
|
|
|
const unit = units[i];
|
2026-05-30 23:45:26 +08:00
|
|
|
if (unit.type === "activity") {
|
|
|
|
|
if (!markedCurrentActivity) {
|
|
|
|
|
indices.add(i);
|
|
|
|
|
markedCurrentActivity = true;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-05-29 03:42:53 +08:00
|
|
|
if (unit.message.role === "assistant" && unit.message.isStreaming) continue;
|
|
|
|
|
if (unit.message.role === "user") break;
|
|
|
|
|
}
|
2026-05-30 23:45:26 +08:00
|
|
|
return indices;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-16 01:14:11 +08:00
|
|
|
function unitKey(unit: DisplayUnit, index: number): string {
|
2026-05-30 23:45:26 +08:00
|
|
|
if (unit.type === "activity") {
|
2026-05-16 01:14:11 +08:00
|
|
|
const anchor = unit.messages[0]?.id;
|
2026-05-30 23:45:26 +08:00
|
|
|
return anchor != null ? `activity-${anchor}` : `activity-idx-${index}`;
|
2026-05-16 01:14:11 +08:00
|
|
|
}
|
|
|
|
|
return unit.message.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function marginAfterPrevUnit(prev: DisplayUnit): string {
|
2026-05-30 23:45:26 +08:00
|
|
|
if (prev.type === "activity") {
|
2026-05-16 01:14:11 +08:00
|
|
|
return "mt-4";
|
|
|
|
|
}
|
|
|
|
|
const p = prev.message;
|
|
|
|
|
const denseP =
|
|
|
|
|
p.kind === "trace"
|
2026-05-13 08:05:34 +00:00
|
|
|
|| (
|
2026-05-16 01:14:11 +08:00
|
|
|
p.role === "assistant"
|
|
|
|
|
&& p.content.trim().length === 0
|
|
|
|
|
&& (!!p.reasoning || !!p.reasoningStreaming)
|
|
|
|
|
);
|
|
|
|
|
if (denseP) {
|
|
|
|
|
return "mt-2";
|
|
|
|
|
}
|
|
|
|
|
return "mt-5";
|
2026-05-13 08:05:34 +00:00
|
|
|
}
|