trying to implement the it create react agent but I got stuck on filtering the ui messages. not usre what im doing is wrong but am seeing redundant messages in the ui.
python`{messages
.filter(
(m) =>
(m.type === “human” || m.type === “ai”) &&
m.content &&
(typeof m.content === “string”
? m.content.trim() !== “”
: JSON.stringify(m.content).trim() !== “”)
)
.map((m, i) => (
console.log(“UI for message”, m.id,
values?.ui?.filter((ui: any) => ui.metadata?.message_id === m.id)),
<div key={`msg-${i}`} className="space-y-2">
{/* Normal chat message */}
<ChatMessage message={m} />
{/* Attached UI components for this message */}
{values?.ui
.map((ui: any) => (
<LoadExternalComponent
key={ui.id}
stream={stream}
message={ui}
components={{
weather: WeatherComponent,
}}
/>
))}
</div>
))}
</div>`
`@tool
def get_weather(location: str) → str:
“”“Get the current weather for a location.”“”
weather_data = {
“city”: location,
“temperature”: “72°F”,
“condition”: “Sunny”,
“humidity”: “45%”,
“wind”: “5 mph”,
}
message = AIMessage(
id=str(uuid.uuid4()),
content=f"Weather in {location}: {weather_data['temperature']}, {weather_data['condition']}",
)
push_ui_message(
"weather",
props=weather_data,
message=message, # This associates UI with this specific message
id=f"weather-{location}" # Use unique ID per location
)
return f"Weather in {location}: {weather_data['temperature']}, {weather_data['condition']}"
tools = [
list_products,
product_details,
search_products,
add_to_cart,
view_cart,
checkout,
get_order_status,
get_weather,
]
class CustomAgentState(AgentState):
ui: Annotated[Sequence[AnyUIMessage], ui_message_reducer]
4. Create the agent with tools
agent = create_react_agent(
model=llm,
tools=tools,
state_schema=CustomAgentState,
prompt=shopping_prompt,
)
`
Iknow i have to do the filter on the message id but as this push the ui from the tool how i will have access to the messsage id that agent gives?
Thanks in advance!