refactor: extract prompt bundle builder from agent

This commit is contained in:
2026-03-01 15:21:49 +08:00
parent b323a64b0b
commit 55e6fd51ec
2 changed files with 55 additions and 32 deletions

50
core/prompt_flow.py Normal file
View File

@@ -0,0 +1,50 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING, List
if TYPE_CHECKING:
from core.pydantic_ai_agent import AgentDeps, ConversationState, CustomerMessage, CustomerServiceAgent
@dataclass
class PromptBundle:
user_prompt: str
deps: "AgentDeps"
history: List
def build_prompt_bundle(
agent: "CustomerServiceAgent",
*,
message: "CustomerMessage",
state: "ConversationState",
) -> PromptBundle:
from core.pydantic_ai_agent import AgentDeps
user_prompt = agent._build_prompt(message, state)
profile_context = agent._get_customer_profile_context(message.from_id)
if profile_context:
user_prompt = profile_context + "\n\n" + user_prompt
refusal_hint = agent._get_refusal_context_hint(message.from_id, message.msg, profile_context or "")
if refusal_hint:
user_prompt = refusal_hint + "\n\n" + user_prompt
conv_context = agent._get_conversation_context(message.from_id, acc_id=message.acc_id or "")
if conv_context:
user_prompt = conv_context + user_prompt
intent_hint = agent._get_intent_emotion_hint(message.msg)
if intent_hint:
user_prompt = intent_hint + "\n\n" + user_prompt
deps = AgentDeps(
msg_id=message.msg_id,
acc_id=message.acc_id,
from_id=message.from_id,
platform=message.acc_type,
)
history = agent.message_histories.get(message.from_id, [])
return PromptBundle(user_prompt=user_prompt, deps=deps, history=history)