Repository navigation
Expand file tree
/
Copy pathlive.ts
More file actions
255 lines (233 loc) · 9.41 KB
/
Copy pathlive.ts
File metadata and controls
255 lines (233 loc) · 9.41 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Live generation: read the `ai` function's event stream (see
// supabase/functions/ai/stream.ts) and report the model's text as it is
// written. The finished request resolves exactly as a plain one does.
import { FunctionsFetchError, FunctionsHttpError } from '@supabase/supabase-js';
import { parsePartialJson } from '@debatelab/core/ai/partial-json';
import { consumeSSEChunk, type ParsedSSEEvent } from './sse';
export type LivePhase = 'research' | 'writing' | 'checking';
export interface LiveGeneration {
/** The answer's JSON as far as the model has written it. Throttled. */
onDraft: (draft: Record<string, unknown>) => void;
/** A step that writes nothing yet started, such as web research. */
onPhase?: (phase: LivePhase) => void;
}
const DRAFT_INTERVAL_MS = 50;
/**
* After `result`, keep reading only this long. The function closes the body
* a moment later; waiting for that close avoids cancelling the fetch, and
* the cap stops a body that never closes from holding the reader open.
*/
export const LIVE_STREAM_DRAIN_MS = 2_000;
/**
* The function sends a keepalive every 15 s. Three missed in a row means the
* stream is gone even if the connection still looks open, as when a proxy
* holds the tail of a response.
*/
export const LIVE_STREAM_STALL_MS = 45_000;
/**
* A stalled stream is left open, not cancelled: closing it would make the
* function cancel the run whose stored result the caller is about to read.
* It is cancelled after this long, past any run's wall clock, or when the
* caller aborts.
*/
export const LIVE_STREAM_STALLED_OPEN_MS = 160_000;
/**
* Why a live stream ended without its answer: the body ended, the
* connection dropped, nothing arrived for LIVE_STREAM_STALL_MS, or the
* `result` event arrived but could not be read. The caller can still recover
* the stored result (run-recovery.ts) before it reports the failure.
*/
export type LiveStreamFailure = 'ended' | 'dropped' | 'stalled' | 'unreadable';
export class LiveStreamError extends Error {
readonly reason: LiveStreamFailure;
/** The read error behind a dropped stream. */
readonly cause?: unknown;
constructor(reason: LiveStreamFailure, cause?: unknown) {
super(reason === 'unreadable' ? 'The answer arrived but could not be read.' : 'The response ended before the answer was complete.');
this.name = 'LiveStreamError';
this.reason = reason;
this.cause = cause;
}
}
/** Reads one chunk, or 'stalled' when nothing arrives in time. */
async function readWithin(reader: ReadableStreamDefaultReader<Uint8Array>, ms: number): Promise<ReadableStreamReadResult<Uint8Array> | 'stalled'> {
let timer: ReturnType<typeof setTimeout> | undefined;
try {
return await Promise.race([
reader.read(),
new Promise<'stalled'>((resolve) => {
timer = setTimeout(() => resolve('stalled'), ms);
}),
]);
} finally {
if (timer) clearTimeout(timer);
}
}
function isPhase(value: unknown): value is LivePhase {
return value === 'research' || value === 'writing' || value === 'checking';
}
/**
* Reads until the body ends, the caller aborts, the read fails, or `capMs`
* elapses. True means the body ended, so the caller must not cancel it.
*/
async function drainUntilEnd(
reader: ReadableStreamDefaultReader<Uint8Array>,
signal: AbortSignal | undefined,
capMs: number,
): Promise<boolean> {
let timer: ReturnType<typeof setTimeout> | undefined;
const cap = new Promise<'cap'>((resolve) => {
timer = setTimeout(() => resolve('cap'), capMs);
});
try {
while (!signal?.aborted) {
const read = reader.read().then(
(chunk) => ({ kind: 'chunk' as const, chunk }),
(): { kind: 'error' } => ({ kind: 'error' }),
);
const winner = await Promise.race([
read,
cap.then((): { kind: 'cap' } => ({ kind: 'cap' })),
]);
if (winner.kind !== 'chunk') return false;
if (winner.chunk.done) return true;
}
return false;
} finally {
if (timer) clearTimeout(timer);
}
}
/**
* Reads a successful `ai` response. A JSON body (a server from before live
* generation, or a cached reply) is returned as is; an event stream is
* followed to its `result`, whose status decides success or failure.
* The answer is returned as soon as `result` arrives. Reading continues
* until the body ends so the fetch is not cancelled in the gap before the
* server closes it.
*/
export async function readLiveResponse<T>(
response: Response,
live: LiveGeneration,
signal?: AbortSignal,
): Promise<T | FunctionsHttpError | FunctionsFetchError> {
const contentType = response.headers.get('content-type') ?? '';
if (!contentType.includes('text/event-stream') || !response.body) return await response.json() as T;
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let text = '';
let shown = '';
let timer: ReturnType<typeof setTimeout> | null = null;
let result: { status: number; body: unknown } | null = null;
let unreadableResult = false;
const showDraft = () => {
if (timer) clearTimeout(timer);
timer = null;
if (text === shown) return;
shown = text;
const draft = parsePartialJson(text);
if (draft) live.onDraft(draft);
};
const scheduleDraft = () => {
if (!timer) timer = setTimeout(showDraft, DRAFT_INTERVAL_MS);
};
const handle = (events: ParsedSSEEvent[]) => {
for (const { event, data } of events) {
const payload = data && typeof data === 'object' ? data as Record<string, unknown> : {};
if (event === 'delta' && typeof payload.text === 'string') {
text += payload.text;
scheduleDraft();
} else if (event === 'reset') {
// A repair retry writes the answer again from the start.
text = '';
shown = '';
live.onDraft({});
} else if (event === 'phase' && isPhase(payload.phase)) {
// Text written before this phase lands first, so a throttled draft
// cannot arrive after it and look like writing again.
showDraft();
live.onPhase?.(payload.phase);
} else if (event === 'result') {
// A result that is not the {status, body} the function sends was
// damaged on the way; the stored copy may still be read.
if (typeof payload.status === 'number') result = { status: payload.status, body: payload.body };
else unreadableResult = true;
}
}
};
// A fetch wrapper (the Jam recorder wraps window.fetch) can keep a
// response streaming after its request is aborted, so stop reading here.
const stopReading = () => {
reader.cancel(signal?.reason).catch((): undefined => undefined);
};
signal?.addEventListener('abort', stopReading, { once: true });
// Cancel only while the body is still open. Cancelling after `result`,
// before the server closes, aborts the fetch and drops the body.
let streamEnded = false;
let draining = false;
let stalled = false;
const release = () => {
if (timer) clearTimeout(timer);
timer = null;
if (stalled) {
// Keep the connection, so the run keeps going and stores its result.
setTimeout(stopReading, LIVE_STREAM_STALLED_OPEN_MS);
return;
}
signal?.removeEventListener('abort', stopReading);
if (!streamEnded) reader.cancel().catch((): undefined => undefined);
};
let failure: LiveStreamError | null = null;
try {
while (!result && !failure) {
if (signal?.aborted) throw new DOMException('The request was canceled.', 'AbortError');
let chunk: Awaited<ReturnType<typeof readWithin>>;
try {
chunk = await readWithin(reader, LIVE_STREAM_STALL_MS);
} catch (error) {
if (signal?.aborted) throw new DOMException('The request was canceled.', 'AbortError');
// The connection dropped (ERR_CONNECTION_CLOSED, network change).
failure = new LiveStreamError('dropped', error);
break;
}
if (signal?.aborted) throw new DOMException('The request was canceled.', 'AbortError');
if (chunk === 'stalled') {
stalled = true;
failure = new LiveStreamError('stalled');
break;
}
if (chunk.done) {
streamEnded = true;
break;
}
const parsed = consumeSSEChunk(buffer, decoder.decode(chunk.value, { stream: true }));
buffer = parsed.buffer;
handle(parsed.events);
}
// A last event without its closing blank line still counts.
if (!result && !failure) handle(consumeSSEChunk('', `${buffer}${decoder.decode()}\n\n`).events);
// Hand the answer back before the tail, so a slow close cannot delay the UI.
if (result && !streamEnded) {
if (timer) clearTimeout(timer);
timer = null;
draining = true;
void drainUntilEnd(reader, signal, LIVE_STREAM_DRAIN_MS)
.then((ended) => {
streamEnded = ended;
})
.catch((): undefined => undefined)
.finally(release);
}
} finally {
if (!draining) release();
}
const final = result as { status: number; body: unknown } | null;
if (!final) return new FunctionsFetchError(failure ?? new LiveStreamError(unreadableResult ? 'unreadable' : 'ended'));
if (final.status >= 200 && final.status < 300) return final.body as T;
// The same error a plain request would have produced, so error mapping
// (limits, model access, provider failures) does not change.
return new FunctionsHttpError(new Response(JSON.stringify(final.body ?? {}), {
status: final.status,
headers: { 'Content-Type': 'application/json' },
}));
}