|
| 1 | +"""Capture real DeepSeek responses to JSON fixtures for PR-20. |
| 2 | +
|
| 3 | +Usage:: |
| 4 | +
|
| 5 | + # One scenario, single user prompt: |
| 6 | + uv run python scripts/capture_deepseek_fixtures.py \\ |
| 7 | + --scenario inspect_basic_fps \\ |
| 8 | + --prompt "What's the current FPS of the project?" |
| 9 | +
|
| 10 | + # Multi-turn — feed extra prompts after the first response: |
| 11 | + uv run python scripts/capture_deepseek_fixtures.py \\ |
| 12 | + --scenario memory_save_and_recall \\ |
| 13 | + --prompt "Save a memory called X with content 'hello'" \\ |
| 14 | + --prompt "Now recall X and quote the content" |
| 15 | +
|
| 16 | +The recorder: |
| 17 | + 1. Spawns a localhost HTTP server that forwards every POST to |
| 18 | + https://api.deepseek.com/anthropic/v1/messages. |
| 19 | + 2. Constructs the standalone ``Agent`` (from |
| 20 | + ``td_component/tdpilot_api_agent.py``) pointing at the proxy. |
| 21 | + 3. Uses a stub dispatcher that returns realistic-shaped TD tool |
| 22 | + results — matches the shape real TD's MCP handlers return so |
| 23 | + the model's behavior tracks production. |
| 24 | + 4. Every (request, response) pair is appended to |
| 25 | + ``tests/fixtures/deepseek/<scenario>.json`` (pretty-printed, |
| 26 | + sorted keys, ASCII-safe). |
| 27 | +
|
| 28 | +Note: this script costs DeepSeek API credits. Each fixture is a |
| 29 | +small handful of API calls (typically 2-5 turns × $0.01-0.03). |
| 30 | +The captured fixtures live in the repo so subsequent CI runs are |
| 31 | +free. |
| 32 | +
|
| 33 | +API key resolution mirrors ``tdpilot_api_config.fetch_api_key``: |
| 34 | + TDPILOT_API_KEY env > ~/.tdpilot-api/config.json > .env file. |
| 35 | +""" |
| 36 | + |
| 37 | +from __future__ import annotations |
| 38 | + |
| 39 | +import argparse |
| 40 | +import json |
| 41 | +import sys |
| 42 | +import threading |
| 43 | +import urllib.error |
| 44 | +import urllib.request |
| 45 | +from datetime import datetime, timezone |
| 46 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 47 | +from pathlib import Path |
| 48 | +from typing import Any |
| 49 | + |
| 50 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 51 | +sys.path.insert(0, str(REPO_ROOT / "td_component")) |
| 52 | +sys.path.insert(0, str(REPO_ROOT / "tests")) |
| 53 | + |
| 54 | +# Capture and replay use the SAME stub dispatcher so a fixture |
| 55 | +# captured against version N replays cleanly against version N+1 |
| 56 | +# unless the agent's tool-use logic itself changes. |
| 57 | +from _mock_dispatcher import default_tools_for_capture, stub_dispatcher # type: ignore[import-not-found] |
| 58 | +from tdpilot_api_agent import Agent, AgentError # type: ignore[import-not-found] |
| 59 | +from tdpilot_api_config import fetch_api_key # type: ignore[import-not-found] |
| 60 | + |
| 61 | +# --------------------------------------------------------------------------- |
| 62 | +# Recording proxy server |
| 63 | +# --------------------------------------------------------------------------- |
| 64 | + |
| 65 | + |
| 66 | +class RecordingProxy: |
| 67 | + """HTTP proxy that forwards POSTs to real DeepSeek and records both |
| 68 | + sides for fixture writing. Single-threaded — captures are |
| 69 | + sequential and we don't want concurrent overlap. |
| 70 | + """ |
| 71 | + |
| 72 | + def __init__(self, real_base_url: str, api_key: str) -> None: |
| 73 | + self.real_base_url = real_base_url.rstrip("/") |
| 74 | + self.api_key = api_key |
| 75 | + self.exchanges: list[dict] = [] |
| 76 | + self._httpd: HTTPServer | None = None |
| 77 | + self._thread: threading.Thread | None = None |
| 78 | + |
| 79 | + @property |
| 80 | + def base_url(self) -> str: |
| 81 | + if self._httpd is None: |
| 82 | + raise RuntimeError("not started") |
| 83 | + host, port = self._httpd.server_address[:2] |
| 84 | + if isinstance(host, bytes): |
| 85 | + host = host.decode("ascii") |
| 86 | + return f"http://{host}:{port}/anthropic" |
| 87 | + |
| 88 | + def start(self) -> None: |
| 89 | + owner = self |
| 90 | + |
| 91 | + class _Handler(BaseHTTPRequestHandler): |
| 92 | + def log_message(self, fmt, *args): |
| 93 | + return |
| 94 | + |
| 95 | + def do_POST(self): # noqa: N802 |
| 96 | + owner._handle_post(self) |
| 97 | + |
| 98 | + self._httpd = HTTPServer(("127.0.0.1", 0), _Handler) |
| 99 | + self._thread = threading.Thread( |
| 100 | + target=self._httpd.serve_forever, |
| 101 | + name="RecordingProxy", |
| 102 | + daemon=True, |
| 103 | + ) |
| 104 | + self._thread.start() |
| 105 | + |
| 106 | + def stop(self) -> None: |
| 107 | + if self._httpd is None: |
| 108 | + return |
| 109 | + self._httpd.shutdown() |
| 110 | + self._httpd.server_close() |
| 111 | + if self._thread is not None: |
| 112 | + self._thread.join(timeout=3.0) |
| 113 | + self._httpd = None |
| 114 | + self._thread = None |
| 115 | + |
| 116 | + def __enter__(self) -> RecordingProxy: |
| 117 | + self.start() |
| 118 | + return self |
| 119 | + |
| 120 | + def __exit__(self, *exc_info) -> None: |
| 121 | + self.stop() |
| 122 | + |
| 123 | + def _handle_post(self, h: BaseHTTPRequestHandler) -> None: |
| 124 | + length = int(h.headers.get("Content-Length", "0") or "0") |
| 125 | + body = h.rfile.read(length) if length else b"" |
| 126 | + try: |
| 127 | + body_dict = json.loads(body.decode("utf-8")) if body else {} |
| 128 | + except (ValueError, UnicodeDecodeError): |
| 129 | + body_dict = {} |
| 130 | + |
| 131 | + if not h.path.endswith("/v1/messages"): |
| 132 | + h.send_response(404) |
| 133 | + h.end_headers() |
| 134 | + return |
| 135 | + |
| 136 | + # Forward to real DeepSeek using the agent's actual headers. |
| 137 | + # We set x-api-key from our stored key; the agent's outbound |
| 138 | + # header (sk-mock) is intentionally replaced — the real |
| 139 | + # endpoint needs the real key. |
| 140 | + target_url = f"{self.real_base_url}/v1/messages" |
| 141 | + req = urllib.request.Request( |
| 142 | + url=target_url, |
| 143 | + method="POST", |
| 144 | + headers={ |
| 145 | + "Content-Type": "application/json", |
| 146 | + "x-api-key": self.api_key, |
| 147 | + "anthropic-version": "2023-06-01", |
| 148 | + }, |
| 149 | + data=body, |
| 150 | + ) |
| 151 | + try: |
| 152 | + with urllib.request.urlopen(req, timeout=120.0) as resp: |
| 153 | + resp_body = resp.read() |
| 154 | + resp_status = resp.status |
| 155 | + except urllib.error.HTTPError as exc: |
| 156 | + resp_body = exc.read() |
| 157 | + resp_status = exc.code |
| 158 | + except Exception as exc: |
| 159 | + err_body = json.dumps({"error": {"message": f"proxy upstream: {exc}"}}).encode() |
| 160 | + h.send_response(502) |
| 161 | + h.send_header("Content-Type", "application/json") |
| 162 | + h.send_header("Content-Length", str(len(err_body))) |
| 163 | + h.end_headers() |
| 164 | + h.wfile.write(err_body) |
| 165 | + return |
| 166 | + |
| 167 | + try: |
| 168 | + resp_dict = json.loads(resp_body.decode("utf-8")) |
| 169 | + except (ValueError, UnicodeDecodeError): |
| 170 | + resp_dict = {} |
| 171 | + |
| 172 | + # Record (request, response) for the fixture. |
| 173 | + self.exchanges.append({"request": body_dict, "response": resp_dict}) |
| 174 | + |
| 175 | + # Pass through to the agent. |
| 176 | + h.send_response(resp_status) |
| 177 | + h.send_header("Content-Type", "application/json") |
| 178 | + h.send_header("Content-Length", str(len(resp_body))) |
| 179 | + h.end_headers() |
| 180 | + h.wfile.write(resp_body) |
| 181 | + |
| 182 | + |
| 183 | +# --------------------------------------------------------------------------- |
| 184 | +# Capture orchestrator |
| 185 | +# --------------------------------------------------------------------------- |
| 186 | + |
| 187 | + |
| 188 | +def capture( |
| 189 | + scenario: str, |
| 190 | + prompts: list[str], |
| 191 | + *, |
| 192 | + real_base_url: str = "https://api.deepseek.com/anthropic", |
| 193 | + model: str = "deepseek-v4-pro", |
| 194 | + model_tier: str = "auto", |
| 195 | + out_dir: Path | None = None, |
| 196 | + system_prompt: str = "", |
| 197 | + tools: list[dict] | None = None, |
| 198 | +) -> Path: |
| 199 | + """Run the agent against the recording proxy and write the fixture |
| 200 | + file. Returns the on-disk path of the written fixture. |
| 201 | + """ |
| 202 | + api_key = fetch_api_key() |
| 203 | + if not api_key: |
| 204 | + raise SystemExit( |
| 205 | + "No DeepSeek API key found. Set TDPILOT_API_KEY or " |
| 206 | + "configure ~/.tdpilot-api/config.json before running." |
| 207 | + ) |
| 208 | + |
| 209 | + out_dir = out_dir or REPO_ROOT / "tests" / "fixtures" / "deepseek" |
| 210 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 211 | + out_path = out_dir / f"{scenario}.json" |
| 212 | + |
| 213 | + text_chunks: list[str] = [] |
| 214 | + tool_calls: list[tuple[str, dict]] = [] |
| 215 | + tool_results: list[tuple[str, Any, bool]] = [] |
| 216 | + |
| 217 | + proxy = RecordingProxy(real_base_url=real_base_url, api_key=api_key) |
| 218 | + proxy.start() |
| 219 | + try: |
| 220 | + agent = Agent( |
| 221 | + api_key="sk-recorder-bypass", # not actually used; proxy injects real key |
| 222 | + dispatcher=stub_dispatcher, |
| 223 | + tools=tools or [], |
| 224 | + system_prompt=system_prompt, |
| 225 | + base_url=proxy.base_url, |
| 226 | + model=model, |
| 227 | + model_tier=model_tier, |
| 228 | + on_text=text_chunks.append, |
| 229 | + on_tool_call=lambda n, a: tool_calls.append((n, a)), |
| 230 | + on_tool_result=lambda n, r, e: tool_results.append((n, r, e)), |
| 231 | + ) |
| 232 | + for prompt in prompts: |
| 233 | + agent.add_user_message(prompt) |
| 234 | + try: |
| 235 | + agent.run_turn() |
| 236 | + except AgentError as exc: |
| 237 | + print(f"[capture] AgentError on prompt {prompt!r}: {exc}", file=sys.stderr) |
| 238 | + break |
| 239 | + finally: |
| 240 | + proxy.stop() |
| 241 | + |
| 242 | + fixture = { |
| 243 | + "scenario": scenario, |
| 244 | + "captured_at": datetime.now(timezone.utc).isoformat(timespec="seconds"), |
| 245 | + "model": model, |
| 246 | + "model_tier": model_tier, |
| 247 | + "prompts": list(prompts), |
| 248 | + "tool_calls_observed": [{"name": n, "args": a} for n, a in tool_calls], |
| 249 | + "final_text_concatenated": "\n".join(text_chunks), |
| 250 | + "exchanges": proxy.exchanges, |
| 251 | + } |
| 252 | + out_path.write_text( |
| 253 | + json.dumps(fixture, indent=2, ensure_ascii=False, sort_keys=False), |
| 254 | + encoding="utf-8", |
| 255 | + ) |
| 256 | + print( |
| 257 | + f"[capture] wrote {out_path} — {len(proxy.exchanges)} exchange(s), " |
| 258 | + f"{len(tool_calls)} tool call(s), {len(text_chunks)} text chunk(s)" |
| 259 | + ) |
| 260 | + return out_path |
| 261 | + |
| 262 | + |
| 263 | +# --------------------------------------------------------------------------- |
| 264 | +# CLI |
| 265 | +# --------------------------------------------------------------------------- |
| 266 | + |
| 267 | + |
| 268 | +def main() -> None: |
| 269 | + p = argparse.ArgumentParser(description=__doc__) |
| 270 | + p.add_argument("--scenario", required=True, help="fixture name (file stem)") |
| 271 | + p.add_argument( |
| 272 | + "--prompt", |
| 273 | + action="append", |
| 274 | + required=True, |
| 275 | + help="user prompt (repeat for multi-turn)", |
| 276 | + ) |
| 277 | + p.add_argument( |
| 278 | + "--model-tier", |
| 279 | + default="auto", |
| 280 | + choices=("auto", "flash", "pro"), |
| 281 | + help="model routing tier", |
| 282 | + ) |
| 283 | + p.add_argument( |
| 284 | + "--model", |
| 285 | + default="deepseek-v4-pro", |
| 286 | + help="model name when tier=pro", |
| 287 | + ) |
| 288 | + p.add_argument( |
| 289 | + "--system-prompt", |
| 290 | + default="You are TDPilot, an assistant inside TouchDesigner. Use tools when needed.", |
| 291 | + help="system prompt for the capture session", |
| 292 | + ) |
| 293 | + p.add_argument( |
| 294 | + "--out-dir", |
| 295 | + type=Path, |
| 296 | + default=None, |
| 297 | + help="override the fixture output directory (default: tests/fixtures/deepseek)", |
| 298 | + ) |
| 299 | + args = p.parse_args() |
| 300 | + |
| 301 | + capture( |
| 302 | + scenario=args.scenario, |
| 303 | + prompts=list(args.prompt), |
| 304 | + model=args.model, |
| 305 | + model_tier=args.model_tier, |
| 306 | + system_prompt=args.system_prompt, |
| 307 | + out_dir=args.out_dir, |
| 308 | + tools=default_tools_for_capture(), |
| 309 | + ) |
| 310 | + |
| 311 | + |
| 312 | +if __name__ == "__main__": |
| 313 | + main() |
0 commit comments