@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/models.ts CHANGED
@@ -40,24 +40,23 @@ function splitWireId(wireId: string): { base: string; effort: Effort } {
40
40
 
41
41
  function thinkingLevelMap(variants: Variant[], reasoning: boolean): ThinkingLevelMap {
42
42
  if (!reasoning) return { off: (variants.find((variant) => variant.effort === 'medium') ?? variants[0])?.wireId ?? null };
43
- const map: ThinkingLevelMap = {};
44
- for (const level of LEVELS_WITHOUT_WIRE_ID) map[level] = null;
45
- for (const variant of variants) map[variant.effort] = variant.wireId;
46
- return map;
43
+ const unmapped = Object.fromEntries(LEVELS_WITHOUT_WIRE_ID.map((level) => [level, null]));
44
+ const mapped = Object.fromEntries(variants.map((variant) => [variant.effort, variant.wireId]));
45
+ return { ...unmapped, ...mapped } as ThinkingLevelMap;
47
46
  }
48
47
 
49
48
  /** One Pi model per base family. Its thinking levels map to Google's per-effort wire ids. */
50
49
  export function catalogModels(catalog: Catalog): CodeAssistModel[] {
51
- const families = new Map<string, Variant[]>();
52
50
  const shown = new Set((catalog.agentModelSorts ?? []).flatMap((sort) => (sort.groups ?? []).flatMap((group) => group.modelIds ?? [])));
53
- for (const wireId of shown) {
51
+ const entries = [...shown].flatMap((wireId): { base: string; variant: Variant }[] => {
54
52
  const info = catalog.models?.[wireId];
55
- if (!info || wireId.endsWith('-disabled')) continue;
53
+ if (!info || wireId.endsWith('-disabled')) return [];
56
54
  const { base, effort } = splitWireId(wireId);
57
- families.set(base, [...(families.get(base) ?? []), { wireId, effort, info }]);
58
- }
55
+ return [{ base, variant: { wireId, effort, info } }];
56
+ });
57
+ const families = [...new Set(entries.map((entry) => entry.base))].map((id) => ({ id, variants: entries.filter((entry) => entry.base === id).map((entry) => entry.variant) }));
59
58
 
60
- return [...families].map(([id, variants]) => {
59
+ return families.map(({ id, variants }) => {
61
60
  const reasoning = variants.some((variant) => variant.info.supportsThinking);
62
61
  const first = variants[0]?.info;
63
62
  return {
@@ -76,6 +75,8 @@ export function catalogModels(catalog: Catalog): CodeAssistModel[] {
76
75
  });
77
76
  }
78
77
 
78
+ const BASELINE_CONTEXT_WINDOW = 1_048_576;
79
+ const BASELINE_MAX_OUTPUT_TOKENS = 65_536;
79
80
  const BASELINE_FAMILIES = [
80
81
  ['gemini-4-argon', 'Gemini 4 Argon'],
81
82
  ['gemini-3.8-flash', 'Gemini 3.8 Flash'],
@@ -87,10 +88,11 @@ const BASELINE_EFFORTS = [
87
88
  ] as const;
88
89
  const baselineWireIds = BASELINE_FAMILIES.flatMap(([id]) => BASELINE_EFFORTS.map(([effort]) => `${id}-${effort}`));
89
90
 
90
- // Shown until the first live refresh. A live model with the same id replaces its entry.
91
91
  export const BASELINE_MODELS: CodeAssistModel[] = catalogModels({
92
92
  models: Object.fromEntries(
93
- BASELINE_FAMILIES.flatMap(([id, name]) => BASELINE_EFFORTS.map(([effort, label]) => [`${id}-${effort}`, { displayName: `${name} (${label})`, supportsImages: true, supportsThinking: true, maxTokens: 1048576, maxOutputTokens: 65536 }])),
93
+ BASELINE_FAMILIES.flatMap(([id, name]) =>
94
+ BASELINE_EFFORTS.map(([effort, label]) => [`${id}-${effort}`, { displayName: `${name} (${label})`, supportsImages: true, supportsThinking: true, maxTokens: BASELINE_CONTEXT_WINDOW, maxOutputTokens: BASELINE_MAX_OUTPUT_TOKENS }]),
95
+ ),
94
96
  ),
95
97
  agentModelSorts: [{ groups: [{ modelIds: baselineWireIds }] }],
96
98
  });
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,9 +46,11 @@ 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
- // Code Assist answers 429 RESOURCE_EXHAUSTED to any system prompt saying it runs "inside pi"; the same prompt without the clause gets a 200.
37
- const PI_HARNESS_CLAUSE = / running inside pi(?:, a coding agent harness)?(?=[.,])/gi;
38
54
  const THOUGHT_SIGNATURE = /^[A-Za-z0-9+/]+={0,2}$/;
39
55
  const JSON_SCHEMA_NAME_MAPS = new Set(['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas', 'dependencies']);
40
56
  const JSON_SCHEMA_SUBSCHEMAS = new Set([
@@ -56,6 +72,36 @@ const JSON_SCHEMA_SUBSCHEMAS = new Set([
56
72
  'contentSchema',
57
73
  ]);
58
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
+
59
105
  export function buildCodeAssistRequest(input: { model: Model<Api>; context: TranscriptContext; project: string; options: SimpleStreamOptions }): CodeAssistRequestBody {
60
106
  const { model, context, project, options } = input;
61
107
  const systemPrompt = getCurrentSystemPrompt(context.messages);
@@ -70,26 +116,11 @@ export function buildCodeAssistRequest(input: { model: Model<Api>; context: Tran
70
116
  userAgent: 'antigravity',
71
117
  requestType: 'agent',
72
118
  request: {
73
- contents: convertMessages(model, context),
74
- ...(systemPrompt ? { systemInstruction: { role: 'user' as const, parts: [{ text: scrub(systemPrompt.replace(PI_HARNESS_CLAUSE, '')) }] } } : {}),
75
- ...(tools.length > 0
76
- ? {
77
- tools: [
78
- {
79
- functionDeclarations: tools.map((tool) => {
80
- const declaration = toToolDeclaration(tool);
81
- return { name: declaration.name, description: declaration.description, parametersJsonSchema: constToEnum(declaration.parameters) };
82
- }),
83
- },
84
- ],
85
- }
86
- : {}),
119
+ contents: [...(systemPrompt ? [{ role: 'user' as const, parts: [{ text: scrub(systemPrompt) }] }] : []), ...convertMessages(model, context)],
120
+ ...(systemPrompt ? { systemInstruction: systemInstruction() } : {}),
121
+ ...(tools.length > 0 ? { tools: toolDeclarations(tools) } : {}),
87
122
  ...(tools.length > 0 && options.toolChoice === 'none' ? { toolConfig: { functionCallingConfig: { mode: 'NONE' as const } } } : {}),
88
- generationConfig: {
89
- ...(options.temperature === undefined ? {} : { temperature: options.temperature }),
90
- ...(options.maxTokens === undefined ? {} : { maxOutputTokens: options.maxTokens }),
91
- ...(thinkingLevel ? { thinkingConfig: { includeThoughts: true as const, thinkingLevel } } : {}),
92
- },
123
+ generationConfig: generationConfig(options, thinkingLevel),
93
124
  ...(options.sessionId === undefined ? {} : { sessionId: options.sessionId }),
94
125
  },
95
126
  };
@@ -101,73 +132,98 @@ function scrub(text: string): string {
101
132
 
102
133
  function geminiMajorVersion(modelId: string): number | undefined {
103
134
  const match = /^gemini-(\d+)/.exec(modelId.toLowerCase());
104
- return match?.[1] === undefined ? undefined : Number.parseInt(match[1], 10);
135
+ return match?.[1] === undefined ? undefined : Number.parseInt(match[1], DECIMAL_RADIX);
105
136
  }
106
137
 
107
- function convertMessages(model: Model<Api>, context: TranscriptContext): WireContent[] {
108
- const major = geminiMajorVersion(model.id);
109
- const needsCallIds = major !== undefined && major >= 3;
110
- // Code Assist matches a functionResponse to its functionCall by this id on Gemini 3+, so both sides must sanitize identically.
111
- const wireId = (id: string): { id?: string } => (needsCallIds ? { id: id.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64) } : {});
112
- const contents: WireContent[] = [];
113
- let pending: ToolCall[] = [];
114
- const answered = new Set<string>();
138
+ interface ConvertState {
139
+ contents: WireContent[];
140
+ pending: ToolCall[];
141
+ answered: ReadonlySet<string>;
115
142
  // Code Assist wants every functionResponse of a round in one user turn, so Gemini 2.x image turns wait until the round ends.
116
- const imageTurns: WireContent[] = [];
143
+ imageTurns: WireContent[];
144
+ }
117
145
 
118
- const pushToolResponse = (part: NonNullable<WirePart['functionResponse']>, trailingImages: WirePart[] = []): void => {
119
- const last = contents.at(-1);
120
- if (last?.role === 'user' && last.parts.some((candidate) => candidate.functionResponse)) last.parts.push({ functionResponse: part });
121
- else contents.push({ role: 'user', parts: [{ functionResponse: part }] });
122
- if (trailingImages.length > 0) imageTurns.push({ role: 'user', parts: [{ text: 'Tool result image:' }, ...trailingImages] });
123
- };
124
- // A functionCall without a functionResponse makes Code Assist reject the whole request.
125
- const answerOrphans = (): void => {
126
- for (const call of pending) {
127
- if (!answered.has(call.id)) pushToolResponse({ ...wireId(call.id), name: call.name, response: { error: 'No result provided' } });
128
- }
129
- pending = [];
130
- answered.clear();
131
- contents.push(...imageTurns.splice(0));
132
- };
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
+ }
133
162
 
134
- for (const message of context.messages) {
135
- if (message.role === 'user') {
136
- answerOrphans();
137
- const parts: WirePart[] =
138
- 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 } }));
139
- if (parts.length > 0) contents.push({ role: 'user', parts });
140
- } else if (message.role === 'assistant') {
141
- answerOrphans();
142
- if (message.stopReason === 'error' || message.stopReason === 'aborted') continue;
143
- const sameModel = message.provider === model.provider && message.api === model.api && message.model === model.id;
144
- const signed = (signature: string | undefined): { thoughtSignature?: string } => (sameModel && signature && signature.length % 4 === 0 && THOUGHT_SIGNATURE.test(signature) ? { thoughtSignature: signature } : {});
145
- const parts: WirePart[] = [];
146
- for (const block of message.content) {
147
- if (block.type === 'text') {
148
- const signature = signed(block.textSignature);
149
- if (block.text.trim() || signature.thoughtSignature) parts.push({ text: scrub(block.text), ...signature });
150
- } else if (block.type === 'thinking') {
151
- if (!sameModel) continue;
152
- const signature = signed(block.thinkingSignature);
153
- if (block.thinking.trim() || signature.thoughtSignature) parts.push({ thought: true, text: scrub(block.thinking), ...signature });
154
- } else {
155
- parts.push({ functionCall: { ...wireId(block.id), name: block.name, args: block.arguments }, ...signed(block.thoughtSignature) });
156
- }
157
- }
158
- pending = message.content.filter((block): block is ToolCall => block.type === 'toolCall');
159
- if (parts.length > 0) contents.push({ role: 'model', parts });
160
- } else if (message.role === 'toolResult') {
161
- answered.add(message.toolCallId);
162
- const text = message.content.flatMap((block) => (block.type === 'text' ? [block.text] : [])).join('\n');
163
- const images: WirePart[] = message.content.flatMap((block) => (block.type === 'image' && model.input.includes('image') ? [{ inlineData: { mimeType: block.mimeType, data: block.data } }] : []));
164
- const value = text.trim() ? scrub(text) : images.length > 0 ? '(see attached image)' : '';
165
- const nested = major === undefined || major >= 3;
166
- 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 }] : [];
167
185
  }
168
- }
169
- answerOrphans();
170
- 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;
171
227
  }
172
228
 
173
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
+ }