@posthog/ai 7.15.0 → 7.16.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.
@@ -10,7 +10,7 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
10
 
11
11
  var AnthropicOriginal__default = /*#__PURE__*/_interopDefault(AnthropicOriginal);
12
12
 
13
- var version = "7.15.0";
13
+ var version = "7.16.0";
14
14
 
15
15
  // Type guards for safer type checking
16
16
 
@@ -2,7 +2,7 @@ import AnthropicOriginal from '@anthropic-ai/sdk';
2
2
  import { v4 } from 'uuid';
3
3
  import { uuidv7 } from '@posthog/core';
4
4
 
5
- var version = "7.15.0";
5
+ var version = "7.16.0";
6
6
 
7
7
  // Type guards for safer type checking
8
8
 
@@ -6,7 +6,7 @@ var genai = require('@google/genai');
6
6
  var uuid = require('uuid');
7
7
  var core = require('@posthog/core');
8
8
 
9
- var version = "7.15.0";
9
+ var version = "7.16.0";
10
10
 
11
11
  // Type guards for safer type checking
12
12
 
@@ -2,7 +2,7 @@ import { GoogleGenAI } from '@google/genai';
2
2
  import { v4 } from 'uuid';
3
3
  import { uuidv7 } from '@posthog/core';
4
4
 
5
- var version = "7.15.0";
5
+ var version = "7.16.0";
6
6
 
7
7
  // Type guards for safer type checking
8
8
 
