ak-gemini 1.1.0 → 1.1.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 (4) hide show
  1. package/index.cjs +14 -10
  2. package/index.js +18 -13
  3. package/package.json +1 -1
  4. package/types.d.ts +3 -1
package/index.cjs CHANGED
@@ -178,7 +178,7 @@ function AITransformFactory(options = {}) {
178
178
  }
179
179
  this.vertexai = options.vertexai || false;
180
180
  this.project = options.project || process.env.GOOGLE_CLOUD_PROJECT || null;
181
- this.location = options.location || process.env.GOOGLE_CLOUD_LOCATION || "us-central1";
181
+ this.location = options.location || process.env.GOOGLE_CLOUD_LOCATION || void 0;
182
182
  this.googleAuthOptions = options.googleAuthOptions || null;
183
183
  this.apiKey = options.apiKey !== void 0 && options.apiKey !== null ? options.apiKey : GEMINI_API_KEY;
184
184
  if (!this.vertexai && !this.apiKey) {
@@ -249,7 +249,11 @@ function AITransformFactory(options = {}) {
249
249
  this.groundingConfig = options.groundingConfig || {};
250
250
  this.labels = options.labels || {};
251
251
  if (Object.keys(this.labels).length > 0 && logger_default.level !== "silent") {
252
- logger_default.debug(`Billing labels configured: ${JSON.stringify(this.labels)}`);
252
+ if (!this.vertexai) {
253
+ logger_default.warn(`Billing labels are only supported with Vertex AI. Labels will be ignored.`);
254
+ } else {
255
+ logger_default.debug(`Billing labels configured: ${JSON.stringify(this.labels)}`);
256
+ }
253
257
  }
254
258
  if (this.promptKey === this.answerKey) {
255
259
  throw new Error("Source and target keys cannot be the same. Please provide distinct keys.");
@@ -259,7 +263,7 @@ function AITransformFactory(options = {}) {
259
263
  logger_default.debug(`Using keys - Source: "${this.promptKey}", Target: "${this.answerKey}", Context: "${this.contextKey}"`);
260
264
  logger_default.debug(`Max output tokens set to: ${this.chatConfig.maxOutputTokens}`);
261
265
  if (this.vertexai) {
262
- logger_default.debug(`Using Vertex AI - Project: ${this.project}, Location: ${this.location}`);
266
+ logger_default.debug(`Using Vertex AI - Project: ${this.project}, Location: ${this.location || "global (default)"}`);
263
267
  if (this.googleAuthOptions?.keyFilename) {
264
268
  logger_default.debug(`Auth: Service account key file: ${this.googleAuthOptions.keyFilename}`);
265
269
  } else if (this.googleAuthOptions?.credentials) {
@@ -275,7 +279,7 @@ function AITransformFactory(options = {}) {
275
279
  const clientOptions = this.vertexai ? {
276
280
  vertexai: true,
277
281
  project: this.project,
278
- location: this.location,
282
+ ...this.location && { location: this.location },
279
283
  ...this.googleAuthOptions && { googleAuthOptions: this.googleAuthOptions }
280
284
  } : { apiKey: this.apiKey };
281
285
  const ai = new import_genai.GoogleGenAI(clientOptions);
@@ -290,7 +294,7 @@ async function initChat(force = false) {
290
294
  // @ts-ignore
291
295
  config: {
292
296
  ...this.chatConfig,
293
- ...Object.keys(this.labels).length > 0 && { labels: this.labels }
297
+ ...this.vertexai && Object.keys(this.labels).length > 0 && { labels: this.labels }
294
298
  },
295
299
  history: []
296
300
  };
@@ -373,7 +377,7 @@ ${contextText}
373
377
  // @ts-ignore
374
378
  config: {
375
379
  ...this.chatConfig,
376
- ...Object.keys(this.labels).length > 0 && { labels: this.labels }
380
+ ...this.vertexai && Object.keys(this.labels).length > 0 && { labels: this.labels }
377
381
  },
378
382
  history: [...currentHistory, ...historyToAdd]
379
383
  });
@@ -388,7 +392,7 @@ async function rawMessage(sourcePayload, messageOptions = {}) {
388
392
  }
389
393
  const actualPayload = typeof sourcePayload === "string" ? sourcePayload : JSON.stringify(sourcePayload, null, 2);
390
394
  const mergedLabels = { ...this.labels, ...messageOptions.labels || {} };
391
- const hasLabels = Object.keys(mergedLabels).length > 0;
395
+ const hasLabels = this.vertexai && Object.keys(mergedLabels).length > 0;
392
396
  try {
393
397
  const sendParams = { message: actualPayload };
394
398
  if (hasLabels) {
@@ -603,7 +607,7 @@ async function resetChat() {
603
607
  // @ts-ignore
604
608
  config: {
605
609
  ...this.chatConfig,
606
- ...Object.keys(this.labels).length > 0 && { labels: this.labels }
610
+ ...this.vertexai && Object.keys(this.labels).length > 0 && { labels: this.labels }
607
611
  },
608
612
  history: []
609
613
  };
@@ -647,7 +651,7 @@ async function clearConversation() {
647
651
  // @ts-ignore
648
652
  config: {
649
653
  ...this.chatConfig,
650
- ...Object.keys(this.labels).length > 0 && { labels: this.labels }
654
+ ...this.vertexai && Object.keys(this.labels).length > 0 && { labels: this.labels }
651
655
  },
652
656
  history: exampleHistory
653
657
  });
@@ -701,7 +705,7 @@ async function statelessMessage(sourcePayload, options = {}, validatorFn = null)
701
705
  contents,
702
706
  config: {
703
707
  ...this.chatConfig,
704
- ...Object.keys(mergedLabels).length > 0 && { labels: mergedLabels }
708
+ ...this.vertexai && Object.keys(mergedLabels).length > 0 && { labels: mergedLabels }
705
709
  }
706
710
  });
707
711
  this.lastResponseMetadata = {
package/index.js CHANGED
@@ -194,7 +194,7 @@ function AITransformFactory(options = {}) {
194
194
  // Vertex AI configuration
195
195
  this.vertexai = options.vertexai || false;
196
196
  this.project = options.project || process.env.GOOGLE_CLOUD_PROJECT || null;
197
- this.location = options.location || process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
197
+ this.location = options.location || process.env.GOOGLE_CLOUD_LOCATION || undefined;
198
198
  this.googleAuthOptions = options.googleAuthOptions || null;
199
199
 
200
200
  // API Key (for Gemini API, not Vertex AI)
@@ -295,10 +295,14 @@ function AITransformFactory(options = {}) {
295
295
  this.enableGrounding = options.enableGrounding || false;
296
296
  this.groundingConfig = options.groundingConfig || {};
297
297
 
298
- // Billing labels for cost segmentation
298
+ // Billing labels for cost segmentation (Vertex AI only)
299
299
  this.labels = options.labels || {};
300
300
  if (Object.keys(this.labels).length > 0 && log.level !== 'silent') {
301
- log.debug(`Billing labels configured: ${JSON.stringify(this.labels)}`);
301
+ if (!this.vertexai) {
302
+ log.warn(`Billing labels are only supported with Vertex AI. Labels will be ignored.`);
303
+ } else {
304
+ log.debug(`Billing labels configured: ${JSON.stringify(this.labels)}`);
305
+ }
302
306
  }
303
307
 
304
308
  if (this.promptKey === this.answerKey) {
@@ -311,7 +315,7 @@ function AITransformFactory(options = {}) {
311
315
  log.debug(`Max output tokens set to: ${this.chatConfig.maxOutputTokens}`);
312
316
  // Log authentication method
313
317
  if (this.vertexai) {
314
- log.debug(`Using Vertex AI - Project: ${this.project}, Location: ${this.location}`);
318
+ log.debug(`Using Vertex AI - Project: ${this.project}, Location: ${this.location || 'global (default)'}`);
315
319
  if (this.googleAuthOptions?.keyFilename) {
316
320
  log.debug(`Auth: Service account key file: ${this.googleAuthOptions.keyFilename}`);
317
321
  } else if (this.googleAuthOptions?.credentials) {
@@ -330,7 +334,7 @@ function AITransformFactory(options = {}) {
330
334
  ? {
331
335
  vertexai: true,
332
336
  project: this.project,
333
- location: this.location,
337
+ ...(this.location && { location: this.location }),
334
338
  ...(this.googleAuthOptions && { googleAuthOptions: this.googleAuthOptions })
335
339
  }
336
340
  : { apiKey: this.apiKey };
@@ -357,7 +361,7 @@ async function initChat(force = false) {
357
361
  // @ts-ignore
358
362
  config: {
359
363
  ...this.chatConfig,
360
- ...(Object.keys(this.labels).length > 0 && { labels: this.labels })
364
+ ...(this.vertexai && Object.keys(this.labels).length > 0 && { labels: this.labels })
361
365
  },
362
366
  history: [],
363
367
  };
@@ -472,7 +476,7 @@ async function seedWithExamples(examples) {
472
476
  // @ts-ignore
473
477
  config: {
474
478
  ...this.chatConfig,
475
- ...(Object.keys(this.labels).length > 0 && { labels: this.labels })
479
+ ...(this.vertexai && Object.keys(this.labels).length > 0 && { labels: this.labels })
476
480
  },
477
481
  history: [...currentHistory, ...historyToAdd],
478
482
  });
@@ -509,13 +513,14 @@ async function rawMessage(sourcePayload, messageOptions = {}) {
509
513
  : JSON.stringify(sourcePayload, null, 2);
510
514
 
511
515
  // Merge instance labels with per-message labels (per-message takes precedence)
516
+ // Labels only supported with Vertex AI
512
517
  const mergedLabels = { ...this.labels, ...(messageOptions.labels || {}) };
513
- const hasLabels = Object.keys(mergedLabels).length > 0;
518
+ const hasLabels = this.vertexai && Object.keys(mergedLabels).length > 0;
514
519
 
515
520
  try {
516
521
  const sendParams = { message: actualPayload };
517
522
 
518
- // Add config with labels if we have any
523
+ // Add config with labels if we have any (Vertex AI only)
519
524
  if (hasLabels) {
520
525
  sendParams.config = { labels: mergedLabels };
521
526
  }
@@ -862,7 +867,7 @@ async function resetChat() {
862
867
  // @ts-ignore
863
868
  config: {
864
869
  ...this.chatConfig,
865
- ...(Object.keys(this.labels).length > 0 && { labels: this.labels })
870
+ ...(this.vertexai && Object.keys(this.labels).length > 0 && { labels: this.labels })
866
871
  },
867
872
  history: [],
868
873
  };
@@ -933,7 +938,7 @@ async function clearConversation() {
933
938
  // @ts-ignore
934
939
  config: {
935
940
  ...this.chatConfig,
936
- ...(Object.keys(this.labels).length > 0 && { labels: this.labels })
941
+ ...(this.vertexai && Object.keys(this.labels).length > 0 && { labels: this.labels })
937
942
  },
938
943
  history: exampleHistory,
939
944
  });
@@ -1019,7 +1024,7 @@ async function statelessMessage(sourcePayload, options = {}, validatorFn = null)
1019
1024
  // Add the user message
1020
1025
  contents.push({ role: 'user', parts: [{ text: payloadStr }] });
1021
1026
 
1022
- // Merge labels
1027
+ // Merge labels (Vertex AI only)
1023
1028
  const mergedLabels = { ...this.labels, ...(options.labels || {}) };
1024
1029
 
1025
1030
  // Use generateContent instead of chat.sendMessage
@@ -1028,7 +1033,7 @@ async function statelessMessage(sourcePayload, options = {}, validatorFn = null)
1028
1033
  contents: contents,
1029
1034
  config: {
1030
1035
  ...this.chatConfig,
1031
- ...(Object.keys(mergedLabels).length > 0 && { labels: mergedLabels })
1036
+ ...(this.vertexai && Object.keys(mergedLabels).length > 0 && { labels: mergedLabels })
1032
1037
  }
1033
1038
  });
1034
1039
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "ak-gemini",
3
3
  "author": "ak@mixpanel.com",
4
4
  "description": "AK's Generative AI Helper for doing... transforms",
5
- "version": "1.1.0",
5
+ "version": "1.1.1",
6
6
  "main": "index.js",
7
7
  "files": [
8
8
  "index.js",
package/types.d.ts CHANGED
@@ -87,6 +87,7 @@ export interface AITransformerContext {
87
87
  enableGrounding?: boolean; // Enable Google Search grounding (default: false, WARNING: costs $35/1k queries)
88
88
  groundingConfig?: Record<string, any>; // Additional grounding configuration options
89
89
  labels?: Record<string, string>; // Custom labels for billing segmentation (keys: 1-63 chars lowercase, values: max 63 chars)
90
+ vertexai?: boolean; // Whether using Vertex AI (true) or Gemini API (false)
90
91
  estimate?: (nextPayload: Record<string, unknown> | string) => Promise<{ inputTokens: number }>;
91
92
  getLastUsage?: () => UsageData | null;
92
93
  lastResponseMetadata?: ResponseMetadata | null; // Metadata from the last API response
@@ -153,7 +154,7 @@ export interface AITransformerOptions {
153
154
  // Use these instead of apiKey for Vertex AI with service account authentication
154
155
  vertexai?: boolean; // Set to true to use Vertex AI instead of Gemini API
155
156
  project?: string; // Google Cloud project ID (required for Vertex AI)
156
- location?: string; // Google Cloud location/region (e.g., 'us-central1') - required for Vertex AI
157
+ location?: string; // Google Cloud location/region (e.g., 'us-central1'). If not set, defaults to 'global' endpoint
157
158
  googleAuthOptions?: GoogleAuthOptions; // Authentication options for Vertex AI (keyFilename, credentials, etc.)
158
159
  }
159
160
 
@@ -185,6 +186,7 @@ export declare class AITransformer {
185
186
  enableGrounding: boolean;
186
187
  groundingConfig: Record<string, any>;
187
188
  labels: Record<string, string>;
189
+ vertexai: boolean;
188
190
  /** Metadata from the last API response (model version, token counts, etc.) */
189
191
  lastResponseMetadata: ResponseMetadata | null;
190
192
  /** Number of history items that are seeded examples (used by clearConversation) */