@posthog/ai 7.6.1 → 7.7.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 +154 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +88 -1
- package/dist/index.mjs +154 -2
- 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/vercel/index.cjs +1 -1
- package/dist/vercel/index.mjs +1 -1
- package/package.json +3 -3
package/dist/anthropic/index.cjs
CHANGED
|
@@ -11,7 +11,7 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
|
11
11
|
|
|
12
12
|
var AnthropicOriginal__default = /*#__PURE__*/_interopDefault(AnthropicOriginal);
|
|
13
13
|
|
|
14
|
-
var version = "7.
|
|
14
|
+
var version = "7.7.0";
|
|
15
15
|
|
|
16
16
|
// Type guards for safer type checking
|
|
17
17
|
|
package/dist/anthropic/index.mjs
CHANGED
package/dist/gemini/index.cjs
CHANGED
package/dist/gemini/index.mjs
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -30,7 +30,7 @@ function _interopNamespace(e) {
|
|
|
30
30
|
var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
|
|
31
31
|
var AnthropicOriginal__default = /*#__PURE__*/_interopDefault(AnthropicOriginal);
|
|
32
32
|
|
|
33
|
-
var version = "7.
|
|
33
|
+
var version = "7.7.0";
|
|
34
34
|
|
|
35
35
|
// Type guards for safer type checking
|
|
36
36
|
const isString = value => {
|
|
@@ -4286,10 +4286,163 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
4286
4286
|
}
|
|
4287
4287
|
}
|
|
4288
4288
|
|
|
4289
|
+
/// <reference lib="dom" />
|
|
4290
|
+
const DEFAULT_CACHE_TTL_SECONDS = 300; // 5 minutes
|
|
4291
|
+
function isPromptApiResponse(data) {
|
|
4292
|
+
return typeof data === 'object' && data !== null && 'prompt' in data && typeof data.prompt === 'string';
|
|
4293
|
+
}
|
|
4294
|
+
function isPromptsWithPostHog(options) {
|
|
4295
|
+
return 'posthog' in options;
|
|
4296
|
+
}
|
|
4297
|
+
/**
|
|
4298
|
+
* Prompts class for fetching and compiling LLM prompts from PostHog
|
|
4299
|
+
*
|
|
4300
|
+
* @example
|
|
4301
|
+
* ```ts
|
|
4302
|
+
* // With PostHog client
|
|
4303
|
+
* const prompts = new Prompts({ posthog })
|
|
4304
|
+
*
|
|
4305
|
+
* // Or with direct options (no PostHog client needed)
|
|
4306
|
+
* const prompts = new Prompts({
|
|
4307
|
+
* personalApiKey: 'phx_xxx',
|
|
4308
|
+
* host: 'https://us.i.posthog.com',
|
|
4309
|
+
* })
|
|
4310
|
+
*
|
|
4311
|
+
* // Fetch with caching and fallback
|
|
4312
|
+
* const template = await prompts.get('support-system-prompt', {
|
|
4313
|
+
* cacheTtlSeconds: 300,
|
|
4314
|
+
* fallback: 'You are a helpful assistant.',
|
|
4315
|
+
* })
|
|
4316
|
+
*
|
|
4317
|
+
* // Compile with variables
|
|
4318
|
+
* const systemPrompt = prompts.compile(template, {
|
|
4319
|
+
* company: 'Acme Corp',
|
|
4320
|
+
* tier: 'premium',
|
|
4321
|
+
* })
|
|
4322
|
+
* ```
|
|
4323
|
+
*/
|
|
4324
|
+
class Prompts {
|
|
4325
|
+
constructor(options) {
|
|
4326
|
+
this.cache = new Map();
|
|
4327
|
+
this.defaultCacheTtlSeconds = options.defaultCacheTtlSeconds ?? DEFAULT_CACHE_TTL_SECONDS;
|
|
4328
|
+
if (isPromptsWithPostHog(options)) {
|
|
4329
|
+
this.personalApiKey = options.posthog.options.personalApiKey ?? '';
|
|
4330
|
+
this.host = options.posthog.host;
|
|
4331
|
+
} else {
|
|
4332
|
+
// Direct options
|
|
4333
|
+
this.personalApiKey = options.personalApiKey;
|
|
4334
|
+
this.host = options.host ?? 'https://us.i.posthog.com';
|
|
4335
|
+
}
|
|
4336
|
+
}
|
|
4337
|
+
/**
|
|
4338
|
+
* Fetch a prompt by name from the PostHog API
|
|
4339
|
+
*
|
|
4340
|
+
* @param name - The name of the prompt to fetch
|
|
4341
|
+
* @param options - Optional settings for caching and fallback
|
|
4342
|
+
* @returns The prompt string
|
|
4343
|
+
* @throws Error if the prompt cannot be fetched and no fallback is provided
|
|
4344
|
+
*/
|
|
4345
|
+
async get(name, options) {
|
|
4346
|
+
const cacheTtlSeconds = options?.cacheTtlSeconds ?? this.defaultCacheTtlSeconds;
|
|
4347
|
+
const fallback = options?.fallback;
|
|
4348
|
+
// Check cache first
|
|
4349
|
+
const cached = this.cache.get(name);
|
|
4350
|
+
const now = Date.now();
|
|
4351
|
+
if (cached) {
|
|
4352
|
+
const isFresh = now - cached.fetchedAt < cacheTtlSeconds * 1000;
|
|
4353
|
+
if (isFresh) {
|
|
4354
|
+
return cached.prompt;
|
|
4355
|
+
}
|
|
4356
|
+
}
|
|
4357
|
+
// Try to fetch from API
|
|
4358
|
+
try {
|
|
4359
|
+
const prompt = await this.fetchPromptFromApi(name);
|
|
4360
|
+
const fetchedAt = Date.now();
|
|
4361
|
+
// Update cache
|
|
4362
|
+
this.cache.set(name, {
|
|
4363
|
+
prompt,
|
|
4364
|
+
fetchedAt
|
|
4365
|
+
});
|
|
4366
|
+
return prompt;
|
|
4367
|
+
} catch (error) {
|
|
4368
|
+
// Fallback order:
|
|
4369
|
+
// 1. Return stale cache (with warning)
|
|
4370
|
+
if (cached) {
|
|
4371
|
+
console.warn(`[PostHog Prompts] Failed to fetch prompt "${name}", using stale cache:`, error);
|
|
4372
|
+
return cached.prompt;
|
|
4373
|
+
}
|
|
4374
|
+
// 2. Return fallback (with warning)
|
|
4375
|
+
if (fallback !== undefined) {
|
|
4376
|
+
console.warn(`[PostHog Prompts] Failed to fetch prompt "${name}", using fallback:`, error);
|
|
4377
|
+
return fallback;
|
|
4378
|
+
}
|
|
4379
|
+
// 3. Throw error
|
|
4380
|
+
throw error;
|
|
4381
|
+
}
|
|
4382
|
+
}
|
|
4383
|
+
/**
|
|
4384
|
+
* Compile a prompt template with variable substitution
|
|
4385
|
+
*
|
|
4386
|
+
* Variables in the format `{{variableName}}` will be replaced with values from the variables object.
|
|
4387
|
+
* Unmatched variables are left unchanged.
|
|
4388
|
+
*
|
|
4389
|
+
* @param prompt - The prompt template string
|
|
4390
|
+
* @param variables - Object containing variable values
|
|
4391
|
+
* @returns The compiled prompt string
|
|
4392
|
+
*/
|
|
4393
|
+
compile(prompt, variables) {
|
|
4394
|
+
return prompt.replace(/\{\{([\w.-]+)\}\}/g, (match, variableName) => {
|
|
4395
|
+
if (variableName in variables) {
|
|
4396
|
+
return String(variables[variableName]);
|
|
4397
|
+
}
|
|
4398
|
+
return match;
|
|
4399
|
+
});
|
|
4400
|
+
}
|
|
4401
|
+
/**
|
|
4402
|
+
* Clear the cache for a specific prompt or all prompts
|
|
4403
|
+
*
|
|
4404
|
+
* @param name - Optional prompt name to clear. If not provided, clears all cached prompts.
|
|
4405
|
+
*/
|
|
4406
|
+
clearCache(name) {
|
|
4407
|
+
if (name !== undefined) {
|
|
4408
|
+
this.cache.delete(name);
|
|
4409
|
+
} else {
|
|
4410
|
+
this.cache.clear();
|
|
4411
|
+
}
|
|
4412
|
+
}
|
|
4413
|
+
async fetchPromptFromApi(name) {
|
|
4414
|
+
if (!this.personalApiKey) {
|
|
4415
|
+
throw new Error('[PostHog Prompts] personalApiKey is required to fetch prompts. ' + 'Please provide it when initializing the Prompts instance.');
|
|
4416
|
+
}
|
|
4417
|
+
const url = `${this.host}/api/projects/@current/llm_prompts/name/${encodeURIComponent(name)}/`;
|
|
4418
|
+
const response = await fetch(url, {
|
|
4419
|
+
method: 'GET',
|
|
4420
|
+
headers: {
|
|
4421
|
+
Authorization: `Bearer ${this.personalApiKey}`
|
|
4422
|
+
}
|
|
4423
|
+
});
|
|
4424
|
+
if (!response.ok) {
|
|
4425
|
+
if (response.status === 404) {
|
|
4426
|
+
throw new Error(`[PostHog Prompts] Prompt "${name}" not found`);
|
|
4427
|
+
}
|
|
4428
|
+
if (response.status === 403) {
|
|
4429
|
+
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.');
|
|
4430
|
+
}
|
|
4431
|
+
throw new Error(`[PostHog Prompts] Failed to fetch prompt "${name}": HTTP ${response.status}`);
|
|
4432
|
+
}
|
|
4433
|
+
const data = await response.json();
|
|
4434
|
+
if (!isPromptApiResponse(data)) {
|
|
4435
|
+
throw new Error(`[PostHog Prompts] Invalid response format for prompt "${name}"`);
|
|
4436
|
+
}
|
|
4437
|
+
return data.prompt;
|
|
4438
|
+
}
|
|
4439
|
+
}
|
|
4440
|
+
|
|
4289
4441
|
exports.Anthropic = PostHogAnthropic;
|
|
4290
4442
|
exports.AzureOpenAI = PostHogAzureOpenAI;
|
|
4291
4443
|
exports.GoogleGenAI = PostHogGoogleGenAI;
|
|
4292
4444
|
exports.LangChainCallbackHandler = LangChainCallbackHandler;
|
|
4293
4445
|
exports.OpenAI = PostHogOpenAI;
|
|
4446
|
+
exports.Prompts = Prompts;
|
|
4294
4447
|
exports.withTracing = wrapVercelLanguageModel;
|
|
4295
4448
|
//# sourceMappingURL=index.cjs.map
|