@justjoshn/pi-google-oauth-v2 0.1.1 → 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/README.md +46 -19
- package/package.json +5 -2
- package/src/auth.ts +60 -45
- package/src/code-assist.ts +41 -23
- package/src/index.ts +33 -13
- package/src/loopback.ts +60 -36
- package/src/models.ts +14 -12
- package/src/request.ts +139 -83
- package/src/stream-reducer.ts +165 -0
- package/src/stream.ts +154 -231
package/src/stream.ts
CHANGED
|
@@ -1,50 +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';
|
|
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({});
|
|
16
20
|
|
|
17
|
-
interface CodeAssistChunk {
|
|
18
|
-
response?: {
|
|
19
|
-
candidates?: { content?: { parts?: ResponsePart[] }; finishReason?: string; finishMessage?: string }[];
|
|
20
|
-
usageMetadata?: { promptTokenCount?: number; cachedContentTokenCount?: number; candidatesTokenCount?: number; thoughtsTokenCount?: number; totalTokenCount?: number };
|
|
21
|
-
modelVersion?: string;
|
|
22
|
-
promptFeedback?: { blockReason?: string; blockReasonMessage?: string };
|
|
23
|
-
};
|
|
24
|
-
traceId?: string;
|
|
25
|
-
error?: { message?: string };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
interface ResponsePart {
|
|
29
|
-
text?: string;
|
|
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]);
|
|
36
|
-
|
|
37
|
-
// Google returns 429 RESOURCE_EXHAUSTED for a short-term capacity limit, so retry with the server's delay.
|
|
38
21
|
function retryDelayMs(body: string, headers: Headers): number | undefined {
|
|
39
22
|
const retryAfter = headers.get('retry-after');
|
|
40
23
|
if (retryAfter) {
|
|
41
24
|
const seconds = Number(retryAfter);
|
|
42
|
-
if (Number.isFinite(seconds)) return seconds *
|
|
25
|
+
if (Number.isFinite(seconds)) return seconds * MS_PER_SECOND;
|
|
43
26
|
const when = Date.parse(retryAfter);
|
|
44
27
|
if (!Number.isNaN(when)) return Math.max(0, when - Date.now());
|
|
45
28
|
}
|
|
46
29
|
const hinted = body.match(/"retryDelay":\s*"([0-9.]+)s"/i) ?? body.match(/retry in ([0-9.]+)\s*s/i);
|
|
47
|
-
return hinted?.[1] ? Number(hinted[1]) *
|
|
30
|
+
return hinted?.[1] ? Number(hinted[1]) * MS_PER_SECOND : undefined;
|
|
48
31
|
}
|
|
49
32
|
|
|
50
33
|
function sleep(ms: number, signal: AbortSignal | undefined): Promise<void> {
|
|
@@ -62,88 +45,95 @@ function sleep(ms: number, signal: AbortSignal | undefined): Promise<void> {
|
|
|
62
45
|
});
|
|
63
46
|
}
|
|
64
47
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
api: model.api,
|
|
75
|
-
provider: model.provider,
|
|
76
|
-
model: model.id,
|
|
77
|
-
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
|
|
78
|
-
stopReason: 'pending',
|
|
79
|
-
timestamp: Date.now(),
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
(async () => {
|
|
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 capacityThrottle = response.status === 429 && !quotaExhausted;
|
|
109
|
-
if (!RETRYABLE_STATUS.has(response.status) || quotaExhausted || attempt >= baseRetries) {
|
|
110
|
-
// Google says "check quota" for a shared-capacity throttle too, which misreads as the user's own quota.
|
|
111
|
-
if (capacityThrottle) throw new Error('Google Code Assist is temporarily at capacity (429). This is shared capacity, not your plan quota. Try again in a moment.');
|
|
112
|
-
throw new Error(`Google Code Assist error (${response.status}): ${message}`);
|
|
113
|
-
}
|
|
114
|
-
const delay = Math.min(retryDelayMs(text, response.headers) ?? 1000 * 2 ** attempt, maxRetryDelayMs);
|
|
115
|
-
await sleep(delay, options.signal);
|
|
116
|
-
}
|
|
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
|
+
}
|
|
117
74
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
} finally {
|
|
132
|
-
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}`);
|
|
133
88
|
}
|
|
134
|
-
|
|
89
|
+
await sleep(serverDelay ?? MS_PER_SECOND * BACKOFF_BASE ** attempt, options.signal);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
135
92
|
|
|
136
|
-
|
|
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));
|
|
137
99
|
}
|
|
138
100
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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');
|
|
145
108
|
}
|
|
146
|
-
|
|
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 };
|
|
147
137
|
}
|
|
148
138
|
|
|
149
139
|
async function* parseSse(body: ReadableStream<Uint8Array> | null, signal: AbortSignal | undefined): AsyncGenerator<CodeAssistChunk> {
|
|
@@ -152,143 +142,76 @@ async function* parseSse(body: ReadableStream<Uint8Array> | null, signal: AbortS
|
|
|
152
142
|
const decoder = new TextDecoder();
|
|
153
143
|
const onAbort = () => void reader.cancel().catch(() => undefined);
|
|
154
144
|
signal?.addEventListener('abort', onAbort, { once: true });
|
|
155
|
-
let
|
|
156
|
-
let dataLines: string[] = [];
|
|
157
|
-
|
|
158
|
-
const flush = (): CodeAssistChunk | undefined => {
|
|
159
|
-
if (dataLines.length === 0) return undefined;
|
|
160
|
-
const data = dataLines.join('\n');
|
|
161
|
-
dataLines = [];
|
|
162
|
-
try {
|
|
163
|
-
return JSON.parse(data) as CodeAssistChunk;
|
|
164
|
-
} catch {
|
|
165
|
-
throw new Error('Google Code Assist sent a malformed event');
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
const readLine = (line: string): CodeAssistChunk | undefined => {
|
|
169
|
-
if (line === '') return flush();
|
|
170
|
-
if (line.startsWith('data:')) dataLines.push(line.slice(5).replace(/^ /, ''));
|
|
171
|
-
return undefined;
|
|
172
|
-
};
|
|
145
|
+
let state: SseState = { buffered: '', endedWithCR: false, dataLines: [] };
|
|
173
146
|
|
|
174
147
|
try {
|
|
175
148
|
for (;;) {
|
|
176
149
|
const { done, value } = await reader.read();
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
for (const line of lines) {
|
|
181
|
-
const chunk = readLine(line);
|
|
182
|
-
if (chunk) yield chunk;
|
|
183
|
-
}
|
|
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);
|
|
184
153
|
if (done) break;
|
|
185
154
|
}
|
|
186
|
-
|
|
187
|
-
if (last) yield last;
|
|
155
|
+
if (state.dataLines.length > 0) yield parseEvent(state.dataLines.join('\n'));
|
|
188
156
|
} finally {
|
|
189
157
|
signal?.removeEventListener('abort', onAbort);
|
|
190
158
|
reader.releaseLock();
|
|
191
159
|
}
|
|
192
160
|
}
|
|
193
161
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
this.output = output;
|
|
207
|
-
this.model = model;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
push(chunk: CodeAssistChunk): void {
|
|
211
|
-
if (chunk.error) throw new CodeAssistError(chunk.error.message ?? 'Google Code Assist returned an error');
|
|
212
|
-
if (chunk.traceId) this.output.responseId ||= chunk.traceId;
|
|
213
|
-
const response = chunk.response;
|
|
214
|
-
if (!response) return;
|
|
215
|
-
if (response.modelVersion) this.output.responseModel = response.modelVersion;
|
|
216
|
-
const feedback = response.promptFeedback;
|
|
217
|
-
if (feedback?.blockReason) throw new Error(withGoogleMessage(`Google blocked the prompt: ${feedback.blockReason}`, feedback.blockReasonMessage));
|
|
218
|
-
|
|
219
|
-
const candidate = response.candidates?.[0];
|
|
220
|
-
for (const part of candidate?.content?.parts ?? []) {
|
|
221
|
-
if (typeof part.text === 'string') this.appendText(part.text, part.thought === true, part.thoughtSignature);
|
|
222
|
-
else if (part.functionCall) this.pushToolCall(part.functionCall, part.thoughtSignature);
|
|
223
|
-
}
|
|
224
|
-
if (candidate?.finishReason) this.finishReason = candidate.finishReason;
|
|
225
|
-
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
|
+
}
|
|
226
174
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const thoughts = usage.thoughtsTokenCount ?? 0;
|
|
231
|
-
this.output.usage = {
|
|
232
|
-
input: (usage.promptTokenCount ?? 0) - cached,
|
|
233
|
-
output: (usage.candidatesTokenCount ?? 0) + thoughts,
|
|
234
|
-
cacheRead: cached,
|
|
235
|
-
cacheWrite: 0,
|
|
236
|
-
reasoning: thoughts,
|
|
237
|
-
totalTokens: usage.totalTokenCount ?? 0,
|
|
238
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
239
|
-
};
|
|
240
|
-
calculateCost(this.model, this.output.usage);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
175
|
+
function doneEvent(output: AssistantMessage): AssistantMessageEvent {
|
|
176
|
+
return { type: 'done', reason: output.stopReason === 'toolUse' || output.stopReason === 'length' ? output.stopReason : 'stop', message: output };
|
|
177
|
+
}
|
|
243
178
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
this.output.rawStopReason = reason;
|
|
249
|
-
if (reason === 'MAX_TOKENS') this.output.stopReason = 'length';
|
|
250
|
-
else if (reason === 'STOP') this.output.stopReason = this.output.content.some((block) => block.type === 'toolCall') ? 'toolUse' : 'stop';
|
|
251
|
-
else throw new Error(withGoogleMessage(`Google stopped with ${reason}`, this.finishMessage));
|
|
252
|
-
}
|
|
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
|
+
}
|
|
253
183
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
const
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
} else if (block?.type === 'text') {
|
|
269
|
-
block.text += text;
|
|
270
|
-
if (signature) block.textSignature = signature;
|
|
271
|
-
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));
|
|
272
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));
|
|
273
205
|
}
|
|
206
|
+
}
|
|
274
207
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
private closeOpen(): void {
|
|
287
|
-
if (!this.open) return;
|
|
288
|
-
const contentIndex = this.output.content.length - 1;
|
|
289
|
-
const block = this.output.content[contentIndex];
|
|
290
|
-
if (block?.type === 'thinking') this.stream.push({ type: 'thinking_end', contentIndex, content: block.thinking, partial: this.output });
|
|
291
|
-
else if (block?.type === 'text') this.stream.push({ type: 'text_end', contentIndex, content: block.text, partial: this.output });
|
|
292
|
-
this.open = undefined;
|
|
293
|
-
}
|
|
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;
|
|
294
217
|
}
|