feat(tools): add OpenClaw remote agent tool integration
- Detect x-openclaw flag in OpenAPI schema and init dedicated config - Implement multimodal input/output (image download, compress, base64) - Add OpenClaw connection test and status validation in tool service - Fix auth_config token check to support both api_key and bearer_token - Inject runtime context (user_id, conversation_id, files) in chat services
This commit is contained in:
@@ -165,7 +165,18 @@ class AppChatService:
|
||||
multimodal_service = MultimodalService(self.db, model_info)
|
||||
processed_files = await multimodal_service.process_files(files)
|
||||
logger.info(f"处理了 {len(processed_files)} 个文件")
|
||||
|
||||
#============为 OpenClaw 工具注入会话session======
|
||||
# 为 OpenClaw 工具注入运行时上下文
|
||||
for t in tools:
|
||||
if hasattr(t, 'tool_instance') and hasattr(t.tool_instance, '_is_openclaw'):
|
||||
if t.tool_instance._is_openclaw:
|
||||
t.tool_instance._user_id = user_id or "anonymous"
|
||||
t.tool_instance._conversation_id = (
|
||||
str(conversation_id) if conversation_id else None)
|
||||
# 注入用户上传的文件
|
||||
if processed_files:
|
||||
t.tool_instance._uploaded_files = processed_files
|
||||
#============为 OpenClaw 工具注入会话session======
|
||||
# 调用 Agent(支持多模态)
|
||||
result = await agent.chat(
|
||||
message=message,
|
||||
@@ -413,6 +424,17 @@ class AppChatService:
|
||||
processed_files = await multimodal_service.process_files(files)
|
||||
logger.info(f"处理了 {len(processed_files)} 个文件")
|
||||
|
||||
#============为 OpenClaw 工具注入运行时上下文======
|
||||
for t in tools:
|
||||
if hasattr(t, 'tool_instance') and hasattr(t.tool_instance, '_is_openclaw'):
|
||||
if t.tool_instance._is_openclaw:
|
||||
t.tool_instance._user_id = user_id or "anonymous"
|
||||
t.tool_instance._conversation_id = (
|
||||
str(conversation_id) if conversation_id else None)
|
||||
if processed_files:
|
||||
t.tool_instance._uploaded_files = processed_files
|
||||
#============为 OpenClaw 工具注入运行时上下文结束======
|
||||
|
||||
# 流式调用 Agent(支持多模态),同时并行启动 TTS
|
||||
full_content = ""
|
||||
full_reasoning = ""
|
||||
|
||||
@@ -640,7 +640,18 @@ class AgentRunService:
|
||||
multimodal_service = MultimodalService(self.db, model_info)
|
||||
processed_files = await multimodal_service.process_files(files)
|
||||
logger.info(f"处理了 {len(processed_files)} 个文件,provider={provider}")
|
||||
|
||||
#================= 为 OpenClaw 工具注入运行时上下文==========
|
||||
for t in tools:
|
||||
logger.info(f"检查工具: {type(t).__name__}, has_tool_instance={hasattr(t, 'tool_instance')}, is_openclaw={getattr(getattr(t, 'tool_instance', None), '_is_openclaw', 'N/A')}")
|
||||
if hasattr(t, 'tool_instance') and hasattr(t.tool_instance, '_is_openclaw'):
|
||||
if t.tool_instance._is_openclaw:
|
||||
t.tool_instance._user_id = user_id or "anonymous"
|
||||
t.tool_instance._conversation_id = (
|
||||
str(conversation_id) if conversation_id else None)
|
||||
if processed_files:
|
||||
t.tool_instance._uploaded_files = processed_files
|
||||
logger.info(f"已注入 _uploaded_files, 数量: {len(processed_files)}")
|
||||
#================= 为 OpenClaw 工具注入运行时上下文结束==========
|
||||
# 7. 知识库检索
|
||||
context = None
|
||||
|
||||
@@ -890,7 +901,18 @@ class AgentRunService:
|
||||
multimodal_service = MultimodalService(self.db, model_info)
|
||||
processed_files = await multimodal_service.process_files(files)
|
||||
logger.info(f"处理了 {len(processed_files)} 个文件,provider={provider}")
|
||||
|
||||
#============为 OpenClaw 工具注入会话session======
|
||||
# 为 OpenClaw 工具注入运行时上下文
|
||||
for t in tools:
|
||||
if hasattr(t, 'tool_instance') and hasattr(t.tool_instance, '_is_openclaw'):
|
||||
if t.tool_instance._is_openclaw:
|
||||
t.tool_instance._user_id = user_id or "anonymous"
|
||||
t.tool_instance._conversation_id = (
|
||||
str(conversation_id) if conversation_id else None)
|
||||
# 注入用户上传的文件
|
||||
if processed_files:
|
||||
t.tool_instance._uploaded_files = processed_files
|
||||
#============为 OpenClaw 工具注入会话session======
|
||||
# 7. 知识库检索
|
||||
context = None
|
||||
|
||||
|
||||
@@ -330,6 +330,20 @@ class ToolService:
|
||||
if config.tool_type == ToolType.MCP.value:
|
||||
return await self._test_mcp_connection(config)
|
||||
elif config.tool_type == ToolType.CUSTOM.value:
|
||||
# ========== 测试工具连接 OpenClaw 特判 ==========
|
||||
custom_config = self.custom_repo.find_by_tool_id(self.db, config.id)
|
||||
if custom_config and custom_config.schema_content:
|
||||
schema = custom_config.schema_content
|
||||
if isinstance(schema, str):
|
||||
try:
|
||||
schema = json.loads(schema)
|
||||
except json.JSONDecodeError:
|
||||
schema = {}
|
||||
#请求头中包含OpenClaw字段
|
||||
if isinstance(schema, dict) and schema.get("info", {}).get("x-openclaw"):
|
||||
return await self._test_openclaw_connection(custom_config, schema)
|
||||
# ========== OpenClaw 特判结束 ==========
|
||||
#正常自定义工具逻辑
|
||||
return await self._test_custom_connection(config)
|
||||
elif config.tool_type == ToolType.BUILTIN.value:
|
||||
return await self._test_builtin_connection(config)
|
||||
@@ -339,6 +353,45 @@ class ToolService:
|
||||
except Exception as e:
|
||||
return {"success": False, "message": f"测试失败: {str(e)}"}
|
||||
|
||||
#=============测试openclaw连接 特判===============
|
||||
async def _test_openclaw_connection(
|
||||
self, custom_config: CustomToolConfig, schema: dict
|
||||
) -> Dict[str, Any]:
|
||||
"""测试 OpenClaw 连接"""
|
||||
try:
|
||||
info = schema.get("info", {})
|
||||
servers = schema.get("servers", [])
|
||||
base_url = servers[0]["url"] if servers else ""
|
||||
token = (custom_config.auth_config or {}).get("token", "")
|
||||
agent_id = info.get("x-openclaw-agent-id", "main")
|
||||
model = info.get("x-openclaw-default-model", "openclaw")
|
||||
|
||||
url = f"{base_url.rstrip('/')}/v1/responses"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
"x-openclaw-agent-id": agent_id
|
||||
}
|
||||
body = {
|
||||
"model": model,
|
||||
"user": "connection-test",
|
||||
"input": "hi",
|
||||
"stream": False
|
||||
}
|
||||
|
||||
timeout_config = aiohttp.ClientTimeout(total=30)
|
||||
async with aiohttp.ClientSession(timeout=timeout_config) as session:
|
||||
async with session.post(url, json=body, headers=headers) as resp:
|
||||
if resp.status < 400:
|
||||
return {"success": True, "message": "OpenClaw 连接成功"}
|
||||
error_text = await resp.text()
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"OpenClaw HTTP {resp.status}: {error_text[:200]}"
|
||||
}
|
||||
except Exception as e:
|
||||
return {"success": False, "message": f"OpenClaw 连接失败: {str(e)}"}
|
||||
#=============测试openclaw连接结束===========
|
||||
def ensure_builtin_tools_initialized(self, tenant_id: uuid.UUID):
|
||||
"""确保内置工具已初始化"""
|
||||
existing = self.tool_repo.exists_builtin_for_tenant(self.db, tenant_id)
|
||||
@@ -1139,6 +1192,27 @@ class ToolService:
|
||||
custom_config = self.db.query(CustomToolConfig).filter(
|
||||
CustomToolConfig.id == tool_config.id
|
||||
).first()
|
||||
# ========== 更新工具 OpenClaw 特判 ==========
|
||||
if custom_config and custom_config.schema_content:
|
||||
schema = custom_config.schema_content
|
||||
if isinstance(schema, str):
|
||||
try:
|
||||
schema = json.loads(schema)
|
||||
except json.JSONDecodeError:
|
||||
schema = {}
|
||||
info = schema.get("info", {}) if isinstance(schema, dict) else {}
|
||||
if info.get("x-openclaw"):
|
||||
servers = schema.get("servers", [])
|
||||
has_url = bool(servers and servers[0].get("url"))
|
||||
has_agent_id = bool(info.get("x-openclaw-agent-id"))
|
||||
has_token = bool(custom_config.auth_config
|
||||
and custom_config.auth_config.get("api_key"))
|
||||
if has_url and has_agent_id and has_token:
|
||||
tool_config.status = ToolStatus.AVAILABLE.value
|
||||
else:
|
||||
tool_config.status = ToolStatus.UNCONFIGURED.value
|
||||
return
|
||||
# ========== OpenClaw 特判结束 ==========
|
||||
|
||||
if custom_config and tool_config.name and (custom_config.schema_content or custom_config.schema_url):
|
||||
tool_config.status = ToolStatus.AVAILABLE.value
|
||||
|
||||
Reference in New Issue
Block a user