"""一次性验证脚本:真实 DeepSeek 架构师 + 脚手架 Worker,走完整协作四步。 步骤映射: 1) architect.brief 分析拆解 -> 交流文本(交接文档) 2) worker 读交接文档构建实现(第 1 次故意输出不合格 -> 触发问题) 3) worker 验证失败 -> issue 交接至文档 -> architect.decide 裁决(真实 API) 4) worker 按裁决修复 -> 全步完成 -> architect.final_review 终审(真实 API) """ import asyncio import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from gateway.api import build_agent_chat # noqa: E402 复用 .env 里的 key 构建客户端 from router_system.architect import build_architect # noqa: E402 from router_system.pipeline import CollaborativePipeline # noqa: E402 from router_system.worker import WorkerLoop # noqa: E402 QUERY = "用 Python 写一个函数 count_primes(n),返回小于 n 的质数个数,附单元测试。" GOOD_CODE = '''```python def count_primes(n): if n < 3: return 0 is_prime = [True] * n is_prime[0] = is_prime[1] = False for i in range(2, int(n ** 0.5) + 1): if is_prime[i]: for j in range(i * i, n, i): is_prime[j] = False return sum(is_prime) def test_count_primes(): assert count_primes(0) == 0 assert count_primes(2) == 0 assert count_primes(10) == 4 assert count_primes(20) == 8 ```''' async def main() -> None: cfg = {"base_url": "https://api.deepseek.com", "model": "deepseek-chat"} architect = build_architect({**cfg, "api_key": _env_key()}) print(f"[architect] model={architect.model} key={'已配置' if architect.api_key else '缺失'}") calls = {"n": 0} async def scripted_generate(prompt: str) -> str: """第 1 次故意交不合格产物(触发 issue->decide),第 2 次交合格实现。""" calls["n"] += 1 if calls["n"] == 1: print("[worker] 第 1 次生成:故意输出不合格(无代码块)") return "这一步我没想清楚,先给个思路:应该用筛法,但代码还没写。" print("[worker] 第 2 次生成:交出完整实现(含测试)") return GOOD_CODE worker = WorkerLoop(generate=scripted_generate, max_fix_attempts=1, model_used="scripted-worker") pipe = CollaborativePipeline(architect=architect, worker=worker, fast_path=False, rounds_cap=6, api_token_cap=30000) result = await pipe.run(QUERY, request_id="collabdemo01") print("\n========== 路线 ==========") print(" -> ".join(result.route)) ws = json.loads(Path(result.workspace_path).read_text(encoding="utf-8")) print("\n========== 交流文本关键内容 ==========") print("brief.goal:", ws["brief"]["goal"][:80]) print("plan:", [(p["id"], p["task"][:36]) for p in ws["brief"]["plan"]]) print("issues:", [(i["id"], i["step"], i["summary"][:40]) for i in ws.get("issues", [])]) print("decisions:", [(d["ref"], d["reply"][:60]) for d in ws.get("decisions", [])]) print("progress:", [(p["step"], p["status"]) for p in ws.get("progress", [])]) print("final verdict:", ws["meta"].get("review_verdict", "(看 route)")) print("\n========== 结果 ==========") print("status:", result.status, "| rounds:", result.rounds_used, "| api_tokens:", result.api_input_tokens, "+", result.api_output_tokens, "| latency:", round(result.latency_ms), "ms") print("response 前 300 字:\n", result.response[:300]) def _env_key() -> str: import os from dotenv import load_dotenv load_dotenv(Path(__file__).resolve().parent / ".env") key = os.environ.get("DEEPSEEK_API_KEY") if not key: print("缺少 DEEPSEEK_API_KEY,无法做真实协作验证") sys.exit(1) return key if __name__ == "__main__": asyncio.run(main())