@fleetagent/pi-ai 0.1.8 → 0.2.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.
Files changed (148) hide show
  1. package/dist/cli.d.ts.map +1 -1
  2. package/dist/cli.js +29 -24
  3. package/dist/cli.js.map +1 -1
  4. package/dist/env-api-keys.d.ts.map +1 -1
  5. package/dist/env-api-keys.js +1 -1
  6. package/dist/env-api-keys.js.map +1 -1
  7. package/dist/image-models.generated.d.ts +225 -0
  8. package/dist/image-models.generated.d.ts.map +1 -1
  9. package/dist/image-models.generated.js +227 -2
  10. package/dist/image-models.generated.js.map +1 -1
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/models.d.ts +37 -2
  15. package/dist/models.d.ts.map +1 -1
  16. package/dist/models.generated.d.ts +6120 -2593
  17. package/dist/models.generated.d.ts.map +1 -1
  18. package/dist/models.generated.js +5347 -2110
  19. package/dist/models.generated.js.map +1 -1
  20. package/dist/models.js +259 -9
  21. package/dist/models.js.map +1 -1
  22. package/dist/oauth.d.ts +6 -1
  23. package/dist/oauth.d.ts.map +1 -1
  24. package/dist/oauth.js +4 -1
  25. package/dist/oauth.js.map +1 -1
  26. package/dist/providers/amazon-bedrock.d.ts +5 -4
  27. package/dist/providers/amazon-bedrock.d.ts.map +1 -1
  28. package/dist/providers/amazon-bedrock.js +303 -330
  29. package/dist/providers/amazon-bedrock.js.map +1 -1
  30. package/dist/providers/anthropic.d.ts +5 -4
  31. package/dist/providers/anthropic.d.ts.map +1 -1
  32. package/dist/providers/anthropic.js +492 -499
  33. package/dist/providers/anthropic.js.map +1 -1
  34. package/dist/providers/azure-openai-responses.d.ts +4 -2
  35. package/dist/providers/azure-openai-responses.d.ts.map +1 -1
  36. package/dist/providers/azure-openai-responses.js +52 -35
  37. package/dist/providers/azure-openai-responses.js.map +1 -1
  38. package/dist/providers/faux.d.ts +19 -22
  39. package/dist/providers/faux.d.ts.map +1 -1
  40. package/dist/providers/faux.js +106 -70
  41. package/dist/providers/faux.js.map +1 -1
  42. package/dist/providers/github-copilot-headers.d.ts +7 -4
  43. package/dist/providers/github-copilot-headers.d.ts.map +1 -1
  44. package/dist/providers/github-copilot-headers.js.map +1 -1
  45. package/dist/providers/google-shared.d.ts +24 -5
  46. package/dist/providers/google-shared.d.ts.map +1 -1
  47. package/dist/providers/google-shared.js +139 -140
  48. package/dist/providers/google-shared.js.map +1 -1
  49. package/dist/providers/google-vertex.d.ts +3 -7
  50. package/dist/providers/google-vertex.d.ts.map +1 -1
  51. package/dist/providers/google-vertex.js +216 -219
  52. package/dist/providers/google-vertex.js.map +1 -1
  53. package/dist/providers/google.d.ts +4 -7
  54. package/dist/providers/google.d.ts.map +1 -1
  55. package/dist/providers/google.js +214 -218
  56. package/dist/providers/google.js.map +1 -1
  57. package/dist/providers/images/openrouter.d.ts.map +1 -1
  58. package/dist/providers/images/openrouter.js +37 -27
  59. package/dist/providers/images/openrouter.js.map +1 -1
  60. package/dist/providers/mistral.d.ts +7 -6
  61. package/dist/providers/mistral.d.ts.map +1 -1
  62. package/dist/providers/mistral.js +287 -265
  63. package/dist/providers/mistral.js.map +1 -1
  64. package/dist/providers/openai-codex-responses.d.ts +8 -5
  65. package/dist/providers/openai-codex-responses.d.ts.map +1 -1
  66. package/dist/providers/openai-codex-responses.js +493 -391
  67. package/dist/providers/openai-codex-responses.js.map +1 -1
  68. package/dist/providers/openai-completions.d.ts +7 -10
  69. package/dist/providers/openai-completions.d.ts.map +1 -1
  70. package/dist/providers/openai-completions.js +670 -664
  71. package/dist/providers/openai-completions.js.map +1 -1
  72. package/dist/providers/openai-responses-shared.d.ts +5 -4
  73. package/dist/providers/openai-responses-shared.d.ts.map +1 -1
  74. package/dist/providers/openai-responses-shared.js +390 -360
  75. package/dist/providers/openai-responses-shared.js.map +1 -1
  76. package/dist/providers/openai-responses.d.ts +5 -5
  77. package/dist/providers/openai-responses.d.ts.map +1 -1
  78. package/dist/providers/openai-responses.js +134 -109
  79. package/dist/providers/openai-responses.js.map +1 -1
  80. package/dist/providers/simple-options.d.ts +4 -2
  81. package/dist/providers/simple-options.d.ts.map +1 -1
  82. package/dist/providers/simple-options.js.map +1 -1
  83. package/dist/providers/transform-messages.d.ts.map +1 -1
  84. package/dist/providers/transform-messages.js +112 -137
  85. package/dist/providers/transform-messages.js.map +1 -1
  86. package/dist/stream.d.ts +0 -1
  87. package/dist/stream.d.ts.map +1 -1
  88. package/dist/stream.js +0 -1
  89. package/dist/stream.js.map +1 -1
  90. package/dist/types.d.ts +91 -63
  91. package/dist/types.d.ts.map +1 -1
  92. package/dist/types.js.map +1 -1
  93. package/dist/utils/error-body.d.ts.map +1 -1
  94. package/dist/utils/error-body.js +15 -4
  95. package/dist/utils/error-body.js.map +1 -1
  96. package/dist/utils/estimate.d.ts +4 -0
  97. package/dist/utils/estimate.d.ts.map +1 -1
  98. package/dist/utils/estimate.js +3 -1
  99. package/dist/utils/estimate.js.map +1 -1
  100. package/dist/utils/headers.d.ts +3 -0
  101. package/dist/utils/headers.d.ts.map +1 -1
  102. package/dist/utils/headers.js +21 -0
  103. package/dist/utils/headers.js.map +1 -1
  104. package/dist/utils/json-parse.d.ts.map +1 -1
  105. package/dist/utils/json-parse.js +17 -19
  106. package/dist/utils/json-parse.js.map +1 -1
  107. package/dist/utils/oauth/anthropic.d.ts +4 -11
  108. package/dist/utils/oauth/anthropic.d.ts.map +1 -1
  109. package/dist/utils/oauth/anthropic.js +100 -117
  110. package/dist/utils/oauth/anthropic.js.map +1 -1
  111. package/dist/utils/oauth/device-code.d.ts.map +1 -1
  112. package/dist/utils/oauth/device-code.js +51 -38
  113. package/dist/utils/oauth/device-code.js.map +1 -1
  114. package/dist/utils/oauth/github-copilot.d.ts +6 -9
  115. package/dist/utils/oauth/github-copilot.d.ts.map +1 -1
  116. package/dist/utils/oauth/github-copilot.js +47 -42
  117. package/dist/utils/oauth/github-copilot.js.map +1 -1
  118. package/dist/utils/oauth/index.d.ts +8 -17
  119. package/dist/utils/oauth/index.d.ts.map +1 -1
  120. package/dist/utils/oauth/index.js +27 -23
  121. package/dist/utils/oauth/index.js.map +1 -1
  122. package/dist/utils/oauth/oauth-page.d.ts.map +1 -1
  123. package/dist/utils/oauth/oauth-page.js.map +1 -1
  124. package/dist/utils/oauth/openai-codex.d.ts +7 -12
  125. package/dist/utils/oauth/openai-codex.d.ts.map +1 -1
  126. package/dist/utils/oauth/openai-codex.js +72 -89
  127. package/dist/utils/oauth/openai-codex.js.map +1 -1
  128. package/dist/utils/oauth/pkce.d.ts +5 -4
  129. package/dist/utils/oauth/pkce.d.ts.map +1 -1
  130. package/dist/utils/oauth/pkce.js.map +1 -1
  131. package/dist/utils/oauth/types.d.ts +7 -2
  132. package/dist/utils/oauth/types.d.ts.map +1 -1
  133. package/dist/utils/oauth/types.js.map +1 -1
  134. package/dist/utils/provider-retry.d.ts +16 -0
  135. package/dist/utils/provider-retry.d.ts.map +1 -0
  136. package/dist/utils/provider-retry.js +94 -0
  137. package/dist/utils/provider-retry.js.map +1 -0
  138. package/dist/utils/retry.d.ts +14 -0
  139. package/dist/utils/retry.d.ts.map +1 -1
  140. package/dist/utils/retry.js +84 -0
  141. package/dist/utils/retry.js.map +1 -1
  142. package/dist/utils/typebox-helpers.d.ts +5 -4
  143. package/dist/utils/typebox-helpers.d.ts.map +1 -1
  144. package/dist/utils/typebox-helpers.js.map +1 -1
  145. package/dist/utils/validation.d.ts.map +1 -1
  146. package/dist/utils/validation.js +102 -99
  147. package/dist/utils/validation.js.map +1 -1
  148. package/package.json +2 -2
