@avadisabelle/ava-pi-ai 0.61.1

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 (170) hide show
  1. package/README.md +1229 -0
  2. package/dist/api-registry.d.ts +20 -0
  3. package/dist/api-registry.d.ts.map +1 -0
  4. package/dist/api-registry.js +44 -0
  5. package/dist/api-registry.js.map +1 -0
  6. package/dist/bedrock-provider.d.ts +5 -0
  7. package/dist/bedrock-provider.d.ts.map +1 -0
  8. package/dist/bedrock-provider.js +6 -0
  9. package/dist/bedrock-provider.js.map +1 -0
  10. package/dist/cli.d.ts +3 -0
  11. package/dist/cli.d.ts.map +1 -0
  12. package/dist/cli.js +116 -0
  13. package/dist/cli.js.map +1 -0
  14. package/dist/env-api-keys.d.ts +9 -0
  15. package/dist/env-api-keys.d.ts.map +1 -0
  16. package/dist/env-api-keys.js +107 -0
  17. package/dist/env-api-keys.js.map +1 -0
  18. package/dist/index.d.ts +24 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +13 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/models.d.ts +24 -0
  23. package/dist/models.d.ts.map +1 -0
  24. package/dist/models.generated.d.ts +14368 -0
  25. package/dist/models.generated.d.ts.map +1 -0
  26. package/dist/models.generated.js +14153 -0
  27. package/dist/models.generated.js.map +1 -0
  28. package/dist/models.js +55 -0
  29. package/dist/models.js.map +1 -0
  30. package/dist/oauth.d.ts +2 -0
  31. package/dist/oauth.d.ts.map +1 -0
  32. package/dist/oauth.js +2 -0
  33. package/dist/oauth.js.map +1 -0
  34. package/dist/providers/amazon-bedrock.d.ts +15 -0
  35. package/dist/providers/amazon-bedrock.d.ts.map +1 -0
  36. package/dist/providers/amazon-bedrock.js +622 -0
  37. package/dist/providers/amazon-bedrock.js.map +1 -0
  38. package/dist/providers/anthropic.d.ts +40 -0
  39. package/dist/providers/anthropic.d.ts.map +1 -0
  40. package/dist/providers/anthropic.js +743 -0
  41. package/dist/providers/anthropic.js.map +1 -0
  42. package/dist/providers/azure-openai-responses.d.ts +15 -0
  43. package/dist/providers/azure-openai-responses.d.ts.map +1 -0
  44. package/dist/providers/azure-openai-responses.js +187 -0
  45. package/dist/providers/azure-openai-responses.js.map +1 -0
  46. package/dist/providers/github-copilot-headers.d.ts +8 -0
  47. package/dist/providers/github-copilot-headers.d.ts.map +1 -0
  48. package/dist/providers/github-copilot-headers.js +29 -0
  49. package/dist/providers/github-copilot-headers.js.map +1 -0
  50. package/dist/providers/google-gemini-cli.d.ts +74 -0
  51. package/dist/providers/google-gemini-cli.d.ts.map +1 -0
  52. package/dist/providers/google-gemini-cli.js +760 -0
  53. package/dist/providers/google-gemini-cli.js.map +1 -0
  54. package/dist/providers/google-shared.d.ts +65 -0
  55. package/dist/providers/google-shared.d.ts.map +1 -0
  56. package/dist/providers/google-shared.js +312 -0
  57. package/dist/providers/google-shared.js.map +1 -0
  58. package/dist/providers/google-vertex.d.ts +15 -0
  59. package/dist/providers/google-vertex.d.ts.map +1 -0
  60. package/dist/providers/google-vertex.js +402 -0
  61. package/dist/providers/google-vertex.js.map +1 -0
  62. package/dist/providers/google.d.ts +13 -0
  63. package/dist/providers/google.d.ts.map +1 -0
  64. package/dist/providers/google.js +358 -0
  65. package/dist/providers/google.js.map +1 -0
  66. package/dist/providers/mistral.d.ts +22 -0
  67. package/dist/providers/mistral.d.ts.map +1 -0
  68. package/dist/providers/mistral.js +501 -0
  69. package/dist/providers/mistral.js.map +1 -0
  70. package/dist/providers/openai-codex-responses.d.ts +9 -0
  71. package/dist/providers/openai-codex-responses.d.ts.map +1 -0
  72. package/dist/providers/openai-codex-responses.js +741 -0
  73. package/dist/providers/openai-codex-responses.js.map +1 -0
  74. package/dist/providers/openai-completions.d.ts +15 -0
  75. package/dist/providers/openai-completions.d.ts.map +1 -0
  76. package/dist/providers/openai-completions.js +736 -0
  77. package/dist/providers/openai-completions.js.map +1 -0
  78. package/dist/providers/openai-responses-shared.d.ts +17 -0
  79. package/dist/providers/openai-responses-shared.d.ts.map +1 -0
  80. package/dist/providers/openai-responses-shared.js +453 -0
  81. package/dist/providers/openai-responses-shared.js.map +1 -0
  82. package/dist/providers/openai-responses.d.ts +13 -0
  83. package/dist/providers/openai-responses.d.ts.map +1 -0
  84. package/dist/providers/openai-responses.js +201 -0
  85. package/dist/providers/openai-responses.js.map +1 -0
  86. package/dist/providers/register-builtins.d.ts +38 -0
  87. package/dist/providers/register-builtins.d.ts.map +1 -0
  88. package/dist/providers/register-builtins.js +261 -0
  89. package/dist/providers/register-builtins.js.map +1 -0
  90. package/dist/providers/simple-options.d.ts +8 -0
  91. package/dist/providers/simple-options.d.ts.map +1 -0
  92. package/dist/providers/simple-options.js +35 -0
  93. package/dist/providers/simple-options.js.map +1 -0
  94. package/dist/providers/transform-messages.d.ts +8 -0
  95. package/dist/providers/transform-messages.d.ts.map +1 -0
  96. package/dist/providers/transform-messages.js +155 -0
  97. package/dist/providers/transform-messages.js.map +1 -0
  98. package/dist/stream.d.ts +8 -0
  99. package/dist/stream.d.ts.map +1 -0
  100. package/dist/stream.js +27 -0
  101. package/dist/stream.js.map +1 -0
  102. package/dist/types.d.ts +294 -0
  103. package/dist/types.d.ts.map +1 -0
  104. package/dist/types.js +2 -0
  105. package/dist/types.js.map +1 -0
  106. package/dist/utils/event-stream.d.ts +21 -0
  107. package/dist/utils/event-stream.d.ts.map +1 -0
  108. package/dist/utils/event-stream.js +81 -0
  109. package/dist/utils/event-stream.js.map +1 -0
  110. package/dist/utils/hash.d.ts +3 -0
  111. package/dist/utils/hash.d.ts.map +1 -0
  112. package/dist/utils/hash.js +14 -0
  113. package/dist/utils/hash.js.map +1 -0
  114. package/dist/utils/json-parse.d.ts +9 -0
  115. package/dist/utils/json-parse.d.ts.map +1 -0
  116. package/dist/utils/json-parse.js +29 -0
  117. package/dist/utils/json-parse.js.map +1 -0
  118. package/dist/utils/oauth/anthropic.d.ts +25 -0
  119. package/dist/utils/oauth/anthropic.d.ts.map +1 -0
  120. package/dist/utils/oauth/anthropic.js +335 -0
  121. package/dist/utils/oauth/anthropic.js.map +1 -0
  122. package/dist/utils/oauth/github-copilot.d.ts +30 -0
  123. package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
  124. package/dist/utils/oauth/github-copilot.js +292 -0
  125. package/dist/utils/oauth/github-copilot.js.map +1 -0
  126. package/dist/utils/oauth/google-antigravity.d.ts +26 -0
  127. package/dist/utils/oauth/google-antigravity.d.ts.map +1 -0
  128. package/dist/utils/oauth/google-antigravity.js +376 -0
  129. package/dist/utils/oauth/google-antigravity.js.map +1 -0
  130. package/dist/utils/oauth/google-gemini-cli.d.ts +26 -0
  131. package/dist/utils/oauth/google-gemini-cli.d.ts.map +1 -0
  132. package/dist/utils/oauth/google-gemini-cli.js +481 -0
  133. package/dist/utils/oauth/google-gemini-cli.js.map +1 -0
  134. package/dist/utils/oauth/index.d.ts +61 -0
  135. package/dist/utils/oauth/index.d.ts.map +1 -0
  136. package/dist/utils/oauth/index.js +131 -0
  137. package/dist/utils/oauth/index.js.map +1 -0
  138. package/dist/utils/oauth/oauth-page.d.ts +3 -0
  139. package/dist/utils/oauth/oauth-page.d.ts.map +1 -0
  140. package/dist/utils/oauth/oauth-page.js +105 -0
  141. package/dist/utils/oauth/oauth-page.js.map +1 -0
  142. package/dist/utils/oauth/openai-codex.d.ts +34 -0
  143. package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
  144. package/dist/utils/oauth/openai-codex.js +373 -0
  145. package/dist/utils/oauth/openai-codex.js.map +1 -0
  146. package/dist/utils/oauth/pkce.d.ts +13 -0
  147. package/dist/utils/oauth/pkce.d.ts.map +1 -0
  148. package/dist/utils/oauth/pkce.js +31 -0
  149. package/dist/utils/oauth/pkce.js.map +1 -0
  150. package/dist/utils/oauth/types.d.ts +47 -0
  151. package/dist/utils/oauth/types.d.ts.map +1 -0
  152. package/dist/utils/oauth/types.js +2 -0
  153. package/dist/utils/oauth/types.js.map +1 -0
  154. package/dist/utils/overflow.d.ts +52 -0
  155. package/dist/utils/overflow.d.ts.map +1 -0
  156. package/dist/utils/overflow.js +117 -0
  157. package/dist/utils/overflow.js.map +1 -0
  158. package/dist/utils/sanitize-unicode.d.ts +22 -0
  159. package/dist/utils/sanitize-unicode.d.ts.map +1 -0
  160. package/dist/utils/sanitize-unicode.js +26 -0
  161. package/dist/utils/sanitize-unicode.js.map +1 -0
  162. package/dist/utils/typebox-helpers.d.ts +17 -0
  163. package/dist/utils/typebox-helpers.d.ts.map +1 -0
  164. package/dist/utils/typebox-helpers.js +21 -0
  165. package/dist/utils/typebox-helpers.js.map +1 -0
  166. package/dist/utils/validation.d.ts +18 -0
  167. package/dist/utils/validation.d.ts.map +1 -0
  168. package/dist/utils/validation.js +80 -0
  169. package/dist/utils/validation.js.map +1 -0
  170. package/package.json +114 -0
