zebflow 0.10.10

Reactive web automation platform — pipelines, templates, and real-time collaboration in a single binary.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
import { useEffect, useMemo, useRef, useState, cx } from "zeb/react";
import Button from "@/components/ui/button";
import Kbd from "@/components/ui/kbd";
import Checkbox from "@/components/ui/checkbox";
import { useStudioChrome } from "@/pages/project-studio/components/studio-chrome-context";
import { navigate, patchOverlay } from "@/pages/project-studio/components/studio-shell-behavior";

type ConsoleLine = {
  id: number;
  text: string;
  cls?: string;
  isLink?: string;
};

const LINE_STYLES: Record<string, string> = {
  "cli-echo": "text-gray-400",
  "cli-info": "text-gray-500 italic",
  "cli-error": "text-red-400",
  "cli-success": "text-green-400",
  "cli-warning": "text-amber-300",
  "cli-muted": "text-gray-500",
  "cli-blank": "block h-[0.6em]",
  "cli-ai": "text-sky-300 whitespace-pre-wrap break-words",
  "cli-tool": "text-[#ebbe7d] italic",
  "cli-thinking": "text-gray-600 italic",
  "cli-nav": "",
};

const DSL_VERBS = new Set([
  "get", "describe", "register", "activate", "deactivate",
  "execute", "run", "patch", "git", "node", "clear", "help",
]);

const ASSISTANT_CONFIG_CODES = new Set([
  "ASSISTANT_NOT_CONFIGURED",
  "ASSISTANT_DISABLED",
  "ASSISTANT_NO_LLM",
  "ASSISTANT_CREDENTIAL_MISSING",
  "ASSISTANT_CREDENTIAL_INVALID",
]);

function lineClass(cls?: string) {
  const base = "text-gray-400 whitespace-pre break-all";
  if (!cls) return base;
  const extra = cls.split(/\s+/).map((c) => LINE_STYLES[c] ?? "").join(" ");
  return cx(base, extra);
}