@@ -1,8 +1,9 @@
1
1
  import { GoogleGenAI, ResourceScope, ThinkingLevel, } from "@google/genai";
2
2
  import { calculateCost, clampThinkingLevel } from "../models.js";
3
3
  import { AssistantMessageEventStream } from "../utils/event-stream.js";
4
+ import { providerHeadersToRecord } from "../utils/headers.js";
4
5
  import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
5
- import { convertMessages, convertTools, isThinkingPart, mapStopReason, mapToolChoice, retainThoughtSignature, } from "./google-shared.js";
6
+ import { convertMessages, convertTools, isThinkingPart, mapStopReason, mapToolChoice, retainThoughtSignature, retryGoogleRequest, } from "./google-shared.js";
6
7
  import { buildBaseOptions } from "./simple-options.js";
7
8
  const API_VERSION = "v1";
8
9
  const GCP_VERTEX_CREDENTIALS_MARKER = "gcp-vertex-credentials";
@@ -13,214 +14,204 @@ const THINKING_LEVEL_MAP = {
13
14
  MEDIUM: ThinkingLevel.MEDIUM,
14
15
  HIGH: ThinkingLevel.HIGH,
15
16
  };
17
+ function createGoogleVertexOutput(model) {
18
+ return {
19
+ role: "assistant",
20
+ content: [],
21
+ api: "google-vertex",
22
+ provider: model.provider,
23
+ model: model.id,
24
+ usage: {
25
+ input: 0,
26
+ output: 0,
27
+ cacheRead: 0,
28
+ cacheWrite: 0,
29
+ totalTokens: 0,
30
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
31
+ },
32
+ stopReason: "stop",
33
+ timestamp: Date.now(),
34
+ };
35
+ }
36
+ function googleVertexBlockIndex(state) {
37
+ return state.output.content.length - 1;
38
+ }
39
+ function endGoogleVertexBlock(state) {
40
+ const block = state.currentBlock;
41
+ if (!block)
42
+ return;
43
+ if (block.type === "text") {
44
+ state.stream.push({
45
+ type: "text_end",
46
+ contentIndex: googleVertexBlockIndex(state),
47
+ content: block.text,
48
+ partial: state.output,
49
+ });
50
+ }
51
+ else {
52
+ state.stream.push({
53
+ type: "thinking_end",
54
+ contentIndex: googleVertexBlockIndex(state),
55
+ content: block.thinking,
56
+ partial: state.output,
57
+ });
58
+ }
59
+ state.currentBlock = null;
60
+ }
61
+ function ensureGoogleVertexTextBlock(state, isThinking) {
62
+ const current = state.currentBlock;
63
+ if (current && ((isThinking && current.type === "thinking") || (!isThinking && current.type === "text"))) {
64
+ return current;
65
+ }
66
+ endGoogleVertexBlock(state);
67
+ if (isThinking) {
68
+ const block = { type: "thinking", thinking: "", thinkingSignature: undefined };
69
+ state.currentBlock = block;
70
+ state.output.content.push(block);
71
+ state.stream.push({ type: "thinking_start", contentIndex: googleVertexBlockIndex(state), partial: state.output });
72
+ return block;
73
+ }
74
+ const block = { type: "text", text: "" };
75
+ state.currentBlock = block;
76
+ state.output.content.push(block);
77
+ state.stream.push({ type: "text_start", contentIndex: googleVertexBlockIndex(state), partial: state.output });
78
+ return block;
79
+ }
80
+ function appendGoogleVertexTextPart(state, part) {
81
+ if (part.text === undefined)
82
+ return;
83
+ const block = ensureGoogleVertexTextBlock(state, isThinkingPart(part));
84
+ if (block.type === "thinking") {
85
+ block.thinking += part.text;
86
+ block.thinkingSignature = retainThoughtSignature(block.thinkingSignature, part.thoughtSignature);
87
+ state.stream.push({
88
+ type: "thinking_delta",
89
+ contentIndex: googleVertexBlockIndex(state),
90
+ delta: part.text,
91
+ partial: state.output,
92
+ });
93
+ return;
94
+ }
95
+ block.text += part.text;
96
+ block.textSignature = retainThoughtSignature(block.textSignature, part.thoughtSignature);
97
+ state.stream.push({
98
+ type: "text_delta",
99
+ contentIndex: googleVertexBlockIndex(state),
100
+ delta: part.text,
101
+ partial: state.output,
102
+ });
103
+ }
104
+ function appendGoogleVertexToolCall(state, part) {
105
+ if (!part.functionCall)
106
+ return;
107
+ endGoogleVertexBlock(state);
108
+ const providedId = part.functionCall.id;
109
+ const needsNewId = !providedId || state.output.content.some((block) => block.type === "toolCall" && block.id === providedId);
110
+ const toolCall = {
111
+ type: "toolCall",
112
+ id: needsNewId ? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}` : providedId,
113
+ name: part.functionCall.name || "",
114
+ arguments: part.functionCall.args ?? {},
115
+ ...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),
116
+ };
117
+ state.output.content.push(toolCall);
118
+ state.stream.push({ type: "toolcall_start", contentIndex: googleVertexBlockIndex(state), partial: state.output });
119
+ state.stream.push({
120
+ type: "toolcall_delta",
121
+ contentIndex: googleVertexBlockIndex(state),
122
+ delta: JSON.stringify(toolCall.arguments),
123
+ partial: state.output,
124
+ });
125
+ state.stream.push({
126
+ type: "toolcall_end",
127
+ contentIndex: googleVertexBlockIndex(state),
128
+ toolCall,
129
+ partial: state.output,
130
+ });
131
+ }
132
+ function applyGoogleVertexFinishReason(output, candidate) {
133
+ if (!candidate?.finishReason)
134
+ return;
135
+ output.stopReason = mapStopReason(candidate.finishReason);
136
+ if (output.content.some((block) => block.type === "toolCall"))
137
+ output.stopReason = "toolUse";
138
+ }
139
+ function applyGoogleVertexUsage(output, chunk, model) {
140
+ if (!chunk.usageMetadata)
141
+ return;
142
+ output.usage = {
143
+ input: (chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),
144
+ output: (chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
145
+ cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,
146
+ cacheWrite: 0,
147
+ totalTokens: chunk.usageMetadata.totalTokenCount || 0,
148
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
149
+ };
150
+ calculateCost(model, output.usage);
151
+ }
152
+ function processGoogleVertexChunk(state, chunk, model) {
153
+ state.output.responseId ||= chunk.responseId;
154
+ const candidate = chunk.candidates?.[0];
155
+ for (const part of candidate?.content?.parts ?? []) {
156
+ appendGoogleVertexTextPart(state, part);
157
+ appendGoogleVertexToolCall(state, part);
158
+ }
159
+ applyGoogleVertexFinishReason(state.output, candidate);
160
+ applyGoogleVertexUsage(state.output, chunk, model);
161
+ }
162
+ function completeGoogleVertexStream(state, signal) {
163
+ endGoogleVertexBlock(state);
164
+ if (signal?.aborted)
165
+ throw new Error("Request was aborted");
166
+ if (state.output.stopReason === "pending") {
167
+ throw new Error("Google Vertex stream ended without a finish reason");
168
+ }
169
+ if (state.output.stopReason === "aborted" || state.output.stopReason === "error") {
170
+ throw new Error("An unknown error occurred");
171
+ }
172
+ state.stream.push({ type: "done", reason: state.output.stopReason, message: state.output });
173
+ state.stream.end();
174
+ }
175
+ function failGoogleVertexStream(state, error, signal) {
176
+ for (const block of state.output.content) {
177
+ if ("index" in block)
178
+ delete block.index;
179
+ }
180
+ state.output.stopReason = signal?.aborted ? "aborted" : "error";
181
+ state.output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
182
+ state.stream.push({ type: "error", reason: state.output.stopReason, error: state.output });
183
+ state.stream.end();
184
+ }
185
+ async function runGoogleVertexStream(model, context, options, state) {
186
+ try {
187
+ const apiKey = resolveApiKey(options);
188
+ const client = apiKey
189
+ ? createClientWithApiKey(model, apiKey, options?.headers)
190
+ : createClient(model, resolveProject(options), resolveLocation(options), options?.headers);
191
+ let params = buildParams(model, context, options);
192
+ const nextParams = await options?.onPayload?.(params, model);
193
+ if (nextParams !== undefined)
194
+ params = nextParams;
195
+ const googleStream = await retryGoogleRequest(() => client.models.generateContentStream(params), options);
196
+ state.stream.push({ type: "start", partial: state.output });
197
+ for await (const chunk of googleStream)
198
+ processGoogleVertexChunk(state, chunk, model);
199
+ completeGoogleVertexStream(state, options?.signal);
200
+ }
201
+ catch (error) {
202
+ failGoogleVertexStream(state, error, options?.signal);
203
+ }
204
+ }
16
205
  // Counter for generating unique tool call IDs
17
206
  let toolCallCounter = 0;
18
207
  export const streamGoogleVertex = (model, context, options) => {
19
208
  const stream = new AssistantMessageEventStream();
20
- (async () => {
21
- const output = {
22
- role: "assistant",
23
- content: [],
24
- api: "google-vertex",
25
- provider: model.provider,
26
- model: model.id,
27
- usage: {
28
- input: 0,
29
- output: 0,
30
- cacheRead: 0,
31
- cacheWrite: 0,
32
- totalTokens: 0,
33
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
34
- },
35
- stopReason: "stop",
36
- timestamp: Date.now(),
37
- };
38
- try {
39
- const apiKey = resolveApiKey(options);
40
- // Create the client using either a Vertex API key, if provided, or ADC with project and location
41
- const client = apiKey
42
- ? createClientWithApiKey(model, apiKey, options?.headers)
43
- : createClient(model, resolveProject(options), resolveLocation(options), options?.headers);
44
- let params = buildParams(model, context, options);
45
- const nextParams = await options?.onPayload?.(params, model);
46
- if (nextParams !== undefined) {
47
- params = nextParams;
48
- }
49
- const googleStream = await client.models.generateContentStream(params);
50
- stream.push({ type: "start", partial: output });
51
- let currentBlock = null;
52
- const blocks = output.content;
53
- const blockIndex = () => blocks.length - 1;
54
- for await (const chunk of googleStream) {
55
- // Vertex uses the same @google/genai GenerateContentResponse type as Gemini.
56
- // responseId is documented there as an output-only identifier for each response.
57
- output.responseId ||= chunk.responseId;
58
- const candidate = chunk.candidates?.[0];
59
- if (candidate?.content?.parts) {
60
- for (const part of candidate.content.parts) {
61
- if (part.text !== undefined) {
62
- const isThinking = isThinkingPart(part);
63
- if (!currentBlock ||
64
- (isThinking && currentBlock.type !== "thinking") ||
65
- (!isThinking && currentBlock.type !== "text")) {
66
- if (currentBlock) {
67
- if (currentBlock.type === "text") {
68
- stream.push({
69
- type: "text_end",
70
- contentIndex: blocks.length - 1,
71
- content: currentBlock.text,
72
- partial: output,
73
- });
74
- }
75
- else {
76
- stream.push({
77
- type: "thinking_end",
78
- contentIndex: blockIndex(),
79
- content: currentBlock.thinking,
80
- partial: output,
81
- });
82
- }
83
- }
84
- if (isThinking) {
85
- currentBlock = { type: "thinking", thinking: "", thinkingSignature: undefined };
86
- output.content.push(currentBlock);
87
- stream.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
88
- }
89
- else {
90
- currentBlock = { type: "text", text: "" };
91
- output.content.push(currentBlock);
92
- stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
93
- }
94
- }
95
- if (currentBlock.type === "thinking") {
96
- currentBlock.thinking += part.text;
97
- currentBlock.thinkingSignature = retainThoughtSignature(currentBlock.thinkingSignature, part.thoughtSignature);
98
- stream.push({
99
- type: "thinking_delta",
100
- contentIndex: blockIndex(),
101
- delta: part.text,
102
- partial: output,
103
- });
104
- }
105
- else {
106
- currentBlock.text += part.text;
107
- currentBlock.textSignature = retainThoughtSignature(currentBlock.textSignature, part.thoughtSignature);
108
- stream.push({
109
- type: "text_delta",
110
- contentIndex: blockIndex(),
111
- delta: part.text,
112
- partial: output,
113
- });
114
- }
115
- }
116
- if (part.functionCall) {
117
- if (currentBlock) {
118
- if (currentBlock.type === "text") {
119
- stream.push({
120
- type: "text_end",
121
- contentIndex: blockIndex(),
122
- content: currentBlock.text,
123
- partial: output,
124
- });
125
- }
126
- else {
127
- stream.push({
128
- type: "thinking_end",
129
- contentIndex: blockIndex(),
130
- content: currentBlock.thinking,
131
- partial: output,
132
- });
133
- }
134
- currentBlock = null;
135
- }
136
- const providedId = part.functionCall.id;
137
- const needsNewId = !providedId || output.content.some((b) => b.type === "toolCall" && b.id === providedId);
138
- const toolCallId = needsNewId
139
- ? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`
140
- : providedId;
141
- const toolCall = {
142
- type: "toolCall",
143
- id: toolCallId,
144
- name: part.functionCall.name || "",
145
- arguments: part.functionCall.args ?? {},
146
- ...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),
147
- };
148
- output.content.push(toolCall);
149
- stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
150
- stream.push({
151
- type: "toolcall_delta",
152
- contentIndex: blockIndex(),
153
- delta: JSON.stringify(toolCall.arguments),
154
- partial: output,
155
- });
156
- stream.push({ type: "toolcall_end", contentIndex: blockIndex(), toolCall, partial: output });
157
- }
158
- }
159
- }
160
- if (candidate?.finishReason) {
161
- output.stopReason = mapStopReason(candidate.finishReason);
162
- if (output.content.some((b) => b.type === "toolCall")) {
163
- output.stopReason = "toolUse";
164
- }
165
- }
166
- if (chunk.usageMetadata) {
167
- output.usage = {
168
- input: (chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),
169
- output: (chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
170
- cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,
171
- cacheWrite: 0,
172
- totalTokens: chunk.usageMetadata.totalTokenCount || 0,
173
- cost: {
174
- input: 0,
175
- output: 0,
176
- cacheRead: 0,
177
- cacheWrite: 0,
178
- total: 0,
179
- },
180
- };
181
- calculateCost(model, output.usage);
182
- }
183
- }
184
- if (currentBlock) {
185
- if (currentBlock.type === "text") {
186
- stream.push({
187
- type: "text_end",
188
- contentIndex: blockIndex(),
189
- content: currentBlock.text,
190
- partial: output,
191
- });
192
- }
193
- else {
194
- stream.push({
195
- type: "thinking_end",
196
- contentIndex: blockIndex(),
197
- content: currentBlock.thinking,
198
- partial: output,
199
- });
200
- }
201
- }
202
- if (options?.signal?.aborted) {
203
- throw new Error("Request was aborted");
204
- }
205
- if (output.stopReason === "aborted" || output.stopReason === "error") {
206
- throw new Error("An unknown error occurred");
207
- }
208
- stream.push({ type: "done", reason: output.stopReason, message: output });
209
- stream.end();
210
- }
211
- catch (error) {
212
- // Remove internal index property used during streaming
213
- for (const block of output.content) {
214
- if ("index" in block) {
215
- delete block.index;
216
- }
217
- }
218
- output.stopReason = options?.signal?.aborted ? "aborted" : "error";
219
- output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
220
- stream.push({ type: "error", reason: output.stopReason, error: output });
221
- stream.end();
222
- }
223
- })();
209
+ const state = {
210
+ output: createGoogleVertexOutput(model),
211
+ stream,
212
+ currentBlock: null,
213
+ };
214
+ void runGoogleVertexStream(model, context, options, state);
224
215
  return stream;
