forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2ui_fixed_schema.py
More file actions
64 lines (55 loc) · 2.01 KB
/
Copy patha2ui_fixed_schema.py
File metadata and controls
64 lines (55 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Fixed-schema A2UI tool — flight search results.
The A2UI component schema is loaded from JSON; only the flight data changes per
call. The tool result carries ``a2ui_operations``, which the frontend renders.
"""
from __future__ import annotations
from pathlib import Path
from typing import Any, TypedDict
from claude_agent_sdk import tool
from copilotkit import a2ui
CATALOG_ID = "copilotkit://app-dashboard-catalog"
FLIGHT_SURFACE_ID = "flight-search-results"
FLIGHT_SCHEMA = a2ui.load_schema(
Path(__file__).parent / "a2ui" / "schemas" / "flight_schema.json"
)
class Flight(TypedDict):
id: str
airline: str
airlineLogo: str
flightNumber: str
origin: str
destination: str
date: str
departureTime: str
arrivalTime: str
duration: str
status: str
statusIcon: str
price: str
@tool(
"search_flights",
"Search for flights and display the results as rich cards. Return exactly 2 "
"flights. Each flight must have: id, airline, airlineLogo (Google favicon API "
"URL for the airline domain), flightNumber, origin, destination, date (e.g. "
'"Tue, Mar 18" — use near-future dates), departureTime, arrivalTime, duration '
'(e.g. "4h 25m"), status (e.g. "On Time" or "Delayed"), statusIcon (colored '
"dot URL: https://placehold.co/12/22c55e/22c55e.png for On Time, "
'https://placehold.co/12/eab308/eab308.png for Delayed), and price (e.g. "$289").',
{"flights": list[Flight]},
)
async def search_flights(args: dict[str, Any]) -> dict[str, Any]:
flights = args.get("flights", [])
return {
"content": [
{
"type": "text",
"text": a2ui.render(
operations=[
a2ui.create_surface(FLIGHT_SURFACE_ID, catalog_id=CATALOG_ID),
a2ui.update_components(FLIGHT_SURFACE_ID, FLIGHT_SCHEMA),
a2ui.update_data_model(FLIGHT_SURFACE_ID, {"flights": flights}),
]
),
}
]
}