Commit ef4e3cd4 by sunqi02

灾后再建

1 parent a1db4827
import logging
import uuid
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
from pydantic import BaseModel, Field
from typing import Optional, List
......@@ -25,7 +26,9 @@ class PositionInfo(BaseModel):
class AgentInfo(BaseModel):
agent_id: int = Field(..., description="Agent ID,对应 agent.code")
agent_name: str = Field(..., description="Agent 名称")
agent_name: Optional[str] = Field(None, description="Agent 名称")
bot_id: Optional[str] = Field(None, description="机器人编码")
is_steward: Optional[bool] = Field(False, description="是否为管家 Agent")
class MessageInfo(BaseModel):
......@@ -39,7 +42,7 @@ class MessageInfo(BaseModel):
class ChatRequest(BaseModel):
session_id: str = Field(..., description="会话ID")
session_id: Optional[str] = Field(None, description="会话ID,缺失时自动生成")
group: Optional[GroupInfo] = Field(default=None, description="群信息")
position: Optional[PositionInfo] = Field(default=None, description="岗位信息")
agents: List[AgentInfo] = Field(default_factory=list, description="可用 Agent 列表")
......@@ -61,6 +64,10 @@ async def chat(
接收消息后立即返回 200,Agent 在后台异步处理。
处理结果通过 PostReplyHook 发送到 IM。
"""
# 0. session_id 缺失时自动生成
if not request.session_id:
request.session_id = f"sess_{uuid.uuid4().hex[:12]}"
# 1. 提取 query:text 消息的 msg_content 用 \n 拼接
text_parts = []
file_url: Optional[str] = None
......
......@@ -2,7 +2,7 @@
"company": "公司",
"max_messages": 50,
"server": {
"host": "192.168.1.191",
"host": "0.0.0.0",
"port": 8000,
"reload": true
},
......
{
"session_id": "test_sess_001",
"group": {
"group_contact_id": "4BD6DE10053F765A7F161DF36A6E0031901A2DDBF0758730A19ADA8F0B6DD9F8",
"group_name": "测试群"
},
"position": {
"position_id": 1,
"position_code": "store_sales",
"position_name": "驻店销售岗"
},
"agents": [
{"agent_id": 100, "bot_id": "ABE6E30E3476E955853410C13E38C1065194F219CF554649F1C4F9C615435A82"},
{"agent_id": 101, "bot_id": "ABE6E30E3476E955853410C13E38C1065194F219CF554649F1C4F9C615435A82"}
{"agent_id": 100, "bot_id": "ABE6E30E3476E955853410C13E38C1065194F219CF554649F1C4F9C615435A82", "is_steward": true},
{"agent_id": 101, "bot_id": "ABE6E30E3476E955853410C13E38C1065194F219CF554649F1C4F9C615435A82", "is_steward": false}
],
"messages": [
{
......
......@@ -259,6 +259,79 @@ class TestChatCompletionsAPI:
)
assert resp.status_code == 400
def test_session_id_auto_generated(self):
"""不传 session_id → 自动生成 sess_ 前缀"""
with patch.object(test_hub, 'model', new=_make_mock_model_reply("你好")):
with patch.object(test_hub.formatter, 'format', new=AsyncMock(side_effect=_noop_format)):
resp = client.post(
"/api/v1/chat/completions",
json={
"messages": [
{
"msg_id": "m1",
"msg_time": "2026-05-23T10:00:00Z",
"sender_id": "u1",
"msg_type": "text",
"msg_content": "你好",
"payload": {},
}
],
},
)
assert resp.status_code == 200
data = resp.json()["data"]
assert data["status"] == "ok"
assert data["session_id"].startswith("sess_")
def test_agents_without_name(self):
"""agents 不含 agent_name → 不报错"""
with patch.object(test_hub, 'model', new=_make_mock_model_reply("收到")):
with patch.object(test_hub.formatter, 'format', new=AsyncMock(side_effect=_noop_format)):
resp = client.post(
"/api/v1/chat/completions",
json={
"session_id": "sess_no_name",
"agents": [{"agent_id": 101}],
"messages": [
{
"msg_id": "m1",
"msg_time": "2026-05-23T10:00:00Z",
"sender_id": "u1",
"msg_type": "text",
"msg_content": "你好",
"payload": {},
}
],
},
)
assert resp.status_code == 200
def test_agents_with_bot_id_and_steward(self):
"""agents 含 bot_id 和 is_steward → 正确接收"""
with patch.object(test_hub, 'model', new=_make_mock_model_reply("收到")):
with patch.object(test_hub.formatter, 'format', new=AsyncMock(side_effect=_noop_format)):
resp = client.post(
"/api/v1/chat/completions",
json={
"session_id": "sess_steward",
"agents": [
{"agent_id": 101, "bot_id": "bot_001", "is_steward": True},
{"agent_id": 102, "bot_id": "bot_002", "is_steward": False},
],
"messages": [
{
"msg_id": "m1",
"msg_time": "2026-05-23T10:00:00Z",
"sender_id": "u1",
"msg_type": "text",
"msg_content": "你好",
"payload": {},
}
],
},
)
assert resp.status_code == 200
# ═══════════════════════════════════════════════════════════════════
# PostReplyHook 单元测试
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!