TBC/agent/llm_client.py
D-Robotics 637b1026df release: v1.2.0 - 安全加固与性能优化
Security:
- 添加 API 认证中间件 (PLATFORM_API_TOKEN Bearer Token)
- SSRF 防护: 校验 git URL scheme + 过滤内网 IP
- 路径遍历修复: module 参数添加 is_relative_to 校验
- 异常信息泄露修复: 统一错误处理不返回内部信息
- API Key 不持久化到数据库
- 硬编码 SSH Key 移至环境变量
- CORS 生产环境校验

Rate Limiting:
- 新增内存滑动窗口速率限制 (6个POST端点)

Performance:
- ThreadPoolExecutor(max_workers=4) 限制并发
- 任务超时 _enforce_proc_timeout (默认3600s)
- JSON 原子写入 tempfile+os.replace
- 日志轮转 RotatingFileHandler
- httpx 连接池配置
- 随机名改用 uuid4
- 前端 Error Boundary
- 前端统一 EventSource 使用

Docs:
- 更新 .env.example, CHANGELOG, API.md, CONFIG.md, AGENTS.md
- .gitignore 添加 .venv/ 和 backend/db.json
2026-07-16 15:00:05 +08:00

115 lines
3.6 KiB
Python

import os
import json
import httpx
from dataclasses import dataclass, field
from typing import Optional
@dataclass
class LLMConfig:
provider: str = "deepseek"
model: str = "deepseek-chat"
base_url: str = "https://api.deepseek.com"
api_key: str = ""
temperature: float = 0.3
max_tokens: int = 8192
@dataclass
class LLMMessage:
role: str
content: str
@dataclass
class LLMResponse:
content: str
usage: dict = field(default_factory=dict)
class LLMClient:
def __init__(self, config: LLMConfig):
self.config = config
self._client = httpx.Client(
timeout=120.0,
limits=httpx.Limits(max_connections=10, max_keepalive_connections=5),
)
if config.provider == "deepseek":
self.config.base_url = self.config.base_url.rstrip("/")
def chat(self, messages: list[LLMMessage], system_prompt: str = "") -> LLMResponse:
full_messages = []
if system_prompt:
full_messages.append({"role": "system", "content": system_prompt})
for msg in messages:
full_messages.append({"role": msg.role, "content": msg.content})
headers = {
"Authorization": f"Bearer {self.config.api_key}",
"Content-Type": "application/json",
}
if self.config.provider == "deepseek":
url = f"{self.config.base_url}/v1/chat/completions"
else:
url = f"{self.config.base_url}/chat/completions"
payload = {
"model": self.config.model,
"messages": full_messages,
"temperature": self.config.temperature,
"max_tokens": self.config.max_tokens,
}
response = self._client.post(url, headers=headers, json=payload)
response.raise_for_status()
data = response.json()
content = data["choices"][0]["message"]["content"]
usage = data.get("usage", {})
return LLMResponse(content=content, usage=usage)
def chat_stream(self, messages: list[LLMMessage], system_prompt: str = ""):
full_messages = []
if system_prompt:
full_messages.append({"role": "system", "content": system_prompt})
for msg in messages:
full_messages.append({"role": msg.role, "content": msg.content})
headers = {
"Authorization": f"Bearer {self.config.api_key}",
"Content-Type": "application/json",
}
if self.config.provider == "deepseek":
url = f"{self.config.base_url}/v1/chat/completions"
else:
url = f"{self.config.base_url}/chat/completions"
payload = {
"model": self.config.model,
"messages": full_messages,
"temperature": self.config.temperature,
"max_tokens": self.config.max_tokens,
"stream": True,
}
with self._client.stream("POST", url, headers=headers, json=payload) as response:
response.raise_for_status()
for line in response.iter_lines():
if line.startswith("data: "):
data_str = line[6:]
if data_str == "[DONE]":
break
try:
data = json.loads(data_str)
delta = data["choices"][0].get("delta", {})
content = delta.get("content", "")
if content:
yield content
except json.JSONDecodeError:
continue
def close(self):
self._client.close()