@posthog/ai 7.10.0 → 7.11.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.
package/dist/index.d.ts CHANGED
@@ -403,7 +403,8 @@ declare class Prompts {
403
403
  private defaultCacheTtlSeconds;
404
404
  private cache;
405
405
  constructor(options: PromptsOptions);
406
- private getCacheKey;
406
+ private getPromptCache;
407
+ private getOrCreatePromptCache;
407
408
  private getPromptLabel;
408
409
  /**
409
410
  * Fetch a prompt by name from the PostHog API
@@ -428,9 +429,10 @@ declare class Prompts {
428
429
  /**
429
430
  * Clear the cache for a specific prompt or all prompts
430
431
  *
431
- * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt.
432
+ * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt unless a version is also provided.
433
+ * @param version - Optional prompt version to clear. Requires a prompt name.
432
434
  */
433
- clearCache(name?: string): void;
435
+ clearCache(name?: string, version?: number): void;
434
436
  private fetchPromptFromApi;
435
437
  }
436
438
 
package/dist/index.mjs CHANGED
@@ -5,7 +5,7 @@ import { uuidv7 } from '@posthog/core';
5
5
  import AnthropicOriginal from '@anthropic-ai/sdk';
6
6
  import { GoogleGenAI } from '@google/genai';
7
7
 
8
- var version = "7.10.0";
8
+ var version = "7.11.0";
9
9
 
10
10
  // Type guards for safer type checking
11
11
  const isString = value => {
@@ -5189,8 +5189,17 @@ class Prompts {
5189
5189
  this.host = options.host ?? 'https://us.posthog.com';
5190
5190
  }
5191
5191
  }
5192
- getCacheKey(name, version) {
5193
- return version === undefined ? `${name}::latest` : `${name}::version:${version}`;
5192
+ getPromptCache(name) {
5193
+ return this.cache.get(name);
5194
+ }
5195
+ getOrCreatePromptCache(name) {
5196
+ const cachedPromptVersions = this.cache.get(name);
5197
+ if (cachedPromptVersions) {
5198
+ return cachedPromptVersions;
5199
+ }
5200
+ const promptVersions = new Map();
5201
+ this.cache.set(name, promptVersions);
5202
+ return promptVersions;
5194
5203
  }
5195
5204
  getPromptLabel(name, version) {
5196
5205
  return version === undefined ? `"${name}"` : `"${name}" version ${version}`;
@@ -5207,10 +5216,9 @@ class Prompts {
5207
5216
  const cacheTtlSeconds = options?.cacheTtlSeconds ?? this.defaultCacheTtlSeconds;
5208
5217
  const fallback = options?.fallback;
5209
5218
  const version = options?.version;
5210
- const cacheKey = this.getCacheKey(name, version);
5211
5219
  const promptLabel = this.getPromptLabel(name, version);
5212
5220
  // Check cache first
5213
- const cached = this.cache.get(cacheKey);
5221
+ const cached = this.getPromptCache(name)?.get(version);
5214
5222
  const now = Date.now();
5215
5223
  if (cached) {
5216
5224
  const isFresh = now - cached.fetchedAt < cacheTtlSeconds * 1000;
@@ -5223,7 +5231,7 @@ class Prompts {
5223
5231
  const prompt = await this.fetchPromptFromApi(name, version);
5224
5232
  const fetchedAt = Date.now();
5225
5233
  // Update cache
5226
- this.cache.set(cacheKey, {
5234
+ this.getOrCreatePromptCache(name).set(version, {
5227
5235
  prompt,
5228
5236
  fetchedAt
5229
5237
  });
@@ -5265,23 +5273,25 @@ class Prompts {
5265
5273
  /**
5266
5274
  * Clear the cache for a specific prompt or all prompts
5267
5275
  *
5268
- * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt.
5276
+ * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt unless a version is also provided.
5277
+ * @param version - Optional prompt version to clear. Requires a prompt name.
5269
5278
  */
5270
- clearCache(name) {
5271
- if (name !== undefined) {
5272
- const latestKey = this.getCacheKey(name);
5273
- const versionPrefix = `${name}::version:`;
5274
- for (const key of this.cache.keys()) {
5275
- if (key === latestKey) {
5276
- this.cache.delete(key);
5277
- continue;
5278
- }
5279
- if (key.startsWith(versionPrefix) && /^\d+$/.test(key.slice(versionPrefix.length))) {
5280
- this.cache.delete(key);
5281
- }
5282
- }
5283
- } else {
5279
+ clearCache(name, version) {
5280
+ if (version !== undefined && name === undefined) {
5281
+ throw new Error("'version' requires 'name' to be provided");
5282
+ }
5283
+ if (name === undefined) {
5284
5284
  this.cache.clear();
5285
+ return;
5286
+ }
5287
+ if (version === undefined) {
5288
+ this.cache.delete(name);
5289
+ return;
5290
+ }
5291
+ const promptVersions = this.getPromptCache(name);
5292
+ promptVersions?.delete(version);
5293
+ if (promptVersions?.size === 0) {
5294
+ this.cache.delete(name);
5285
5295
  }
5286
5296
  }
5287
5297
  async fetchPromptFromApi(name, version) {