Open History to see the last commit
1import { useEffect } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
4
5/**
6 * Harness page for interrupts that arrive on the stream without a resume
7 * binding this client owns. `/api/foreign-interrupt` ends a run with one bound
8 * interrupt and one foreign one; this page renders both by `kind` so the spec
9 * can assert the foreign one is visible but not resolvable, and that it does
10 * not block resolving the bound one.
11 */
12function ForeignInterruptPage() {
13 const { interrupts, messages, sendMessage } = useChat({
14 threadId: 'foreign-1',
15 connection: fetchServerSentEvents('/api/foreign-interrupt'),
16 })
17
18 const assistantText = messages
19 .filter((message) => message.role === 'assistant')
20 .flatMap((message) =>
21 message.parts.flatMap((part) =>
22 part.type === 'text' ? [part.content] : [],
23 ),
24 )
25 .join('')
26
27 useEffect(() => {
28 void sendMessage('go')
29 // Fire the single run once on mount; the harness route ignores the content.
30 // eslint-disable-next-line react-hooks/exhaustive-deps
31 }, [])
32
33 return (
34 <div data-testid="foreign-interrupt-page">
35 <div data-testid="interrupt-count">{interrupts.length}</div>
36 <div data-testid="assistant-text">{assistantText}</div>
37 {interrupts.map((interrupt) => (
38 <div key={interrupt.id} data-testid={`interrupt-${interrupt.id}`}>
39 <span data-testid={`kind-${interrupt.id}`}>{interrupt.kind}</span>
40 <span data-testid={`can-resolve-${interrupt.id}`}>
41 {String(interrupt.canResolve)}
42 </span>
43 <span data-testid={`message-${interrupt.id}`}>
44 {interrupt.message ?? interrupt.reason}
45 </span>
46 {interrupt.kind === 'generic' ? (
47 <button
48 data-testid={`resolve-${interrupt.id}`}
49 onClick={() => interrupt.resolveInterrupt({ confirmed: true })}
50 >
51 Confirm
52 </button>
53 ) : null}
54 </div>
55 ))}
56 </div>
57 )
58}
59
60export const Route = createFileRoute('/foreign-interrupt')({
61 component: ForeignInterruptPage,
62})