@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.
- 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 +41 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +10 -2
- package/dist/index.mjs +41 -15
- 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/index.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ import { BaseMessage } from '@langchain/core/messages';
|
|
|
23
23
|
interface GetPromptOptions {
|
|
24
24
|
cacheTtlSeconds?: number;
|
|
25
25
|
fallback?: string;
|
|
26
|
+
version?: number;
|
|
26
27
|
}
|
|
27
28
|
/**
|
|
28
29
|
* Variables for prompt compilation
|
|
@@ -383,6 +384,11 @@ type PromptsOptions = PromptsWithPostHogOptions | PromptsDirectOptions;
|
|
|
383
384
|
* fallback: 'You are a helpful assistant.',
|
|
384
385
|
* })
|
|
385
386
|
*
|
|
387
|
+
* // Or fetch an exact published version
|
|
388
|
+
* const v3Template = await prompts.get('support-system-prompt', {
|
|
389
|
+
* version: 3,
|
|
390
|
+
* })
|
|
391
|
+
*
|
|
386
392
|
* // Compile with variables
|
|
387
393
|
* const systemPrompt = prompts.compile(template, {
|
|
388
394
|
* company: 'Acme Corp',
|
|
@@ -397,11 +403,13 @@ declare class Prompts {
|
|
|
397
403
|
private defaultCacheTtlSeconds;
|
|
398
404
|
private cache;
|
|
399
405
|
constructor(options: PromptsOptions);
|
|
406
|
+
private getCacheKey;
|
|
407
|
+
private getPromptLabel;
|
|
400
408
|
/**
|
|
401
409
|
* Fetch a prompt by name from the PostHog API
|
|
402
410
|
*
|
|
403
411
|
* @param name - The name of the prompt to fetch
|
|
404
|
-
* @param options - Optional settings for caching and
|
|
412
|
+
* @param options - Optional settings for caching, fallback, and exact version selection
|
|
405
413
|
* @returns The prompt string
|
|
406
414
|
* @throws Error if the prompt cannot be fetched and no fallback is provided
|
|
407
415
|
*/
|
|
@@ -420,7 +428,7 @@ declare class Prompts {
|
|
|
420
428
|
/**
|
|
421
429
|
* Clear the cache for a specific prompt or all prompts
|
|
422
430
|
*
|
|
423
|
-
* @param name - Optional prompt name to clear. If
|
|
431
|
+
* @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt.
|
|
424
432
|
*/
|
|
425
433
|
clearCache(name?: string): void;
|
|
426
434
|
private fetchPromptFromApi;
|
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.
|
|
8
|
+
var version = "7.10.0";
|
|
9
9
|
|
|
10
10
|
// Type guards for safer type checking
|
|
11
11
|
const isString = value => {
|
|
@@ -5162,6 +5162,11 @@ function isPromptsWithPostHog(options) {
|
|
|
5162
5162
|
* fallback: 'You are a helpful assistant.',
|
|
5163
5163
|
* })
|
|
5164
5164
|
*
|
|
5165
|
+
* // Or fetch an exact published version
|
|
5166
|
+
* const v3Template = await prompts.get('support-system-prompt', {
|
|
5167
|
+
* version: 3,
|
|
5168
|
+
* })
|
|
5169
|
+
*
|
|
5165
5170
|
* // Compile with variables
|
|
5166
5171
|
* const systemPrompt = prompts.compile(template, {
|
|
5167
5172
|
* company: 'Acme Corp',
|
|
@@ -5184,19 +5189,28 @@ class Prompts {
|
|
|
5184
5189
|
this.host = options.host ?? 'https://us.posthog.com';
|
|
5185
5190
|
}
|
|
5186
5191
|
}
|
|
5192
|
+
getCacheKey(name, version) {
|
|
5193
|
+
return version === undefined ? `${name}::latest` : `${name}::version:${version}`;
|
|
5194
|
+
}
|
|
5195
|
+
getPromptLabel(name, version) {
|
|
5196
|
+
return version === undefined ? `"${name}"` : `"${name}" version ${version}`;
|
|
5197
|
+
}
|
|
5187
5198
|
/**
|
|
5188
5199
|
* Fetch a prompt by name from the PostHog API
|
|
5189
5200
|
*
|
|
5190
5201
|
* @param name - The name of the prompt to fetch
|
|
5191
|
-
* @param options - Optional settings for caching and
|
|
5202
|
+
* @param options - Optional settings for caching, fallback, and exact version selection
|
|
5192
5203
|
* @returns The prompt string
|
|
5193
5204
|
* @throws Error if the prompt cannot be fetched and no fallback is provided
|
|
5194
5205
|
*/
|
|
5195
5206
|
async get(name, options) {
|
|
5196
5207
|
const cacheTtlSeconds = options?.cacheTtlSeconds ?? this.defaultCacheTtlSeconds;
|
|
5197
5208
|
const fallback = options?.fallback;
|
|
5209
|
+
const version = options?.version;
|
|
5210
|
+
const cacheKey = this.getCacheKey(name, version);
|
|
5211
|
+
const promptLabel = this.getPromptLabel(name, version);
|
|
5198
5212
|
// Check cache first
|
|
5199
|
-
const cached = this.cache.get(
|
|
5213
|
+
const cached = this.cache.get(cacheKey);
|
|
5200
5214
|
const now = Date.now();
|
|
5201
5215
|
if (cached) {
|
|
5202
5216
|
const isFresh = now - cached.fetchedAt < cacheTtlSeconds * 1000;
|
|
@@ -5206,10 +5220,10 @@ class Prompts {
|
|
|
5206
5220
|
}
|
|
5207
5221
|
// Try to fetch from API
|
|
5208
5222
|
try {
|
|
5209
|
-
const prompt = await this.fetchPromptFromApi(name);
|
|
5223
|
+
const prompt = await this.fetchPromptFromApi(name, version);
|
|
5210
5224
|
const fetchedAt = Date.now();
|
|
5211
5225
|
// Update cache
|
|
5212
|
-
this.cache.set(
|
|
5226
|
+
this.cache.set(cacheKey, {
|
|
5213
5227
|
prompt,
|
|
5214
5228
|
fetchedAt
|
|
5215
5229
|
});
|
|
@@ -5218,12 +5232,12 @@ class Prompts {
|
|
|
5218
5232
|
// Fallback order:
|
|
5219
5233
|
// 1. Return stale cache (with warning)
|
|
5220
5234
|
if (cached) {
|
|
5221
|
-
console.warn(`[PostHog Prompts] Failed to fetch prompt
|
|
5235
|
+
console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using stale cache:`, error);
|
|
5222
5236
|
return cached.prompt;
|
|
5223
5237
|
}
|
|
5224
5238
|
// 2. Return fallback (with warning)
|
|
5225
5239
|
if (fallback !== undefined) {
|
|
5226
|
-
console.warn(`[PostHog Prompts] Failed to fetch prompt
|
|
5240
|
+
console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using fallback:`, error);
|
|
5227
5241
|
return fallback;
|
|
5228
5242
|
}
|
|
5229
5243
|
// 3. Throw error
|
|
@@ -5251,16 +5265,26 @@ class Prompts {
|
|
|
5251
5265
|
/**
|
|
5252
5266
|
* Clear the cache for a specific prompt or all prompts
|
|
5253
5267
|
*
|
|
5254
|
-
* @param name - Optional prompt name to clear. If
|
|
5268
|
+
* @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt.
|
|
5255
5269
|
*/
|
|
5256
5270
|
clearCache(name) {
|
|
5257
5271
|
if (name !== undefined) {
|
|
5258
|
-
this.
|
|
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
|
+
}
|
|
5259
5283
|
} else {
|
|
5260
5284
|
this.cache.clear();
|
|
5261
5285
|
}
|
|
5262
5286
|
}
|
|
5263
|
-
async fetchPromptFromApi(name) {
|
|
5287
|
+
async fetchPromptFromApi(name, version) {
|
|
5264
5288
|
if (!this.personalApiKey) {
|
|
5265
5289
|
throw new Error('[PostHog Prompts] personalApiKey is required to fetch prompts. ' + 'Please provide it when initializing the Prompts instance.');
|
|
5266
5290
|
}
|
|
@@ -5269,7 +5293,9 @@ class Prompts {
|
|
|
5269
5293
|
}
|
|
5270
5294
|
const encodedPromptName = encodeURIComponent(name);
|
|
5271
5295
|
const encodedProjectApiKey = encodeURIComponent(this.projectApiKey);
|
|
5272
|
-
const
|
|
5296
|
+
const versionQuery = version === undefined ? '' : `&version=${encodeURIComponent(String(version))}`;
|
|
5297
|
+
const promptLabel = this.getPromptLabel(name, version);
|
|
5298
|
+
const url = `${this.host}/api/environments/@current/llm_prompts/name/${encodedPromptName}/?token=${encodedProjectApiKey}${versionQuery}`;
|
|
5273
5299
|
const response = await fetch(url, {
|
|
5274
5300
|
method: 'GET',
|
|
5275
5301
|
headers: {
|
|
@@ -5278,16 +5304,16 @@ class Prompts {
|
|
|
5278
5304
|
});
|
|
5279
5305
|
if (!response.ok) {
|
|
5280
5306
|
if (response.status === 404) {
|
|
5281
|
-
throw new Error(`[PostHog Prompts] Prompt
|
|
5307
|
+
throw new Error(`[PostHog Prompts] Prompt ${promptLabel} not found`);
|
|
5282
5308
|
}
|
|
5283
5309
|
if (response.status === 403) {
|
|
5284
|
-
throw new Error(`[PostHog Prompts] Access denied for prompt
|
|
5310
|
+
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.');
|
|
5285
5311
|
}
|
|
5286
|
-
throw new Error(`[PostHog Prompts] Failed to fetch prompt
|
|
5312
|
+
throw new Error(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}: HTTP ${response.status}`);
|
|
5287
5313
|
}
|
|
5288
5314
|
const data = await response.json();
|
|
5289
5315
|
if (!isPromptApiResponse(data)) {
|
|
5290
|
-
throw new Error(`[PostHog Prompts] Invalid response format for prompt
|
|
5316
|
+
throw new Error(`[PostHog Prompts] Invalid response format for prompt ${promptLabel}`);
|
|
5291
5317
|
}
|
|
5292
5318
|
return data.prompt;
|
|
5293
5319
|
}
|