feat(chat): add conversational chat mode with intent parsing and plan/confirm flow
This commit is contained in:
@@ -9,6 +9,7 @@ import urllib.request
|
||||
from pathlib import Path
|
||||
from .queue import list_blocked
|
||||
from .guardrails import get_cfg, set_cfg
|
||||
from .chat_mode import load_mode, save_mode, interpret_text, set_pending, pop_pending
|
||||
from .risk import classify, needs_approval
|
||||
from .audit import log_event
|
||||
|
||||
@@ -115,6 +116,8 @@ def help_text() -> str:
|
||||
"- queue deny <task_id> [reason] (or /deny <task_id> [reason])\n"
|
||||
"- run once\n"
|
||||
"- mode autonomous on|off\n"
|
||||
"- mode chat on|off\n"
|
||||
"- mode strict on|off\n"
|
||||
"- goals show\n"
|
||||
"- goals add <text>\n"
|
||||
"- guardrails show\n"
|
||||
@@ -258,6 +261,18 @@ def parse_and_handle(chat_id: int, msg_id: int, text: str) -> str:
|
||||
cfg = get_cfg(); cfg['max_tasks_per_hour'] = int(parts[3]); set_cfg(cfg)
|
||||
return 'guardrails updated'
|
||||
|
||||
if parts[:2] == ['mode', 'chat']:
|
||||
if len(parts) != 3 or parts[2] not in {'on','off'}:
|
||||
return 'Usage: mode chat on|off'
|
||||
m = load_mode(); m['chat_enabled'] = (parts[2]=='on'); save_mode(m)
|
||||
return f"chat mode set to {parts[2]}"
|
||||
|
||||
if parts[:2] == ['mode', 'strict']:
|
||||
if len(parts) != 3 or parts[2] not in {'on','off'}:
|
||||
return 'Usage: mode strict on|off'
|
||||
m = load_mode(); m['strict_mode'] = (parts[2]=='on'); save_mode(m)
|
||||
return f"strict mode set to {parts[2]}"
|
||||
|
||||
if parts[:2] == ['goals', 'show']:
|
||||
goals = load_goals()
|
||||
return json.dumps(goals, indent=2)[:3500] if goals else 'No goals set.'
|
||||
@@ -270,6 +285,33 @@ def parse_and_handle(chat_id: int, msg_id: int, text: str) -> str:
|
||||
save_goals(goals)
|
||||
return 'Goal added.'
|
||||
|
||||
# conversational mode fallback (non-command chat)
|
||||
mode = load_mode()
|
||||
if mode.get('chat_enabled', True):
|
||||
uid = str(chat_id)
|
||||
interp = interpret_text(raw)
|
||||
intent = (interp.get('intent') or 'unknown').lower()
|
||||
|
||||
if intent == 'approval':
|
||||
pending = pop_pending(uid)
|
||||
if pending:
|
||||
code, out = run_prime(['queue','add', pending.get('task_type','planning'), pending.get('task','')])
|
||||
return f"Approved. Queued action.\n{out[:3000]}"
|
||||
return 'No pending action to approve.'
|
||||
|
||||
if intent == 'action-request':
|
||||
task_type = interp.get('task_type') or 'planning'
|
||||
task = interp.get('task') or raw
|
||||
strict = bool(mode.get('strict_mode', False))
|
||||
if strict or bool(interp.get('needs_confirmation', True)):
|
||||
set_pending(uid, task_type, task)
|
||||
return f"Plan: I can execute this as {task_type}.\nTask: {task}\nReply 'approve' to proceed or 'deny' to cancel."
|
||||
code, out = run_prime(['queue','add', task_type, task])
|
||||
return f"Queued.\n{out[:3000]}"
|
||||
|
||||
if intent in {'chat','ask','unknown'}:
|
||||
return interp.get('reply') or 'I'm here. What would you like to do next?'
|
||||
|
||||
return "Unknown command.\n" + help_text()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user