File size: 13,456 Bytes
d8804ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81ef15a
d8804ab
81ef15a
d8804ab
81ef15a
d8804ab
81ef15a
d8804ab
81ef15a
d8804ab
 
 
 
 
 
 
 
 
81ef15a
d8804ab
d3e44c7
d8804ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
587459d
 
 
d8804ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import React, { useState, useEffect, useRef } from "react";
import { 
  Terminal as TerminalIcon, 
  Activity, 
  Cpu, 
  Sparkles, 
  HelpCircle,
  Play
} from "lucide-react";

interface TerminalLine {
  type: "input" | "output" | "error" | "success" | "info" | "system";
  text: string;
}

const presets = [
  { label: "⚡ Diagnostics", cmd: "status", icon: Activity },
  { label: "📊 Benchmarks", cmd: "benchmark", icon: Cpu },
  { label: "🧠 Route Query", cmd: "route", icon: Sparkles },
  { label: "❓ CLI Help", cmd: "help", icon: HelpCircle },
];

export function HubLiveTerminal() {
  const [history, setHistory] = useState<TerminalLine[]>([]);
  const [inputValue, setInputValue] = useState("");
  const [isProcessing, setIsProcessing] = useState(false);
  const consoleContainerRef = useRef<HTMLDivElement>(null);

  // Helper to append lines to the terminal history
  const appendLine = (type: TerminalLine["type"], text: string) => {
    setHistory((prev) => [...prev, { type, text }]);
  };

  // Scroll to bottom of terminal container whenever history updates
  useEffect(() => {
    if (consoleContainerRef.current) {
      consoleContainerRef.current.scrollTop = consoleContainerRef.current.scrollHeight;
    }
  }, [history]);

  // Initial simulated boot sequence
  useEffect(() => {
    const runBootSequence = async () => {
      setIsProcessing(true);
      await new Promise((r) => setTimeout(r, 400));
      appendLine("output", "HubAPI Neural Gateway Client CLI [v1.0.0]");
      await new Promise((r) => setTimeout(r, 300));
      appendLine("info", "Syncing connections with global model registries…");
      await new Promise((r) => setTimeout(r, 500));
      appendLine("success", "✔ Connection established with gateway node US-East-1");
      appendLine("success", "✔ Model router mesh status: ONLINE");
      await new Promise((r) => setTimeout(r, 300));
      appendLine("output", "Ready for execution. Type a command or click a quick action above.");
      setIsProcessing(false);
    };
    runBootSequence();
  }, []);

  // Simulates character-by-character typing for quick presets
  const simulateTyping = (cmdText: string) => {
    if (isProcessing) return;
    setIsProcessing(true);
    setInputValue("");
    let idx = 0;

    const timer = setInterval(() => {
      setInputValue(cmdText.slice(0, idx + 1));
      idx++;
      if (idx >= cmdText.length) {
        clearInterval(timer);
        setTimeout(() => {
          setInputValue("");
          processCommand(cmdText);
        }, 200);
      }
    }, 50);
  };

  // Parse and process command scripts
  const processCommand = async (cmd: string) => {
    setIsProcessing(true);
    appendLine("input", `> ${cmd}`);

    const cleanCmd = cmd.trim().toLowerCase();
    const args = cleanCmd.split(" ");
    const primary = args[0];

    // Wait a brief delay to simulate network latency
    await new Promise((r) => setTimeout(r, 400));

    switch (primary) {
      case "help":
        appendLine("system", "HUBAPI CLI COMMAND DIRECTORY");
        appendLine("info", "  status      - Check active node routing latency & system loads");
        appendLine("info", "  benchmark   - Run latency and cost tests across primary LLM nodes");
        appendLine("info", "  route       - Run intelligent query routing simulation");
        appendLine("info", "  clear       - Clear the screen buffer logs");
        appendLine("info", "  help        - Display this menu guide");
        break;

      case "clear":
        setHistory([]);
        break;

      case "status":
        appendLine("output", "Broadcasting health status query across model registry…");
        await new Promise((r) => setTimeout(r, 450));
        appendLine("success", "✔ Primary Gateway: US-East-1 Node (Virginia) - Active [14ms]");
        appendLine("success", "✔ Secondary Node: EU-West-3 Node (Paris) - Active [76ms]");
        await new Promise((r) => setTimeout(r, 300));
        appendLine("info", "Connected Model Providers: OpenAI, Anthropic, Google, Mistral, Meta");
        appendLine("output", "System Status: HEALTHY | Current load: 12.8% | Nodes online: 142/142");
        break;

      case "benchmark":
        appendLine("output", "Starting model speed/cost benchmark (10KB prompt payload)…");
        await new Promise((r) => setTimeout(r, 600));
        appendLine("system", "MODEL             LATENCY      COST/1M TOK      ROUTING SCORE");
        appendLine("system", "---------------------------------------------------------------");
        await new Promise((r) => setTimeout(r, 200));
        appendLine("info",   "Claude 4.7 Opus   270ms        $3.00            98.5/100 (Optimum)");
        await new Promise((r) => setTimeout(r, 150));
        appendLine("output", "Qwen 4.6          310ms        $5.00            97.0/100");
        await new Promise((r) => setTimeout(r, 150));
        appendLine("output", "Gemini 3.5 Pro    430ms        $7.00            94.8/100");
        await new Promise((r) => setTimeout(r, 150));
        appendLine("output", "GLM 5.1           480ms        $4.00            91.5/100");
        await new Promise((r) => setTimeout(r, 300));
        appendLine("success", "🏆 Recommendation: Route to Claude 4.7 Opus (Optimizes cost/speed ratio)");
        break;

      case "route":
        appendLine("output", "Simulating dynamic query routing…");
        await new Promise((r) => setTimeout(r, 400));
        appendLine("info", "[ROUTER] Ingress query: ‘Analyze scanned legal document (2.4M tokens)’");
        await new Promise((r) => setTimeout(r, 450));
        appendLine("info", "[ROUTER] Checking constraints: Context size > 2M, Cost budget < $20");
        await new Promise((r) => setTimeout(r, 450));
        appendLine("info", "[ROUTER] Checking candidates: Claude 4.7 Opus (budget fail), Qwen 4.6 (context limit fail)");
        await new Promise((r) => setTimeout(r, 500));
        appendLine("success", "✔ Route mapped: Gemini 3.5 Pro (2M context limit native)");
        await new Promise((r) => setTimeout(r, 300));
        appendLine("success", "✔ Query processed. Execution: 1.62s. Transaction Cost: $14.40");
        break;

      case "":
        break;

      default:
        appendLine("error", `Command not found: '${cmd}'. Type 'help' to see available commands.`);
    }

    setIsProcessing(false);
  };

  const handleCommandSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    if (isProcessing || !inputValue.trim()) return;
    const cmd = inputValue;
    setInputValue("");
    processCommand(cmd);
  };

  const [cpuLoad, setCpuLoad] = useState(12.8);

  useEffect(() => {
    const interval = setInterval(() => {
      setCpuLoad(Number((10 + Math.random() * 5).toFixed(1)));
    }, 3000);
    return () => clearInterval(interval);
  }, []);

  return (
    <div className="w-full max-w-3xl mx-auto my-8 md:my-16 px-4 relative">
      {/* Decorative background grid and vectors */}
      <div className="absolute -inset-10 pointer-events-none bg-radial from-primary/5 via-transparent to-transparent blur-2xl -z-10" />

      {/* Preset Command Buttons (Futuristic clip buttons) */}
      <div className="flex flex-wrap items-center justify-center gap-2 mb-6">
        {presets.map((preset, idx) => {
          const Icon = preset.icon;
          return (
            <button
              key={idx}
              onClick={() => simulateTyping(preset.cmd)}
              disabled={isProcessing}
              className="inverted-card flex items-center gap-1.5 px-4 py-2 text-[10px] md:text-xs font-mono border hover:bg-primary/10 hover:border-primary hover:text-primary hover:shadow-[0_0_15px_var(--primary-glow)] text-muted-foreground transition-[background-color,border-color,color,box-shadow,transform] duration-300 active:scale-95 disabled:opacity-50 disabled:pointer-events-none focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-primary clip-cyber-button"
            >
              <Icon className="w-3.5 h-3.5 text-primary" />
              <span>{preset.label}</span>
            </button>
          );
        })}
      </div>

      {/* Terminal Shell Window (Cyber Glass with Corner Brackets) */}
      <div className="w-full shadow-2xl overflow-hidden border border-border dark:border-primary/30 relative z-10 bg-card/95 dark:bg-card/95 backdrop-blur-xl transition-colors duration-300 corner-bracket p-0.5 rounded-none">
        <div className="corner-bracket-inner" />
        
        {/* Terminal Header */}
        <div className="flex items-center justify-between px-4 py-2.5 border-b border-primary/20 bg-primary/[0.04] dark:bg-primary/[0.02]">
          <div className="flex items-center gap-2">
            <div className="w-2 h-2 rounded-full bg-red-500/80 animate-pulse shadow-[0_0_6px_#ef4444]" />
            <div className="w-2 h-2 rounded-full bg-yellow-500/80 shadow-[0_0_6px_#eab308]" />
            <div className="w-2 h-2 rounded-full bg-green-500/80 shadow-[0_0_6px_#22c55e]" />
          </div>
          
          <div className="text-[10px] md:text-xs font-mono text-primary/80 uppercase tracking-widest flex items-center gap-1.5 font-bold">
            <TerminalIcon className="w-3.5 h-3.5 text-primary animate-pulse" />
            <span>HUBAPI Network Shell</span>
          </div>

          <div className="flex items-center gap-3">
            {/* Live CPU Telemetry Meter */}
            <div className="flex items-center gap-1.5 border-l border-primary/20 pl-3">
              <Cpu className="w-3 h-3 text-primary/60" />
              <span className="text-[9px] font-mono text-primary/70 hidden sm:block">CPU: {cpuLoad}%</span>
              <div className="w-8 h-1 bg-primary/10 border border-primary/20 rounded-full overflow-hidden hidden sm:block">
                <div className="h-full bg-primary transition-all duration-500" style={{ width: `${(cpuLoad / 20) * 100}%` }} />
              </div>
            </div>

            <div className="flex items-center gap-1.5">
              <span className="relative flex h-1.5 w-1.5">
                <span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75"></span>
                <span className="relative inline-flex rounded-full h-1.5 w-1.5 bg-emerald-500"></span>
              </span>
              <span className="text-[8px] font-mono text-emerald-500 tracking-wider">ONLINE</span>
            </div>
          </div>
        </div>

        {/* CRT Scanline Console Box */}
        <div className="relative w-full scanline">
          <div 
            ref={consoleContainerRef}
            className="p-4 h-[300px] overflow-y-auto flex flex-col gap-1.5 font-mono text-[11px] md:text-xs text-left scrollbar-thin scrollbar-thumb-primary/10 select-text bg-black/90 text-primary/90"
          >
            {history.map((line, idx) => {
              let colorClass = "text-primary/80";
              if (line.type === "input") colorClass = "text-primary font-bold shadow-[0_0_8px_rgba(255,170,64,0.3)]";
              if (line.type === "error") colorClass = "text-destructive font-semibold";
              if (line.type === "success") colorClass = "text-emerald-400 font-semibold shadow-[0_0_8px_rgba(52,211,153,0.2)]";
              if (line.type === "info") colorClass = "text-blue-400";
              if (line.type === "system") colorClass = "text-amber-500 font-bold shadow-[0_0_8px_rgba(245,158,11,0.2)]";
              if (line.type === "output") colorClass = "text-primary/70";

              return (
                <div key={idx} className={`leading-relaxed whitespace-pre-wrap ${colorClass}`}>
                  {line.text}
                </div>
              );
            })}
          </div>
        </div>

        {/* Command Interactive Prompt Form (Glowing Neon input) */}
        <form 
          onSubmit={handleCommandSubmit} 
          className="flex items-center gap-2 p-3 border-t border-primary/20 bg-primary/[0.02] font-mono text-xs md:text-sm relative"
        >
          <span className="text-primary font-bold ml-1 select-none animate-pulse">&gt;</span>
          <input
            id="terminal-cli-input"
            name="terminal_cli"
            aria-label="Interactive terminal CLI command input"
            type="text"
            value={inputValue}
            onChange={(e) => setInputValue(e.target.value)}
            disabled={isProcessing}
            placeholder={isProcessing ? "Executing command sequence…" : "Type command (e.g. status, benchmark) and hit Enter…"}
            className="flex-1 bg-transparent border-none outline-none text-primary placeholder:text-primary/30 select-text focus:ring-0 focus:outline-none"
          />
          <button
            type="submit"
            disabled={isProcessing || !inputValue.trim()}
            className="flex items-center gap-1.5 px-4 py-1.5 rounded-none bg-primary/10 text-primary border border-primary/40 text-[10px] font-mono hover:bg-primary/25 hover:shadow-[0_0_10px_rgba(255,170,64,0.2)] transition-[background-color,border-color,box-shadow,transform] duration-200 uppercase tracking-wider disabled:opacity-30 disabled:pointer-events-none active:scale-95 clip-cyber-button"
          >
            <Play className="w-3 h-3 fill-current" aria-hidden="true" />
            <span>Run</span>
          </button>
        </form>
      </div>
    </div>
  );
}