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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0da93e3
06a1a2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3f97db
 
0da93e3
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
#!/usr/bin/env python3
# app.py β€” CHB v7.1 Professional Frontend (CLI + API, animated splash, ChatGPT-style TUI)
# Requires: rich, fastapi, uvicorn
# Brain file expected: multimodular_modul_v7.py (same folder)

import os, sys, time, json, argparse, pathlib, base64, random, string, datetime, threading
from typing import Any, Dict, Tuple, Optional

# -------------------- Optional deps (graceful) --------------------
try:
    from rich.console import Console
    from rich.panel import Panel
    from rich.table import Table
    from rich.layout import Layout
    from rich.live import Live
    from rich.align import Align
    from rich.markdown import Markdown
    from rich.text import Text
    from rich.box import ROUNDED
except Exception as e:
    print("Missing 'rich'. Install: pip install rich")
    raise

try:
    from fastapi import FastAPI, Request, HTTPException
    import uvicorn
except Exception:
    FastAPI = None  # API disabled if FastAPI not installed

# -------------------- Load brain module --------------------
BRAIN_CANDIDATES = [
    "multimodular_modul_v7",      # provided by user
]
def load_brain():
    for name in BRAIN_CANDIDATES:
        try:
            mod = __import__(name)
            # Common exports
            if hasattr(mod, "AGENT"): return getattr(mod, "AGENT")
            if hasattr(mod, "agent"): return getattr(mod, "agent")
            for cls in ("SuperAgentV7","MultimodalBrain","SuperAgent","Agent","Brain"):
                if hasattr(mod, cls):
                    return getattr(mod, cls)()
            return mod
        except Exception:
            continue
    return None

BRAIN = load_brain()

def brain_call(fn_candidates, *args, **kwargs) -> Tuple[bool, Any]:
    """Try multiple function names on brain (and nested .chb)."""
    if BRAIN is None: return False, "Brain not loaded"
    # direct on brain
    for fn in fn_candidates:
        f = getattr(BRAIN, fn, None)
        if callable(f):
            try: return True, f(*args, **kwargs)
            except Exception as e: return False, f"{fn} error: {e}"
    # nested .chb
    chb = getattr(BRAIN, "chb", None)
    if chb:
        for fn in fn_candidates:
            f = getattr(chb, fn, None)
            if callable(f):
                try: return True, f(*args, **kwargs)
                except Exception as e: return False, f"chb.{fn} error: {e}"
    return False, f"Missing: {', '.join(fn_candidates)}"

# -------------------- API Keys & Limits --------------------
API_KEYS_FILE = "api_keys.json"
DAILY_LIMIT = 1000
API_LOCK = threading.Lock()
def _load_keys() -> Dict[str, Dict[str, Any]]:
    if os.path.exists(API_KEYS_FILE):
        try:
            return json.load(open(API_KEYS_FILE,"r"))
        except Exception:
            return {}
    return {}
def _save_keys(data: Dict[str, Any]):
    json.dump(data, open(API_KEYS_FILE,"w"), indent=2)
def _today() -> str:
    return datetime.date.today().isoformat()

def api_key_generate() -> str:
    with API_LOCK:
        data = _load_keys()
        key = ''.join(random.choices(string.ascii_letters + string.digits, k=25))
        data[key] = {"usage": 0, "last_reset": _today(), "created": datetime.datetime.utcnow().isoformat()+"Z"}
        _save_keys(data)
        return key

def api_key_revoke(key: str) -> bool:
    with API_LOCK:
        data = _load_keys()
        if key in data:
            del data[key]
            _save_keys(data)
            return True
        return False

def api_key_validate_and_count(key: str) -> bool:
    with API_LOCK:
        data = _load_keys()
        if key not in data: return False
        # reset day
        if data[key].get("last_reset") != _today():
            data[key]["usage"] = 0
            data[key]["last_reset"] = _today()
        if data[key]["usage"] >= DAILY_LIMIT: return False
        data[key]["usage"] += 1
        _save_keys(data)
        return True

def api_stats() -> Dict[str, Any]:
    with API_LOCK:
        data = _load_keys()
        return {
            "active_keys": len(data),
            "limit_per_day": DAILY_LIMIT,
            "keys": data
        }

