Commit 3c4e1142 by sunqi02

更新企微消息发送请求的构建

1 parent 9d4f1051
...@@ -87,8 +87,9 @@ async def chat( ...@@ -87,8 +87,9 @@ async def chat(
# 2. 提取 user_id:取最后一条消息的 sender_id # 2. 提取 user_id:取最后一条消息的 sender_id
user_id = request.messages[-1].sender_id user_id = request.messages[-1].sender_id
# 3. 提取 allowed_agent_codes:agent_id → 内部 agent code 映射 # 3. 提取 allowed_agent_codes 和 bot_id_mapping
allowed_agent_codes: Optional[list] = None allowed_agent_codes: Optional[list] = None
bot_id_mapping: dict = {}
if request.agents: if request.agents:
# 构建 agent.code → 内部名称(dict key)映射 # 构建 agent.code → 内部名称(dict key)映射
code_to_name = { code_to_name = {
...@@ -100,6 +101,9 @@ async def chat( ...@@ -100,6 +101,9 @@ async def chat(
internal_name = code_to_name.get(agent_code_str) internal_name = code_to_name.get(agent_code_str)
if internal_name and internal_name != "main": if internal_name and internal_name != "main":
allowed_agent_codes.append(internal_name) allowed_agent_codes.append(internal_name)
# 提取 bot_id 映射:{internal_agent_name: bot_id}
if internal_name and agent_info.bot_id:
bot_id_mapping[internal_name] = agent_info.bot_id
if not allowed_agent_codes: if not allowed_agent_codes:
allowed_agent_codes = None # 无有效子 agent,走默认路由 allowed_agent_codes = None # 无有效子 agent,走默认路由
...@@ -113,6 +117,7 @@ async def chat( ...@@ -113,6 +117,7 @@ async def chat(
user_query=query, user_query=query,
file_url=file_url, file_url=file_url,
allowed_agent_codes=allowed_agent_codes, allowed_agent_codes=allowed_agent_codes,
bot_id_mapping=bot_id_mapping,
) )
return {"status": "ok", "session_id": session_id} return {"status": "ok", "session_id": session_id}
...@@ -126,6 +131,7 @@ async def _process_message_async( ...@@ -126,6 +131,7 @@ async def _process_message_async(
user_query: str, user_query: str,
file_url: Optional[str], file_url: Optional[str],
allowed_agent_codes: Optional[list], allowed_agent_codes: Optional[list],
bot_id_mapping: Optional[dict] = None,
): ):
"""后台执行 Agent 处理流程,异常不影响 HTTP 响应。""" """后台执行 Agent 处理流程,异常不影响 HTTP 响应。"""
try: try:
...@@ -136,6 +142,7 @@ async def _process_message_async( ...@@ -136,6 +142,7 @@ async def _process_message_async(
user_query=user_query, user_query=user_query,
file_url=file_url, file_url=file_url,
allowed_agent_codes=allowed_agent_codes, allowed_agent_codes=allowed_agent_codes,
bot_id_mapping=bot_id_mapping,
) )
except Exception: except Exception:
logger.exception( logger.exception(
......
"""Console IM service — prints hook output instead of sending to real IM.""" """Console IM service — prints hook output instead of sending to real IM."""
import json import json
import time
from typing import Dict, Any, Optional from typing import Dict, Any, Optional
...@@ -21,6 +22,7 @@ class ConsoleIMService: ...@@ -21,6 +22,7 @@ class ConsoleIMService:
"content": content, "content": content,
"contact_id": contact_id, "contact_id": contact_id,
"msg_type": "text", "msg_type": "text",
"biz_rela_no": f"biz-msg-{int(time.time() * 1000)}",
} }
if at_list: if at_list:
payload["at_list"] = at_list payload["at_list"] = at_list
......
import time
from typing import Dict, Any, Optional from typing import Dict, Any, Optional
import httpx import httpx
...@@ -45,6 +46,7 @@ class IMService: ...@@ -45,6 +46,7 @@ class IMService:
"content": content, "content": content,
"bot_id": bot_id, "bot_id": bot_id,
"msg_type": "text", "msg_type": "text",
"biz_rela_no": f"biz-msg-{int(time.time() * 1000)}",
} }
if at_list: if at_list:
payload["at_list"] = at_list payload["at_list"] = at_list
...@@ -53,14 +55,20 @@ class IMService: ...@@ -53,14 +55,20 @@ class IMService:
if self.api_key: if self.api_key:
headers["X-Api-Key"] = self.api_key headers["X-Api-Key"] = self.api_key
url = f"{self.base_url}/api/v1/business/messages/send"
print(f"\n[IM] 调用: {url}")
print(f"[IM] 请求体: {payload}\n")
try: try:
response = await self.client.post( response = await self.client.post(
f"{self.base_url}/api/v1/business/messages/send", url,
json=payload, json=payload,
headers=headers headers=headers
) )
response.raise_for_status() response.raise_for_status()
return response.json() result = response.json()
print(f"[IM] 响应: {result}\n")
return result
except httpx.HTTPStatusError as e: except httpx.HTTPStatusError as e:
raise IMServiceError(f"IM服务返回错误: {e.response.status_code} - {e.response.text}") raise IMServiceError(f"IM服务返回错误: {e.response.status_code} - {e.response.text}")
except httpx.RequestError as e: except httpx.RequestError as e:
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
"base_path": "" "base_path": ""
}, },
"im": { "im": {
"use_console": true, "use_console": false,
"base_url": "http://0.0.0.0:8080", "base_url": "http://yjy.dev.glsx.com.cn/webim",
"api_key": "" "api_key": ""
}, },
"wecom_sync": { "wecom_sync": {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!