@justjoshn/pi-google-oauth-v2 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -1
- package/src/auth.ts +59 -45
- package/src/code-assist.ts +41 -23
- package/src/index.ts +16 -19
- package/src/loopback.ts +60 -36
- package/src/models.ts +14 -11
- package/src/request.ts +138 -91
- package/src/stream-reducer.ts +165 -0
- package/src/stream.ts +154 -238
package/src/stream.ts
CHANGED
|
@@ -1,49 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type AssistantMessageEventStream,
|
|
5
|
-
calculateCost,
|
|
6
|
-
createAssistantMessageEventStream,
|
|
7
|
-
type Model,
|
|
8
|
-
type SimpleStreamOptions,
|
|
9
|
-
type ToolCall,
|
|
10
|
-
type TranscriptContext,
|
|
11
|
-
uuidv7,
|
|
12
|
-
} from '@earendil-works/pi-ai';
|
|
13
|
-
import { CodeAssistError, googleErrorMessage } from './code-assist.ts';
|
|
1
|
+
import { type Api, type AssistantMessage, type AssistantMessageEvent, type AssistantMessageEventStream, createAssistantMessageEventStream, type Model, type SimpleStreamOptions, type TranscriptContext } from '@earendil-works/pi-ai';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { googleErrorMessage } from './code-assist.ts';
|
|
14
4
|
import { ANTIGRAVITY_USER_AGENT, PRIVATE_PROJECT_HEADER } from './constants.ts';
|
|
15
5
|
import { buildCodeAssistRequest } from './request.ts';
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
thought?: boolean;
|
|
31
|
-
thoughtSignature?: string;
|
|
32
|
-
functionCall?: { id?: string; name?: string; args?: ToolCall['arguments'] };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const RETRYABLE_STATUS = new Set([429, 500, 502, 503, 504]);
|
|
6
|
+
import { type CodeAssistChunk, closeOpenBlock, createReducerState, finishStream, type ReducerState, type ReducerStep, reduceChunk } from './stream-reducer.ts';
|
|
7
|
+
|
|
8
|
+
const HTTP_TOO_MANY_REQUESTS = 429;
|
|
9
|
+
const HTTP_INTERNAL_ERROR = 500;
|
|
10
|
+
const HTTP_BAD_GATEWAY = 502;
|
|
11
|
+
const HTTP_UNAVAILABLE = 503;
|
|
12
|
+
const HTTP_GATEWAY_TIMEOUT = 504;
|
|
13
|
+
const RETRYABLE_STATUS = new Set([HTTP_TOO_MANY_REQUESTS, HTTP_INTERNAL_ERROR, HTTP_BAD_GATEWAY, HTTP_UNAVAILABLE, HTTP_GATEWAY_TIMEOUT]);
|
|
14
|
+
const MS_PER_SECOND = 1000;
|
|
15
|
+
const DEFAULT_MAX_RETRY_DELAY_MS = 60_000;
|
|
16
|
+
const BACKOFF_BASE = 2;
|
|
17
|
+
const SSE_DATA_PREFIX = 'data:';
|
|
18
|
+
const SSE_LINE_BREAK = /\r\n|\n|\r/;
|
|
19
|
+
const SSE_EVENT = z.looseObject({});
|
|
36
20
|
|
|
37
21
|
function retryDelayMs(body: string, headers: Headers): number | undefined {
|
|
38
22
|
const retryAfter = headers.get('retry-after');
|
|
39
23
|
if (retryAfter) {
|
|
40
24
|
const seconds = Number(retryAfter);
|
|
41
|
-
if (Number.isFinite(seconds)) return seconds *
|
|
25
|
+
if (Number.isFinite(seconds)) return seconds * MS_PER_SECOND;
|
|
42
26
|
const when = Date.parse(retryAfter);
|
|
43
27
|
if (!Number.isNaN(when)) return Math.max(0, when - Date.now());
|
|
44
28
|
}
|
|
45
29
|
const hinted = body.match(/"retryDelay":\s*"([0-9.]+)s"/i) ?? body.match(/retry in ([0-9.]+)\s*s/i);
|
|
46
|
-
return hinted?.[1] ? Number(hinted[1]) *
|
|
30
|
+
return hinted?.[1] ? Number(hinted[1]) * MS_PER_SECOND : undefined;
|
|
47
31
|
}
|
|
48
32
|
|
|
49
33
|
function sleep(ms: number, signal: AbortSignal | undefined): Promise<void> {
|
|
@@ -61,91 +45,95 @@ function sleep(ms: number, signal: AbortSignal | undefined): Promise<void> {
|
|
|
61
45
|
});
|
|
62
46
|
}
|
|
63
47
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
api: model.api,
|
|
74
|
-
provider: model.provider,
|
|
75
|
-
model: model.id,
|
|
76
|
-
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
|
|
77
|
-
stopReason: 'pending',
|
|
78
|
-
timestamp: Date.now(),
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
(async () => {
|
|
82
|
-
let reducer: ResponseReducer | undefined;
|
|
83
|
-
try {
|
|
84
|
-
const { project, headers } = takePrivateProjectHeader(options.headers);
|
|
85
|
-
if (!options.apiKey || !project) throw new Error('Google AI login is incomplete; run /login again');
|
|
48
|
+
function takePrivateProjectHeader(headers: SimpleStreamOptions['headers']): { project: string | undefined; headers: Record<string, string> } {
|
|
49
|
+
const entries = Object.entries(headers ?? {});
|
|
50
|
+
const project = entries
|
|
51
|
+
.filter(([name]) => name.toLowerCase() === PRIVATE_PROJECT_HEADER)
|
|
52
|
+
.map(([, value]) => value ?? undefined)
|
|
53
|
+
.at(-1);
|
|
54
|
+
const forwarded = entries.flatMap(([name, value]): [string, string][] => (name.toLowerCase() !== PRIVATE_PROJECT_HEADER && value !== null && name.toLowerCase() !== 'user-agent' ? [[name, value]] : []));
|
|
55
|
+
return { project, headers: Object.fromEntries(forwarded) };
|
|
56
|
+
}
|
|
86
57
|
|
|
87
|
-
|
|
88
|
-
|
|
58
|
+
function requestInit(options: SimpleStreamOptions, headers: Record<string, string>, body: unknown): RequestInit {
|
|
59
|
+
return {
|
|
60
|
+
method: 'POST',
|
|
61
|
+
headers: { Authorization: `Bearer ${options.apiKey}`, 'Content-Type': 'application/json', Accept: 'text/event-stream', ...headers, 'User-Agent': ANTIGRAVITY_USER_AGENT },
|
|
62
|
+
body: JSON.stringify(body),
|
|
63
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
89
66
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
};
|
|
98
|
-
const baseRetries = options.maxRetries ?? 0;
|
|
99
|
-
const maxRetryDelayMs = options.maxRetryDelayMs ?? 60_000;
|
|
100
|
-
let response: Response;
|
|
101
|
-
for (let attempt = 0; ; attempt++) {
|
|
102
|
-
response = await doFetch(url, init);
|
|
103
|
-
await options.onResponse?.({ status: response.status, headers: Object.fromEntries(response.headers) }, model);
|
|
104
|
-
if (response.ok) break;
|
|
105
|
-
const text = await response.text().catch(() => '');
|
|
106
|
-
const message = googleErrorMessage(text) ?? (response.statusText || 'request failed');
|
|
107
|
-
const quotaExhausted = /QUOTA_EXHAUSTED|Individual quota reached/i.test(text);
|
|
108
|
-
const failure = `Google Code Assist API error (${response.status}): ${message}${quotaExhausted ? ' (quota exceeded)' : ''}`;
|
|
109
|
-
if (!RETRYABLE_STATUS.has(response.status) || quotaExhausted || attempt >= baseRetries) {
|
|
110
|
-
throw new Error(failure);
|
|
111
|
-
}
|
|
112
|
-
const serverDelay = retryDelayMs(text, response.headers);
|
|
113
|
-
if (serverDelay !== undefined && maxRetryDelayMs > 0 && serverDelay > maxRetryDelayMs) {
|
|
114
|
-
throw new Error(`Server requested ${serverDelay}ms retry delay, exceeding configured cap ${maxRetryDelayMs}ms. ${failure}`);
|
|
115
|
-
}
|
|
116
|
-
await sleep(serverDelay ?? 1000 * 2 ** attempt, options.signal);
|
|
117
|
-
}
|
|
67
|
+
async function describeFailure(response: Response): Promise<{ text: string; failure: string; quotaExhausted: boolean }> {
|
|
68
|
+
const text = await response.text().catch(() => '');
|
|
69
|
+
const message = googleErrorMessage(text) ?? (response.statusText || 'request failed');
|
|
70
|
+
const quotaExhausted = /QUOTA_EXHAUSTED|Individual quota reached/i.test(text);
|
|
71
|
+
const failure = `Google Code Assist API error (${response.status}): ${message}${quotaExhausted ? ' (quota exceeded)' : ''}`;
|
|
72
|
+
return { text, failure, quotaExhausted };
|
|
73
|
+
}
|
|
118
74
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
stream.push({ type: 'error', reason: output.stopReason, error: output });
|
|
133
|
-
} finally {
|
|
134
|
-
stream.end();
|
|
75
|
+
async function fetchWithRetry(model: Model<Api>, options: SimpleStreamOptions, url: string, init: RequestInit): Promise<Response> {
|
|
76
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
77
|
+
const baseRetries = options.maxRetries ?? 0;
|
|
78
|
+
const maxRetryDelayMs = options.maxRetryDelayMs ?? DEFAULT_MAX_RETRY_DELAY_MS;
|
|
79
|
+
for (let attempt = 0; ; attempt++) {
|
|
80
|
+
const response = await doFetch(url, init);
|
|
81
|
+
await options.onResponse?.({ status: response.status, headers: Object.fromEntries(response.headers) }, model);
|
|
82
|
+
if (response.ok) return response;
|
|
83
|
+
const { text, failure, quotaExhausted } = await describeFailure(response);
|
|
84
|
+
if (!RETRYABLE_STATUS.has(response.status) || quotaExhausted || attempt >= baseRetries) throw new Error(failure);
|
|
85
|
+
const serverDelay = retryDelayMs(text, response.headers);
|
|
86
|
+
if (serverDelay !== undefined && maxRetryDelayMs > 0 && serverDelay > maxRetryDelayMs) {
|
|
87
|
+
throw new Error(`Server requested ${serverDelay}ms retry delay, exceeding configured cap ${maxRetryDelayMs}ms. ${failure}`);
|
|
135
88
|
}
|
|
136
|
-
|
|
89
|
+
await sleep(serverDelay ?? MS_PER_SECOND * BACKOFF_BASE ** attempt, options.signal);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
137
92
|
|
|
138
|
-
|
|
93
|
+
async function openResponse(model: Model<Api>, context: TranscriptContext, options: SimpleStreamOptions): Promise<Response> {
|
|
94
|
+
const { project, headers } = takePrivateProjectHeader(options.headers);
|
|
95
|
+
if (!options.apiKey || !project) throw new Error('Google AI login is incomplete; run /login again');
|
|
96
|
+
const built: unknown = buildCodeAssistRequest({ model, context, project, options });
|
|
97
|
+
const body = (await options.onPayload?.(built, model)) ?? built;
|
|
98
|
+
return fetchWithRetry(model, options, `${model.baseUrl}:streamGenerateContent?alt=sse`, requestInit(options, headers, body));
|
|
139
99
|
}
|
|
140
100
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
101
|
+
const toChunk = (value: unknown): CodeAssistChunk => value as CodeAssistChunk;
|
|
102
|
+
|
|
103
|
+
function parseEvent(data: string): CodeAssistChunk {
|
|
104
|
+
try {
|
|
105
|
+
return toChunk(SSE_EVENT.parse(JSON.parse(data)));
|
|
106
|
+
} catch {
|
|
107
|
+
throw new Error('Google Code Assist sent a malformed event');
|
|
147
108
|
}
|
|
148
|
-
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
interface SseState {
|
|
112
|
+
readonly buffered: string;
|
|
113
|
+
readonly endedWithCR: boolean;
|
|
114
|
+
readonly dataLines: readonly string[];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface SseLines {
|
|
118
|
+
readonly dataLines: readonly string[];
|
|
119
|
+
readonly events: readonly string[];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const readSseLine = (acc: SseLines, line: string): SseLines => {
|
|
123
|
+
if (line === '') return acc.dataLines.length > 0 ? { dataLines: [], events: [...acc.events, acc.dataLines.join('\n')] } : acc;
|
|
124
|
+
if (line.startsWith(SSE_DATA_PREFIX)) return { ...acc, dataLines: [...acc.dataLines, line.slice(SSE_DATA_PREFIX.length).replace(/^ /, '')] };
|
|
125
|
+
return acc;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** Splits decoded text into complete SSE lines. The tail stays buffered until its line break arrives. */
|
|
129
|
+
function feedSse(state: SseState, decoded: string, done: boolean): { state: SseState; events: readonly string[] } {
|
|
130
|
+
const received = decoded.length > 0;
|
|
131
|
+
const text = received ? state.buffered + (state.endedWithCR && decoded.startsWith('\n') ? decoded.slice(1) : decoded) : state.buffered;
|
|
132
|
+
const lines = text.split(SSE_LINE_BREAK);
|
|
133
|
+
const complete = done ? lines : lines.slice(0, -1);
|
|
134
|
+
const read = complete.reduce<SseLines>(readSseLine, { dataLines: state.dataLines, events: [] });
|
|
135
|
+
const next: SseState = { buffered: done ? '' : (lines.at(-1) ?? ''), endedWithCR: received ? decoded.endsWith('\r') : state.endedWithCR, dataLines: read.dataLines };
|
|
136
|
+
return { state: next, events: read.events };
|
|
149
137
|
}
|
|
150
138
|
|
|
151
139
|
async function* parseSse(body: ReadableStream<Uint8Array> | null, signal: AbortSignal | undefined): AsyncGenerator<CodeAssistChunk> {
|
|
@@ -154,148 +142,76 @@ async function* parseSse(body: ReadableStream<Uint8Array> | null, signal: AbortS
|
|
|
154
142
|
const decoder = new TextDecoder();
|
|
155
143
|
const onAbort = () => void reader.cancel().catch(() => undefined);
|
|
156
144
|
signal?.addEventListener('abort', onAbort, { once: true });
|
|
157
|
-
let
|
|
158
|
-
let previousChunkEndedCR = false;
|
|
159
|
-
let dataLines: string[] = [];
|
|
160
|
-
|
|
161
|
-
const flush = (): CodeAssistChunk | undefined => {
|
|
162
|
-
if (dataLines.length === 0) return undefined;
|
|
163
|
-
const data = dataLines.join('\n');
|
|
164
|
-
dataLines = [];
|
|
165
|
-
try {
|
|
166
|
-
return JSON.parse(data) as CodeAssistChunk;
|
|
167
|
-
} catch {
|
|
168
|
-
throw new Error('Google Code Assist sent a malformed event');
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
const readLine = (line: string): CodeAssistChunk | undefined => {
|
|
172
|
-
if (line === '') return flush();
|
|
173
|
-
if (line.startsWith('data:')) dataLines.push(line.slice(5).replace(/^ /, ''));
|
|
174
|
-
return undefined;
|
|
175
|
-
};
|
|
145
|
+
let state: SseState = { buffered: '', endedWithCR: false, dataLines: [] };
|
|
176
146
|
|
|
177
147
|
try {
|
|
178
148
|
for (;;) {
|
|
179
149
|
const { done, value } = await reader.read();
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
previousChunkEndedCR = decoded.endsWith('\r');
|
|
184
|
-
}
|
|
185
|
-
const lines = buffered.split(/\r\n|\n|\r/);
|
|
186
|
-
buffered = done ? '' : (lines.pop() ?? '');
|
|
187
|
-
for (const line of lines) {
|
|
188
|
-
const chunk = readLine(line);
|
|
189
|
-
if (chunk) yield chunk;
|
|
190
|
-
}
|
|
150
|
+
const fed = feedSse(state, decoder.decode(value, { stream: !done }), done);
|
|
151
|
+
state = fed.state;
|
|
152
|
+
for (const data of fed.events) yield parseEvent(data);
|
|
191
153
|
if (done) break;
|
|
192
154
|
}
|
|
193
|
-
|
|
194
|
-
if (last) yield last;
|
|
155
|
+
if (state.dataLines.length > 0) yield parseEvent(state.dataLines.join('\n'));
|
|
195
156
|
} finally {
|
|
196
157
|
signal?.removeEventListener('abort', onAbort);
|
|
197
158
|
reader.releaseLock();
|
|
198
159
|
}
|
|
199
160
|
}
|
|
200
161
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
this.output = output;
|
|
214
|
-
this.model = model;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
push(chunk: CodeAssistChunk): void {
|
|
218
|
-
if (chunk.error) throw new CodeAssistError(chunk.error.message ?? 'Google Code Assist returned an error');
|
|
219
|
-
if (chunk.traceId) this.output.responseId ||= chunk.traceId;
|
|
220
|
-
const response = chunk.response;
|
|
221
|
-
if (!response) return;
|
|
222
|
-
if (response.modelVersion) this.output.responseModel = response.modelVersion;
|
|
223
|
-
const feedback = response.promptFeedback;
|
|
224
|
-
if (feedback?.blockReason) throw new Error(withGoogleMessage(`Google blocked the prompt: ${feedback.blockReason}`, feedback.blockReasonMessage));
|
|
225
|
-
|
|
226
|
-
const candidate = response.candidates?.[0];
|
|
227
|
-
for (const part of candidate?.content?.parts ?? []) {
|
|
228
|
-
if (typeof part.text === 'string') this.appendText(part.text, part.thought === true, part.thoughtSignature);
|
|
229
|
-
else if (part.functionCall) this.pushToolCall(part.functionCall, part.thoughtSignature);
|
|
230
|
-
}
|
|
231
|
-
if (candidate?.finishReason) this.finishReason = candidate.finishReason;
|
|
232
|
-
if (candidate?.finishMessage) this.finishMessage = candidate.finishMessage;
|
|
162
|
+
function initialOutput(model: Model<Api>): AssistantMessage {
|
|
163
|
+
return {
|
|
164
|
+
role: 'assistant',
|
|
165
|
+
content: [],
|
|
166
|
+
api: model.api,
|
|
167
|
+
provider: model.provider,
|
|
168
|
+
model: model.id,
|
|
169
|
+
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
|
|
170
|
+
stopReason: 'pending',
|
|
171
|
+
timestamp: Date.now(),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
233
174
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const thoughts = usage.thoughtsTokenCount ?? 0;
|
|
238
|
-
this.output.usage = {
|
|
239
|
-
input: (usage.promptTokenCount ?? 0) - cached,
|
|
240
|
-
output: (usage.candidatesTokenCount ?? 0) + thoughts,
|
|
241
|
-
cacheRead: cached,
|
|
242
|
-
cacheWrite: 0,
|
|
243
|
-
reasoning: thoughts,
|
|
244
|
-
totalTokens: usage.totalTokenCount ?? 0,
|
|
245
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
246
|
-
};
|
|
247
|
-
calculateCost(this.model, this.output.usage);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
175
|
+
function doneEvent(output: AssistantMessage): AssistantMessageEvent {
|
|
176
|
+
return { type: 'done', reason: output.stopReason === 'toolUse' || output.stopReason === 'length' ? output.stopReason : 'stop', message: output };
|
|
177
|
+
}
|
|
250
178
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
this.output.rawStopReason = reason;
|
|
256
|
-
if (reason === 'MAX_TOKENS') this.output.stopReason = 'length';
|
|
257
|
-
else if (reason === 'STOP') this.output.stopReason = this.output.content.some((block) => block.type === 'toolCall') ? 'toolUse' : 'stop';
|
|
258
|
-
else throw new Error(withGoogleMessage(`Google stopped with ${reason}`, this.finishMessage));
|
|
259
|
-
}
|
|
179
|
+
function errorEvent(output: AssistantMessage, error: unknown, signal: AbortSignal | undefined): AssistantMessageEvent {
|
|
180
|
+
const failed: AssistantMessage = { ...output, stopReason: signal?.aborted ? 'aborted' : 'error', errorMessage: error instanceof Error ? error.message : String(error) };
|
|
181
|
+
return { type: 'error', reason: failed.stopReason === 'aborted' ? 'aborted' : 'error', error: failed };
|
|
182
|
+
}
|
|
260
183
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
} else if (block?.type === 'text') {
|
|
276
|
-
block.text += text;
|
|
277
|
-
if (signature) block.textSignature = signature;
|
|
278
|
-
this.stream.push({ type: 'text_delta', contentIndex, delta: text, partial: this.output });
|
|
184
|
+
/** Runs the whole exchange and reports every outcome, including failures, through `emit`. It never rejects. */
|
|
185
|
+
async function runStream(model: Model<Api>, context: TranscriptContext, options: SimpleStreamOptions, emit: (event: AssistantMessageEvent) => void): Promise<void> {
|
|
186
|
+
let current: ReducerState = createReducerState(initialOutput(model));
|
|
187
|
+
const apply = (step: ReducerStep): void => {
|
|
188
|
+
current = step.state;
|
|
189
|
+
for (const event of step.events) emit(event);
|
|
190
|
+
if (step.error) throw step.error;
|
|
191
|
+
};
|
|
192
|
+
try {
|
|
193
|
+
const response = await openResponse(model, context, options);
|
|
194
|
+
emit({ type: 'start', partial: current.output });
|
|
195
|
+
for await (const chunk of parseSse(response.body, options.signal)) {
|
|
196
|
+
await options.onProviderStreamEvent?.(chunk, model);
|
|
197
|
+
apply(reduceChunk(current, chunk, model));
|
|
279
198
|
}
|
|
199
|
+
if (options.signal?.aborted) throw new Error('Request was aborted');
|
|
200
|
+
apply(finishStream(current));
|
|
201
|
+
emit(doneEvent(current.output));
|
|
202
|
+
} catch (error) {
|
|
203
|
+
apply(closeOpenBlock(current));
|
|
204
|
+
emit(errorEvent(current.output, error, options.signal));
|
|
280
205
|
}
|
|
206
|
+
}
|
|
281
207
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
closeOpen(): void {
|
|
294
|
-
if (!this.open) return;
|
|
295
|
-
const contentIndex = this.output.content.length - 1;
|
|
296
|
-
const block = this.output.content[contentIndex];
|
|
297
|
-
if (block?.type === 'thinking') this.stream.push({ type: 'thinking_end', contentIndex, content: block.thinking, partial: this.output });
|
|
298
|
-
else if (block?.type === 'text') this.stream.push({ type: 'text_end', contentIndex, content: block.text, partial: this.output });
|
|
299
|
-
this.open = undefined;
|
|
300
|
-
}
|
|
208
|
+
/**
|
|
209
|
+
* Serves both stream and streamSimple. Never throws: every failure, including a missing token or
|
|
210
|
+
* project, ends the stream with an `error` event.
|
|
211
|
+
*/
|
|
212
|
+
export function streamCodeAssist(model: Model<Api>, context: TranscriptContext, options: SimpleStreamOptions = {}): AssistantMessageEventStream {
|
|
213
|
+
const stream = createAssistantMessageEventStream();
|
|
214
|
+
const emit = (event: AssistantMessageEvent): void => stream.push(event);
|
|
215
|
+
void runStream(model, context, options, emit).finally(() => stream.end());
|
|
216
|
+
return stream;
|
|
301
217
|
}
|