# -------------------- FastAPI (shared brain) --------------------
APP = None
if FastAPI:
    APP = FastAPI(title="CHB Universal Brain API", version="7.1")

    @APP.post("/chat")
    async def api_chat(req: Request):
        key = req.headers.get("x-api-key")
        if not key or not api_key_validate_and_count(key):
            raise HTTPException(status_code=401, detail="Invalid key or limit reached")
        data = await req.json()
        msg = data.get("message","")
        ok, res = brain_call(["ctb_handle","process_input","chat","chat_message","handle_input"], input_data=msg)
        if not ok: raise HTTPException(status_code=500, detail=str(res))
        return {"reply": res}

    @APP.post("/search")
    async def api_search(req: Request):
        key = req.headers.get("x-api-key")
        if not key or not api_key_validate_and_count(key):
            raise HTTPException(status_code=401, detail="Invalid key or limit reached")
        q = (await req.json()).get("query","")
        ok, res = brain_call(["search_kb","facts_search","search_facts","query_facts"], q)
        if not ok: raise HTTPException(status_code=500, detail=str(res))
        return {"results": res}

    @APP.post("/upload")
    async def api_upload(req: Request):
        key = req.headers.get("x-api-key")
        if not key or not api_key_validate_and_count(key):
            raise HTTPException(status_code=401, detail="Invalid key or limit reached")
        data = await req.json()
        path = data.get("path")
        b64 = data.get("base64")
        tmp_path = None
        if b64:
            raw = base64.b64decode(b64)
            tmp_path = f"upload_{int(time.time())}.bin"
            open(tmp_path,"wb").write(raw)
            path = tmp_path
        if not path: raise HTTPException(status_code=400, detail="Provide 'path' or 'base64'")
        payload = _plan_results_for_path(path)
        ok, res = brain_call(["submit_plan_results","handle_plan_results","submit_plan"], plan_id=f"api_upload_{int(time.time())}", results=payload)
        if tmp_path and os.path.exists(tmp_path): os.remove(tmp_path)
        if not ok: raise HTTPException(status_code=500, detail=str(res))
        return {"status":"ok","details":res}

    @APP.get("/skills")
    def api_skills(tag: Optional[str] = None, top: int = 10):
        # no key required here? Make it protected too:
        raise HTTPException(status_code=405, detail="Use POST /skills with x-api-key")

    @APP.post("/skills")
    async def api_skills_post(req: Request):
        key = req.headers.get("x-api-key")
        if not key or not api_key_validate_and_count(key):
            raise HTTPException(status_code=401, detail="Invalid key or limit reached")
        data = await req.json()
        tag = data.get("tag","")
        top = int(data.get("top",10))
        if tag:
            ok, res = brain_call(["top_skills","top_by_tag"], tag, top)
        else:
            ok, res = brain_call(["list_skills","get_skills"])
        if not ok: raise HTTPException(status_code=500, detail=str(res))
        return {"skills": res}

    @APP.get("/backup")
    def api_backup_get():
        return {"detail":"Use POST /backup with x-api-key"}

    @APP.post("/backup")
    async def api_backup(req: Request):
        key = req.headers.get("x-api-key")
        if not key or not api_key_validate_and_count(key):
            raise HTTPException(status_code=401, detail="Invalid key or limit reached")
        ok, path = brain_call(["download_latest_backup","latest_backup","get_latest_backup"])
        if not ok or not path:
            ok2, created = brain_call(["backup_create","create_backup","create_backup_zip"])
            if not ok2: raise HTTPException(status_code=500, detail=str(created))
            path = created
        return {"backup_path": path}

    @APP.get("/status")
    def api_status():
        # Public status (no key): only meta
        meta = {"api": "online", "keys": api_stats()["active_keys"], "limit_per_day": DAILY_LIMIT}
        return meta

    @APP.get("/key/new")
    def api_key_new():
        return {"api_key": api_key_generate()}

    @APP.post("/key/revoke")
    async def api_key_revoke(req: Request):
        data = await req.json()
        key = data.get("api_key")
        if not key: raise HTTPException(status_code=400, detail="api_key required")
        ok = api_key_revoke(key)
        if not ok: raise HTTPException(status_code=404, detail="Key not found")
        return {"status":"revoked"}

# -------------------- CLI (ChatGPT-style TUI) --------------------
console = Console()

CHB_ASCII = """
 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•—  β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— 
β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—
β–ˆβ–ˆβ•‘     β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•
β–ˆβ–ˆβ•‘     β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—
β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•
 β•šβ•β•β•β•β•β•β•šβ•β•  β•šβ•β•β•šβ•β•β•β•β•β•
"""

def boot_splash():
    console.clear()
    panel = Panel.fit(Text(CHB_ASCII, style="bold cyan"), title="Close-to-Human Brain v7.1", border_style="cyan", padding=(1,4))
    console.print(panel, justify="center")
    steps = [
        "Initializing Universal Brain",
        "Loading Core Modules",
        "Connecting Global Sync",
        "Activating CTB Pipeline",
        "Preparing Creative Skill Vault"
    ]
    for s in steps:
        console.print(f"[cyan]β†’ {s}...[/cyan]")
        time.sleep(0.5)
    console.print("[bold green]βœ… Ready![/bold green]")
    time.sleep(0.6)
    console.clear()

