sycommon-python-lib 0.2.3a12__py3-none-any.whl → 0.2.4a0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sycommon/agent/__init__.py +26 -0
- sycommon/agent/deep_agent.py +6 -2
- sycommon/agent/mcp/__init__.py +30 -0
- sycommon/agent/mcp/models.py +56 -0
- sycommon/agent/mcp/tool_loader.py +174 -0
- sycommon/agent/multi_agent_team.py +6 -2
- sycommon/config/PgConfig.py +54 -0
- sycommon/database/pg_checkpoint_service.py +124 -0
- sycommon/llm/get_llm.py +1 -2
- sycommon/services.py +18 -2
- {sycommon_python_lib-0.2.3a12.dist-info → sycommon_python_lib-0.2.4a0.dist-info}/METADATA +8 -6
- {sycommon_python_lib-0.2.3a12.dist-info → sycommon_python_lib-0.2.4a0.dist-info}/RECORD +15 -10
- {sycommon_python_lib-0.2.3a12.dist-info → sycommon_python_lib-0.2.4a0.dist-info}/WHEEL +0 -0
- {sycommon_python_lib-0.2.3a12.dist-info → sycommon_python_lib-0.2.4a0.dist-info}/entry_points.txt +0 -0
- {sycommon_python_lib-0.2.3a12.dist-info → sycommon_python_lib-0.2.4a0.dist-info}/top_level.txt +0 -0
sycommon/agent/__init__.py
CHANGED
|
@@ -105,6 +105,18 @@ from sycommon.agent.chat_events import (
|
|
|
105
105
|
error_event,
|
|
106
106
|
cancelled_event,
|
|
107
107
|
)
|
|
108
|
+
from sycommon.agent.mcp import (
|
|
109
|
+
MCPToolStatus,
|
|
110
|
+
MCPServerConfig,
|
|
111
|
+
MCPServerCreateRequest,
|
|
112
|
+
MCPServerUpdateRequest,
|
|
113
|
+
MCPServerTestRequest,
|
|
114
|
+
MCPServerTestResult,
|
|
115
|
+
load_mcp_tools,
|
|
116
|
+
test_mcp_connection,
|
|
117
|
+
sanitize_name,
|
|
118
|
+
)
|
|
119
|
+
from sycommon.database.pg_checkpoint_service import PgCheckpointService
|
|
108
120
|
|
|
109
121
|
__all__ = [
|
|
110
122
|
# 沙箱
|
|
@@ -147,4 +159,18 @@ __all__ = [
|
|
|
147
159
|
"done_event",
|
|
148
160
|
"error_event",
|
|
149
161
|
"cancelled_event",
|
|
162
|
+
|
|
163
|
+
# MCP 工具集成
|
|
164
|
+
"MCPToolStatus",
|
|
165
|
+
"MCPServerConfig",
|
|
166
|
+
"MCPServerCreateRequest",
|
|
167
|
+
"MCPServerUpdateRequest",
|
|
168
|
+
"MCPServerTestRequest",
|
|
169
|
+
"MCPServerTestResult",
|
|
170
|
+
"load_mcp_tools",
|
|
171
|
+
"test_mcp_connection",
|
|
172
|
+
"sanitize_name",
|
|
173
|
+
|
|
174
|
+
# PG Checkpoint 服务
|
|
175
|
+
"PgCheckpointService",
|
|
150
176
|
]
|
sycommon/agent/deep_agent.py
CHANGED
|
@@ -613,8 +613,12 @@ async def create_deep_agent(
|
|
|
613
613
|
|
|
614
614
|
# 创建 checkpointer
|
|
615
615
|
if checkpointer is None:
|
|
616
|
-
from
|
|
617
|
-
|
|
616
|
+
from sycommon.database.pg_checkpoint_service import PgCheckpointService
|
|
617
|
+
if PgCheckpointService.is_initialized():
|
|
618
|
+
checkpointer = await PgCheckpointService.get_checkpointer()
|
|
619
|
+
else:
|
|
620
|
+
from langgraph.checkpoint.memory import MemorySaver
|
|
621
|
+
checkpointer = MemorySaver()
|
|
618
622
|
|
|
619
623
|
tid = thread_id or user_id
|
|
620
624
|
agent_config = {"configurable": {"thread_id": tid}}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""MCP 工具集成模块
|
|
2
|
+
|
|
3
|
+
提供 MCP 服务器连接、工具发现和加载功能。
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from sycommon.agent.mcp.models import (
|
|
7
|
+
MCPToolStatus,
|
|
8
|
+
MCPServerConfig,
|
|
9
|
+
MCPServerCreateRequest,
|
|
10
|
+
MCPServerUpdateRequest,
|
|
11
|
+
MCPServerTestRequest,
|
|
12
|
+
MCPServerTestResult,
|
|
13
|
+
)
|
|
14
|
+
from sycommon.agent.mcp.tool_loader import (
|
|
15
|
+
load_mcp_tools,
|
|
16
|
+
test_mcp_connection,
|
|
17
|
+
sanitize_name,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"MCPToolStatus",
|
|
22
|
+
"MCPServerConfig",
|
|
23
|
+
"MCPServerCreateRequest",
|
|
24
|
+
"MCPServerUpdateRequest",
|
|
25
|
+
"MCPServerTestRequest",
|
|
26
|
+
"MCPServerTestResult",
|
|
27
|
+
"load_mcp_tools",
|
|
28
|
+
"test_mcp_connection",
|
|
29
|
+
"sanitize_name",
|
|
30
|
+
]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""MCP 服务器配置数据模型"""
|
|
2
|
+
|
|
3
|
+
from typing import Optional, List
|
|
4
|
+
from pydantic import BaseModel
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MCPToolStatus(BaseModel):
|
|
8
|
+
"""MCP 工具可用状态"""
|
|
9
|
+
tool_name: str
|
|
10
|
+
description: Optional[str] = None
|
|
11
|
+
available: bool = True
|
|
12
|
+
last_check_at: Optional[str] = None
|
|
13
|
+
last_error: Optional[str] = None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class MCPServerConfig(BaseModel):
|
|
17
|
+
id: str
|
|
18
|
+
user_id: Optional[str] = None
|
|
19
|
+
name: str
|
|
20
|
+
server_url: str
|
|
21
|
+
description: Optional[str] = None
|
|
22
|
+
headers: Optional[dict] = None
|
|
23
|
+
enabled: bool = True
|
|
24
|
+
sanitized_name: str = ""
|
|
25
|
+
created_at: str
|
|
26
|
+
updated_at: str
|
|
27
|
+
tools: Optional[List[MCPToolStatus]] = None
|
|
28
|
+
server_status: Optional[str] = None
|
|
29
|
+
server_error: Optional[str] = None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class MCPServerCreateRequest(BaseModel):
|
|
33
|
+
name: str
|
|
34
|
+
server_url: str
|
|
35
|
+
description: Optional[str] = None
|
|
36
|
+
headers: Optional[dict] = None
|
|
37
|
+
enabled: bool = True
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class MCPServerUpdateRequest(BaseModel):
|
|
41
|
+
name: Optional[str] = None
|
|
42
|
+
server_url: Optional[str] = None
|
|
43
|
+
description: Optional[str] = None
|
|
44
|
+
headers: Optional[dict] = None
|
|
45
|
+
enabled: Optional[bool] = None
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class MCPServerTestRequest(BaseModel):
|
|
49
|
+
server_url: str
|
|
50
|
+
headers: Optional[dict] = None
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class MCPServerTestResult(BaseModel):
|
|
54
|
+
success: bool
|
|
55
|
+
tools: Optional[list] = None
|
|
56
|
+
error: Optional[str] = None
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"""MCP 工具加载器
|
|
2
|
+
|
|
3
|
+
使用 langchain-mcp-adapters 官方库连接远程 MCP 服务器,
|
|
4
|
+
发现工具并作为 LangChain BaseTool 注入 Agent。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import re
|
|
8
|
+
import hashlib
|
|
9
|
+
from typing import List, Optional, Callable, Awaitable
|
|
10
|
+
|
|
11
|
+
from langchain_core.tools import BaseTool
|
|
12
|
+
|
|
13
|
+
from sycommon.logging.kafka_log import SYLogger
|
|
14
|
+
from sycommon.agent.mcp.models import MCPServerConfig
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def sanitize_name(name: str) -> str:
|
|
18
|
+
"""将名称转为合法的标识符,中文等非ASCII字符做 transliterate"""
|
|
19
|
+
sanitized = re.sub(r'[^a-zA-Z0-9_]', '_', name).strip('_')
|
|
20
|
+
if not sanitized:
|
|
21
|
+
h = hashlib.md5(name.encode()).hexdigest()[:8]
|
|
22
|
+
sanitized = f"mcp_{h}"
|
|
23
|
+
return sanitized
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def wrap_tool_with_error_handler(
|
|
27
|
+
tool: BaseTool,
|
|
28
|
+
server_config_id: str,
|
|
29
|
+
server_name: str,
|
|
30
|
+
original_tool_name: str,
|
|
31
|
+
on_tool_success: Optional[Callable[[str, str], Awaitable[None]]] = None,
|
|
32
|
+
on_tool_error: Optional[Callable[[str, str, str], Awaitable[None]]] = None,
|
|
33
|
+
) -> BaseTool:
|
|
34
|
+
"""包装 MCP 工具的 coroutine,捕获连接/超时等异常,返回友好的错误信息而非抛出异常。
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
on_tool_success: 异步回调 (server_config_id, tool_name) -> None
|
|
38
|
+
on_tool_error: 异步回调 (server_config_id, tool_name, error_msg) -> None
|
|
39
|
+
"""
|
|
40
|
+
original_coroutine = tool.coroutine
|
|
41
|
+
tool_name = tool.name
|
|
42
|
+
|
|
43
|
+
async def _safe_coroutine(*args, **kwargs):
|
|
44
|
+
try:
|
|
45
|
+
result = await original_coroutine(*args, **kwargs)
|
|
46
|
+
if on_tool_success:
|
|
47
|
+
try:
|
|
48
|
+
await on_tool_success(server_config_id, original_tool_name)
|
|
49
|
+
except Exception:
|
|
50
|
+
pass
|
|
51
|
+
return result
|
|
52
|
+
except Exception as e:
|
|
53
|
+
err_type = type(e).__name__
|
|
54
|
+
err_msg = str(e)[:500]
|
|
55
|
+
SYLogger.warning(f"[MCP] 工具 '{tool_name}' 调用失败 ({err_type}): {err_msg}")
|
|
56
|
+
|
|
57
|
+
if on_tool_error:
|
|
58
|
+
try:
|
|
59
|
+
await on_tool_error(server_config_id, original_tool_name, f"{err_type}: {err_msg}")
|
|
60
|
+
except Exception:
|
|
61
|
+
pass
|
|
62
|
+
|
|
63
|
+
friendly_msg = (
|
|
64
|
+
f"MCP 工具调用失败:工具 '{tool_name}' (服务器: {server_name}) 当前不可用。\n"
|
|
65
|
+
f"错误类型: {err_type}\n"
|
|
66
|
+
f"可能原因: MCP 服务器未启动、网络不可达或连接超时。\n"
|
|
67
|
+
f"请尝试不使用该工具继续完成任务,或联系管理员检查 MCP 服务 '{server_name}'。"
|
|
68
|
+
)
|
|
69
|
+
return [friendly_msg]
|
|
70
|
+
|
|
71
|
+
tool.coroutine = _safe_coroutine
|
|
72
|
+
return tool
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
async def load_mcp_tools(
|
|
76
|
+
configs: List[MCPServerConfig],
|
|
77
|
+
on_tool_success: Optional[Callable[[str, str], Awaitable[None]]] = None,
|
|
78
|
+
on_tool_error: Optional[Callable[[str, str, str], Awaitable[None]]] = None,
|
|
79
|
+
on_batch_available: Optional[Callable[[str, list], Awaitable[None]]] = None,
|
|
80
|
+
on_server_failure: Optional[Callable[[str, str], Awaitable[None]]] = None,
|
|
81
|
+
) -> List[BaseTool]:
|
|
82
|
+
"""加载 MCP 工具列表
|
|
83
|
+
|
|
84
|
+
接受 MCPServerConfig 列表,逐个服务器连接并加载工具。
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
configs: MCP 服务器配置列表(仅 enabled 的会被处理)
|
|
88
|
+
on_tool_success: 单个工具调用成功回调
|
|
89
|
+
on_tool_error: 单个工具调用失败回调
|
|
90
|
+
on_batch_available: 服务器连接成功后批量标记工具可用回调 (config_id, tool_names)
|
|
91
|
+
on_server_failure: 服务器连接失败回调 (config_id, error_msg)
|
|
92
|
+
"""
|
|
93
|
+
enabled_configs = [c for c in configs if c.enabled]
|
|
94
|
+
if not enabled_configs:
|
|
95
|
+
return []
|
|
96
|
+
|
|
97
|
+
all_tools = []
|
|
98
|
+
for config in enabled_configs:
|
|
99
|
+
try:
|
|
100
|
+
from langchain_mcp_adapters.client import MultiServerMCPClient
|
|
101
|
+
|
|
102
|
+
key = sanitize_name(config.name)
|
|
103
|
+
client_config = {
|
|
104
|
+
key: {
|
|
105
|
+
"url": config.server_url,
|
|
106
|
+
"transport": "streamable_http",
|
|
107
|
+
**({"headers": config.headers} if config.headers else {}),
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
client = MultiServerMCPClient(client_config)
|
|
112
|
+
tools = await client.get_tools()
|
|
113
|
+
|
|
114
|
+
original_names = []
|
|
115
|
+
for tool in tools:
|
|
116
|
+
original_name = tool.name
|
|
117
|
+
original_names.append(original_name)
|
|
118
|
+
tool.name = f"mcp__{key}__{original_name}"
|
|
119
|
+
if tool.description and not tool.description.startswith("[MCP"):
|
|
120
|
+
tool.description = f"[MCP:{config.name}] {tool.description}"
|
|
121
|
+
wrap_tool_with_error_handler(
|
|
122
|
+
tool, config.id, config.name, original_name,
|
|
123
|
+
on_tool_success=on_tool_success,
|
|
124
|
+
on_tool_error=on_tool_error,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
all_tools.extend(tools)
|
|
128
|
+
|
|
129
|
+
if on_batch_available:
|
|
130
|
+
try:
|
|
131
|
+
await on_batch_available(config.id, original_names)
|
|
132
|
+
except Exception as e:
|
|
133
|
+
SYLogger.warning(f"[MCP] 写入工具状态失败: {e}")
|
|
134
|
+
|
|
135
|
+
SYLogger.info(f"[MCP] 服务器 '{config.name}' 加载了 {len(tools)} 个工具")
|
|
136
|
+
|
|
137
|
+
except Exception as e:
|
|
138
|
+
SYLogger.warning(f"[MCP] 服务器 '{config.name}' ({config.server_url}) 连接失败,跳过: {e}")
|
|
139
|
+
if on_server_failure:
|
|
140
|
+
try:
|
|
141
|
+
await on_server_failure(config.id, str(e)[:300])
|
|
142
|
+
except Exception:
|
|
143
|
+
pass
|
|
144
|
+
|
|
145
|
+
if all_tools:
|
|
146
|
+
SYLogger.info(f"[MCP] 共加载 {len(all_tools)} 个 MCP 工具")
|
|
147
|
+
return all_tools
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
async def test_mcp_connection(server_url: str, headers: dict = None) -> dict:
|
|
151
|
+
"""测试 MCP 服务器连接,返回发现的工具列表"""
|
|
152
|
+
try:
|
|
153
|
+
from langchain_mcp_adapters.client import MultiServerMCPClient
|
|
154
|
+
|
|
155
|
+
client_config = {
|
|
156
|
+
"test": {
|
|
157
|
+
"url": server_url,
|
|
158
|
+
"transport": "streamable_http",
|
|
159
|
+
**({"headers": headers} if headers else {}),
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
client = MultiServerMCPClient(client_config)
|
|
164
|
+
tools = await client.get_tools()
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
"success": True,
|
|
168
|
+
"tools": [{"name": t.name, "description": t.description or ""} for t in tools],
|
|
169
|
+
}
|
|
170
|
+
except Exception as e:
|
|
171
|
+
return {
|
|
172
|
+
"success": False,
|
|
173
|
+
"error": str(e),
|
|
174
|
+
}
|
|
@@ -574,8 +574,12 @@ async def create_multi_agent_team(
|
|
|
574
574
|
|
|
575
575
|
# 创建 checkpointer
|
|
576
576
|
if checkpointer is None:
|
|
577
|
-
from
|
|
578
|
-
|
|
577
|
+
from sycommon.database.pg_checkpoint_service import PgCheckpointService
|
|
578
|
+
if PgCheckpointService.is_initialized():
|
|
579
|
+
checkpointer = await PgCheckpointService.get_checkpointer()
|
|
580
|
+
else:
|
|
581
|
+
from langgraph.checkpoint.memory import MemorySaver
|
|
582
|
+
checkpointer = MemorySaver()
|
|
579
583
|
|
|
580
584
|
tid = thread_id or user_id
|
|
581
585
|
agent_config = {"configurable": {"thread_id": tid}}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from pydantic import BaseModel, Field
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class PgConfig(BaseModel):
|
|
5
|
+
"""PostgreSQL 配置"""
|
|
6
|
+
host: str = Field(default="localhost", description="PG 主机地址")
|
|
7
|
+
port: int = Field(default=5432, description="PG 端口")
|
|
8
|
+
dbname: str = Field(default="postgres", description="数据库名")
|
|
9
|
+
user: str = Field(default="postgres", description="用户名")
|
|
10
|
+
password: str = Field(default="", description="密码")
|
|
11
|
+
min_pool_size: int = Field(default=2, description="连接池最小连接数")
|
|
12
|
+
max_pool_size: int = Field(default=10, description="连接池最大连接数")
|
|
13
|
+
|
|
14
|
+
@classmethod
|
|
15
|
+
def from_dict(cls, config: dict) -> "PgConfig":
|
|
16
|
+
"""从字典解析配置
|
|
17
|
+
|
|
18
|
+
支持两种格式:
|
|
19
|
+
1. 扁平格式: {"host": "...", "port": 5432, ...}
|
|
20
|
+
2. Spring 格式: {"spring": {"datasource": {"url": "jdbc:postgresql://...", ...}}}
|
|
21
|
+
"""
|
|
22
|
+
spring = config.get("spring", {})
|
|
23
|
+
if spring:
|
|
24
|
+
ds = spring.get("datasource", {})
|
|
25
|
+
url = ds.get("url", "")
|
|
26
|
+
return cls._from_jdbc_url(url, ds.get("username", ""), ds.get("password", ""))
|
|
27
|
+
|
|
28
|
+
return cls(
|
|
29
|
+
host=config.get("host", "localhost"),
|
|
30
|
+
port=config.get("port", 5432),
|
|
31
|
+
dbname=config.get("dbname", config.get("database", "postgres")),
|
|
32
|
+
user=config.get("user", config.get("username", "postgres")),
|
|
33
|
+
password=config.get("password", ""),
|
|
34
|
+
min_pool_size=config.get("min_pool_size", 2),
|
|
35
|
+
max_pool_size=config.get("max_pool_size", 10),
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
@classmethod
|
|
39
|
+
def _from_jdbc_url(cls, url: str, username: str, password: str) -> "PgConfig":
|
|
40
|
+
"""从 JDBC URL 解析配置"""
|
|
41
|
+
# jdbc:postgresql://host:port/dbname
|
|
42
|
+
if "://" in url:
|
|
43
|
+
url = url.split("://", 1)[1]
|
|
44
|
+
parts = url.split("/")
|
|
45
|
+
dbname = parts[1] if len(parts) > 1 else "postgres"
|
|
46
|
+
host_port = parts[0].split(":")
|
|
47
|
+
host = host_port[0]
|
|
48
|
+
port = int(host_port[1]) if len(host_port) > 1 else 5432
|
|
49
|
+
return cls(host=host, port=port, dbname=dbname, user=username, password=password)
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def dsn(self) -> str:
|
|
53
|
+
"""生成 psycopg 连接字符串"""
|
|
54
|
+
return f"host={self.host} port={self.port} dbname={self.dbname} user={self.user} password={self.password}"
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""
|
|
2
|
+
PostgreSQL Checkpoint 服务
|
|
3
|
+
|
|
4
|
+
为 LangGraph Agent 提供 PostgreSQL 持久化 checkpoint 存储。
|
|
5
|
+
使用 AsyncPostgresSaver,适配 FastAPI 的异步环境。
|
|
6
|
+
|
|
7
|
+
用法:
|
|
8
|
+
# 通过 Services.plugins 自动初始化
|
|
9
|
+
Services.plugins(app, pg_checkpoint_service=True)
|
|
10
|
+
|
|
11
|
+
# 或手动初始化
|
|
12
|
+
await PgCheckpointService.setup({"host": "10.10.6.203", "port": 5432, ...})
|
|
13
|
+
|
|
14
|
+
# 获取 checkpointer
|
|
15
|
+
checkpointer = await PgCheckpointService.get_checkpointer()
|
|
16
|
+
"""
|
|
17
|
+
import logging
|
|
18
|
+
from typing import Optional, Any
|
|
19
|
+
|
|
20
|
+
from sycommon.config.Config import SingletonMeta
|
|
21
|
+
from sycommon.config.PgConfig import PgConfig
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class PgCheckpointService(metaclass=SingletonMeta):
|
|
25
|
+
"""PostgreSQL Checkpoint 服务(单例)"""
|
|
26
|
+
_checkpointer: Optional[Any] = None
|
|
27
|
+
_initialized: bool = False
|
|
28
|
+
_config: Optional[PgConfig] = None
|
|
29
|
+
_pool: Optional[Any] = None
|
|
30
|
+
|
|
31
|
+
def __init__(self):
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
@classmethod
|
|
35
|
+
async def setup(cls, config: Optional[dict] = None):
|
|
36
|
+
"""初始化 PG Checkpoint 服务
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
config: 配置字典。不传则尝试从 Nacos 读取 pg.yml。
|
|
40
|
+
"""
|
|
41
|
+
if cls._initialized:
|
|
42
|
+
return
|
|
43
|
+
|
|
44
|
+
try:
|
|
45
|
+
if config is None:
|
|
46
|
+
try:
|
|
47
|
+
from sycommon.config.Config import Config
|
|
48
|
+
config = Config().config.get('llm', {}).get('PostgreSQL')
|
|
49
|
+
if not config:
|
|
50
|
+
logging.info("未从 Nacos 获取到 PostgreSQL 配置,PG Checkpoint 服务将禁用")
|
|
51
|
+
return
|
|
52
|
+
logging.info("从 Nacos 获取 PostgreSQL 配置成功")
|
|
53
|
+
except Exception as e:
|
|
54
|
+
logging.info(f"从 Nacos 读取 PostgreSQL 配置失败: {e},PG Checkpoint 服务将禁用")
|
|
55
|
+
return
|
|
56
|
+
|
|
57
|
+
cls._config = PgConfig.from_dict(config)
|
|
58
|
+
|
|
59
|
+
from psycopg_pool import AsyncConnectionPool
|
|
60
|
+
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
|
|
61
|
+
|
|
62
|
+
conn_string = cls._config.dsn
|
|
63
|
+
|
|
64
|
+
cls._pool = AsyncConnectionPool(
|
|
65
|
+
conninfo=conn_string,
|
|
66
|
+
min_size=cls._config.min_pool_size,
|
|
67
|
+
max_size=cls._config.max_pool_size,
|
|
68
|
+
open=False,
|
|
69
|
+
kwargs={"autocommit": True, "prepare_threshold": 0},
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
await cls._pool.open()
|
|
73
|
+
|
|
74
|
+
cls._checkpointer = AsyncPostgresSaver(cls._pool)
|
|
75
|
+
await cls._checkpointer.setup()
|
|
76
|
+
|
|
77
|
+
cls._initialized = True
|
|
78
|
+
logging.info(
|
|
79
|
+
f"PgCheckpointService 初始化成功,地址: {cls._config.host}:{cls._config.port}/{cls._config.dbname}"
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
except Exception as e:
|
|
83
|
+
logging.error(f"PgCheckpointService 初始化失败: {e}", exc_info=True)
|
|
84
|
+
# 清理可能已创建的连接池
|
|
85
|
+
if cls._pool:
|
|
86
|
+
try:
|
|
87
|
+
await cls._pool.close()
|
|
88
|
+
except Exception:
|
|
89
|
+
pass
|
|
90
|
+
cls._pool = None
|
|
91
|
+
cls._checkpointer = None
|
|
92
|
+
|
|
93
|
+
@classmethod
|
|
94
|
+
async def get_checkpointer(cls) -> Any:
|
|
95
|
+
"""获取 AsyncPostgresSaver 实例
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
AsyncPostgresSaver 实例
|
|
99
|
+
|
|
100
|
+
Raises:
|
|
101
|
+
RuntimeError: 如果服务未初始化
|
|
102
|
+
"""
|
|
103
|
+
if not cls._initialized or not cls._checkpointer:
|
|
104
|
+
raise RuntimeError("PgCheckpointService 未初始化,请先调用 PgCheckpointService.setup()")
|
|
105
|
+
return cls._checkpointer
|
|
106
|
+
|
|
107
|
+
@classmethod
|
|
108
|
+
def is_initialized(cls) -> bool:
|
|
109
|
+
"""检查是否已初始化"""
|
|
110
|
+
return cls._initialized
|
|
111
|
+
|
|
112
|
+
@classmethod
|
|
113
|
+
async def close(cls):
|
|
114
|
+
"""关闭 PG 连接池"""
|
|
115
|
+
if cls._pool:
|
|
116
|
+
try:
|
|
117
|
+
await cls._pool.close()
|
|
118
|
+
logging.info("PgCheckpointService 连接池已关闭")
|
|
119
|
+
except Exception as e:
|
|
120
|
+
logging.error(f"关闭 PgCheckpointService 连接池失败: {e}")
|
|
121
|
+
finally:
|
|
122
|
+
cls._pool = None
|
|
123
|
+
cls._checkpointer = None
|
|
124
|
+
cls._initialized = False
|
sycommon/llm/get_llm.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import os
|
|
2
1
|
from sycommon.llm.llm_logger import LLMLogger
|
|
3
2
|
from langchain.chat_models import init_chat_model
|
|
4
3
|
from sycommon.config.LLMConfig import LLMConfig
|
|
@@ -160,7 +159,7 @@ def get_llm(
|
|
|
160
159
|
"model_provider": llmConfig.provider,
|
|
161
160
|
"model": llmConfig.model,
|
|
162
161
|
"base_url": llmConfig.baseUrl,
|
|
163
|
-
"api_key":
|
|
162
|
+
"api_key": llmConfig.apiKey or "-",
|
|
164
163
|
"callbacks": callbacks,
|
|
165
164
|
"temperature": temperature,
|
|
166
165
|
"streaming": streaming,
|
sycommon/services.py
CHANGED
|
@@ -20,6 +20,7 @@ from sycommon.synacos.feign import close_all_feign_sessions
|
|
|
20
20
|
from sycommon.synacos.feign_client import close_all_feign_client_sessions
|
|
21
21
|
from sycommon.database.elasticsearch_service import ElasticsearchService
|
|
22
22
|
from sycommon.database.redis_service import RedisService
|
|
23
|
+
from sycommon.database.pg_checkpoint_service import PgCheckpointService
|
|
23
24
|
from sycommon.database.database_service import DatabaseService
|
|
24
25
|
from sycommon.xxljob.xxljob_service import XxlJobService
|
|
25
26
|
|
|
@@ -109,6 +110,7 @@ class Services(metaclass=SingletonMeta):
|
|
|
109
110
|
# 可插拔服务开关(从类变量中继承 plugins() 阶段的设置)
|
|
110
111
|
self._enable_es: bool = Services.__dict__.get('_enable_es', False)
|
|
111
112
|
self._enable_redis: bool = Services.__dict__.get('_enable_redis', False)
|
|
113
|
+
self._enable_pg: bool = Services.__dict__.get('_enable_pg', False)
|
|
112
114
|
self._enable_sandbox: bool = Services.__dict__.get('_enable_sandbox', False)
|
|
113
115
|
self._enable_xxljob: bool = Services.__dict__.get('_enable_xxljob', False)
|
|
114
116
|
|
|
@@ -148,6 +150,7 @@ class Services(metaclass=SingletonMeta):
|
|
|
148
150
|
rabbitmq_senders: Optional[List[RabbitMQSendConfig]] = None,
|
|
149
151
|
elasticsearch_service: Optional[Callable[[dict], None]] = None,
|
|
150
152
|
redis_service: Optional[Callable[[dict], None]] = None,
|
|
153
|
+
pg_checkpoint_service: Optional[Union[bool, dict]] = None,
|
|
151
154
|
sandbox_service: bool = False,
|
|
152
155
|
deep_agent_service: bool = False,
|
|
153
156
|
multi_agent_service: bool = False,
|
|
@@ -193,6 +196,7 @@ class Services(metaclass=SingletonMeta):
|
|
|
193
196
|
# 保存可插拔服务开关状态到类变量(实例创建时会被拷贝到实例变量)
|
|
194
197
|
Services._enable_es = elasticsearch_service is not None
|
|
195
198
|
Services._enable_redis = redis_service is not None
|
|
199
|
+
Services._enable_pg = pg_checkpoint_service is not None
|
|
196
200
|
Services._enable_sandbox = sandbox_service or deep_agent_service or multi_agent_service
|
|
197
201
|
Services._enable_xxljob = xxljob_service is not None
|
|
198
202
|
|
|
@@ -297,14 +301,19 @@ class Services(metaclass=SingletonMeta):
|
|
|
297
301
|
|
|
298
302
|
app_instance.state.services = instance
|
|
299
303
|
|
|
300
|
-
# 4.
|
|
304
|
+
# 4. 初始化 PG Checkpoint 服务(如果启用)
|
|
305
|
+
if instance._enable_pg:
|
|
306
|
+
pg_config = pg_checkpoint_service if isinstance(pg_checkpoint_service, dict) else None
|
|
307
|
+
await PgCheckpointService.setup(pg_config)
|
|
308
|
+
|
|
309
|
+
# 5. 启动沙箱后台清理任务(如果沙箱服务启用)
|
|
301
310
|
if instance._enable_sandbox:
|
|
302
311
|
from sycommon.middleware.sandbox import _cleanup_finished_processes
|
|
303
312
|
sandbox_cleanup_task = asyncio.create_task(
|
|
304
313
|
_cleanup_finished_processes())
|
|
305
314
|
logging.info("沙箱后台清理任务已启动")
|
|
306
315
|
|
|
307
|
-
#
|
|
316
|
+
# 6. 执行用户定义的生命周期
|
|
308
317
|
if cls._user_lifespan:
|
|
309
318
|
async with cls._user_lifespan(app_instance):
|
|
310
319
|
yield
|
|
@@ -472,6 +481,13 @@ class Services(metaclass=SingletonMeta):
|
|
|
472
481
|
except Exception as e:
|
|
473
482
|
logging.debug(f"关闭 RedisService 时发生异常: {e}")
|
|
474
483
|
|
|
484
|
+
# 关闭 PG Checkpoint 服务(仅当启用时)
|
|
485
|
+
if cls._instance and cls._instance._enable_pg:
|
|
486
|
+
try:
|
|
487
|
+
await PgCheckpointService.close()
|
|
488
|
+
except Exception as e:
|
|
489
|
+
logging.debug(f"关闭 PgCheckpointService 时发生异常: {e}")
|
|
490
|
+
|
|
475
491
|
# 关闭 XXL-JOB 执行器(仅当启用时)
|
|
476
492
|
if cls._instance and cls._instance._enable_xxljob:
|
|
477
493
|
try:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sycommon-python-lib
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.4a0
|
|
4
4
|
Summary: Add your description here
|
|
5
5
|
Requires-Python: >=3.11
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
@@ -8,17 +8,18 @@ Requires-Dist: aio-pika>=9.6.2
|
|
|
8
8
|
Requires-Dist: aiohttp>=3.13.5
|
|
9
9
|
Requires-Dist: aiomysql>=0.3.2
|
|
10
10
|
Requires-Dist: anyio>=4.12.1
|
|
11
|
-
Requires-Dist: decorator>=5.
|
|
11
|
+
Requires-Dist: decorator>=5.3.0
|
|
12
12
|
Requires-Dist: deepagents>=0.6.1
|
|
13
13
|
Requires-Dist: elasticsearch>=9.4.0
|
|
14
14
|
Requires-Dist: fastapi>=0.136.1
|
|
15
15
|
Requires-Dist: jinja2>=3.1.6
|
|
16
16
|
Requires-Dist: kafka-python>=2.3.1
|
|
17
|
-
Requires-Dist: langchain>=1.3.
|
|
17
|
+
Requires-Dist: langchain>=1.3.1
|
|
18
18
|
Requires-Dist: langchain-core>=1.4.0
|
|
19
19
|
Requires-Dist: langchain-openai>=1.2.1
|
|
20
20
|
Requires-Dist: langfuse>=4.6.1
|
|
21
21
|
Requires-Dist: langgraph>=1.2.0
|
|
22
|
+
Requires-Dist: langgraph-checkpoint-postgres>=3.1.0
|
|
22
23
|
Requires-Dist: langgraph-checkpoint-redis>=0.4.1
|
|
23
24
|
Requires-Dist: ldap3>=2.9.1
|
|
24
25
|
Requires-Dist: loguru>=0.7.3
|
|
@@ -28,17 +29,18 @@ Requires-Dist: psutil>=7.2.2
|
|
|
28
29
|
Requires-Dist: pyxxl>=0.4.6
|
|
29
30
|
Requires-Dist: pydantic>=2.13.4
|
|
30
31
|
Requires-Dist: python-dotenv>=1.2.2
|
|
31
|
-
Requires-Dist: python-multipart>=0.0.
|
|
32
|
+
Requires-Dist: python-multipart>=0.0.29
|
|
32
33
|
Requires-Dist: pyyaml>=6.0.3
|
|
33
34
|
Requires-Dist: redis>=7.3.0
|
|
34
35
|
Requires-Dist: sentry-sdk[fastapi]>=2.60.0
|
|
35
36
|
Requires-Dist: sqlalchemy[asyncio]>=2.0.48
|
|
36
37
|
Requires-Dist: starlette[full]>=1.0.0
|
|
37
|
-
Requires-Dist: tiktoken>=0.
|
|
38
|
-
Requires-Dist: uvicorn>=0.
|
|
38
|
+
Requires-Dist: tiktoken>=0.13.0
|
|
39
|
+
Requires-Dist: uvicorn>=0.47.0
|
|
39
40
|
Requires-Dist: wecom-aibot-python-sdk>=1.0.2
|
|
40
41
|
Requires-Dist: twine>=6.2.0
|
|
41
42
|
Requires-Dist: minio>=7.2.20
|
|
43
|
+
Requires-Dist: langchain-mcp-adapters>=0.2.2
|
|
42
44
|
|
|
43
45
|
# sycommon-python-lib
|
|
44
46
|
|
|
@@ -125,13 +125,16 @@ sycli/rl/strategy_generator.py,sha256=RzYkaj4jQ_5aNOuj1WA-SXIHfO5h1nTSSHmLZpO-gi
|
|
|
125
125
|
sycli/rl/strategy_prompts.py,sha256=dwx291OnyywP6z7uKmoDPZNCC4MNrIyDANdAEO0evJs,2873
|
|
126
126
|
sycli/skills/__init__.py,sha256=0o9HuaDSvN-z0JZwlDSwU7WxtchQn7f0OtluIMgtC0Q,212
|
|
127
127
|
sycommon/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
128
|
-
sycommon/services.py,sha256=
|
|
129
|
-
sycommon/agent/__init__.py,sha256=
|
|
128
|
+
sycommon/services.py,sha256=UtWn36dPmB9g1CTKkNgzbU8M28iOkZd2BtdhvDZLfbk,22214
|
|
129
|
+
sycommon/agent/__init__.py,sha256=mxceAeUifQ-DKvWp7ZEJIFlmOCb5wpYHPGQw3rwEN8I,4378
|
|
130
130
|
sycommon/agent/agent_manager.py,sha256=UhhaekEumT7g4v_Z1UB4jTp13X0n8M8erYaQdkGGWkA,13620
|
|
131
131
|
sycommon/agent/chat_events.py,sha256=bWAMWYIZ2L_yqUcn5jq9ius_lQxLHEv4zQLEqX6UaeM,13190
|
|
132
|
-
sycommon/agent/deep_agent.py,sha256=
|
|
133
|
-
sycommon/agent/multi_agent_team.py,sha256=
|
|
132
|
+
sycommon/agent/deep_agent.py,sha256=WTGKWx0MxmiIbbrKPTbQl6on2w4LQRWtH0hdiViA-dE,31709
|
|
133
|
+
sycommon/agent/multi_agent_team.py,sha256=NHmsUNwe3huguUUzbeoiOjgo9wh4-eXH0AjPDvm1dP4,26781
|
|
134
134
|
sycommon/agent/summarization_utils.py,sha256=PRCIFtYBrH0bbSxsIc-qpC4iEXJzk72UuR7u5mQTt2w,7360
|
|
135
|
+
sycommon/agent/mcp/__init__.py,sha256=iKrdDhIrFsNIkqG_kgcwNe-nOiM6uVfolKv44LfQ-FQ,636
|
|
136
|
+
sycommon/agent/mcp/models.py,sha256=RBAIbGETNXkqD3wQZT7eKS4ozkgE9DQEneF1WKZf1C0,1355
|
|
137
|
+
sycommon/agent/mcp/tool_loader.py,sha256=SEny14f7Bm9I17pT-9PJWMbhi9Ki77wvCR0KRNEJmyM,6428
|
|
135
138
|
sycommon/agent/sandbox/__init__.py,sha256=jR7LlkD4J4Y6QYyRXQClkwmqDBCCPmycV_hQV9p9YHw,4621
|
|
136
139
|
sycommon/agent/sandbox/file_ops.py,sha256=6ymRMM0WchM7G_YmF1ckrLjf5s_JCh1wrAp2g_-sg8k,23162
|
|
137
140
|
sycommon/agent/sandbox/http_sandbox_backend.py,sha256=mjiTZnADvUq_rO05ewllo_eGDS4uTdD2e2GGYvBpF-Q,56150
|
|
@@ -149,6 +152,7 @@ sycommon/config/EmbeddingConfig.py,sha256=gPKwiDYbeu1GpdIZXMmgqM7JqBIzCXi0yYuGRL
|
|
|
149
152
|
sycommon/config/LLMConfig.py,sha256=pjMiUgsUaKHw6WNi3weL2ilE9nFua5MkXg96kgwtuzY,510
|
|
150
153
|
sycommon/config/LangfuseConfig.py,sha256=t2LulAtnMUvIINOKHXNWlT5PtgNb7IuaHURjWlbma38,370
|
|
151
154
|
sycommon/config/MQConfig.py,sha256=_RDcmIdyWKjmgM5ZnriOoI-DpaxgXs7CD0awdAD6z88,252
|
|
155
|
+
sycommon/config/PgConfig.py,sha256=Hs9LwgIxSBxcFP16oq18N6Gq9hU2qVl4-7bPfd-ON_s,2333
|
|
152
156
|
sycommon/config/RedisConfig.py,sha256=gIa4BS8L_HdmBg9Dkv3cuIK6CU9zt9RodZOJUuUlh5Y,5235
|
|
153
157
|
sycommon/config/RerankerConfig.py,sha256=35sVwzus2IscvTHnCG63Orl2pC-pMsrVi6wAGDmOH3U,341
|
|
154
158
|
sycommon/config/SentryConfig.py,sha256=OsLb3G9lTsCSZ7tWkcXWJHmvfILQopBxje5pjnkFJfo,320
|
|
@@ -159,6 +163,7 @@ sycommon/database/async_database_service.py,sha256=HZSV0ntVTteT-VZfkM9dwuld-gN5C
|
|
|
159
163
|
sycommon/database/base_db_service.py,sha256=J5ELHMNeGfzA6zVcASPSPZ0XNKrRY3_gdGmVkZw3Mto,946
|
|
160
164
|
sycommon/database/database_service.py,sha256=IMoJ9554dYkr6QfRofvNa0VR24U1WQDz_ATrg0-6EQ0,3857
|
|
161
165
|
sycommon/database/elasticsearch_service.py,sha256=qm490GRlxZlYsQgyfyclSbARRP1-Tc4Lwav3lbPINvQ,3092
|
|
166
|
+
sycommon/database/pg_checkpoint_service.py,sha256=pfexd_to1u3TmrswraMQ-dvMJsjPRE36pmDf2wgaNyA,4129
|
|
162
167
|
sycommon/database/redis_service.py,sha256=tPw8UgeuyYQBxWfPRjx7VqlSRFNxIsnR0WSGd36GaA8,20509
|
|
163
168
|
sycommon/database/token_usage_db_service.py,sha256=_hoeB4lYPhDOlJLaUIHIl7z-DNpzsRYrPn5oboD1Y38,6254
|
|
164
169
|
sycommon/health/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -171,7 +176,7 @@ sycommon/heartbeat_process/heartbeat_process_manager.py,sha256=24qUKs8qegdWHqcox
|
|
|
171
176
|
sycommon/heartbeat_process/heartbeat_process_worker.py,sha256=duuAEFwda43Y6pZE8tOOspitlyxecaFsg1n1iH9jQDQ,16863
|
|
172
177
|
sycommon/llm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
173
178
|
sycommon/llm/embedding.py,sha256=Xwmg1HcgbdW7OcpYyzu8k7U-27rC5lzjIqbg221CcyY,19129
|
|
174
|
-
sycommon/llm/get_llm.py,sha256=
|
|
179
|
+
sycommon/llm/get_llm.py,sha256=9F2EJhP3ujbk78h1jW8T9Jafb-79U2QAUutYaSbTHvY,7572
|
|
175
180
|
sycommon/llm/llm_logger.py,sha256=LLXiESwDP5f8dB50nFabShVoLKv8UCf2ll69zo1FOso,3365
|
|
176
181
|
sycommon/llm/llm_tokens.py,sha256=yGEessxfk5wRMrPGyWHhiiIIQFDVT23FSaqnwqHGCoY,4712
|
|
177
182
|
sycommon/llm/llm_with_token_tracking.py,sha256=vrdH5LlXg1y2glDPJVawT9A-JoXL9qnvMfHAU3CNy70,12928
|
|
@@ -260,8 +265,8 @@ sycommon/tools/syemail.py,sha256=BDFhgf7WDOQeTcjxJEQdu0dQhnHFPO_p3eI0-Ni3LhQ,561
|
|
|
260
265
|
sycommon/tools/timing.py,sha256=OiiE7P07lRoMzX9kzb8sZU9cDb0zNnqIlY5pWqHcnkY,2064
|
|
261
266
|
sycommon/xxljob/__init__.py,sha256=7eoBlQxv-B39IfRSCY2bkqdGYs1QRe1umAWd88VMEEM,86
|
|
262
267
|
sycommon/xxljob/xxljob_service.py,sha256=JIEJaGXhqrTLcyxlyynSrsHg9bBnDNzX-D4qIWLRPUE,6815
|
|
263
|
-
sycommon_python_lib-0.2.
|
|
264
|
-
sycommon_python_lib-0.2.
|
|
265
|
-
sycommon_python_lib-0.2.
|
|
266
|
-
sycommon_python_lib-0.2.
|
|
267
|
-
sycommon_python_lib-0.2.
|
|
268
|
+
sycommon_python_lib-0.2.4a0.dist-info/METADATA,sha256=yMGghe0oJib602tFNPsCc3gpMVe-j0u8inTbvCWqkFI,7836
|
|
269
|
+
sycommon_python_lib-0.2.4a0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
270
|
+
sycommon_python_lib-0.2.4a0.dist-info/entry_points.txt,sha256=gsR4SssKxDWjRU8ggidzNcdMXDPRSKRS7UaGyNP84Qg,92
|
|
271
|
+
sycommon_python_lib-0.2.4a0.dist-info/top_level.txt,sha256=RgphKrg7nJyZ7irJqbxFr-5H2LUYTvI7ivoWZH2hcD0,29
|
|
272
|
+
sycommon_python_lib-0.2.4a0.dist-info/RECORD,,
|
|
File without changes
|
{sycommon_python_lib-0.2.3a12.dist-info → sycommon_python_lib-0.2.4a0.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{sycommon_python_lib-0.2.3a12.dist-info → sycommon_python_lib-0.2.4a0.dist-info}/top_level.txt
RENAMED
|
File without changes
|