@ssweens/pi-vertex 1.1.12 → 1.3.0

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/models/maas.ts CHANGED
@@ -1,14 +1,58 @@
1
1
  /**
2
- * MaaS (Model-as-a-Service) open model definitions for Vertex AI
3
- * Source: https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-partner-models
4
- * Pricing: https://cloud.google.com/vertex-ai/generative-ai/pricing#partner-models
5
- * All prices per 1M tokens
2
+ * MaaS (Model-as-a-Service) open and partner model definitions for Vertex AI
3
+ * Source: https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/maas/use-open-models
4
+ * Pricing: https://cloud.google.com/model-garden/partner-models/pricing
5
+ * All prices per 1M tokens (<= 200K input tokens where pricing is tiered)
6
+ *
7
+ * Retired and removed: the open model endpoints Google shut down on 2026-10-21
8
+ * (DeepSeek V3.2/V3.1/R1/OCR, GLM 5/4.7, gpt-oss-20b, Kimi K2 Thinking,
9
+ * Llama 3.3 70B, MiniMax M2, Qwen3 235B/Coder/Next) and Grok 4.1 Fast
10
+ * (shut down 2026-08-20).
6
11
  */
7
12
 
8
13
  import type { VertexModelConfig } from "../types.js";
9
14
 
10
15
  export const MAAS_MODELS: VertexModelConfig[] = [
11
16
  // --- xAI Grok ---
17
+ // Grok 4.7: GA 2026-09-30. Same context and prices as Grok 4.6 (2x above 200K input).
18
+ {
19
+ id: "grok-4.7",
20
+ name: "Grok 4.7",
21
+ apiId: "grok-4.7",
22
+ publisher: "xai",
23
+ endpointType: "maas",
24
+ contextWindow: 524288,
25
+ maxTokens: 32000,
26
+ input: ["text", "image"],
27
+ reasoning: true,
28
+ tools: true,
29
+ cost: {
30
+ input: 2.00,
31
+ output: 6.00,
32
+ cacheRead: 0.50,
33
+ cacheWrite: 0,
34
+ },
35
+ region: "global",
36
+ },
37
+ {
38
+ id: "grok-4.6",
39
+ name: "Grok 4.6",
40
+ apiId: "grok-4.6",
41
+ publisher: "xai",
42
+ endpointType: "maas",
43
+ contextWindow: 524288,
44
+ maxTokens: 32000,
45
+ input: ["text", "image"],
46
+ reasoning: true,
47
+ tools: true,
48
+ cost: {
49
+ input: 2.00,
50
+ output: 6.00,
51
+ cacheRead: 0.50,
52
+ cacheWrite: 0,
53
+ },
54
+ region: "global",
55
+ },
12
56
  {
13
57
  id: "grok-4.3",
14
58
  name: "Grok 4.3",
@@ -47,24 +91,76 @@ export const MAAS_MODELS: VertexModelConfig[] = [
47
91
  },
48
92
  region: "global",
49
93
  },
94
+
95
+ // --- Meta Muse (Preview) ---
96
+ // Served only through the stateless OpenAI-compatible Responses API on the
97
+ // global endpoint (no chat completions, no `store: true`). Input and output
98
+ // share the 1,048,576-token context; maxTokens is a default that leaves room
99
+ // for input. Reasoning effort minimal..xhigh (Ember also max); `none` is
100
+ // rejected, so "off" sends no effort and the model uses its default.
50
101
  {
51
- id: "grok-4.1-fast-reasoning",
52
- name: "Grok 4.1 Fast Reasoning",
53
- apiId: "grok-4.1-fast-reasoning",
54
- publisher: "xai",
102
+ id: "muse-wonder-1.0",
103
+ name: "Muse Wonder 1.0",
104
+ apiId: "muse-wonder-1.0",
105
+ publisher: "meta",
55
106
  endpointType: "maas",
56
- contextWindow: 2000000,
57
- maxTokens: 32000,
107
+ maasApi: "responses",
108
+ contextWindow: 1048576,
109
+ maxTokens: 128000,
110
+ input: ["text", "image"],
111
+ reasoning: true,
112
+ tools: true,
113
+ cost: {
114
+ input: 4.00,
115
+ output: 20.00,
116
+ cacheRead: 0.20,
117
+ cacheWrite: 0,
118
+ },
119
+ region: "global",
120
+ thinkingLevelMap: { xhigh: "xhigh" },
121
+ },
122
+ {
123
+ id: "muse-spark-1.3",
124
+ name: "Muse Spark 1.3",
125
+ apiId: "muse-spark-1.3",
126
+ publisher: "meta",
127
+ endpointType: "maas",
128
+ maasApi: "responses",
129
+ contextWindow: 1048576,
130
+ maxTokens: 128000,
131
+ input: ["text", "image"],
132
+ reasoning: true,
133
+ tools: true,
134
+ cost: {
135
+ input: 1.25,
136
+ output: 4.25,
137
+ cacheRead: 0.15,
138
+ cacheWrite: 0,
139
+ },
140
+ region: "global",
141
+ thinkingLevelMap: { xhigh: "xhigh" },
142
+ },
143
+ {
144
+ id: "muse-ember-1.0",
145
+ name: "Muse Ember 1.0",
146
+ apiId: "muse-ember-1.0",
147
+ publisher: "meta",
148
+ endpointType: "maas",
149
+ maasApi: "responses",
150
+ contextWindow: 1048576,
151
+ maxTokens: 128000,
58
152
  input: ["text", "image"],
59
153
  reasoning: true,
60
154
  tools: true,
155
+ // Above 200K input every token bills at 3x ($0.30/$1.50/$0.012).
61
156
  cost: {
62
- input: 0.20,
157
+ input: 0.10,
63
158
  output: 0.50,
64
- cacheRead: 0.05,
159
+ cacheRead: 0.004,
65
160
  cacheWrite: 0,
66
161
  },
67
162
  region: "global",
163
+ thinkingLevelMap: { xhigh: "xhigh", max: "max" },
68
164
  },
69
165
 
70
166
  // --- Meta Llama ---
@@ -106,25 +202,6 @@ export const MAAS_MODELS: VertexModelConfig[] = [
106
202
  },
107
203
  region: "global",
108
204
  },
