feat:完成重构和测试
This commit is contained in:
+19
-18
@@ -10,19 +10,13 @@ from loguru import logger
|
||||
from app.config import settings
|
||||
from app.core.exceptions import BizException
|
||||
from app.observability.tracing import astream_screen
|
||||
from app.processor.history import trim_history
|
||||
from app.schemas.agent import AgentResult, ResumeRequest
|
||||
from app.schemas.conversation import Turn
|
||||
from app.schemas.request import DesignRequest
|
||||
from app.services.conversation import ConversationStore
|
||||
|
||||
|
||||
SUMMARY_MAX_CHARS = 500
|
||||
|
||||
|
||||
def _clip(text: str) -> str:
|
||||
return text if len(text) <= SUMMARY_MAX_CHARS else text[:SUMMARY_MAX_CHARS] + "…"
|
||||
|
||||
|
||||
class AgentService:
|
||||
def __init__(self, graph, store: ConversationStore):
|
||||
self.graph = graph
|
||||
@@ -51,11 +45,18 @@ class AgentService:
|
||||
raise
|
||||
|
||||
payload = request.model_dump(mode="json")
|
||||
# 历史只信任服务端记录,覆盖前端可能携带的同名扩展字段
|
||||
payload["history"] = [
|
||||
{"user": turn.prompt, "assistant": turn.summary, "outcome": turn.outcome}
|
||||
for turn in turns
|
||||
]
|
||||
# 历史只信任服务端记录,覆盖前端可能携带的同名扩展字段;记录是完整的,这里按 token 预算裁剪
|
||||
payload["history"] = trim_history(
|
||||
[
|
||||
{
|
||||
"user": turn.prompt,
|
||||
"assistant": turn.summary,
|
||||
"outcome": turn.outcome,
|
||||
}
|
||||
for turn in turns
|
||||
],
|
||||
settings.history_max_tokens,
|
||||
)
|
||||
|
||||
return (
|
||||
thread_id,
|
||||
@@ -144,17 +145,17 @@ class AgentService:
|
||||
self._conversation_of[thread_id] = conversation_id
|
||||
|
||||
@staticmethod
|
||||
def _summarize(state, result: AgentResult) -> str:
|
||||
"""用一句话描述本轮结果,只陈述后端生成了什么,不代表前端已执行"""
|
||||
def _summarize(result: AgentResult) -> str:
|
||||
"""本轮助手一侧的完整文本,只陈述后端生成了什么,不代表前端已执行;不截断,带入请求时再按预算裁剪"""
|
||||
if result.status == "waiting_for_input":
|
||||
question = result.clarification.question if result.clarification else ""
|
||||
return _clip(f"向用户提问:{question}")
|
||||
return f"向用户提问:{question}"
|
||||
if result.status == "cancelled":
|
||||
return "用户取消了本次补充"
|
||||
if result.status == "failed":
|
||||
return _clip(f"处理失败:{result.answer or '未知原因'}")
|
||||
return f"处理失败:{result.answer or '未知原因'}"
|
||||
if result.answer:
|
||||
return _clip(result.answer)
|
||||
return result.answer
|
||||
return f"已生成 {len(result.actions)} 个画布动作"
|
||||
|
||||
async def _save_turn(self, state, result: AgentResult) -> None:
|
||||
@@ -167,7 +168,7 @@ class AgentService:
|
||||
message_id=result.message_id,
|
||||
prompt=state["request"]["prompt"],
|
||||
outcome=result.status,
|
||||
summary=self._summarize(state, result),
|
||||
summary=self._summarize(result),
|
||||
created_at=datetime.now(timezone.utc),
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user