@kenkaiiii/gg-ai 4.3.205 → 4.3.207

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -157,7 +157,7 @@ interface StreamOptions {
157
157
  signal?: AbortSignal;
158
158
  /** Prompt cache retention preference. Providers map this to their supported values. Default: "short". */
159
159
  cacheRetention?: CacheRetention;
160
- /** Stable per-session cache routing key for providers that support it (OpenAI, Moonshot). */
160
+ /** Stable per-session cache routing key for providers that support it (OpenAI, Moonshot, Gemini Code Assist). */
161
161
  promptCacheKey?: string;
162
162
  /** OpenAI service tier for latency-sensitive requests. Only sent to first-party OpenAI API calls. */
163
163
  serviceTier?: "auto" | "default" | "flex" | "priority";
package/dist/index.d.ts CHANGED
@@ -157,7 +157,7 @@ interface StreamOptions {
157
157
  signal?: AbortSignal;
158
158
  /** Prompt cache retention preference. Providers map this to their supported values. Default: "short". */
159
159
  cacheRetention?: CacheRetention;
160
- /** Stable per-session cache routing key for providers that support it (OpenAI, Moonshot). */
160
+ /** Stable per-session cache routing key for providers that support it (OpenAI, Moonshot, Gemini Code Assist). */
161
161
  promptCacheKey?: string;
162
162
  /** OpenAI service tier for latency-sensitive requests. Only sent to first-party OpenAI API calls. */
163
163
  serviceTier?: "auto" | "default" | "flex" | "priority";
package/dist/index.js CHANGED
@@ -1932,6 +1932,8 @@ var DEFAULT_CODE_ASSIST_BASE_URL = "https://cloudcode-pa.googleapis.com";
1932
1932
  var CODE_ASSIST_API_VERSION = "v1internal";
1933
1933
  var GEMINI_CLI_USER_AGENT = "google-gemini-cli";
1934
1934
  var GEMINI_CLI_API_CLIENT = "gemini-cli/0.0.0";
1935
+ var CODE_ASSIST_NON_STREAMING_RETRIES = 3;
1936
+ var CODE_ASSIST_NON_STREAMING_RETRY_DELAY_MS = 1e3;
1935
1937
  var SYNTHETIC_THOUGHT_SIGNATURE = "skip_thought_signature_validator";
1936
1938
  var CODE_ASSIST_SUPPORTED_MODELS = /* @__PURE__ */ new Set([
1937
1939
  "gemini-3-pro-preview",
@@ -1956,7 +1958,10 @@ function getGoogleProject(options) {
1956
1958
  return options.projectId ?? env?.GOOGLE_CLOUD_PROJECT ?? env?.GOOGLE_CLOUD_PROJECT_ID;
1957
1959
  }
1958
1960
  function getCodeAssistEndpoint(method) {
1959
- return new URL(`${DEFAULT_CODE_ASSIST_BASE_URL}/${CODE_ASSIST_API_VERSION}:${method}`);
1961
+ const env = getEnvironment();
1962
+ const endpoint = env?.CODE_ASSIST_ENDPOINT ?? DEFAULT_CODE_ASSIST_BASE_URL;
1963
+ const version = env?.CODE_ASSIST_API_VERSION || CODE_ASSIST_API_VERSION;
1964
+ return new URL(`${endpoint}/${version}:${method}`);
1960
1965
  }
1961
1966
  function formatUnsupportedModelMessage(model) {
1962
1967
  return `Gemini OAuth is configured to use the Gemini Code Assist subscription endpoint only. That endpoint does not currently expose model "${model}".`;
@@ -2135,7 +2140,8 @@ function buildGenerateRequest(options) {
2135
2140
  ...systemInstruction ? { systemInstruction } : {},
2136
2141
  ...tools ? { tools } : {},
2137
2142
  ...toolConfig ? { toolConfig } : {},
2138
- ...Object.keys(generationConfig).length > 0 ? { generationConfig } : {}
2143
+ ...Object.keys(generationConfig).length > 0 ? { generationConfig } : {},
2144
+ ...options.promptCacheKey ? { session_id: options.promptCacheKey } : {}
2139
2145
  };
2140
2146
  }
2141
2147
  function buildCodeAssistRequest(options, request, projectId) {
@@ -2255,31 +2261,78 @@ function readFunctionCallPart(part) {
2255
2261
  function makeToolCallId(index, providerId) {
2256
2262
  return providerId ?? `gemini_call_${index}_${crypto.randomUUID().replace(/-/g, "")}`;
2257
2263
  }
2258
- function streamGemini(options) {
2259
- return new StreamResult(runStream4(options));
2264
+ function shouldRetryCodeAssistStatus(status) {
2265
+ return status === 429 || status === 499 || status >= 500 && status <= 599;
2266
+ }
2267
+ function isAbortError(err) {
2268
+ return err instanceof Error && err.name === "AbortError";
2269
+ }
2270
+ async function sleep(ms, signal) {
2271
+ if (ms <= 0) return;
2272
+ await new Promise((resolve, reject) => {
2273
+ const cleanup = () => signal?.removeEventListener("abort", onAbort);
2274
+ const timer = setTimeout(() => {
2275
+ cleanup();
2276
+ resolve();
2277
+ }, ms);
2278
+ const onAbort = () => {
2279
+ clearTimeout(timer);
2280
+ cleanup();
2281
+ reject(new DOMException("The operation was aborted.", "AbortError"));
2282
+ };
2283
+ signal?.addEventListener("abort", onAbort, { once: true });
2284
+ if (signal?.aborted) onAbort();
2285
+ });
2260
2286
  }
2261
- async function* runStream4(options) {
2262
- const useStreaming = options.streaming !== false;
2263
- const method = useStreaming ? "streamGenerateContent" : "generateContent";
2264
- const plan = buildRequestPlan(options, method);
2265
- if (useStreaming) plan.url.searchParams.set("alt", "sse");
2266
- let response;
2287
+ async function fetchCodeAssist(plan, options) {
2267
2288
  try {
2268
- response = await fetch(plan.url, {
2289
+ const response = await fetch(plan.url, {
2269
2290
  method: "POST",
2270
2291
  headers: plan.headers,
2271
2292
  body: JSON.stringify(plan.body),
2272
2293
  signal: options.signal
2273
2294
  });
2295
+ if (!response.ok) {
2296
+ const text = await response.text().catch(() => "");
2297
+ throw new ProviderError("gemini", formatErrorMessage(response.status, text, options.model), {
2298
+ statusCode: response.status
2299
+ });
2300
+ }
2301
+ return response;
2274
2302
  } catch (err) {
2275
2303
  throw toError3(err);
2276
2304
  }
2277
- if (!response.ok) {
2278
- const text = await response.text().catch(() => "");
2279
- throw new ProviderError("gemini", formatErrorMessage(response.status, text, options.model), {
2280
- statusCode: response.status
2281
- });
2305
+ }
2306
+ async function fetchCodeAssistWithRetry(plan, options) {
2307
+ let lastError;
2308
+ for (let attempt = 0; attempt <= CODE_ASSIST_NON_STREAMING_RETRIES; attempt++) {
2309
+ try {
2310
+ return await fetchCodeAssist(plan, options);
2311
+ } catch (err) {
2312
+ const error = toError3(err);
2313
+ const statusCode = error instanceof ProviderError ? error.statusCode : void 0;
2314
+ if (options.signal?.aborted || isAbortError(error) || attempt === CODE_ASSIST_NON_STREAMING_RETRIES || statusCode != null && !shouldRetryCodeAssistStatus(statusCode)) {
2315
+ throw error;
2316
+ }
2317
+ lastError = error;
2318
+ }
2319
+ try {
2320
+ await sleep(CODE_ASSIST_NON_STREAMING_RETRY_DELAY_MS, options.signal);
2321
+ } catch (err) {
2322
+ throw toError3(err);
2323
+ }
2282
2324
  }
2325
+ throw lastError ?? new ProviderError("gemini", "Gemini Code Assist request failed.");
2326
+ }
2327
+ function streamGemini(options) {
2328
+ return new StreamResult(runStream4(options));
2329
+ }
2330
+ async function* runStream4(options) {
2331
+ const useStreaming = options.streaming !== false;
2332
+ const method = useStreaming ? "streamGenerateContent" : "generateContent";
2333
+ const plan = buildRequestPlan(options, method);
2334
+ if (useStreaming) plan.url.searchParams.set("alt", "sse");
2335
+ const response = useStreaming ? await fetchCodeAssist(plan, options) : await fetchCodeAssistWithRetry(plan, options);
2283
2336
  const contentParts = [];
2284
2337
  const pendingToolCalls = [];
2285
2338
  let textAccum = "";