tui-thought.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { SessionEventType, type SessionEvent } from "@zcode/contracts";
  4. import { getZCodeCopy } from "@zcode/i18n";
  5. import { applySessionEventToState } from "../src/app-events.js";
  6. import type { Message, ThoughtTranscriptPart } from "../src/app-model.js";
  7. import {
  8. ThoughtTranscriptPartFrame,
  9. thoughtPlaceholderLabel,
  10. } from "../src/app-thought-components.js";
  11. const EN_COPY = getZCodeCopy("en-US").tui;
  12. test("projects streamed reasoning as an expandable thought placeholder", () => {
  13. const state = createSessionEventState();
  14. state.apply(
  15. sessionEvent(SessionEventType.ModelStreaming, {
  16. assistantMessageId: "assistant-1",
  17. delta: "private thinking",
  18. kind: "reasoning_delta",
  19. }),
  20. );
  21. const part = onlyThoughtPart(state.messages);
  22. assert.equal(part.status, "thinking");
  23. assert.equal(part.text, "private thinking");
  24. assert.equal(part.contentCharCount, "private thinking".length);
  25. assert.equal(state.messages[0]?.content.includes("private thinking"), false);
  26. state.apply(
  27. sessionEvent(SessionEventType.ModelStreaming, {
  28. assistantMessageId: "assistant-1",
  29. delta: "",
  30. kind: "reasoning_end",
  31. }),
  32. );
  33. assert.equal(onlyThoughtPart(state.messages).status, "thought");
  34. });
  35. test("does not create a thought placeholder for an empty reasoning start", () => {
  36. const state = createSessionEventState();
  37. state.apply(
  38. sessionEvent(SessionEventType.ModelStreaming, {
  39. assistantMessageId: "assistant-1",
  40. delta: "",
  41. kind: "reasoning_start",
  42. }),
  43. );
  44. assert.deepEqual(state.messages, []);
  45. });
  46. test("marks an active thought complete when the stream finishes", () => {
  47. const state = createSessionEventState();
  48. state.apply(
  49. sessionEvent(SessionEventType.ModelStreaming, {
  50. assistantMessageId: "assistant-1",
  51. delta: "hidden",
  52. kind: "reasoning_delta",
  53. }),
  54. );
  55. state.apply(
  56. sessionEvent(SessionEventType.ModelStreaming, {
  57. assistantMessageId: "assistant-1",
  58. delta: "",
  59. done: true,
  60. kind: "finish",
  61. }),
  62. );
  63. assert.equal(onlyThoughtPart(state.messages).status, "thought");
  64. assert.equal(state.messages[0]?.streaming, false);
  65. });
  66. test("keeps thought text and tool parts in stream order", () => {
  67. const state = createSessionEventState();
  68. state.apply(
  69. sessionEvent(SessionEventType.ModelStreaming, {
  70. assistantMessageId: "assistant-1",
  71. delta: "hidden",
  72. kind: "reasoning_delta",
  73. }),
  74. );
  75. state.apply(
  76. sessionEvent(SessionEventType.ModelStreaming, {
  77. assistantMessageId: "assistant-1",
  78. delta: "",
  79. kind: "reasoning_end",
  80. }),
  81. );
  82. state.apply(
  83. sessionEvent(SessionEventType.ModelStreaming, {
  84. assistantMessageId: "assistant-1",
  85. delta: "Visible answer.",
  86. kind: "text_delta",
  87. }),
  88. );
  89. state.apply(
  90. sessionEvent(SessionEventType.ModelStreaming, {
  91. assistantMessageId: "assistant-1",
  92. delta: "",
  93. kind: "tool_call",
  94. toolCallId: "call-1",
  95. toolName: "Read",
  96. }),
  97. );
  98. state.apply(
  99. sessionEvent(SessionEventType.ToolCallScheduled, {
  100. input: { file_path: "package.json" },
  101. toolCallId: "call-1",
  102. toolName: "Read",
  103. }),
  104. );
  105. assert.deepEqual(
  106. state.messages[0]?.parts?.map((part) => part.type),
  107. ["thought", "text", "tool"],
  108. );
  109. });
  110. test("renders expanded thought content with plus and minus markers", () => {
  111. const part: ThoughtTranscriptPart = {
  112. contentCharCount: 14,
  113. status: "thought",
  114. text: "private thinking",
  115. type: "thought",
  116. };
  117. const frame = ThoughtTranscriptPartFrame({
  118. copy: EN_COPY,
  119. expanded: true,
  120. onToggle: () => {},
  121. part,
  122. });
  123. const lines = collectTextLines(frame);
  124. assert.equal(thoughtPlaceholderLabel(part, EN_COPY), "Thought");
  125. assert.ok(lines.some((line) => line.includes("- Thought")));
  126. assert.ok(lines.some((line) => line.includes("private thinking")));
  127. });
  128. function createSessionEventState(): {
  129. apply: (event: SessionEvent) => void;
  130. messages: Message[];
  131. } {
  132. const state: { messages: Message[] } = { messages: [] };
  133. const assistantMessageIdsByToolCallId = new Map<string, string>();
  134. const toolNamesById = new Map<string, string>();
  135. return {
  136. apply: (event) => {
  137. applySessionEventToState(event, {
  138. assistantMessageIdsByToolCallId,
  139. setActiveTurnId: () => {},
  140. setCacheStats: () => {},
  141. setContextUsage: () => {},
  142. setLastError: () => {},
  143. setLiveModelText: () => {},
  144. setMessages: (updater) => {
  145. state.messages = typeof updater === "function" ? updater(state.messages) : updater;
  146. },
  147. setModel: () => {},
  148. setNetworkRequests: () => {},
  149. setStatus: () => {},
  150. setTodos: () => {},
  151. setUsage: () => {},
  152. toolNamesById,
  153. });
  154. },
  155. get messages() {
  156. return state.messages;
  157. },
  158. };
  159. }
  160. function onlyThoughtPart(messages: Message[]): ThoughtTranscriptPart {
  161. const part = messages[0]?.parts?.[0];
  162. assert.equal(part?.type, "thought");
  163. return part;
  164. }
  165. function collectTextLines(node: unknown): string[] {
  166. const lines: string[] = [];
  167. const visit = (value: unknown) => {
  168. if (typeof value === "string") {
  169. lines.push(value);
  170. return;
  171. }
  172. if (Array.isArray(value)) {
  173. for (const child of value) visit(child);
  174. return;
  175. }
  176. if (typeof value === "object" && value !== null && "props" in value) {
  177. const element = value as { props?: { children?: unknown } };
  178. visit(element.props?.children);
  179. }
  180. };
  181. visit(node);
  182. return lines;
  183. }
  184. function sessionEvent(type: SessionEventType, payload: unknown): SessionEvent {
  185. return {
  186. id: "event-1",
  187. payload,
  188. sequenceNumber: 1,
  189. sessionId: "session-1",
  190. timestamp: new Date(0),
  191. traceId: "trace-1",
  192. type,
  193. } as SessionEvent;
  194. }