@justjoshn/pi-google-oauth-v2 0.1.2 → 0.1.4

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/request.ts CHANGED
@@ -1,6 +1,20 @@
1
1
  import { randomUUID } from 'node:crypto';
2
2
 
3
- import { type Api, clampThinkingLevel, getCurrentSystemPrompt, getCurrentTools, type Model, type ModelThinkingLevel, type SimpleStreamOptions, type ToolCall, type TranscriptContext, toToolDeclaration } from '@earendil-works/pi-ai';
3
+ import {
4
+ type Api,
5
+ type AssistantMessage,
6
+ clampThinkingLevel,
7
+ getCurrentSystemPrompt,
8
+ getCurrentTools,
9
+ type Model,
10
+ type ModelThinkingLevel,
11
+ type SimpleStreamOptions,
12
+ type ToolCall,
13
+ type ToolResultMessage,
14
+ type TranscriptContext,
15
+ toToolDeclaration,
16
+ type UserMessage,
17
+ } from '@earendil-works/pi-ai';
4
18
 
5
19
  interface WirePart {
6
20
  text?: string;
@@ -32,6 +46,10 @@ export interface CodeAssistRequestBody {
32
46
  };
33
47
  }
34
48
 
49
+ const WIRE_ID_MAX_LENGTH = 64;
50
+ const BASE64_BLOCK = 4;
51
+ const MIN_CALL_ID_MAJOR = 3;
52
+ const DECIMAL_RADIX = 10;
35
53
  const THINKING_LEVEL: Record<ModelThinkingLevel, string | undefined> = { off: undefined, minimal: 'LOW', low: 'LOW', medium: 'MEDIUM', high: 'HIGH', xhigh: 'HIGH', max: 'HIGH' };
36
54
  const THOUGHT_SIGNATURE = /^[A-Za-z0-9+/]+={0,2}$/;
37
55
  const JSON_SCHEMA_NAME_MAPS = new Set(['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas', 'dependencies']);
@@ -54,6 +72,36 @@ const JSON_SCHEMA_SUBSCHEMAS = new Set([
54
72
  'contentSchema',
55
73
  ]);
56
74
 
