import { useState } from "react"; import { CopilotKitProvider, CopilotChat, useInterrupt, useConfigureSuggestions, } from "@copilotkit/react-core/v2"; import "@copilotkit/react-core/v2/styles.css"; import { InterruptCard } from "../components/InterruptCard"; type AgentType = "tanstack" | "aisdk"; /** * for one agent with AG-UI interrupts rendered INSIDE the chat. * * The `bookFlight` tool carries each SDK's native `needsApproval` flag, so * asking the agent to book a flight pauses the run as a standard AG-UI * interrupt. `useInterrupt({renderInChat:true})` drops the InterruptCard into * the conversation; resolving it resumes the run with the human's choice. * * The resolved payload is a real booking result (`{status:"booked"|"declined"}`) * rather than a bare approval flag — that's what the tool call "returns", so the * model treats each flight as settled instead of re-calling bookFlight in a loop. */ function InterruptChat({ agentType }: { agentType: AgentType }) { useConfigureSuggestions( { available: "always", consumerAgentId: agentType, suggestions: [ { title: "Book 1 flight", message: "Book a flight to Tokyo" }, { title: "Book 2 flights", message: "Book flights to Berlin and Rome" }, { title: "Book 3 flights", message: "Book flights to Tokyo, Paris and London", }, ], }, [agentType], ); useInterrupt({ agentId: agentType, renderInChat: true, render: ({ interrupt, interrupts, resolve, cancel }) => { const list = interrupts.length > 0 ? interrupts : interrupt ? [interrupt] : []; return (
{list.map((it, i) => ( resolve( (payload as { approved?: boolean })?.approved ? { status: "booked" } : { status: "declined" }, it.id, ) } onCancel={() => cancel(it.id)} /> ))}
); }, }); return ( console.error("[CopilotChat] Error:", event)} /> ); } export default function InterruptsRoute() { const [agentType, setAgentType] = useState("tanstack"); return (

Native interrupts

Agent:

The bookFlight tool uses each SDK's native{" "} needsApproval flag → surfaces as an AG-UI interrupt in-chat. Try the suggestion pills below. Requires{" "} OPENAI_API_KEY on the server.

); }