conversation-timeline-resize.fixture.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. import { MarkdownTable } from "@/components/ai-elements/markdown-table.js";
  2. import { StrictMode, useRef, useState } from "react";
  3. import { createRoot } from "react-dom/client";
  4. import type { ConversationRow, UserInputRow } from "@zcode/shared/zcode-protocol-v4";
  5. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  6. import { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js";
  7. import { ConversationTimeline } from "@/v4/ConversationTimeline.js";
  8. import { TooltipProvider } from "@/components/ui/tooltip.js";
  9. import "@/styles.css";
  10. const query = new URLSearchParams(location.search);
  11. const theme = query.get("theme") === "light" ? "light" : "dark";
  12. document.documentElement.className = `${theme} theme-zai-${theme}`;
  13. const userRows: UserInputRow[] = Array.from({ length: 35 }, (_, index) => ({
  14. rowId: query.get("heavy") === "true" ? index * 200 + 1 : index + 1,
  15. turnId: `turn-${index + 1}`,
  16. createdAt: 1_700_000_000_000 + index,
  17. createdAtSeq: index + 1,
  18. kind: "userInput",
  19. origin: "realUser",
  20. text: `E2E_SRM10_${index + 1} ${"宽度变化时文字会重新换行,阅读历史不应被测高夺回滚动权。 Resize must preserve user scroll ownership. ".repeat(2)}`,
  21. attachments: [],
  22. }));
  23. // 侧栏 resize 的慢 trace 包含后台结果轮;该轮直接展示工具摘要,不能只用文本行回归。
  24. const initialRows: ConversationRow[] = userRows.flatMap((row, index): ConversationRow[] => {
  25. if (query.get("heavy") !== "true" || index !== 20) return [row];
  26. return [
  27. {
  28. rowId: row.rowId,
  29. turnId: row.turnId,
  30. createdAt: row.createdAt,
  31. createdAtSeq: row.createdAtSeq,
  32. kind: "turnHeader",
  33. origin: "backgroundResult",
  34. state: "completedSuccess",
  35. startedAt: row.createdAt,
  36. endedAt: row.createdAt + 1000,
  37. },
  38. ...Array.from(
  39. { length: 134 },
  40. (_, toolIndex): ConversationRow => ({
  41. rowId: row.rowId + toolIndex + 1,
  42. turnId: row.turnId,
  43. createdAt: row.createdAt,
  44. createdAtSeq: row.createdAtSeq,
  45. kind: "toolCall",
  46. toolCallId: `resize-tool-${toolIndex}`,
  47. toolName: "FixtureTool",
  48. status: "success",
  49. inputText: '{"query":"fixture"}',
  50. input: { query: "fixture" },
  51. }),
  52. ),
  53. ];
  54. });
  55. const navigatorRows: ConversationRow[] = userRows.slice(0, 3).flatMap((row, index) => [
  56. { ...row, rowId: index * 3 + 1, text: `Navigation turn ${index + 1}` },
  57. {
  58. ...row,
  59. rowId: index * 3 + 2,
  60. kind: "assistantText" as const,
  61. state: "complete" as const,
  62. text: Array.from(
  63. { length: 32 },
  64. (_, line) =>
  65. `[Link ${index}-${line}](https://example.com/${index}/${line}) selectable navigation regression text`,
  66. ).join("\n\n"),
  67. },
  68. ]);
  69. const hydrationRows: ConversationRow[] = [
  70. {
  71. rowId: 1,
  72. turnId: "hydrate-leading",
  73. createdAt: 1,
  74. createdAtSeq: 1,
  75. kind: "turnHeader",
  76. origin: "backgroundResult",
  77. state: "completedSuccess",
  78. startedAt: 1,
  79. endedAt: 1000,
  80. },
  81. ...Array.from(
  82. { length: 134 },
  83. (_, index): ConversationRow => ({
  84. rowId: index + 2,
  85. turnId: "hydrate-leading",
  86. createdAt: index + 2,
  87. createdAtSeq: index + 2,
  88. kind: "toolCall",
  89. toolCallId: `hydrate-tool-${index}`,
  90. toolName: "FixtureTool",
  91. status: "success",
  92. inputText: '{"query":"fixture"}',
  93. input: { query: "fixture" },
  94. }),
  95. ),
  96. ...userRows.slice(0, 5).map((row, index) => ({ ...row, rowId: 200 + index })),
  97. ];
  98. const identityMode = query.get("identity");
  99. const boundaryMode = query.get("boundary");
  100. const terminalMode = query.get("terminal");
  101. const terminalStartedAt = Date.now() - 5000;
  102. function terminalRows(stopped: boolean, nextTurn: boolean): ConversationRow[] {
  103. const row = (rowId: number, turnId: string) => ({
  104. rowId,
  105. turnId,
  106. createdAt: terminalStartedAt + rowId,
  107. createdAtSeq: rowId,
  108. });
  109. return [
  110. {
  111. ...row(1, "prior-turn"),
  112. kind: "turnHeader",
  113. origin: "userInput",
  114. state: "completedSuccess",
  115. startedAt: terminalStartedAt - 1000,
  116. endedAt: terminalStartedAt,
  117. },
  118. {
  119. ...row(2, "prior-turn"),
  120. kind: "userInput",
  121. origin: "realUser",
  122. text: "E2E_SSV06_PRIOR_INPUT",
  123. },
  124. {
  125. ...row(3, "prior-turn"),
  126. kind: "assistantText",
  127. state: "complete",
  128. text: "E2E_SSV06_PRIOR_REPLY",
  129. },
  130. {
  131. ...row(4, "terminal-turn"),
  132. kind: "turnHeader",
  133. origin: "userInput",
  134. state: !stopped
  135. ? "running"
  136. : terminalMode === "complete"
  137. ? "completedSuccess"
  138. : terminalMode === "fail"
  139. ? "failed"
  140. : "completedInterrupted",
  141. startedAt: terminalStartedAt,
  142. ...(stopped ? { endedAt: terminalStartedAt + 5000, activeMs: 5000 } : {}),
  143. },
  144. {
  145. ...row(5, "terminal-turn"),
  146. kind: "userInput",
  147. origin: "realUser",
  148. text: "E2E_SSV06_CURRENT_INPUT",
  149. },
  150. {
  151. ...row(6, "terminal-turn"),
  152. kind: "assistantText",
  153. state: "complete",
  154. assistantResponseId: "history-response",
  155. text: `E2E_SSV06_HISTORY ${"停止前已显示的过程内容应保留,不应重新淡入。 Existing content keeps its identity. ".repeat(40)}`,
  156. },
  157. {
  158. ...row(7, "terminal-turn"),
  159. kind: "toolCall",
  160. toolCallId: "terminal-tool",
  161. toolName: "FixtureTool",
  162. status: "success",
  163. input: { query: "E2E_SSV06_TOOL" },
  164. inputText: '{"query":"E2E_SSV06_TOOL"}',
  165. },
  166. {
  167. ...row(8, "terminal-turn"),
  168. kind: "assistantText",
  169. state: !stopped ? "streaming" : terminalMode === "complete" ? "complete" : "interrupted",
  170. assistantResponseId: "tail-response",
  171. // 正常完成会折叠过程区;最终正文仍需超过视口,才能验证离底阅读的滚动归属。
  172. text: `E2E_SSV06_PARTIAL ${"最终正文保持可见,已有内容不应被重新挂载。 Terminal text remains visible. ".repeat(50)}`,
  173. },
  174. ...(nextTurn
  175. ? [
  176. {
  177. ...row(9, "next-turn"),
  178. kind: "turnHeader" as const,
  179. origin: "userInput" as const,
  180. state: "running" as const,
  181. startedAt: terminalStartedAt + 6000,
  182. },
  183. {
  184. ...row(10, "next-turn"),
  185. kind: "userInput" as const,
  186. origin: "realUser" as const,
  187. text: "E2E_SSV06_NEXT_INPUT",
  188. },
  189. {
  190. ...row(11, "next-turn"),
  191. kind: "assistantText" as const,
  192. state: "streaming" as const,
  193. text: "E2E_SSV06_NEXT_REPLY",
  194. },
  195. ]
  196. : []),
  197. ];
  198. }
  199. const boundaryPages =
  200. boundaryMode === "mixed"
  201. ? [
  202. [40, 41, 50, 51, 60, 61],
  203. [10, 11, 20, 39, 40, 41, 50, 51, 60, 61],
  204. [10, 11, 20, 39, 40, 41, 50, 51, 52, 60, 61, 62, 70, 80, 81],
  205. ]
  206. : [
  207. [50, 51],
  208. [50, 51, 60, 61],
  209. [10, 11, 20, 49, 50, 51, 60, 61, 62, 70, 80, 81],
  210. ];
  211. const boundaryRows = (page: number): ConversationRow[] => [
  212. {
  213. rowId: 1,
  214. turnId: "boundary-turn",
  215. createdAt: 1,
  216. createdAtSeq: 1,
  217. kind: "turnHeader",
  218. origin: "userInput",
  219. state: "failed",
  220. startedAt: 1,
  221. },
  222. ...boundaryPages[page]!.map(
  223. (rowId): ConversationRow => ({
  224. rowId,
  225. turnId: "boundary-turn",
  226. createdAt: rowId,
  227. createdAtSeq: rowId,
  228. kind: "toolCall",
  229. toolCallId: `boundary-tool-${rowId}`,
  230. toolName: [10, 11, 39, 40, 41, 60, 61, 62, 80, 81].includes(rowId)
  231. ? "mcp__computer-use__screenshot"
  232. : "FixtureTool",
  233. assistantResponseId: "boundary-shared-response",
  234. status: "success",
  235. input: { query: `E2E_SSV05_BOUNDARY_${rowId}` },
  236. inputText: JSON.stringify({ query: `E2E_SSV05_BOUNDARY_${rowId}` }),
  237. }),
  238. ),
  239. ];
  240. const identityRows: ConversationRow[] = [
  241. {
  242. rowId: 1,
  243. turnId: "identity-turn",
  244. createdAt: 1,
  245. createdAtSeq: 1,
  246. kind: "turnHeader",
  247. origin: "userInput",
  248. state: identityMode === "stream" ? "running" : "failed",
  249. startedAt: 1,
  250. },
  251. ...Array.from(
  252. { length: 240 },
  253. (_, index): ConversationRow => ({
  254. rowId: index + 2,
  255. turnId: "identity-turn",
  256. createdAt: index + 2,
  257. createdAtSeq: index + 2,
  258. kind: "toolCall",
  259. toolCallId: `identity-tool-${index}`,
  260. toolName:
  261. identityMode === "cua" && index === 160 ? "mcp__computer-use__screenshot" : "FixtureTool",
  262. assistantResponseId: `identity-response-${index}`,
  263. status: "success",
  264. input: { query: `E2E_SSV05_${index}` },
  265. inputText: JSON.stringify({ query: `E2E_SSV05_${index}` }),
  266. }),
  267. ),
  268. ...(identityMode === "final" || identityMode === "cua"
  269. ? [
  270. {
  271. rowId: 242,
  272. turnId: "identity-turn",
  273. createdAt: 242,
  274. createdAtSeq: 242,
  275. kind: "assistantText" as const,
  276. text: "E2E_SSV05_FINAL",
  277. state: "complete" as const,
  278. },
  279. ]
  280. : []),
  281. ];
  282. function Fixture() {
  283. const [stopped, setStopped] = useState(false);
  284. const [nextTurn, setNextTurn] = useState(false);
  285. const [boundaryPage, setBoundaryPage] = useState(0);
  286. const [session, setSession] = useState("a");
  287. const [draft, setDraft] = useState(false);
  288. const [rows, setRows] = useState(
  289. identityMode
  290. ? identityMode === "stream"
  291. ? identityRows.slice(0, 60)
  292. : identityRows.slice(-60)
  293. : query.get("hydrate") === "true"
  294. ? hydrationRows.slice(-60)
  295. : initialRows,
  296. );
  297. const back = useRef<(() => void) | null>(null);
  298. const locate = useRef<((target: { unitIndex: number; rowId: number }) => void) | null>(null);
  299. const displayedRows = query.has("navigatorHit")
  300. ? navigatorRows
  301. : draft
  302. ? []
  303. : terminalMode
  304. ? terminalRows(stopped, nextTurn)
  305. : boundaryMode
  306. ? boundaryRows(boundaryPage)
  307. : rows;
  308. return (
  309. <TooltipProvider>
  310. <ZCodeIntlProvider initialLocale={query.get("mobile") === "true" ? "en-US" : "zh-CN"}>
  311. <div className="flex h-dvh min-w-0 flex-col bg-background text-foreground">
  312. <nav className="flex shrink-0 gap-2 text-ui-caption">
  313. {terminalMode && (
  314. <>
  315. <button onClick={() => setStopped(true)}>Stop</button>
  316. <button onClick={() => setNextTurn(true)}>Next turn</button>
  317. </>
  318. )}
  319. {boundaryMode && (
  320. <button onClick={() => setBoundaryPage((page) => page + 1)}>Next boundary</button>
  321. )}
  322. <output data-testid="boundary-page">{boundaryPage}</output>
  323. <button onClick={() => setDraft((value) => !value)}>Draft</button>
  324. <button onClick={() => setSession((value) => (value === "a" ? "b" : "a"))}>
  325. Switch
  326. </button>
  327. <button onClick={() => back.current?.()}>Bottom</button>
  328. <button onClick={() => locate.current?.({ unitIndex: 0, rowId: 1 })}>Locate</button>
  329. {query.get("heavy") === "true" && (
  330. <button onClick={() => locate.current?.({ unitIndex: 19, rowId: 3801 })}>
  331. Tools
  332. </button>
  333. )}
  334. <button
  335. onClick={() =>
  336. setRows((value) =>
  337. value.map((row, index) =>
  338. index === value.length - 1 && row.kind === "userInput"
  339. ? {
  340. ...row,
  341. text: row.text + " 新增内容 asynchronous content growth. ".repeat(20),
  342. }
  343. : row,
  344. ),
  345. )
  346. }
  347. >
  348. Grow
  349. </button>
  350. <button onClick={() => setRows(hydrationRows)}>Hydrate</button>
  351. <button
  352. onClick={() =>
  353. setRows((current) =>
  354. identityMode === "stream"
  355. ? identityRows.slice(0, current.length + 60)
  356. : identityRows.slice(-current.length - 60),
  357. )
  358. }
  359. >
  360. Next page
  361. </button>
  362. <output data-testid="scope">{session}</output>
  363. </nav>
  364. <div
  365. className={`flex min-h-0 min-w-0 flex-1 ${query.has("draft") || query.has("navigatorHit") ? "@container/conversation" : ""}`}
  366. >
  367. <ConversationTimeline
  368. rows={displayedRows}
  369. sessionPhase={
  370. terminalMode
  371. ? !stopped || nextTurn
  372. ? "running"
  373. : terminalMode === "complete"
  374. ? "completedSuccess"
  375. : terminalMode === "fail"
  376. ? "error"
  377. : "completedInterrupted"
  378. : boundaryMode
  379. ? "error"
  380. : identityMode
  381. ? identityMode === "stream"
  382. ? "running"
  383. : "error"
  384. : undefined
  385. }
  386. totalCount={displayedRows.length}
  387. centerEmptyStateWithDock={draft}
  388. compactEmptyStateWithDock={query.get("mobile") === "true"}
  389. sessionKey={session}
  390. scrollMemoryKey={`fixture::main::${session}`}
  391. compactForRemoteControl={query.get("mobile") === "true"}
  392. scrollToBottomActionRef={back}
  393. scrollToQueryActionRef={locate}
  394. rowContext={{
  395. codePreviewSettings: DEFAULT_CODE_PREVIEW_SETTINGS,
  396. sessionId: session,
  397. theme,
  398. workspacePath: "/fixture",
  399. onOpenBrowserUrl: query.has("navigatorHit") ? () => {} : undefined,
  400. }}
  401. bottomDock={
  402. <div
  403. className={`h-16 w-full bg-background ${draft ? "max-w-2xl" : ""}`}
  404. data-testid="fixture-composer"
  405. >
  406. Composer
  407. </div>
  408. }
  409. />
  410. </div>
  411. </div>
  412. </ZCodeIntlProvider>
  413. </TooltipProvider>
  414. );
  415. }
  416. function TableMotionFixture() {
  417. return (
  418. <TooltipProvider>
  419. <ZCodeIntlProvider initialLocale="en-US">
  420. <main
  421. className="min-h-dvh bg-background text-foreground text-ui-base"
  422. data-markdown-table-layout-root="true"
  423. data-testid="table-layout-root"
  424. style={{ width: "100%", transition: "width 200ms ease-out" }}
  425. >
  426. <section style={{ width: "65%", marginInline: "auto" }}>
  427. {[0, 1].map((index) => (
  428. <MarkdownTable key={index}>
  429. <thead>
  430. <tr>
  431. {[0, 1, 2, 3].map((column) => (
  432. <th key={column}>Column {column}</th>
  433. ))}
  434. </tr>
  435. </thead>
  436. <tbody>
  437. {Array.from({ length: 6 }, (_, row) => (
  438. <tr key={row}>
  439. {[0, 1, 2, 3].map((column) => (
  440. <td key={column}>
  441. Table {index} row {row} column {column}: a wide value for horizontal
  442. scrolling
  443. </td>
  444. ))}
  445. </tr>
  446. ))}
  447. </tbody>
  448. </MarkdownTable>
  449. ))}
  450. </section>
  451. </main>
  452. </ZCodeIntlProvider>
  453. </TooltipProvider>
  454. );
  455. }
  456. createRoot(document.getElementById("root")!).render(
  457. query.has("table-motion") ? (
  458. <TableMotionFixture />
  459. ) : boundaryMode ? (
  460. <StrictMode>
  461. <Fixture />
  462. </StrictMode>
  463. ) : (
  464. <Fixture />
  465. ),
  466. );