@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/src/stream.ts CHANGED
@@ -1,50 +1,33 @@
1
- import {
2
- type Api,
3
- type AssistantMessage,
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 * 1000;
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]) * 1000 : undefined;
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
- * Serves both stream and streamSimple. Never throws: every failure, including a missing token or
67
- * project, ends the stream with an `error` event.
68
- */
69
- export function streamCodeAssist(model: Model<Api>, context: TranscriptContext, options: SimpleStreamOptions = {}): AssistantMessageEventStream {
70
- const stream = createAssistantMessageEventStream();
71
- const output: AssistantMessage = {
72
- role: 'assistant',
73
- content: [],
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
- let body: unknown = buildCodeAssistRequest({ model, context, project, options });
88
- body = (await options.onPayload?.(body, model)) ?? body;
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
- const doFetch = options.fetch ?? globalThis.fetch;
91
- const url = `${model.baseUrl}:streamGenerateContent?alt=sse`;
92
- const init: RequestInit = {
93
- method: 'POST',
94
- headers: { Authorization: `Bearer ${options.apiKey}`, 'Content-Type': 'application/json', Accept: 'text/event-stream', ...headers, 'User-Agent': ANTIGRAVITY_USER_AGENT },
95
- body: JSON.stringify(body),
96
- ...(options.signal ? { signal: options.signal } : {}),
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
- stream.push({ type: 'start', partial: output });
119
- const reducer = new ResponseReducer(stream, output, model);
120
- for await (const chunk of parseSse(response.body, options.signal)) {
121
- await options.onProviderStreamEvent?.(chunk, model);
122
- reducer.push(chunk);
123
- }
124
- if (options.signal?.aborted) throw new Error('Request was aborted');
125
- reducer.finish();
126
- stream.push({ type: 'done', reason: output.stopReason === 'toolUse' || output.stopReason === 'length' ? output.stopReason : 'stop', message: output });
127
- } catch (error) {
128
- output.stopReason = options.signal?.aborted ? 'aborted' : 'error';
129
- output.errorMessage = error instanceof Error ? error.message : String(error);
130
- stream.push({ type: 'error', reason: output.stopReason, error: output });
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
- return stream;
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
- function takePrivateProjectHeader(headers: SimpleStreamOptions['headers']): { project: string | undefined; headers: Record<string, string> } {
140
- let project: string | undefined;
141
- const forwarded: Record<string, string> = {};
142
- for (const [name, value] of Object.entries(headers ?? {})) {
143
- if (name.toLowerCase() === PRIVATE_PROJECT_HEADER) project = value ?? undefined;
144
- else if (value !== null && name.toLowerCase() !== 'user-agent') forwarded[name] = value;
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
- return { project, headers: forwarded };
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 buffered = '';
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
- buffered += decoder.decode(value, { stream: !done });
178
- const lines = buffered.split(/\r\n|\n|\r/);
179
- buffered = done ? '' : (lines.pop() ?? '');
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
- const last = flush();
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
- const withGoogleMessage = (summary: string, message: string | undefined): string => (message ? `${summary}: ${message}` : summary);
195
-
196
- class ResponseReducer {
197
- private readonly stream: AssistantMessageEventStream;
198
- private readonly output: AssistantMessage;
199
- private readonly model: Model<Api>;
200
- private open: 'text' | 'thinking' | undefined;
201
- private finishReason: string | undefined;
202
- private finishMessage: string | undefined;
203
-
204
- constructor(stream: AssistantMessageEventStream, output: AssistantMessage, model: Model<Api>) {
205
- this.stream = stream;
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
- const usage = response.usageMetadata;
228
- if (usage) {
229
- const cached = usage.cachedContentTokenCount ?? 0;
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
- finish(): void {
245
- this.closeOpen();
246
- const reason = this.finishReason;
247
- if (!reason) throw new Error('Google stream ended without a finish reason');
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
- private appendText(text: string, thought: boolean, signature: string | undefined): void {
255
- const kind = thought ? 'thinking' : 'text';
256
- if (this.open !== kind) {
257
- this.closeOpen();
258
- this.output.content.push(thought ? { type: 'thinking', thinking: '' } : { type: 'text', text: '' });
259
- this.open = kind;
260
- this.stream.push({ type: thought ? 'thinking_start' : 'text_start', contentIndex: this.output.content.length - 1, partial: this.output });
261
- }
262
- const contentIndex = this.output.content.length - 1;
263
- const block = this.output.content[contentIndex];
264
- if (block?.type === 'thinking') {
265
- block.thinking += text;
266
- if (signature) block.thinkingSignature = signature;
267
- this.stream.push({ type: 'thinking_delta', contentIndex, delta: text, partial: this.output });
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
- private pushToolCall(call: NonNullable<ResponsePart['functionCall']>, signature: string | undefined): void {
276
- this.closeOpen();
277
- const duplicate = call.id !== undefined && this.output.content.some((block) => block.type === 'toolCall' && block.id === call.id);
278
- const toolCall: ToolCall = { type: 'toolCall', id: call.id && !duplicate ? call.id : `call_${uuidv7()}`, name: call.name ?? '', arguments: call.args ?? {}, ...(signature ? { thoughtSignature: signature } : {}) };
279
- this.output.content.push(toolCall);
280
- const contentIndex = this.output.content.length - 1;
281
- this.stream.push({ type: 'toolcall_start', contentIndex, partial: this.output });
282
- this.stream.push({ type: 'toolcall_delta', contentIndex, delta: JSON.stringify(toolCall.arguments), partial: this.output });
283
- this.stream.push({ type: 'toolcall_end', contentIndex, toolCall, partial: this.output });
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
  }