queued-summary-streaming.fixture.tsx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { useSyncExternalStore } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import { QueuedSummaryContent } from "@/ToolCallBlocks/QueuedSummaryContent.js";
  4. import { ConsecutiveSummaryGroup } from "@/ToolCallBlocks/ToolSummaryRow.js";
  5. import { Reasoning, ReasoningTrigger } from "@/components/ai-elements/reasoning.js";
  6. import { SummaryGroupBoundaryContext } from "@/v4/conversationRowContext.js";
  7. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  8. import "@/styles.css";
  9. const params = new URLSearchParams(location.search);
  10. const theme = params.get("theme") === "dark" ? "dark" : "light";
  11. document.documentElement.className = `${theme} theme-zai-${theme}`;
  12. let snapshot = {
  13. key: "A",
  14. text: "A initial",
  15. secondary: "secondary 0",
  16. trailing: "+0",
  17. enabled: true,
  18. disabled: false,
  19. streaming: true,
  20. mounted: true,
  21. };
  22. const listeners = new Set<() => void>();
  23. const subscribe = (listener: () => void) => {
  24. listeners.add(listener);
  25. return () => listeners.delete(listener);
  26. };
  27. const getSnapshot = () => snapshot;
  28. const update = (patch: Partial<typeof snapshot>) => {
  29. snapshot = { ...snapshot, ...patch };
  30. for (const listener of listeners) listener();
  31. };
  32. // 同步订阅通知与真实投影 store 同形;microtask 连续交付固定触发旧实现的嵌套更新问题。
  33. Object.assign(window, {
  34. summaryFixture: {
  35. update,
  36. async burst() {
  37. for (let i = 1; i <= 200; i++) {
  38. update({
  39. text: `${"stream ".repeat(i)}A token ${i}`,
  40. secondary: `secondary ${i}`,
  41. trailing: `+${i}`,
  42. });
  43. await Promise.resolve();
  44. }
  45. },
  46. },
  47. });
  48. function Fixture() {
  49. const state = useSyncExternalStore(subscribe, getSnapshot);
  50. const thought = (
  51. <Reasoning isStreaming={state.streaming}>
  52. <ReasoningTrigger streamingText={state.text} />
  53. </Reasoning>
  54. );
  55. return (
  56. <main className="min-h-screen bg-background p-4 text-ui-base text-foreground">
  57. {state.mounted && (
  58. <section data-summary-fixture>
  59. {params.has("group") ? (
  60. <SummaryGroupBoundaryContext.Provider value={{ taskRunning: true, tailRowId: 2 }}>
  61. <ConsecutiveSummaryGroup
  62. summary={thought}
  63. contentKey={state.key}
  64. refreshVersion={state.text}
  65. statistics="Reasoning"
  66. lastRowId={2}
  67. hasActiveItem={state.streaming}
  68. >
  69. {thought}
  70. </ConsecutiveSummaryGroup>
  71. </SummaryGroupBoundaryContext.Provider>
  72. ) : params.has("reasoning") ? (
  73. thought
  74. ) : (
  75. <QueuedSummaryContent
  76. contentKey={state.key}
  77. contentRefreshVersion={state.text}
  78. primaryText={<span data-primary>{state.text}</span>}
  79. secondaryText={<span data-secondary>{state.secondary}</span>}
  80. trailingText={<span data-trailing>{state.trailing}</span>}
  81. enabled={state.enabled}
  82. disableAnimation={state.disabled}
  83. />
  84. )}
  85. </section>
  86. )}
  87. </main>
  88. );
  89. }
  90. createRoot(document.getElementById("root")!).render(
  91. <ZCodeIntlProvider initialLocale={params.get("locale") === "zh-CN" ? "zh-CN" : "en-US"}>
  92. <Fixture />
  93. </ZCodeIntlProvider>,
  94. );