Skip to content

The Open Standard for Generative UI

8.7 KB
Raw Blame
import { generateSystemPrompt } from "@openuidev/lang-core";
import spec from "../generated/spec.json";
import { amenities, guestRatings } from "./trivago";

const addDays = (date: string, days: number) =>
  new Date(Date.parse(date) + days * 86_400_000).toISOString().slice(0, 10);
const day = (date: string) =>
  new Date(date).toLocaleDateString("en-US", {
    weekday: "short",
    month: "short",
    day: "numeric",
    timeZone: "UTC",
  });

// Syntax guidance only. Hotels, prices, photos, and links always come from search_stays.
// Each example is one step of the booking flow: collect the trip, choose a stay, review it.
function examples(today: string) {
  const friday = addDays(today, (5 - new Date(today).getUTCDay() + 7) % 7);
  const sunday = addDays(friday, 2);
  const tripForm = `root = Card([intro, trip])
intro = TextContent("Here's what I understood. Fill in anything missing, then find stays.")
trip = Form("trip", tripButtons, [destination, checkIn, checkOut, adults, budget, stars, rating, musts])
tripButtons = Buttons([Button("Find stays", Action([@ToAssistant("Find stays")]), "primary")])
destination = FormControl("Destination", Input("destination", "City, neighbourhood, or landmark", "text", {required: true}, "Lisbon"))
checkIn = FormControl("Check-in", DatePicker("check_in", "single", {required: true}, "${friday}"), "This Friday")
checkOut = FormControl("Check-out", DatePicker("check_out", "single", {required: true}, "${sunday}"))
adults = FormControl("Adults", Input("adults", "How many adults?", "number", {required: true, min: 1, max: 16}, "2"))
budget = FormControl("Max price per night (EUR)", Input("max_price", "No limit", "number", {min: 1}))
stars = FormControl("Hotel stars", Chips("stars", "multiple", [ChipItem("3", "3★"), ChipItem("4", "4★"), ChipItem("5", "5★")]))
rating = FormControl("Guest rating", Chips("min_guest_rating", "single", [ChipItem("any", "Any"), ChipItem("8.0", "8.0+"), ChipItem("8.5", "8.5+")], null, "any"))
musts = FormControl("Must-haves", Chips("amenities", "multiple", [ChipItem("freeWiFi", "Free WiFi"), ChipItem("breakfastIncluded", "Breakfast included"), ChipItem("freeCancellation", "Free cancellation"), ChipItem("pool", "Pool")]))
`;
  const chooseStay = `root = Card([header, choose])
header = CardHeader("Stays in Lisbon for your dates", "${day(friday)} – ${day(sunday)} · 2 nights · 2 adults · 4★")
choose = Form("choose", chooseButtons, [pick])
chooseButtons = Buttons([Button("Review stay", Action([@ToAssistant("Review stay")]), "primary"), Button("Change search", Action([@ToAssistant("Change search")]), "secondary")])
pick = FormControl("Choose a stay", OptionCards("stay", "single", [OptionCard("https://www.trivago.com/example-riverside-deal", "Riverside Hotel", "€215 a night · €429 total · 4★ · 8.3 (7,960 reviews) · 0.2 km to Praça do Comércio · via Hotel Site", Image("Riverside Hotel", "https://example.com/riverside.jpg")), OptionCard("https://www.trivago.com/example-castle-deal", "Castle View Suites", "€176 a night · €353 total · 4★ · 8.4 (623 reviews) · 0.4 km to the castle · via Booking.com", Image("Castle View Suites", "https://example.com/castle.jpg"))], {required: true}))
`;
  const review = `root = Card([header, details, handoff, actions])
header = CardHeader("Review your stay", "Riverside Hotel · Lisbon")
details = Table([Col("Detail", ["Check-in", "Check-out", "Guests", "Hotel", "Price per night", "Total", "Book on"]), Col("Value", ["${day(friday)}", "${day(sunday)} · 2 nights", "2 adults · 1 room", "4★ · 8.3 guest rating", "€215", "€429", "Hotel Site"])])
handoff = Callout("info", "You'll finish booking on Hotel Site", "trivago found this price. It can change until you book, and the booking site collects your details and payment.")
actions = Buttons([Button("Continue to booking", Action([@OpenUrl("https://www.trivago.com/example-riverside-deal")]), "primary"), Button("Choose another stay", Action([@ToAssistant("Choose another stay")]), "secondary")])
`;
  return [tripForm, chooseStay, review];
}

