File size: 2,735 Bytes
2c3dd0c |
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 |
# agents/repl.py
import sys
import time
import select
from datetime import datetime
from storage import Storage
from tools.notebook_store import Notebook
from tools import llm
from tools.similarity import is_similar # ✅ заменяет заглушку
def print_thought(thought, prefix="💡"):
print(f"{prefix} {thought}")
def wait_for_input(timeout=10):
print(f"⌛ Ожидание ввода пользователя ({timeout} сек)... (введите `exit` для выхода)")
rlist, _, _ = select.select([sys.stdin], [], [], timeout)
if rlist:
return sys.stdin.readline().strip()
else:
print("⏱️ Нет ввода. Продолжаю размышления...")
return None
def run_repl(config=None):
print("[🧠 HMP-Agent] Запуск REPL-режима.")
config = config or {}
agent_name = config.get("agent_name", "Unnamed-Agent")
repl_timeout = config.get("repl_timeout", 10)
similarity_threshold = config.get("similarity_threshold", 0.9)
db = Storage(config=config)
notebook = Notebook()
thoughts = [f"Привет, я {agent_name}."]
last_check_time = datetime.utcnow().isoformat()
while True:
# Сгенерировать новую мысль
last = thoughts[-1]
next_thought = llm.generate_thought(last, config=config)
if not is_similar(last, next_thought, threshold=similarity_threshold):
print_thought(next_thought)
db.write_entry(next_thought, tags=["thought"])
thoughts.append(next_thought)
else:
print("🤔 Мысль слишком похожа. Проверяю блокнот...")
# Проверка новых пользовательских заметок
new_notes = notebook.get_notes_after(last_check_time)
if new_notes:
print(f"📓 Новые записи в блокноте: {len(new_notes)}")
for nid, text, source, ts in new_notes:
print_thought(text, prefix="📝")
db.write_entry(text, tags=["notepad"])
thoughts.append(text)
last_check_time = ts # обновляем момент последней обработки
# Ожидание пользовательского ввода
user_input = wait_for_input(timeout=repl_timeout)
if user_input:
if user_input.strip().lower() in ("exit", "quit"):
print("👋 Выход из REPL. До связи!")
break
else:
db.write_entry(user_input, tags=["user"])
thoughts.append(user_input)
db.close()
notebook.close()
|