@posthog/ai 7.14.0 → 7.16.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/README.md +30 -7
- 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 +69 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +50 -6
- package/dist/index.mjs +69 -26
- 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/openai-agents/index.cjs +655 -0
- package/dist/openai-agents/index.cjs.map +1 -0
- package/dist/openai-agents/index.d.ts +101 -0
- package/dist/openai-agents/index.mjs +652 -0
- package/dist/openai-agents/index.mjs.map +1 -0
- package/dist/otel/index.cjs +103 -5
- package/dist/otel/index.cjs.map +1 -1
- package/dist/otel/index.d.ts +66 -5
- package/dist/otel/index.mjs +103 -6
- package/dist/otel/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +1 -1
- package/dist/vercel/index.mjs +1 -1
- package/package.json +21 -2
package/dist/index.d.ts
CHANGED
|
@@ -22,7 +22,42 @@ interface GetPromptOptions {
|
|
|
22
22
|
cacheTtlSeconds?: number;
|
|
23
23
|
fallback?: string;
|
|
24
24
|
version?: number;
|
|
25
|
+
/**
|
|
26
|
+
* When true, returns a `PromptResult` object with metadata (source, name, version)
|
|
27
|
+
* instead of a plain string.
|
|
28
|
+
*
|
|
29
|
+
* Omitting this option or setting it to false is deprecated and will be removed
|
|
30
|
+
* in a future major version.
|
|
31
|
+
*/
|
|
32
|
+
withMetadata?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Result from the Prompts API or local cache — carries real metadata.
|
|
36
|
+
*/
|
|
37
|
+
interface PromptRemoteResult {
|
|
38
|
+
source: 'api' | 'cache' | 'stale_cache';
|
|
39
|
+
prompt: string;
|
|
40
|
+
name: string;
|
|
41
|
+
version: number;
|
|
25
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Result when the fetch failed and no cache was available — fell back to the
|
|
45
|
+
* hardcoded fallback string. name and version are undefined so they remain
|
|
46
|
+
* accessible on the PromptResult union without a type guard.
|
|
47
|
+
*/
|
|
48
|
+
interface PromptCodeFallbackResult {
|
|
49
|
+
source: 'code_fallback';
|
|
50
|
+
prompt: string;
|
|
51
|
+
name: undefined;
|
|
52
|
+
version: undefined;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Discriminated union returned by `Prompts.get()` when `withMetadata: true`.
|
|
56
|
+
*
|
|
57
|
+
* Narrow on `source` to guarantee metadata, or access `result.name` /
|
|
58
|
+
* `result.version` directly as `string | undefined` / `number | undefined`.
|
|
59
|
+
*/
|
|
60
|
+
type PromptResult = PromptRemoteResult | PromptCodeFallbackResult;
|
|
26
61
|
/**
|
|
27
62
|
* Variables for prompt compilation
|
|
28
63
|
*/
|
|
@@ -340,19 +375,28 @@ declare class Prompts {
|
|
|
340
375
|
private host;
|
|
341
376
|
private defaultCacheTtlSeconds;
|
|
342
377
|
private cache;
|
|
378
|
+
private hasWarnedDeprecation;
|
|
343
379
|
constructor(options: PromptsOptions);
|
|
344
380
|
private getPromptCache;
|
|
345
381
|
private getOrCreatePromptCache;
|
|
346
382
|
private getPromptLabel;
|
|
347
383
|
/**
|
|
348
|
-
* Fetch a prompt by name from the PostHog API
|
|
384
|
+
* Fetch a prompt by name from the PostHog API.
|
|
349
385
|
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
* @throws Error if the prompt cannot be fetched and no fallback is provided
|
|
386
|
+
* When `withMetadata` is `true`, returns a `PromptResult` object with `source`,
|
|
387
|
+
* `name`, and `version` metadata. When omitted or `false`, returns a plain string
|
|
388
|
+
* (deprecated — will be removed in a future major version).
|
|
354
389
|
*/
|
|
390
|
+
get(name: string, options: GetPromptOptions & {
|
|
391
|
+
withMetadata: true;
|
|
392
|
+
}): Promise<PromptResult>;
|
|
393
|
+
/** @deprecated Omitting `withMetadata` is deprecated. Pass `{ withMetadata: true }` to receive a `PromptResult`. */
|
|
355
394
|
get(name: string, options?: GetPromptOptions): Promise<string>;
|
|
395
|
+
/**
|
|
396
|
+
* Internal method that handles cache + fetch logic, returning full metadata.
|
|
397
|
+
* Does NOT handle the string `fallback` option — callers handle that.
|
|
398
|
+
*/
|
|
399
|
+
private getInternal;
|
|
356
400
|
/**
|
|
357
401
|
* Compile a prompt template with variable substitution
|
|
358
402
|
*
|
|
@@ -374,4 +418,4 @@ declare class Prompts {
|
|
|
374
418
|
private fetchPromptFromApi;
|
|
375
419
|
}
|
|
376
420
|
|
|
377
|
-
export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, PostHogGoogleGenAI as GoogleGenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI, Prompts, wrapVercelLanguageModel as withTracing };
|
|
421
|
+
export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, PostHogGoogleGenAI as GoogleGenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI, type PromptCodeFallbackResult, type PromptRemoteResult, type PromptResult, Prompts, wrapVercelLanguageModel as withTracing };
|
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.16.0";
|
|
9
9
|
|
|
10
10
|
// Type guards for safer type checking
|
|
11
11
|
const isString = value => {
|
|
@@ -4437,7 +4437,11 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
4437
4437
|
/// <reference lib="dom" />
|
|
4438
4438
|
const DEFAULT_CACHE_TTL_SECONDS = 300; // 5 minutes
|
|
4439
4439
|
function isPromptApiResponse(data) {
|
|
4440
|
-
|
|
4440
|
+
if (typeof data !== 'object' || data === null) {
|
|
4441
|
+
return false;
|
|
4442
|
+
}
|
|
4443
|
+
const record = data;
|
|
4444
|
+
return typeof record.prompt === 'string' && typeof record.name === 'string' && typeof record.version === 'number';
|
|
4441
4445
|
}
|
|
4442
4446
|
function isPromptsWithPostHog(options) {
|
|
4443
4447
|
return 'posthog' in options;
|
|
@@ -4478,6 +4482,7 @@ function isPromptsWithPostHog(options) {
|
|
|
4478
4482
|
class Prompts {
|
|
4479
4483
|
constructor(options) {
|
|
4480
4484
|
this.cache = new Map();
|
|
4485
|
+
this.hasWarnedDeprecation = false;
|
|
4481
4486
|
this.defaultCacheTtlSeconds = options.defaultCacheTtlSeconds ?? DEFAULT_CACHE_TTL_SECONDS;
|
|
4482
4487
|
if (isPromptsWithPostHog(options)) {
|
|
4483
4488
|
this.personalApiKey = options.posthog.options.personalApiKey ?? '';
|
|
@@ -4505,17 +4510,42 @@ class Prompts {
|
|
|
4505
4510
|
getPromptLabel(name, version) {
|
|
4506
4511
|
return version === undefined ? `"${name}"` : `"${name}" version ${version}`;
|
|
4507
4512
|
}
|
|
4513
|
+
async get(name, options) {
|
|
4514
|
+
const withMetadata = options?.withMetadata;
|
|
4515
|
+
if (withMetadata === undefined && !this.hasWarnedDeprecation) {
|
|
4516
|
+
this.hasWarnedDeprecation = true;
|
|
4517
|
+
console.warn('[PostHog Prompts] Calling get() without { withMetadata: true } is deprecated and will be ' + 'removed in a future major version. Pass { withMetadata: true } to receive a PromptResult ' + 'object with source, name, and version metadata. ' + 'You can pass { withMetadata: false } to silence this warning, but the plain-string return ' + 'will still be removed in the next major version.');
|
|
4518
|
+
}
|
|
4519
|
+
try {
|
|
4520
|
+
const result = await this.getInternal(name, options);
|
|
4521
|
+
if (withMetadata) {
|
|
4522
|
+
return result;
|
|
4523
|
+
}
|
|
4524
|
+
return result.prompt;
|
|
4525
|
+
} catch (error) {
|
|
4526
|
+
const fallback = options?.fallback;
|
|
4527
|
+
if (fallback !== undefined) {
|
|
4528
|
+
const promptLabel = this.getPromptLabel(name, options?.version);
|
|
4529
|
+
console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using fallback:`, error);
|
|
4530
|
+
if (withMetadata) {
|
|
4531
|
+
return {
|
|
4532
|
+
source: 'code_fallback',
|
|
4533
|
+
prompt: fallback,
|
|
4534
|
+
name: undefined,
|
|
4535
|
+
version: undefined
|
|
4536
|
+
};
|
|
4537
|
+
}
|
|
4538
|
+
return fallback;
|
|
4539
|
+
}
|
|
4540
|
+
throw error;
|
|
4541
|
+
}
|
|
4542
|
+
}
|
|
4508
4543
|
/**
|
|
4509
|
-
*
|
|
4510
|
-
*
|
|
4511
|
-
* @param name - The name of the prompt to fetch
|
|
4512
|
-
* @param options - Optional settings for caching, fallback, and exact version selection
|
|
4513
|
-
* @returns The prompt string
|
|
4514
|
-
* @throws Error if the prompt cannot be fetched and no fallback is provided
|
|
4544
|
+
* Internal method that handles cache + fetch logic, returning full metadata.
|
|
4545
|
+
* Does NOT handle the string `fallback` option — callers handle that.
|
|
4515
4546
|
*/
|
|
4516
|
-
async
|
|
4547
|
+
async getInternal(name, options) {
|
|
4517
4548
|
const cacheTtlSeconds = options?.cacheTtlSeconds ?? this.defaultCacheTtlSeconds;
|
|
4518
|
-
const fallback = options?.fallback;
|
|
4519
4549
|
const version = options?.version;
|
|
4520
4550
|
const promptLabel = this.getPromptLabel(name, version);
|
|
4521
4551
|
// Check cache first
|
|
@@ -4524,32 +4554,41 @@ class Prompts {
|
|
|
4524
4554
|
if (cached) {
|
|
4525
4555
|
const isFresh = now - cached.fetchedAt < cacheTtlSeconds * 1000;
|
|
4526
4556
|
if (isFresh) {
|
|
4527
|
-
|
|
4557
|
+
const {
|
|
4558
|
+
fetchedAt: _,
|
|
4559
|
+
...cachedResult
|
|
4560
|
+
} = cached;
|
|
4561
|
+
return {
|
|
4562
|
+
source: 'cache',
|
|
4563
|
+
...cachedResult
|
|
4564
|
+
};
|
|
4528
4565
|
}
|
|
4529
4566
|
}
|
|
4530
4567
|
// Try to fetch from API
|
|
4531
4568
|
try {
|
|
4532
|
-
const
|
|
4533
|
-
const fetchedAt = Date.now();
|
|
4569
|
+
const fetched = await this.fetchPromptFromApi(name, version);
|
|
4534
4570
|
// Update cache
|
|
4535
4571
|
this.getOrCreatePromptCache(name).set(version, {
|
|
4536
|
-
|
|
4537
|
-
fetchedAt
|
|
4572
|
+
...fetched,
|
|
4573
|
+
fetchedAt: Date.now()
|
|
4538
4574
|
});
|
|
4539
|
-
return
|
|
4575
|
+
return {
|
|
4576
|
+
source: 'api',
|
|
4577
|
+
...fetched
|
|
4578
|
+
};
|
|
4540
4579
|
} catch (error) {
|
|
4541
|
-
//
|
|
4542
|
-
// 1. Return stale cache (with warning)
|
|
4580
|
+
// Return stale cache (with warning)
|
|
4543
4581
|
if (cached) {
|
|
4582
|
+
const {
|
|
4583
|
+
fetchedAt: _,
|
|
4584
|
+
...cachedResult
|
|
4585
|
+
} = cached;
|
|
4544
4586
|
console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using stale cache:`, error);
|
|
4545
|
-
return
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4549
|
-
console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptLabel}, using fallback:`, error);
|
|
4550
|
-
return fallback;
|
|
4587
|
+
return {
|
|
4588
|
+
source: 'stale_cache',
|
|
4589
|
+
...cachedResult
|
|
4590
|
+
};
|
|
4551
4591
|
}
|
|
4552
|
-
// 3. Throw error
|
|
4553
4592
|
throw error;
|
|
4554
4593
|
}
|
|
4555
4594
|
}
|
|
@@ -4626,7 +4665,11 @@ class Prompts {
|
|
|
4626
4665
|
if (!isPromptApiResponse(data)) {
|
|
4627
4666
|
throw new Error(`[PostHog Prompts] Invalid response format for prompt ${promptLabel}`);
|
|
4628
4667
|
}
|
|
4629
|
-
return
|
|
4668
|
+
return {
|
|
4669
|
+
prompt: data.prompt,
|
|
4670
|
+
name: data.name,
|
|
4671
|
+
version: data.version
|
|
4672
|
+
};
|
|
4630
4673
|
}
|
|
4631
4674
|
}
|
|
4632
4675
|
|