109
- {
110
- id: "llama-3.3-70b",
111
- name: "Llama 3.3 70B",
112
- apiId: "meta/llama-3.3-70b-instruct-maas",
113
- publisher: "meta",
114
- endpointType: "maas",
115
- contextWindow: 128000,
116
- maxTokens: 8192,
117
- input: ["text"],
118
- reasoning: false,
119
- tools: true,
120
- cost: {
121
- input: 0.72,
122
- output: 0.72,
123
- cacheRead: 0,
124
- cacheWrite: 0,
125
- },
126
- region: "global",
127
- },
128
205
 
129
206
  // --- Mistral AI ---
130
207
  {
@@ -205,81 +282,28 @@ export const MAAS_MODELS: VertexModelConfig[] = [
205
282
  },
206
283
 
207
284
  // --- DeepSeek ---
285
+ // DeepSeek-V4.1-Flash: global only. 1M context; output shares it, so maxTokens
286
+ // is a default. Thinks only when `reasoning_effort` is sent: minimal/low/medium/high/max
287
+ // (`xhigh` and `none` are rejected), so "off" sends nothing and does not think.
208
288
  {
209
- id: "deepseek-v3.2",
210
- name: "DeepSeek V3.2",
211
- apiId: "deepseek-ai/deepseek-v3.2-maas",
212
- publisher: "deepseek-ai",
213
- endpointType: "maas",
214
- contextWindow: 163840,
215
- maxTokens: 32000,
216
- input: ["text"],
217
- reasoning: true,
218
- tools: true,
219
- cost: {
220
- input: 0.56,
221
- output: 1.68,
222
- cacheRead: 0.056,
223
- cacheWrite: 0,
224
- },
225
- region: "global",
226
- },
227
- {
228
- id: "deepseek-v3.1",
229
- name: "DeepSeek V3.1",
230
- apiId: "deepseek-ai/deepseek-v3.1-maas",
231
- publisher: "deepseek-ai",
232
- endpointType: "maas",
233
- contextWindow: 163840,
234
- maxTokens: 32000,
235
- input: ["text"],
236
- reasoning: true,
237
- tools: true,
238
- cost: {
239
- input: 0.60,
240
- output: 1.70,
241
- cacheRead: 0.06,
242
- cacheWrite: 0,
243
- },
244
- region: "global",
245
- },
246
- {
247
- id: "deepseek-r1",
248
- name: "DeepSeek R1",
249
- apiId: "deepseek-ai/deepseek-r1-0528-maas",
289
+ id: "deepseek-v4.1-flash",
290
+ name: "DeepSeek V4.1 Flash",
291
+ apiId: "deepseek-ai/deepseek-v4.1-flash-maas",
250
292
  publisher: "deepseek-ai",
251
293
  endpointType: "maas",
252
- contextWindow: 163840,
253
- maxTokens: 32000,
254
- input: ["text"],
294
+ contextWindow: 1048576,
295
+ maxTokens: 65536,
296
+ input: ["text", "image"],
255
297
  reasoning: true,
256
298
  tools: true,
257
- cost: {
258
- input: 1.35,
259
- output: 5.40,
260
- cacheRead: 0,
261
- cacheWrite: 0,
262
- },
263
- region: "global",
264
- },
265
- {
266
- id: "deepseek-ocr",
267
- name: "DeepSeek OCR",
268
- apiId: "deepseek-ai/deepseek-ocr-maas",
269
- publisher: "deepseek-ai",
270
- endpointType: "maas",
271
- contextWindow: 163840,
272
- maxTokens: 32000,
273
- input: ["text", "image"],
274
- reasoning: false,
275
- tools: false,
276
299
  cost: {
277
300
  input: 0.30,
278
301
  output: 1.20,
279
- cacheRead: 0,
302
+ cacheRead: 0.03,
280
303
  cacheWrite: 0,
281
304
  },
282
305
  region: "global",
306
+ thinkingLevelMap: { max: "max" },
283
307
  },
284
308
 
285
309
  // --- OpenAI (gpt-oss) ---
@@ -302,145 +326,6 @@ export const MAAS_MODELS: VertexModelConfig[] = [
302
326
  },
303
327
  region: "global",
304
328
  },
