This commit is contained in:
parent
1ac121e9cb
commit
2502d2f663
|
|
@ -431,12 +431,10 @@ def tool_call(item: MessageHistoryItem) -> rx.Component:
|
|||
"square-function",
|
||||
width="14px",
|
||||
height="14px",
|
||||
color="var(--prismui-color-3)",
|
||||
),
|
||||
rx.text(
|
||||
item.title,
|
||||
font_size="var(--prismui-font-size-1)",
|
||||
color="var(--prismui-color-3)",
|
||||
class_name=rx.cond(item.is_running, "is_running", None),
|
||||
),
|
||||
align_items="center",
|
||||
|
|
@ -451,13 +449,12 @@ def tool_call(item: MessageHistoryItem) -> rx.Component:
|
|||
margin="4px 0",
|
||||
line_height="1.6",
|
||||
font_size="var(--prismui-font-size-1)",
|
||||
color="var(--prismui-color-3)",
|
||||
),
|
||||
# 审批
|
||||
rx.cond(
|
||||
item.has_approval,
|
||||
rx.hstack(
|
||||
rx.input(
|
||||
rx.text_area(
|
||||
placeholder="请输入",
|
||||
# 值绑定审批用户提示词
|
||||
value=item.approval_user_prompt,
|
||||
|
|
@ -495,6 +492,7 @@ def tool_call(item: MessageHistoryItem) -> rx.Component:
|
|||
),
|
||||
rx.fragment(),
|
||||
),
|
||||
width="100%",
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -187,18 +187,12 @@ class ConversationState(rx.State):
|
|||
if not conversation:
|
||||
return []
|
||||
message_history: list[MessageHistoryItem] = []
|
||||
has_approval = False
|
||||
approval_user_prompt = ""
|
||||
for message in conversation.messages.values():
|
||||
approval = message.approval
|
||||
if isinstance(approval, Approval):
|
||||
has_approval = True
|
||||
approval_user_prompt = approval.user_prompt
|
||||
message_history.append(
|
||||
MessageHistoryItem(
|
||||
**message.model_dump(),
|
||||
has_approval=has_approval,
|
||||
approval_user_prompt=approval_user_prompt,
|
||||
has_approval=isinstance(approval := message.approval, Approval),
|
||||
approval_user_prompt=getattr(approval, "user_prompt", ""),
|
||||
)
|
||||
)
|
||||
return message_history
|
||||
|
|
@ -464,9 +458,9 @@ class ConversationState(rx.State):
|
|||
]
|
||||
message.is_running = False
|
||||
message.title = f"{tool_name}已完成"
|
||||
message.content = cast(
|
||||
str, content
|
||||
) # 约定工具返回分片内容必为字符串
|
||||
# 若内容非空字符串则更新
|
||||
if content := message.content:
|
||||
message.content = content
|
||||
yield # 通知前端更新渲染
|
||||
messages.append(message)
|
||||
|
||||
|
|
|
|||
|
|
@ -220,7 +220,7 @@ async def book_flight(ctx: RunContext[Deps]) -> Flight | NoResult:
|
|||
raise ModelRetry("必须先使用 search_flights 查询航班")
|
||||
|
||||
if not ctx.tool_call_approved:
|
||||
raise ApprovalRequired(metadata={"content": "您要预定哪班航班?"})
|
||||
raise ApprovalRequired(metadata={"content": "请输入航班号:\n\n"})
|
||||
|
||||
# 提取到的航班号
|
||||
extracted_flight_number = (
|
||||
|
|
@ -235,7 +235,11 @@ async def book_flight(ctx: RunContext[Deps]) -> Flight | NoResult:
|
|||
return NoResult()
|
||||
|
||||
if extracted_flight_number not in flight_numbers:
|
||||
raise ModelRetry(f"提取到的航班号 {extracted_flight_number} 不在查询到的航班中")
|
||||
raise ApprovalRequired(
|
||||
metadata={
|
||||
"content": f"提取到的航班号 {extracted_flight_number} 不在查询到的航班中。请确认后重新输入。"
|
||||
}
|
||||
)
|
||||
# 预定到的航班
|
||||
ctx.deps.booked_flight = next(
|
||||
flight
|
||||
|
|
@ -329,7 +333,14 @@ async def run_stream_events(
|
|||
event.part.content += f"{searched_flight.number} {searched_flight.airfare}$ 于 {searched_flight.date.strftime('%Y-%m-%d')} 从 {searched_flight.origin_airport_code} 到 {searched_flight.destination_airport_code}\n\n"
|
||||
yield event
|
||||
|
||||
# 预定航班无需业务定义
|
||||
case "预定航班":
|
||||
if isinstance(deps.booked_flight, Flight):
|
||||
event.part.content = ""
|
||||
else:
|
||||
event.part.content = (
|
||||
"未提取到航班号,请检查后重试\n\n"
|
||||
)
|
||||
yield event
|
||||
|
||||
case AgentRunResultEvent(result=result):
|
||||
match result:
|
||||
|
|
@ -337,11 +348,10 @@ async def run_stream_events(
|
|||
match output:
|
||||
case Flight():
|
||||
yield AgentPromptEvent(
|
||||
content=f"已预定航班:\n\n{output.number} {output.airfare}$ 于 {output.date.strftime('%Y-%m-%d')} 从 {output.origin_airport_code} 到 {output.destination_airport_code}\n\n"
|
||||
content=f"已成功预定:\n\n{output.number} {output.airfare}$ 于 {output.date.strftime('%Y-%m-%d')} 从 {output.origin_airport_code} 到 {output.destination_airport_code}\n\n本次会话结束。"
|
||||
)
|
||||
case NoResult():
|
||||
yield AgentPromptEvent(
|
||||
content="未提取到航班号,请检查后重试"
|
||||
content="预定失败\n\n本次会话结束。"
|
||||
)
|
||||
case DeferredToolRequests():
|
||||
yield event
|
||||
yield event
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in New Issue