-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmanager.ts
More file actions
276 lines (238 loc) · 8.66 KB
/
Copy pathmanager.ts
File metadata and controls
276 lines (238 loc) · 8.66 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { ApiClient } from "../apiClient/index.js";
import { ensureAsyncIterable, ensureReadableStream } from "../streams/asyncIterableStream.js";
import { AnyZodFetchOptions } from "../zodfetch.js";
import { taskContext } from "../task-context-api.js";
import { CreateStreamResponseLike, StreamInstance } from "./streamInstance.js";
import {
RealtimeStreamInstance,
RealtimeStreamOperationOptions,
RealtimeStreamsManager,
StreamWriteResult,
} from "./types.js";
export class StandardRealtimeStreamsManager implements RealtimeStreamsManager {
constructor(
private apiClient: ApiClient,
private baseUrl: string,
private debug: boolean = false
) {}
// Track active streams - using a Set allows multiple streams for the same key to coexist
private activeStreams = new Set<{
wait: () => Promise<StreamWriteResult>;
abortController: AbortController;
}>();
// Cache of in-flight / resolved `createStream` responses, keyed by
// `${runId}:${key}`. S2 v2 access tokens are scoped to the org basin
// (default 1-day TTL server-side) so reusing them across repeated
// `pipe()` calls for the same `(runId, key)` is safe, and avoids the
// per-call PUT that pushes `streamId` onto `TaskRun.realtimeStreams`,
// which under chat-agent-style hot-loop writers caused row-lock
// contention on the writer DB.
private createStreamCache = new Map<string, Promise<CreateStreamResponseLike>>();
reset(): void {
this.activeStreams.clear();
this.createStreamCache.clear();
}
private getCachedCreateStream(
runId: string,
key: string,
requestOptions: AnyZodFetchOptions | undefined
): Promise<CreateStreamResponseLike> {
const cacheKey = `${runId}:${key}`;
const cached = this.createStreamCache.get(cacheKey);
if (cached) {
return cached;
}
const promise = this.apiClient.createStream(runId, "self", key, requestOptions);
this.createStreamCache.set(cacheKey, promise);
// Evict on failure so the next call retries instead of returning a
// poisoned cache entry forever.
promise.catch((err) => {
if (this.createStreamCache.get(cacheKey) === promise) {
this.createStreamCache.delete(cacheKey);
}
});
return promise;
}
/**
* Reactive invalidation: a writer's `wait()` rejecting can mean the
* cached S2 credentials have gone stale (expired token, revoked
* access, basin retired), so evict the cached `createStream` response
* for `(runId, key)` and let the next `pipe()` re-PUT to mint fresh
* credentials. Compare by identity so a fresh promise installed by a
* concurrent caller isn't accidentally cleared.
*/
private evictCreateStreamIfStale(
runId: string,
key: string,
expected: Promise<CreateStreamResponseLike>
): void {
const cacheKey = `${runId}:${key}`;
if (this.createStreamCache.get(cacheKey) === expected) {
this.createStreamCache.delete(cacheKey);
}
}
public pipe<T>(
key: string,
source: AsyncIterable<T> | ReadableStream<T>,
options?: RealtimeStreamOperationOptions
): RealtimeStreamInstance<T> {
// Normalize ReadableStream to AsyncIterable
const readableStreamSource = ensureReadableStream(source);
const runId = getRunIdForOptions(options);
if (!runId) {
throw new Error(
"Could not determine the target run ID for the realtime stream. Please specify a target run ID using the `target` option."
);
}
// Create an AbortController for this stream
const abortController = new AbortController();
// Chain with user-provided signal if present
const combinedSignal = options?.signal
? AbortSignal.any?.([options.signal, abortController.signal]) ?? abortController.signal
: abortController.signal;
// Capture which cached promise this writer uses so reactive
// invalidation below evicts only if the cache still holds it (a
// concurrent caller may have already refreshed it).
const activeCreatePromise = this.getCachedCreateStream(
runId,
key,
options?.requestOptions
);
const streamInstance = new StreamInstance({
apiClient: this.apiClient,
baseUrl: this.baseUrl,
runId,
key,
source: readableStreamSource,
signal: combinedSignal,
requestOptions: options?.requestOptions,
target: options?.target,
debug: this.debug,
createStream: () => activeCreatePromise,
});
// Register this stream
const streamInfo = { wait: () => streamInstance.wait(), abortController };
this.activeStreams.add(streamInfo);
// Single internal chain that handles activeStreams cleanup AND
// reactive invalidation. On rejection we evict the cached
// `createStream` entry so the next pipe() for the same `(runId, key)`
// re-PUTs and recovers (e.g. when a cached S2 access token expired
// mid-process). Customer awaiters still observe the rejection via
// the returned `wait()`; this chain just keeps the cleanup path
// from surfacing as unhandled.
streamInstance.wait().then(
() => {
this.activeStreams.delete(streamInfo);
},
(err) => {
this.evictCreateStreamIfStale(runId, key, activeCreatePromise);
this.activeStreams.delete(streamInfo);
}
);
return {
wait: () => streamInstance.wait(),
stream: streamInstance.stream,
};
}
public async append<TPart extends BodyInit>(
key: string,
part: TPart,
options?: RealtimeStreamOperationOptions
): Promise<void> {
const runId = getRunIdForOptions(options);
if (!runId) {
throw new Error(
"Could not determine the target run ID for the realtime stream. Please specify a target run ID using the `target` option."
);
}
const result = await this.apiClient.appendToStream(
runId,
"self",
key,
part,
options?.requestOptions
);
if (!result.ok) {
throw new Error(`Failed to append to stream: ${result.message ?? "Unknown error"}`);
}
}
public hasActiveStreams(): boolean {
return this.activeStreams.size > 0;
}
// Waits for all the streams to finish
public async waitForAllStreams(timeout: number = 60_000): Promise<void> {
if (this.activeStreams.size === 0) {
return;
}
const promises = Array.from(this.activeStreams).map((stream) => stream.wait());
// Create a timeout promise that resolves to a special sentinel value
const TIMEOUT_SENTINEL = Symbol("timeout");
const timeoutPromise = new Promise<typeof TIMEOUT_SENTINEL>((resolve) =>
setTimeout(() => resolve(TIMEOUT_SENTINEL), timeout)
);
// Race between all streams completing/rejecting and the timeout
const result = await Promise.race([Promise.all(promises), timeoutPromise]);
// Check if we timed out
if (result === TIMEOUT_SENTINEL) {
// Timeout occurred - abort all active streams
const abortedCount = this.activeStreams.size;
for (const streamInfo of this.activeStreams) {
streamInfo.abortController.abort();
this.activeStreams.delete(streamInfo);
}
throw new Error(
`Timeout waiting for streams to finish after ${timeout}ms. Aborted ${abortedCount} active stream(s).`
);
}
// If we reach here, Promise.all completed (either all resolved or one rejected)
// Any rejection from Promise.all will have already propagated
}
}
function getRunIdForOptions(options?: RealtimeStreamOperationOptions): string | undefined {
if (options?.target) {
if (options.target === "parent") {
return taskContext.ctx?.run?.parentTaskRunId ?? taskContext.ctx?.run?.id;
}
if (options.target === "root") {
return taskContext.ctx?.run?.rootTaskRunId ?? taskContext.ctx?.run?.id;
}
if (options.target === "self") {
return taskContext.ctx?.run?.id;
}
return options.target;
}
return taskContext.ctx?.run?.id;
}
type ParsedStreamResponse =
| {
version: "v1";
}
| {
version: "v2";
accessToken: string;
basin: string;
flushIntervalMs?: number;
maxRetries?: number;
};
function parseCreateStreamResponse(
version: string,
headers: Record<string, string> | undefined
): ParsedStreamResponse {
if (version === "v1") {
return { version: "v1" };
}
const accessToken = headers?.["x-s2-access-token"];
const basin = headers?.["x-s2-basin"];
if (!accessToken || !basin) {
return { version: "v1" };
}
const flushIntervalMs = headers?.["x-s2-flush-interval-ms"];
const maxRetries = headers?.["x-s2-max-retries"];
return {
version: "v2",
accessToken,
basin,
flushIntervalMs: flushIntervalMs ? parseInt(flushIntervalMs) : undefined,
maxRetries: maxRetries ? parseInt(maxRetries) : undefined,
};
}