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