mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-05-19 16:12:30 +00:00
Batch stream deltas, window long transcripts, lazy-load syntax highlighting, and refine activity/composer interactions. Add title refresh retries plus tests for streaming, windowing, code blocks, and live activity behavior.
163 lines
4.7 KiB
TypeScript
163 lines
4.7 KiB
TypeScript
import { useMemo } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { MessageBubble } from "@/components/MessageBubble";
|
|
import {
|
|
AgentActivityCluster,
|
|
isAgentActivityMember,
|
|
} from "@/components/thread/AgentActivityCluster";
|
|
import type { UIMessage } from "@/lib/types";
|
|
|
|
interface ThreadMessagesProps {
|
|
messages: UIMessage[];
|
|
/** When true, agent turn still in flight — keeps activity cluster expanded. */
|
|
isStreaming?: boolean;
|
|
hiddenMessageCount?: number;
|
|
onLoadEarlier?: () => void;
|
|
}
|
|
|
|
export type DisplayUnit =
|
|
| { type: "cluster"; messages: UIMessage[] }
|
|
| { type: "single"; message: UIMessage };
|
|
|
|
/** 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];
|
|
if (u.type !== "single" || u.message.role !== "assistant") return true;
|
|
for (let j = index + 1; j < units.length; j++) {
|
|
const v = units[j];
|
|
if (v.type === "single" && v.message.role === "user") break;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
export function buildDisplayUnits(messages: UIMessage[]): DisplayUnit[] {
|
|
const out: DisplayUnit[] = [];
|
|
let i = 0;
|
|
while (i < messages.length) {
|
|
const m = messages[i];
|
|
if (isAgentActivityMember(m)) {
|
|
const cluster: UIMessage[] = [];
|
|
while (i < messages.length && isAgentActivityMember(messages[i])) {
|
|
cluster.push(messages[i]);
|
|
i += 1;
|
|
}
|
|
out.push({ type: "cluster", messages: cluster });
|
|
continue;
|
|
}
|
|
out.push({ type: "single", message: m });
|
|
i += 1;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
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];
|
|
if (unit.type === "single" && unit.message.role === "user") {
|
|
hasLaterUnitBeforeUser = false;
|
|
continue;
|
|
}
|
|
if (unit.type === "single" && unit.message.role === "assistant") {
|
|
flags[i] = !hasLaterUnitBeforeUser;
|
|
}
|
|
hasLaterUnitBeforeUser = true;
|
|
}
|
|
return flags;
|
|
}
|
|
|
|
export function ThreadMessages({
|
|
messages,
|
|
isStreaming = false,
|
|
hiddenMessageCount = 0,
|
|
onLoadEarlier,
|
|
}: ThreadMessagesProps) {
|
|
const { t } = useTranslation();
|
|
const units = useMemo(() => buildDisplayUnits(messages), [messages]);
|
|
const copyFlags = useMemo(() => assistantCopyFlags(units), [units]);
|
|
|
|
return (
|
|
<div className="flex w-full flex-col">
|
|
{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}
|
|
{units.map((unit, index) => {
|
|
const prev = units[index - 1];
|
|
const marginTop =
|
|
index > 0
|
|
? marginAfterPrevUnit(prev)
|
|
: "";
|
|
const next = units[index + 1];
|
|
const hasBodyBelow =
|
|
unit.type === "cluster"
|
|
&& next?.type === "single"
|
|
&& next.message.role === "assistant";
|
|
|
|
return (
|
|
<div key={unitKey(unit, index)} className={marginTop}>
|
|
{unit.type === "cluster" ? (
|
|
<AgentActivityCluster
|
|
messages={unit.messages}
|
|
isTurnStreaming={isStreaming}
|
|
hasBodyBelow={hasBodyBelow}
|
|
/>
|
|
) : (
|
|
<MessageBubble
|
|
message={unit.message}
|
|
showAssistantCopyAction={
|
|
unit.message.role === "assistant"
|
|
? copyFlags[index]
|
|
: true
|
|
}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function unitKey(unit: DisplayUnit, index: number): string {
|
|
if (unit.type === "cluster") {
|
|
const anchor = unit.messages[0]?.id;
|
|
return anchor != null ? `cluster-${anchor}` : `cluster-idx-${index}`;
|
|
}
|
|
return unit.message.id;
|
|
}
|
|
|
|
function marginAfterPrevUnit(prev: DisplayUnit): string {
|
|
if (prev.type === "cluster") {
|
|
return "mt-4";
|
|
}
|
|
const p = prev.message;
|
|
const denseP =
|
|
p.kind === "trace"
|
|
|| (
|
|
p.role === "assistant"
|
|
&& p.content.trim().length === 0
|
|
&& (!!p.reasoning || !!p.reasoningStreaming)
|
|
);
|
|
if (denseP) {
|
|
return "mt-2";
|
|
}
|
|
return "mt-5";
|
|
}
|