@@ -0,0 +1,760 @@
1
+ /**
2
+ * Google Gemini CLI / Antigravity provider.
3
+ * Shared implementation for both google-gemini-cli and google-antigravity providers.
4
+ * Uses the Cloud Code Assist API endpoint to access Gemini and Claude models.
5
+ */
6
+ import { calculateCost } from "../models.js";
7
+ import { AssistantMessageEventStream } from "../utils/event-stream.js";
8
+ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
9
+ import { convertMessages, convertTools, isThinkingPart, mapStopReasonString, mapToolChoice, retainThoughtSignature, } from "./google-shared.js";
10
+ import { buildBaseOptions, clampReasoning } from "./simple-options.js";
11
+ const DEFAULT_ENDPOINT = "https://cloudcode-pa.googleapis.com";
12
+ const ANTIGRAVITY_DAILY_ENDPOINT = "https://daily-cloudcode-pa.sandbox.googleapis.com";
13
+ const ANTIGRAVITY_AUTOPUSH_ENDPOINT = "https://autopush-cloudcode-pa.sandbox.googleapis.com";
14
+ const ANTIGRAVITY_ENDPOINT_FALLBACKS = [
15
+ ANTIGRAVITY_DAILY_ENDPOINT,
16
+ ANTIGRAVITY_AUTOPUSH_ENDPOINT,
17
+ DEFAULT_ENDPOINT,
18
+ ];
19
+ // Headers for Gemini CLI (prod endpoint)
20
+ const GEMINI_CLI_HEADERS = {
21
+ "User-Agent": "google-cloud-sdk vscode_cloudshelleditor/0.1",
22
+ "X-Goog-Api-Client": "gl-node/22.17.0",
23
+ "Client-Metadata": JSON.stringify({
24
+ ideType: "IDE_UNSPECIFIED",
25
+ platform: "PLATFORM_UNSPECIFIED",
26
+ pluginType: "GEMINI",
27
+ }),
28
+ };
29
+ // Headers for Antigravity (sandbox endpoint) - requires specific User-Agent
30
+ const DEFAULT_ANTIGRAVITY_VERSION = "1.18.4";
31
+ function getAntigravityHeaders() {
32
+ const version = process.env.PI_AI_ANTIGRAVITY_VERSION || DEFAULT_ANTIGRAVITY_VERSION;
33
+ return {
34
+ "User-Agent": `antigravity/${version} darwin/arm64`,
35
+ };
36
+ }
37
+ // Antigravity system instruction (compact version from CLIProxyAPI).
38
+ const ANTIGRAVITY_SYSTEM_INSTRUCTION = "You are Antigravity, a powerful agentic AI coding assistant designed by the Google Deepmind team working on Advanced Agentic Coding." +
39
+ "You are pair programming with a USER to solve their coding task. The task may require creating a new codebase, modifying or debugging an existing codebase, or simply answering a question." +
40
+ "**Absolute paths only**" +
41
+ "**Proactiveness**";
42
+ // Counter for generating unique tool call IDs
43
+ let toolCallCounter = 0;
44
+ // Retry configuration
45
+ const MAX_RETRIES = 3;
46
+ const BASE_DELAY_MS = 1000;
47
+ const MAX_EMPTY_STREAM_RETRIES = 2;
48
+ const EMPTY_STREAM_BASE_DELAY_MS = 500;
49
+ const CLAUDE_THINKING_BETA_HEADER = "interleaved-thinking-2025-05-14";
50
+ /**
51
+ * Extract retry delay from Gemini error response (in milliseconds).
52
+ * Checks headers first (Retry-After, x-ratelimit-reset, x-ratelimit-reset-after),
53
+ * then parses body patterns like:
54
+ * - "Your quota will reset after 39s"
55
+ * - "Your quota will reset after 18h31m10s"
56
+ * - "Please retry in Xs" or "Please retry in Xms"
57
+ * - "retryDelay": "34.074824224s" (JSON field)
58
+ */
59
+ export function extractRetryDelay(errorText, response) {
60
+ const normalizeDelay = (ms) => (ms > 0 ? Math.ceil(ms + 1000) : undefined);
61
+ const headers = response instanceof Headers ? response : response?.headers;
62
+ if (headers) {
63
+ const retryAfter = headers.get("retry-after");
64
+ if (retryAfter) {
65
+ const retryAfterSeconds = Number(retryAfter);
66
+ if (Number.isFinite(retryAfterSeconds)) {
67
+ const delay = normalizeDelay(retryAfterSeconds * 1000);
68
+ if (delay !== undefined) {
69
+ return delay;
70
+ }
71
+ }
72
+ const retryAfterDate = new Date(retryAfter);
73
+ const retryAfterMs = retryAfterDate.getTime();
74
+ if (!Number.isNaN(retryAfterMs)) {
75
+ const delay = normalizeDelay(retryAfterMs - Date.now());
76
+ if (delay !== undefined) {
77
+ return delay;
78
+ }
79
+ }
80
+ }
81
+ const rateLimitReset = headers.get("x-ratelimit-reset");
82
+ if (rateLimitReset) {
83
+ const resetSeconds = Number.parseInt(rateLimitReset, 10);
84
+ if (!Number.isNaN(resetSeconds)) {
85
+ const delay = normalizeDelay(resetSeconds * 1000 - Date.now());
86
+ if (delay !== undefined) {
87
+ return delay;
88
+ }
89
+ }
90
+ }
91
+ const rateLimitResetAfter = headers.get("x-ratelimit-reset-after");
92
+ if (rateLimitResetAfter) {
93
+ const resetAfterSeconds = Number(rateLimitResetAfter);
94
+ if (Number.isFinite(resetAfterSeconds)) {
95
+ const delay = normalizeDelay(resetAfterSeconds * 1000);
96
+ if (delay !== undefined) {
97
+ return delay;
98
+ }
99
+ }
100
+ }
101
+ }
102
+ // Pattern 1: "Your quota will reset after ..." (formats: "18h31m10s", "10m15s", "6s", "39s")
103
+ const durationMatch = errorText.match(/reset after (?:(\d+)h)?(?:(\d+)m)?(\d+(?:\.\d+)?)s/i);
104
+ if (durationMatch) {
105
+ const hours = durationMatch[1] ? parseInt(durationMatch[1], 10) : 0;
106
+ const minutes = durationMatch[2] ? parseInt(durationMatch[2], 10) : 0;
107
+ const seconds = parseFloat(durationMatch[3]);
108
+ if (!Number.isNaN(seconds)) {
109
+ const totalMs = ((hours * 60 + minutes) * 60 + seconds) * 1000;
110
+ const delay = normalizeDelay(totalMs);
111
+ if (delay !== undefined) {
112
+ return delay;
113
+ }
114
+ }
115
+ }
116
+ // Pattern 2: "Please retry in X[ms|s]"
117
+ const retryInMatch = errorText.match(/Please retry in ([0-9.]+)(ms|s)/i);
118
+ if (retryInMatch?.[1]) {
119
+ const value = parseFloat(retryInMatch[1]);
120
+ if (!Number.isNaN(value) && value > 0) {
121
+ const ms = retryInMatch[2].toLowerCase() === "ms" ? value : value * 1000;
122
+ const delay = normalizeDelay(ms);
123
+ if (delay !== undefined) {
124
+ return delay;
125
+ }
126
+ }
127
+ }
128
+ // Pattern 3: "retryDelay": "34.074824224s" (JSON field in error details)
129
+ const retryDelayMatch = errorText.match(/"retryDelay":\s*"([0-9.]+)(ms|s)"/i);
130
+ if (retryDelayMatch?.[1]) {
131
+ const value = parseFloat(retryDelayMatch[1]);
132
+ if (!Number.isNaN(value) && value > 0) {
133
+ const ms = retryDelayMatch[2].toLowerCase() === "ms" ? value : value * 1000;
134
+ const delay = normalizeDelay(ms);
135
+ if (delay !== undefined) {
136
+ return delay;
137
+ }
138
+ }
139
+ }
140
+ return undefined;
141
+ }
142
+ function needsClaudeThinkingBetaHeader(model) {
143
+ return model.provider === "google-antigravity" && model.id.startsWith("claude-") && model.reasoning;
144
+ }
145
+ function isGemini3ProModel(modelId) {
146
+ return /gemini-3(?:\.1)?-pro/.test(modelId.toLowerCase());
147
+ }
148
+ function isGemini3FlashModel(modelId) {
149
+ return /gemini-3(?:\.1)?-flash/.test(modelId.toLowerCase());
150
+ }
151
+ function isGemini3Model(modelId) {
152
+ return isGemini3ProModel(modelId) || isGemini3FlashModel(modelId);
153
+ }
154
+ /**
155
+ * Check if an error is retryable (rate limit, server error, network error, etc.)
156
+ */
157
+ function isRetryableError(status, errorText) {
158
+ if (status === 429 || status === 500 || status === 502 || status === 503 || status === 504) {
159
+ return true;
160
+ }
161
+ return /resource.?exhausted|rate.?limit|overloaded|service.?unavailable|other.?side.?closed/i.test(errorText);
162
+ }
163
+ /**
164
+ * Extract a clean, user-friendly error message from Google API error response.
165
+ * Parses JSON error responses and returns just the message field.
166
+ */
167
+ function extractErrorMessage(errorText) {
168
+ try {
169
+ const parsed = JSON.parse(errorText);
170
+ if (parsed.error?.message) {
171
+ return parsed.error.message;
172
+ }
173
+ }
174
+ catch {
175
+ // Not JSON, return as-is
176
+ }
177
+ return errorText;
178
+ }
179
+ /**
180
+ * Sleep for a given number of milliseconds, respecting abort signal.
181
+ */
182
+ function sleep(ms, signal) {
183
+ return new Promise((resolve, reject) => {
184
+ if (signal?.aborted) {
185
+ reject(new Error("Request was aborted"));
186
+ return;
187
+ }
188
+ const timeout = setTimeout(resolve, ms);
189
+ signal?.addEventListener("abort", () => {
190
+ clearTimeout(timeout);
191
+ reject(new Error("Request was aborted"));
192
+ });
193
+ });
194
+ }
195
+ export const streamGoogleGeminiCli = (model, context, options) => {
196
+ const stream = new AssistantMessageEventStream();
197
+ (async () => {
198
+ const output = {
199
+ role: "assistant",
200
+ content: [],
201
+ api: "google-gemini-cli",
202
+ provider: model.provider,
203
+ model: model.id,
204
+ usage: {
205
+ input: 0,
206
+ output: 0,
207
+ cacheRead: 0,
208
+ cacheWrite: 0,
209
+ totalTokens: 0,
210
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
211
+ },
212
+ stopReason: "stop",
213
+ timestamp: Date.now(),
214
+ };
215
+ try {
216
+ // apiKey is JSON-encoded: { token, projectId }
217
+ const apiKeyRaw = options?.apiKey;
218
+ if (!apiKeyRaw) {
219
+ throw new Error("Google Cloud Code Assist requires OAuth authentication. Use /login to authenticate.");
220
+ }
221
+ let accessToken;
222
+ let projectId;
223
+ try {
224
+ const parsed = JSON.parse(apiKeyRaw);
225
+ accessToken = parsed.token;
226
+ projectId = parsed.projectId;
227
+ }
228
+ catch {
229
+ throw new Error("Invalid Google Cloud Code Assist credentials. Use /login to re-authenticate.");
230
+ }
231
+ if (!accessToken || !projectId) {
232
+ throw new Error("Missing token or projectId in Google Cloud credentials. Use /login to re-authenticate.");
233
+ }
234
+ const isAntigravity = model.provider === "google-antigravity";
235
+ const baseUrl = model.baseUrl?.trim();
236
+ const endpoints = baseUrl ? [baseUrl] : isAntigravity ? ANTIGRAVITY_ENDPOINT_FALLBACKS : [DEFAULT_ENDPOINT];
237
+ let requestBody = buildRequest(model, context, projectId, options, isAntigravity);
238
+ const nextRequestBody = await options?.onPayload?.(requestBody, model);
239
+ if (nextRequestBody !== undefined) {
240
+ requestBody = nextRequestBody;
241
+ }
242
+ const headers = isAntigravity ? getAntigravityHeaders() : GEMINI_CLI_HEADERS;
243
+ const requestHeaders = {
244
+ Authorization: `Bearer ${accessToken}`,
245
+ "Content-Type": "application/json",
246
+ Accept: "text/event-stream",
247
+ ...headers,
248
+ ...(needsClaudeThinkingBetaHeader(model) ? { "anthropic-beta": CLAUDE_THINKING_BETA_HEADER } : {}),
249
+ ...options?.headers,
250
+ };
251
+ const requestBodyJson = JSON.stringify(requestBody);
252
+ // Fetch with retry logic for rate limits, transient errors, and endpoint fallbacks.
253
+ // On 403/404, immediately try the next endpoint (no delay).
254
+ // On 429/5xx, retry with backoff on the same or next endpoint.
255
+ let response;
256
+ let lastError;
257
+ let requestUrl;
258
+ let endpointIndex = 0;
259
+ for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
260
+ if (options?.signal?.aborted) {
261
+ throw new Error("Request was aborted");
262
+ }
263
+ try {
264
+ const endpoint = endpoints[endpointIndex];
265
+ requestUrl = `${endpoint}/v1internal:streamGenerateContent?alt=sse`;
266
+ response = await fetch(requestUrl, {
267
+ method: "POST",
268
+ headers: requestHeaders,
269
+ body: requestBodyJson,
270
+ signal: options?.signal,
271
+ });
272
+ if (response.ok) {
273
+ break; // Success, exit retry loop
274
+ }
275
+ const errorText = await response.text();
276
+ // On 403/404, cascade to the next endpoint immediately (no delay)
277
+ if ((response.status === 403 || response.status === 404) && endpointIndex < endpoints.length - 1) {
278
+ endpointIndex++;
279
+ continue;
280
+ }
281
+ // Check if retryable (429, 5xx, network patterns)
282
+ if (attempt < MAX_RETRIES && isRetryableError(response.status, errorText)) {
283
+ // Advance endpoint if possible
284
+ if (endpointIndex < endpoints.length - 1) {
285
+ endpointIndex++;
286
+ }
287
+ // Use server-provided delay or exponential backoff
288
+ const serverDelay = extractRetryDelay(errorText, response);
289
+ const delayMs = serverDelay ?? BASE_DELAY_MS * 2 ** attempt;
290
+ // Check if server delay exceeds max allowed (default: 60s)
291
+ const maxDelayMs = options?.maxRetryDelayMs ?? 60000;
292
+ if (maxDelayMs > 0 && serverDelay && serverDelay > maxDelayMs) {
293
+ const delaySeconds = Math.ceil(serverDelay / 1000);
294
+ throw new Error(`Server requested ${delaySeconds}s retry delay (max: ${Math.ceil(maxDelayMs / 1000)}s). ${extractErrorMessage(errorText)}`);
295
+ }
296
+ await sleep(delayMs, options?.signal);
297
+ continue;
298
+ }
299
+ // Not retryable or max retries exceeded
300
+ throw new Error(`Cloud Code Assist API error (${response.status}): ${extractErrorMessage(errorText)}`);
301
+ }
302
+ catch (error) {
303
+ // Check for abort - fetch throws AbortError, our code throws "Request was aborted"
304
+ if (error instanceof Error) {
305
+ if (error.name === "AbortError" || error.message === "Request was aborted") {
306
+ throw new Error("Request was aborted");
307
+ }
308
+ }
309
+ // Extract detailed error message from fetch errors (Node includes cause)
310
+ lastError = error instanceof Error ? error : new Error(String(error));
311
+ if (lastError.message === "fetch failed" && lastError.cause instanceof Error) {
312
+ lastError = new Error(`Network error: ${lastError.cause.message}`);
313
+ }
314
+ // Network errors are retryable
315
+ if (attempt < MAX_RETRIES) {
316
+ const delayMs = BASE_DELAY_MS * 2 ** attempt;
317
+ await sleep(delayMs, options?.signal);
318
+ continue;
319
+ }
320
+ throw lastError;
321
+ }
322
+ }
323
+ if (!response || !response.ok) {
324
+ throw lastError ?? new Error("Failed to get response after retries");
325
+ }
326
+ let started = false;
327
+ const ensureStarted = () => {
328
+ if (!started) {
329
+ stream.push({ type: "start", partial: output });
330
+ started = true;
331
+ }
332
+ };
333
+ const resetOutput = () => {
334
+ output.content = [];
335
+ output.usage = {
336
+ input: 0,
337
+ output: 0,
338
+ cacheRead: 0,
339
+ cacheWrite: 0,
340
+ totalTokens: 0,
341
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
342
+ };
343
+ output.stopReason = "stop";
344
+ output.errorMessage = undefined;
345
+ output.timestamp = Date.now();
346
+ started = false;
347
+ };
348
+ const streamResponse = async (activeResponse) => {
349
+ if (!activeResponse.body) {
350
+ throw new Error("No response body");
351
+ }
352
+ let hasContent = false;
353
+ let currentBlock = null;
354
+ const blocks = output.content;
355
+ const blockIndex = () => blocks.length - 1;
356
+ // Read SSE stream
357
+ const reader = activeResponse.body.getReader();
358
+ const decoder = new TextDecoder();
359
+ let buffer = "";
360
+ // Set up abort handler to cancel reader when signal fires
361
+ const abortHandler = () => {
362
+ void reader.cancel().catch(() => { });
363
+ };
364
+ options?.signal?.addEventListener("abort", abortHandler);
365
+ try {
366
+ while (true) {
367
+ // Check abort signal before each read
368
+ if (options?.signal?.aborted) {
369
+ throw new Error("Request was aborted");
370
+ }
371
+ const { done, value } = await reader.read();
372
+ if (done)
373
+ break;
374
+ buffer += decoder.decode(value, { stream: true });
375
+ const lines = buffer.split("\n");
376
+ buffer = lines.pop() || "";
377
+ for (const line of lines) {
378
+ if (!line.startsWith("data:"))
379
+ continue;
380
+ const jsonStr = line.slice(5).trim();
381
+ if (!jsonStr)
382
+ continue;
383
+ let chunk;
384
+ try {
385
+ chunk = JSON.parse(jsonStr);
386
+ }
387
+ catch {
388
+ continue;
389
+ }
390
+ // Unwrap the response
391
+ const responseData = chunk.response;
392
+ if (!responseData)
393
+ continue;
394
+ // Cloud Code Assist mirrors Gemini's responseId field. Keep the first non-empty one.
395
+ // A single streamed response should retain the same ID across chunks.
396
+ output.responseId ||= responseData.responseId;
397
+ const candidate = responseData.candidates?.[0];
398
+ if (candidate?.content?.parts) {
399
+ for (const part of candidate.content.parts) {
400
+ if (part.text !== undefined) {
401
+ hasContent = true;
402
+ const isThinking = isThinkingPart(part);
403
+ if (!currentBlock ||
404
+ (isThinking && currentBlock.type !== "thinking") ||
405
+ (!isThinking && currentBlock.type !== "text")) {
406
+ if (currentBlock) {
407
+ if (currentBlock.type === "text") {
408
+ stream.push({
409
+ type: "text_end",
410
+ contentIndex: blocks.length - 1,
411
+ content: currentBlock.text,
412
+ partial: output,
413
+ });
414
+ }
415
+ else {
416
+ stream.push({
417
+ type: "thinking_end",
418
+ contentIndex: blockIndex(),
419
+ content: currentBlock.thinking,
420
+ partial: output,
421
+ });
422
+ }
423
+ }
424
+ if (isThinking) {
425
+ currentBlock = { type: "thinking", thinking: "", thinkingSignature: undefined };
426
+ output.content.push(currentBlock);
427
+ ensureStarted();
428
+ stream.push({
429
+ type: "thinking_start",
430
+ contentIndex: blockIndex(),
431
+ partial: output,
432
+ });
433
+ }
434
+ else {
435
+ currentBlock = { type: "text", text: "" };
436
+ output.content.push(currentBlock);
437
+ ensureStarted();
438
+ stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
439
+ }
440
+ }
441
+ if (currentBlock.type === "thinking") {
442
+ currentBlock.thinking += part.text;
443
+ currentBlock.thinkingSignature = retainThoughtSignature(currentBlock.thinkingSignature, part.thoughtSignature);
444
+ stream.push({
445
+ type: "thinking_delta",
446
+ contentIndex: blockIndex(),
447
+ delta: part.text,
448
+ partial: output,
449
+ });
450
+ }
451
+ else {
452
+ currentBlock.text += part.text;
453
+ currentBlock.textSignature = retainThoughtSignature(currentBlock.textSignature, part.thoughtSignature);
454
+ stream.push({
455
+ type: "text_delta",
456
+ contentIndex: blockIndex(),
457
+ delta: part.text,
458
+ partial: output,
459
+ });
460
+ }
461
+ }
462
+ if (part.functionCall) {
463
+ hasContent = true;
464
+ if (currentBlock) {
465
+ if (currentBlock.type === "text") {
466
+ stream.push({
467
+ type: "text_end",
468
+ contentIndex: blockIndex(),
469
+ content: currentBlock.text,
470
+ partial: output,
471
+ });
472
+ }
473
+ else {
474
+ stream.push({
475
+ type: "thinking_end",
476
+ contentIndex: blockIndex(),
477
+ content: currentBlock.thinking,
478
+ partial: output,
479
+ });
480
+ }
481
+ currentBlock = null;
482
+ }
483
+ const providedId = part.functionCall.id;
484
+ const needsNewId = !providedId ||
485
+ output.content.some((b) => b.type === "toolCall" && b.id === providedId);
486
+ const toolCallId = needsNewId
487
+ ? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`
488
+ : providedId;
489
+ const toolCall = {
490
+ type: "toolCall",
491
+ id: toolCallId,
492
+ name: part.functionCall.name || "",
493
+ arguments: part.functionCall.args ?? {},
494
+ ...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),
495
+ };
496
+ output.content.push(toolCall);
497
+ ensureStarted();
498
+ stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
499
+ stream.push({
500
+ type: "toolcall_delta",
501
+ contentIndex: blockIndex(),
502
+ delta: JSON.stringify(toolCall.arguments),
503
+ partial: output,
504
+ });
505
+ stream.push({
506
+ type: "toolcall_end",
507
+ contentIndex: blockIndex(),
508
+ toolCall,
509
+ partial: output,
510
+ });
511
+ }
512
+ }
513
+ }
514
+ if (candidate?.finishReason) {
515
+ output.stopReason = mapStopReasonString(candidate.finishReason);
516
+ if (output.content.some((b) => b.type === "toolCall")) {
517
+ output.stopReason = "toolUse";
518
+ }
519
+ }
520
+ if (responseData.usageMetadata) {
521
+ // promptTokenCount includes cachedContentTokenCount, so subtract to get fresh input
522
+ const promptTokens = responseData.usageMetadata.promptTokenCount || 0;
523
+ const cacheReadTokens = responseData.usageMetadata.cachedContentTokenCount || 0;
524
+ output.usage = {
525
+ input: promptTokens - cacheReadTokens,
526
+ output: (responseData.usageMetadata.candidatesTokenCount || 0) +
527
+ (responseData.usageMetadata.thoughtsTokenCount || 0),
528
+ cacheRead: cacheReadTokens,
529
+ cacheWrite: 0,
530
+ totalTokens: responseData.usageMetadata.totalTokenCount || 0,
531
+ cost: {
532
+ input: 0,
533
+ output: 0,
534
+ cacheRead: 0,
535
+ cacheWrite: 0,
536
+ total: 0,
537
+ },
538
+ };
539
+ calculateCost(model, output.usage);
540
+ }
541
+ }
542
+ }
543
+ }
544
+ finally {
545
+ options?.signal?.removeEventListener("abort", abortHandler);
546
+ }
547
+ if (currentBlock) {
548
+ if (currentBlock.type === "text") {
549
+ stream.push({
550
+ type: "text_end",
551
+ contentIndex: blockIndex(),
552
+ content: currentBlock.text,
553
+ partial: output,
554
+ });
555
+ }
556
+ else {
557
+ stream.push({
558
+ type: "thinking_end",
559
+ contentIndex: blockIndex(),
560
+ content: currentBlock.thinking,
561
+ partial: output,
562
+ });
563
+ }
564
+ }
565
+ return hasContent;
566
+ };
567
+ let receivedContent = false;
568
+ let currentResponse = response;
569
+ for (let emptyAttempt = 0; emptyAttempt <= MAX_EMPTY_STREAM_RETRIES; emptyAttempt++) {
570
+ if (options?.signal?.aborted) {
571
+ throw new Error("Request was aborted");
572
+ }
573
+ if (emptyAttempt > 0) {
574
+ const backoffMs = EMPTY_STREAM_BASE_DELAY_MS * 2 ** (emptyAttempt - 1);
575
+ await sleep(backoffMs, options?.signal);
576
+ if (!requestUrl) {
577
+ throw new Error("Missing request URL");
578
+ }
579
+ currentResponse = await fetch(requestUrl, {
580
+ method: "POST",
581
+ headers: requestHeaders,
582
+ body: requestBodyJson,
583
+ signal: options?.signal,
584
+ });
585
+ if (!currentResponse.ok) {
586
+ const retryErrorText = await currentResponse.text();
587
+ throw new Error(`Cloud Code Assist API error (${currentResponse.status}): ${retryErrorText}`);
588
+ }
589
+ }
590
+ const streamed = await streamResponse(currentResponse);
591
+ if (streamed) {
592
+ receivedContent = true;
593
+ break;
594
+ }
595
+ if (emptyAttempt < MAX_EMPTY_STREAM_RETRIES) {
596
+ resetOutput();
597
+ }
598
+ }
599
+ if (!receivedContent) {
600
+ throw new Error("Cloud Code Assist API returned an empty response");
601
+ }
602
+ if (options?.signal?.aborted) {
603
+ throw new Error("Request was aborted");
604
+ }
605
+ if (output.stopReason === "aborted" || output.stopReason === "error") {
606
+ throw new Error("An unknown error occurred");
607
+ }
608
+ stream.push({ type: "done", reason: output.stopReason, message: output });
609
+ stream.end();
610
+ }
611
+ catch (error) {
612
+ for (const block of output.content) {
613
+ if ("index" in block) {
614
+ delete block.index;
615
+ }
616
+ }
617
+ output.stopReason = options?.signal?.aborted ? "aborted" : "error";
618
+ output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
619
+ stream.push({ type: "error", reason: output.stopReason, error: output });
620
+ stream.end();
621
+ }
622
+ })();
623
+ return stream;
624
+ };
625
+ export const streamSimpleGoogleGeminiCli = (model, context, options) => {
626
+ const apiKey = options?.apiKey;
627
+ if (!apiKey) {
628
+ throw new Error("Google Cloud Code Assist requires OAuth authentication. Use /login to authenticate.");
629
+ }
630
+ const base = buildBaseOptions(model, options, apiKey);
631
+ if (!options?.reasoning) {
632
+ return streamGoogleGeminiCli(model, context, {
633
+ ...base,
634
+ thinking: { enabled: false },
635
+ });
636
+ }
637
+ const effort = clampReasoning(options.reasoning);
638
+ if (isGemini3Model(model.id)) {
639
+ return streamGoogleGeminiCli(model, context, {
640
+ ...base,
641
+ thinking: {
642
+ enabled: true,
643
+ level: getGeminiCliThinkingLevel(effort, model.id),
644
+ },
645
+ });
646
+ }
647
+ const defaultBudgets = {
648
+ minimal: 1024,
649
+ low: 2048,
650
+ medium: 8192,
651
+ high: 16384,
652
+ };
653
+ const budgets = { ...defaultBudgets, ...options.thinkingBudgets };
654
+ const minOutputTokens = 1024;
655
+ let thinkingBudget = budgets[effort];
656
+ const maxTokens = Math.min((base.maxTokens || 0) + thinkingBudget, model.maxTokens);
657
+ if (maxTokens <= thinkingBudget) {
658
+ thinkingBudget = Math.max(0, maxTokens - minOutputTokens);
659
+ }
660
+ return streamGoogleGeminiCli(model, context, {
661
+ ...base,
662
+ maxTokens,
663
+ thinking: {
664
+ enabled: true,
665
+ budgetTokens: thinkingBudget,
666
+ },
667
+ });
668
+ };
669
+ export function buildRequest(model, context, projectId, options = {}, isAntigravity = false) {
670
+ const contents = convertMessages(model, context);
671
+ const generationConfig = {};
672
+ if (options.temperature !== undefined) {
673
+ generationConfig.temperature = options.temperature;
674
+ }
675
+ if (options.maxTokens !== undefined) {
676
+ generationConfig.maxOutputTokens = options.maxTokens;
677
+ }
678
+ // Thinking config
679
+ if (options.thinking?.enabled && model.reasoning) {
680
+ generationConfig.thinkingConfig = {
681
+ includeThoughts: true,
682
+ };
683
+ // Gemini 3 models use thinkingLevel, older models use thinkingBudget
684
+ if (options.thinking.level !== undefined) {
685
+ // Cast to any since our GoogleThinkingLevel mirrors Google's ThinkingLevel enum values
686
+ generationConfig.thinkingConfig.thinkingLevel = options.thinking.level;
687
+ }
688
+ else if (options.thinking.budgetTokens !== undefined) {
689
+ generationConfig.thinkingConfig.thinkingBudget = options.thinking.budgetTokens;
690
+ }
691
+ }
692
+ const request = {
693
+ contents,
694
+ };
695
+ request.sessionId = options.sessionId;
696
+ // System instruction must be object with parts, not plain string
697
+ if (context.systemPrompt) {
698
+ request.systemInstruction = {
699
+ parts: [{ text: sanitizeSurrogates(context.systemPrompt) }],
700
+ };
701
+ }
702
+ if (Object.keys(generationConfig).length > 0) {
703
+ request.generationConfig = generationConfig;
704
+ }
705
+ if (context.tools && context.tools.length > 0) {
706
+ // Claude models on Cloud Code Assist need the legacy `parameters` field;
707
+ // the API translates it into Anthropic's `input_schema`.
708
+ const useParameters = model.id.startsWith("claude-");
709
+ request.tools = convertTools(context.tools, useParameters);
710
+ if (options.toolChoice) {
711
+ request.toolConfig = {
712
+ functionCallingConfig: {
713
+ mode: mapToolChoice(options.toolChoice),
714
+ },
715
+ };
716
+ }
717
+ }
718
+ if (isAntigravity) {
719
+ const existingParts = request.systemInstruction?.parts ?? [];
720
+ request.systemInstruction = {
721
+ role: "user",
722
+ parts: [
723
+ { text: ANTIGRAVITY_SYSTEM_INSTRUCTION },
724
+ { text: `Please ignore following [ignore]${ANTIGRAVITY_SYSTEM_INSTRUCTION}[/ignore]` },
725
+ ...existingParts,
726
+ ],
727
+ };
728
+ }
729
+ return {
730
+ project: projectId,
731
+ model: model.id,
732
+ request,
733
+ ...(isAntigravity ? { requestType: "agent" } : {}),
734
+ userAgent: isAntigravity ? "antigravity" : "pi-coding-agent",
735
+ requestId: `${isAntigravity ? "agent" : "pi"}-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
736
+ };
737
+ }
738
+ function getGeminiCliThinkingLevel(effort, modelId) {
739
+ if (isGemini3ProModel(modelId)) {
740
+ switch (effort) {
741
+ case "minimal":
742
+ case "low":
743
+ return "LOW";
744
+ case "medium":
745
+ case "high":
746
+ return "HIGH";
747
+ }
748
+ }
749
+ switch (effort) {
750
+ case "minimal":
751
+ return "MINIMAL";
752
+ case "low":
753
+ return "LOW";
754
+ case "medium":
755
+ return "MEDIUM";
756
+ case "high":
757
+ return "HIGH";
758
+ }
759
+ }
760
+ //# sourceMappingURL=google-gemini-cli.js.map