xonecas.io
70160
Ce

cel-tui / files

cel-tui / benchmarks / helpers.ts

10.5 KB
1/**
2 * Shared helpers for benchmark tree construction.
3 */
4
5import { HStack, Text, TextInput, VStack } from "@cel-tui/core";
6import type { ContainerNode, Node, StyleProps } from "@cel-tui/types";
7
8/** Build a flat VStack with N Text children. */
9export function flatTree(n: number): ContainerNode {
10  const children: Node[] = [];
11  for (let i = 0; i < n; i++) {
12    children.push(
13      Text(`Line ${i}: The quick brown fox jumps over the lazy dog`),
14    );
15  }
16  return VStack({ height: "100%" }, children);
17}
18
19/** Build a nested tree: depth levels of alternating VStack/HStack, `breadth` children per level. */
20export function nestedTree(depth: number, breadth: number): ContainerNode {
21  function build(d: number, isV: boolean): Node {
22    if (d === 0) {
23      return Text("leaf content here");
24    }
25    const children: Node[] = [];
26    for (let i = 0; i < breadth; i++) {
27      children.push(build(d - 1, !isV));
28    }
29    return isV ? VStack({ flex: 1 }, children) : HStack({ flex: 1 }, children);
30  }
31  return build(depth, true) as ContainerNode;
32}
33
34/** Build a styled tree with bgColor, fgColor, bold, focusStyle — stress inheritance. */
35export function styledTree(n: number): ContainerNode {
36  const children: Node[] = [];
37  for (let i = 0; i < n; i++) {
38    children.push(
39      VStack({ fgColor: "color06", bgColor: "color00", padding: { x: 1 } }, [
40        Text(`Styled line ${i}`, { bold: true, fgColor: "color03" }),
41        HStack({ fgColor: "color07" }, [
42          Text("left "),
43          Text("right", { fgColor: "color02" }),
44        ]),
45      ]),
46    );
47  }
48  return VStack(
49    { height: "100%", fgColor: "color07", bgColor: "color04" },
50    children,
51  );
52}
53
54/** Build a scrollable tree with more content than fits. */
55export function scrollableTree(lines: number): ContainerNode {
56  const children: Node[] = [];
57  for (let i = 0; i < lines; i++) {
58    children.push(Text(`Scrollable line ${i}: lorem ipsum dolor sit amet`));
59  }
60  return VStack(
61    { height: "100%", overflow: "scroll", scrollbar: true },
62    children,
63  );
64}
65
66/** Build a word-wrap heavy tree. */
67export function wrappedTextTree(paragraphs: number): ContainerNode {
68  const lorem =
69    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.";
70  const children: Node[] = [];
71  for (let i = 0; i < paragraphs; i++) {
72    children.push(Text(lorem, { wrap: "word" }));
73  }
74  return VStack({ height: "100%" }, children);
75}
76
77/** Build a tree resembling the Ink benchmark: styled text, flex layout, word-wrap. */
78export function inkComparableTree(): ContainerNode {
79  return VStack({ padding: { y: 1, x: 1 } }, [
80    Text("Hello World", { underline: true, bold: true, fgColor: "color01" }),
81    VStack({ width: 60 }, [
82      Text(
83        "Cupcake ipsum dolor sit amet candy candy. Sesame snaps cookie I love tootsie roll apple pie bonbon wafer. Caramels sesame snaps icing cotton candy I love cookie sweet roll. I love bonbon sweet.",
84        { wrap: "word" },
85      ),
86    ]),
87    VStack({}, [
88      Text("Colors:", { fgColor: "color00", bgColor: "color07" }),
89      VStack({ padding: { x: 1 } }, [
90        Text("- Red", { fgColor: "color01" }),
91        Text("- Blue", { fgColor: "color04" }),
92        Text("- Green", { fgColor: "color02" }),
93      ]),
94    ]),
95  ]);
96}
97
98/** Build a complex app-like tree (header, scrollable body, input bar). */
99export function appTree(messageCount: number): ContainerNode {
100  const messages: Node[] = [];
101  for (let i = 0; i < messageCount; i++) {
102    messages.push(
103      VStack({}, [
104        Text(`▶ User message ${i}`, { bold: true, fgColor: "color04" }),
105        Text(
106          `  This is the content of message ${i}. It can be quite long and detailed.`,
107        ),
108      ]),
109    );
110  }
111  return VStack({ height: "100%", fgColor: "color07" }, [
112    // Header
113    HStack({ height: 1, padding: { x: 1 } }, [
114      Text("Agent Chat", { bold: true }),
115      VStack({ flex: 1 }, []),
116      Text("model: gpt", { fgColor: "color08" }),
117    ]),
118    // Body
119    VStack(
120      { flex: 1, overflow: "scroll", scrollbar: true, padding: { x: 1 } },
121      messages,
122    ),
123    // Input bar
124    HStack({ height: 1, padding: { x: 1 }, gap: 1 }, [
125      Text("> "),
126      Text("type a message...", { fgColor: "color08" }),
127      HStack({ bgColor: "color08" }, [Text("[Send]", { bold: true })]),
128    ]),
129  ]);
130}
131
132const TOKEN_TEXT = [
133  "const",
134  " ",
135  "result",
136  " ",
137  "=",
138  " ",
139  "await",
140  " ",
141  "agent",
142  ".",
143  "run",
144  "(",
145  "{",
146  " cwd",
147  ": ",
148  "projectRoot",
149  ", ",
150  "prompt",
151  ": ",
152  "message",
153  " }",
154  ")",
155  ";",
156  " // stream tool output",
157];
158
159const TOKEN_STYLES: StyleProps[] = [
160  { fgColor: "color05", bold: true },
161  { fgColor: "color07" },
162  { fgColor: "color06" },
163  { fgColor: "color07" },
164  { fgColor: "color08" },
165  { fgColor: "color07" },
166  { fgColor: "color05", bold: true },
167  { fgColor: "color07" },
168  { fgColor: "color02" },
169  { fgColor: "color08" },
170  { fgColor: "color04" },
171  { fgColor: "color08" },
172  { fgColor: "color03" },
173  { fgColor: "color06" },
174  { fgColor: "color08" },
175  { fgColor: "color02" },
176  { fgColor: "color08" },
177  { fgColor: "color06" },
178  { fgColor: "color08" },
179  { fgColor: "color02" },
180  { fgColor: "color03" },
181  { fgColor: "color08" },
182  { fgColor: "color08" },
183  { fgColor: "color08", italic: true },
184];
185
186const MESSAGE_BODIES = [
187  "I'll inspect the rendering pipeline and then patch the benchmark helpers.",
188  "Please add a regression case for wide token rows and scrollback-heavy conversations.",
189  "tool: grep completed with 14 matches across packages/core/src/layout.ts and paint.ts",
190  "The fix preserves controlled scroll offsets while keeping the cursor visible.",
191];
192
193const MESSAGE_KINDS = ["user", "assistant", "tool"] as const;
194
195/** Build one wrapping HStack row with many styled Text token children. */
196export function hstackTokenRow(tokenCount: number): ContainerNode {
197  const children: Node[] = [];
198  for (let i = 0; i < tokenCount; i++) {
199    const index = i % TOKEN_TEXT.length;
200    children.push(Text(TOKEN_TEXT[index], TOKEN_STYLES[index]));
201  }
202  return HStack({ flexWrap: "wrap" }, children);
203}
204
205/** Build a syntax-highlight-like block without depending on clew. */
206export function syntaxLikeBlock(
207  lines: number,
208  tokensPerLine: number,
209): ContainerNode {
210  const children: Node[] = [];
211  for (let i = 0; i < lines; i++) {
212    children.push(
213      HStack({ flexWrap: "wrap" }, [
214        Text(`${String(i + 1).padStart(3, " ")} │ `, { fgColor: "color08" }),
215        ...hstackTokenRow(tokensPerLine).children,
216      ]),
217    );
218  }
219  return VStack({ gap: 0 }, children);
220}
221
222/** Build one representative variable-height mini-coder conversation row. */
223export function miniCoderMessage(index: number): ContainerNode {
224  const kind = MESSAGE_KINDS[index % MESSAGE_KINDS.length];
225  const isUser = kind === "user";
226  const isTool = kind === "tool";
227  const title = isUser ? "user" : isTool ? "tool" : "assistant";
228  const color = isUser ? "color04" : isTool ? "color03" : "color02";
229  const children: Node[] = [
230    HStack({ gap: 1 }, [
231      Text(title, { bold: true, fgColor: color }),
232      Text(`#${index}`, { fgColor: "color08" }),
233      Text(isTool ? "shell" : "chat", { fgColor: "color08" }),
234    ]),
235    Text(MESSAGE_BODIES[index % MESSAGE_BODIES.length], { wrap: "word" }),
236  ];
237
238  if (!isUser) {
239    children.push(syntaxLikeBlock(isTool ? 3 : 5, isTool ? 10 : 16));
240  }
241
242  children.push(
243    HStack({ gap: 1, flexWrap: "wrap" }, [
244      HStack({ padding: { x: 1 }, bgColor: "color08" }, [
245        Text(isTool ? "tool-call" : "tokens", { fgColor: "color00" }),
246      ]),
247      HStack({ padding: { x: 1 }, bgColor: "color00" }, [
248        Text(isTool ? "exit 0" : `${800 + (index % 400)} tok`, {
249          fgColor: "color07",
250        }),
251      ]),
252      Text("cached", { fgColor: "color08" }),
253    ]),
254  );
255
256  return VStack(
257    {
258      gap: 1,
259      padding: { x: 1, y: 1 },
260      bgColor: isUser ? "color00" : undefined,
261      fgColor: "color07",
262    },
263    children,
264  );
265}
266
267function miniCoderRoot(messages: Node[], totalMessages: number): ContainerNode {
268  return VStack({ height: "100%", gap: 1, padding: { x: 1, y: 1 } }, [
269    VStack(
270      { flex: 1, gap: 1, overflow: "scroll", scrollOffset: Infinity },
271      messages,
272    ),
273    HStack({ height: 1, gap: 1 }, [
274      Text("mini-coder", { bold: true, fgColor: "color02" }),
275      Text(`${totalMessages} messages`, { fgColor: "color08" }),
276      Text("model: sonnet", { fgColor: "color08" }),
277    ]),
278    TextInput({
279      height: 3,
280      value: "Summarize the benchmark bottleneck and propose the next patch.",
281      onChange: () => {},
282      placeholder: Text("message mini-coder", { fgColor: "color08" }),
283      bgColor: "color00",
284      fgColor: "color07",
285    }),
286  ]);
287}
288
289/** Build a mini-coder-next-like unvirtualized conversation tree. */
290export function miniCoderNextConversationTree(
291  messageCount: number,
292): ContainerNode {
293  const messages: Node[] = [];
294  for (let i = 0; i < messageCount; i++) {
295    messages.push(miniCoderMessage(i));
296  }
297  return miniCoderRoot(messages, messageCount);
298}
299
300/**
301 * Build a mini-coder-like virtualized conversation tree.
302 * Only a visible slice is rendered; fixed-height spacers represent the rest.
303 */
304export function miniCoderVirtualizedConversationTree(
305  totalMessages: number,
306  visibleMessages: number,
307): ContainerNode {
308  const start = Math.max(0, totalMessages - visibleMessages);
309  const hiddenBefore = start;
310  const hiddenAfter = Math.max(0, totalMessages - start - visibleMessages);
311  const messages: Node[] = [];
312
313  if (hiddenBefore > 0) {
314    messages.push(VStack({ height: hiddenBefore * 8 }, []));
315  }
316  for (
317    let i = start;
318    i < Math.min(totalMessages, start + visibleMessages);
319    i++
320  ) {
321    messages.push(miniCoderMessage(i));
322  }
323  if (hiddenAfter > 0) {
324    messages.push(VStack({ height: hiddenAfter * 8 }, []));
325  }
326
327  return miniCoderRoot(messages, totalMessages);
328}
329
330/** Representative mini-coder-next message nodes for content-height measurement. */
331export function miniCoderNextMeasureNodes(count: number): Node[] {
332  const nodes: Node[] = [];
333  for (let i = 0; i < count; i++) {
334    nodes.push(miniCoderMessage(i));
335  }
336  return nodes;
337}
338
339/** Pure ASCII string for width benchmarking. */
340export const ASCII_STRING =
341  "The quick brown fox jumps over the lazy dog. 0123456789!@#$%^&*()";
342
343/** CJK + emoji string for width benchmarking. */
344export const CJK_EMOJI_STRING = "世界你好 🌍🎉 東京タワー 한국어 テスト 😀👨‍👩‍👧";
345
346/** Mixed content string. */
347export const MIXED_STRING =
348  "Hello 世界! Testing 🎉 mixed content with ASCII and CJK 東京.";