conversation-navigator-performance.fixture.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { useCallback, useRef, useState } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import type { ConversationRow } from "@zcode/shared/zcode-protocol-v4";
  4. import { ConversationTimeline } from "@/v4/ConversationTimeline.js";
  5. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  6. import { TooltipProvider } from "@/components/ui/tooltip.js";
  7. import { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js";
  8. import "@/styles.css";
  9. const params = new URLSearchParams(location.search);
  10. const theme = params.get("theme") === "light" ? "light" : "dark";
  11. document.documentElement.className = `${theme} theme-zai-${theme}`;
  12. const paragraph = "Deterministic long conversation. 保留完整的问题目录和正文阅读位置。 ".repeat(6);
  13. const initial: ConversationRow[] = Array.from({ length: 80 }, (_, index) => {
  14. const base = { turnId: `perf-${index}`, createdAt: 1, createdAtSeq: index * 10 + 1 };
  15. const rows: ConversationRow[] = [
  16. {
  17. ...base,
  18. rowId: index * 10 + 1,
  19. kind: "turnHeader",
  20. origin: "userInput",
  21. state: "completedSuccess",
  22. startedAt: 1,
  23. endedAt: 2,
  24. },
  25. {
  26. ...base,
  27. rowId: index * 10 + 2,
  28. kind: "userInput",
  29. origin: "realUser",
  30. text: `E2E_TN10 query ${index}`,
  31. attachments: [],
  32. },
  33. {
  34. ...base,
  35. rowId: index * 10 + 3,
  36. kind: "assistantText",
  37. state: "complete",
  38. text: `Answer ${index}\n\n${Array(8).fill(paragraph).join("\n\n")}`,
  39. },
  40. ];
  41. if (index === 20) {
  42. rows.push(
  43. { ...base, rowId: 204, kind: "userInput", origin: "realUser", text: "E2E_TN10 guide one" },
  44. { ...base, rowId: 205, kind: "assistantText", state: "complete", text: paragraph },
  45. { ...base, rowId: 206, kind: "userInput", origin: "realUser", text: "E2E_TN10 guide two" },
  46. { ...base, rowId: 207, kind: "assistantText", state: "complete", text: paragraph },
  47. );
  48. }
  49. return rows;
  50. }).flat();
  51. function Fixture() {
  52. const [rows, setRows] = useState(params.has("entrance") ? initial.slice(-6) : initial);
  53. const [hidden, setHidden] = useState(false);
  54. const [query, setQuery] = useState("");
  55. const find = useRef({ calls: 0, matchCount: 0 });
  56. const onFind = useCallback((state: { matchCount: number }) => {
  57. find.current = { calls: find.current.calls + 1, matchCount: state.matchCount };
  58. }, []);
  59. const locate = useRef<((target: { unitIndex: number; rowId: number }) => void) | null>(null);
  60. const bottom = useRef<(() => void) | null>(null);
  61. const context = useRef({
  62. codePreviewSettings: DEFAULT_CODE_PREVIEW_SETTINGS,
  63. sessionId: "tn10",
  64. theme,
  65. workspacePath: "/fixture",
  66. }).current;
  67. Object.assign(window, {
  68. __navigatorPerf: {
  69. hydrate: () => setRows(initial),
  70. stream: () =>
  71. setRows((value) =>
  72. value.map((row) =>
  73. row.rowId === 793 && row.kind === "assistantText"
  74. ? { ...row, text: `${row.text} Streaming update.` }
  75. : row,
  76. ),
  77. ),
  78. hide: setHidden,
  79. search: setQuery,
  80. find: () => find.current,
  81. jump: (rowId: number) => locate.current?.({ unitIndex: Math.floor(rowId / 10), rowId }),
  82. bottom: () => bottom.current?.(),
  83. revise: () =>
  84. setRows((value) =>
  85. value.map((row) =>
  86. row.rowId === 203 && row.kind === "assistantText"
  87. ? { ...row, text: `Revised answer\n\n${row.text}` }
  88. : row,
  89. ),
  90. ),
  91. grow: () =>
  92. setRows((value) =>
  93. value.map((row) =>
  94. row.rowId === 203 && row.kind === "assistantText"
  95. ? { ...row, text: `${row.text}\n\n${paragraph}` }
  96. : row,
  97. ),
  98. ),
  99. },
  100. });
  101. return (
  102. <TooltipProvider>
  103. <ZCodeIntlProvider initialLocale={params.get("mobile") === "true" ? "en-US" : "zh-CN"}>
  104. <div className="@container/conversation flex h-dvh min-w-0 flex-col bg-background text-foreground">
  105. <ConversationTimeline
  106. rows={rows}
  107. totalCount={rows.length}
  108. sessionKey="tn10"
  109. rowContext={context}
  110. hideTurnNavigator={hidden}
  111. conversationFindQuery={query}
  112. onConversationFindMatchStateChange={onFind}
  113. scrollToQueryActionRef={locate}
  114. scrollToBottomActionRef={bottom}
  115. bottomDock={
  116. <div className="h-16 bg-background" data-testid="perf-composer">
  117. Composer
  118. </div>
  119. }
  120. />
  121. </div>
  122. </ZCodeIntlProvider>
  123. </TooltipProvider>
  124. );
  125. }
  126. createRoot(document.getElementById("root")!).render(<Fixture />);