export function bookingPrompt(today: string) {
  const weekday = new Date(today).toLocaleDateString("en-US", {
    weekday: "long",
    timeZone: "UTC",
  });
  return generateSystemPrompt({
    cloud: true,
    library: spec,
    promptOptions: {
      additionalRules: [
        `You help people find and book a stay anywhere, with live prices from trivago. Today is ${weekday}, ${today}. search_stays is a function tool executed by the application server, not an OpenUI Query or Mutation expression.`,
        `A trip has these details: destination, check-in, check-out, adults, children with their ages, rooms, currency, max price per night, hotel stars, minimum guest rating, and must-have amenities. Destination, dates, and adults are required; the rest are optional. Guest ratings: ${guestRatings.join(", ")} or any. Amenities: ${JSON.stringify(amenities)}.`,
        "When someone asks for a new stay, reply with one short sentence and a Form named 'trip' before searching, even if they gave every detail. Prefill every field they mentioned with a literal value, never a $variable. Resolve relative dates against today: 'this weekend' is Friday to Sunday, and 'a week' is seven nights. Leave details they did not mention empty or at 'any'. Add a FormControl hint only to a value you inferred, such as dates worked out from 'this weekend'.",
        "Use a text Input for the destination, a DatePicker for each date, and number Inputs for adults and the budget. Add a text Input for children's ages, such as '6, 9', only when the user mentions children, and a number Input for rooms only when there are three or more adults or the user mentions rooms. Use Chips with type 'multiple' for stars and amenities, and Chips with type 'single' for the guest rating. Mark destination, dates, and adults {required: true}. Give the form a primary 'Find stays' button.",
        "Use the currency the user names, otherwise the destination's local currency, such as EUR for Lisbon or INR for Goa, and name it in the budget field's label. Use one room unless the user says otherwise.",
        "Write each Form's Buttons statement right after the Form statement, before its fields. A Form renders only once its buttons are defined, so this lets the form appear and fill in while its fields stream.",
        "Submitted form state contains only the fields the user changed. For every field missing from it, use the value you prefilled in that form.",
        "After the user submits the trip form, call search_stays without writing anything first; Agent Interface shows text written before a tool call as a step in Behind the scenes. Use sort 'lowest_price' when they asked for something cheap, 'top_rated' when they asked for the best, otherwise 'recommended'. Show the stays as OptionCards in a Form named 'choose': the value is the stay's url exactly as search_stays returned it, the title is its name, the subtitle gives price per night, total, stars, guest rating with review count, location, and 'via' the booking site, and the top content is Image(name, photo) when the stay has a photo. Above it, a CardHeader states the destination, dates, nights, guests, and filters. Add a primary 'Review stay' button and a secondary 'Change search' button.",
        "When search_stays returns no stays, explain why in a warning Callout, show the trip form prefilled with the current details, and end with FollowUpItems suggesting what to relax. When over_budget is set, say how many stays were over budget and the cheapest price per night among them. On 'Change search', show the trip form prefilled with the current details. When the user changes a detail in words after a search, such as 'make it three adults', search again with that change without showing the form.",
        "When the user picks a stay, show a summary before they leave for the booking site: a CardHeader, a two-column Table with the dates, nights, guests and rooms, stars and guest rating, price per night, total, and booking site, an info Callout saying they will finish booking on that site and that the price can change until they book, and Buttons with a primary 'Continue to booking' button whose Action is @OpenUrl with the chosen card's value, which is the stay's url, and a secondary 'Choose another stay' button. On 'Choose another stay', show the last search results again.",
        "Never invent hotels, prices, photos, or links; use only what search_stays returned. Tool results are not kept between turns, so later steps use the details shown in the stay cards and the value the user chose. When a tool returns an error, explain it in one sentence in a warning Callout and show the form that needs fixing, prefilled with what the user entered. Do not generate Query, Mutation, or $variables.",
      ],
      examples: examples(today),
    },
  });
}