package/dist/index.cjs CHANGED
@@ -29,7 +29,7 @@ function _interopNamespace(e) {
29
29
  var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
30
30
  var AnthropicOriginal__default = /*#__PURE__*/_interopDefault(AnthropicOriginal);
31
31
 
32
- var version = "7.15.0";
32
+ var version = "7.16.0";
33
33
 
34
34
  // Type guards for safer type checking
35
35
  const isString = value => {
@@ -4461,7 +4461,11 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
4461
4461
  /// <reference lib="dom" />
4462
4462
  const DEFAULT_CACHE_TTL_SECONDS = 300; // 5 minutes
4463
4463
  function isPromptApiResponse(data) {
4464
- return typeof data === 'object' && data !== null && 'prompt' in data && typeof data.prompt === 'string';
4464
+ if (typeof data !== 'object' || data === null) {
4465
+ return false;
4466
+ }
4467
+ const record = data;
4468
+ return typeof record.prompt === 'string' && typeof record.name === 'string' && typeof record.version === 'number';
4465
4469
  }
4466
4470
  function isPromptsWithPostHog(options) {
4467
4471
  return 'posthog' in options;
@@ -4502,6 +4506,7 @@ function isPromptsWithPostHog(options) {
4502
4506
  class Prompts {
4503
4507
  constructor(options) {
4504
4508
  this.cache = new Map();
4509
+ this.hasWarnedDeprecation = false;
4505
4510
  this.defaultCacheTtlSeconds = options.defaultCacheTtlSeconds ?? DEFAULT_CACHE_TTL_SECONDS;
4506
4511
  if (isPromptsWithPostHog(options)) {
4507
4512
  this.personalApiKey = options.posthog.options.personalApiKey ?? '';
@@ -4529,17 +4534,42 @@ class Prompts {
4529
4534
  getPromptLabel(name, version) {
4530
4535
  return version === undefined ? `"${name}"` : `"${name}" version ${version}`;
4531
4536
  }
4537
+ async get(name, options) {
4538
+ const withMetadata = options?.withMetadata;
4539
+ if (withMetadata === undefined && !this.hasWarnedDeprecation) {
4540
+ this.hasWarnedDeprecation = true;
4541
+ console.warn('[PostHog Prompts] Calling get() without { withMetadata: true } is deprecated and will be ' + 'removed in a future major version. Pass { withMetadata: true } to receive a PromptResult ' + 'object with source, name, and version metadata. ' + 'You can pass { withMetadata: false } to silence this warning, but the plain-string return ' + 'will still be removed in the next major version.');
4542
+ }
4543
+ try {
4544
+ const result = await this.getInternal(name, options);
4545
+ if (withMetadata) {
4546
+ return result;
4547
+ }
4548
+ return result.prompt;
4549
+ } catch (error) {
4550
+ const fallback = options?.fallback;
4551
+ if (fallback !== undefined) {
4552
+ const promptLabel = this.getPromptLabel(name, options?.version);
4553
+ console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using fallback:`, error);
4554
+ if (withMetadata) {
4555
+ return {
4556
+ source: 'code_fallback',
4557
+ prompt: fallback,
4558
+ name: undefined,
4559
+ version: undefined
4560
+ };
4561
+ }
4562
+ return fallback;
4563
+ }
4564
+ throw error;
4565
+ }
4566
+ }
4532
4567
  /**
4533
- * Fetch a prompt by name from the PostHog API
4534
- *
4535
- * @param name - The name of the prompt to fetch
4536
- * @param options - Optional settings for caching, fallback, and exact version selection
4537
- * @returns The prompt string
4538
- * @throws Error if the prompt cannot be fetched and no fallback is provided
4568
+ * Internal method that handles cache + fetch logic, returning full metadata.
4569
+ * Does NOT handle the string `fallback` option — callers handle that.
4539
4570
  */
4540
- async get(name, options) {
4571
+ async getInternal(name, options) {
4541
4572
  const cacheTtlSeconds = options?.cacheTtlSeconds ?? this.defaultCacheTtlSeconds;
4542
- const fallback = options?.fallback;
4543
4573
  const version = options?.version;
4544
4574
  const promptLabel = this.getPromptLabel(name, version);
4545
4575
  // Check cache first
@@ -4548,32 +4578,41 @@ class Prompts {
4548
4578
  if (cached) {
4549
4579
  const isFresh = now - cached.fetchedAt < cacheTtlSeconds * 1000;
4550
4580
  if (isFresh) {
4551
- return cached.prompt;
4581
+ const {
4582
+ fetchedAt: _,
4583
+ ...cachedResult
4584
+ } = cached;
4585
+ return {
4586
+ source: 'cache',
4587
+ ...cachedResult
4588
+ };
4552
4589
  }
4553
4590
  }
4554
4591
  // Try to fetch from API
4555
4592
  try {
4556
- const prompt = await this.fetchPromptFromApi(name, version);
4557
- const fetchedAt = Date.now();
4593
+ const fetched = await this.fetchPromptFromApi(name, version);
4558
4594
  // Update cache
4559
4595
  this.getOrCreatePromptCache(name).set(version, {
4560
- prompt,
4561
- fetchedAt
4596
+ ...fetched,
4597
+ fetchedAt: Date.now()
4562
4598
  });
4563
- return prompt;
4599
+ return {
4600
+ source: 'api',
4601
+ ...fetched
4602
+ };
4564
4603
  } catch (error) {
4565
- // Fallback order:
4566
- // 1. Return stale cache (with warning)
4604
+ // Return stale cache (with warning)
4567
4605
  if (cached) {
4606
+ const {
4607
+ fetchedAt: _,
4608
+ ...cachedResult
4609
+ } = cached;
4568
4610
  console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using stale cache:`, error);
4569
- return cached.prompt;
4570
- }
4571
- // 2. Return fallback (with warning)
4572
- if (fallback !== undefined) {
4573
- console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using fallback:`, error);
4574
- return fallback;
4611
+ return {
4612
+ source: 'stale_cache',
4613
+ ...cachedResult
4614
+ };
4575
4615
  }
4576
- // 3. Throw error
4577
4616
  throw error;
4578
4617
  }
4579
4618
  }
@@ -4650,7 +4689,11 @@ class Prompts {
4650
4689
  if (!isPromptApiResponse(data)) {
4651
4690
  throw new Error(`[PostHog Prompts] Invalid response format for prompt ${promptLabel}`);
4652
4691
  }
4653
- return data.prompt;
4692
+ return {
4693
+ prompt: data.prompt,
4694
+ name: data.name,
4695
+ version: data.version
4696
+ };
4654
4697
  }
4655
4698
  }
4656
4699