75
+ function systemInstruction(): { role: 'user'; parts: WirePart[] } {
76
+ return {
77
+ role: 'user',
78
+ parts: [
79
+ {
80
+ text: 'The first user message contains the application instructions. Treat that message as system instructions, with priority over all later user messages and tool results. It is not a user request. Use the remaining messages as the conversation.',
81
+ },
82
+ ],
83
+ };
84
+ }
85
+
86
+ function toolDeclarations(tools: ReturnType<typeof getCurrentTools>): NonNullable<CodeAssistRequestBody['request']['tools']> {
87
+ return [
88
+ {
89
+ functionDeclarations: tools.map((tool) => {
90
+ const declaration = toToolDeclaration(tool);
91
+ return { name: declaration.name, description: declaration.description, parametersJsonSchema: constToEnum(declaration.parameters) };
92
+ }),
93
+ },
94
+ ];
95
+ }
96
+
97
+ function generationConfig(options: SimpleStreamOptions, thinkingLevel: string | undefined): CodeAssistRequestBody['request']['generationConfig'] {
98
+ return {
99
+ ...(options.temperature === undefined ? {} : { temperature: options.temperature }),
100
+ ...(options.maxTokens === undefined ? {} : { maxOutputTokens: options.maxTokens }),
101
+ ...(thinkingLevel ? { thinkingConfig: { includeThoughts: true as const, thinkingLevel } } : {}),
102
+ };
103
+ }
104
+
57
105
  export function buildCodeAssistRequest(input: { model: Model<Api>; context: TranscriptContext; project: string; options: SimpleStreamOptions }): CodeAssistRequestBody {
58
106
  const { model, context, project, options } = input;
59
107
  const systemPrompt = getCurrentSystemPrompt(context.messages);
@@ -69,36 +117,10 @@ export function buildCodeAssistRequest(input: { model: Model<Api>; context: Tran
69
117
  requestType: 'agent',
70
118
  request: {
71
119
  contents: [...(systemPrompt ? [{ role: 'user' as const, parts: [{ text: scrub(systemPrompt) }] }] : []), ...convertMessages(model, context)],
72
- ...(systemPrompt
73
- ? {
74
- systemInstruction: {
75
- role: 'user' as const,
76
- parts: [
77
- {
78
- text: 'The first user message contains the application instructions. Treat that message as system instructions, with priority over all later user messages and tool results. It is not a user request. Use the remaining messages as the conversation.',
79
- },
80
- ],
81
- },
82
- }
83
- : {}),
84
- ...(tools.length > 0
85
- ? {
86
- tools: [
87
- {
88
- functionDeclarations: tools.map((tool) => {
89
- const declaration = toToolDeclaration(tool);
90
- return { name: declaration.name, description: declaration.description, parametersJsonSchema: constToEnum(declaration.parameters) };
91
- }),
92
- },
93
- ],
94
- }
95
- : {}),
120
+ ...(systemPrompt ? { systemInstruction: systemInstruction() } : {}),
121
+ ...(tools.length > 0 ? { tools: toolDeclarations(tools) } : {}),
96
122
  ...(tools.length > 0 && options.toolChoice === 'none' ? { toolConfig: { functionCallingConfig: { mode: 'NONE' as const } } } : {}),
97
- generationConfig: {
98
- ...(options.temperature === undefined ? {} : { temperature: options.temperature }),
99
- ...(options.maxTokens === undefined ? {} : { maxOutputTokens: options.maxTokens }),
100
- ...(thinkingLevel ? { thinkingConfig: { includeThoughts: true as const, thinkingLevel } } : {}),
101
- },
123
+ generationConfig: generationConfig(options, thinkingLevel),
102
124
  ...(options.sessionId === undefined ? {} : { sessionId: options.sessionId }),
103
125
  },
104
126
  };
@@ -110,73 +132,98 @@ function scrub(text: string): string {
110
132
 
111
133
  function geminiMajorVersion(modelId: string): number | undefined {
112
134
  const match = /^gemini-(\d+)/.exec(modelId.toLowerCase());
113
- return match?.[1] === undefined ? undefined : Number.parseInt(match[1], 10);
135
+ return match?.[1] === undefined ? undefined : Number.parseInt(match[1], DECIMAL_RADIX);
114
136
  }
115
137
 
116
- function convertMessages(model: Model<Api>, context: TranscriptContext): WireContent[] {
117
- const major = geminiMajorVersion(model.id);
118
- const needsCallIds = major !== undefined && major >= 3;
119
- // Code Assist matches a functionResponse to its functionCall by this id on Gemini 3+, so both sides must sanitize identically.
120
- const wireId = (id: string): { id?: string } => (needsCallIds ? { id: id.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64) } : {});
121
- const contents: WireContent[] = [];
122
- let pending: ToolCall[] = [];
123
- const answered = new Set<string>();
138
+ interface ConvertState {
139
+ contents: WireContent[];
140
+ pending: ToolCall[];
141
+ answered: ReadonlySet<string>;
124
142
  // Code Assist wants every functionResponse of a round in one user turn, so Gemini 2.x image turns wait until the round ends.
125
- const imageTurns: WireContent[] = [];
143
+ imageTurns: WireContent[];
144
+ }
126
145
 
127
- const pushToolResponse = (part: NonNullable<WirePart['functionResponse']>, trailingImages: WirePart[] = []): void => {
128
- const last = contents.at(-1);
129
- if (last?.role === 'user' && last.parts.some((candidate) => candidate.functionResponse)) last.parts.push({ functionResponse: part });
130
- else contents.push({ role: 'user', parts: [{ functionResponse: part }] });
131
- if (trailingImages.length > 0) imageTurns.push({ role: 'user', parts: [{ text: 'Tool result image:' }, ...trailingImages] });
132
- };
133
- // A functionCall without a functionResponse makes Code Assist reject the whole request.
134
- const answerOrphans = (): void => {
135
- for (const call of pending) {
136
- if (!answered.has(call.id)) pushToolResponse({ ...wireId(call.id), name: call.name, response: { error: 'No result provided' } });
137
- }
138
- pending = [];
139
- answered.clear();
140
- contents.push(...imageTurns.splice(0));
141
- };
146
+ interface ConvertEnv {
147
+ model: Model<Api>;
148
+ major: number | undefined;
149
+ // Code Assist matches a functionResponse to its functionCall by this id on Gemini 3+, so both sides must sanitize identically.
150
+ wireId: (id: string) => { id?: string };
151
+ }
152
+
153
+ function withToolResponse(state: ConvertState, part: NonNullable<WirePart['functionResponse']>, trailingImages: WirePart[] = []): ConvertState {
154
+ const last = state.contents.at(-1);
155
+ const contents: WireContent[] =
156
+ last?.role === 'user' && last.parts.some((candidate) => candidate.functionResponse)
157
+ ? [...state.contents.slice(0, -1), { ...last, parts: [...last.parts, { functionResponse: part }] }]
158
+ : [...state.contents, { role: 'user', parts: [{ functionResponse: part }] }];
159
+ const imageTurns: WireContent[] = trailingImages.length > 0 ? [...state.imageTurns, { role: 'user', parts: [{ text: 'Tool result image:' }, ...trailingImages] }] : state.imageTurns;
160
+ return { ...state, contents, imageTurns };
161
+ }
142
162
 
143
- for (const message of context.messages) {
144
- if (message.role === 'user') {
145
- answerOrphans();
146
- const parts: WirePart[] =
147
- typeof message.content === 'string' ? [{ text: scrub(message.content) }] : message.content.map((block) => (block.type === 'text' ? { text: scrub(block.text) } : { inlineData: { mimeType: block.mimeType, data: block.data } }));
148
- if (parts.length > 0) contents.push({ role: 'user', parts });
149
- } else if (message.role === 'assistant') {
150
- answerOrphans();
151
- if (message.stopReason === 'error' || message.stopReason === 'aborted') continue;
152
- const sameModel = message.provider === model.provider && message.api === model.api && message.model === model.id;
153
- const signed = (signature: string | undefined): { thoughtSignature?: string } => (sameModel && signature && signature.length % 4 === 0 && THOUGHT_SIGNATURE.test(signature) ? { thoughtSignature: signature } : {});
154
- const parts: WirePart[] = [];
155
- for (const block of message.content) {
156
- if (block.type === 'text') {
157
- const signature = signed(block.textSignature);
158
- if (block.text.trim() || signature.thoughtSignature) parts.push({ text: scrub(block.text), ...signature });
159
- } else if (block.type === 'thinking') {
160
- if (!sameModel) continue;
161
- const signature = signed(block.thinkingSignature);
162
- if (block.thinking.trim() || signature.thoughtSignature) parts.push({ thought: true, text: scrub(block.thinking), ...signature });
163
- } else {
164
- parts.push({ functionCall: { ...wireId(block.id), name: block.name, args: block.arguments }, ...signed(block.thoughtSignature) });
165
- }
166
- }
167
- pending = message.content.filter((block): block is ToolCall => block.type === 'toolCall');
168
- if (parts.length > 0) contents.push({ role: 'model', parts });
169
- } else if (message.role === 'toolResult') {
170
- answered.add(message.toolCallId);
171
- const text = message.content.flatMap((block) => (block.type === 'text' ? [block.text] : [])).join('\n');
172
- const images: WirePart[] = message.content.flatMap((block) => (block.type === 'image' && model.input.includes('image') ? [{ inlineData: { mimeType: block.mimeType, data: block.data } }] : []));
173
- const value = text.trim() ? scrub(text) : images.length > 0 ? '(see attached image)' : '';
174
- const nested = major === undefined || major >= 3;
175
- pushToolResponse({ ...wireId(message.toolCallId), name: message.toolName, response: message.isError ? { error: value } : { output: value }, ...(images.length > 0 && nested ? { parts: images } : {}) }, nested ? [] : images);
163
+ // A functionCall without a functionResponse makes Code Assist reject the whole request.
164
+ function answerOrphans(state: ConvertState, env: ConvertEnv): ConvertState {
165
+ const orphans = state.pending.filter((call) => !state.answered.has(call.id));
166
+ const answered = orphans.reduce((acc, call) => withToolResponse(acc, { ...env.wireId(call.id), name: call.name, response: { error: 'No result provided' } }), state);
167
+ return { contents: [...answered.contents, ...answered.imageTurns], pending: [], answered: new Set<string>(), imageTurns: [] };
168
+ }
169
+
170
+ function convertUser(state: ConvertState, message: UserMessage, env: ConvertEnv): ConvertState {
171
+ const settled = answerOrphans(state, env);
172
+ const parts: WirePart[] =
173
+ typeof message.content === 'string' ? [{ text: scrub(message.content) }] : message.content.map((block) => (block.type === 'text' ? { text: scrub(block.text) } : { inlineData: { mimeType: block.mimeType, data: block.data } }));
174
+ return parts.length > 0 ? { ...settled, contents: [...settled.contents, { role: 'user', parts }] } : settled;
175
+ }
176
+
177
+ function assistantParts(message: AssistantMessage, env: ConvertEnv): WirePart[] {
178
+ const { model } = env;
179
+ const sameModel = message.provider === model.provider && message.api === model.api && message.model === model.id;
180
+ const signed = (signature: string | undefined): { thoughtSignature?: string } => (sameModel && signature && signature.length % BASE64_BLOCK === 0 && THOUGHT_SIGNATURE.test(signature) ? { thoughtSignature: signature } : {});
181
+ return message.content.flatMap((block): WirePart[] => {
182
+ if (block.type === 'text') {
183
+ const signature = signed(block.textSignature);
184
+ return block.text.trim() || signature.thoughtSignature ? [{ text: scrub(block.text), ...signature }] : [];
176
185
  }
177
- }
178
- answerOrphans();
179
- return contents;
186
+ if (block.type === 'thinking') {
187
+ if (!sameModel) return [];
188
+ const signature = signed(block.thinkingSignature);
189
+ return block.thinking.trim() || signature.thoughtSignature ? [{ thought: true, text: scrub(block.thinking), ...signature }] : [];
190
+ }
191
+ return [{ functionCall: { ...env.wireId(block.id), name: block.name, args: block.arguments }, ...signed(block.thoughtSignature) }];
192
+ });
193
+ }
194
+
195
+ function convertAssistant(state: ConvertState, message: AssistantMessage, env: ConvertEnv): ConvertState {
196
+ const settled = answerOrphans(state, env);
197
+ if (message.stopReason === 'error' || message.stopReason === 'aborted') return settled;
198
+ const parts = assistantParts(message, env);
199
+ const pending = message.content.filter((block): block is ToolCall => block.type === 'toolCall');
200
+ return { ...settled, pending, contents: parts.length > 0 ? [...settled.contents, { role: 'model', parts }] : settled.contents };
201
+ }
202
+
203
+ function convertToolResult(state: ConvertState, message: ToolResultMessage, env: ConvertEnv): ConvertState {
204
+ const { model, major } = env;
205
+ const text = message.content.flatMap((block) => (block.type === 'text' ? [block.text] : [])).join('\n');
206
+ const images: WirePart[] = message.content.flatMap((block) => (block.type === 'image' && model.input.includes('image') ? [{ inlineData: { mimeType: block.mimeType, data: block.data } }] : []));
207
+ const value = text.trim() ? scrub(text) : images.length > 0 ? '(see attached image)' : '';
208
+ const nested = major === undefined || major >= MIN_CALL_ID_MAJOR;
209
+ const answered = new Set([...state.answered, message.toolCallId]);
210
+ const part = { ...env.wireId(message.toolCallId), name: message.toolName, response: message.isError ? { error: value } : { output: value }, ...(images.length > 0 && nested ? { parts: images } : {}) };
211
+ return withToolResponse({ ...state, answered }, part, nested ? [] : images);
212
+ }
213
+
214
+ function convertMessages(model: Model<Api>, context: TranscriptContext): WireContent[] {
215
+ const major = geminiMajorVersion(model.id);
216
+ const needsCallIds = major !== undefined && major >= MIN_CALL_ID_MAJOR;
217
+ const wireId = (id: string): { id?: string } => (needsCallIds ? { id: id.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, WIRE_ID_MAX_LENGTH) } : {});
218
+ const env: ConvertEnv = { model, major, wireId };
219
+ const initial: ConvertState = { contents: [], pending: [], answered: new Set<string>(), imageTurns: [] };
220
+ const final = context.messages.reduce<ConvertState>((state, message) => {
221
+ if (message.role === 'user') return convertUser(state, message, env);
222
+ if (message.role === 'assistant') return convertAssistant(state, message, env);
223
+ if (message.role === 'toolResult') return convertToolResult(state, message, env);
224
+ return state;
225
+ }, initial);
226
+ return answerOrphans(final, env).contents;
180
227
  }
181
228
 
182
229
  /** Gemini ignores `const` inside parametersJsonSchema but honors a single-value `enum`. */
@@ -0,0 +1,165 @@
1
+ import { type Api, type AssistantMessage, type AssistantMessageEvent, calculateCost, type Model, type ToolCall, uuidv7 } from '@earendil-works/pi-ai';
2
+ import { CodeAssistError } from './code-assist.ts';
3
+
4
+ export interface ResponsePart {
5
+ text?: string;
6
+ thought?: boolean;
7
+ thoughtSignature?: string;
8
+ functionCall?: { id?: string; name?: string; args?: ToolCall['arguments'] };
9
+ }
10
+
11
+ export interface CodeAssistChunk {
12
+ response?: {
13
+ candidates?: { content?: { parts?: ResponsePart[] }; finishReason?: string; finishMessage?: string }[];
14
+ usageMetadata?: { promptTokenCount?: number; cachedContentTokenCount?: number; candidatesTokenCount?: number; thoughtsTokenCount?: number; totalTokenCount?: number };
15
+ modelVersion?: string;
16
+ promptFeedback?: { blockReason?: string; blockReasonMessage?: string };
17
+ };
18
+ traceId?: string;
19
+ error?: { message?: string };
20
+ }
21
+
22
+ /** Everything the stream has learned so far. Each step returns a new state and never edits the old one. */
23
+ export interface ReducerState {
24
+ readonly output: AssistantMessage;
25
+ readonly open: 'text' | 'thinking' | undefined;
26
+ readonly finishReason: string | undefined;
27
+ readonly finishMessage: string | undefined;
28
+ }
29
+
30
+ /** `error` is raised by the caller after it has applied `state` and sent `events`. */
31
+ export interface ReducerStep {
32
+ readonly state: ReducerState;
33
+ readonly events: readonly AssistantMessageEvent[];
34
+ readonly error?: Error;
35
+ }
36
+
37
+ type Block = AssistantMessage['content'][number];
38
+ type ToolCallPart = NonNullable<ResponsePart['functionCall']>;
39
+
40
+ const withGoogleMessage = (summary: string, message: string | undefined): string => (message ? `${summary}: ${message}` : summary);
41
+
42
+ export function createReducerState(output: AssistantMessage): ReducerState {
43
+ return { output, open: undefined, finishReason: undefined, finishMessage: undefined };
44
+ }
45
+
46
+ const noEvents = (state: ReducerState): ReducerStep => ({ state, events: [] });
47
+
48
+ const chain = (first: ReducerStep, next: ReducerStep): ReducerStep => ({ state: next.state, events: [...first.events, ...next.events] });
49
+
50
+ const withOutput = (state: ReducerState, output: AssistantMessage): ReducerState => ({ ...state, output });
51
+
52
+ const withLastBlock = (output: AssistantMessage, block: Block): AssistantMessage => ({ ...output, content: [...output.content.slice(0, -1), block] });
53
+
54
+ export function closeOpenBlock(state: ReducerState): ReducerStep {
55
+ if (!state.open) return noEvents(state);
56
+ const contentIndex = state.output.content.length - 1;
57
+ const block = state.output.content[contentIndex];
58
+ const closed: ReducerState = { ...state, open: undefined };
59
+ if (block?.type === 'thinking') return { state: closed, events: [{ type: 'thinking_end', contentIndex, content: block.thinking, partial: state.output }] };
60
+ if (block?.type === 'text') return { state: closed, events: [{ type: 'text_end', contentIndex, content: block.text, partial: state.output }] };
61
+ return { state: closed, events: [] };
62
+ }
63
+
64
+ function openBlock(state: ReducerState, kind: 'text' | 'thinking'): ReducerStep {
65
+ const closed = closeOpenBlock(state);
66
+ const block: Block = kind === 'thinking' ? { type: 'thinking', thinking: '' } : { type: 'text', text: '' };
67
+ const output: AssistantMessage = { ...closed.state.output, content: [...closed.state.output.content, block] };
68
+ const event: AssistantMessageEvent = { type: kind === 'thinking' ? 'thinking_start' : 'text_start', contentIndex: output.content.length - 1, partial: output };
69
+ return { state: { ...closed.state, output, open: kind }, events: [...closed.events, event] };
70
+ }
71
+
72
+ function appendText(state: ReducerState, text: string, thought: boolean, signature: string | undefined): ReducerStep {
73
+ const kind = thought ? 'thinking' : 'text';
74
+ const opened = state.open === kind ? noEvents(state) : openBlock(state, kind);
75
+ const { output } = opened.state;
76
+ const contentIndex = output.content.length - 1;
77
+ const block = output.content[contentIndex];
78
+ if (block?.type === 'thinking') {
79
+ const partial = withLastBlock(output, { ...block, thinking: block.thinking + text, ...(signature ? { thinkingSignature: signature } : {}) });
80
+ return chain(opened, { state: withOutput(opened.state, partial), events: [{ type: 'thinking_delta', contentIndex, delta: text, partial }] });
81
+ }
82
+ if (block?.type === 'text') {
83
+ const partial = withLastBlock(output, { ...block, text: block.text + text, ...(signature ? { textSignature: signature } : {}) });
84
+ return chain(opened, { state: withOutput(opened.state, partial), events: [{ type: 'text_delta', contentIndex, delta: text, partial }] });
85
+ }
86
+ return opened;
87
+ }
88
+
89
+ function addToolCall(state: ReducerState, call: ToolCallPart, signature: string | undefined): ReducerStep {
90
+ const closed = closeOpenBlock(state);
91
+ const duplicate = call.id !== undefined && closed.state.output.content.some((block) => block.type === 'toolCall' && block.id === call.id);
92
+ const toolCall: ToolCall = { type: 'toolCall', id: call.id && !duplicate ? call.id : `call_${uuidv7()}`, name: call.name ?? '', arguments: call.args ?? {}, ...(signature ? { thoughtSignature: signature } : {}) };
93
+ const output: AssistantMessage = { ...closed.state.output, content: [...closed.state.output.content, toolCall] };
94
+ const contentIndex = output.content.length - 1;
95
+ const events: AssistantMessageEvent[] = [
96
+ { type: 'toolcall_start', contentIndex, partial: output },
97
+ { type: 'toolcall_delta', contentIndex, delta: JSON.stringify(toolCall.arguments), partial: output },
98
+ { type: 'toolcall_end', contentIndex, toolCall, partial: output },
99
+ ];
100
+ return chain(closed, { state: withOutput(closed.state, output), events });
101
+ }
102
+
103
+ function applyPart(step: ReducerStep, part: ResponsePart): ReducerStep {
104
+ if (typeof part.text === 'string') return chain(step, appendText(step.state, part.text, part.thought === true, part.thoughtSignature));
105
+ if (part.functionCall) return chain(step, addToolCall(step.state, part.functionCall, part.thoughtSignature));
106
+ return step;
107
+ }
108
+
109
+ function usageFrom(model: Model<Api>, usage: NonNullable<NonNullable<CodeAssistChunk['response']>['usageMetadata']>): AssistantMessage['usage'] {
110
+ const cached = usage.cachedContentTokenCount ?? 0;
111
+ const thoughts = usage.thoughtsTokenCount ?? 0;
112
+ const counted = {
113
+ input: (usage.promptTokenCount ?? 0) - cached,
114
+ output: (usage.candidatesTokenCount ?? 0) + thoughts,
115
+ cacheRead: cached,
116
+ cacheWrite: 0,
117
+ reasoning: thoughts,
118
+ totalTokens: usage.totalTokenCount ?? 0,
119
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
120
+ };
121
+ return { ...counted, cost: calculateCost(model, counted) };
122
+ }
123
+
124
+ function withMetadata(state: ReducerState, chunk: CodeAssistChunk): ReducerState {
125
+ const responseId = chunk.traceId && !state.output.responseId ? { responseId: chunk.traceId } : {};
126
+ const responseModel = chunk.response?.modelVersion ? { responseModel: chunk.response.modelVersion } : {};
127
+ return withOutput(state, { ...state.output, ...responseId, ...responseModel });
128
+ }
129
+
130
+ /** Applies one chunk. Throws only when the chunk itself is an error, before any state changes. */
131
+ export function reduceChunk(state: ReducerState, chunk: CodeAssistChunk, model: Model<Api>): ReducerStep {
132
+ if (chunk.error) throw new CodeAssistError(chunk.error.message ?? 'Google Code Assist returned an error');
133
+ const stamped = withMetadata(state, chunk);
134
+ const { response } = chunk;
135
+ if (!response) return noEvents(stamped);
136
+ const feedback = response.promptFeedback;
137
+ if (feedback?.blockReason) return { ...noEvents(stamped), error: new Error(withGoogleMessage(`Google blocked the prompt: ${feedback.blockReason}`, feedback.blockReasonMessage)) };
138
+
139
+ const candidate = response.candidates?.[0];
140
+ const parted = (candidate?.content?.parts ?? []).reduce<ReducerStep>(applyPart, noEvents(stamped));
141
+ const usage = response.usageMetadata;
142
+ const finished: ReducerState = {
143
+ ...parted.state,
144
+ finishReason: candidate?.finishReason ? candidate.finishReason : parted.state.finishReason,
145
+ finishMessage: candidate?.finishMessage ? candidate.finishMessage : parted.state.finishMessage,
146
+ };
147
+ return { state: usage ? withOutput(finished, { ...finished.output, usage: usageFrom(model, usage) }) : finished, events: parted.events };
148
+ }
149
+
150
+ function stopOutput(output: AssistantMessage, reason: string): AssistantMessage {
151
+ if (reason === 'MAX_TOKENS') return { ...output, stopReason: 'length' };
152
+ if (reason === 'STOP') return { ...output, stopReason: output.content.some((block) => block.type === 'toolCall') ? 'toolUse' : 'stop' };
153
+ return output;
154
+ }
155
+
156
+ /** Closes any open block and settles the stop reason. The step carries an error when Google did not stop cleanly. */
157
+ export function finishStream(state: ReducerState): ReducerStep {
158
+ const closed = closeOpenBlock(state);
159
+ const reason = closed.state.finishReason;
160
+ if (!reason) return { ...closed, error: new Error('Google stream ended without a finish reason') };
161
+ const output = stopOutput({ ...closed.state.output, rawStopReason: reason }, reason);
162
+ const settled: ReducerStep = { state: withOutput(closed.state, output), events: closed.events };
163
+ if (reason === 'MAX_TOKENS' || reason === 'STOP') return settled;
164
+ return { ...settled, error: new Error(withGoogleMessage(`Google stopped with ${reason}`, closed.state.finishMessage)) };
165
+ }