@posthog/ai 7.10.0 → 7.11.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.10.0";
13
+ var version = "7.11.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.10.0";
5
+ var version = "7.11.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.10.0";
9
+ var version = "7.11.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.10.0";
5
+ var version = "7.11.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.10.0";
32
+ var version = "7.11.1";
33
33
 
34
34
  // Type guards for safer type checking
35
35
  const isString = value => {
@@ -5213,8 +5213,17 @@ class Prompts {
5213
5213
  this.host = options.host ?? 'https://us.posthog.com';
5214
5214
  }
5215
5215
  }
5216
- getCacheKey(name, version) {
5217
- return version === undefined ? `${name}::latest` : `${name}::version:${version}`;
5216
+ getPromptCache(name) {
5217
+ return this.cache.get(name);
5218
+ }
5219
+ getOrCreatePromptCache(name) {
5220
+ const cachedPromptVersions = this.cache.get(name);
5221
+ if (cachedPromptVersions) {
5222
+ return cachedPromptVersions;
5223
+ }
5224
+ const promptVersions = new Map();
5225
+ this.cache.set(name, promptVersions);
5226
+ return promptVersions;
5218
5227
  }
5219
5228
  getPromptLabel(name, version) {
5220
5229
  return version === undefined ? `"${name}"` : `"${name}" version ${version}`;
@@ -5231,10 +5240,9 @@ class Prompts {
5231
5240
  const cacheTtlSeconds = options?.cacheTtlSeconds ?? this.defaultCacheTtlSeconds;
5232
5241
  const fallback = options?.fallback;
5233
5242
  const version = options?.version;
5234
- const cacheKey = this.getCacheKey(name, version);
5235
5243
  const promptLabel = this.getPromptLabel(name, version);
5236
5244
  // Check cache first
5237
- const cached = this.cache.get(cacheKey);
5245
+ const cached = this.getPromptCache(name)?.get(version);
5238
5246
  const now = Date.now();
5239
5247
  if (cached) {
5240
5248
  const isFresh = now - cached.fetchedAt < cacheTtlSeconds * 1000;
@@ -5247,7 +5255,7 @@ class Prompts {
5247
5255
  const prompt = await this.fetchPromptFromApi(name, version);
5248
5256
  const fetchedAt = Date.now();
5249
5257
  // Update cache
5250
- this.cache.set(cacheKey, {
5258
+ this.getOrCreatePromptCache(name).set(version, {
5251
5259
  prompt,
5252
5260
  fetchedAt
5253
5261
  });
@@ -5289,23 +5297,25 @@ class Prompts {
5289
5297
  /**
5290
5298
  * Clear the cache for a specific prompt or all prompts
5291
5299
  *
5292
- * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt.
5300
+ * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt unless a version is also provided.
5301
+ * @param version - Optional prompt version to clear. Requires a prompt name.
5293
5302
  */
5294
- clearCache(name) {
5295
- if (name !== undefined) {
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
- }
5307
- } else {
5303
+ clearCache(name, version) {
5304
+ if (version !== undefined && name === undefined) {
5305
+ throw new Error("'version' requires 'name' to be provided");
5306
+ }
5307
+ if (name === undefined) {
5308
5308
  this.cache.clear();
5309
+ return;
5310
+ }
5311
+ if (version === undefined) {
5312
+ this.cache.delete(name);
5313
+ return;
5314
+ }
5315
+ const promptVersions = this.getPromptCache(name);
5316
+ promptVersions?.delete(version);
5317
+ if (promptVersions?.size === 0) {
5318
+ this.cache.delete(name);
5309
5319
  }
5310
5320
  }
5311
5321
  async fetchPromptFromApi(name, version) {