streamingToolInputPreview.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. appendZCodeStreamingToolInputDelta,
  4. buildZCodeStreamingToolInputPreview,
  5. markZCodeStreamingToolInputPreviewMaterialized,
  6. shouldMaterializeZCodeStreamingToolInputPreview,
  7. } from "../src/streaming-tool-input-preview.js";
  8. describe("streaming tool input preview", () => {
  9. it("extracts file path and growing content from partial JSON", () => {
  10. const first = appendZCodeStreamingToolInputDelta(
  11. undefined,
  12. '{"file_path":"src/app.ts","content":"line 1\\n',
  13. );
  14. const second = appendZCodeStreamingToolInputDelta(first, "line 2");
  15. const preview = buildZCodeStreamingToolInputPreview(second.rawInput);
  16. expect(first.deltaCount).toBe(1);
  17. expect(second.deltaCount).toBe(2);
  18. expect(preview.complete).toBe(false);
  19. expect(preview.input).toMatchObject({
  20. file_path: "src/app.ts",
  21. content: "line 1\nline 2",
  22. });
  23. });
  24. it("extracts a growing ExitPlanMode plan from partial JSON", () => {
  25. const preview = buildZCodeStreamingToolInputPreview(
  26. '{"plan":"# 实施计划\\n\\n- 第一步\\n- 第二',
  27. );
  28. expect(preview.complete).toBe(false);
  29. expect(preview.input).toEqual({
  30. plan: "# 实施计划\n\n- 第一步\n- 第二",
  31. });
  32. });
  33. it("prefers complete tool call input when available", () => {
  34. const preview = buildZCodeStreamingToolInputPreview('{"file_path":"old.ts"', {
  35. file_path: "src/final.ts",
  36. content: "done",
  37. });
  38. expect(preview.complete).toBe(true);
  39. expect(preview.input).toEqual({
  40. file_path: "src/final.ts",
  41. content: "done",
  42. });
  43. });
  44. it("extracts a user-facing title from partial tool input", () => {
  45. const preview = buildZCodeStreamingToolInputPreview(
  46. '{"code":"await openPage();","title":"打开目标页面',
  47. );
  48. expect(preview.complete).toBe(false);
  49. expect(preview.input).toMatchObject({ title: "打开目标页面" });
  50. });
  51. it("extracts a CreateWorkflow name and growing script from partial JSON", () => {
  52. // 工作流卡的流式草稿靠半截 script 扫站;name 先到就先显示名字而不是兜底词。
  53. const preview = buildZCodeStreamingToolInputPreview(
  54. '{"name":"Research pipeline","script":"phase(\\"plan\\");\\nconst p = agent(\\"pl',
  55. );
  56. expect(preview.complete).toBe(false);
  57. expect(preview.input).toMatchObject({
  58. name: "Research pipeline",
  59. script: 'phase("plan");\nconst p = agent("pl',
  60. });
  61. });
  62. it("budgets active preview materialization and keeps background as tombstone", () => {
  63. const first = appendZCodeStreamingToolInputDelta(undefined, '{"content":"a');
  64. expect(shouldMaterializeZCodeStreamingToolInputPreview(first)).toBe(true);
  65. markZCodeStreamingToolInputPreviewMaterialized(first, 1_000);
  66. const second = appendZCodeStreamingToolInputDelta(first, "b");
  67. expect(
  68. shouldMaterializeZCodeStreamingToolInputPreview(second, {
  69. now: 1_100,
  70. }),
  71. ).toBe(false);
  72. expect(
  73. shouldMaterializeZCodeStreamingToolInputPreview(second, {
  74. now: 2_000,
  75. }),
  76. ).toBe(true);
  77. expect(
  78. shouldMaterializeZCodeStreamingToolInputPreview(second, {
  79. mode: "background-summary",
  80. now: 2_000,
  81. }),
  82. ).toBe(false);
  83. });
  84. it.each(["Write", "Edit"])(
  85. "%s previews the first delta eagerly and subsequent deltas once per second",
  86. (toolName) => {
  87. const first = appendZCodeStreamingToolInputDelta(undefined, '{"content":"a');
  88. expect(shouldMaterializeZCodeStreamingToolInputPreview(first, { now: 1_000, toolName })).toBe(
  89. true,
  90. );
  91. markZCodeStreamingToolInputPreviewMaterialized(first, 1_000);
  92. const second = appendZCodeStreamingToolInputDelta(first, "b".repeat(9 * 1024));
  93. expect(
  94. shouldMaterializeZCodeStreamingToolInputPreview(second, { now: 1_999, toolName }),
  95. ).toBe(false);
  96. expect(
  97. shouldMaterializeZCodeStreamingToolInputPreview(second, { now: 2_000, toolName }),
  98. ).toBe(true);
  99. },
  100. );
  101. it("keeps the raw-growth shortcut for non-file tools", () => {
  102. const first = appendZCodeStreamingToolInputDelta(undefined, '{"command":"p');
  103. markZCodeStreamingToolInputPreviewMaterialized(first, 1_000);
  104. const second = appendZCodeStreamingToolInputDelta(first, "x".repeat(8 * 1024));
  105. expect(
  106. shouldMaterializeZCodeStreamingToolInputPreview(second, {
  107. now: 1_001,
  108. toolName: "Bash",
  109. }),
  110. ).toBe(true);
  111. });
  112. it("does not time-flush large active inputs before raw growth budget", () => {
  113. const first = appendZCodeStreamingToolInputDelta(undefined, "x".repeat(9 * 1024));
  114. expect(shouldMaterializeZCodeStreamingToolInputPreview(first)).toBe(true);
  115. markZCodeStreamingToolInputPreviewMaterialized(first, 1_000);
  116. const slowChunk = appendZCodeStreamingToolInputDelta(first, "y");
  117. expect(
  118. shouldMaterializeZCodeStreamingToolInputPreview(slowChunk, {
  119. now: 10_000,
  120. }),
  121. ).toBe(false);
  122. const grownChunk = appendZCodeStreamingToolInputDelta(first, "y".repeat(8 * 1024));
  123. expect(
  124. shouldMaterializeZCodeStreamingToolInputPreview(grownChunk, {
  125. now: 10_000,
  126. }),
  127. ).toBe(true);
  128. });
  129. });