305
- {
306
- id: "gpt-oss-20b",
307
- name: "GPT-OSS 20B",
308
- apiId: "openai/gpt-oss-20b-maas",
309
- publisher: "openai",
310
- endpointType: "maas",
311
- contextWindow: 131072,
312
- maxTokens: 32000,
313
- input: ["text"],
314
- reasoning: false,
315
- tools: true,
316
- cost: {
317
- input: 0.07,
318
- output: 0.25,
319
- cacheRead: 0.007,
320
- cacheWrite: 0,
321
- },
322
- region: "global",
323
- },
324
-
325
- // --- Qwen ---
326
- {
327
- id: "qwen3-235b",
328
- name: "Qwen 3 235B",
329
- apiId: "qwen/qwen3-235b-a22b-instruct-2507-maas",
330
- publisher: "qwen",
331
- endpointType: "maas",
332
- contextWindow: 262144,
333
- maxTokens: 32000,
334
- input: ["text"],
335
- reasoning: true,
336
- tools: true,
337
- cost: {
338
- input: 0.22,
339
- output: 0.88,
340
- cacheRead: 0,
341
- cacheWrite: 0,
342
- },
343
- region: "global",
344
- },
345
- {
346
- id: "qwen3-next-instruct",
347
- name: "Qwen 3 Next Instruct",
348
- apiId: "qwen/qwen3-next-instruct-80b-maas",
349
- publisher: "qwen",
350
- endpointType: "maas",
351
- contextWindow: 262144,
352
- maxTokens: 32000,
353
- input: ["text"],
354
- reasoning: true,
355
- tools: true,
356
- cost: {
357
- input: 0.15,
358
- output: 1.20,
359
- cacheRead: 0,
360
- cacheWrite: 0,
361
- },
362
- region: "global",
363
- },
364
- {
365
- id: "qwen3-next-thinking",
366
- name: "Qwen 3 Next Thinking",
367
- apiId: "qwen/qwen3-next-thinking-80b-maas",
368
- publisher: "qwen",
369
- endpointType: "maas",
370
- contextWindow: 262144,
371
- maxTokens: 32000,
372
- input: ["text"],
373
- reasoning: true,
374
- tools: true,
375
- cost: {
376
- input: 0.15,
377
- output: 1.20,
378
- cacheRead: 0,
379
- cacheWrite: 0,
380
- },
381
- region: "global",
382
- },
383
- {
384
- id: "qwen3-coder",
385
- name: "Qwen 3 Coder",
386
- apiId: "qwen/qwen3-coder-480b-a35b-instruct-maas",
387
- publisher: "qwen",
388
- endpointType: "maas",
389
- contextWindow: 262144,
390
- maxTokens: 32000,
391
- input: ["text"],
392
- reasoning: true,
393
- tools: true,
394
- cost: {
395
- input: 0.22,
396
- output: 1.80,
397
- cacheRead: 0.022,
398
- cacheWrite: 0,
399
- },
400
- region: "global",
401
- },
402
-
403
- // --- Moonshot ---
404
- {
405
- id: "kimi-k2-thinking",
406
- name: "Kimi K2 Thinking",
407
- apiId: "moonshotai/kimi-k2-thinking-maas",
408
- publisher: "moonshotai",
409
- endpointType: "maas",
410
- contextWindow: 262144,
411
- maxTokens: 32000,
412
- input: ["text"],
413
- reasoning: true,
414
- tools: true,
415
- cost: {
416
- input: 0.60,
417
- output: 2.50,
418
- cacheRead: 0.06,
419
- cacheWrite: 0,
420
- },
421
- region: "global",
422
- },
423
-
424
- // --- MiniMax ---
425
- {
426
- id: "minimax-m2",
427
- name: "MiniMax M2",
428
- apiId: "minimaxai/minimax-m2-maas",
429
- publisher: "minimaxai",
430
- endpointType: "maas",
431
- contextWindow: 196608,
432
- maxTokens: 32000,
433
- input: ["text"],
434
- reasoning: true,
435
- tools: true,
436
- cost: {
437
- input: 0.30,
438
- output: 1.20,
439
- cacheRead: 0.03,
440
- cacheWrite: 0,
441
- },
442
- region: "global",
443
- },
444
329
 
