#!/usr/bin/env python3
"""
s17: 自主 Agent — 空闲轮询 + 自动认领 + WORK/IDLE 生命周期。
运行: python s17_autonomous_agents/code.py
需要: pip install anthropic python-dotenv + .env 中配置 ANTHROPIC_API_KEY
相对 s16 的变化:
- scan_unclaimed_tasks:查找 pending、未分配且依赖已完成的任务
- idle_poll:60 秒轮询循环(收件箱 + 任务板),在 IDLE 中分发 shutdown
- claim_task:owner 检查 + 返回值校验
- 队友生命周期:WORK → IDLE → SHUTDOWN
- 队友工具:+ list_tasks、claim_task、complete_task(5→8)
- consume_lead_inbox:统一收件箱消费器,用于协议 + 上下文注入
- 上下文压缩后重新注入身份提示
ASCII 生命周期:
WORK: inbox → LLM → tools → (tool_use? 循环) → (完成? → IDLE)
IDLE: 5s 轮询 → 有 inbox? → WORK / 有未认领任务? → 认领 → WORK / 60s? → SHUTDOWN
"""
import os, subprocess, json, time, random, threading
from pathlib import Path
from datetime import datetime
from dataclasses import dataclass, asdict, field
try:
import readline
readline.parse_and_bind('set bind-tty-special-chars off')
except ImportError:
pass
from anthropic import Anthropic
from dotenv import load_dotenv
load_dotenv(override=True)
if os.getenv("ANTHROPIC_BASE_URL"):
os.environ.pop("ANTHROPIC_AUTH_TOKEN", None)
WORKDIR = Path.cwd()
client = Anthropic(base_url=os.getenv("ANTHROPIC_BASE_URL"))
MODEL = os.environ["MODEL_ID"]
# ── 任务系统 (来自 s12) ──
TASKS_DIR = WORKDIR / ".tasks"
TASKS_DIR.mkdir(exist_ok=True)
@dataclass
class Task:
id: str
subject: str
description: str
status: str
owner: str | None
blockedBy: list[str]
def _task_path(task_id: str) -> Path:
return TASKS_DIR / f"{task_id}.json"
def create_task(subject: str, description: str = "",
blockedBy: list[str] | None = None) -> Task:
task = Task(
id=f"task_{int(time.time())}_{random.randint(0, 9999):04d}",
subject=subject, description=description,
status="pending", owner=None,
blockedBy=blockedBy or [],
)
save_task(task)
return task
def save_task(task: Task):
_task_path(task.id).write_text(json.dumps(asdict(task), indent=2))
def load_task(task_id: str) -> Task:
return Task(**json.loads(_task_path(task_id).read_text()))
def list_tasks() -> list[Task]:
return [Task(**json.loads(p.read_text()))
for p in sorted(TASKS_DIR.glob("task_*.json"))]
def get_task(task_id: str) -> str:
task = load_task(task_id)
return json.dumps(asdict(task), indent=2)
def can_start(task_id: str) -> bool:
task = load_task(task_id)
for dep_id in task.blockedBy:
if not _task_path(dep_id).exists():
return False
if load_task(dep_id).status != "completed":
return False
return True
def claim_task(task_id: str, owner: str = "agent") -> str:
task = load_task(task_id)
if task.status != "pending":
status = {"pending": "待处理", "in_progress": "进行中",
"completed": "已完成"}.get(task.status, task.status)
return f"任务 {task_id} 当前状态为 {status},无法认领"
if task.owner:
return f"任务 {task_id} 已由 {task.owner} 负责"
if not can_start(task_id):
deps = [d for d in task.blockedBy
if _task_path(d).exists() and load_task(d).status != "completed"]
missing = [d for d in task.blockedBy if not _task_path(d).exists()]
parts = []
if deps: parts.append(f"被阻塞于:{deps}")
if missing: parts.append(f"缺失依赖:{missing}")
return "无法开始 — " + ", ".join(parts)
task.owner = owner
task.status = "in_progress"
save_task(task)
print(f" \033[36m[认领] {task.subject} → in_progress\033[0m")
return f"已认领 {task.id} ({task.subject})"
def complete_task(task_id: str) -> str:
task = load_task(task_id)
if task.status != "in_progress":
status = {"pending": "待处理", "in_progress": "进行中",
"completed": "已完成"}.get(task.status, task.status)
return f"任务 {task_id} 当前状态为 {status},无法完成"
task.status = "completed"
save_task(task)
unblocked = [t.subject for t in list_tasks()
if t.status == "pending" and t.blockedBy and can_start(t.id)]
print(f" \033[32m[完成] {task.subject} ✓\033[0m")
msg = f"已完成 {task.id} ({task.subject})"
if unblocked:
msg += f"\n已解除阻塞:{', '.join(unblocked)}"
return msg
# ── 提示词组装 (来自 s10) ──
PROMPT_SECTIONS = {
"identity": "你是一个编码 Agent。直接行动,不要只解释。",
"tools": "可用工具:bash, read_file, write_file, "
"create_task, list_tasks, get_task, claim_task, complete_task, "
"spawn_teammate, send_message, check_inbox, "
"request_shutdown, request_plan, review_plan.",
"workspace": f"工作目录:{WORKDIR}",
"memory": "有可用的相关记忆时,会在下方注入。",
}
def assemble_system_prompt(context: dict) -> str:
sections = [PROMPT_SECTIONS["identity"],
PROMPT_SECTIONS["tools"],
PROMPT_SECTIONS["workspace"]]
if context.get("memories"):
sections.append(f"相关记忆:\n{context['memories']}")
return "\n\n".join(sections)
_last_context_hash, _last_prompt = None, None
def get_system_prompt(context: dict) -> str:
global _last_context_hash, _last_prompt
h = json.dumps(context, sort_keys=True)
if h == _last_context_hash and _last_prompt:
return _last_prompt
_last_context_hash, _last_prompt = h, assemble_system_prompt(context)
return _last_prompt
# ── 工具 (来自 s15) ──
def safe_path(p: str) -> Path:
path = (WORKDIR / p).resolve()
if not path.is_relative_to(WORKDIR):
raise ValueError(f"路径逃逸出工作区:{p}")
return path
def run_bash(command: str) -> str:
try:
r = subprocess.run(command, shell=True, cwd=WORKDIR,
capture_output=True, text=True, timeout=120)
out = (r.stdout + r.stderr).strip()
return out[:50000] if out else "(无输出)"
except subprocess.TimeoutExpired:
return "错误:执行超时(120 秒)"
def run_read(path: str, limit: int | None = None) -> str:
try:
lines = safe_path(path).read_text().splitlines()
if limit and limit < len(lines):
lines = lines[:limit] + [f"... ({len(lines) - limit} 行更多内容)"]
return "\n".join(lines)
except Exception as e:
return f"错误:{e}"
def run_write(path: str, content: str) -> str:
try:
fp = safe_path(path)
fp.parent.mkdir(parents=True, exist_ok=True)
fp.write_text(content)
return f"已写入 {len(content)} 字节到 {path}"
except Exception as e:
return f"错误:{e}"
# ── MessageBus (来自 s15) ──
MAILBOX_DIR = WORKDIR / ".mailboxes"
MAILBOX_DIR.mkdir(exist_ok=True)
class MessageBus:
def send(self, from_agent: str, to_agent: str, content: str,
msg_type: str = "message", metadata: dict = None):
msg = {"from": from_agent, "to": to_agent,
"content": content, "type": msg_type,
"ts": time.time(), "metadata": metadata or {}}
inbox = MAILBOX_DIR / f"{to_agent}.jsonl"
with open(inbox, "a") as f:
f.write(json.dumps(msg) + "\n")
print(f" \033[33m[bus] {from_agent} → {to_agent}: "
f"({msg_type}) {content[:50]}\033[0m")
def read_inbox(self, agent: str) -> list[dict]:
inbox = MAILBOX_DIR / f"{agent}.jsonl"
if not inbox.exists():
return []
msgs = [json.loads(line) for line in inbox.read_text().splitlines()
if line.strip()]
inbox.unlink()
return msgs
BUS = MessageBus()
active_teammates: dict[str, bool] = {}
# ── 协议状态 (来自 s16) ──
@dataclass
class ProtocolState:
request_id: str
type: str
sender: str
target: str
status: str
payload: str
created_at: float = field(default_factory=time.time)
pending_requests: dict[str, ProtocolState] = {}
def new_request_id() -> str:
return f"req_{random.randint(0, 999999):06d}"
def match_response(response_type: str, request_id: str, approve: bool):
"""通过 request_id 将响应关联到原始请求。"""
state = pending_requests.get(request_id)
if not state:
print(f" \033[31m[协议] 未知 request_id: {request_id}\033[0m")
return
if state.type == "shutdown" and response_type != "shutdown_response":
print(f" \033[31m[协议] 类型不匹配:期望 shutdown_response,"
f"实际得到 {response_type}\033[0m")
return
if state.type == "plan_approval" and response_type != "plan_approval_response":
print(f" \033[31m[协议] 类型不匹配:期望 plan_approval_response,"
f"实际得到 {response_type}\033[0m")
return
state.status = "approved" if approve else "rejected"
icon = "✓" if approve else "✗"
color = "32" if approve else "31"
print(f" \033[{color}m[协议] {state.type} {icon} "
f"({request_id}: {state.status})\033[0m")
# ── 自主 Agent (s17 新增) ──
IDLE_POLL_INTERVAL = 5 # 秒
IDLE_TIMEOUT = 60 # 秒
def scan_unclaimed_tasks() -> list[dict]:
"""查找依赖均已完成、未分配的 pending 任务。"""
unclaimed = []
for f in sorted(TASKS_DIR.glob("task_*.json")):
task = json.loads(f.read_text())
if (task.get("status") == "pending"
and not task.get("owner")
and can_start(task["id"])):
unclaimed.append(task)
return unclaimed
def idle_poll(name: str, messages: list, role: str) -> str:
"""轮询 60 秒。返回 'work'、'shutdown' 或 'timeout'。"""
for _ in range(IDLE_TIMEOUT // IDLE_POLL_INTERVAL):
time.sleep(IDLE_POLL_INTERVAL)
# 检查收件箱 — 优先分发协议消息
inbox = BUS.read_inbox(name)
if inbox:
# 检查 shutdown_request
for msg in inbox:
if msg.get("type") == "shutdown_request":
req_id = msg.get("metadata", {}).get("request_id", "")
BUS.send(name, "lead", "正在平滑关闭。",
"shutdown_response",
{"request_id": req_id, "approve": True})
print(f" \033[35m[协议] {name} 在空闲阶段已批准关闭 "
f"({req_id})\033[0m")
return "shutdown"
# 非协议收件箱:注入并恢复工作
messages.append({"role": "user",
"content": "" + json.dumps(inbox) + ""})
print(f" \033[36m[空闲] {name} 发现收件箱消息\033[0m")
return "work"
# 扫描任务板
unclaimed = scan_unclaimed_tasks()
if unclaimed:
task = unclaimed[0]
result = claim_task(task["id"], name)
if "已认领" in result:
messages.append({"role": "user",
"content": f"任务 {task['id']}: "
f"{task['subject']}"})
print(f" \033[32m[空闲] {name} 自动认领:"
f"{task['subject']}\033[0m")
return "work"
print(f" \033[33m[空闲] {name} 认领失败:"
f"{result}\033[0m")
print(f" \033[31m[空闲] {name} 超时({IDLE_TIMEOUT}s)\033[0m")
return "timeout"
# ── 队友线程(来自 s15 + s16 + s17) ──
def spawn_teammate_thread(name: str, role: str, prompt: str) -> str:
if name in active_teammates:
return f"队友 '{name}' 已存在"
system = (f"你是 '{name}',角色是 {role}。"
f"使用工具完成任务。"
f"你可以从任务板列出并认领任务。"
f"检查收件箱中的协议消息。")
def handle_inbox_message(name: str, msg: dict, messages: list):
"""按类型分发收到的协议消息。"""
msg_type = msg.get("type", "message")
meta = msg.get("metadata", {})
req_id = meta.get("request_id", "")
if msg_type == "shutdown_request":
BUS.send(name, "lead", "正在平滑关闭。",
"shutdown_response",
{"request_id": req_id, "approve": True})
print(f" \033[35m[协议] {name} 已批准关闭 "
f"({req_id})\033[0m")
return True
if msg_type == "plan_approval_response":
approve = meta.get("approve", False)
if approve:
messages.append({"role": "user",
"content": "[计划已批准] 继续执行任务。"})
else:
messages.append({"role": "user",
"content": f"[计划已拒绝] 反馈:{msg['content']}"})
return False
def run():
messages = [{"role": "user", "content": prompt}]
sub_tools = [
{"name": "bash", "description": "运行一条 shell 命令。",
"input_schema": {"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"]}},
{"name": "read_file", "description": "读取文件。",
"input_schema": {"type": "object",
"properties": {"path": {"type": "string"}},
"required": ["path"]}},
{"name": "write_file", "description": "写入文件。",
"input_schema": {"type": "object",
"properties": {"path": {"type": "string"},
"content": {"type": "string"}},
"required": ["path", "content"]}},
{"name": "send_message",
"description": "向另一个 Agent 发送消息。",
"input_schema": {"type": "object",
"properties": {"to": {"type": "string"},
"content": {"type": "string"}},
"required": ["to", "content"]}},
{"name": "submit_plan",
"description": "提交计划给 Lead 审批。",
"input_schema": {"type": "object",
"properties": {"plan": {"type": "string"}},
"required": ["plan"]}},
# s17 新增:队友可以列出、认领并完成任务
{"name": "list_tasks",
"description": "列出任务板上的所有任务。",
"input_schema": {"type": "object", "properties": {},
"required": []}},
{"name": "claim_task",
"description": "认领一个待处理任务。",
"input_schema": {"type": "object",
"properties": {"task_id": {"type": "string"}},
"required": ["task_id"]}},
{"name": "complete_task",
"description": "将进行中的任务标记为已完成。",
"input_schema": {"type": "object",
"properties": {"task_id": {"type": "string"}},
"required": ["task_id"]}},
]
def _run_list_tasks():
个任务 = list_tasks()
if not 个任务:
return "暂无任务。"
status_labels = {"pending": "待处理", "in_progress": "进行中",
"completed": "已完成"}
return "\n".join(
f" {t.id}: {t.subject} [{status_labels.get(t.status, t.status)}]"
for t in 个任务)
def _run_claim_task(task_id: str):
return claim_task(task_id, owner=name)
def _run_complete_task(task_id: str):
return complete_task(task_id)
sub_handlers = {
"bash": run_bash, "read_file": run_read, "write_file": run_write,
"send_message": lambda to, content: (BUS.send(name, to, content),
"已发送")[1],
"submit_plan": lambda plan: _teammate_submit_plan(name, plan),
"list_tasks": _run_list_tasks,
"claim_task": _run_claim_task,
"complete_task": _run_complete_task,
}
# 外层循环:WORK → IDLE 周期
while True:
# 身份重新注入 (s17)
if len(messages) <= 3:
messages.insert(0, {"role": "user",
"content": f"你是 '{name}',角色:{role}。"
f"继续你的工作。"})
# WORK 阶段
should_shutdown = False
for _ in range(10):
inbox = BUS.read_inbox(name)
for msg in inbox:
stopped = handle_inbox_message(name, msg, messages)
if stopped:
should_shutdown = True
break
if should_shutdown:
break
if inbox and not should_shutdown:
non_protocol = [m for m in inbox
if m.get("type") == "message"]
if non_protocol:
messages.append({"role": "user",
"content": f"{json.dumps(non_protocol)}"})
try:
response = client.messages.create(
model=MODEL, system=system, messages=messages[-20:],
tools=sub_tools, max_tokens=8000)
except Exception:
break
messages.append({"role": "assistant", "content": response.content})
if response.stop_reason != "tool_use":
break
results = []
for block in response.content:
if block.type == "tool_use":
handler = sub_handlers.get(block.name)
output = handler(**block.input) if handler else "未知"
results.append({"type": "tool_result",
"tool_use_id": block.id,
"content": str(output)})
messages.append({"role": "user", "content": results})
if should_shutdown:
break
# IDLE 阶段 (s17 新增)
idle_result = idle_poll(name, messages, role)
if idle_result == "shutdown":
break
if idle_result == "timeout":
break
# 摘要
summary = "已完成。"
for msg in reversed(messages):
if msg["role"] == "assistant" and isinstance(msg["content"], list):
for b in msg["content"]:
if getattr(b, "type", None) == "text":
summary = b.text
break
else:
continue
break
BUS.send(name, "lead", summary, "result")
active_teammates.pop(name, None)
print(f" \033[32m[队友] {name} 已完成\033[0m")
active_teammates[name] = True
threading.Thread(target=run, daemon=True).start()
print(f" \033[36m[队友] {name} 已作为 {role} 启动\033[0m")
return f"队友 '{name}' 已启动为 {role}(自主模式)"
def _teammate_submit_plan(from_name: str, plan: str) -> str:
"""队友向 Lead 提交计划以供审批。"""
req_id = new_request_id()
pending_requests[req_id] = ProtocolState(
request_id=req_id, type="plan_approval",
sender=from_name, target="lead",
status="pending", payload=plan)
BUS.send(from_name, "lead", plan,
"plan_approval_request",
{"request_id": req_id})
return f"计划已提交({req_id})。正在等待审批..."
# ── Lead 协议工具 (来自 s16) ──
def run_request_shutdown(teammate: str) -> str:
req_id = new_request_id()
pending_requests[req_id] = ProtocolState(
request_id=req_id, type="shutdown",
sender="lead", target=teammate,
status="pending", payload="")
BUS.send("lead", teammate, "请平滑关闭。",
"shutdown_request",
{"request_id": req_id})
print(f" \033[35m[协议] shutdown_request → {teammate} "
f"({req_id})\033[0m")
return f"已向 {teammate} 发送关闭请求(req: {req_id})"
def run_request_plan(teammate: str, task: str) -> str:
"""Lead 要求队友提交计划。"""
BUS.send("lead", teammate, f"请为以下任务提交计划:{task}",
"message")
return f"已要求 {teammate} 提交计划"
def run_review_plan(request_id: str, approve: bool,
feedback: str = "") -> str:
state = pending_requests.get(request_id)
if not state:
return f"请求 {request_id} 未找到"
if state.status != "pending":
return f"请求 {request_id} 已经是 {state.status}"
state.status = "approved" if approve else "rejected"
BUS.send("lead", state.sender,
feedback or ("已批准" if approve else "已拒绝"),
"plan_approval_response",
{"request_id": request_id, "approve": approve})
icon = "✓" if approve else "✗"
print(f" \033[32m[协议] 计划 {icon} ({request_id})\033[0m")
return f"计划已{'批准' if approve else '拒绝'}({request_id})"
# ── 基础工具处理器 ──
def run_create_task(subject: str, description: str = "",
blockedBy: list[str] | None = None) -> str:
task = create_task(subject, description, blockedBy)
deps = f"(依赖:{', '.join(blockedBy)})" if blockedBy else ""
print(f" \033[34m[创建] {task.subject}{deps}\033[0m")
return f"已创建 {task.id}: {task.subject}{deps}"
def run_list_tasks() -> str:
个任务 = list_tasks()
if not 个任务:
return "暂无任务。"
status_labels = {"pending": "待处理", "in_progress": "进行中",
"completed": "已完成"}
return "\n".join(
f" {t.id}: {t.subject} "
f"[{status_labels.get(t.status, t.status)}]"
for t in 个任务)
def run_get_task(task_id: str) -> str:
return get_task(task_id)
def run_claim_task(task_id: str) -> str:
return claim_task(task_id, owner="agent")
def run_complete_task(task_id: str) -> str:
return complete_task(task_id)
def run_spawn_teammate(name: str, role: str, prompt: str) -> str:
return spawn_teammate_thread(name, role, prompt)
def run_send_message(to: str, content: str) -> str:
BUS.send("lead", to, content)
return f"已发送给 {to}"
def consume_lead_inbox(route_protocol=True) -> list[dict]:
"""读取 Lead 收件箱:路由协议响应,并返回所有消息。"""
msgs = BUS.read_inbox("lead")
if route_protocol:
for msg in msgs:
meta = msg.get("metadata", {})
req_id = meta.get("request_id", "")
msg_type = msg.get("type", "")
if req_id and msg_type.endswith("_response"):
match_response(msg_type, req_id, meta.get("approve", False))
return msgs
def run_check_inbox() -> str:
msgs = consume_lead_inbox(route_protocol=True)
if not msgs:
return "(收件箱为空)"
lines = []
for m in msgs:
meta = m.get("metadata", {})
req_id = meta.get("request_id", "")
tag = f" [{m['type']} req:{req_id}]" if req_id else f" [{m['type']}]"
lines.append(f" [{m['from']}]{tag} {m['content'][:200]}")
return "\n".join(lines)
# ── 工具定义 ──
TOOLS = [
{"name": "bash", "description": "运行一条 shell 命令。",
"input_schema": {"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"]}},
{"name": "read_file", "description": "读取文件内容。",
"input_schema": {"type": "object",
"properties": {"path": {"type": "string"},
"limit": {"type": "integer"}},
"required": ["path"]}},
{"name": "write_file", "description": "向文件写入内容。",
"input_schema": {"type": "object",
"properties": {"path": {"type": "string"},
"content": {"type": "string"}},
"required": ["path", "content"]}},
{"name": "create_task",
"description": "创建一个任务。",
"input_schema": {"type": "object",
"properties": {"subject": {"type": "string"},
"description": {"type": "string"},
"blockedBy": {"type": "array",
"items": {"type": "string"}}},
"required": ["subject"]}},
{"name": "list_tasks",
"description": "列出所有任务。",
"input_schema": {"type": "object", "properties": {}, "required": []}},
{"name": "get_task",
"description": "获取指定任务的完整详情。",
"input_schema": {"type": "object",
"properties": {"task_id": {"type": "string"}},
"required": ["task_id"]}},
{"name": "claim_task",
"description": "认领一个待处理任务。",
"input_schema": {"type": "object",
"properties": {"task_id": {"type": "string"}},
"required": ["task_id"]}},
{"name": "complete_task",
"description": "完成一个进行中的任务。",
"input_schema": {"type": "object",
"properties": {"task_id": {"type": "string"}},
"required": ["task_id"]}},
{"name": "spawn_teammate",
"description": "启动一个自主队友 Agent。",
"input_schema": {"type": "object",
"properties": {"name": {"type": "string"},
"role": {"type": "string"},
"prompt": {"type": "string"}},
"required": ["name", "role", "prompt"]}},
{"name": "send_message",
"description": "向队友发送消息。",
"input_schema": {"type": "object",
"properties": {"to": {"type": "string"},
"content": {"type": "string"}},
"required": ["to", "content"]}},
{"name": "check_inbox",
"description": "检查收件箱中的消息和协议响应。",
"input_schema": {"type": "object", "properties": {}, "required": []}},
{"name": "request_shutdown",
"description": "请求队友平滑关闭。",
"input_schema": {"type": "object",
"properties": {"teammate": {"type": "string"}},
"required": ["teammate"]}},
{"name": "request_plan",
"description": "要求队友提交计划以供审查。",
"input_schema": {"type": "object",
"properties": {"teammate": {"type": "string"},
"task": {"type": "string"}},
"required": ["teammate", "task"]}},
{"name": "review_plan",
"description": "批准或拒绝已提交的计划。",
"input_schema": {"type": "object",
"properties": {
"request_id": {"type": "string"},
"approve": {"type": "boolean"},
"feedback": {"type": "string"}},
"required": ["request_id", "approve"]}},
]
TOOL_HANDLERS = {
"bash": run_bash, "read_file": run_read, "write_file": run_write,
"create_task": run_create_task, "list_tasks": run_list_tasks,
"get_task": run_get_task,
"claim_task": run_claim_task, "complete_task": run_complete_task,
"spawn_teammate": run_spawn_teammate,
"send_message": run_send_message, "check_inbox": run_check_inbox,
"request_shutdown": run_request_shutdown,
"request_plan": run_request_plan, "review_plan": run_review_plan,
}
# ── 上下文 ──
MEMORY_DIR = WORKDIR / ".memory"
MEMORY_INDEX = MEMORY_DIR / "MEMORY.md"
def update_context(context: dict, messages: list) -> dict:
memories = ""
if MEMORY_INDEX.exists():
memories = MEMORY_INDEX.read_text()[:2000]
return {"memories": memories}
# ── Agent 循环 ──
def agent_loop(messages: list, context: dict):
system = get_system_prompt(context)
while True:
try:
response = client.messages.create(
model=MODEL, system=system, messages=messages,
tools=TOOLS, max_tokens=8000)
except Exception as e:
messages.append({"role": "assistant", "content": [
{"type": "text", "text": f"[错误] {type(e).__name__}: {e}"}]})
return
messages.append({"role": "assistant", "content": response.content})
if response.stop_reason != "tool_use":
return
results = []
for block in response.content:
if block.type != "tool_use":
continue
print(f"\033[36m> {block.name}\033[0m")
handler = TOOL_HANDLERS.get(block.name)
output = handler(**block.input) if handler else "未知"
print(str(output)[:300])
results.append({"type": "tool_result",
"tool_use_id": block.id, "content": output})
messages.append({"role": "user", "content": results})
context = update_context(context, messages)
system = get_system_prompt(context)
if __name__ == "__main__":
print("s17: 自主 Agent")
print("输入问题后按回车发送。输入 q 退出。\n")
history = []
context = {"memories": ""}
while True:
try:
query = input("\033[36ms17 >> \033[0m")
except (EOFError, KeyboardInterrupt):
break
if query.strip().lower() in ("q", "exit", ""):
break
history.append({"role": "user", "content": query})
agent_loop(history, context)
context = update_context(context, history)
for block in history[-1]["content"]:
if getattr(block, "type", None) == "text":
print(block.text)
elif isinstance(block, dict) and block.get("type") == "text":
print(block.get("text", ""))
# 消费 Lead 收件箱:路由协议 + 注入历史
inbox = consume_lead_inbox(route_protocol=True)
if inbox:
inbox_text = "\n".join(
f"来自 {m['from']} [{m.get('type', 'message')}]: "
f"{m['content'][:200]}" for m in inbox)
history.append({"role": "user",
"content": f"[收件箱]\n{inbox_text}"})
print()