feat: 拼多多发布页接入场景智能体与表单改写工具

This commit is contained in:
2026-09-20 23:18:28 +08:00
parent d28b4d45c3
commit a140cff81a
31 changed files with 1660 additions and 44 deletions
+21 -1
View File
@@ -18,6 +18,8 @@ router = APIRouter(prefix="/sessions", tags=["chat"])
class ChatBody(BaseModel):
content: str = Field(min_length=1, max_length=20000)
# 发布场景:当前表单快照,供 ui_get_publish_snapshot
formSnapshot: dict | None = None
@router.get("/{thread_id}/messages")
@@ -57,7 +59,12 @@ async def chat_stream(
async def gen():
try:
async for event in svc.stream_chat(actor, thread_id, body.content):
async for event in svc.stream_chat(
actor,
thread_id,
body.content,
form_snapshot=body.formSnapshot,
):
if event.kind == "token" and event.text:
yield {
"event": "token",
@@ -75,6 +82,19 @@ async def chat_stream(
ensure_ascii=False,
),
}
elif event.kind == "client_tool":
yield {
"event": "client_tool",
"data": json.dumps(
{
"toolCallId": event.tool_call_id,
"name": event.name,
"args": event.args or {},
"label": event.label,
},
ensure_ascii=False,
),
}
yield {"event": "done", "data": "{}"}
except LookupError as e:
yield {"event": "error", "data": json.dumps({"message": str(e)}, ensure_ascii=False)}
+17 -2
View File
@@ -2,7 +2,7 @@
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException
from fastapi import APIRouter, Depends, HTTPException, Query
from pydantic import BaseModel, Field
from api.deps import current_actor, session_service
@@ -17,6 +17,10 @@ class CreateSessionBody(BaseModel):
title: str | None = None
llmProviderId: int | None = None
llmModel: str | None = None
scene: str | None = None
sceneRef: str | None = None
# 同 scene+sceneRef 已有活跃会话则复用(发布草稿一会话)
reuseBySceneRef: bool = False
class PatchSessionBody(BaseModel):
@@ -30,6 +34,8 @@ def _session_vo(s) -> dict:
"threadId": s.thread_id,
"title": s.title,
"status": s.status.value,
"scene": s.scene or "general",
"sceneRef": s.scene_ref,
"llmProviderId": s.llm_provider_id,
"llmModel": s.llm_model,
"createdAt": s.created_at.isoformat() + "Z",
@@ -40,10 +46,11 @@ def _session_vo(s) -> dict:
@router.get("")
async def list_sessions(
scene: str | None = Query(default="general"),
actor: Actor = Depends(current_actor),
svc: SessionService = Depends(session_service),
):
rows = await svc.list_mine(actor)
rows = await svc.list_mine(actor, scene=scene)
return ok({"items": [_session_vo(s) for s in rows]})
@@ -53,11 +60,19 @@ async def create_session(
actor: Actor = Depends(current_actor),
svc: SessionService = Depends(session_service),
):
scene = (body.scene or "general").strip() or "general"
ref = (body.sceneRef or "").strip() or None
if body.reuseBySceneRef and ref:
existing = await svc.find_by_scene_ref(actor, scene=scene, scene_ref=ref)
if existing is not None:
return ok(_session_vo(existing))
s = await svc.create(
actor,
body.title,
llm_provider_id=body.llmProviderId,
llm_model=body.llmModel,
scene=scene,
scene_ref=ref,
)
return ok(_session_vo(s))