134 lines
5.0 KiB
Python
134 lines
5.0 KiB
Python
import os, sys, threading, time
|
|
import signal
|
|
|
|
import config
|
|
|
|
from services.xmpp_client import XMPPClient
|
|
|
|
from scripts.llm_client import LLMClient
|
|
from tools import coder, rag, carrack
|
|
from scripts import gadget
|
|
from scripts.persona import build_system_prompt
|
|
|
|
tools_definition = [
|
|
|
|
gadget.tools_mapping( schema = coder.schema_read_file, handler = coder.read_file ),
|
|
gadget.tools_mapping( schema = coder.schema_write_file, handler = coder.write_file ),
|
|
gadget.tools_mapping( schema = coder.schema_edit_file, handler = coder.edit_file ),
|
|
gadget.tools_mapping( schema = coder.schema_run_bash, handler = coder.run_bash ),
|
|
gadget.tools_mapping( schema = coder.schema_search_code, handler = coder.search_code ),
|
|
gadget.tools_mapping( schema = coder.schema_git_operation, handler = coder.git_operation ),
|
|
|
|
gadget.tools_mapping( schema = rag.schema_ingest_files, handler = rag.ingest_files ),
|
|
gadget.tools_mapping( schema = rag.schema_store_knowledge, handler = rag.store_knowledge ),
|
|
gadget.tools_mapping( schema = rag.schema_search_knowledge, handler = rag.search_knowledge ),
|
|
gadget.tools_mapping( schema = rag.schema_create_collection, handler = rag.create_collection ),
|
|
gadget.tools_mapping( schema = rag.schema_delete_collection, handler = rag.delete_collection ),
|
|
gadget.tools_mapping( schema = rag.schema_list_collections, handler = rag.list_collections ),
|
|
gadget.tools_mapping( schema = rag.schema_inspect_collection, handler = rag.inspect_collection ),
|
|
|
|
gadget.tools_mapping( schema = carrack.schema_sendhttprequest, handler = carrack.sendhttprequest ),
|
|
|
|
]
|
|
|
|
TOOLS = gadget.tool_schemas (tools_definition)
|
|
TOOL_HANDLERS = gadget.tool_handlers (tools_definition)
|
|
|
|
|
|
def main():
|
|
llm_client = LLMClient(config.llm_baseurl, config.llm_model, config.llm_api_key, config.llm_timeout)
|
|
|
|
workspace = None
|
|
i = 1
|
|
while i < len(sys.argv):
|
|
if sys.argv[i] in ('-w', '--workspace') and i + 1 < len(sys.argv):
|
|
workspace = sys.argv[i + 1]
|
|
i += 2
|
|
else:
|
|
i += 1
|
|
|
|
if workspace:
|
|
resolved = os.path.abspath(workspace)
|
|
if not os.path.isdir(resolved):
|
|
print(f"Error: '{resolved}' is not a valid directory")
|
|
sys.exit(1)
|
|
os.chdir(resolved)
|
|
|
|
services = []
|
|
|
|
if config.XMPP_ENABLED:
|
|
muc_rooms = []
|
|
if config.XMPP_MUC_ROOMS.strip():
|
|
muc_rooms = [r.strip() for r in config.XMPP_MUC_ROOMS.split(',') if r.strip()]
|
|
client = XMPPClient(
|
|
jid = config.XMPP_USERNAME,
|
|
password = config.XMPP_PASSWORD,
|
|
llm_client = llm_client,
|
|
tools_definition = tools_definition,
|
|
TOOLS = TOOLS,
|
|
TOOL_HANDLERS = TOOL_HANDLERS,
|
|
build_system_prompt = build_system_prompt,
|
|
agent_max_iterations = config.AGENT_MAX_ITERATIONS,
|
|
muc_rooms = muc_rooms,
|
|
)
|
|
services.append(client)
|
|
|
|
if config.TELEGRAM_ENABLED:
|
|
from services.telegram_client import TelegramClient
|
|
|
|
allowed_ids = []
|
|
if config.TELEGRAM_ALLOWED_GROUP_IDS.strip():
|
|
allowed_ids = [r.strip() for r in config.TELEGRAM_ALLOWED_GROUP_IDS.split(',') if r.strip()]
|
|
|
|
tg = TelegramClient(
|
|
token = config.TELEGRAM_TOKEN,
|
|
llm_client = llm_client,
|
|
tools_definition = tools_definition,
|
|
TOOLS = TOOLS,
|
|
TOOL_HANDLERS = TOOL_HANDLERS,
|
|
build_system_prompt = build_system_prompt,
|
|
agent_max_iterations = config.AGENT_MAX_ITERATIONS,
|
|
allowed_group_ids = allowed_ids,
|
|
)
|
|
services.append(tg)
|
|
|
|
if services:
|
|
threads = []
|
|
for svc in services:
|
|
t = threading.Thread(target=svc.start, daemon=True)
|
|
t.start()
|
|
threads.append(t)
|
|
|
|
_shutdown = False
|
|
|
|
def _handle_sig(signum, frame):
|
|
nonlocal _shutdown
|
|
print("\nShutting down...")
|
|
for svc in services:
|
|
svc.stop()
|
|
_shutdown = True
|
|
|
|
signal.signal(signal.SIGTERM, _handle_sig)
|
|
signal.signal(signal.SIGINT, _handle_sig)
|
|
|
|
try:
|
|
while not _shutdown:
|
|
time.sleep(1)
|
|
except KeyboardInterrupt:
|
|
_shutdown = True
|
|
print("Exiting.")
|
|
else:
|
|
from tui import HendrikTUI
|
|
HendrikTUI(
|
|
llm_client = llm_client,
|
|
tools_definition = tools_definition,
|
|
TOOLS = TOOLS,
|
|
TOOL_HANDLERS = TOOL_HANDLERS,
|
|
build_system_prompt = build_system_prompt,
|
|
agent_max_iterations = config.AGENT_MAX_ITERATIONS,
|
|
).run()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|