#!/usr/bin/env python3
import os
import sys
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr

HERMES_ROOT = os.path.expanduser('/Users/apayne/.hermes/hermes-agent')
sys.path.insert(0, HERMES_ROOT)
os.chdir(HERMES_ROOT)
from cli import main as hermes_main

session_id = None

print('Hermes -> Gemma4 via local Ollama')
print('Model: gemma4:latest')
print('Base URL: http://localhost:11434/v1')
print('Commands: /exit to quit, /session to print current session id')
print()

while True:
    try:
        user = input('gemma4-hermes ❯ ').strip()
    except (EOFError, KeyboardInterrupt):
        print('\nExiting.')
        break

    if not user:
        continue
    if user in {'/exit', '/quit'}:
        print('Goodbye.')
        break
    if user == '/session':
        print(session_id or '(no session yet)')
        continue

    out = StringIO()
    err = StringIO()
    exit_code = 0
    with redirect_stdout(out), redirect_stderr(err):
        try:
            hermes_main(
                query=user,
                model='gemma4:latest',
                provider='ollama',
                base_url='http://localhost:11434/v1',
                api_key='ollama',
                quiet=True,
                toolsets='none',
                max_turns=8,
                pass_session_id=True,
                resume=session_id,
            )
        except SystemExit as e:
            exit_code = 0 if e.code is None else int(e.code)
        except Exception as e:
            print(f'ERROR: {type(e).__name__}: {e}')
            exit_code = 1

    raw = out.getvalue().splitlines()
    new_session = None
    cleaned = []
    for line in raw:
        if line.startswith('session_id:'):
            new_session = line.split(':', 1)[1].strip()
            continue
        if line.startswith('Warning: Unknown toolsets:'):
            continue
        if line.startswith('__EXIT__='):
            continue
        cleaned.append(line)

    if new_session:
        session_id = new_session

    text = '\n'.join(cleaned).strip()
    if text:
        print(text)
    else:
        print(f'(no response text captured; exit={exit_code})')

    if exit_code != 0:
        print(f'[nonzero exit: {exit_code}]')

    print()
