reasoning-render-performance.fixture.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { useMemo, useState } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import type { ConversationRow } from "@zcode/shared/zcode-protocol-v4";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  5. import { TooltipProvider } from "@/components/ui/tooltip.js";
  6. import { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js";
  7. import { ConversationTimeline } from "@/v4/ConversationTimeline.js";
  8. import "@/styles.css";
  9. const query = new URLSearchParams(location.search);
  10. const theme = query.get("theme") === "dark" ? "dark" : "light";
  11. const mobile = query.get("mobile") === "true";
  12. document.documentElement.className = `${theme} theme-zai-${theme}`;
  13. const count = Number(query.get("groups") ?? 40);
  14. let nextRowId = 1;
  15. const base = () => {
  16. const rowId = nextRowId++;
  17. return { rowId, turnId: "perf-turn", createdAt: rowId, createdAtSeq: rowId };
  18. };
  19. const initial: ConversationRow[] = [
  20. { ...base(), kind: "turnHeader", origin: "backgroundResult", state: "running", startedAt: 1 },
  21. ...Array.from({ length: count }, (_, index): ConversationRow[] => [
  22. ...Array.from({ length: index === 0 ? 120 : 3 }, (_, child): ConversationRow => {
  23. const input = {
  24. file_path: `/workspace/group-${index}/file-${child}.ts`,
  25. content: "export const value = 1;",
  26. };
  27. return {
  28. ...base(),
  29. kind: "toolCall",
  30. toolCallId: `perf-${index}-${child}`,
  31. toolName: "Write",
  32. status: "success",
  33. input,
  34. inputText: JSON.stringify(input),
  35. };
  36. }),
  37. {
  38. ...base(),
  39. kind: "reasoning",
  40. state: "complete",
  41. text: `E2E_XSG04_THOUGHT_${index}\n${"思考详情保持完整。 Thinking detail stays intact. ".repeat(80)}`,
  42. },
  43. {
  44. ...base(),
  45. kind: "toolCall",
  46. toolCallId: `boundary-${index}`,
  47. toolName: "FixtureTool",
  48. status: "success",
  49. input: { query: "boundary" },
  50. inputText: '{"query":"boundary"}',
  51. },
  52. ]).flat(),
  53. { ...base(), kind: "reasoning", state: "streaming", text: "E2E_XSG04_LIVE" },
  54. ];
  55. function Fixture() {
  56. const [rows, setRows] = useState(initial);
  57. const [tick, setTick] = useState(0);
  58. const context = useMemo(
  59. () => ({
  60. codePreviewSettings: DEFAULT_CODE_PREVIEW_SETTINGS,
  61. sessionId: "perf-session",
  62. theme,
  63. workspacePath: "/workspace",
  64. messageStreamShowReasoning: true,
  65. }),
  66. [],
  67. );
  68. // 只推进确定性输入;渲染、分组、测量和折叠均执行真实产品组件。
  69. Object.assign(window, {
  70. __reasoningPerf: {
  71. revise: () =>
  72. setRows((current) =>
  73. current.map((row) =>
  74. row.kind === "toolCall" && row.toolCallId === "perf-0-1"
  75. ? {
  76. ...row,
  77. toolName: "Bash",
  78. input: { command: "echo E2E_XSG04_UPDATED" },
  79. inputText: '{"command":"echo E2E_XSG04_UPDATED"}',
  80. }
  81. : row,
  82. ),
  83. ),
  84. step: () => {
  85. setRows((current) =>
  86. current.map((row, index) =>
  87. index === current.length - 1 && row.kind === "reasoning"
  88. ? { ...row, text: row.text + " next" }
  89. : row,
  90. ),
  91. );
  92. setTick((value) => value + 1);
  93. },
  94. },
  95. });
  96. return (
  97. <TooltipProvider>
  98. <ZCodeIntlProvider initialLocale={mobile ? "en-US" : "zh-CN"}>
  99. <div className="flex h-dvh flex-col bg-background text-foreground">
  100. <output data-testid="tick">{tick}</output>
  101. <ConversationTimeline
  102. rows={rows}
  103. totalCount={rows.length}
  104. sessionPhase="running"
  105. sessionKey="perf-session"
  106. scrollMemoryKey="perf-session"
  107. compactForRemoteControl={mobile}
  108. rowContext={context}
  109. />
  110. </div>
  111. </ZCodeIntlProvider>
  112. </TooltipProvider>
  113. );
  114. }
  115. createRoot(document.getElementById("root")!).render(<Fixture />);