@gullabs/google 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { LlmError, EFFORT_BUDGET, assertNever, classifyError, redactSecrets } from '@gullabs/core';
1
+ import { LlmError, assertNever, classifyError, redactSecrets } from '@gullabs/core';
2
2
 
3
3
  // src/adapter.ts
4
4
 
@@ -19,6 +19,14 @@ async function buildGoogleClient(auth) {
19
19
  };
20
20
  }
21
21
 
22
+ // src/reasoning-budget.ts
23
+ var GOOGLE_REASONING_EFFORT_BUDGET = {
24
+ none: 0,
25
+ low: 1024,
26
+ medium: 8192,
27
+ high: 24576
28
+ };
29
+
22
30
  // src/flex-fallback.ts
23
31
  var CAPACITY_PATTERNS = [
24
32
  /capacity/i,
@@ -49,6 +57,231 @@ function isGeminiCapacityError(err) {
49
57
  }
50
58
 
51
59
  // src/adapter.ts
60
+ var ALLOWED_GOOGLE_PROVIDER_OPTION_KEYS = /* @__PURE__ */ new Set([
61
+ "cachedContent",
62
+ "httpOptions",
63
+ "safetySettings",
64
+ "tools"
65
+ ]);
66
+ var ALLOWED_GOOGLE_HTTP_OPTION_KEYS = /* @__PURE__ */ new Set(["timeout"]);
67
+ var RESERVED_GOOGLE_PROVIDER_OPTION_KEYS = /* @__PURE__ */ new Set([
68
+ "abortSignal",
69
+ "imageConfig",
70
+ "maxOutputTokens",
71
+ "mediaResolution",
72
+ "responseFormat",
73
+ "responseMimeType",
74
+ "responseModalities",
75
+ "responseSchema",
76
+ "responseJsonSchema",
77
+ "serviceTier",
78
+ "speechConfig",
79
+ "stopSequences",
80
+ "temperature",
81
+ "thinkingConfig",
82
+ "topK",
83
+ "topP",
84
+ "_responseJsonSchema"
85
+ ]);
86
+ var GOOGLE_SEARCH_SUPPORTED_MODELS = /* @__PURE__ */ new Set([
87
+ "gemini-2.5-flash",
88
+ "gemini-2.5-flash-lite",
89
+ "gemini-2.5-pro",
90
+ "gemini-3-flash-preview",
91
+ "gemini-3.1-flash-lite",
92
+ "gemini-3.1-pro-preview",
93
+ "gemini-3.5-flash"
94
+ ]);
95
+ var STRUCTURED_OUTPUT_GOOGLE_SEARCH_ALLOWLIST = /* @__PURE__ */ new Set([
96
+ "gemini-3.1-pro-preview",
97
+ "gemini-3.5-flash"
98
+ ]);
99
+ function isPlainRecord(value) {
100
+ return typeof value === "object" && value !== null && !Array.isArray(value);
101
+ }
102
+ function isEmptyPlainObject(value) {
103
+ return isPlainRecord(value) && Object.keys(value).length === 0;
104
+ }
105
+ function badGoogleProviderOptions(message) {
106
+ return new LlmError(message, { kind: "bad_request", retryable: false });
107
+ }
108
+ function parseGoogleTool(tool, model) {
109
+ if (!isPlainRecord(tool)) {
110
+ throw badGoogleProviderOptions(
111
+ `providerOptions.google.tools entries must be objects for model "${model}".`
112
+ );
113
+ }
114
+ const keys = Object.keys(tool);
115
+ if (keys.length !== 1) {
116
+ throw badGoogleProviderOptions(
117
+ `providerOptions.google.tools entries must have exactly one supported tool key for model "${model}".`
118
+ );
119
+ }
120
+ const key = keys[0];
121
+ switch (key) {
122
+ case "googleSearch": {
123
+ if (!isEmptyPlainObject(tool["googleSearch"])) {
124
+ throw badGoogleProviderOptions(
125
+ `providerOptions.google.tools[].googleSearch must be an empty object for model "${model}".`
126
+ );
127
+ }
128
+ return { googleSearch: {} };
129
+ }
130
+ default:
131
+ throw badGoogleProviderOptions(
132
+ `providerOptions.google.tools[].${key} is not supported for model "${model}".`
133
+ );
134
+ }
135
+ }
136
+ function parseGoogleSafetySetting(setting, index, model) {
137
+ if (!isPlainRecord(setting)) {
138
+ throw badGoogleProviderOptions(
139
+ `providerOptions.google.safetySettings[${index}] must be an object for model "${model}".`
140
+ );
141
+ }
142
+ const keys = Object.keys(setting);
143
+ const unknownKeys = keys.filter((key) => key !== "category" && key !== "threshold");
144
+ if (unknownKeys.length > 0) {
145
+ throw badGoogleProviderOptions(
146
+ `providerOptions.google.safetySettings[${index}] contains unsupported keys [${unknownKeys.join(
147
+ ", "
148
+ )}] for model "${model}". Allowed keys: category, threshold.`
149
+ );
150
+ }
151
+ if (typeof setting["category"] !== "string" || setting["category"].length === 0) {
152
+ throw badGoogleProviderOptions(
153
+ `providerOptions.google.safetySettings[${index}].category must be a non-empty string for model "${model}".`
154
+ );
155
+ }
156
+ if (typeof setting["threshold"] !== "string" || setting["threshold"].length === 0) {
157
+ throw badGoogleProviderOptions(
158
+ `providerOptions.google.safetySettings[${index}].threshold must be a non-empty string for model "${model}".`
159
+ );
160
+ }
161
+ return {
162
+ category: setting["category"],
163
+ threshold: setting["threshold"]
164
+ };
165
+ }
166
+ function mapGoogleProviderOptions(googleOpts, model, structuredOutputRequested, descriptorGrounding) {
167
+ if (googleOpts === void 0) {
168
+ return {};
169
+ }
170
+ if (!isPlainRecord(googleOpts)) {
171
+ throw badGoogleProviderOptions(
172
+ `providerOptions.google must be an object for model "${model}".`
173
+ );
174
+ }
175
+ const reservedKeys = Object.keys(googleOpts).filter(
176
+ (key) => RESERVED_GOOGLE_PROVIDER_OPTION_KEYS.has(key)
177
+ );
178
+ if (reservedKeys.length > 0) {
179
+ throw badGoogleProviderOptions(
180
+ `providerOptions.google reserves keys [${reservedKeys.join(
181
+ ", "
182
+ )}] for typed model config on "${model}".`
183
+ );
184
+ }
185
+ const unknownKeys = Object.keys(googleOpts).filter(
186
+ (key) => !ALLOWED_GOOGLE_PROVIDER_OPTION_KEYS.has(key) && !RESERVED_GOOGLE_PROVIDER_OPTION_KEYS.has(key)
187
+ );
188
+ if (unknownKeys.length > 0) {
189
+ throw badGoogleProviderOptions(
190
+ `providerOptions.google contains unsupported keys [${unknownKeys.join(
191
+ ", "
192
+ )}] for model "${model}". Allowed keys: cachedContent, httpOptions, safetySettings, tools.`
193
+ );
194
+ }
195
+ const mapped = {};
196
+ if (googleOpts["cachedContent"] !== void 0) {
197
+ if (typeof googleOpts["cachedContent"] !== "string" || googleOpts["cachedContent"].length === 0) {
198
+ throw badGoogleProviderOptions(
199
+ `providerOptions.google.cachedContent must be a non-empty string for model "${model}".`
200
+ );
201
+ }
202
+ mapped.cachedContent = googleOpts["cachedContent"];
203
+ }
204
+ if (googleOpts["httpOptions"] !== void 0) {
205
+ if (!isPlainRecord(googleOpts["httpOptions"])) {
206
+ throw badGoogleProviderOptions(
207
+ `providerOptions.google.httpOptions must be an object for model "${model}".`
208
+ );
209
+ }
210
+ const unknownHttpOptionKeys = Object.keys(googleOpts["httpOptions"]).filter(
211
+ (key) => !ALLOWED_GOOGLE_HTTP_OPTION_KEYS.has(key)
212
+ );
213
+ if (unknownHttpOptionKeys.length > 0) {
214
+ throw badGoogleProviderOptions(
215
+ `providerOptions.google.httpOptions contains unsupported keys [${unknownHttpOptionKeys.join(
216
+ ", "
217
+ )}] for model "${model}". Allowed keys: timeout.`
218
+ );
219
+ }
220
+ const timeout = googleOpts["httpOptions"]["timeout"];
221
+ if (timeout !== void 0) {
222
+ if (typeof timeout !== "number" || !Number.isInteger(timeout) || timeout <= 0) {
223
+ throw badGoogleProviderOptions(
224
+ `providerOptions.google.httpOptions.timeout must be a positive integer for model "${model}".`
225
+ );
226
+ }
227
+ mapped.httpOptions = { timeout };
228
+ } else {
229
+ mapped.httpOptions = {};
230
+ }
231
+ }
232
+ if (googleOpts["safetySettings"] !== void 0) {
233
+ if (!Array.isArray(googleOpts["safetySettings"])) {
234
+ throw badGoogleProviderOptions(
235
+ `providerOptions.google.safetySettings must be an array for model "${model}".`
236
+ );
237
+ }
238
+ mapped.safetySettings = googleOpts["safetySettings"].map(
239
+ (setting, index) => parseGoogleSafetySetting(setting, index, model)
240
+ );
241
+ }
242
+ if (googleOpts["tools"] !== void 0) {
243
+ if (!Array.isArray(googleOpts["tools"])) {
244
+ throw badGoogleProviderOptions(
245
+ `providerOptions.google.tools must be an array for model "${model}".`
246
+ );
247
+ }
248
+ const tools = googleOpts["tools"].map((tool) => parseGoogleTool(tool, model));
249
+ const groundingSupported = descriptorGrounding === true || descriptorGrounding === void 0 && GOOGLE_SEARCH_SUPPORTED_MODELS.has(model);
250
+ if (!groundingSupported) {
251
+ throw badGoogleProviderOptions(
252
+ `providerOptions.google.tools is not supported for model "${model}".`
253
+ );
254
+ }
255
+ if (structuredOutputRequested && !STRUCTURED_OUTPUT_GOOGLE_SEARCH_ALLOWLIST.has(model)) {
256
+ throw badGoogleProviderOptions(
257
+ `Structured output with googleSearch is supported only for models "gemini-3.1-pro-preview" and "gemini-3.5-flash"; got "${model}".`
258
+ );
259
+ }
260
+ mapped.tools = tools;
261
+ }
262
+ return mapped;
263
+ }
264
+ function assertSamplingAllowed(config, model, sampling) {
265
+ if (sampling !== "fixed") {
266
+ return;
267
+ }
268
+ const offendingSampling = [];
269
+ if ("temperature" in config) {
270
+ offendingSampling.push("temperature");
271
+ }
272
+ if ("topP" in config) {
273
+ offendingSampling.push("topP");
274
+ }
275
+ if ("topK" in config) {
276
+ offendingSampling.push("topK");
277
+ }
278
+ if (offendingSampling.length > 0) {
279
+ throw new LlmError(
280
+ `Sampling parameters [${offendingSampling.join(", ")}] are not supported for model "${model}" (fixed sampling).`,
281
+ { kind: "bad_request", retryable: false }
282
+ );
283
+ }
284
+ }
52
285
  function mapFinishReason(raw) {
53
286
  if (raw === void 0) return void 0;
54
287
  switch (raw) {
@@ -170,12 +403,6 @@ function geminiAdapter(opts) {
170
403
  );
171
404
  }
172
405
  config.serviceTier = explicit;
173
- } else {
174
- if (req.modelDescriptor === void 0) {
175
- config.serviceTier = "flex";
176
- } else if (supported?.includes("flex") === true) {
177
- config.serviceTier = "flex";
178
- }
179
406
  }
180
407
  const reasoning = genConfig.reasoning;
181
408
  if (reasoning !== void 0) {
@@ -187,7 +414,7 @@ function geminiAdapter(opts) {
187
414
  );
188
415
  }
189
416
  if (reasoningApi === "budget") {
190
- const budget = reasoning.budgetTokens !== void 0 ? reasoning.budgetTokens : reasoning.effort !== void 0 ? EFFORT_BUDGET[reasoning.effort] : void 0;
417
+ const budget = reasoning.budgetTokens !== void 0 ? reasoning.budgetTokens : reasoning.effort !== void 0 ? GOOGLE_REASONING_EFFORT_BUDGET[reasoning.effort] : void 0;
191
418
  config.thinkingConfig = {
192
419
  ...budget !== void 0 ? { thinkingBudget: budget } : {},
193
420
  ...reasoning.includeThoughts === true ? { includeThoughts: true } : {}
@@ -237,54 +464,25 @@ function geminiAdapter(opts) {
237
464
  config.responseSchema = req.outputJsonSchema;
238
465
  }
239
466
  }
240
- const googleOpts = genConfig.providerOptions?.["google"];
241
- if (googleOpts !== void 0 && typeof googleOpts === "object" && googleOpts !== null) {
242
- Object.assign(config, googleOpts);
467
+ const googleProviderConfig = mapGoogleProviderOptions(
468
+ genConfig.providerOptions?.["google"],
469
+ model,
470
+ structuredOutputRequested,
471
+ req.modelDescriptor?.capabilities?.grounding
472
+ );
473
+ if (googleProviderConfig.cachedContent !== void 0) {
474
+ config.cachedContent = googleProviderConfig.cachedContent;
243
475
  }
244
- const mergedServiceTier = config.serviceTier;
245
- if (mergedServiceTier !== void 0 && req.modelDescriptor !== void 0) {
246
- const supportedAfterMerge = req.modelDescriptor.capabilities?.serviceTiers;
247
- if (supportedAfterMerge === void 0 || !supportedAfterMerge.includes(mergedServiceTier)) {
248
- throw new LlmError(
249
- `serviceTier "${mergedServiceTier}" is not supported for model "${model}".`,
250
- { kind: "bad_request", retryable: false }
251
- );
252
- }
476
+ if (googleProviderConfig.httpOptions !== void 0) {
477
+ config.httpOptions = googleProviderConfig.httpOptions;
253
478
  }
254
- if (req.modelDescriptor?.capabilities?.sampling === "fixed") {
255
- const offendingSampling = [];
256
- if ("temperature" in config) {
257
- offendingSampling.push("temperature");
258
- }
259
- if ("topP" in config) {
260
- offendingSampling.push("topP");
261
- }
262
- if ("topK" in config) {
263
- offendingSampling.push("topK");
264
- }
265
- if (offendingSampling.length > 0) {
266
- throw new LlmError(
267
- `Sampling parameters [${offendingSampling.join(
268
- ", "
269
- )}] are not supported for model "${model}" (fixed sampling); they were supplied via providerOptions.google.`,
270
- { kind: "bad_request", retryable: false }
271
- );
272
- }
479
+ if (googleProviderConfig.safetySettings !== void 0) {
480
+ config.safetySettings = googleProviderConfig.safetySettings;
273
481
  }
274
- const configAsAny = config;
275
- const groundingRequested = Array.isArray(configAsAny.tools) && configAsAny.tools.some((t) => {
276
- if (t !== null && typeof t === "object") {
277
- const tool = t;
278
- return "googleSearch" in tool || "googleSearchRetrieval" in tool;
279
- }
280
- return false;
281
- });
282
- if (groundingRequested && structuredOutputRequested) {
283
- throw new LlmError(
284
- "Grounding (googleSearch) cannot be combined with structured output (output.jsonSchema) on Gemini; choose one.",
285
- { kind: "bad_request", retryable: false }
286
- );
482
+ if (googleProviderConfig.tools !== void 0) {
483
+ config.tools = googleProviderConfig.tools;
287
484
  }
485
+ assertSamplingAllowed(config, model, req.modelDescriptor?.capabilities?.sampling);
288
486
  let tierTimeoutHandle;
289
487
  const clearTierTimeout = () => {
290
488
  if (tierTimeoutHandle !== void 0) {
@@ -475,7 +673,7 @@ async function buildFilesClient(auth) {
475
673
  return {
476
674
  async upload(params) {
477
675
  const fileArg = params.file instanceof Uint8Array ? new Blob(
478
- [params.file],
676
+ [Uint8Array.from(params.file)],
479
677
  params.config?.mimeType !== void 0 && params.config.mimeType.length > 0 ? { type: params.config.mimeType } : {}
480
678
  ) : params.file;
481
679
  const result = await ai.files.upload({