445
330
  // --- Google Gemma (MaaS) ---
446
331
  {
@@ -465,39 +350,20 @@ export const MAAS_MODELS: VertexModelConfig[] = [
465
350
 
466
351
  // --- GLM (Zhipu AI) ---
467
352
  {
468
- id: "glm-5",
469
- name: "GLM 5",
470
- apiId: "zai-org/glm-5-maas",
471
- publisher: "zai-org",
472
- endpointType: "maas",
473
- contextWindow: 200000,
474
- maxTokens: 32000,
475
- input: ["text"],
476
- reasoning: true,
477
- tools: true,
478
- cost: {
479
- input: 1.00,
480
- output: 3.20,
481
- cacheRead: 0.10,
482
- cacheWrite: 0,
483
- },
484
- region: "global",
485
- },
486
- {
487
- id: "glm-4.7",
488
- name: "GLM 4.7",
489
- apiId: "zai-org/glm-4.7-maas",
353
+ id: "glm-5.2",
354
+ name: "GLM 5.2",
355
+ apiId: "zai-org/glm-5.2-maas",
490
356
  publisher: "zai-org",
491
357
  endpointType: "maas",
492
- contextWindow: 200000,
493
- maxTokens: 32000,
358
+ contextWindow: 1000000,
359
+ maxTokens: 64000,
494
360
  input: ["text"],
495
361
  reasoning: true,
496
362
  tools: true,
497
363
  cost: {
498
- input: 0.60,
499
- output: 2.20,
500
- cacheRead: 0,
364
+ input: 1.40,
365
+ output: 4.40,
366
+ cacheRead: 0.14,
501
367
  cacheWrite: 0,
502
368
  },
503
369
  region: "global",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ssweens/pi-vertex",
3
- "version": "1.1.12",
3
+ "version": "1.3.0",
4
4
  "description": "Google Vertex AI provider for Pi coding agent - supports Gemini, Claude, and all MaaS models",
5
5
  "type": "module",
6
6
  "main": "index.ts",
@@ -16,21 +16,30 @@
16
16
  "CHANGELOG.md",
17
17
  "TEST_COVERAGE.md",
18
18
  "LICENSE",
19
- "screenshot.png"
19
+ "screenshot.png",
20
+ "tests/"
20
21
  ],
21
22
  "scripts": {
22
23
  "clean": "echo 'nothing to clean'",
23
24
  "build": "echo 'nothing to build'",
24
- "check": "echo 'nothing to check'"
25
+ "check": "echo 'nothing to check'",
26
+ "test": "node --experimental-strip-types --test tests/*.mjs"
25
27
  },
26
28
  "dependencies": {
27
29
  "@anthropic-ai/vertex-sdk": "^0.14.4",
28
- "@google/genai": "^1.42.0",
30
+ "@google/genai": "^2.22.0",
29
31
  "google-auth-library": "^9.0.0"
30
32
  },
31
33
  "peerDependencies": {
32
- "@mariozechner/pi-ai": "*",
33
- "@mariozechner/pi-coding-agent": "*"
34
+ "@earendil-works/pi-ai": ">=0.99.1 <0.100.0",
35
+ "@earendil-works/pi-coding-agent": ">=0.99.1 <0.100.0"
36
+ },
37
+ "devDependencies": {
38
+ "@earendil-works/pi-ai": "0.99.1",
39
+ "@earendil-works/pi-coding-agent": "0.99.1"
40
+ },
41
+ "overrides": {
42
+ "@earendil-works/pi-ai": "$@earendil-works/pi-ai"
34
43
  },
35
44
  "pi": {
36
45
  "extensions": [
@@ -4,59 +4,69 @@
4
4
  * Aligned with pi-mono's google-vertex.ts for consistent handling of:
5
5
  * - Thinking content (thought blocks with signatures)
6
6
  * - Tool calls with unique IDs and deduplication
7
- * - Thinking configuration (levels for Gemini 3, budgets for Gemini 2.5)
7
+ * - Thinking configuration (levels for Gemini 3/4, budgets for Gemini 2.5)
8
8
  * - Usage tracking including thinking tokens
9
9
  */
10
10
 
11
- import { GoogleGenAI, FinishReason, ThinkingLevel } from "@google/genai";
12
11
  import type { VertexModelConfig, Context, StreamOptions, AssistantMessage } from "../types.js";
13
12
  import { getAuthConfig, resolveLocation } from "../auth.js";
14
13
  import { sanitizeText, convertToGeminiMessages, convertToolsForGemini, retainThoughtSignature, calculateCost } from "../utils.js";
15
- import { createAssistantMessageEventStream, type AssistantMessageEventStream } from "@mariozechner/pi-ai";
14
+ import { createAssistantMessageEventStream, type AssistantMessageEventStream, type JsonObject } from "@earendil-works/pi-ai";
16
15
 
17
16
  // Module-level counter for generating unique tool call IDs (matches pi-mono pattern)
18
17
  let toolCallCounter = 0;
19
18
 
20
- const THINKING_LEVEL_MAP: Record<string, ThinkingLevel> = {
21
- minimal: ThinkingLevel.MINIMAL,
22
- low: ThinkingLevel.LOW,
23
- medium: ThinkingLevel.MEDIUM,
24
- high: ThinkingLevel.HIGH,
25
- };
26
-
27
19
  interface GeminiThinkingConfig {
28
20
  includeThoughts?: boolean;
29
21
  thinkingBudget?: number;
30
- thinkingLevel?: ThinkingLevel;
22
+ thinkingLevel?: string;
31
23
  }
32
24
 
33
25
  function isGemini3ProModel(modelId: string): boolean {
34
26
  return /gemini-3(?:\.\d+)?-pro/.test(modelId.toLowerCase());
35
27
  }
36
28
 
37
- function isGemini3FlashModel(modelId: string): boolean {
38
- return /gemini-3(?:\.\d+)?-flash/.test(modelId.toLowerCase());
29
+ function isGemini38FlashModel(modelId: string): boolean {
30
+ return /gemini-3\.8-flash/.test(modelId.toLowerCase());
31
+ }
32
+
33
+ function isGemini4Model(modelId: string): boolean {
34
+ return modelId.toLowerCase().startsWith("gemini-4");
39
35
  }
40
36
 
41
37
  function isGemini25ProModel(modelId: string): boolean {
42
38
  return /gemini-2\.5-pro/.test(modelId.toLowerCase());
43
39
  }
44
40
 
45
- function getGemini3ThinkingLevel(effort: string, modelId: string): ThinkingLevel {
46
- if (isGemini3ProModel(modelId)) {
47
- // Pro only supports LOW/MEDIUM/HIGH — floor minimal/low to LOW
48
- if (effort === "minimal" || effort === "low") return ThinkingLevel.LOW;
49
- if (effort === "medium") return ThinkingLevel.MEDIUM;
50
- return ThinkingLevel.HIGH;
41
+ // Gemini 3 and 4 models are configured with thinking levels; Gemini 2.5 uses budgets.
42
+ function usesThinkingLevels(apiId: string): boolean {
43
+ return apiId.startsWith("gemini-3") || apiId.startsWith("gemini-4");
44
+ }
45
+
46
+ function getGeminiThinkingLevel(
47
+ effort: string,
48
+ modelId: string,
49
+ levels: { LOW: string; MEDIUM: string; HIGH: string },
50
+ levelByEffort: Record<string, string>,
51
+ ): string {
52
+ if (isGemini3ProModel(modelId) || isGemini4Model(modelId) ||
53
+ (isGemini38FlashModel(modelId) && effort === "minimal")) {
54
+ // Gemini 3 Pro, Gemini 4, and Gemini 3.8 Flash reject MINIMAL (HTTP 400) — floor to LOW
55
+ if (effort === "minimal" || effort === "low") return levels.LOW;
56
+ if (effort === "medium") return levels.MEDIUM;
57
+ return levels.HIGH;
51
58
  }
52
- return THINKING_LEVEL_MAP[effort];
59
+ return levelByEffort[effort];
53
60
  }
54
61
 
55
- function mapGeminiStopReason(reason: string): "stop" | "length" | "toolUse" | "error" {
62
+ function mapGeminiStopReason(
63
+ reason: string,
64
+ finishReason: { STOP: string; MAX_TOKENS: string },
65
+ ): "stop" | "length" | "toolUse" | "error" {
56
66
  switch (reason) {
57
- case FinishReason.STOP:
67
+ case finishReason.STOP:
58
68
  return "stop";
59
- case FinishReason.MAX_TOKENS:
69
+ case finishReason.MAX_TOKENS:
60
70
  return "length";
61
71
  default:
62
72
  return "error";
@@ -93,6 +103,13 @@ export function streamGemini(
93
103
  // Priority: config file > env var > model region > default
94
104
  const location = resolveLocation(model.region);
95
105
  const auth = getAuthConfig(location);
106
+ const { GoogleGenAI, FinishReason, ThinkingLevel } = await import("@google/genai");
107
+ const thinkingLevelByEffort = {
108
+ minimal: ThinkingLevel.MINIMAL,
109
+ low: ThinkingLevel.LOW,
110
+ medium: ThinkingLevel.MEDIUM,
111
+ high: ThinkingLevel.HIGH,
112
+ };
96
113
 
97
114
  // Create client with explicit API version (matches pi-mono)
98
115
  const client = new GoogleGenAI({
@@ -126,13 +143,18 @@ export function streamGemini(
126
143
  // Add thinking configuration (matches pi-mono's buildParams logic).
127
144
  if (model.reasoning) {
128
145
  if (options?.reasoning) {
129
- const effort = options.reasoning === "xhigh" ? "high" : options.reasoning;
130
- const isGemini3 = model.apiId.startsWith("gemini-3");
146
+ // XHIGH goes only to models that declare it (Gemini 4 Hydrogen); every
147
+ // other Gemini model answers XHIGH with HTTP 400, so it caps at HIGH.
148
+ const supportsXhigh = model.thinkingLevelMap?.xhigh != null;
149
+ const effort = options.reasoning === "xhigh" || options.reasoning === "max" ? "high" : options.reasoning;
150
+ const usesLevels = usesThinkingLevels(model.apiId);
131
151
  const thinkingConfig: GeminiThinkingConfig = { includeThoughts: true };
132
152
 
133
- if (isGemini3) {
134
- // Gemini 3 Pro doesn't support MINIMAL; Flash models do.
135
- thinkingConfig.thinkingLevel = getGemini3ThinkingLevel(effort, model.apiId);
153
+ if (usesLevels) {
154
+ thinkingConfig.thinkingLevel = supportsXhigh && options.reasoning === "xhigh"
155
+ // @google/genai's ThinkingLevel enum predates XHIGH; the SDK forwards the string.
156
+ ? "XHIGH"
157
+ : getGeminiThinkingLevel(effort, model.apiId, ThinkingLevel, thinkingLevelByEffort);
136
158
  } else {
137
159
  // Gemini 2.5 models use thinking budgets (token counts)
138
160
  const budgets: Record<string, number> = {
@@ -147,12 +169,12 @@ export function streamGemini(
147
169
  config.thinkingConfig = thinkingConfig;
148
170
  } else {
149
171
  // If no reasoning level is specified:
150
- // - For Gemini 3.x/3.5 models, omit thinkingConfig entirely so Vertex AI uses
172
+ // - For Gemini 3.x/3.5/4 models, omit thinkingConfig entirely so Vertex AI uses
151
173
  // the model's native default level (e.g. MEDIUM for 3.5, HIGH for others).
152
174
  // - For Gemini 2.5 models, apply a healthy thinking budget floor (thinking is
153
175
  // disabled by default on 2.5).
154
- const isGemini3 = model.apiId.startsWith("gemini-3");
155
- if (!isGemini3) {
176
+ const usesLevels = usesThinkingLevels(model.apiId);
177
+ if (!usesLevels) {
156
178
  config.thinkingConfig = {
157
179
  includeThoughts: true,
158
180
  thinkingBudget: model.apiId.includes("2.5-pro") ? 2048 : 1024,
@@ -252,7 +274,8 @@ export function streamGemini(
252
274
  type: "toolCall" as const,
253
275
  id: toolCallId,
254
276
  name: part.functionCall.name || "",
255
- arguments: (part.functionCall.args as Record<string, unknown>) ?? {},
277
+ // Gemini returns tool args as parsed JSON, so they are always JSON values.
278
+ arguments: (part.functionCall.args as JsonObject | undefined) ?? {},
256
279
  ...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),
257
280
  };
258
281
 
@@ -267,7 +290,7 @@ export function streamGemini(
267
290
 
268
291
  // Handle finish reason
269
292
  if (candidate?.finishReason) {
270
- output.stopReason = mapGeminiStopReason(candidate.finishReason);
293
+ output.stopReason = mapGeminiStopReason(candidate.finishReason, FinishReason);
271
294
  if (candidate.finishReason === FinishReason.SAFETY) {
272
295
  output.errorMessage = "Content blocked by safety filters";
273
296
  }
@@ -311,7 +334,19 @@ export function streamGemini(
311
334
  throw new Error("Request was aborted");
312
335
  }
313
336
 
314
- stream.push({ type: "done", reason: output.stopReason, message: output });
337
+ // A "done" event only carries successful stop reasons. Safety blocks and
338
+ // unmapped finish reasons surface as an error event instead, matching the
339
+ // catch block below.
340
+ const finalReason = output.stopReason;
341
+ if (finalReason === "stop" || finalReason === "length" || finalReason === "toolUse") {
342
+ stream.push({ type: "done", reason: finalReason, message: output });
343
+ } else {
344
+ // Error events only carry "aborted" | "error"; anything else that is
345
+ // not a successful stop (Pi 0.99 added "pending"/"deferred") is an error.
346
+ const errorReason = finalReason === "aborted" ? "aborted" : "error";
347
+ output.stopReason = errorReason;
348
+ stream.push({ type: "error", reason: errorReason, error: output });
349
+ }
315
350
  stream.end();
316
351
  } catch (error) {
317
352
  output.stopReason = options?.signal?.aborted ? "aborted" : "error";