| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- import { useVirtualizer } from "@tanstack/react-virtual";
- import { useEffect, type RefObject } from "react";
- import type { ZCodeModelTrajectoryRecord } from "@zcode/services";
- import {
- type TrajectorySearchMatch,
- type TrajectorySearchTimelineItem,
- } from "@/ModelTrajectorySearch.js";
- import {
- applyTrajectorySearchHighlights,
- clearTrajectorySearchHighlights,
- isTrajectorySearchTargetMounted,
- scrollTrajectorySearchRangeIntoView,
- } from "@/ModelTrajectorySearchHighlight.js";
- import { CallCard, type IntlShape } from "@/ModelTrajectoryPaneParts.js";
- export function ModelTrajectoryTimeline({
- items,
- searchQuery,
- searchMatches,
- activeSearchMatch,
- intl,
- scrollContainerRef,
- }: {
- items: TrajectorySearchTimelineItem[];
- searchQuery: string;
- searchMatches: TrajectorySearchMatch[];
- activeSearchMatch: TrajectorySearchMatch | null;
- intl: IntlShape;
- scrollContainerRef: RefObject<HTMLDivElement | null>;
- }) {
- const virtualizer = useVirtualizer({
- count: items.length,
- getScrollElement: () => scrollContainerRef.current,
- getItemKey: (index) => items[index]?.key ?? index,
- estimateSize: () => 240,
- overscan: 3,
- });
- // 搜索切换会同时收起旧命中并展开新命中;此时禁止 virtualizer 根据每次测高回调连续
- // 修正滚动锚点,由下方文本级定位在布局稳定后一次完成滚动。
- virtualizer.shouldAdjustScrollPositionOnItemSizeChange = () => !searchQuery;
- const roleWidthLabels = ["system", "user", "assistant", "tool"].map((role) =>
- intl.formatMessage({ id: `modelTrajectory.role.${role}` }),
- );
- const virtualItems = virtualizer.getVirtualItems();
- const firstVirtualItem = virtualItems[0];
- const lastVirtualItem = virtualItems.at(-1);
- const topSpacerHeight = firstVirtualItem?.start ?? 0;
- const bottomSpacerHeight = lastVirtualItem ? virtualizer.getTotalSize() - lastVirtualItem.end : 0;
- // 只跟踪挂载集合。把 start 放进依赖会让每次动态测高都重新安排一次精确滚动。
- const mountedRowsKey = virtualItems.map((item) => item.key).join("|");
- useEffect(() => () => clearTrajectorySearchHighlights(), []);
- useEffect(() => {
- const root = scrollContainerRef.current;
- if (
- !activeSearchMatch ||
- !root ||
- isTrajectorySearchTargetMounted(root, activeSearchMatch.expansionKey)
- ) {
- return;
- }
- // 目标已经挂载时跳过 Call 级预滚动,否则随后文本级定位会造成连续两次跳动。
- virtualizer.scrollToIndex(activeSearchMatch.callIndex, { align: "center" });
- }, [activeSearchMatch, scrollContainerRef, virtualizer]);
- useEffect(() => {
- const root = scrollContainerRef.current;
- if (!root || !searchQuery) {
- clearTrajectorySearchHighlights();
- return;
- }
- let secondFrame = 0;
- const firstFrame = window.requestAnimationFrame(() => {
- virtualizer.measure();
- secondFrame = window.requestAnimationFrame(() => {
- const activeRange = applyTrajectorySearchHighlights({
- root,
- query: searchQuery,
- matches: searchMatches,
- activeMatch: activeSearchMatch,
- });
- if (activeRange) scrollTrajectorySearchRangeIntoView(activeRange, root);
- });
- });
- return () => {
- window.cancelAnimationFrame(firstFrame);
- window.cancelAnimationFrame(secondFrame);
- };
- }, [
- activeSearchMatch,
- mountedRowsKey,
- scrollContainerRef,
- searchMatches,
- searchQuery,
- virtualizer,
- ]);
- return (
- <ol
- data-trajectory-timeline=""
- className="grid grid-cols-[max-content_minmax(0,1fr)_auto] gap-x-2"
- >
- {roleWidthLabels.map((label) => (
- <li
- key={label}
- aria-hidden="true"
- className="invisible col-start-1 row-start-1 h-0 overflow-hidden whitespace-nowrap font-mono text-ui-sm"
- >
- {label}
- </li>
- ))}
- {topSpacerHeight > 0 ? (
- <li aria-hidden="true" className="col-span-full" style={{ height: topSpacerHeight }} />
- ) : null}
- {virtualItems.map((virtualItem) => {
- const item = items[virtualItem.index];
- if (!item) return null;
- return (
- <li
- key={item.key}
- ref={virtualizer.measureElement}
- data-index={virtualItem.index}
- data-trajectory-virtual-row=""
- className="col-span-full grid w-full grid-cols-subgrid"
- >
- <CallCard
- record={item.record}
- index={virtualItem.index}
- inputMessages={item.inputMessages}
- expansionKeyPrefix={item.key}
- intl={intl}
- />
- </li>
- );
- })}
- {bottomSpacerHeight > 0 ? (
- <li aria-hidden="true" className="col-span-full" style={{ height: bottomSpacerHeight }} />
- ) : null}
- </ol>
- );
- }
- export function resolveTrajectoryTimelineItems(records: ZCodeModelTrajectoryRecord[]) {
- let previousConversationMessageCount = 0;
- return records.map((record, index) => {
- const resolvedInput = resolveTrajectoryInputMessages({
- index,
- previousConversationMessageCount,
- record,
- });
- previousConversationMessageCount = resolvedInput.nextConversationMessageCount;
- return {
- key: `${record.requestId}:${index}`,
- record,
- inputMessages: resolvedInput.inputMessages,
- };
- });
- }
- export function resolveTrajectoryInputMessages({
- record,
- index,
- previousConversationMessageCount,
- }: {
- record: ZCodeModelTrajectoryRecord;
- index: number;
- previousConversationMessageCount: number;
- }): {
- inputMessages: ZCodeModelTrajectoryRecord["request"]["messages"];
- nextConversationMessageCount: number;
- } {
- const messages = record.request.messages;
- const usesConversationDelta = shouldUseConversationDelta(record);
- const deltaMessages = usesConversationDelta
- ? computeDeltaMessages(messages, previousConversationMessageCount, index)
- : messages;
- return {
- // 首条展示完整起始上下文;后续主会话只展示非 assistant 的新增(assistant 由上一条 Output 呈现)。
- // sidecar/compact 等辅助请求有独立 prompt,不能套用主会话的消息数 delta,否则会隐藏标题生成 prompt。
- inputMessages: usesConversationDelta
- ? index === 0
- ? deltaMessages
- : deltaMessages.filter((message) => message.role !== "assistant")
- : deltaMessages,
- nextConversationMessageCount: usesConversationDelta
- ? messages.length
- : previousConversationMessageCount,
- };
- }
- function shouldUseConversationDelta(record: ZCodeModelTrajectoryRecord): boolean {
- const kind = record.callSource?.kind;
- return kind === undefined || kind === "main" || kind === "subagent";
- }
- function computeDeltaMessages(
- messages: ZCodeModelTrajectoryRecord["request"]["messages"],
- previousMessageCount: number,
- index: number,
- ): ZCodeModelTrajectoryRecord["request"]["messages"] {
- if (index === 0) return messages;
- if (messages.length > previousMessageCount) return messages.slice(previousMessageCount);
- if (messages.length < previousMessageCount) return messages;
- return [];
- }
|