Skip to content

Commit e88a04f

Browse files
authored
fix: Prevent flight rebooking by adding booking confirmation message (#549)
This PR addresses an issue where the agent could attempt to rebook a flight immediately after a successful booking. ## Problem Previously, after the `insert_ticket` tool successfully executed, a confirmation message was not added to the agent's conversation history from our custom node. This lack of explicit confirmation left the agent without the necessary context to know the booking was complete. As a result, the agent could incorrectly infer that the booking task had failed and attempt to perform the booking again. ## Solution This PR modifies the custom node to add a success message to the agent's conversation history immediately after the `insert_ticket` tool completes successfully. This message explicitly confirms that the flight has been booked, providing the agent with the proper context to conclude the booking process and not attempt a rebooking. This PR also makes the booking success/decline messages consistent between LangGraph and UI chat history. This is so that after a page refresh, the messages remains consistent on the Chat UI.
1 parent a9c32c4 commit e88a04f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

agent/react_graph.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from langchain_core.messages import (
2020
AIMessage,
2121
BaseMessage,
22+
HumanMessage,
2223
ToolCall,
2324
)
2425
from langchain_core.prompts.chat import ChatPromptTemplate
@@ -164,7 +165,16 @@ async def insert_ticket_node(state: UserState):
164165
if hasattr(last_message, "tool_calls") and len(last_message.tool_calls) > 0:
165166
tool_call = last_message.tool_calls[0]
166167
tool_args = tool_call.get("args")
167-
await insert_ticket.ainvoke(tool_args)
168+
output = await insert_ticket.ainvoke(tool_args)
169+
human_message = HumanMessage(content="Looks good to me.")
170+
ai_message = AIMessage(
171+
content=(
172+
"Your flight has been successfully booked."
173+
if output == "null"
174+
else output
175+
)
176+
)
177+
return {"messages": [human_message, ai_message]}
168178

169179
# Define constant node strings
170180
AGENT_NODE = "agent"

app.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ async def book_flight(request: Request, params: str = Body(embed=True)):
181181
response = await agent.user_session_insert_ticket(request.session["uuid"], params)
182182
# Note in the history, that the ticket has been successfully booked
183183
request.session["history"].append(
184-
{"type": "ai", "data": {"content": "I have booked your ticket."}}
184+
{"type": "ai", "data": {"content": "Your flight has been successfully booked."}}
185185
)
186186
return response
187187

@@ -198,7 +198,10 @@ async def decline_flight(request: Request):
198198
{"type": "ai", "data": {"content": "Please confirm if you would like to book."}}
199199
)
200200
request.session["history"].append(
201-
{"type": "human", "data": {"content": "I changed my mind."}}
201+
{
202+
"type": "human",
203+
"data": {"content": "I changed my mind. Decline ticket booking."},
204+
}
202205
)
203206
request.session["history"].append({"type": "ai", "data": {"content": response}})
204207
return response

0 commit comments

Comments
 (0)