streaming-render-debug.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Debug script to reproduce streaming rendering issues.
  3. * Uses real fixture data that caused the bug.
  4. * Run with: npx tsx test/streaming-render-debug.ts
  5. */
  6. import type { AssistantMessage } from "@earendil-works/pi-ai";
  7. import { ProcessTerminal, TUI } from "@earendil-works/pi-tui";
  8. import { readFileSync } from "fs";
  9. import { dirname, join } from "path";
  10. import { fileURLToPath } from "url";
  11. import { AssistantMessageComponent } from "../src/modes/interactive/components/assistant-message.ts";
  12. import { initTheme } from "../src/modes/interactive/theme/theme.ts";
  13. const __filename = fileURLToPath(import.meta.url);
  14. const __dirname = dirname(__filename);
  15. // Initialize dark theme with full color support
  16. process.env.COLORTERM = "truecolor";
  17. initTheme("dark");
  18. // Load the real fixture that caused the bug
  19. const fixtureMessage: AssistantMessage = JSON.parse(
  20. readFileSync(join(__dirname, "fixtures/assistant-message-with-thinking-code.json"), "utf-8"),
  21. );
  22. // Extract thinking and text content
  23. const thinkingContent = fixtureMessage.content.find((c) => c.type === "thinking");
  24. const textContent = fixtureMessage.content.find((c) => c.type === "text");
  25. if (!thinkingContent || thinkingContent.type !== "thinking") {
  26. console.error("No thinking content in fixture");
  27. process.exit(1);
  28. }
  29. const fullThinkingText = thinkingContent.thinking;
  30. const fullTextContent = textContent && textContent.type === "text" ? textContent.text : "";
  31. async function sleep(ms: number): Promise<void> {
  32. return new Promise((resolve) => setTimeout(resolve, ms));
  33. }
  34. async function main() {
  35. const terminal = new ProcessTerminal();
  36. const tui = new TUI(terminal);
  37. // Start with empty message
  38. const message = {
  39. role: "assistant",
  40. content: [{ type: "thinking", thinking: "" }],
  41. } as AssistantMessage;
  42. const component = new AssistantMessageComponent(message, false);
  43. tui.addChild(component);
  44. tui.start();
  45. // Simulate streaming thinking content
  46. let thinkingBuffer = "";
  47. const chunkSize = 10; // characters per "token"
  48. for (let i = 0; i < fullThinkingText.length; i += chunkSize) {
  49. thinkingBuffer += fullThinkingText.slice(i, i + chunkSize);
  50. // Update message content
  51. const updatedMessage = {
  52. role: "assistant",
  53. content: [{ type: "thinking", thinking: thinkingBuffer }],
  54. } as AssistantMessage;
  55. component.updateContent(updatedMessage);
  56. tui.requestRender();
  57. await sleep(15); // Simulate token delay
  58. }
  59. // Now add the text content
  60. await sleep(500);
  61. const finalMessage = {
  62. role: "assistant",
  63. content: [
  64. { type: "thinking", thinking: fullThinkingText },
  65. { type: "text", text: fullTextContent },
  66. ],
  67. } as AssistantMessage;
  68. component.updateContent(finalMessage);
  69. tui.requestRender();
  70. // Keep alive for a moment to see the result
  71. await sleep(3000);
  72. tui.stop();
  73. process.exit(0);
  74. }
  75. main().catch(console.error);