Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

phase-capture.ts

140 lines3.7 KB

1/**
2 * Per-testId capture of middleware phase observations + yielded chunks for the
3 * `phase-recorder` mode of `/middleware-test`. Mirrors the pattern used by
4 * `otel-capture.ts`: server-side middleware records into a module-global Map
5 * keyed by `testId`; the test page fetches it via
6 * `GET /api/middleware-test?testId=...&kind=phase` after the run finishes and
7 * surfaces it in DOM elements that the Playwright spec reads.
8 */
9​
10export interface YieldedChunkSummary {
11 /** The chunk's discriminant (e.g. RUN_STARTED, TEXT_MESSAGE_CONTENT). */
12 type: string
13 /** The run ID emitted on this specific stream chunk, when the event has one. */
14 runId?: string
15 outcomeType?: string
16 interruptCount?: number
18​
19export interface GenericBoundaryCapture {
20 phase: string
21 runId: string
23​
24export interface GenericResolutionCapture {
25 definitionId: string
26 status: 'resolved' | 'cancelled'
27 response?: unknown
29​
30export interface GenericToolExecutionCapture {
31 name: string
32 side: 'server'
34​
35export interface PhaseCapture {
36 /**
37 * Phases observed by `onChunk` across the entire run, in chunk-arrival
38 * order. Duplicates are preserved so tests can spot transitions; specs
39 * that only care about presence should use `.includes('structuredOutput')`.
40 */
41 phases: Array<string>
42 /** Count of `onFinish` invocations. */
43 onFinishCount: number
44 /** Count of `onError` invocations. */
45 onErrorCount: number
46 /**
47 * Chunks that were yielded out of `chat()` to the SSE consumer. Captured
48 * by teeing the iterable in `api.middleware-test.ts` after the middleware
49 * chain has applied its transformations.
50 */
51 yieldedChunks: Array<YieldedChunkSummary>
52 boundaries: Array<GenericBoundaryCapture>
53 resolutions: Array<GenericResolutionCapture>
54 policies: Array<'continue' | 'cancel' | 'stop'>
55 toolExecutions: Array<GenericToolExecutionCapture>
57​
58const captures: Map<string, PhaseCapture> = new Map()
59​
60function bucketFor(captureId: string): PhaseCapture {
61 let bucket = captures.get(captureId)
62 if (!bucket) {
63 bucket = {
64 phases: [],
65 onFinishCount: 0,
66 onErrorCount: 0,
67 yieldedChunks: [],
68 boundaries: [],
69 resolutions: [],
70 policies: [],
71 toolExecutions: [],
72 }
73 captures.set(captureId, bucket)
74 }
75 return bucket
77​
78export function resetPhaseCapture(captureId: string): void {
79 captures.set(captureId, {
80 phases: [],
81 onFinishCount: 0,
82 onErrorCount: 0,
83 yieldedChunks: [],
84 boundaries: [],
85 resolutions: [],
86 policies: [],
87 toolExecutions: [],
88 })
90​
91export function getPhaseCapture(captureId: string): PhaseCapture {
92 return bucketFor(captureId)
94​
95export function recordPhase(captureId: string, phase: string): void {
96 bucketFor(captureId).phases.push(phase)
98​
99export function recordOnFinish(captureId: string): void {
100 bucketFor(captureId).onFinishCount += 1
102​
103export function recordOnError(captureId: string): void {
104 bucketFor(captureId).onErrorCount += 1
106​
107export function recordYieldedChunk(
108 captureId: string,
109 chunk: YieldedChunkSummary,
110): void {
111 bucketFor(captureId).yieldedChunks.push(chunk)
113​
114export function recordGenericBoundary(
115 captureId: string,
116 boundary: GenericBoundaryCapture,
117): void {
118 bucketFor(captureId).boundaries.push(boundary)
120​
121export function recordGenericResolution(
122 captureId: string,
123 resolution: GenericResolutionCapture,
124): void {
125 bucketFor(captureId).resolutions.push(resolution)
127​
128export function recordGenericPolicy(
129 captureId: string,
130 policy: 'continue' | 'cancel' | 'stop',
131): void {
132 bucketFor(captureId).policies.push(policy)
134​
135export function recordGenericToolExecution(
136 captureId: string,
137 execution: GenericToolExecutionCapture,
138): void {
139 bucketFor(captureId).toolExecutions.push(execution)