mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 11:09:19 +08:00
feat: 拼多多发布页接入场景智能体与表单改写工具
This commit is contained in:
@@ -9,6 +9,7 @@ from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, Tool
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from agent.tools.labels import tool_status_label
|
||||
from agent.tools.publish_ui import is_client_tool_name
|
||||
from domain.models import ChatMessage, LlmRuntimeConfig, MessageRole, StreamEvent
|
||||
from domain.ports import ChatStreamer
|
||||
|
||||
@@ -88,6 +89,13 @@ class OpenAICompatibleStreamer(ChatStreamer):
|
||||
name = tc.get("name") or ""
|
||||
args = tc.get("args") or {}
|
||||
call_id = tc.get("id") or name
|
||||
if is_client_tool_name(name):
|
||||
yield StreamEvent.client_tool(
|
||||
tool_call_id=str(call_id),
|
||||
name=name,
|
||||
args=args if isinstance(args, dict) else {},
|
||||
label=tool_status_label(name),
|
||||
)
|
||||
tool = tool_map.get(name)
|
||||
try:
|
||||
if tool is None:
|
||||
|
||||
@@ -21,7 +21,10 @@ async def ensure_indexes(db: AsyncIOMotorDatabase) -> None:
|
||||
[("company_id", 1), ("user_id", 1), ("thread_id", 1)], unique=True
|
||||
)
|
||||
await db.agent_sessions.create_index(
|
||||
[("company_id", 1), ("user_id", 1), ("status", 1), ("updated_at", -1)]
|
||||
[("company_id", 1), ("user_id", 1), ("status", 1), ("scene", 1), ("updated_at", -1)]
|
||||
)
|
||||
await db.agent_sessions.create_index(
|
||||
[("company_id", 1), ("user_id", 1), ("scene", 1), ("scene_ref", 1), ("status", 1)]
|
||||
)
|
||||
await db.agent_messages.create_index(
|
||||
[("company_id", 1), ("user_id", 1), ("thread_id", 1), ("seq", 1)]
|
||||
|
||||
@@ -48,6 +48,8 @@ class MongoSessionRepository(SessionRepository):
|
||||
"title": session.title,
|
||||
"status": session.status.value,
|
||||
"summary": session.summary,
|
||||
"scene": session.scene or "general",
|
||||
"scene_ref": session.scene_ref,
|
||||
"llm_provider_id": session.llm_provider_id,
|
||||
"llm_model": session.llm_model,
|
||||
"created_at": session.created_at,
|
||||
@@ -63,20 +65,59 @@ class MongoSessionRepository(SessionRepository):
|
||||
)
|
||||
return None if doc is None else self._to_session(doc)
|
||||
|
||||
async def list_active(self, company_id: int, user_id: str, limit: int = 50) -> list[Session]:
|
||||
async def list_active(
|
||||
self,
|
||||
company_id: int,
|
||||
user_id: str,
|
||||
limit: int = 50,
|
||||
*,
|
||||
scene: str | None = None,
|
||||
) -> list[Session]:
|
||||
query: dict[str, Any] = {
|
||||
"company_id": company_id,
|
||||
"user_id": user_id,
|
||||
"status": SessionStatus.ACTIVE.value,
|
||||
}
|
||||
# 缺省只列经营助手;旧文档无 scene 字段视为 general
|
||||
want = (scene or "general").strip() or "general"
|
||||
if want == "general":
|
||||
query["$or"] = [
|
||||
{"scene": "general"},
|
||||
{"scene": {"$exists": False}},
|
||||
{"scene": None},
|
||||
]
|
||||
else:
|
||||
query["scene"] = want
|
||||
cursor = (
|
||||
self._col.find(
|
||||
{
|
||||
"company_id": company_id,
|
||||
"user_id": user_id,
|
||||
"status": SessionStatus.ACTIVE.value,
|
||||
}
|
||||
)
|
||||
self._col.find(query)
|
||||
.sort("updated_at", -1)
|
||||
.limit(limit)
|
||||
)
|
||||
return [self._to_session(d) async for d in cursor]
|
||||
|
||||
async def find_active_by_scene_ref(
|
||||
self,
|
||||
company_id: int,
|
||||
user_id: str,
|
||||
*,
|
||||
scene: str,
|
||||
scene_ref: str,
|
||||
) -> Session | None:
|
||||
ref = (scene_ref or "").strip()
|
||||
if not ref:
|
||||
return None
|
||||
doc = await self._col.find_one(
|
||||
{
|
||||
"company_id": company_id,
|
||||
"user_id": user_id,
|
||||
"status": SessionStatus.ACTIVE.value,
|
||||
"scene": (scene or "").strip() or "general",
|
||||
"scene_ref": ref,
|
||||
},
|
||||
sort=[("updated_at", -1)],
|
||||
)
|
||||
return self._to_session(doc) if doc else None
|
||||
|
||||
async def update(self, session: Session) -> Session:
|
||||
session.updated_at = datetime.utcnow()
|
||||
await self._col.update_one(
|
||||
@@ -90,6 +131,8 @@ class MongoSessionRepository(SessionRepository):
|
||||
"title": session.title,
|
||||
"status": session.status.value,
|
||||
"summary": session.summary,
|
||||
"scene": session.scene or "general",
|
||||
"scene_ref": session.scene_ref,
|
||||
"llm_provider_id": session.llm_provider_id,
|
||||
"llm_model": session.llm_model,
|
||||
"updated_at": session.updated_at,
|
||||
@@ -108,6 +151,8 @@ class MongoSessionRepository(SessionRepository):
|
||||
title=doc.get("title") or "新对话",
|
||||
status=SessionStatus(doc.get("status") or SessionStatus.ACTIVE.value),
|
||||
summary=doc.get("summary"),
|
||||
scene=(doc.get("scene") or "general"),
|
||||
scene_ref=doc.get("scene_ref"),
|
||||
llm_provider_id=_optional_int(doc.get("llm_provider_id")),
|
||||
llm_model=doc.get("llm_model"),
|
||||
created_at=_dt(doc.get("created_at")),
|
||||
|
||||
Reference in New Issue
Block a user