@posthog/ai 7.8.10 → 7.8.12

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.ts CHANGED
@@ -31,6 +31,7 @@ type PromptVariables = Record<string, string | number | boolean>;
31
31
  */
32
32
  interface PromptsDirectOptions {
33
33
  personalApiKey: string;
34
+ projectApiKey: string;
34
35
  host?: string;
35
36
  defaultCacheTtlSeconds?: number;
36
37
  }
@@ -308,6 +309,7 @@ type PromptsOptions = PromptsWithPostHogOptions | PromptsDirectOptions;
308
309
  * // Or with direct options (no PostHog client needed)
309
310
  * const prompts = new Prompts({
310
311
  * personalApiKey: 'phx_xxx',
312
+ * projectApiKey: 'phc_xxx',
311
313
  * host: 'https://us.posthog.com',
312
314
  * })
313
315
  *
@@ -326,6 +328,7 @@ type PromptsOptions = PromptsWithPostHogOptions | PromptsDirectOptions;
326
328
  */
327
329
  declare class Prompts {
328
330
  private personalApiKey;
331
+ private projectApiKey;
329
332
  private host;
330
333
  private defaultCacheTtlSeconds;
331
334
  private cache;
package/dist/index.mjs CHANGED
@@ -1,12 +1,11 @@
1
1
  import { OpenAI, AzureOpenAI } from 'openai';
2
- import { Buffer } from 'buffer';
3
2
  import * as uuid from 'uuid';
4
3
  import { v4 } from 'uuid';
5
4
  import { uuidv7 } from '@posthog/core';
6
5
  import AnthropicOriginal from '@anthropic-ai/sdk';
7
6
  import { GoogleGenAI } from '@google/genai';
8
7
 
9
- var version = "7.8.10";
8
+ var version = "7.8.12";
10
9
 
11
10
  // Type guards for safer type checking
12
11
  const isString = value => {
@@ -414,9 +413,17 @@ const formatResponseGemini = response => {
414
413
  // Handle audio/media inline data
415
414
  const mimeType = part.inlineData.mimeType || 'audio/pcm';
416
415
  let data = part.inlineData.data;
417
- // Handle binary data (Buffer/Uint8Array -> base64)
418
- if (data instanceof Uint8Array || Buffer.isBuffer(data)) {
419
- data = Buffer.from(data).toString('base64');
416
+ // Handle binary data (Uint8Array/Buffer -> base64)
417
+ if (data instanceof Uint8Array) {
418
+ if (typeof Buffer !== 'undefined') {
419
+ data = Buffer.from(data).toString('base64');
420
+ } else {
421
+ let binary = '';
422
+ for (let i = 0; i < data.length; i++) {
423
+ binary += String.fromCharCode(data[i]);
424
+ }
425
+ data = btoa(binary);
426
+ }
420
427
  }
421
428
  // Sanitize base64 data for images and other large inline data
422
429
  data = redactBase64DataUrl(data);
@@ -642,7 +649,8 @@ function sanitizeValues(obj) {
642
649
  }
643
650
  const jsonSafe = JSON.parse(JSON.stringify(obj));
644
651
  if (typeof jsonSafe === 'string') {
645
- return Buffer.from(jsonSafe, STRING_FORMAT).toString(STRING_FORMAT);
652
+ // Sanitize lone surrogates by round-tripping through UTF-8
653
+ return new TextDecoder().decode(new TextEncoder().encode(jsonSafe));
646
654
  } else if (Array.isArray(jsonSafe)) {
647
655
  return jsonSafe.map(sanitizeValues);
648
656
  } else if (jsonSafe && typeof jsonSafe === 'object') {
@@ -2111,11 +2119,12 @@ const mapVercelPrompt = messages => {
2111
2119
  });
2112
2120
  try {
2113
2121
  // Trim the inputs array until its JSON size fits within MAX_OUTPUT_SIZE
2122
+ const encoder = new TextEncoder();
2114
2123
  let serialized = JSON.stringify(inputs);
2115
2124
  let removedCount = 0;
2116
2125
  // We need to keep track of the initial size of the inputs array because we're going to be mutating it
2117
2126
  const initialSize = inputs.length;
2118
- for (let i = 0; i < initialSize && Buffer.byteLength(serialized, 'utf8') > MAX_OUTPUT_SIZE; i++) {
2127
+ for (let i = 0; i < initialSize && encoder.encode(serialized).byteLength > MAX_OUTPUT_SIZE; i++) {
2119
2128
  inputs.shift();
2120
2129
  removedCount++;
2121
2130
  serialized = JSON.stringify(inputs);
@@ -4366,6 +4375,7 @@ function isPromptsWithPostHog(options) {
4366
4375
  * // Or with direct options (no PostHog client needed)
4367
4376
  * const prompts = new Prompts({
4368
4377
  * personalApiKey: 'phx_xxx',
4378
+ * projectApiKey: 'phc_xxx',
4369
4379
  * host: 'https://us.posthog.com',
4370
4380
  * })
4371
4381
  *
@@ -4388,10 +4398,12 @@ class Prompts {
4388
4398
  this.defaultCacheTtlSeconds = options.defaultCacheTtlSeconds ?? DEFAULT_CACHE_TTL_SECONDS;
4389
4399
  if (isPromptsWithPostHog(options)) {
4390
4400
  this.personalApiKey = options.posthog.options.personalApiKey ?? '';
4401
+ this.projectApiKey = options.posthog.apiKey ?? '';
4391
4402
  this.host = options.posthog.host;
4392
4403
  } else {
4393
4404
  // Direct options
4394
4405
  this.personalApiKey = options.personalApiKey;
4406
+ this.projectApiKey = options.projectApiKey;
4395
4407
  this.host = options.host ?? 'https://us.posthog.com';
4396
4408
  }
4397
4409
  }
@@ -4475,7 +4487,12 @@ class Prompts {
4475
4487
  if (!this.personalApiKey) {
4476
4488
  throw new Error('[PostHog Prompts] personalApiKey is required to fetch prompts. ' + 'Please provide it when initializing the Prompts instance.');
4477
4489
  }
4478
- const url = `${this.host}/api/environments/@current/llm_prompts/name/${encodeURIComponent(name)}/`;
4490
+ if (!this.projectApiKey) {
4491
+ throw new Error('[PostHog Prompts] projectApiKey is required to fetch prompts. ' + 'Please provide it when initializing the Prompts instance.');
4492
+ }
4493
+ const encodedPromptName = encodeURIComponent(name);
4494
+ const encodedProjectApiKey = encodeURIComponent(this.projectApiKey);
4495
+ const url = `${this.host}/api/environments/@current/llm_prompts/name/${encodedPromptName}/?token=${encodedProjectApiKey}`;
4479
4496
  const response = await fetch(url, {
4480
4497
  method: 'GET',
4481
4498
  headers: {