225
216
  };
226
217
  export const streamSimpleGoogleVertex = (model, context, options) => {
@@ -278,8 +269,9 @@ function buildHttpOptions(model, optionsHeaders) {
278
269
  httpOptions.apiVersion = "";
279
270
  }
280
271
  }
281
- if (model.headers || optionsHeaders) {
282
- httpOptions.headers = { ...model.headers, ...optionsHeaders };
272
+ const headers = providerHeadersToRecord({ ...model.headers, ...optionsHeaders });
273
+ if (headers) {
274
+ httpOptions.headers = headers;
283
275
  }
284
276
  return Object.keys(httpOptions).length > 0 ? httpOptions : undefined;
285
277
  }
@@ -323,6 +315,21 @@ function resolveLocation(options) {
323
315
  }
324
316
  return location;
325
317
  }
318
+ function resolveGoogleVertexThinkingConfig(model, thinking) {
319
+ if (thinking?.enabled && model.reasoning) {
320
+ const config = { includeThoughts: true };
321
+ if (thinking.level !== undefined) {
322
+ config.thinkingLevel = THINKING_LEVEL_MAP[thinking.level];
323
+ }
324
+ else if (thinking.budgetTokens !== undefined) {
325
+ config.thinkingBudget = thinking.budgetTokens;
326
+ }
327
+ return config;
328
+ }
329
+ if (model.reasoning && thinking && !thinking.enabled)
330
+ return getDisabledThinkingConfig(model);
331
+ return undefined;
332
+ }
326
333
  function buildParams(model, context, options = {}) {
327
334
  const contents = convertMessages(model, context);
328
335
  const generationConfig = {};
@@ -347,19 +354,9 @@ function buildParams(model, context, options = {}) {
347
354
  else {
348
355
  config.toolConfig = undefined;
349
356
  }
350
- if (options.thinking?.enabled && model.reasoning) {
351
- const thinkingConfig = { includeThoughts: true };
352
- if (options.thinking.level !== undefined) {
353
- thinkingConfig.thinkingLevel = THINKING_LEVEL_MAP[options.thinking.level];
354
- }
355
- else if (options.thinking.budgetTokens !== undefined) {
356
- thinkingConfig.thinkingBudget = options.thinking.budgetTokens;
357
- }
357
+ const thinkingConfig = resolveGoogleVertexThinkingConfig(model, options.thinking);
358
+ if (thinkingConfig)
358
359
  config.thinkingConfig = thinkingConfig;
359
- }
360
- else if (model.reasoning && options.thinking && !options.thinking.enabled) {
361
- config.thinkingConfig = getDisabledThinkingConfig(model);
362
- }
363
360
  if (options.signal) {
364
361
  if (options.signal.aborted) {
365
362
  throw new Error("Request aborted");
@@ -1 +1 @@
1
- {"version":3,"file":"google-vertex.js","sourceRoot":"","sources":["../../src/providers/google-vertex.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,WAAW,EAEX,aAAa,EAEb,aAAa,GACb,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,cAAc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CAAC,WAAW,CAAC,EAAE,CAAC;QACpC,MAAM,OAAO,GAAyC;YACrD,OAAO,EAAE,GAAG;YACZ,GAAG,EAAE,IAAI;YACT,MAAM,EAAE,IAAI;YACZ,IAAI,EAAE,KAAK;SACX,CAAC;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;IAED,OAAO,CAAC,CAAC,CAAC;AAAA,CACV","sourcesContent":["import {\n\ttype GenerateContentConfig,\n\ttype GenerateContentParameters,\n\tGoogleGenAI,\n\ttype HttpOptions,\n\tResourceScope,\n\ttype ThinkingConfig,\n\tThinkingLevel,\n} from \"@google/genai\";\nimport { calculateCost, clampThinkingLevel } from \"../models.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tContext,\n\tModel,\n\tThinkingLevel as PiThinkingLevel,\n\tSimpleStreamOptions,\n\tStreamFunction,\n\tStreamOptions,\n\tTextContent,\n\tThinkingBudgets,\n\tThinkingContent,\n\tToolCall,\n} from \"../types.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { sanitizeSurrogates } from \"../utils/sanitize-unicode.ts\";\nimport type { GoogleThinkingLevel } from \"./google-shared.ts\";\nimport {\n\tconvertMessages,\n\tconvertTools,\n\tisThinkingPart,\n\tmapStopReason,\n\tmapToolChoice,\n\tretainThoughtSignature,\n} from \"./google-shared.ts\";\nimport { buildBaseOptions } from \"./simple-options.ts\";\n\nexport interface GoogleVertexOptions extends StreamOptions {\n\ttoolChoice?: \"auto\" | \"none\" | \"any\";\n\tthinking?: {\n\t\tenabled: boolean;\n\t\tbudgetTokens?: number; // -1 for dynamic, 0 to disable\n\t\tlevel?: GoogleThinkingLevel;\n\t};\n\tproject?: string;\n\tlocation?: string;\n}\n\nconst API_VERSION = \"v1\";\nconst GCP_VERTEX_CREDENTIALS_MARKER = \"gcp-vertex-credentials\";\n\nconst THINKING_LEVEL_MAP: Record<GoogleThinkingLevel, ThinkingLevel> = {\n\tTHINKING_LEVEL_UNSPECIFIED: ThinkingLevel.THINKING_LEVEL_UNSPECIFIED,\n\tMINIMAL: ThinkingLevel.MINIMAL,\n\tLOW: ThinkingLevel.LOW,\n\tMEDIUM: ThinkingLevel.MEDIUM,\n\tHIGH: ThinkingLevel.HIGH,\n};\n\n// Counter for generating unique tool call IDs\nlet toolCallCounter = 0;\n\nexport const streamGoogleVertex: StreamFunction<\"google-vertex\", GoogleVertexOptions> = (\n\tmodel: Model<\"google-vertex\">,\n\tcontext: Context,\n\toptions?: GoogleVertexOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\n\t(async () => {\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: \"google-vertex\" as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"stop\",\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\ttry {\n\t\t\tconst apiKey = resolveApiKey(options);\n\t\t\t// Create the client using either a Vertex API key, if provided, or ADC with project and location\n\t\t\tconst client = apiKey\n\t\t\t\t? createClientWithApiKey(model, apiKey, options?.headers)\n\t\t\t\t: createClient(model, resolveProject(options), resolveLocation(options), options?.headers);\n\t\t\tlet params = buildParams(model, context, options);\n\t\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\t\tif (nextParams !== undefined) {\n\t\t\t\tparams = nextParams as GenerateContentParameters;\n\t\t\t}\n\t\t\tconst googleStream = await client.models.generateContentStream(params);\n\n\t\t\tstream.push({ type: \"start\", partial: output });\n\t\t\tlet currentBlock: TextContent | ThinkingContent | null = null;\n\t\t\tconst blocks = output.content;\n\t\t\tconst blockIndex = () => blocks.length - 1;\n\t\t\tfor await (const chunk of googleStream) {\n\t\t\t\t// Vertex uses the same @google/genai GenerateContentResponse type as Gemini.\n\t\t\t\t// responseId is documented there as an output-only identifier for each response.\n\t\t\t\toutput.responseId ||= chunk.responseId;\n\t\t\t\tconst candidate = chunk.candidates?.[0];\n\t\t\t\tif (candidate?.content?.parts) {\n\t\t\t\t\tfor (const part of candidate.content.parts) {\n\t\t\t\t\t\tif (part.text !== undefined) {\n\t\t\t\t\t\t\tconst isThinking = isThinkingPart(part);\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t!currentBlock ||\n\t\t\t\t\t\t\t\t(isThinking && currentBlock.type !== \"thinking\") ||\n\t\t\t\t\t\t\t\t(!isThinking && currentBlock.type !== \"text\")\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\t\t\t\t\t\tcontentIndex: blocks.length - 1,\n\t\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (isThinking) {\n\t\t\t\t\t\t\t\t\tcurrentBlock = { type: \"thinking\", thinking: \"\", thinkingSignature: undefined };\n\t\t\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\t\t\tstream.push({ type: \"thinking_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tcurrentBlock = { type: \"text\", text: \"\" };\n\t\t\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\t\t\tstream.push({ type: \"text_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\t\tcurrentBlock.thinking += part.text;\n\t\t\t\t\t\t\t\tcurrentBlock.thinkingSignature = retainThoughtSignature(\n\t\t\t\t\t\t\t\t\tcurrentBlock.thinkingSignature,\n\t\t\t\t\t\t\t\t\tpart.thoughtSignature,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\tdelta: part.text,\n\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcurrentBlock.text += part.text;\n\t\t\t\t\t\t\t\tcurrentBlock.textSignature = retainThoughtSignature(\n\t\t\t\t\t\t\t\t\tcurrentBlock.textSignature,\n\t\t\t\t\t\t\t\t\tpart.thoughtSignature,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\tdelta: part.text,\n\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (part.functionCall) {\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcurrentBlock = null;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst providedId = part.functionCall.id;\n\t\t\t\t\t\t\tconst needsNewId =\n\t\t\t\t\t\t\t\t!providedId || output.content.some((b) => b.type === \"toolCall\" && b.id === providedId);\n\t\t\t\t\t\t\tconst toolCallId = needsNewId\n\t\t\t\t\t\t\t\t? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`\n\t\t\t\t\t\t\t\t: providedId;\n\n\t\t\t\t\t\t\tconst toolCall: ToolCall = {\n\t\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\t\tid: toolCallId,\n\t\t\t\t\t\t\t\tname: part.functionCall.name || \"\",\n\t\t\t\t\t\t\t\targuments: (part.functionCall.args as Record<string, any>) ?? {},\n\t\t\t\t\t\t\t\t...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),\n\t\t\t\t\t\t\t};\n\n\t\t\t\t\t\t\toutput.content.push(toolCall);\n\t\t\t\t\t\t\tstream.push({ type: \"toolcall_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"toolcall_delta\",\n\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\tdelta: JSON.stringify(toolCall.arguments),\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tstream.push({ type: \"toolcall_end\", contentIndex: blockIndex(), toolCall, partial: output });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (candidate?.finishReason) {\n\t\t\t\t\toutput.stopReason = mapStopReason(candidate.finishReason);\n\t\t\t\t\tif (output.content.some((b) => b.type === \"toolCall\")) {\n\t\t\t\t\t\toutput.stopReason = \"toolUse\";\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (chunk.usageMetadata) {\n\t\t\t\t\toutput.usage = {\n\t\t\t\t\t\tinput:\n\t\t\t\t\t\t\t(chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),\n\t\t\t\t\t\toutput:\n\t\t\t\t\t\t\t(chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),\n\t\t\t\t\t\tcacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,\n\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\ttotalTokens: chunk.usageMetadata.totalTokenCount || 0,\n\t\t\t\t\t\tcost: {\n\t\t\t\t\t\t\tinput: 0,\n\t\t\t\t\t\t\toutput: 0,\n\t\t\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\t\ttotal: 0,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t\tcalculateCost(model, output.usage);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (currentBlock) {\n\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\tstream.push({\n\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tstream.push({\n\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tif (output.stopReason === \"aborted\" || output.stopReason === \"error\") {\n\t\t\t\tthrow new Error(\"An unknown error occurred\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\t// Remove internal index property used during streaming\n\t\t\tfor (const block of output.content) {\n\t\t\t\tif (\"index\" in block) {\n\t\t\t\t\tdelete (block as { index?: number }).index;\n\t\t\t\t}\n\t\t\t}\n\t\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\toutput.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);\n\t\t\tstream.push({ type: \"error\", reason: output.stopReason, error: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nexport const streamSimpleGoogleVertex: StreamFunction<\"google-vertex\", SimpleStreamOptions> = (\n\tmodel: Model<\"google-vertex\">,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tconst base = buildBaseOptions(model, context, options, undefined);\n\tif (!options?.reasoning) {\n\t\treturn streamGoogleVertex(model, context, {\n\t\t\t...base,\n\t\t\tthinking: { enabled: false },\n\t\t} satisfies GoogleVertexOptions);\n\t}\n\n\tconst clampedReasoning = clampThinkingLevel(model, options.reasoning);\n\tconst effort = (clampedReasoning === \"off\" ? \"high\" : clampedReasoning) as ClampedThinkingLevel;\n\tconst geminiModel = model as unknown as Model<\"google-generative-ai\">;\n\n\tif (isGemini3ProModel(geminiModel) || isGemini3FlashModel(geminiModel)) {\n\t\treturn streamGoogleVertex(model, context, {\n\t\t\t...base,\n\t\t\tthinking: {\n\t\t\t\tenabled: true,\n\t\t\t\tlevel: getGemini3ThinkingLevel(effort, geminiModel),\n\t\t\t},\n\t\t} satisfies GoogleVertexOptions);\n\t}\n\n\treturn streamGoogleVertex(model, context, {\n\t\t...base,\n\t\tthinking: {\n\t\t\tenabled: true,\n\t\t\tbudgetTokens: getGoogleBudget(geminiModel, effort, options.thinkingBudgets),\n\t\t},\n\t} satisfies GoogleVertexOptions);\n};\n\nfunction createClient(\n\tmodel: Model<\"google-vertex\">,\n\tproject: string,\n\tlocation: string,\n\toptionsHeaders?: Record<string, string>,\n): GoogleGenAI {\n\treturn new GoogleGenAI({\n\t\tvertexai: true,\n\t\tproject,\n\t\tlocation,\n\t\tapiVersion: API_VERSION,\n\t\thttpOptions: buildHttpOptions(model, optionsHeaders),\n\t});\n}\n\nfunction createClientWithApiKey(\n\tmodel: Model<\"google-vertex\">,\n\tapiKey: string,\n\toptionsHeaders?: Record<string, string>,\n): GoogleGenAI {\n\treturn new GoogleGenAI({\n\t\tvertexai: true,\n\t\tapiKey,\n\t\tapiVersion: API_VERSION,\n\t\thttpOptions: buildHttpOptions(model, optionsHeaders),\n\t});\n}\n\nfunction buildHttpOptions(\n\tmodel: Model<\"google-vertex\">,\n\toptionsHeaders?: Record<string, string>,\n): HttpOptions | undefined {\n\tconst httpOptions: HttpOptions = {};\n\tconst baseUrl = resolveCustomBaseUrl(model.baseUrl);\n\tif (baseUrl) {\n\t\thttpOptions.baseUrl = baseUrl;\n\t\thttpOptions.baseUrlResourceScope = ResourceScope.COLLECTION;\n\t\tif (baseUrlIncludesApiVersion(baseUrl)) {\n\t\t\thttpOptions.apiVersion = \"\";\n\t\t}\n\t}\n\n\tif (model.headers || optionsHeaders) {\n\t\thttpOptions.headers = { ...model.headers, ...optionsHeaders };\n\t}\n\n\treturn Object.keys(httpOptions).length > 0 ? httpOptions : undefined;\n}\n\nfunction resolveCustomBaseUrl(baseUrl: string): string | undefined {\n\tconst trimmed = baseUrl.trim();\n\tif (!trimmed || trimmed.includes(\"{location}\")) {\n\t\treturn undefined;\n\t}\n\treturn trimmed;\n}\n\nfunction baseUrlIncludesApiVersion(baseUrl: string): boolean {\n\ttry {\n\t\tconst url = new URL(baseUrl);\n\t\treturn url.pathname.split(\"/\").some((part) => /^v\\d+(?:beta\\d*)?$/.test(part));\n\t} catch {\n\t\treturn /(?:^|\\/)v\\d+(?:beta\\d*)?(?:\\/|$)/.test(baseUrl);\n\t}\n}\n\nfunction resolveApiKey(options?: GoogleVertexOptions): string | undefined {\n\tconst apiKey = options?.apiKey?.trim() || process.env.GOOGLE_CLOUD_API_KEY?.trim();\n\tif (!apiKey || apiKey === GCP_VERTEX_CREDENTIALS_MARKER || isPlaceholderApiKey(apiKey)) {\n\t\treturn undefined;\n\t}\n\treturn apiKey;\n}\n\nfunction isPlaceholderApiKey(apiKey: string): boolean {\n\treturn /^<[^>]+>$/.test(apiKey);\n}\n\nfunction resolveProject(options?: GoogleVertexOptions): string {\n\tconst project = options?.project || process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT;\n\tif (!project) {\n\t\tthrow new Error(\n\t\t\t\"Vertex AI requires a project ID. Set GOOGLE_CLOUD_PROJECT/GCLOUD_PROJECT or pass project in options.\",\n\t\t);\n\t}\n\treturn project;\n}\n\nfunction resolveLocation(options?: GoogleVertexOptions): string {\n\tconst location = options?.location || process.env.GOOGLE_CLOUD_LOCATION;\n\tif (!location) {\n\t\tthrow new Error(\"Vertex AI requires a location. Set GOOGLE_CLOUD_LOCATION or pass location in options.\");\n\t}\n\treturn location;\n}\n\nfunction buildParams(\n\tmodel: Model<\"google-vertex\">,\n\tcontext: Context,\n\toptions: GoogleVertexOptions = {},\n): GenerateContentParameters {\n\tconst contents = convertMessages(model, context);\n\n\tconst generationConfig: GenerateContentConfig = {};\n\tif (options.temperature !== undefined) {\n\t\tgenerationConfig.temperature = options.temperature;\n\t}\n\tif (options.maxTokens !== undefined) {\n\t\tgenerationConfig.maxOutputTokens = options.maxTokens;\n\t}\n\n\tconst config: GenerateContentConfig = {\n\t\t...(Object.keys(generationConfig).length > 0 && generationConfig),\n\t\t...(context.systemPrompt && { systemInstruction: sanitizeSurrogates(context.systemPrompt) }),\n\t\t...(context.tools && context.tools.length > 0 && { tools: convertTools(context.tools) }),\n\t};\n\n\tif (context.tools && context.tools.length > 0 && options.toolChoice) {\n\t\tconfig.toolConfig = {\n\t\t\tfunctionCallingConfig: {\n\t\t\t\tmode: mapToolChoice(options.toolChoice),\n\t\t\t},\n\t\t};\n\t} else {\n\t\tconfig.toolConfig = undefined;\n\t}\n\n\tif (options.thinking?.enabled && model.reasoning) {\n\t\tconst thinkingConfig: ThinkingConfig = { includeThoughts: true };\n\t\tif (options.thinking.level !== undefined) {\n\t\t\tthinkingConfig.thinkingLevel = THINKING_LEVEL_MAP[options.thinking.level];\n\t\t} else if (options.thinking.budgetTokens !== undefined) {\n\t\t\tthinkingConfig.thinkingBudget = options.thinking.budgetTokens;\n\t\t}\n\t\tconfig.thinkingConfig = thinkingConfig;\n\t} else if (model.reasoning && options.thinking && !options.thinking.enabled) {\n\t\tconfig.thinkingConfig = getDisabledThinkingConfig(model);\n\t}\n\n\tif (options.signal) {\n\t\tif (options.signal.aborted) {\n\t\t\tthrow new Error(\"Request aborted\");\n\t\t}\n\t\tconfig.abortSignal = options.signal;\n\t}\n\n\tconst params: GenerateContentParameters = {\n\t\tmodel: model.id,\n\t\tcontents,\n\t\tconfig,\n\t};\n\n\treturn params;\n}\n\ntype ClampedThinkingLevel = Exclude<PiThinkingLevel, \"xhigh\">;\n\nfunction isGemini3ProModel(model: Model<\"google-generative-ai\">): boolean {\n\treturn /gemini-3(?:\\.\\d+)?-pro/.test(model.id.toLowerCase());\n}\n\nfunction isGemini3FlashModel(model: Model<\"google-generative-ai\">): boolean {\n\treturn /gemini-3(?:\\.\\d+)?-flash/.test(model.id.toLowerCase());\n}\n\nfunction getDisabledThinkingConfig(model: Model<\"google-vertex\">): ThinkingConfig {\n\t// Google docs: Gemini 3.1 Pro cannot disable thinking, and Gemini 3 Flash / Flash-Lite\n\t// do not support full thinking-off either. For Gemini 3 models, use the lowest supported\n\t// thinkingLevel without includeThoughts so hidden thinking remains invisible to pi.\n\tconst geminiModel = model as unknown as Model<\"google-generative-ai\">;\n\tif (isGemini3ProModel(geminiModel)) {\n\t\treturn { thinkingLevel: ThinkingLevel.LOW };\n\t}\n\tif (isGemini3FlashModel(geminiModel)) {\n\t\treturn { thinkingLevel: ThinkingLevel.MINIMAL };\n\t}\n\n\t// Gemini 2.x supports disabling via thinkingBudget = 0.\n\treturn { thinkingBudget: 0 };\n}\n\nfunction getGemini3ThinkingLevel(\n\teffort: ClampedThinkingLevel,\n\tmodel: Model<\"google-generative-ai\">,\n): GoogleThinkingLevel {\n\tif (isGemini3ProModel(model)) {\n\t\tswitch (effort) {\n\t\t\tcase \"minimal\":\n\t\t\tcase \"low\":\n\t\t\t\treturn \"LOW\";\n\t\t\tcase \"medium\":\n\t\t\tcase \"high\":\n\t\t\t\treturn \"HIGH\";\n\t\t}\n\t}\n\tswitch (effort) {\n\t\tcase \"minimal\":\n\t\t\treturn \"MINIMAL\";\n\t\tcase \"low\":\n\t\t\treturn \"LOW\";\n\t\tcase \"medium\":\n\t\t\treturn \"MEDIUM\";\n\t\tcase \"high\":\n\t\t\treturn \"HIGH\";\n\t}\n}\n\nfunction getGoogleBudget(\n\tmodel: Model<\"google-generative-ai\">,\n\teffort: ClampedThinkingLevel,\n\tcustomBudgets?: ThinkingBudgets,\n): number {\n\tif (customBudgets?.[effort] !== undefined) {\n\t\treturn customBudgets[effort]!;\n\t}\n\n\tif (model.id.includes(\"2.5-pro\")) {\n\t\tconst budgets: Record<ClampedThinkingLevel, number> = {\n\t\t\tminimal: 128,\n\t\t\tlow: 2048,\n\t\t\tmedium: 8192,\n\t\t\thigh: 32768,\n\t\t};\n\t\treturn budgets[effort];\n\t}\n\n\tif (model.id.includes(\"2.5-flash\")) {\n\t\tconst budgets: Record<ClampedThinkingLevel, number> = {\n\t\t\tminimal: 128,\n\t\t\tlow: 2048,\n\t\t\tmedium: 8192,\n\t\t\thigh: 24576,\n\t\t};\n\t\treturn budgets[effort];\n\t}\n\n\treturn -1;\n}\n"]}
1
+ {"version":3,"file":"google-vertex.js","sourceRoot":"","sources":["../../src/providers/google-vertex.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,WAAW,EAGX,aAAa,EAEb,aAAa,GACb,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAgBjE,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAOlE,OAAO,EACN,eAAe,EACf,YAAY,EACZ,cAAc,EACd,aAAa,EACb,aAAa,EACb,sBAAsB,EACtB,kBAAkB,GAClB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AASvD,MAAM,WAAW,GAAG,IAAI,CAAC;AACzB,MAAM,6BAA6B,GAAG,wBAAwB,CAAC;AAE/D,MAAM,kBAAkB,GAA+C;IACtE,0BAA0B,EAAE,aAAa,CAAC,0BAA0B;IACpE,OAAO,EAAE,aAAa,CAAC,OAAO;IAC9B,GAAG,EAAE,aAAa,CAAC,GAAG;IACtB,MAAM,EAAE,aAAa,CAAC,MAAM;IAC5B,IAAI,EAAE,aAAa,CAAC,IAAI;CACxB,CAAC;AAIF,SAAS,wBAAwB,CAAC,KAA6B,EAAoB;IAClF,OAAO;QACN,IAAI,E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AAA,CACD;AAED,SAAS,eAAe,CACvB,KAAoC,EACpC,MAA4B,EAC5B,aAA+B,EACtB;IACT,IAAI,aAAa,EAAE,CAAC,MAAM,CAAC,KAAK,SAAS,EAAE,CAAC;QAC3C,OAAO,aAAa,CAAC,MAAM,CAAE,CAAC;IAC/B,CAAC;IAED,IAAI,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QAClC,MAAM,OAAO,GAAyC;YACrD,OAAO,EAAE,GAAG;YACZ,GAAG,EAAE,IAAI;YACT,MAAM,EAAE,IAAI;YACZ,IAAI,EAAE,KAAK;SACX,CAAC;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;IAED,IAAI,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;QACpC,MAAM,OAAO,GAAyC;YACrD,OAAO,EAAE,GAAG;YACZ,GAAG,EAAE,IAAI;YACT,MAAM,EAAE,IAAI;YACZ,IAAI,EAAE,KAAK;SACX,CAAC;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;IAED,OAAO,CAAC,CAAC,CAAC;AAAA,CACV","sourcesContent":["import {\n\ttype Candidate,\n\ttype GenerateContentConfig,\n\ttype GenerateContentParameters,\n\ttype GenerateContentResponse,\n\tGoogleGenAI,\n\ttype HttpOptions,\n\ttype Part,\n\tResourceScope,\n\ttype ThinkingConfig,\n\tThinkingLevel,\n} from \"@google/genai\";\nimport { calculateCost, clampThinkingLevel } from \"../models.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tContext,\n\tModel,\n\tThinkingLevel as PiThinkingLevel,\n\tProviderHeaders,\n\tSimpleStreamOptions,\n\tStreamFunction,\n\tStreamOptions,\n\tTextContent,\n\tThinkingBudgets,\n\tThinkingContent,\n\tToolCall,\n} from \"../types.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { providerHeadersToRecord } from \"../utils/headers.ts\";\nimport { sanitizeSurrogates } from \"../utils/sanitize-unicode.ts\";\nimport type {\n\tGoogleStreamState,\n\tGoogleThinkingLevel,\n\tGoogleThinkingOptions,\n\tGoogleToolChoice,\n} from \"./google-shared.ts\";\nimport {\n\tconvertMessages,\n\tconvertTools,\n\tisThinkingPart,\n\tmapStopReason,\n\tmapToolChoice,\n\tretainThoughtSignature,\n\tretryGoogleRequest,\n} from \"./google-shared.ts\";\nimport { buildBaseOptions } from \"./simple-options.ts\";\n\nexport interface GoogleVertexOptions extends StreamOptions {\n\ttoolChoice?: GoogleToolChoice;\n\tthinking?: GoogleThinkingOptions;\n\tproject?: string;\n\tlocation?: string;\n}\n\nconst API_VERSION = \"v1\";\nconst GCP_VERTEX_CREDENTIALS_MARKER = \"gcp-vertex-credentials\";\n\nconst THINKING_LEVEL_MAP: Record<GoogleThinkingLevel, ThinkingLevel> = {\n\tTHINKING_LEVEL_UNSPECIFIED: ThinkingLevel.THINKING_LEVEL_UNSPECIFIED,\n\tMINIMAL: ThinkingLevel.MINIMAL,\n\tLOW: ThinkingLevel.LOW,\n\tMEDIUM: ThinkingLevel.MEDIUM,\n\tHIGH: ThinkingLevel.HIGH,\n};\n\ntype GoogleVertexStreamState = GoogleStreamState;\n\nfunction createGoogleVertexOutput(model: Model<\"google-vertex\">): AssistantMessage {\n\treturn {\n\t\trole: \"assistant\",\n\t\tcontent: [],\n\t\tapi: \"google-vertex\" as Api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\tusage: {\n\t\t\tinput: 0,\n\t\t\toutput: 0,\n\t\t\tcacheRead: 0,\n\t\t\tcacheWrite: 0,\n\t\t\ttotalTokens: 0,\n\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t},\n\t\tstopReason: \"stop\",\n\t\ttimestamp: Date.now(),\n\t};\n}\n\nfunction googleVertexBlockIndex(state: GoogleVertexStreamState): number {\n\treturn state.output.content.length - 1;\n}\n\nfunction endGoogleVertexBlock(state: GoogleVertexStreamState): void {\n\tconst block = state.currentBlock;\n\tif (!block) return;\n\tif (block.type === \"text\") {\n\t\tstate.stream.push({\n\t\t\ttype: \"text_end\",\n\t\t\tcontentIndex: googleVertexBlockIndex(state),\n\t\t\tcontent: block.text,\n\t\t\tpartial: state.output,\n\t\t});\n\t} else {\n\t\tstate.stream.push({\n\t\t\ttype: \"thinking_end\",\n\t\t\tcontentIndex: googleVertexBlockIndex(state),\n\t\t\tcontent: block.thinking,\n\t\t\tpartial: state.output,\n\t\t});\n\t}\n\tstate.currentBlock = null;\n}\n\nfunction ensureGoogleVertexTextBlock(\n\tstate: GoogleVertexStreamState,\n\tisThinking: boolean,\n): TextContent | ThinkingContent {\n\tconst current = state.currentBlock;\n\tif (current && ((isThinking && current.type === \"thinking\") || (!isThinking && current.type === \"text\"))) {\n\t\treturn current;\n\t}\n\tendGoogleVertexBlock(state);\n\tif (isThinking) {\n\t\tconst block: ThinkingContent = { type: \"thinking\", thinking: \"\", thinkingSignature: undefined };\n\t\tstate.currentBlock = block;\n\t\tstate.output.content.push(block);\n\t\tstate.stream.push({ type: \"thinking_start\", contentIndex: googleVertexBlockIndex(state), partial: state.output });\n\t\treturn block;\n\t}\n\tconst block: TextContent = { type: \"text\", text: \"\" };\n\tstate.currentBlock = block;\n\tstate.output.content.push(block);\n\tstate.stream.push({ type: \"text_start\", contentIndex: googleVertexBlockIndex(state), partial: state.output });\n\treturn block;\n}\n\nfunction appendGoogleVertexTextPart(state: GoogleVertexStreamState, part: Part): void {\n\tif (part.text === undefined) return;\n\tconst block = ensureGoogleVertexTextBlock(state, isThinkingPart(part));\n\tif (block.type === \"thinking\") {\n\t\tblock.thinking += part.text;\n\t\tblock.thinkingSignature = retainThoughtSignature(block.thinkingSignature, part.thoughtSignature);\n\t\tstate.stream.push({\n\t\t\ttype: \"thinking_delta\",\n\t\t\tcontentIndex: googleVertexBlockIndex(state),\n\t\t\tdelta: part.text,\n\t\t\tpartial: state.output,\n\t\t});\n\t\treturn;\n\t}\n\tblock.text += part.text;\n\tblock.textSignature = retainThoughtSignature(block.textSignature, part.thoughtSignature);\n\tstate.stream.push({\n\t\ttype: \"text_delta\",\n\t\tcontentIndex: googleVertexBlockIndex(state),\n\t\tdelta: part.text,\n\t\tpartial: state.output,\n\t});\n}\n\nfunction appendGoogleVertexToolCall(state: GoogleVertexStreamState, part: Part): void {\n\tif (!part.functionCall) return;\n\tendGoogleVertexBlock(state);\n\tconst providedId = part.functionCall.id;\n\tconst needsNewId =\n\t\t!providedId || state.output.content.some((block) => block.type === \"toolCall\" && block.id === providedId);\n\tconst toolCall: ToolCall = {\n\t\ttype: \"toolCall\",\n\t\tid: needsNewId ? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}` : providedId,\n\t\tname: part.functionCall.name || \"\",\n\t\targuments: (part.functionCall.args as Record<string, any>) ?? {},\n\t\t...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),\n\t};\n\tstate.output.content.push(toolCall);\n\tstate.stream.push({ type: \"toolcall_start\", contentIndex: googleVertexBlockIndex(state), partial: state.output });\n\tstate.stream.push({\n\t\ttype: \"toolcall_delta\",\n\t\tcontentIndex: googleVertexBlockIndex(state),\n\t\tdelta: JSON.stringify(toolCall.arguments),\n\t\tpartial: state.output,\n\t});\n\tstate.stream.push({\n\t\ttype: \"toolcall_end\",\n\t\tcontentIndex: googleVertexBlockIndex(state),\n\t\ttoolCall,\n\t\tpartial: state.output,\n\t});\n}\n\nfunction applyGoogleVertexFinishReason(output: AssistantMessage, candidate: Candidate | undefined): void {\n\tif (!candidate?.finishReason) return;\n\toutput.stopReason = mapStopReason(candidate.finishReason);\n\tif (output.content.some((block) => block.type === \"toolCall\")) output.stopReason = \"toolUse\";\n}\n\nfunction applyGoogleVertexUsage(\n\toutput: AssistantMessage,\n\tchunk: GenerateContentResponse,\n\tmodel: Model<\"google-vertex\">,\n): void {\n\tif (!chunk.usageMetadata) return;\n\toutput.usage = {\n\t\tinput: (chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),\n\t\toutput: (chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),\n\t\tcacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,\n\t\tcacheWrite: 0,\n\t\ttotalTokens: chunk.usageMetadata.totalTokenCount || 0,\n\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t};\n\tcalculateCost(model, output.usage);\n}\n\nfunction processGoogleVertexChunk(\n\tstate: GoogleVertexStreamState,\n\tchunk: GenerateContentResponse,\n\tmodel: Model<\"google-vertex\">,\n): void {\n\tstate.output.responseId ||= chunk.responseId;\n\tconst candidate = chunk.candidates?.[0];\n\tfor (const part of candidate?.content?.parts ?? []) {\n\t\tappendGoogleVertexTextPart(state, part);\n\t\tappendGoogleVertexToolCall(state, part);\n\t}\n\tapplyGoogleVertexFinishReason(state.output, candidate);\n\tapplyGoogleVertexUsage(state.output, chunk, model);\n}\n\nfunction completeGoogleVertexStream(state: GoogleVertexStreamState, signal: AbortSignal | undefined): void {\n\tendGoogleVertexBlock(state);\n\tif (signal?.aborted) throw new Error(\"Request was aborted\");\n\tif (state.output.stopReason === \"pending\") {\n\t\tthrow new Error(\"Google Vertex stream ended without a finish reason\");\n\t}\n\tif (state.output.stopReason === \"aborted\" || state.output.stopReason === \"error\") {\n\t\tthrow new Error(\"An unknown error occurred\");\n\t}\n\tstate.stream.push({ type: \"done\", reason: state.output.stopReason, message: state.output });\n\tstate.stream.end();\n}\n\nfunction failGoogleVertexStream(state: GoogleVertexStreamState, error: unknown, signal: AbortSignal | undefined): void {\n\tfor (const block of state.output.content) {\n\t\tif (\"index\" in block) delete (block as { index?: number }).index;\n\t}\n\tstate.output.stopReason = signal?.aborted ? \"aborted\" : \"error\";\n\tstate.output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);\n\tstate.stream.push({ type: \"error\", reason: state.output.stopReason, error: state.output });\n\tstate.stream.end();\n}\n\nasync function runGoogleVertexStream(\n\tmodel: Model<\"google-vertex\">,\n\tcontext: Context,\n\toptions: GoogleVertexOptions | undefined,\n\tstate: GoogleVertexStreamState,\n): Promise<void> {\n\ttry {\n\t\tconst apiKey = resolveApiKey(options);\n\t\tconst client = apiKey\n\t\t\t? createClientWithApiKey(model, apiKey, options?.headers)\n\t\t\t: createClient(model, resolveProject(options), resolveLocation(options), options?.headers);\n\t\tlet params = buildParams(model, context, options);\n\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\tif (nextParams !== undefined) params = nextParams as GenerateContentParameters;\n\t\tconst googleStream = await retryGoogleRequest(() => client.models.generateContentStream(params), options);\n\t\tstate.stream.push({ type: \"start\", partial: state.output });\n\t\tfor await (const chunk of googleStream) processGoogleVertexChunk(state, chunk, model);\n\t\tcompleteGoogleVertexStream(state, options?.signal);\n\t} catch (error) {\n\t\tfailGoogleVertexStream(state, error, options?.signal);\n\t}\n}\n\n// Counter for generating unique tool call IDs\nlet toolCallCounter = 0;\n\nexport const streamGoogleVertex: StreamFunction<\"google-vertex\", GoogleVertexOptions> = (\n\tmodel: Model<\"google-vertex\">,\n\tcontext: Context,\n\toptions?: GoogleVertexOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\tconst state: GoogleVertexStreamState = {\n\t\toutput: createGoogleVertexOutput(model),\n\t\tstream,\n\t\tcurrentBlock: null,\n\t};\n\tvoid runGoogleVertexStream(model, context, options, state);\n\treturn stream;\n};\n\nexport const streamSimpleGoogleVertex: StreamFunction<\"google-vertex\", SimpleStreamOptions> = (\n\tmodel: Model<\"google-vertex\">,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tconst base = buildBaseOptions(model, context, options, undefined);\n\tif (!options?.reasoning) {\n\t\treturn streamGoogleVertex(model, context, {\n\t\t\t...base,\n\t\t\tthinking: { enabled: false },\n\t\t} satisfies GoogleVertexOptions);\n\t}\n\n\tconst clampedReasoning = clampThinkingLevel(model, options.reasoning);\n\tconst effort = (clampedReasoning === \"off\" ? \"high\" : clampedReasoning) as ClampedThinkingLevel;\n\tconst geminiModel = model as unknown as Model<\"google-generative-ai\">;\n\n\tif (isGemini3ProModel(geminiModel) || isGemini3FlashModel(geminiModel)) {\n\t\treturn streamGoogleVertex(model, context, {\n\t\t\t...base,\n\t\t\tthinking: {\n\t\t\t\tenabled: true,\n\t\t\t\tlevel: getGemini3ThinkingLevel(effort, geminiModel),\n\t\t\t},\n\t\t} satisfies GoogleVertexOptions);\n\t}\n\n\treturn streamGoogleVertex(model, context, {\n\t\t...base,\n\t\tthinking: {\n\t\t\tenabled: true,\n\t\t\tbudgetTokens: getGoogleBudget(geminiModel, effort, options.thinkingBudgets),\n\t\t},\n\t} satisfies GoogleVertexOptions);\n};\n\nfunction createClient(\n\tmodel: Model<\"google-vertex\">,\n\tproject: string,\n\tlocation: string,\n\toptionsHeaders?: ProviderHeaders,\n): GoogleGenAI {\n\treturn new GoogleGenAI({\n\t\tvertexai: true,\n\t\tproject,\n\t\tlocation,\n\t\tapiVersion: API_VERSION,\n\t\thttpOptions: buildHttpOptions(model, optionsHeaders),\n\t});\n}\n\nfunction createClientWithApiKey(\n\tmodel: Model<\"google-vertex\">,\n\tapiKey: string,\n\toptionsHeaders?: ProviderHeaders,\n): GoogleGenAI {\n\treturn new GoogleGenAI({\n\t\tvertexai: true,\n\t\tapiKey,\n\t\tapiVersion: API_VERSION,\n\t\thttpOptions: buildHttpOptions(model, optionsHeaders),\n\t});\n}\n\nfunction buildHttpOptions(model: Model<\"google-vertex\">, optionsHeaders?: ProviderHeaders): HttpOptions | undefined {\n\tconst httpOptions: HttpOptions = {};\n\tconst baseUrl = resolveCustomBaseUrl(model.baseUrl);\n\tif (baseUrl) {\n\t\thttpOptions.baseUrl = baseUrl;\n\t\thttpOptions.baseUrlResourceScope = ResourceScope.COLLECTION;\n\t\tif (baseUrlIncludesApiVersion(baseUrl)) {\n\t\t\thttpOptions.apiVersion = \"\";\n\t\t}\n\t}\n\n\tconst headers = providerHeadersToRecord({ ...model.headers, ...optionsHeaders });\n\tif (headers) {\n\t\thttpOptions.headers = headers;\n\t}\n\n\treturn Object.keys(httpOptions).length > 0 ? httpOptions : undefined;\n}\n\nfunction resolveCustomBaseUrl(baseUrl: string): string | undefined {\n\tconst trimmed = baseUrl.trim();\n\tif (!trimmed || trimmed.includes(\"{location}\")) {\n\t\treturn undefined;\n\t}\n\treturn trimmed;\n}\n\nfunction baseUrlIncludesApiVersion(baseUrl: string): boolean {\n\ttry {\n\t\tconst url = new URL(baseUrl);\n\t\treturn url.pathname.split(\"/\").some((part) => /^v\\d+(?:beta\\d*)?$/.test(part));\n\t} catch {\n\t\treturn /(?:^|\\/)v\\d+(?:beta\\d*)?(?:\\/|$)/.test(baseUrl);\n\t}\n}\n\nfunction resolveApiKey(options?: GoogleVertexOptions): string | undefined {\n\tconst apiKey = options?.apiKey?.trim() || process.env.GOOGLE_CLOUD_API_KEY?.trim();\n\tif (!apiKey || apiKey === GCP_VERTEX_CREDENTIALS_MARKER || isPlaceholderApiKey(apiKey)) {\n\t\treturn undefined;\n\t}\n\treturn apiKey;\n}\n\nfunction isPlaceholderApiKey(apiKey: string): boolean {\n\treturn /^<[^>]+>$/.test(apiKey);\n}\n\nfunction resolveProject(options?: GoogleVertexOptions): string {\n\tconst project = options?.project || process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT;\n\tif (!project) {\n\t\tthrow new Error(\n\t\t\t\"Vertex AI requires a project ID. Set GOOGLE_CLOUD_PROJECT/GCLOUD_PROJECT or pass project in options.\",\n\t\t);\n\t}\n\treturn project;\n}\n\nfunction resolveLocation(options?: GoogleVertexOptions): string {\n\tconst location = options?.location || process.env.GOOGLE_CLOUD_LOCATION;\n\tif (!location) {\n\t\tthrow new Error(\"Vertex AI requires a location. Set GOOGLE_CLOUD_LOCATION or pass location in options.\");\n\t}\n\treturn location;\n}\n\nfunction resolveGoogleVertexThinkingConfig(\n\tmodel: Model<\"google-vertex\">,\n\tthinking: GoogleThinkingOptions | undefined,\n): ThinkingConfig | undefined {\n\tif (thinking?.enabled && model.reasoning) {\n\t\tconst config: ThinkingConfig = { includeThoughts: true };\n\t\tif (thinking.level !== undefined) {\n\t\t\tconfig.thinkingLevel = THINKING_LEVEL_MAP[thinking.level];\n\t\t} else if (thinking.budgetTokens !== undefined) {\n\t\t\tconfig.thinkingBudget = thinking.budgetTokens;\n\t\t}\n\t\treturn config;\n\t}\n\tif (model.reasoning && thinking && !thinking.enabled) return getDisabledThinkingConfig(model);\n\treturn undefined;\n}\n\nfunction buildParams(\n\tmodel: Model<\"google-vertex\">,\n\tcontext: Context,\n\toptions: GoogleVertexOptions = {},\n): GenerateContentParameters {\n\tconst contents = convertMessages(model, context);\n\n\tconst generationConfig: GenerateContentConfig = {};\n\tif (options.temperature !== undefined) {\n\t\tgenerationConfig.temperature = options.temperature;\n\t}\n\tif (options.maxTokens !== undefined) {\n\t\tgenerationConfig.maxOutputTokens = options.maxTokens;\n\t}\n\n\tconst config: GenerateContentConfig = {\n\t\t...(Object.keys(generationConfig).length > 0 && generationConfig),\n\t\t...(context.systemPrompt && { systemInstruction: sanitizeSurrogates(context.systemPrompt) }),\n\t\t...(context.tools && context.tools.length > 0 && { tools: convertTools(context.tools) }),\n\t};\n\n\tif (context.tools && context.tools.length > 0 && options.toolChoice) {\n\t\tconfig.toolConfig = {\n\t\t\tfunctionCallingConfig: {\n\t\t\t\tmode: mapToolChoice(options.toolChoice),\n\t\t\t},\n\t\t};\n\t} else {\n\t\tconfig.toolConfig = undefined;\n\t}\n\n\tconst thinkingConfig = resolveGoogleVertexThinkingConfig(model, options.thinking);\n\tif (thinkingConfig) config.thinkingConfig = thinkingConfig;\n\n\tif (options.signal) {\n\t\tif (options.signal.aborted) {\n\t\t\tthrow new Error(\"Request aborted\");\n\t\t}\n\t\tconfig.abortSignal = options.signal;\n\t}\n\n\tconst params: GenerateContentParameters = {\n\t\tmodel: model.id,\n\t\tcontents,\n\t\tconfig,\n\t};\n\n\treturn params;\n}\n\ntype ClampedThinkingLevel = Exclude<PiThinkingLevel, \"xhigh\">;\n\nfunction isGemini3ProModel(model: Model<\"google-generative-ai\">): boolean {\n\treturn /gemini-3(?:\\.\\d+)?-pro/.test(model.id.toLowerCase());\n}\n\nfunction isGemini3FlashModel(model: Model<\"google-generative-ai\">): boolean {\n\treturn /gemini-3(?:\\.\\d+)?-flash/.test(model.id.toLowerCase());\n}\n\nfunction getDisabledThinkingConfig(model: Model<\"google-vertex\">): ThinkingConfig {\n\t// Google docs: Gemini 3.1 Pro cannot disable thinking, and Gemini 3 Flash / Flash-Lite\n\t// do not support full thinking-off either. For Gemini 3 models, use the lowest supported\n\t// thinkingLevel without includeThoughts so hidden thinking remains invisible to pi.\n\tconst geminiModel = model as unknown as Model<\"google-generative-ai\">;\n\tif (isGemini3ProModel(geminiModel)) {\n\t\treturn { thinkingLevel: ThinkingLevel.LOW };\n\t}\n\tif (isGemini3FlashModel(geminiModel)) {\n\t\treturn { thinkingLevel: ThinkingLevel.MINIMAL };\n\t}\n\n\t// Gemini 2.x supports disabling via thinkingBudget = 0.\n\treturn { thinkingBudget: 0 };\n}\n\nfunction getGemini3ThinkingLevel(\n\teffort: ClampedThinkingLevel,\n\tmodel: Model<\"google-generative-ai\">,\n): GoogleThinkingLevel {\n\tif (isGemini3ProModel(model)) {\n\t\tswitch (effort) {\n\t\t\tcase \"minimal\":\n\t\t\tcase \"low\":\n\t\t\t\treturn \"LOW\";\n\t\t\tcase \"medium\":\n\t\t\tcase \"high\":\n\t\t\t\treturn \"HIGH\";\n\t\t}\n\t}\n\tswitch (effort) {\n\t\tcase \"minimal\":\n\t\t\treturn \"MINIMAL\";\n\t\tcase \"low\":\n\t\t\treturn \"LOW\";\n\t\tcase \"medium\":\n\t\t\treturn \"MEDIUM\";\n\t\tcase \"high\":\n\t\t\treturn \"HIGH\";\n\t}\n}\n\nfunction getGoogleBudget(\n\tmodel: Model<\"google-generative-ai\">,\n\teffort: ClampedThinkingLevel,\n\tcustomBudgets?: ThinkingBudgets,\n): number {\n\tif (customBudgets?.[effort] !== undefined) {\n\t\treturn customBudgets[effort]!;\n\t}\n\n\tif (model.id.includes(\"2.5-pro\")) {\n\t\tconst budgets: Record<ClampedThinkingLevel, number> = {\n\t\t\tminimal: 128,\n\t\t\tlow: 2048,\n\t\t\tmedium: 8192,\n\t\t\thigh: 32768,\n\t\t};\n\t\treturn budgets[effort];\n\t}\n\n\tif (model.id.includes(\"2.5-flash\")) {\n\t\tconst budgets: Record<ClampedThinkingLevel, number> = {\n\t\t\tminimal: 128,\n\t\t\tlow: 2048,\n\t\t\tmedium: 8192,\n\t\t\thigh: 24576,\n\t\t};\n\t\treturn budgets[effort];\n\t}\n\n\treturn -1;\n}\n"]}
@@ -1,12 +1,9 @@
1
1
  import type { SimpleStreamOptions, StreamFunction, StreamOptions } from "../types.ts";
2
- import type { GoogleThinkingLevel } from "./google-shared.ts";
2
+ import type { GoogleThinkingOptions, GoogleToolChoice } from "./google-shared.ts";
3
+ export type { GoogleThinkingOptions } from "./google-shared.ts";
3
4
  export interface GoogleOptions extends StreamOptions {
4
- toolChoice?: "auto" | "none" | "any";
5
- thinking?: {
6
- enabled: boolean;
7
- budgetTokens?: number;
8
- level?: GoogleThinkingLevel;
9
- };
5
+ toolChoice?: GoogleToolChoice;
6
+ thinking?: GoogleThinkingOptions;
10
7
  }
11
8
  export declare const streamGoogle: StreamFunction<"google-generative-ai", GoogleOptions>;
12
9
  export declare const streamSimpleGoogle: StreamFunction<"google-generative-ai", SimpleStreamOptions>;