mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 23:09:19 +08:00
feat: 重构 Agent 目录为 api/agent/infrastructure 分层并更新启动入口
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""编排层:工具装载与 LangGraph 扩展点(不写 Mongo)。"""
|
||||
@@ -0,0 +1 @@
|
||||
"""LangGraph 图定义预留。"""
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Agent 编排:工具装载与(预留)图运行时;不写 Mongo。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from config.settings import Settings
|
||||
|
||||
|
||||
async def load_tools_for_turn(
|
||||
settings: Settings, *, company_id: int, user_id: str
|
||||
) -> list[Any]:
|
||||
"""本轮可用工具 = 本地 builtin + MCP(按租户头)。"""
|
||||
from agent.tools.builtin import builtin_tools
|
||||
from infrastructure.mcp.client import load_mcp_tools
|
||||
|
||||
mcp_tools = await load_mcp_tools(
|
||||
settings, company_id=company_id, user_id=user_id
|
||||
)
|
||||
return [*builtin_tools(), *mcp_tools]
|
||||
@@ -0,0 +1,18 @@
|
||||
"""Agent 运行态占位(LangGraph State 后续落此)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentTurnState:
|
||||
"""单轮对话编排状态(v1 尚未被图驱动,仅作扩展点)。"""
|
||||
|
||||
company_id: int
|
||||
user_id: str
|
||||
thread_id: str
|
||||
memory_lines: list[str] = field(default_factory=list)
|
||||
tool_names: list[str] = field(default_factory=list)
|
||||
extras: dict[str, Any] = field(default_factory=dict)
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Agent 工具面:本地 builtin;MCP 传输在 infrastructure.mcp。"""
|
||||
|
||||
from agent.tools.builtin import builtin_tools
|
||||
|
||||
__all__ = ["builtin_tools"]
|
||||
@@ -0,0 +1,47 @@
|
||||
"""本地工具:不依赖 MCP / 业务库,只提供模型无法自行得知的环境信息。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import date, datetime, timedelta
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from langchain_core.tools import BaseTool, tool
|
||||
|
||||
# 与后端业务库、BI 日期口径一致
|
||||
_TZ = ZoneInfo("Asia/Shanghai")
|
||||
_WEEKDAYS = ("周一", "周二", "周三", "周四", "周五", "周六", "周日")
|
||||
|
||||
|
||||
def builtin_tools() -> list[BaseTool]:
|
||||
return [get_current_time]
|
||||
|
||||
|
||||
@tool
|
||||
def get_current_time() -> str:
|
||||
"""获取当前业务时间(Asia/Shanghai)。
|
||||
|
||||
用户说近 N 天、昨天、本周、本月等相对日期时必须先调用本工具,
|
||||
用返回的 ranges 或 today 换算 yyyy-MM-dd 后再查经营数据,不要向用户索要日期。
|
||||
近 N 天含今天,共 N 个自然日。
|
||||
"""
|
||||
now = datetime.now(_TZ)
|
||||
today = now.date()
|
||||
payload = {
|
||||
"timezone": "Asia/Shanghai",
|
||||
"now": now.isoformat(timespec="seconds"),
|
||||
"today": today.isoformat(),
|
||||
"weekday": _WEEKDAYS[today.weekday()],
|
||||
"ranges": {
|
||||
"yesterday": _span(today - timedelta(days=1), today - timedelta(days=1)),
|
||||
"last_7_days": _span(today - timedelta(days=6), today),
|
||||
"last_30_days": _span(today - timedelta(days=29), today),
|
||||
"this_week": _span(today - timedelta(days=today.weekday()), today),
|
||||
"this_month": _span(today.replace(day=1), today),
|
||||
},
|
||||
}
|
||||
return json.dumps(payload, ensure_ascii=False)
|
||||
|
||||
|
||||
def _span(start: date, end: date) -> dict[str, str]:
|
||||
return {"startDate": start.isoformat(), "endDate": end.isoformat()}
|
||||
Reference in New Issue
Block a user