-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathstream.ts
More file actions
154 lines (136 loc) · 5.6 KB
/
stream.ts
File metadata and controls
154 lines (136 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Event types emitted by AIService
*/
import type { z } from "zod";
import type { MuxReasoningPart, MuxTextPart, MuxToolPart } from "./message";
import type {
AutoCompactionCompletedEventSchema,
AutoCompactionTriggeredEventSchema,
AutoRetryAbandonedEventSchema,
AutoRetryScheduledEventSchema,
AutoRetryStartingEventSchema,
ErrorEventSchema,
ReasoningDeltaEventSchema,
ReasoningEndEventSchema,
StreamAbortReasonSchema,
StreamAbortEventSchema,
StreamLifecycleEventSchema,
StreamLifecyclePhaseSchema,
StreamLifecycleSnapshotSchema,
StreamDeltaEventSchema,
StreamEndEventSchema,
StreamStartEventSchema,
ToolCallDeltaEventSchema,
ToolCallEndEventSchema,
ToolCallStartEventSchema,
BashOutputEventSchema,
TaskCreatedEventSchema,
UsageDeltaEventSchema,
RuntimeStatusEventSchema,
} from "../orpc/schemas";
/**
* Completed message part (reasoning, text, or tool) suitable for serialization
* Used in StreamEndEvent and partial message storage
*/
export type CompletedMessagePart = MuxReasoningPart | MuxTextPart | MuxToolPart;
export type StreamStartEvent = z.infer<typeof StreamStartEventSchema>;
export type StreamDeltaEvent = z.infer<typeof StreamDeltaEventSchema>;
export type StreamEndEvent = z.infer<typeof StreamEndEventSchema>;
export type StreamAbortReason = z.infer<typeof StreamAbortReasonSchema>;
export type StreamLifecyclePhase = z.infer<typeof StreamLifecyclePhaseSchema>;
export type StreamLifecycleSnapshot = z.infer<typeof StreamLifecycleSnapshotSchema>;
export type StreamLifecycleEvent = z.infer<typeof StreamLifecycleEventSchema>;
export function copyStreamLifecycleSnapshot(
snapshot: Pick<StreamLifecycleSnapshot, "phase" | "hadAnyOutput" | "abortReason">
): StreamLifecycleSnapshot {
return {
phase: snapshot.phase,
hadAnyOutput: snapshot.hadAnyOutput,
...(snapshot.abortReason != null ? { abortReason: snapshot.abortReason } : {}),
};
}
export function areStreamLifecycleSnapshotsEqual(
left: Pick<StreamLifecycleSnapshot, "phase" | "hadAnyOutput" | "abortReason"> | null,
right: Pick<StreamLifecycleSnapshot, "phase" | "hadAnyOutput" | "abortReason"> | null
): boolean {
return (
left === right ||
(left !== null &&
right !== null &&
left.phase === right.phase &&
left.hadAnyOutput === right.hadAnyOutput &&
(left.abortReason ?? null) === (right.abortReason ?? null))
);
}
export function isInFlightStreamLifecyclePhase(phase: StreamLifecyclePhase): boolean {
return phase === "preparing" || phase === "streaming" || phase === "completing";
}
export function hasInFlightStreamLifecycle(
snapshot: Pick<StreamLifecycleSnapshot, "phase"> | null | undefined
): boolean {
return snapshot != null && isInFlightStreamLifecyclePhase(snapshot.phase);
}
export interface StreamAbortReasonSnapshot {
reason: StreamAbortReason;
at: number;
}
export type StreamAbortEvent = z.infer<typeof StreamAbortEventSchema>;
export type ErrorEvent = z.infer<typeof ErrorEventSchema>;
export type BashOutputEvent = z.infer<typeof BashOutputEventSchema>;
export type TaskCreatedEvent = z.infer<typeof TaskCreatedEventSchema>;
export type ToolCallStartEvent = z.infer<typeof ToolCallStartEventSchema>;
export type ToolCallDeltaEvent = z.infer<typeof ToolCallDeltaEventSchema>;
export type ToolCallEndEvent = z.infer<typeof ToolCallEndEventSchema>;
export type ReasoningDeltaEvent = z.infer<typeof ReasoningDeltaEventSchema>;
export type ReasoningEndEvent = z.infer<typeof ReasoningEndEventSchema>;
/**
* Emitted on each AI SDK finish-step event, providing incremental usage updates.
* Allows UI to update token display as steps complete (after each tool call or at stream end).
*/
export type UsageDeltaEvent = z.infer<typeof UsageDeltaEventSchema>;
export type AutoCompactionTriggeredEvent = z.infer<typeof AutoCompactionTriggeredEventSchema>;
export type AutoCompactionCompletedEvent = z.infer<typeof AutoCompactionCompletedEventSchema>;
export type AutoRetryScheduledEvent = z.infer<typeof AutoRetryScheduledEventSchema>;
export type AutoRetryStartingEvent = z.infer<typeof AutoRetryStartingEventSchema>;
export type AutoRetryAbandonedEvent = z.infer<typeof AutoRetryAbandonedEventSchema>;
/**
* Progress event for pre-stream startup work.
* Used for both runtime readiness and generic startup breadcrumbs in the barrier UI.
*/
export type RuntimeStatusEvent = z.infer<typeof RuntimeStatusEventSchema>;
/**
* Shared runtime-status helpers used by both the backend session replay path and the
* renderer aggregator so startup breadcrumbs follow the same lifecycle semantics.
*/
export function copyRuntimeStatusEvent(
status: Pick<
RuntimeStatusEvent,
"type" | "workspaceId" | "phase" | "runtimeType" | "source" | "detail"
>
): RuntimeStatusEvent {
return {
type: status.type,
workspaceId: status.workspaceId,
phase: status.phase,
runtimeType: status.runtimeType,
...(status.source != null ? { source: status.source } : {}),
...(status.detail != null ? { detail: status.detail } : {}),
};
}
export function areRuntimeStatusEventsEqual(
left: Pick<RuntimeStatusEvent, "phase" | "runtimeType" | "source" | "detail"> | null,
right: Pick<RuntimeStatusEvent, "phase" | "runtimeType" | "source" | "detail"> | null
): boolean {
return (
left === right ||
(left !== null &&
right !== null &&
left.phase === right.phase &&
left.runtimeType === right.runtimeType &&
(left.source ?? null) === (right.source ?? null) &&
(left.detail ?? null) === (right.detail ?? null))
);
}
export function isTerminalRuntimeStatusPhase(phase: RuntimeStatusEvent["phase"]): boolean {
return phase === "ready" || phase === "error";
}