import { createFlow, START, END } from "@waniwani/sdk/mcp";
import { z } from "zod";
import { bookingApi } from "./booking-api";
export const bookingFunnel = createFlow({
id: "booking",
title: "Booking",
description: "Use when the user wants to book a service. Collects service, slot, and contact info, then confirms the booking.",
state: {
service: z.string().describe("Which service the user wants to book"),
availableSlots: z.array(z.string()).describe("ISO timestamps fetched from the API"),
slot: z.string().describe("Chosen ISO timestamp"),
name: z.string().describe("Full name"),
email: z.string().describe("Email for the confirmation"),
bookingRef: z.string().optional().describe("Booking reference (set on confirmation)"),
},
})
.addNode({
id: "ask_service",
label: "Pick a service",
run: ({ interrupt }) =>
interrupt({
service: {
question: "What would you like to book?",
suggestions: ["Consultation", "Demo", "Onboarding session"],
},
}),
})
.addNode({
id: "fetch_availability",
label: "Fetch availability",
hideFromFunnel: true,
run: async ({ state }) => {
const slots = await bookingApi.listAvailability(state.service);
return { availableSlots: slots };
},
})
.addNode({
id: "ask_slot",
label: "Pick a slot",
run: ({ state, interrupt }) =>
interrupt({
slot: {
question: "Pick a time that works for you.",
suggestions: state.availableSlots,
context: `Available slots (ISO timestamps): ${state.availableSlots.join(", ")}. Present these to the user in their local timezone in a friendly format. When they pick one, set slot to the matching ISO timestamp.`,
},
}),
})
.addNode({
id: "ask_name_email",
label: "Capture contact",
run: ({ interrupt }) =>
interrupt({
name: { question: "Your name?" },
email: { question: "And your email for the confirmation?" },
}),
})
.addNode({
id: "confirm_booking",
label: "Confirm booking",
run: async ({ state }) => {
const booking = await bookingApi.create({
service: state.service,
slot: state.slot,
name: state.name,
email: state.email,
});
return { bookingRef: booking.ref };
},
})
.addEdge(START, "ask_service")
.addEdge("ask_service", "fetch_availability")
.addEdge("fetch_availability", "ask_slot")
.addEdge("ask_slot", "ask_name_email")
.addEdge("ask_name_email", "confirm_booking")
.addEdge("confirm_booking", END)
.compile();