def _layout() -> Layout:
    layout = Layout()
    layout.split(
        Layout(name="header", size=5),
        Layout(name="body", ratio=1),
        Layout(name="footer", size=3),
    )
    layout["body"].split_row(
        Layout(name="sidebar", size=26),
        Layout(name="chat", ratio=1)
    )
    return layout

SIDEBAR_ITEMS = [
    ("πŸ’¬", "Multimodal Chat"),
    ("πŸ“š", "Knowledge Search"),
    ("πŸ“€", "Upload"),
    ("πŸ’Ύ", "Backup/Restore"),
    ("🎨", "Creative Vault"),
    ("🌍", "Sync Status"),
    ("πŸ› ", "API Keys"),
    ("πŸ“΄", "Offline Mode")
]

CHAT_HISTORY = []

def status_bar() -> Text:
    stats = api_stats()
    sync_ok, sync = brain_call(["global_sync_status","sync_status","get_sync_status"])
    sync_txt = "Active" if (sync_ok and str(sync).lower().find("off")==-1) else "Idle"
    t = Text(f"🌐 API: {'ON' if APP else 'OFF'}   πŸ”‘ Keys: {stats.get('active_keys',0)}   πŸ“Š Sync: {sync_txt}   ⏳ Limit: {DAILY_LIMIT}/day")
    t.stylize("cyan")
    return t

def sidebar_panel(active: str) -> Panel:
    table = Table.grid(padding=(0,1))
    for icon, label in SIDEBAR_ITEMS:
        style = "bold white" if label==active else "grey70"
        table.add_row(Text(f"{icon} {label}", style=style))
    return Panel(table, title="CHB", border_style="cyan", box=ROUNDED)

def header_panel() -> Panel:
    return Panel(Text("CHB Universal Brain v7.1", style="bold cyan"), border_style="cyan", box=ROUNDED)

def chat_panel() -> Panel:
    md = ""
    for role, msg in CHAT_HISTORY[-100:]:
        if role=="user": md += f"**You:** {msg}\n\n"
        else: md += f"**CHB:** {msg}\n\n"
    if not md:
        md = "**CHB:** Hello! Type your message. Use `/help` for commands.\n"
    return Panel(Markdown(md), border_style="cyan", box=ROUNDED)

def footer_panel() -> Panel:
    return Panel(status_bar(), border_style="cyan", box=ROUNDED)

def _plan_results_for_path(path: str) -> Dict[str, Any]:
    p = pathlib.Path(path)
    suffix = p.suffix.lower()
    payload = {}
    if suffix in (".png",".jpg",".jpeg",".webp",".bmp"):
        payload["images"] = [{"path": str(p), "quality_score": 0.9, "caption": "", "tags": []}]
    elif suffix in (".mp4",".mov",".mkv",".webm"):
        payload["videos"] = [{"path": str(p), "quality_score": 0.8, "caption": "", "tags": []}]
    elif suffix in (".mp3",".wav",".m4a",".ogg"):
        payload["audios"] = [{"path": str(p), "quality_score": 0.8, "caption": "", "tags": []}]
    else:
        payload["files"] = [{"path": str(p), "meta": {"size": p.stat().st_size if p.exists() else 0}}]
    return payload

