@posthog/ai 7.9.5 → 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/anthropic/index.cjs +1 -1
- package/dist/anthropic/index.mjs +1 -1
- package/dist/gemini/index.cjs +1 -1
- package/dist/gemini/index.mjs +1 -1
- package/dist/index.cjs +54 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +13 -3
- package/dist/index.mjs +54 -18
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +1 -1
- package/dist/langchain/index.mjs +1 -1
- package/dist/openai/index.cjs +1 -1
- package/dist/openai/index.mjs +1 -1
- package/dist/otel/index.cjs +1 -1
- package/dist/otel/index.mjs +1 -1
- package/dist/vercel/index.cjs +1 -1
- package/dist/vercel/index.mjs +1 -1
- package/package.json +3 -3
package/dist/anthropic/index.cjs
CHANGED
|
@@ -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.
|
|
13
|
+
var version = "7.11.0";
|
|
14
14
|
|
|
15
15
|
// Type guards for safer type checking
|
|
16
16
|
|
package/dist/anthropic/index.mjs
CHANGED
package/dist/gemini/index.cjs
CHANGED
package/dist/gemini/index.mjs
CHANGED
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.
|
|
32
|
+
var version = "7.11.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,36 @@ class Prompts {
|
|
|
5208
5213
|
this.host = options.host ?? 'https://us.posthog.com';
|
|
5209
5214
|
}
|
|
5210
5215
|
}
|
|
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;
|
|
5227
|
+
}
|
|
5228
|
+
getPromptLabel(name, version) {
|
|
5229
|
+
return version === undefined ? `"${name}"` : `"${name}" version ${version}`;
|
|
5230
|
+
}
|
|
5211
5231
|
/**
|
|
5212
5232
|
* Fetch a prompt by name from the PostHog API
|
|
5213
5233
|
*
|
|
5214
5234
|
* @param name - The name of the prompt to fetch
|
|
5215
|
-
* @param options - Optional settings for caching and
|
|
5235
|
+
* @param options - Optional settings for caching, fallback, and exact version selection
|
|
5216
5236
|
* @returns The prompt string
|
|
5217
5237
|
* @throws Error if the prompt cannot be fetched and no fallback is provided
|
|
5218
5238
|
*/
|
|
5219
5239
|
async get(name, options) {
|
|
5220
5240
|
const cacheTtlSeconds = options?.cacheTtlSeconds ?? this.defaultCacheTtlSeconds;
|
|
5221
5241
|
const fallback = options?.fallback;
|
|
5242
|
+
const version = options?.version;
|
|
5243
|
+
const promptLabel = this.getPromptLabel(name, version);
|
|
5222
5244
|
// Check cache first
|
|
5223
|
-
const cached = this.
|
|
5245
|
+
const cached = this.getPromptCache(name)?.get(version);
|
|
5224
5246
|
const now = Date.now();
|
|
5225
5247
|
if (cached) {
|
|
5226
5248
|
const isFresh = now - cached.fetchedAt < cacheTtlSeconds * 1000;
|
|
@@ -5230,10 +5252,10 @@ class Prompts {
|
|
|
5230
5252
|
}
|
|
5231
5253
|
// Try to fetch from API
|
|
5232
5254
|
try {
|
|
5233
|
-
const prompt = await this.fetchPromptFromApi(name);
|
|
5255
|
+
const prompt = await this.fetchPromptFromApi(name, version);
|
|
5234
5256
|
const fetchedAt = Date.now();
|
|
5235
5257
|
// Update cache
|
|
5236
|
-
this.
|
|
5258
|
+
this.getOrCreatePromptCache(name).set(version, {
|
|
5237
5259
|
prompt,
|
|
5238
5260
|
fetchedAt
|
|
5239
5261
|
});
|
|
@@ -5242,12 +5264,12 @@ class Prompts {
|
|
|
5242
5264
|
// Fallback order:
|
|
5243
5265
|
// 1. Return stale cache (with warning)
|
|
5244
5266
|
if (cached) {
|
|
5245
|
-
console.warn(`[PostHog Prompts] Failed to fetch prompt
|
|
5267
|
+
console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using stale cache:`, error);
|
|
5246
5268
|
return cached.prompt;
|
|
5247
5269
|
}
|
|
5248
5270
|
// 2. Return fallback (with warning)
|
|
5249
5271
|
if (fallback !== undefined) {
|
|
5250
|
-
console.warn(`[PostHog Prompts] Failed to fetch prompt
|
|
5272
|
+
console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using fallback:`, error);
|
|
5251
5273
|
return fallback;
|
|
5252
5274
|
}
|
|
5253
5275
|
// 3. Throw error
|
|
@@ -5275,16 +5297,28 @@ class Prompts {
|
|
|
5275
5297
|
/**
|
|
5276
5298
|
* Clear the cache for a specific prompt or all prompts
|
|
5277
5299
|
*
|
|
5278
|
-
* @param name - Optional prompt name to clear. If
|
|
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.
|
|
5279
5302
|
*/
|
|
5280
|
-
clearCache(name) {
|
|
5281
|
-
if (
|
|
5282
|
-
|
|
5283
|
-
}
|
|
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) {
|
|
5284
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);
|
|
5285
5319
|
}
|
|
5286
5320
|
}
|
|
5287
|
-
async fetchPromptFromApi(name) {
|
|
5321
|
+
async fetchPromptFromApi(name, version) {
|
|
5288
5322
|
if (!this.personalApiKey) {
|
|
5289
5323
|
throw new Error('[PostHog Prompts] personalApiKey is required to fetch prompts. ' + 'Please provide it when initializing the Prompts instance.');
|
|
5290
5324
|
}
|
|
@@ -5293,7 +5327,9 @@ class Prompts {
|
|
|
5293
5327
|
}
|
|
5294
5328
|
const encodedPromptName = encodeURIComponent(name);
|
|
5295
5329
|
const encodedProjectApiKey = encodeURIComponent(this.projectApiKey);
|
|
5296
|
-
const
|
|
5330
|
+
const versionQuery = version === undefined ? '' : `&version=${encodeURIComponent(String(version))}`;
|
|
5331
|
+
const promptLabel = this.getPromptLabel(name, version);
|
|
5332
|
+
const url = `${this.host}/api/environments/@current/llm_prompts/name/${encodedPromptName}/?token=${encodedProjectApiKey}${versionQuery}`;
|
|
5297
5333
|
const response = await fetch(url, {
|
|
5298
5334
|
method: 'GET',
|
|
5299
5335
|
headers: {
|
|
@@ -5302,16 +5338,16 @@ class Prompts {
|
|
|
5302
5338
|
});
|
|
5303
5339
|
if (!response.ok) {
|
|
5304
5340
|
if (response.status === 404) {
|
|
5305
|
-
throw new Error(`[PostHog Prompts] Prompt
|
|
5341
|
+
throw new Error(`[PostHog Prompts] Prompt ${promptLabel} not found`);
|
|
5306
5342
|
}
|
|
5307
5343
|
if (response.status === 403) {
|
|
5308
|
-
throw new Error(`[PostHog Prompts] Access denied for prompt
|
|
5344
|
+
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
5345
|
}
|
|
5310
|
-
throw new Error(`[PostHog Prompts] Failed to fetch prompt
|
|
5346
|
+
throw new Error(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}: HTTP ${response.status}`);
|
|
5311
5347
|
}
|
|
5312
5348
|
const data = await response.json();
|
|
5313
5349
|
if (!isPromptApiResponse(data)) {
|
|
5314
|
-
throw new Error(`[PostHog Prompts] Invalid response format for prompt
|
|
5350
|
+
throw new Error(`[PostHog Prompts] Invalid response format for prompt ${promptLabel}`);
|
|
5315
5351
|
}
|
|
5316
5352
|
return data.prompt;
|
|
5317
5353
|
}
|