@posthog/ai 7.15.0 → 7.16.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.
@@ -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.1";
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.1";
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.1";
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.1";
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.1";
33
33
 
34
34
  // Type guards for safer type checking
35
35
  const isString = value => {
@@ -4460,8 +4460,20 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
4460
4460
 
4461
4461
  /// <reference lib="dom" />
4462
4462
  const DEFAULT_CACHE_TTL_SECONDS = 300; // 5 minutes
4463
+ const DEFAULT_PROMPTS_HOST = 'https://us.posthog.com';
4464
+ function normalizeApiKey(value) {
4465
+ return typeof value === 'string' ? value.trim() : '';
4466
+ }
4467
+ function normalizeHost(value) {
4468
+ const normalizedHost = typeof value === 'string' ? value.trim() : '';
4469
+ return (normalizedHost || DEFAULT_PROMPTS_HOST).replace(/\/+$/, '');
4470
+ }
4463
4471
  function isPromptApiResponse(data) {
4464
- return typeof data === 'object' && data !== null && 'prompt' in data && typeof data.prompt === 'string';
4472
+ if (typeof data !== 'object' || data === null) {
4473
+ return false;
4474
+ }
4475
+ const record = data;
4476
+ return typeof record.prompt === 'string' && typeof record.name === 'string' && typeof record.version === 'number';
4465
4477
  }
4466
4478
  function isPromptsWithPostHog(options) {
4467
4479
  return 'posthog' in options;
@@ -4502,16 +4514,17 @@ function isPromptsWithPostHog(options) {
4502
4514
  class Prompts {
4503
4515
  constructor(options) {
4504
4516
  this.cache = new Map();
4517
+ this.hasWarnedDeprecation = false;
4505
4518
  this.defaultCacheTtlSeconds = options.defaultCacheTtlSeconds ?? DEFAULT_CACHE_TTL_SECONDS;
4506
4519
  if (isPromptsWithPostHog(options)) {
4507
4520
  this.personalApiKey = options.posthog.options.personalApiKey ?? '';
4508
- this.projectApiKey = options.posthog.apiKey ?? '';
4521
+ this.projectApiKey = options.posthog.apiKey;
4509
4522
  this.host = options.posthog.host;
4510
4523
  } else {
4511
4524
  // Direct options
4512
- this.personalApiKey = options.personalApiKey;
4513
- this.projectApiKey = options.projectApiKey;
4514
- this.host = options.host ?? 'https://us.posthog.com';
4525
+ this.personalApiKey = normalizeApiKey(options.personalApiKey);
4526
+ this.projectApiKey = normalizeApiKey(options.projectApiKey);
4527
+ this.host = normalizeHost(options.host);
4515
4528
  }
4516
4529
  }
4517
4530
  getPromptCache(name) {
@@ -4529,17 +4542,42 @@ class Prompts {
4529
4542
  getPromptLabel(name, version) {
4530
4543
  return version === undefined ? `"${name}"` : `"${name}" version ${version}`;
4531
4544
  }
4545
+ async get(name, options) {
4546
+ const withMetadata = options?.withMetadata;
4547
+ if (withMetadata === undefined && !this.hasWarnedDeprecation) {
4548
+ this.hasWarnedDeprecation = true;
4549
+ 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.');
4550
+ }
4551
+ try {
4552
+ const result = await this.getInternal(name, options);
4553
+ if (withMetadata) {
4554
+ return result;
4555
+ }
4556
+ return result.prompt;
4557
+ } catch (error) {
4558
+ const fallback = options?.fallback;
4559
+ if (fallback !== undefined) {
4560
+ const promptLabel = this.getPromptLabel(name, options?.version);
4561
+ console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using fallback:`, error);
4562
+ if (withMetadata) {
4563
+ return {
4564
+ source: 'code_fallback',
4565
+ prompt: fallback,
4566
+ name: undefined,
4567
+ version: undefined
4568
+ };
4569
+ }
4570
+ return fallback;
4571
+ }
4572
+ throw error;
4573
+ }
4574
+ }
4532
4575
  /**
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
4576
+ * Internal method that handles cache + fetch logic, returning full metadata.
4577
+ * Does NOT handle the string `fallback` option — callers handle that.
4539
4578
  */
4540
- async get(name, options) {
4579
+ async getInternal(name, options) {
4541
4580
  const cacheTtlSeconds = options?.cacheTtlSeconds ?? this.defaultCacheTtlSeconds;
4542
- const fallback = options?.fallback;
4543
4581
  const version = options?.version;
4544
4582
  const promptLabel = this.getPromptLabel(name, version);
4545
4583
  // Check cache first
@@ -4548,32 +4586,41 @@ class Prompts {
4548
4586
  if (cached) {
4549
4587
  const isFresh = now - cached.fetchedAt < cacheTtlSeconds * 1000;
4550
4588
  if (isFresh) {
4551
- return cached.prompt;
4589
+ const {
4590
+ fetchedAt: _,
4591
+ ...cachedResult
4592
+ } = cached;
4593
+ return {
4594
+ source: 'cache',
4595
+ ...cachedResult
4596
+ };
4552
4597
  }
4553
4598
  }
4554
4599
  // Try to fetch from API
4555
4600
  try {
4556
- const prompt = await this.fetchPromptFromApi(name, version);
4557
- const fetchedAt = Date.now();
4601
+ const fetched = await this.fetchPromptFromApi(name, version);
4558
4602
  // Update cache
4559
4603
  this.getOrCreatePromptCache(name).set(version, {
4560
- prompt,
4561
- fetchedAt
4604
+ ...fetched,
4605
+ fetchedAt: Date.now()
4562
4606
  });
4563
- return prompt;
4607
+ return {
4608
+ source: 'api',
4609
+ ...fetched
4610
+ };
4564
4611
  } catch (error) {
4565
- // Fallback order:
4566
- // 1. Return stale cache (with warning)
4612
+ // Return stale cache (with warning)
4567
4613
  if (cached) {
4614
+ const {
4615
+ fetchedAt: _,
4616
+ ...cachedResult
4617
+ } = cached;
4568
4618
  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;
4619
+ return {
4620
+ source: 'stale_cache',
4621
+ ...cachedResult
4622
+ };
4575
4623
  }
4576
- // 3. Throw error
4577
4624
  throw error;
4578
4625
  }
4579
4626
  }
@@ -4650,7 +4697,11 @@ class Prompts {
4650
4697
  if (!isPromptApiResponse(data)) {
4651
4698
  throw new Error(`[PostHog Prompts] Invalid response format for prompt ${promptLabel}`);
4652
4699
  }
4653
- return data.prompt;
4700
+ return {
4701
+ prompt: data.prompt,
4702
+ name: data.name,
4703
+ version: data.version
4704
+ };
4654
4705
  }
4655
4706
  }
4656
4707