Open History to see the last commit
1import { useMemo, useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { useChat } from '@tanstack/ai-react'
4import { EventType } from '@tanstack/ai/client'
5import type { StreamChunk } from '@tanstack/ai/client'
6import type { SubscribeConnectionAdapter } from '@tanstack/ai-client'
7
8function InterruptLineagePage() {
9 const [eventCount, setEventCount] = useState(0)
10 const replay = useMemo(() => {
11 const chunks: Array<StreamChunk> = []
12 let wake: (() => void) | undefined
13 const connection: SubscribeConnectionAdapter = {
14 async *subscribe(signal) {
15 while (!signal?.aborted) {
16 const chunk = chunks.shift()
17 if (chunk) {
18 yield chunk
19 } else {
20 await new Promise<void>((resolve) => {
21 wake = resolve
22 signal?.addEventListener('abort', () => resolve(), { once: true })
23 })
24 }
25 }
26 },
27 send: () => Promise.resolve(),
28 }
29 return {
30 connection,
31 publish(chunk: StreamChunk) {
32 chunks.push(chunk)
33 wake?.()
34 wake = undefined
35 },
36 }
37 }, [])
38 const { interrupts, isSubscribed } = useChat({
39 threadId: 'lineage-1',
40 connection: replay.connection,
41 live: true,
42 onChunk: () => setEventCount((count) => count + 1),
43 })
44
45 const publish = (chunk: StreamChunk) => replay.publish(chunk)
46 const start = (runId: string, parentRunId?: string) =>
47 publish({
48 type: EventType.RUN_STARTED,
49 threadId: 'lineage-1',
50 runId,
51 parentRunId,
52 timestamp: Date.now(),
53 })
54
55 return (
56 <section style={{ fontSize: 20, lineHeight: 1.5, padding: 24 }}>
57 <h1 style={{ fontSize: 28, marginBottom: 16 }}>Interrupt replay</h1>
58 <p>
59 Events processed: <span data-testid="event-count">{eventCount}</span>
60 </p>
61 <p>
62 Pending decisions:{' '}
63 <span data-testid="interrupt-count">{interrupts.length}</span>
64 </p>
65 <fieldset
66 disabled={!isSubscribed}
67 style={{
68 fontSize: 'inherit',
69 display: 'flex',
70 flexWrap: 'wrap',
71 gap: 12,
72 padding: '12px 0',
73 }}
74 >
75 <legend>Replay events</legend>
76 <button
77 className="rounded border px-4 py-2"
78 style={{ font: 'inherit' }}
79 onClick={() => start('run-A')}
80 >
81 Start parent
82 </button>
83 <button
84 className="rounded border px-4 py-2"
85 style={{ font: 'inherit' }}
86 onClick={() => start('run-B', 'run-A')}
87 >
88 Link child
89 </button>
90 <button
91 className="rounded border px-4 py-2"
92 style={{ font: 'inherit' }}
93 onClick={() =>
94 publish({
95 type: EventType.RUN_FINISHED,
96 runId: 'run-B',
97 threadId: 'lineage-1',
98 timestamp: Date.now(),
99 outcome: { type: 'success' },
100 })
101 }
102 >
103 Finish child
104 </button>
105 <button
106 className="rounded border px-4 py-2"
107 style={{ font: 'inherit' }}
108 onClick={() =>
109 publish({
110 type: EventType.RUN_FINISHED,
111 runId: 'run-A',
112 threadId: 'lineage-1',
113 timestamp: Date.now(),
114 outcome: {
115 type: 'interrupt',
116 interrupts: [
117 {
118 id: 'pause-A',
119 reason: 'confirmation',
120 message: 'Approve this step?',
121 },
122 ],
123 },
124 })
125 }
126 >
127 Pause parent
128 </button>
129 </fieldset>
130 {interrupts.map((interrupt) => (
131 <p key={interrupt.id}>{interrupt.message}</p>
132 ))}
133 </section>
134 )
135}
136
137export const Route = createFileRoute('/interrupt-lineage')({
138 component: InterruptLineagePage,
139})