def run_cli():
    boot_splash()
    active = "Multimodal Chat"
    layout = _layout()
    layout["header"].update(header_panel())
    layout["sidebar"].update(sidebar_panel(active))
    layout["chat"].update(chat_panel())
    layout["footer"].update(footer_panel())

    with Live(layout, refresh_per_second=10, screen=True):
        while True:
            try:
                console.print()  # spacing under Live
                user = console.input("[bold blue]You β€Ί [/bold blue]").strip()
            except (KeyboardInterrupt, EOFError):
                break
            if not user: continue

            # Slash commands (stay chat-first; sidebar is thematic)
            if user == "/help":
                help_text = (
                    "**Commands**\n"
                    "β€’ /upload <path> β€” process media/file\n"
                    "β€’ /search <query> β€” knowledge search\n"
                    "β€’ /backup β€” create/show latest backup\n"
                    "β€’ /status β€” show global sync status\n"
                    "β€’ /keys new|revoke <key>|list β€” manage API keys\n"
                    "β€’ /offline β€” toggle offline mode\n"
                    "β€’ /clear β€” clear chat\n"
                    "β€’ /quit β€” exit\n"
                )
                CHAT_HISTORY.append(("chb", help_text))
                layout["chat"].update(chat_panel()); continue

            if user.startswith("/upload "):
                path = user.split(" ",1)[1].strip().strip('"').strip("'")
                payload = _plan_results_for_path(path)
                ok, res = brain_call(["submit_plan_results","handle_plan_results","submit_plan"], plan_id=f"cli_upload_{int(time.time())}", results=payload)
                CHAT_HISTORY.append(("user", user))
                CHAT_HISTORY.append(("chb", f"Upload processed: {res if ok else res}"))
                layout["chat"].update(chat_panel()); continue

            if user.startswith("/search "):
                q = user.split(" ",1)[1].strip()
                ok, res = brain_call(["search_kb","facts_search","search_facts","query_facts"], q)
                CHAT_HISTORY.append(("user", user))
                CHAT_HISTORY.append(("chb", f"Search results:\n{res if ok else res}"))
                layout["chat"].update(chat_panel()); continue

            if user == "/backup":
                ok, path = brain_call(["download_latest_backup","latest_backup","get_latest_backup"])
                if not (ok and path):
                    ok2, created = brain_call(["backup_create","create_backup","create_backup_zip"])
                    CHAT_HISTORY.append(("user","/backup"))
                    CHAT_HISTORY.append(("chb", f"Backup: {created if ok2 else path}"))
                else:
                    CHAT_HISTORY.append(("user","/backup"))
                    CHAT_HISTORY.append(("chb", f"Latest backup: {path}"))
                layout["chat"].update(chat_panel()); continue

            if user == "/status":
                ok, res = brain_call(["global_sync_status","sync_status","get_sync_status"])
                CHAT_HISTORY.append(("user","/status"))
                CHAT_HISTORY.append(("chb", f"Sync status: {res if ok else res}"))
                layout["footer"].update(footer_panel())
                layout["chat"].update(chat_panel()); continue

            if user.startswith("/keys"):
                parts = user.split()
                if len(parts)==2 and parts[1]=="new":
                    k = api_key_generate()
                    CHAT_HISTORY.append(("user", user))
                    CHAT_HISTORY.append(("chb", f"New API key: `{k}`"))
                elif len(parts)==3 and parts[1]=="revoke":
                    ok = api_key_revoke(parts[2])
                    CHAT_HISTORY.append(("user", user))
                    CHAT_HISTORY.append(("chb", "Key revoked" if ok else "Key not found"))
                else:
                    stats = api_stats()
                    CHAT_HISTORY.append(("user", user))
                    CHAT_HISTORY.append(("chb", f"Keys: {list(stats.get('keys',{}).keys())}"))
                layout["footer"].update(footer_panel())
                layout["chat"].update(chat_panel()); continue

            if user == "/offline":
                ok, res = brain_call(["toggle_offline","set_offline","offline_toggle"])
                CHAT_HISTORY.append(("user","/offline"))
                CHAT_HISTORY.append(("chb", f"Offline toggled: {res if ok else res}"))
                layout["footer"].update(footer_panel())
                layout["chat"].update(chat_panel()); continue

            if user == "/clear":
                CHAT_HISTORY.clear()
                layout["chat"].update(chat_panel()); continue

            if user in ("/quit","/exit"):
                break

            # Normal chat -> brain
            CHAT_HISTORY.append(("user", user))
            ok, resp = brain_call(["ctb_handle","process_input","chat","chat_message","handle_input"], input_data=user)
            CHAT_HISTORY.append(("chb", resp if ok else str(resp)))
            layout["chat"].update(chat_panel())

# -------------------- Entry --------------------
def main():
    parser = argparse.ArgumentParser(description="CHB v7.1 Frontend")
    parser.add_argument("--api", action="store_true", help="Run API server only")
    parser.add_argument("--cli", action="store_true", help="Run CLI only")
    parser.add_argument("--both", action="store_true", help="Run CLI and API together")
    parser.add_argument("--host", default="0.0.0.0")
    parser.add_argument("--port", type=int, default=8000)
    args = parser.parse_args()

    if args.api and not FastAPI:
        console.print("[red]FastAPI/uvicorn not installed. Run: pip install fastapi uvicorn[/red]")
        sys.exit(1)

    if args.api and not args.cli and not args.both:
        # API only
        if APP is None:
            console.print("[red]API unavailable.[/red]"); sys.exit(1)
        uvicorn.run(APP, host=args.host, port=args.port)
        return

    if args.both and APP:
        # Run API in background thread + CLI
        def run_api():
            uvicorn.run(APP, host=args.host, port=args.port, log_level="warning")
        t = threading.Thread(target=run_api, daemon=True)
        t.start()
        run_cli()
        return

    # default: CLI only
    run_cli()

if __name__ == "__main__":
    main()