feat(PHASE-03): 环境感知 + 稳定性约束 + Windows 防重复试错 + max_turns=12

This commit is contained in:
hc
2026-04-10 20:34:56 +08:00
parent 1d66019529
commit 587331cbb8
4 changed files with 39 additions and 4 deletions
+26 -2
View File
@@ -2,6 +2,8 @@ from __future__ import annotations
import json
import os
import platform
import sys
import tomllib
from dataclasses import dataclass
from json import JSONDecodeError
@@ -20,7 +22,7 @@ class Config:
model: str
api_key: str
base_url: str | None
max_turns: int = 8
max_turns: int = 12
def resolve_config(workspace: Path, cli: dict[str, Any]) -> Config:
@@ -40,7 +42,7 @@ def resolve_config(workspace: Path, cli: dict[str, Any]) -> Config:
"",
)
base_url = _pick(cli.get("base_url"), os.getenv("CC_SLIM_BASE_URL"), file_cfg.get("base_url"), None)
max_turns_raw = _pick(cli.get("max_turns"), os.getenv("CC_SLIM_MAX_TURNS"), file_cfg.get("max_turns"), 8)
max_turns_raw = _pick(cli.get("max_turns"), os.getenv("CC_SLIM_MAX_TURNS"), file_cfg.get("max_turns"), 12)
if not api_key:
raise ValueError("缺少 API key,请通过 CLI、环境变量或 .cc-slim.toml 提供。")
@@ -120,6 +122,8 @@ class Agent:
def _build_system_prompt(self, workspace: Path) -> str:
parts: list[str] = []
parts.append(self._build_runtime_summary(workspace))
agents = workspace / "AGENTS.md"
if agents.exists():
parts.append(agents.read_text(encoding="utf-8"))
@@ -131,6 +135,26 @@ class Agent:
return "\n\n".join(part.strip() for part in parts if part.strip())
def _build_runtime_summary(self, workspace: Path) -> str:
tool_names = ", ".join(self.tools.keys()) or "(none)"
shell_name = self._detect_shell()
return "\n".join(
[
"## 运行环境",
f"- 平台: {platform.system() or 'Unknown'}",
f"- sys.platform: {sys.platform}",
f"- shell: {shell_name}",
f"- workspace: {workspace}",
f"- 可用工具: {tool_names}",
"- 行动时必须以以上运行环境信息为准,不要默认套用 Unix/Linux 命令习惯。",
]
)
def _detect_shell(self) -> str:
if os.name == "nt":
return os.getenv("COMSPEC", "Windows shell (likely PowerShell or cmd.exe)")
return os.getenv("SHELL", "unknown shell")
def _call_openai(self) -> dict[str, Any]:
response = self.client.chat.completions.create(
model=self.config.model,