-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
701 lines (622 loc) · 31.7 KB
/
server.py
File metadata and controls
701 lines (622 loc) · 31.7 KB
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
694
695
696
697
698
699
700
701
#!/usr/bin/env python3
"""Semplice server per nano-ui.
Fornisce:
- static file (index.html, style.css, app.js, settings.js)
- endpoint API /api/v1/agent che esegue `nanobot agent --message` per ottenere una risposta.
- endpoint API /api/v1/config per leggere/scrivere config.json.
- endpoint API /api/v1/sessions per gestire la cronologia.
- endpoint API /api/v1/restart per il riavvio globale.
- endpoint API /api/v1/telegram/webhook per ricevere messaggi da Telegram e inoltrarli a nanobot.
Token e allowFrom vengono letti da config.json (channels.telegram) ad ogni richiesta.
"""
from __future__ import annotations
import json
import os
import subprocess
import sys
import time
import glob
import logging
import threading
import urllib.request
import urllib.error
from http import HTTPStatus
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from urllib.parse import urlparse
# Setup logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
WORKDIR = Path(__file__).resolve().parent
DEFAULT_CONFIG = WORKDIR / "config.json"
CONFIG_PATH = Path(os.environ.get("NANO_CONFIG_PATH", "/app/config.json"))
if not CONFIG_PATH.exists() and DEFAULT_CONFIG.exists():
CONFIG_PATH = DEFAULT_CONFIG
SESSIONS_DIR = Path(os.environ.get("NANO_SESSIONS_DIR", str(WORKDIR / "sessions")))
SESSIONS_DIR.mkdir(exist_ok=True, parents=True)
# Global process tracking for stop feature
_active_proc: subprocess.Popen | None = None
_proc_lock = threading.Lock()
# ── Telegram config helpers ──────────────────────────────────────────────────
def _load_telegram_config() -> tuple[str, list[str]]:
"""Read token and allowFrom from config.json channels.telegram.
Matches nanobot's native config format:
{
"channels": {
"telegram": {
"enabled": true,
"token": "123456:ABC...",
"allowFrom": ["987654321", "111222333"] // optional
}
}
}
Returns (token, allow_from_list). token may be empty string;
allow_from_list is an empty list if not set (= allow all).
"""
try:
config = json.loads(CONFIG_PATH.read_text(encoding="utf-8"))
tg = config.get("channels", {}).get("telegram", {})
token = str(tg.get("token") or "").strip()
raw_allow = tg.get("allowFrom") or []
allow_from = [str(x).strip() for x in raw_allow if str(x).strip()]
return token, allow_from
except Exception as e:
logger.warning(f"Could not read Telegram config from config.json: {e}")
return "", []
def _telegram_send(chat_id: str | int, text: str, bot_token: str) -> None:
"""Send a text message to a Telegram chat via Bot API."""
if not bot_token:
logger.warning("bot_token empty, cannot send Telegram reply")
return
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
# Telegram messages max 4096 chars — split if needed
chunks = [text[i:i + 4096] for i in range(0, max(len(text), 1), 4096)]
for chunk in chunks:
payload = json.dumps({"chat_id": chat_id, "text": chunk, "parse_mode": "Markdown"}).encode("utf-8")
req = urllib.request.Request(url, data=payload, headers={"Content-Type": "application/json"}, method="POST")
try:
with urllib.request.urlopen(req, timeout=10) as resp:
logger.debug(f"Telegram sendMessage status: {resp.status}")
except urllib.error.URLError as e:
logger.error(f"Telegram sendMessage failed: {e}")
def _run_nanobot(message: str, session: str) -> str:
"""Run nanobot agent via docker exec and return the clean response text."""
cmd = [
"docker", "exec", "-i", "nanobot",
"/usr/local/bin/python", "-m", "nanobot", "agent",
"-m", message, "--session", session, "--no-logs"
]
env = os.environ.copy()
env["PYTHONUNBUFFERED"] = "1"
skip_prefixes = ("Hint:", "`memoryWindow`", "\U0001f408 nanobot", "\U0001f43e nanobot")
try:
proc = subprocess.run(cmd, capture_output=True, text=True, timeout=120, env=env)
lines = []
for line in proc.stdout.splitlines():
stripped = line.strip()
if not stripped:
continue
if any(stripped.startswith(p) for p in skip_prefixes):
continue
# Only treat as reasoning if it starts with the specific nanobot prefix
markers = ["\u21b3", "\u2192", "Thinking...", "Tool", "Calling", "Running", "Analyzing"]
is_reasoning = any(line.lstrip().startswith(m) for m in markers) and line.startswith(" ")
if is_reasoning:
continue
lines.append(line.rstrip())
result = "\n".join(lines).strip()
if proc.returncode != 0 and not result:
result = f"Errore nanobot (exit {proc.returncode})"
return result or "(nessuna risposta)"
except subprocess.TimeoutExpired:
return "Timeout: nanobot non ha risposto in tempo."
except Exception as e:
logger.exception("_run_nanobot failed")
return f"Errore interno: {e}"
class NanoUIHandler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=str(WORKDIR), **kwargs)
def _parse_request_body(self):
try:
length = int(self.headers.get("Content-Length", "0"))
if length == 0:
logger.warning("Empty body or missing Content-Length")
return {}
body = self.rfile.read(length)
decoded = body.decode("utf-8")
logger.debug(f"Parsed body: {decoded}")
return json.loads(decoded)
except Exception as e:
logger.error(f"Error parsing request body: {e}")
return {"error": "invalid json", "detail": str(e)}
def do_GET(self):
parsed = urlparse(self.path)
logger.debug(f"GET {parsed.path}")
if parsed.path == "/api/v1/config":
return self._handle_config_get()
return super().do_GET()
def do_POST(self):
try:
parsed = urlparse(self.path)
logger.debug(f"POST {parsed.path}")
if parsed.path == "/api/v1/agent":
self._handle_single()
elif parsed.path == "/api/v1/config":
self._handle_config_post()
elif parsed.path == "/api/v1/restart":
self._handle_restart()
elif parsed.path == "/api/v1/stop":
self._handle_stop()
elif parsed.path == "/api/v1/context":
self._handle_context()
elif parsed.path == "/api/v1/telegram/webhook":
self._handle_telegram_webhook()
elif parsed.path == "/api/v1/sessions":
data = self._parse_request_body()
action = data.get("action")
logger.debug(f"Session action: {action}")
if action == "list":
self._handle_sessions_list()
elif action == "create":
self._handle_sessions_create(data)
elif action == "load":
self._handle_sessions_load(data.get("id"))
elif action == "save":
self._handle_sessions_save(data.get("id"), data.get("messages"), data.get("title"))
elif action == "delete":
self._handle_sessions_delete(data.get("id"))
elif action == "compact":
self._handle_sessions_compact(data.get("id"), data.get("messages"))
else:
logger.warning(f"Unknown session action: {action}")
self.send_error(HTTPStatus.BAD_REQUEST, f"Unknown session action: {action}")
else:
logger.warning(f"Path not found: {parsed.path}")
self.send_error(HTTPStatus.NOT_FOUND, f"API endpoint not found: {parsed.path}")
except Exception as e:
logger.exception("Internal error in do_POST")
self._send_json({"ok": False, "error": "internal", "detail": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
# ── Telegram webhook ────────────────────────────────────────────────────
def _handle_telegram_webhook(self):
"""Receive a Telegram Update, forward the message to nanobot, reply.
Config is read live from config.json — no restart needed when
token or allowFrom change.
Telegram expects 200 OK quickly; processing runs in a background thread.
"""
token, allow_from = _load_telegram_config()
if not token:
logger.warning("Telegram webhook received but channels.telegram.token is not configured")
return self._send_json({"ok": False, "error": "Telegram token not configured in config.json"})
update = self._parse_request_body()
if not update or "error" in update:
return self._send_json({"ok": False, "error": "invalid update"})
# Acknowledge immediately
self._send_json({"ok": True})
# Process in background
t = threading.Thread(
target=self._process_telegram_update,
args=(update, token, allow_from),
daemon=True,
)
t.start()
def _process_telegram_update(self, update: dict, token: str, allow_from: list[str]) -> None:
"""Background: extract message text, run nanobot, reply to Telegram."""
try:
message = update.get("message") or update.get("edited_message")
if not message:
logger.debug("Telegram update has no message, skipping")
return
chat_id = message.get("chat", {}).get("id")
text = message.get("text", "").strip()
if not chat_id or not text:
logger.debug("Telegram message missing chat_id or text, skipping")
return
# allowFrom whitelist — empty list means allow all
if allow_from and str(chat_id) not in allow_from:
logger.warning(f"Telegram message from unauthorized chat_id {chat_id}, ignoring")
return
session_key = f"telegram_{chat_id}"
logger.info(f"Telegram [{chat_id}] -> nanobot: {text[:80]}")
response = _run_nanobot(text, session_key)
logger.info(f"Telegram [{chat_id}] <- nanobot: {response[:80]}")
_telegram_send(chat_id, response, token)
except Exception:
logger.exception("_process_telegram_update failed")
# ── Existing handlers ────────────────────────────────────────────────────
def _handle_stop(self):
global _active_proc
try:
with _proc_lock:
if _active_proc is not None:
_active_proc.kill()
_active_proc = None
logger.info("Active generation process killed via /stop")
return self._send_json({"ok": True, "killed": True})
return self._send_json({"ok": True, "killed": False, "message": "No active process"})
except Exception as e:
logger.error(f"Stop failed: {e}")
return self._send_json({"ok": False, "error": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
def _handle_context(self):
try:
data = self._parse_request_body()
session_id = data.get("session")
if not session_id:
return self._send_json({"error": "missing session id"}, status=HTTPStatus.BAD_REQUEST)
parts = []
# 1. Potential workspace roots (local and container)
# Prioritize the mounted path /app/workspace/
roots = ["/app/workspace/", "/home/ubuntu/lab/", "/root/", "/"]
# Additional subdirectory to search for memory files
memory_subdirs = ["memory/", ".nanobot/workspace/memory/", ".nanobot/memory/"]
memory_files = [
"SOUL.md", "AGENTS.md", "USER.md", "MEMORY.md", "TOOLS.md", "HISTORY.md"
]
bootstrap_files = [
"bootstrap.md", "SYSTEM.md", "CONTEXT.md"
]
# Helper to try cat across multiple roots
context_errors = []
def try_cat(filename):
# We try both local reading (for mounted volumes) and docker exec (fallback)
for root in roots:
for subdir in [""] + memory_subdirs:
path_str = os.path.join(root, subdir, filename).replace("\\", "/")
if not path_str.startswith("/"): path_str = "/" + path_str
path = Path(path_str)
# A. Try Local Read (Fastest, works for mounted /app/workspace)
try:
if path.exists() and path.is_file():
content = path.read_text(encoding="utf-8").strip()
if content and not content.startswith("Hint:"):
return content
except Exception: pass
# B. Try Docker Exec fallback
cmd = ["docker", "exec", "nanobot", "cat", path_str]
try:
proc = subprocess.run(cmd, capture_output=True, text=True, timeout=2)
if proc.returncode == 0 and proc.stdout.strip():
out = proc.stdout.strip()
if not out.startswith("Hint:"): return out
if proc.stderr: context_errors.append(f"{path_str} (docker): {proc.stderr.strip()}")
except Exception as e:
context_errors.append(f"{path_str} (docker): {e}")
return None
# Collect all unique files
all_files = list(dict.fromkeys(bootstrap_files + memory_files))
for f in all_files:
content = try_cat(f)
if content:
# If the file path is long, just show the basename for cleaner display
display_name = os.path.basename(f)
parts.append(f"## {display_name}\n\n{content}")
# 3. Read Session messages
fpath = SESSIONS_DIR / f"{session_id}.jsonl"
if fpath.exists():
lines = fpath.read_text(encoding="utf-8").splitlines()
messages = []
for line in lines:
if not line.strip(): continue
try:
m = json.loads(line)
if m.get("_type") == "metadata": continue
r = m.get("role", "unknown").upper()
c = m.get("content", "")
messages.append(f"[{r}] {c}")
except Exception: pass
if messages:
parts.append("## Current Session\n" + "\n".join(messages))
if not parts:
# If nothing found, provide a debug hint for the user
roots_str = ", ".join(roots)
err_str = "\n".join(context_errors[:10]) # Show first 10 errors
parts.append(f"## Debug Info (No files found)\n"
f"Searched roots: {roots_str}\n"
f"Container name used: nanobot\n"
f"Errors encountered:\n{err_str}")
raw_context = "\n\n".join(parts)
# Simple word count approximation for tokens (1 word ~ 1.3 tokens)
word_count = len(raw_context.split())
approx_tokens = int(word_count * 1.3)
return self._send_json({
"ok": True,
"raw_context": raw_context,
"approx_tokens": approx_tokens
})
except Exception as e:
logger.error(f"Context failed: {e}")
return self._send_json({"ok": False, "error": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
def _handle_restart(self):
try:
logger.info("Restarting system...")
subprocess.run(["docker", "restart", "nanobot"], check=True)
self._send_json({"ok": True, "message": "System restarting..."})
logger.info("Self-restarting UI server...")
os.execv(sys.executable, [sys.executable] + sys.argv)
except Exception as e:
logger.error(f"Restart failed: {e}")
self._send_json({"ok": False, "error": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
def _handle_config_get(self):
try:
config = json.loads(CONFIG_PATH.read_text(encoding="utf-8"))
return self._send_json({"ok": True, "config": config})
except FileNotFoundError:
return self._send_json({"ok": False, "error": "config not found"}, status=HTTPStatus.NOT_FOUND)
except Exception as e:
return self._send_json({"ok": False, "error": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
def _handle_config_post(self):
data = self._parse_request_body()
config_payload = data.get("config")
if not isinstance(config_payload, dict):
return self._send_json({"error": "config must be an object"}, status=HTTPStatus.BAD_REQUEST)
try:
CONFIG_PATH.write_text(json.dumps(config_payload, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
return self._send_json({"ok": True})
except Exception as e:
return self._send_json({"ok": False, "error": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
def _handle_sessions_list(self):
try:
pattern = str(SESSIONS_DIR / "webui_*.jsonl")
files = glob.glob(pattern)
sessions = []
for fpath in files:
f = Path(fpath)
session_id = f.stem
title_path = SESSIONS_DIR / f"{session_id}.title"
title = None
if title_path.exists():
try:
title = title_path.read_text(encoding="utf-8").strip()
except: pass
if not title:
title = session_id[:20]
try:
lines = f.read_text(encoding="utf-8").splitlines()
for line in lines:
if not line.strip(): continue
msg = json.loads(line)
if msg.get("role") == "system" and msg.get("type") == "metadata":
continue
if msg.get("role") == "user":
content = msg.get("content", "")
title = content[:50] + "..." if len(content) > 50 else content
break
except: pass
mtime = int(f.stat().st_mtime * 1000)
sessions.append({"id": session_id, "title": title or "New chat", "updatedAt": mtime})
sessions.sort(key=lambda x: x["updatedAt"], reverse=True)
self._send_json({"sessions": sessions})
except Exception as e:
logger.error(f"Sessions list failed: {e}")
self._send_json({"error": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
def _handle_sessions_create(self, data):
session_id = f"webui_{int(time.time() * 1000000)}"
fpath = SESSIONS_DIR / f"{session_id}.jsonl"
fpath.write_text("", encoding="utf-8")
self._send_json({"id": session_id, "title": "New chat", "updatedAt": int(time.time() * 1000)})
def _handle_sessions_load(self, session_id):
fpath = SESSIONS_DIR / f"{session_id}.jsonl"
if not fpath.exists():
return self._send_json({"messages": []})
try:
lines = fpath.read_text(encoding="utf-8").splitlines()
messages = []
for line in lines:
if not line.strip():
continue
m = json.loads(line)
if not m or not isinstance(m, dict):
continue
if m.get("role") == "system" and m.get("type") == "metadata":
continue
messages.append(m)
self._send_json({"messages": messages})
except Exception as e:
logger.error(f"Failed to load session {session_id}: {e}")
self._send_json({"error": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
def _handle_sessions_save(self, session_id, messages, title=None):
fpath = SESSIONS_DIR / f"{session_id}.jsonl"
try:
if title:
title_path = SESSIONS_DIR / f"{session_id}.title"
title_path.write_text(title, encoding="utf-8")
with open(fpath, "w", encoding="utf-8") as f:
if messages:
for m in messages:
if not m or not isinstance(m, dict):
continue
if m.get("role") == "system" and m.get("type") == "metadata":
continue
f.write(json.dumps(m, ensure_ascii=False) + "\n")
self._send_json({"ok": True})
except Exception as e:
self._send_json({"error": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
def _handle_sessions_delete(self, session_id):
fpath = SESSIONS_DIR / f"{session_id}.jsonl"
if fpath.exists(): fpath.unlink()
title_path = SESSIONS_DIR / f"{session_id}.title"
if title_path.exists(): title_path.unlink()
self._send_json({"ok": True})
def _handle_sessions_compact(self, session_id, messages=None):
"""Archive a session into memory/HISTORY.md and clean up."""
if messages is None:
fpath = SESSIONS_DIR / f"{session_id}.jsonl"
if not fpath.exists():
return self._send_json({"error": "session not found"}, status=HTTPStatus.NOT_FOUND)
try:
lines = fpath.read_text(encoding="utf-8").splitlines()
messages = [json.loads(line) for line in lines if line.strip()]
except Exception as e:
logger.exception("Error reading session file for compaction")
return self._send_json({"error": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
else:
if not isinstance(messages, list):
return self._send_json({"error": "messages must be an array"}, status=HTTPStatus.BAD_REQUEST)
if not messages:
fpath = SESSIONS_DIR / f"{session_id}.jsonl"
if fpath.exists():
try:
fpath.unlink()
except Exception:
pass
return self._send_json({"ok": True, "message": "Empty session deleted"})
title = session_id
if messages and isinstance(messages[0], dict) and messages[0].get("role") == "user":
content = messages[0].get("content", "")
title = content[:50] + "..." if len(content) > 50 else content
summary = ""
try:
logger.info(f"Generating synthesis for session {session_id}...")
temp_file_created = False
fpath = SESSIONS_DIR / f"{session_id}.jsonl"
if not fpath.exists():
fpath.write_text("\n".join(json.dumps(m, ensure_ascii=False) for m in messages) + "\n", encoding="utf-8")
temp_file_created = True
summary_cmd = [
"docker", "exec", "-i", "nanobot",
"/usr/local/bin/python", "-m", "nanobot", "agent",
"-m", "Provide a extremely concise one-sentence summary (synthesis) of our conversation for the archival log. No noise, just the summary.",
"--session", session_id, "--no-logs"
]
proc = subprocess.run(summary_cmd, capture_output=True, text=True, timeout=20)
if proc.returncode == 0:
raw_sum = proc.stdout.strip()
skip_prefixes = ("Hint:", "`memoryWindow`", "\U0001f408 nanobot", "\U0001f43e nanobot")
sum_lines = []
for sl in raw_sum.splitlines():
s_strip = sl.strip()
if not s_strip or any(s_strip.startswith(p) for p in skip_prefixes): continue
sum_lines.append(sl)
summary = "\n".join(sum_lines).strip()
logger.debug(f"Synthesis result: {summary}")
if temp_file_created and fpath.exists():
fpath.unlink()
except Exception as se:
logger.error(f"Synthesis failed: {se}")
timestamp = time.strftime('%Y-%m-%d %H:%M')
entry_lines = [f"[{timestamp}] Compacted session: {title}"]
if summary:
entry_lines.append(f" [SYNTHESIS] {summary}")
for m in messages:
if not isinstance(m, dict):
continue
role = m.get("role", "unknown").upper()
content = m.get("content", "")
for line in (content.splitlines() if isinstance(content, str) else [str(content)]):
entry_lines.append(f" [{role}] {line}")
entry_text = "\n".join(entry_lines) + "\n\n"
memory_history = SESSIONS_DIR.parent / "memory" / "HISTORY.md"
memory_history.parent.mkdir(parents=True, exist_ok=True)
existing = ""
if memory_history.exists():
existing = memory_history.read_text(encoding="utf-8")
try:
memory_history.write_text(entry_text + existing, encoding="utf-8")
except Exception as e:
logger.exception("Failed to write to HISTORY.md")
return self._send_json({"error": str(e)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
fpath = SESSIONS_DIR / f"{session_id}.jsonl"
if fpath.exists():
try:
fpath.unlink()
except Exception:
pass
self._send_json({"ok": True, "archived_to": str(memory_history), "summary": summary})
def _handle_single(self):
data = self._parse_request_body()
inp = data.get("input")
session = data.get("session") or "webui_direct"
if not inp: return self._send_json({"error": "missing input"}, status=HTTPStatus.BAD_REQUEST)
self.send_response(HTTPStatus.OK)
self.send_header("Content-Type", "text/event-stream")
self.send_header("Cache-Control", "no-cache")
self.send_header("Connection", "keep-alive")
self.end_headers()
cmd = [
"docker", "exec", "-i", "nanobot",
"/usr/local/bin/python", "-m", "nanobot", "agent",
"-m", inp, "--session", session, "--no-logs"
]
def send_event(event_type, payload):
msg = f"event: {event_type}\ndata: {json.dumps(payload, ensure_ascii=False)}\n\n"
self.wfile.write(msg.encode("utf-8"))
self.wfile.flush()
try:
env = os.environ.copy()
env["PYTHONUNBUFFERED"] = "1"
global _active_proc
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, env=env) as proc:
with _proc_lock:
_active_proc = proc
try:
skip_prefixes = ("Hint:", "`memoryWindow`", "\U0001f408 nanobot", "\U0001f43e nanobot")
content_lines = []
is_capturing_content = False
for line in proc.stdout:
logger.debug(f"NANOBOT-OUT: {repr(line)}")
stripped = line.strip()
if not stripped:
if is_capturing_content:
content_lines.append("")
send_event("chunk", {"content": "\n"})
continue
if any(stripped.startswith(p) for p in skip_prefixes):
continue
is_progress = False
if not is_capturing_content:
# Strict reasoning markers: must have spaces AND match the nanobot output style
markers = ["\u21b3", "\u2192", "Thinking...", "Tool", "Calling", "Running", "Analyzing"]
# Only treat as progress if it actually looks like reasoning (indented by nanobot)
if (line.startswith(" ") and not stripped.startswith("```")):
if any(stripped.startswith(m) for m in markers) or "thinking..." in stripped.lower():
is_progress = True
if is_progress:
clean = stripped
for sym in ["\u21b3", "\u2192"]: clean = clean.replace(sym, "")
send_event("progress", {"content": clean.strip() or stripped})
continue
is_capturing_content = True
content_lines.append(line.rstrip())
send_event("chunk", {"content": line})
proc.wait()
if proc.returncode != 0:
send_event("error", {"message": f"nanobot failed ({proc.returncode})"})
while content_lines and not content_lines[0].strip(): content_lines.pop(0)
while content_lines and not content_lines[-1].strip(): content_lines.pop()
result = "\n".join(content_lines).strip()
if result: send_event("final", {"result": result})
self.wfile.write(b"event: end\ndata: {\"done\":true}\n\n")
self.wfile.flush()
self.close_connection = 1
finally:
with _proc_lock:
if _active_proc is proc:
_active_proc = None
except Exception as e:
logger.exception("Error in SSE stream")
send_event("error", {"message": str(e)})
self.close_connection = 1
def _send_json(self, payload: dict, status: int = HTTPStatus.OK) -> None:
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def main(port: int = 18790):
server = ThreadingHTTPServer(("0.0.0.0", port), NanoUIHandler)
logger.info(f"nano-ui server running on http://localhost:{port}")
token, allow_from = _load_telegram_config()
if token:
allow_info = f"allowFrom={allow_from}" if allow_from else "allowFrom=all"
logger.info(f"Telegram bridge enabled ({allow_info}). Config from config.json.")
logger.info("Register webhook: https://api.telegram.org/bot<TOKEN>/setWebhook?url=https://<YOUR_DOMAIN>/api/v1/telegram/webhook")
else:
logger.info("Telegram bridge disabled (channels.telegram.token not set in config.json)")
try: server.serve_forever()
except KeyboardInterrupt: pass
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, default=8000)
args = parser.parse_args()
main(port=args.port)