@posthog/ai 7.9.5 → 7.10.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.9.5";
13
+ var version = "7.10.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.9.5";
5
+ var version = "7.10.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.9.5";
9
+ var version = "7.10.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.9.5";
5
+ var version = "7.10.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.9.5";
32
+ var version = "7.10.0";
33
33
 
34
34
  // Type guards for safer type checking
35
35
  const isString = value => {
@@ -5186,6 +5186,11 @@ function isPromptsWithPostHog(options) {
5186
5186
  * fallback: 'You are a helpful assistant.',
5187
5187
  * })
5188
5188
  *
5189
+ * // Or fetch an exact published version
5190
+ * const v3Template = await prompts.get('support-system-prompt', {
5191
+ * version: 3,
5192
+ * })
5193
+ *
5189
5194
  * // Compile with variables
5190
5195
  * const systemPrompt = prompts.compile(template, {
5191
5196
  * company: 'Acme Corp',
@@ -5208,19 +5213,28 @@ class Prompts {
5208
5213
  this.host = options.host ?? 'https://us.posthog.com';
5209
5214
  }
5210
5215
  }
5216
+ getCacheKey(name, version) {
5217
+ return version === undefined ? `${name}::latest` : `${name}::version:${version}`;
5218
+ }
5219
+ getPromptLabel(name, version) {
5220
+ return version === undefined ? `"${name}"` : `"${name}" version ${version}`;
5221
+ }
5211
5222
  /**
5212
5223
  * Fetch a prompt by name from the PostHog API
5213
5224
  *
5214
5225
  * @param name - The name of the prompt to fetch
5215
- * @param options - Optional settings for caching and fallback
5226
+ * @param options - Optional settings for caching, fallback, and exact version selection
5216
5227
  * @returns The prompt string
5217
5228
  * @throws Error if the prompt cannot be fetched and no fallback is provided
5218
5229
  */
5219
5230
  async get(name, options) {
5220
5231
  const cacheTtlSeconds = options?.cacheTtlSeconds ?? this.defaultCacheTtlSeconds;
5221
5232
  const fallback = options?.fallback;
5233
+ const version = options?.version;
5234
+ const cacheKey = this.getCacheKey(name, version);
5235
+ const promptLabel = this.getPromptLabel(name, version);
5222
5236
  // Check cache first
5223
- const cached = this.cache.get(name);
5237
+ const cached = this.cache.get(cacheKey);
5224
5238
  const now = Date.now();
5225
5239
  if (cached) {
5226
5240
  const isFresh = now - cached.fetchedAt < cacheTtlSeconds * 1000;
@@ -5230,10 +5244,10 @@ class Prompts {
5230
5244
  }
5231
5245
  // Try to fetch from API
5232
5246
  try {
5233
- const prompt = await this.fetchPromptFromApi(name);
5247
+ const prompt = await this.fetchPromptFromApi(name, version);
5234
5248
  const fetchedAt = Date.now();
5235
5249
  // Update cache
5236
- this.cache.set(name, {
5250
+ this.cache.set(cacheKey, {
5237
5251
  prompt,
5238
5252
  fetchedAt
5239
5253
  });
@@ -5242,12 +5256,12 @@ class Prompts {
5242
5256
  // Fallback order:
5243
5257
  // 1. Return stale cache (with warning)
5244
5258
  if (cached) {
5245
- console.warn(`[PostHog Prompts] Failed to fetch prompt "${name}", using stale cache:`, error);
5259
+ console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using stale cache:`, error);
5246
5260
  return cached.prompt;
5247
5261
  }
5248
5262
  // 2. Return fallback (with warning)
5249
5263
  if (fallback !== undefined) {
5250
- console.warn(`[PostHog Prompts] Failed to fetch prompt "${name}", using fallback:`, error);
5264
+ console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using fallback:`, error);
5251
5265
  return fallback;
5252
5266
  }
5253
5267
  // 3. Throw error
@@ -5275,16 +5289,26 @@ class Prompts {
5275
5289
  /**
5276
5290
  * Clear the cache for a specific prompt or all prompts
5277
5291
  *
5278
- * @param name - Optional prompt name to clear. If not provided, clears all cached prompts.
5292
+ * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt.
5279
5293
  */
5280
5294
  clearCache(name) {
5281
5295
  if (name !== undefined) {
5282
- this.cache.delete(name);
5296
+ const latestKey = this.getCacheKey(name);
5297
+ const versionPrefix = `${name}::version:`;
5298
+ for (const key of this.cache.keys()) {
5299
+ if (key === latestKey) {
5300
+ this.cache.delete(key);
5301
+ continue;
5302
+ }
5303
+ if (key.startsWith(versionPrefix) && /^\d+$/.test(key.slice(versionPrefix.length))) {
5304
+ this.cache.delete(key);
5305
+ }
5306
+ }
5283
5307
  } else {
5284
5308
  this.cache.clear();
5285
5309
  }
5286
5310
  }
5287
- async fetchPromptFromApi(name) {
5311
+ async fetchPromptFromApi(name, version) {
5288
5312
  if (!this.personalApiKey) {
5289
5313
  throw new Error('[PostHog Prompts] personalApiKey is required to fetch prompts. ' + 'Please provide it when initializing the Prompts instance.');
5290
5314
  }
@@ -5293,7 +5317,9 @@ class Prompts {
5293
5317
  }
5294
5318
  const encodedPromptName = encodeURIComponent(name);
5295
5319
  const encodedProjectApiKey = encodeURIComponent(this.projectApiKey);
5296
- const url = `${this.host}/api/environments/@current/llm_prompts/name/${encodedPromptName}/?token=${encodedProjectApiKey}`;
5320
+ const versionQuery = version === undefined ? '' : `&version=${encodeURIComponent(String(version))}`;
5321
+ const promptLabel = this.getPromptLabel(name, version);
5322
+ const url = `${this.host}/api/environments/@current/llm_prompts/name/${encodedPromptName}/?token=${encodedProjectApiKey}${versionQuery}`;
5297
5323
  const response = await fetch(url, {
5298
5324
  method: 'GET',
5299
5325
  headers: {
@@ -5302,16 +5328,16 @@ class Prompts {
5302
5328
  });
5303
5329
  if (!response.ok) {
5304
5330
  if (response.status === 404) {
5305
- throw new Error(`[PostHog Prompts] Prompt "${name}" not found`);
5331
+ throw new Error(`[PostHog Prompts] Prompt ${promptLabel} not found`);
5306
5332
  }
5307
5333
  if (response.status === 403) {
5308
- throw new Error(`[PostHog Prompts] Access denied for prompt "${name}". ` + 'Check that your personalApiKey has the correct permissions and the LLM prompts feature is enabled.');
5334
+ throw new Error(`[PostHog Prompts] Access denied for prompt ${promptLabel}. ` + 'Check that your personalApiKey has the correct permissions and the LLM prompts feature is enabled.');
5309
5335
  }
5310
- throw new Error(`[PostHog Prompts] Failed to fetch prompt "${name}": HTTP ${response.status}`);
5336
+ throw new Error(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}: HTTP ${response.status}`);
5311
5337
  }
5312
5338
  const data = await response.json();
5313
5339
  if (!isPromptApiResponse(data)) {
5314
- throw new Error(`[PostHog Prompts] Invalid response format for prompt "${name}"`);
5340
+ throw new Error(`[PostHog Prompts] Invalid response format for prompt ${promptLabel}`);
5315
5341
  }
5316
5342
  return data.prompt;
5317
5343
  }