feat(ai_agent): 新增openclaw_hermes混合AI适配器 新增OpenClawHermesAdapter适配器,结合Hermes记忆功能和OpenClaw执行能力, 实现AI问答流程中的记忆召回、执行操作和记忆存储的完整闭环。 同时更新NoopAdapter提示信息,添加新的模型提供商选项。 feat(business): 新增考勤、工作报告和风险事件业务模型 新增AttendanceRecord、WorkReport、RiskEvent和LegacySyncRun四个业务模型, 扩展业务领域注册表,支持考勤管理、工作报告生成和风险事件跟踪等核心业务功能。 feat(reports): 实现考勤汇总和工作日报周报生成功能 新增attendance_summary方法用于统计每日考勤情况, 新增generate_work_report方法用于生成日/周经营报告, 包含任务完成情况、待处理事项和风险指标等综合信息。 feat(risk): 扩展风险管理API端点和供应商风险检测 新增供应商风险查询端点和风险事件管理端点, 提供风险事件列表查询和自动生成功能, 增强供应商风险评估能力。 feat(feishu): 添加考勤查询命令和风险摘要增强 集成考勤汇总查询功能到飞书命令系统, 在风险摘要中添加供应商风险和开放风险事件统计, 丰富日常经营管理信息展示。 refactor(service): 优化业务服务数据验证和类型转换 重构_model_payload函数实现数据验证和类型转换, 添加列值类型强制转换逻辑,提高API数据处理的准确性和安全性。 build(deps): 添加postgresql数据库驱动依赖 在Dockerfile中添加psycopg[binary]==3.2.3依赖包, 支持PostgreSQL数据库连接和操作。 chore(config): 更新.gitignore文件排除备份和迁移目录 在.gitignore中添加AGENTS.md.bak-*和migration/目录排除规则, 避免备份文件和本地迁移工作区被提交到版本控制系统。 ```
205 lines
7.7 KiB
Python
205 lines
7.7 KiB
Python
from abc import ABC, abstractmethod
|
|
from typing import Any
|
|
|
|
import httpx
|
|
from fastapi import HTTPException
|
|
|
|
from app.core.config import Settings, get_settings
|
|
|
|
|
|
class AIAdapter(ABC):
|
|
"""Interface for model provider adapters."""
|
|
|
|
provider_name: str
|
|
|
|
@abstractmethod
|
|
def ask(self, prompt: str, context: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
raise NotImplementedError
|
|
|
|
|
|
class NoopAdapter(AIAdapter):
|
|
"""Deterministic adapter used when no model provider is configured."""
|
|
|
|
provider_name = "noop"
|
|
|
|
def ask(self, prompt: str, context: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
return {
|
|
"answer": (
|
|
"AI provider is not configured yet. This is a deterministic placeholder. "
|
|
"Set MODEL_PROVIDER to openclaw_hermes, openclaw, hermes, or direct_llm "
|
|
"after credentials are ready."
|
|
),
|
|
"raw": {"prompt": prompt, "context": context or {}},
|
|
}
|
|
|
|
|
|
class OpenClawAdapter(AIAdapter):
|
|
"""Adapter for an OpenClaw-compatible agent endpoint."""
|
|
|
|
provider_name = "openclaw"
|
|
|
|
def __init__(self, settings: Settings):
|
|
self.settings = settings
|
|
|
|
def ask(self, prompt: str, context: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
url = f"{self.settings.openclaw_base_url.rstrip('/')}/api/v1/agent/ask"
|
|
headers = {}
|
|
if self.settings.openclaw_api_key:
|
|
headers["Authorization"] = f"Bearer {self.settings.openclaw_api_key}"
|
|
payload = {"prompt": prompt, "context": context or {}}
|
|
with httpx.Client(timeout=60) as client:
|
|
response = client.post(url, json=payload, headers=headers)
|
|
if response.status_code >= 400:
|
|
raise HTTPException(status_code=502, detail={"openclaw_error": response.text})
|
|
data = response.json()
|
|
return {"answer": data.get("answer") or data.get("content") or str(data), "raw": data}
|
|
|
|
|
|
class HermesAdapter(AIAdapter):
|
|
"""Adapter for a Hermes-compatible memory or agent endpoint."""
|
|
|
|
provider_name = "hermes"
|
|
|
|
def __init__(self, settings: Settings):
|
|
self.settings = settings
|
|
|
|
def ask(self, prompt: str, context: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
url = f"{self.settings.hermes_base_url.rstrip('/')}/api/v1/ask"
|
|
headers = {}
|
|
if self.settings.hermes_api_key:
|
|
headers["Authorization"] = f"Bearer {self.settings.hermes_api_key}"
|
|
payload = {"prompt": prompt, "context": context or {}}
|
|
with httpx.Client(timeout=60) as client:
|
|
response = client.post(url, json=payload, headers=headers)
|
|
if response.status_code >= 400:
|
|
raise HTTPException(status_code=502, detail={"hermes_error": response.text})
|
|
data = response.json()
|
|
return {"answer": data.get("answer") or data.get("content") or str(data), "raw": data}
|
|
|
|
|
|
class OpenClawHermesAdapter(AIAdapter):
|
|
"""Compose Hermes memory with OpenClaw execution."""
|
|
|
|
provider_name = "openclaw_hermes"
|
|
|
|
def __init__(self, settings: Settings):
|
|
self.openclaw = OpenClawAdapter(settings)
|
|
self.hermes = HermesAdapter(settings)
|
|
|
|
def ask(self, prompt: str, context: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
base_context = context or {}
|
|
recall = self._recall_memory(prompt, base_context)
|
|
openclaw_context = {
|
|
**base_context,
|
|
"agent_pipeline": self.provider_name,
|
|
"hermes_memory": recall["answer"],
|
|
}
|
|
openclaw_result = self.openclaw.ask(prompt, openclaw_context)
|
|
remember = self._remember_interaction(prompt, base_context, openclaw_result["answer"])
|
|
return {
|
|
"answer": openclaw_result["answer"],
|
|
"raw": {
|
|
"pipeline": "hermes_recall -> openclaw_answer -> hermes_remember",
|
|
"hermes_recall": recall,
|
|
"openclaw": openclaw_result.get("raw", {}),
|
|
"hermes_remember": remember,
|
|
},
|
|
}
|
|
|
|
def _recall_memory(self, prompt: str, context: dict[str, Any]) -> dict[str, Any]:
|
|
recall_prompt = (
|
|
"Retrieve concise long-term memory, preferences, prior decisions, and relevant "
|
|
"business context for this request. Return only information useful to answer it."
|
|
)
|
|
try:
|
|
result = self.hermes.ask(
|
|
recall_prompt,
|
|
{
|
|
"mode": "memory_recall",
|
|
"user_prompt": prompt,
|
|
"request_context": context,
|
|
},
|
|
)
|
|
except Exception as exc: # Hermes memory should not block OpenClaw execution.
|
|
return {"answer": "", "raw": {}, "error": _error_detail(exc)}
|
|
return {"answer": result["answer"], "raw": result.get("raw", {})}
|
|
|
|
def _remember_interaction(
|
|
self,
|
|
prompt: str,
|
|
context: dict[str, Any],
|
|
answer: str,
|
|
) -> dict[str, Any]:
|
|
remember_prompt = (
|
|
"Store durable lessons from this interaction for future company management "
|
|
"assistance. Ignore transient details and do not store secrets."
|
|
)
|
|
try:
|
|
result = self.hermes.ask(
|
|
remember_prompt,
|
|
{
|
|
"mode": "memory_write",
|
|
"user_prompt": prompt,
|
|
"request_context": context,
|
|
"assistant_answer": answer,
|
|
},
|
|
)
|
|
except Exception as exc:
|
|
return {"ok": False, "raw": {}, "error": _error_detail(exc)}
|
|
return {"ok": True, "raw": result.get("raw", {}), "answer": result["answer"]}
|
|
|
|
|
|
class DirectLLMAdapter(AIAdapter):
|
|
"""Adapter for OpenAI-compatible chat completions APIs."""
|
|
|
|
provider_name = "direct_llm"
|
|
|
|
def __init__(self, settings: Settings):
|
|
self.settings = settings
|
|
|
|
def ask(self, prompt: str, context: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
if not self.settings.direct_llm_api_key:
|
|
raise HTTPException(status_code=503, detail="DIRECT_LLM_API_KEY is not configured")
|
|
url = f"{self.settings.direct_llm_base_url.rstrip('/')}/chat/completions"
|
|
headers = {"Authorization": f"Bearer {self.settings.direct_llm_api_key}"}
|
|
messages = [
|
|
{
|
|
"role": "system",
|
|
"content": (
|
|
"You are a company management AI. Be concise, cite data from context, "
|
|
"and never approve payments, performance changes, or trades automatically."
|
|
),
|
|
},
|
|
{"role": "user", "content": f"Context:\n{context or {}}\n\nTask:\n{prompt}"},
|
|
]
|
|
payload = {"model": self.settings.direct_llm_model, "messages": messages}
|
|
with httpx.Client(timeout=60) as client:
|
|
response = client.post(url, json=payload, headers=headers)
|
|
if response.status_code >= 400:
|
|
raise HTTPException(status_code=502, detail={"llm_error": response.text})
|
|
data = response.json()
|
|
answer = data["choices"][0]["message"]["content"]
|
|
return {"answer": answer, "raw": data}
|
|
|
|
|
|
def get_adapter() -> AIAdapter:
|
|
"""Return the configured AI provider adapter."""
|
|
|
|
settings = get_settings()
|
|
provider = settings.model_provider.lower()
|
|
if provider == "openclaw":
|
|
return OpenClawAdapter(settings)
|
|
if provider == "hermes":
|
|
return HermesAdapter(settings)
|
|
if provider in {"openclaw_hermes", "openclaw-hermes", "hybrid"}:
|
|
return OpenClawHermesAdapter(settings)
|
|
if provider == "direct_llm":
|
|
return DirectLLMAdapter(settings)
|
|
return NoopAdapter()
|
|
|
|
|
|
def _error_detail(exc: Exception) -> Any:
|
|
if isinstance(exc, HTTPException):
|
|
return exc.detail
|
|
return {"type": type(exc).__name__, "message": str(exc)}
|