function sleep(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

function stripThinkTags(text: string) {
  return text.replace(/<think>[\s\S]*?<\/think>\s*/gi, "").trim();
}

async function consumeSse(
  stream: ReadableStream<Uint8Array>,
  onEvent: (event: { event: string; data: string }) => void,
) {
  const reader = stream.getReader();
  const decoder = new TextDecoder();
  let buffer = "";

  while (true) {
    const { value, done } = await reader.read();
    if (done) break;
    buffer += decoder.decode(value, { stream: true });
    let split = buffer.indexOf("\n\n");
    while (split >= 0) {
      const raw = buffer.slice(0, split);
      buffer = buffer.slice(split + 2);
      split = buffer.indexOf("\n\n");
      let event = "message";
      const dataLines: string[] = [];
      for (const line of raw.split("\n")) {
        if (line.startsWith("event:")) event = line.slice(6).trim();
        if (line.startsWith("data:")) dataLines.push(line.slice(5).trimStart());
      }
      onEvent({ event, data: dataLines.join("\n") });
    }
  }
}

class InteractionRunner {
  private cancelled = false;

  constructor(private label: string) {
    patchOverlay({ active: true, label: this.label, clicking: false });
    document.addEventListener("keydown", this.onKey, { capture: true });
  }

  private onKey = (e: KeyboardEvent) => {
    if (e.key === "Escape") {
      e.preventDefault();
      e.stopImmediatePropagation();
      this.cancelled = true;
    }
  };

  private uninstall() {
    patchOverlay({ active: false, label: "" });
    document.removeEventListener("keydown", this.onKey, { capture: true });
  }

  private setStatus(msg: string) {
    patchOverlay({ label: msg });
  }

  private async moveCursorTo(el: Element) {
    const rect = el.getBoundingClientRect();
    patchOverlay({
      cursorX: Math.round(rect.left + rect.width / 2),
      cursorY: Math.round(rect.top + rect.height / 2),
    });
    await sleep(280);
  }

  private async waitForSelector(selector: string, timeoutMs: number): Promise<Element | null> {
    const deadline = Date.now() + timeoutMs;
    while (Date.now() < deadline) {
      if (this.cancelled) return null;
      const el = document.querySelector(selector);
      if (el) return el;
      await sleep(80);
    }
    return null;
  }

  async run(steps: any[]): Promise<void> {
    try {
      for (const step of steps) {
        if (this.cancelled) break;
        await this.executeStep(step);
      }
    } finally {
      this.uninstall();
    }
  }

  private async executeStep(step: any) {
    const action = String(step?.action ?? "");
    this.setStatus(`${this.label} — ${action}…`);

    switch (action) {
      case "navigate": {
        const url = String(step?.url ?? "");
        if (!url) break;
        navigate(url);
        await sleep(900);
        break;
      }
      case "wait_selector": {
        await this.waitForSelector(String(step?.selector ?? ""), Number(step?.timeout_ms ?? 5000));
        break;
      }
      case "click": {
        const selector = String(step?.selector ?? "");
        if (!selector) break;
        const el = await this.waitForSelector(selector, Number(step?.timeout_ms ?? 5000));
        if (!el) break;
        await this.moveCursorTo(el);
        patchOverlay({ clicking: true });
        await sleep(100);
        (el as HTMLElement).click();
        patchOverlay({ clicking: false });
        await sleep(250);
        break;
      }
      default:
        await sleep(120);
        break;
    }
  }
}

export default function ProjectConsole({ owner, project }) {
  const { consoleOpen, toggleConsole, setActivePanel } = useStudioChrome();
  const [lines, setLines] = useState<ConsoleLine[]>([]);
  const [inputValue, setInputValue] = useState("");
  const [busy, setBusy] = useState(false);
  const [pendingLines, setPendingLines] = useState<string[]>([]);
  const [cmdHistory, setCmdHistory] = useState<string[]>([]);
  const [histIdx, setHistIdx] = useState(-1);
  const [useHighModel, setUseHighModel] = useState(false);
  const [autoNavigate, setAutoNavigate] = useState(true);
  const [chatHistory, setChatHistory] = useState<Array<{ role: string; content: string }>>([]);
  const [acItems, setAcItems] = useState<string[]>([]);
  const [acIndex, setAcIndex] = useState(-1);
  const inputRef = useRef<HTMLInputElement | null>(null);
  const bottomRef = useRef<HTMLDivElement | null>(null);
  const lineIdRef = useRef(0);
  const currentPageRef = useRef("");

  const prompt = busy ? "···" : pendingLines.length > 0 ? "···>" : "zf>";
  const chatKey = useMemo(() => `zf-chat-${owner}-${project}`, [owner, project]);
  const dslApi = useMemo(() => `/api/projects/${owner}/${project}/pipelines/dsl`, [owner, project]);
  const chatApi = useMemo(() => `/api/projects/${owner}/${project}/assistant/chat`, [owner, project]);

  function pushLine(text: string, cls?: string, isLink?: string) {
    setLines((prev) => [...prev, { id: lineIdRef.current++, text, cls, isLink }]);
  }

  function dropLine(id: number) {
    setLines((prev) => prev.filter((line) => line.id !== id));
  }

  function clearLines() {
    setLines([]);
  }

  function emitLink(url: string) {
    pushLine(url, "cli-nav", url);
  }

  function printHelp() {
    [
      "DSL Commands:",
      "  get pipelines              List all pipelines",
      "  get nodes                  List all available node kinds",
      "  get connections            List DB connections",
      "  get credentials            List credential keys",
      "  get templates              List template files",
      "  get docs                   List project docs",
      "  describe pipeline <name>   Show pipeline details + nodes + edges",
      "  describe connection <slug> Show connection details",
      "  describe node <kind>       Show node kind details",
      "  register <name> [--path /] | <node-chain>",
      "  patch pipeline <name> node <id> [--flag val...]",
      "  activate pipeline <name>   Make a pipeline live",
      "  deactivate pipeline <name> Take a pipeline offline",
      "  execute pipeline <name>    Execute an active pipeline",
      "  run [--dry-run] | <nodes>  Execute an ephemeral one-shot pipeline",
      "  git status|log|diff|add|commit [args]",
      "  clear                      Clear output",
      "  help                       Show this help",
      "",
      "Everything else is forwarded to the project assistant.",
      "Chain commands with &&.  Continue long commands with \\.",
      "  Shortcuts: ` to toggle console",
    ].forEach((line) => pushLine(line));
  }

  useEffect(() => {
    currentPageRef.current = window.location.pathname;
    const onNav = (e: Event) => {
      const detail = (e as CustomEvent).detail;
      if (detail?.url) currentPageRef.current = detail.url;
    };
    window.addEventListener("rwe:nav", onNav);
    return () => window.removeEventListener("rwe:nav", onNav);
  }, []);

  useEffect(() => {
    try {
      const saved = localStorage.getItem(chatKey);
      const parsed = saved ? JSON.parse(saved) : [];
      const next = Array.isArray(parsed) ? parsed : [];
      setChatHistory(next);
      if (next.length > 0) {
        const recent = next.slice(-20);
        const bootLines: ConsoleLine[] = [];
        let nextId = 0;
        for (const msg of recent) {
          if (msg.role === "user") {
            bootLines.push({ id: nextId++, text: `you> ${msg.content}`, cls: "cli-echo" });
          } else if (msg.role === "assistant") {
            for (const line of String(msg.content || "").split("\n")) {
              bootLines.push({ id: nextId++, text: line, cls: "cli-info" });
            }
          }
        }
        if (bootLines.length) {
          bootLines.push({ id: nextId++, text: "─── above: previous session ───", cls: "cli-muted" });
        }
        bootLines.push({ id: nextId++, text: "Zebflow Console  ·  type commands or ask questions", cls: "cli-muted" });
        bootLines.push({ id: nextId++, text: "  type 'help' for DSL reference  ·  ` to toggle", cls: "cli-muted" });
        lineIdRef.current = nextId;
        setLines(bootLines);
      } else {
        setLines([
          { id: 0, text: "Zebflow Console  ·  type commands or ask questions", cls: "cli-muted" },
          { id: 1, text: "  type 'help' for DSL reference  ·  ` to toggle", cls: "cli-muted" },
        ]);
        lineIdRef.current = 2;
      }
    } catch {
      setChatHistory([]);
      setLines([
        { id: 0, text: "Zebflow Console  ·  type commands or ask questions", cls: "cli-muted" },
        { id: 1, text: "  type 'help' for DSL reference  ·  ` to toggle", cls: "cli-muted" },
      ]);
      lineIdRef.current = 2;
    }
  }, [chatKey]);

  useEffect(() => {
    if (!consoleOpen) return;
    setTimeout(() => inputRef.current?.focus(), 40);
  }, [consoleOpen]);

  useEffect(() => {
    bottomRef.current?.scrollIntoView({ behavior: "instant" as any });
  }, [lines, consoleOpen]);

  useEffect(() => {
    function onKeyDown(e: KeyboardEvent) {
      if (e.key !== "`") return;
      const active = document.activeElement;
      const inInput =
        active instanceof HTMLInputElement ||
        active instanceof HTMLTextAreaElement ||
        (active instanceof HTMLElement && active.isContentEditable);
      const inConsole = active instanceof HTMLElement && !!active.closest?.("[data-console-panel]");
      if (inInput && !inConsole) return;
      e.preventDefault();
      toggleConsole();
    }
    window.addEventListener("keydown", onKeyDown, { capture: true });
    return () => window.removeEventListener("keydown", onKeyDown, { capture: true } as any);
  }, [toggleConsole]);

  function getMatches(value: string) {
    const trimmed = value.trimStart();
    if (!trimmed) return [];
    const lower = trimmed.toLowerCase();
    const fromHistory = [...cmdHistory]
      .reverse()
      .filter((cmd) => cmd.toLowerCase().startsWith(lower) && cmd !== trimmed);
    const fromStatic = [
      "get pipelines", "get nodes", "get connections", "get credentials",
      "get templates", "get docs", "describe pipeline ", "describe connection ",
      "describe node ", "register ", "activate pipeline ", "deactivate pipeline ",
      "execute pipeline ", "run ", "run --dry-run", "patch pipeline ",
      "git status", "git log", "git diff", "git add .", "git commit -m ",
      "clear", "help",
    ].filter((cmd) => cmd.toLowerCase().startsWith(lower) && cmd !== trimmed);
    return [...new Set([...fromHistory, ...fromStatic])].slice(0, 8);
  }

  useEffect(() => {
    setAcIndex(-1);
    setAcItems(getMatches(inputValue));
  }, [inputValue, cmdHistory]);

  async function runDsl(cmd: string) {
    const verb = cmd.split(/\s+/)[0].toLowerCase();
    if (verb === "clear") {
      clearLines();
      return;
    }
    if (verb === "help" || cmd.includes("--help") || /\s-h(\s|$)/.test(cmd)) {
      printHelp();
      return;
    }
    try {
      const resp = await fetch(dslApi, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ dsl: cmd }),
      });
      if (resp.status === 401) {
        navigate("/login");
        return;
      }
      const data = await resp.json().catch(() => null);
      const incoming = Array.isArray(data?.lines) ? data.lines : [];
      if (!incoming.length) {
        pushLine("(no output)", "cli-muted");
      } else {
        for (const line of incoming) {
          const text = typeof line === "string" ? line : String(line?.text ?? "");
          const cls = typeof line === "string" ? undefined : line?.cls;
          pushLine(text, cls);
        }
      }
      if (data?.navigate) {
        emitLink(data.navigate);
        if (autoNavigate) navigate(data.navigate);
      }
    } catch (err: any) {
      pushLine(`Error: ${err?.message || String(err)}`, "cli-error");
    }
  }

  async function runAssistant(message: string) {
    const nextHistory = [...chatHistory, { role: "user", content: message }];
    setChatHistory(nextHistory);
    const thinkingId = lineIdRef.current;
    pushLine("thinking…", "cli-thinking");

    try {
      const response = await fetch(chatApi, {
        method: "POST",
        headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
        body: JSON.stringify({
          message,
          history: nextHistory.slice(-24),
          use_high_model: useHighModel,
          current_page: currentPageRef.current,
          client_time: new Date().toLocaleString(),
        }),
      });

      if (response.status === 401) {
        navigate("/login");
        return;
      }

      if (!response.ok) {
        dropLine(thinkingId);
        const payload = await response.json().catch(() => null);
        const code = String(payload?.error?.code || "");
        const messageText = String(payload?.error?.message || `request failed (${response.status})`);
        if (ASSISTANT_CONFIG_CODES.has(code)) {
          pushLine("AI assistant is not ready for this project.", "cli-warning");
          pushLine(`${messageText}. Add an OpenAI credential and assistant config in Settings.`, "cli-warning");
        } else {
          pushLine(`Error: ${messageText}`, "cli-error");
        }
        setChatHistory((prev) => prev.slice(0, -1));
        return;
      }

      if (!response.body) {
        dropLine(thinkingId);
        pushLine("Error: assistant stream missing response body", "cli-error");
        setChatHistory((prev) => prev.slice(0, -1));
        return;
      }

      let finalContent = "";
      await consumeSse(response.body, ({ event, data }) => {
        try {
          if (event === "tool_call") {
            const p = JSON.parse(data);
            pushLine(`  [${p.tool}]`, "cli-tool");
          } else if (event === "tool_result") {
            const p = JSON.parse(data);
            const preview = String(p.result_preview || "").split("\n")[0].slice(0, 100);
            if (preview) pushLine(`  · ${preview}`, "cli-muted");
          } else if (event === "interaction_sequence") {
            const p = JSON.parse(data);
            if (Array.isArray(p?.steps)) {
              const runner = new InteractionRunner(p.label || "Running…");
              runner.run(p.steps).catch(() => {});
            }
          } else if (event === "navigate") {
            const p = JSON.parse(data);
            if (p?.url) {
              emitLink(p.url);
              if (autoNavigate) navigate(p.url);
            }
          } else if (event === "message") {
            finalContent = stripThinkTags(String(JSON.parse(data)?.content || ""));
            dropLine(thinkingId);
            if (finalContent) {
              for (const line of finalContent.split("\n")) pushLine(line, "cli-ai");
            }
          }
        } catch {
          if (event === "message") {
            finalContent = stripThinkTags(data);
            dropLine(thinkingId);
            if (finalContent) pushLine(finalContent, "cli-ai");
          }
        }
      });

      const completed = [...nextHistory, { role: "assistant", content: finalContent }];
      setChatHistory(completed);
      try {
        localStorage.setItem(chatKey, JSON.stringify(completed.slice(-50)));
      } catch {}
    } catch (err: any) {
      dropLine(thinkingId);
      pushLine(`Error: ${err?.message || String(err)}`, "cli-error");
      setChatHistory((prev) => prev.slice(0, -1));
    }
  }

  async function submitCurrent() {
    const raw = inputValue;
    if (!raw || busy) return;
    setAcItems([]);
    setAcIndex(-1);
    setInputValue("");
    setHistIdx(-1);

    if (raw.trimEnd().endsWith("\\")) {
      const next = [...pendingLines, raw.trimEnd().slice(0, -1)];
      setPendingLines(next);
      pushLine(`${next.length === 1 ? "zf>" : "···"}  ${raw}`, "cli-echo");
      return;
    }

    const cmd = [...pendingLines, raw.trim()].join(" ").trim();
    setPendingLines([]);
    if (!cmd) return;
    setCmdHistory((prev) => [...prev, cmd]);
    pushLine(`zf> ${cmd}`, "cli-echo");
    setBusy(true);
    try {
      const verb = cmd.split(/\s+/)[0].toLowerCase();
      if (DSL_VERBS.has(verb)) {
        await runDsl(cmd);
      } else {
        await runAssistant(cmd);
      }
    } finally {
      setBusy(false);
      setTimeout(() => inputRef.current?.focus(), 0);
    }
  }

  function onInputKeyDown(e: KeyboardEvent) {
    const acVisible = acItems.length > 0;
    if (e.key === "Enter") {
      e.preventDefault();
      void submitCurrent();
      return;
    }
    if (e.key === "Tab") {
      e.preventDefault();
      if (!acVisible) return;
      if (e.shiftKey) {
        setAcIndex((prev) => Math.max(prev - 1, 0));
      } else if (acIndex < 0) {
        setAcIndex(0);
      } else {
        const choice = acItems[acIndex];
        if (choice) {
          setInputValue(choice);
          setAcItems([]);
          setAcIndex(-1);
        }
      }
      return;
    }
    if (acVisible) {
      if (e.key === "ArrowUp") {
        e.preventDefault();
        setAcIndex((prev) => Math.max(prev - 1, 0));
        return;
      }
      if (e.key === "ArrowDown") {
        e.preventDefault();
        setAcIndex((prev) => Math.min(prev + 1, acItems.length - 1));
        return;
      }
      if (e.key === "Escape") {
        e.stopPropagation();
        setAcItems([]);
        setAcIndex(-1);
        return;
      }
    }
    if (e.key === "ArrowUp") {
      e.preventDefault();
      if (!cmdHistory.length) return;
      const nextIdx = Math.min(histIdx + 1, cmdHistory.length - 1);
      setHistIdx(nextIdx);
      setInputValue(cmdHistory[cmdHistory.length - 1 - nextIdx] || "");
      return;
    }
    if (e.key === "ArrowDown") {
      e.preventDefault();
      const nextIdx = Math.max(histIdx - 1, -1);
      setHistIdx(nextIdx);
      setInputValue(nextIdx < 0 ? "" : (cmdHistory[cmdHistory.length - 1 - nextIdx] || ""));
      return;
    }
    if (e.key === "Escape") {
      setPendingLines([]);
      setInputValue("");
    }
  }

  return (
    <div
      className={cx(
        "fixed bottom-0 left-0 right-0 z-[1000] flex flex-col border-t border-white/10 bg-[#080b10] transition",
        consoleOpen ? "max-h-[40vh]" : "max-h-0 overflow-hidden",
      )}
      data-console-panel
      data-owner={owner}
      data-project={project}
      aria-hidden={consoleOpen ? "false" : "true"}
    >
      <div className="flex min-h-[2rem] select-none items-center gap-2 border-b border-white/[0.06] px-4 py-1.5">
        <span className="font-mono text-xs font-bold text-gray-500">Console</span>
        <span className="inline-flex items-center gap-1 text-[0.65rem] font-mono text-gray-700">
          <Kbd>`</Kbd>
          <span>toggle</span>
        </span>
        <div className="ml-auto flex items-center gap-2.5">
          <Checkbox label="High" checked={useHighModel} onChange={(e) => setUseHighModel(!!e.currentTarget.checked)} />
          <Checkbox label="Auto nav" checked={autoNavigate} onChange={(e) => setAutoNavigate(!!e.currentTarget.checked)} />
        </div>
        <Button
          variant="ghost"
          size="icon"
          type="button"
          aria-label="Close console"
          onClick={() => {
            setActivePanel(null);
            if (consoleOpen) toggleConsole();
          }}
          className="ml-1 size-6 text-[0.9rem] text-gray-700 hover:text-gray-400"
        >
          ✕
        </Button>
      </div>

      <div className="flex-1 min-h-0 overflow-y-auto">
        <div className="px-4 py-1.5 font-mono text-[0.78rem] leading-[1.65]">
          {lines.map((line) =>
            line.isLink ? (
              <div key={line.id} className={lineClass(line.cls)}>
                <a
                  href={line.isLink}
                  className="text-sky-400 no-underline hover:text-sky-300 hover:underline"
                  onClick={(e) => {
                    e.preventDefault();
                    navigate(line.isLink!);
                  }}
                >
                  {line.text}
                </a>
              </div>
            ) : (
              <div key={line.id} className={lineClass(line.cls)}>{line.text}</div>
            ),
          )}
          <div ref={bottomRef} />
        </div>
      </div>

      {acItems.length > 0 ? (
        <div className="overflow-hidden border-t border-white/[0.04] bg-[#090d14]">
          {acItems.map((item, i) => {
            const matchLen = inputValue.trimStart().length;
            const historyMatch = cmdHistory.includes(item);
            return (
              <button
                key={`${item}-${i}`}
                type="button"
                className={cx(
                  "flex w-full items-center gap-1 px-4 py-[0.22rem] font-mono text-[0.78rem]",
                  i === acIndex ? "bg-white/[0.06] text-gray-200" : "text-gray-400",
                )}
                onClick={() => {
                  setInputValue(item);
                  setAcItems([]);
                  setAcIndex(-1);
                  setTimeout(() => inputRef.current?.focus(), 0);
                }}
              >
                <span className="text-sky-300">{item.slice(0, matchLen)}</span>
                <span>{item.slice(matchLen)}</span>
                {historyMatch ? (
                  <span className="ml-auto shrink-0 text-[0.68rem] text-[#1e3a52]">hist</span>
                ) : null}
              </button>
            );
          })}
        </div>
      ) : null}

      <form
        className="flex items-center gap-1.5 border-t border-white/[0.06] bg-[#080b10] px-4 pb-2 pt-1.5"
        onSubmit={(e) => {
          e.preventDefault();
          void submitCurrent();
        }}
      >
        <span className="shrink-0 select-none font-mono text-[0.8rem] text-green-500">{prompt}</span>
        <input
          ref={inputRef}
          type="text"
          value={inputValue}
          onInput={(e: any) => setInputValue(e.target.value)}
          onKeyDown={(e: any) => onInputKeyDown(e)}
          className="min-w-0 flex-1 border-none bg-transparent font-mono text-[0.82rem] text-green-300 caret-green-400 outline-none placeholder:text-gray-600"
          placeholder="ask or type commands"
          autoComplete="off"
          spellCheck={false}
          disabled={busy}
        />
      </form>
    </div>
  );
}