Compare commits
No commits in common. "1b2bb8cdc2ca9ac6dc56165840a45f747db1ddf6" and "ed70588d95e6bdafe45270dc964c3502491f8110" have entirely different histories.
1b2bb8cdc2
...
ed70588d95
@ -55,7 +55,7 @@ PhoneWork uses a **Router + Host Client** architecture that supports both single
|
|||||||
| `orchestrator/agent.py` | LangChain agent with per-user history + direct/smart mode + direct Q&A |
|
| `orchestrator/agent.py` | LangChain agent with per-user history + direct/smart mode + direct Q&A |
|
||||||
| `orchestrator/tools.py` | Tools: session management, shell, file ops, web search, scheduler, task status |
|
| `orchestrator/tools.py` | Tools: session management, shell, file ops, web search, scheduler, task status |
|
||||||
| `agent/manager.py` | Session registry with persistence, idle timeout, and auto-background tasks |
|
| `agent/manager.py` | Session registry with persistence, idle timeout, and auto-background tasks |
|
||||||
| `agent/cc_runner.py` | Runs `claude -p` headlessly, manages session continuity via `--resume` |
|
| `agent/pty_process.py` | Runs `claude -p` headlessly, manages session continuity via `--resume` |
|
||||||
| `agent/task_runner.py` | Background task runner with Feishu notifications |
|
| `agent/task_runner.py` | Background task runner with Feishu notifications |
|
||||||
| `agent/scheduler.py` | Reminder scheduler with persistence |
|
| `agent/scheduler.py` | Reminder scheduler with persistence |
|
||||||
| `agent/audit.py` | Audit log of all interactions |
|
| `agent/audit.py` | Audit log of all interactions |
|
||||||
|
|||||||
@ -315,7 +315,7 @@ SERVES_USERS:
|
|||||||
**What the host client runs:**
|
**What the host client runs:**
|
||||||
- Full `orchestrator/agent.py` (mailboy LLM, tool loop, per-user history, active session)
|
- Full `orchestrator/agent.py` (mailboy LLM, tool loop, per-user history, active session)
|
||||||
- Full `orchestrator/tools.py` (CC, shell, file ops, web, scheduler — all local)
|
- Full `orchestrator/tools.py` (CC, shell, file ops, web, scheduler — all local)
|
||||||
- `agent/manager.py`, `agent/cc_runner.py`, `agent/task_runner.py` — unchanged
|
- `agent/manager.py`, `agent/pty_process.py`, `agent/task_runner.py` — unchanged
|
||||||
|
|
||||||
Task completion flow:
|
Task completion flow:
|
||||||
- Background task finishes → host client pushes `TaskComplete` to router
|
- Background task finishes → host client pushes `TaskComplete` to router
|
||||||
@ -495,7 +495,7 @@ PhoneWork/
|
|||||||
│
|
│
|
||||||
├── agent/ # Part of host client (local execution)
|
├── agent/ # Part of host client (local execution)
|
||||||
│ ├── manager.py # Session registry
|
│ ├── manager.py # Session registry
|
||||||
│ ├── cc_runner.py # Claude Code runner
|
│ ├── pty_process.py # Claude Code runner
|
||||||
│ ├── task_runner.py # Background tasks
|
│ ├── task_runner.py # Background tasks
|
||||||
│ ├── scheduler.py # Reminders
|
│ ├── scheduler.py # Reminders
|
||||||
│ └── audit.py # Audit log
|
│ └── audit.py # Audit log
|
||||||
|
|||||||
@ -10,7 +10,7 @@ from dataclasses import dataclass, field, asdict
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
from agent.cc_runner import run_claude, DEFAULT_PERMISSION_MODE, VALID_PERMISSION_MODES
|
from agent.pty_process import run_claude, DEFAULT_PERMISSION_MODE, VALID_PERMISSION_MODES
|
||||||
from agent.audit import log_interaction
|
from agent.audit import log_interaction
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|||||||
@ -12,7 +12,7 @@ from typing import Optional, Tuple
|
|||||||
from agent.manager import manager
|
from agent.manager import manager
|
||||||
from agent.scheduler import scheduler
|
from agent.scheduler import scheduler
|
||||||
from agent.task_runner import task_runner
|
from agent.task_runner import task_runner
|
||||||
from agent.cc_runner import VALID_PERMISSION_MODES, DEFAULT_PERMISSION_MODE
|
from agent.pty_process import VALID_PERMISSION_MODES, DEFAULT_PERMISSION_MODE
|
||||||
from orchestrator.agent import agent
|
from orchestrator.agent import agent
|
||||||
from orchestrator.tools import set_current_user, get_current_chat
|
from orchestrator.tools import set_current_user, get_current_chat
|
||||||
|
|
||||||
@ -44,18 +44,14 @@ def _perm_label(mode: str) -> str:
|
|||||||
|
|
||||||
def parse_command(text: str) -> Optional[Tuple[str, str]]:
|
def parse_command(text: str) -> Optional[Tuple[str, str]]:
|
||||||
"""
|
"""
|
||||||
Parse a bot command from text.
|
Parse a slash command from text.
|
||||||
Returns (command, args) or None if not a command.
|
Returns (command, args) or None if not a command.
|
||||||
Commands must start with COMMAND_PREFIX (default "//").
|
|
||||||
"""
|
"""
|
||||||
from config import COMMAND_PREFIX
|
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
if not text.startswith(COMMAND_PREFIX):
|
if not text.startswith("/"):
|
||||||
return None
|
return None
|
||||||
# Strip the prefix, then split into command word and args
|
parts = text.split(None, 1)
|
||||||
body = text[len(COMMAND_PREFIX):]
|
cmd = parts[0].lower()
|
||||||
parts = body.split(None, 1)
|
|
||||||
cmd = COMMAND_PREFIX + parts[0].lower()
|
|
||||||
args = parts[1] if len(parts) > 1 else ""
|
args = parts[1] if len(parts) > 1 else ""
|
||||||
return (cmd, args)
|
return (cmd, args)
|
||||||
|
|
||||||
@ -72,39 +68,38 @@ async def handle_command(user_id: str, text: str) -> Optional[str]:
|
|||||||
logger.info("Command: %s args=%r user=...%s", cmd, args[:50], user_id[-8:])
|
logger.info("Command: %s args=%r user=...%s", cmd, args[:50], user_id[-8:])
|
||||||
|
|
||||||
# In ROUTER_MODE, only handle router-specific commands locally.
|
# In ROUTER_MODE, only handle router-specific commands locally.
|
||||||
# Session commands (//status, //new, //close, etc.) fall through to node forwarding.
|
# Session commands (/status, /new, /close, etc.) fall through to node forwarding.
|
||||||
from config import ROUTER_MODE, COMMAND_PREFIX
|
from config import ROUTER_MODE
|
||||||
P = COMMAND_PREFIX
|
if ROUTER_MODE and cmd not in ("/nodes", "/node", "/help", "/h", "/?"):
|
||||||
if ROUTER_MODE and cmd not in (P+"nodes", P+"node", P+"help", P+"h", P+"?"):
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
set_current_user(user_id)
|
set_current_user(user_id)
|
||||||
|
|
||||||
if cmd in (P+"new", P+"n"):
|
if cmd in ("/new", "/n"):
|
||||||
return await _cmd_new(user_id, args)
|
return await _cmd_new(user_id, args)
|
||||||
elif cmd in (P+"status", P+"list", P+"ls", P+"l"):
|
elif cmd in ("/status", "/list", "/ls", "/l"):
|
||||||
return await _cmd_status(user_id)
|
return await _cmd_status(user_id)
|
||||||
elif cmd in (P+"close", P+"c"):
|
elif cmd in ("/close", "/c"):
|
||||||
return await _cmd_close(user_id, args)
|
return await _cmd_close(user_id, args)
|
||||||
elif cmd in (P+"switch", P+"s"):
|
elif cmd in ("/switch", "/s"):
|
||||||
return await _cmd_switch(user_id, args)
|
return await _cmd_switch(user_id, args)
|
||||||
elif cmd == P+"retry":
|
elif cmd == "/retry":
|
||||||
return await _cmd_retry(user_id)
|
return await _cmd_retry(user_id)
|
||||||
elif cmd in (P+"help", P+"h", P+"?"):
|
elif cmd in ("/help", "/h", "/?"):
|
||||||
return _cmd_help()
|
return _cmd_help()
|
||||||
elif cmd == P+"direct":
|
elif cmd == "/direct":
|
||||||
return _cmd_direct(user_id)
|
return _cmd_direct(user_id)
|
||||||
elif cmd == P+"smart":
|
elif cmd == "/smart":
|
||||||
return _cmd_smart(user_id)
|
return _cmd_smart(user_id)
|
||||||
elif cmd == P+"tasks":
|
elif cmd == "/tasks":
|
||||||
return _cmd_tasks()
|
return _cmd_tasks()
|
||||||
elif cmd == P+"shell":
|
elif cmd == "/shell":
|
||||||
return await _cmd_shell(args)
|
return await _cmd_shell(args)
|
||||||
elif cmd == P+"remind":
|
elif cmd == "/remind":
|
||||||
return await _cmd_remind(args)
|
return await _cmd_remind(args)
|
||||||
elif cmd == P+"perm":
|
elif cmd == "/perm":
|
||||||
return await _cmd_perm(user_id, args)
|
return await _cmd_perm(user_id, args)
|
||||||
elif cmd in (P+"nodes", P+"node"):
|
elif cmd in ("/nodes", "/node"):
|
||||||
return await _cmd_nodes(user_id, args)
|
return await _cmd_nodes(user_id, args)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
@ -436,24 +431,23 @@ async def _cmd_nodes(user_id: str, args: str) -> str:
|
|||||||
|
|
||||||
def _cmd_help() -> str:
|
def _cmd_help() -> str:
|
||||||
"""Show help."""
|
"""Show help."""
|
||||||
from config import COMMAND_PREFIX as P
|
return """**Commands:**
|
||||||
return f"""**Commands:** (prefix: `{P}`)
|
/new <dir> [msg] [--timeout N] [--idle N] [--perm MODE] - Create session
|
||||||
{P}new <dir> [msg] [--timeout N] [--idle N] [--perm MODE] - Create session
|
/status - Show sessions and current mode
|
||||||
{P}status - Show sessions and current mode
|
/close [n] - Close session (active or by number)
|
||||||
{P}close [n] - Close session (active or by number)
|
/switch <n> - Switch to session by number
|
||||||
{P}switch <n> - Switch to session by number
|
/perm <mode> [conv_id] - Set permission mode (bypass/accept/plan)
|
||||||
{P}perm <mode> [conv_id] - Set permission mode (bypass/accept/plan)
|
/direct - Direct mode: messages → Claude Code (no LLM overhead)
|
||||||
{P}direct - Direct mode: messages → Claude Code (no LLM overhead)
|
/smart - Smart mode: messages → LLM routing (default)
|
||||||
{P}smart - Smart mode: messages → LLM routing (default)
|
/shell <cmd> - Run shell command (bypasses LLM)
|
||||||
{P}shell <cmd> - Run shell command (bypasses LLM)
|
/remind <time> <msg> - Set reminder (e.g. /remind 10m check build)
|
||||||
{P}remind <time> <msg> - Set reminder (e.g. {P}remind 10m check build)
|
/tasks - List background tasks
|
||||||
{P}tasks - List background tasks
|
/nodes - List connected host nodes
|
||||||
{P}nodes - List connected host nodes
|
/node <name> - Switch active node
|
||||||
{P}node <name> - Switch active node
|
/retry - Retry last message
|
||||||
{P}retry - Retry last message
|
/help - Show this help
|
||||||
{P}help - Show this help
|
|
||||||
|
|
||||||
**Permission modes** (used by {P}perm and {P}new --perm):
|
**Permission modes** (used by /perm and /new --perm):
|
||||||
bypass — 跳过所有权限确认,CC 自动执行一切操作(默认)
|
bypass — 跳过所有权限确认,CC 自动执行一切操作(默认)
|
||||||
适合:受信任的沙盒环境、自动化任务
|
适合:受信任的沙盒环境、自动化任务
|
||||||
accept — 自动接受文件编辑,但 shell 命令仍需手动确认
|
accept — 自动接受文件编辑,但 shell 命令仍需手动确认
|
||||||
|
|||||||
@ -25,10 +25,6 @@ METASO_API_KEY: str = _cfg.get("METASO_API_KEY", "")
|
|||||||
ROUTER_MODE: bool = _cfg.get("ROUTER_MODE", False)
|
ROUTER_MODE: bool = _cfg.get("ROUTER_MODE", False)
|
||||||
ROUTER_SECRET: str = _cfg.get("ROUTER_SECRET", "")
|
ROUTER_SECRET: str = _cfg.get("ROUTER_SECRET", "")
|
||||||
|
|
||||||
# Command prefix — the leader string that identifies bot commands.
|
|
||||||
# Default is "//" to avoid conflicts with Claude Code's own "/" commands.
|
|
||||||
COMMAND_PREFIX: str = _cfg.get("COMMAND_PREFIX", "//")
|
|
||||||
|
|
||||||
# Server configuration
|
# Server configuration
|
||||||
PORT: int = _cfg.get("PORT", 8000)
|
PORT: int = _cfg.get("PORT", 8000)
|
||||||
|
|
||||||
|
|||||||
@ -20,10 +20,6 @@ WORKING_DIR: C:/Users/yourname/projects
|
|||||||
# Optional: 秘塔AI Search API key for web search
|
# Optional: 秘塔AI Search API key for web search
|
||||||
METASO_API_KEY: your_metaso_api_key
|
METASO_API_KEY: your_metaso_api_key
|
||||||
|
|
||||||
# Bot command prefix (default: "//")
|
|
||||||
# "//" avoids conflicts with Claude Code's own "/" commands.
|
|
||||||
# COMMAND_PREFIX: "//"
|
|
||||||
|
|
||||||
# Which Feishu users this node serves
|
# Which Feishu users this node serves
|
||||||
# List of open_ids from Feishu
|
# List of open_ids from Feishu
|
||||||
SERVES_USERS:
|
SERVES_USERS:
|
||||||
|
|||||||
@ -21,7 +21,7 @@ from langchain_core.messages import (
|
|||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
|
|
||||||
from agent.manager import manager
|
from agent.manager import manager
|
||||||
from config import OPENAI_API_KEY, OPENAI_BASE_URL, OPENAI_MODEL, WORKING_DIR, COMMAND_PREFIX as _P
|
from config import OPENAI_API_KEY, OPENAI_BASE_URL, OPENAI_MODEL, WORKING_DIR
|
||||||
from orchestrator.tools import TOOLS, set_current_user
|
from orchestrator.tools import TOOLS, set_current_user
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -39,29 +39,27 @@ Pass these names directly to `create_conversation` — the tool resolves them au
|
|||||||
|
|
||||||
{active_session_line}
|
{active_session_line}
|
||||||
|
|
||||||
Bot command prefix: {prefix}
|
|
||||||
|
|
||||||
## Tools — two distinct categories
|
## Tools — two distinct categories
|
||||||
|
|
||||||
### Bot control commands (use `run_command`)
|
### Bot control commands (use `run_command`)
|
||||||
`run_command` executes PhoneWork commands. Use it when the user asks to:
|
`run_command` executes PhoneWork slash commands. Use it when the user asks to:
|
||||||
- Change permission mode: "切换到只读模式" → run_command("{prefix}perm plan")
|
- Change permission mode: "切换到只读模式" → run_command("/perm plan")
|
||||||
- Close/switch sessions: "关掉第一个" → run_command("{prefix}close 1")
|
- Close/switch sessions: "关掉第一个" → run_command("/close 1")
|
||||||
- Change routing mode: "切换到直连模式" → run_command("{prefix}direct")
|
- Change routing mode: "切换到直连模式" → run_command("/direct")
|
||||||
- Set a reminder: "10分钟后提醒我" → run_command("{prefix}remind 10m 提醒我")
|
- Set a reminder: "10分钟后提醒我" → run_command("/remind 10m 提醒我")
|
||||||
- Check status: "看看现在有哪些 session" → run_command("{prefix}status")
|
- Check status: "看看现在有哪些 session" → run_command("/status")
|
||||||
- Any other command the user would type manually
|
- Any other /command the user would type manually
|
||||||
|
|
||||||
Available bot commands (pass verbatim to run_command):
|
Available bot commands (pass verbatim to run_command):
|
||||||
{prefix}new <dir> [msg] [--perm bypass|accept|plan] — create session
|
/new <dir> [msg] [--perm bypass|accept|plan] — create session
|
||||||
{prefix}close [n|conv_id] — close session
|
/close [n|conv_id] — close session
|
||||||
{prefix}switch <n> — switch active session
|
/switch <n> — switch active session
|
||||||
{prefix}perm <mode> [conv_id] — permission mode: bypass (default), accept, plan
|
/perm <mode> [conv_id] — permission mode: bypass (default), accept, plan
|
||||||
{prefix}direct — direct mode (bypass LLM for CC messages)
|
/direct — direct mode (bypass LLM for CC messages)
|
||||||
{prefix}smart — smart mode (LLM routing, default)
|
/smart — smart mode (LLM routing, default)
|
||||||
{prefix}status — list sessions
|
/status — list sessions
|
||||||
{prefix}remind <Ns|Nm|Nh> <msg> — one-shot reminder
|
/remind <Ns|Nm|Nh> <msg> — one-shot reminder
|
||||||
{prefix}tasks — list background tasks
|
/tasks — list background tasks
|
||||||
|
|
||||||
### Host shell commands (use `run_shell`)
|
### Host shell commands (use `run_shell`)
|
||||||
`run_shell` executes shell commands on the host machine (git, ls, cat, pip, etc.).
|
`run_shell` executes shell commands on the host machine (git, ls, cat, pip, etc.).
|
||||||
@ -71,7 +69,7 @@ NEVER use `run_shell` for bot control. NEVER use `run_command` for shell command
|
|||||||
1. NEW session: call `create_conversation` with the project name/path. \
|
1. NEW session: call `create_conversation` with the project name/path. \
|
||||||
If the user's message also contains a task, pass it as `initial_message` too.
|
If the user's message also contains a task, pass it as `initial_message` too.
|
||||||
2. Follow-up to ACTIVE session: call `send_to_conversation` with the active conv_id shown above.
|
2. Follow-up to ACTIVE session: call `send_to_conversation` with the active conv_id shown above.
|
||||||
3. BOT CONTROL: call `run_command` with the appropriate command.
|
3. BOT CONTROL: call `run_command` with the appropriate slash command.
|
||||||
4. GENERAL QUESTIONS: answer directly — do NOT create a session for simple Q&A.
|
4. GENERAL QUESTIONS: answer directly — do NOT create a session for simple Q&A.
|
||||||
5. WEB / SEARCH: use `web` at most twice, then synthesize and reply.
|
5. WEB / SEARCH: use `web` at most twice, then synthesize and reply.
|
||||||
6. BACKGROUND TASKS: when a task starts, reply immediately — do NOT poll `task_status`.
|
6. BACKGROUND TASKS: when a task starts, reply immediately — do NOT poll `task_status`.
|
||||||
@ -121,7 +119,6 @@ class OrchestrationAgent:
|
|||||||
working_dir=WORKING_DIR,
|
working_dir=WORKING_DIR,
|
||||||
active_session_line=active_line,
|
active_session_line=active_line,
|
||||||
today=today,
|
today=today,
|
||||||
prefix=_P,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_active_conv(self, user_id: str) -> Optional[str]:
|
def get_active_conv(self, user_id: str) -> Optional[str]:
|
||||||
@ -146,16 +143,6 @@ class OrchestrationAgent:
|
|||||||
logger.info(">>> user=...%s conv=%s msg=%r", short_uid, active_conv, text[:80])
|
logger.info(">>> user=...%s conv=%s msg=%r", short_uid, active_conv, text[:80])
|
||||||
logger.debug(" history_len=%d", len(self._history[user_id]))
|
logger.debug(" history_len=%d", len(self._history[user_id]))
|
||||||
|
|
||||||
# Always handle bot commands first — even in passthrough mode.
|
|
||||||
# Bot commands must never reach Claude Code.
|
|
||||||
from config import COMMAND_PREFIX
|
|
||||||
if text.strip().startswith(COMMAND_PREFIX):
|
|
||||||
from bot.commands import handle_command
|
|
||||||
result = await handle_command(user_id, text)
|
|
||||||
if result is not None:
|
|
||||||
return result
|
|
||||||
logger.debug(" unknown command, falling through to LLM")
|
|
||||||
|
|
||||||
# Passthrough mode: if enabled and active session, bypass LLM
|
# Passthrough mode: if enabled and active session, bypass LLM
|
||||||
if self._passthrough[user_id] and active_conv:
|
if self._passthrough[user_id] and active_conv:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -76,11 +76,9 @@ async def route(user_id: str, chat_id: str, text: str) -> tuple[Optional[str], s
|
|||||||
if len(online_nodes) == 1:
|
if len(online_nodes) == 1:
|
||||||
return online_nodes[0].node_id, "Only one node available"
|
return online_nodes[0].node_id, "Only one node available"
|
||||||
|
|
||||||
from config import COMMAND_PREFIX
|
if text.strip().startswith("/"):
|
||||||
if text.strip().startswith(COMMAND_PREFIX):
|
|
||||||
cmd = text.strip().split()[0].lower()
|
cmd = text.strip().split()[0].lower()
|
||||||
meta_cmds = {COMMAND_PREFIX + s for s in ("nodes", "node", "help", "h", "?")}
|
if cmd in ("/nodes", "/node", "/help", "/h", "/?"):
|
||||||
if cmd in meta_cmds:
|
|
||||||
return "meta", "Meta command"
|
return "meta", "Meta command"
|
||||||
# Session commands: forward to active node directly (no LLM call needed)
|
# Session commands: forward to active node directly (no LLM call needed)
|
||||||
active = registry.get_active_node(user_id)
|
active = registry.get_active_node(user_id)
|
||||||
|
|||||||
@ -51,7 +51,7 @@ def mock_run_claude():
|
|||||||
Default return value is a short CC-style output string.
|
Default return value is a short CC-style output string.
|
||||||
"""
|
"""
|
||||||
mock = AsyncMock(return_value="Claude Code: task complete.")
|
mock = AsyncMock(return_value="Claude Code: task complete.")
|
||||||
with patch("agent.cc_runner.run_claude", mock), \
|
with patch("agent.pty_process.run_claude", mock), \
|
||||||
patch("agent.manager.run_claude", mock):
|
patch("agent.manager.run_claude", mock):
|
||||||
yield mock
|
yield mock
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user