@posthog/ai 8.10.2 → 8.10.4
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/LICENSE +64 -0
- package/dist/adk/index.cjs +2 -1
- package/dist/adk/index.cjs.map +1 -1
- package/dist/adk/index.mjs +2 -1
- package/dist/adk/index.mjs.map +1 -1
- package/dist/anthropic/index.cjs +3 -3
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.mjs +3 -3
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +3 -3
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +3 -3
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +39 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.mjs +39 -15
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +19 -5
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.d.ts +1 -0
- package/dist/langchain/index.mjs +19 -5
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/langchain/middleware/index.cjs +19 -5
- package/dist/langchain/middleware/index.cjs.map +1 -1
- package/dist/langchain/middleware/index.mjs +19 -5
- package/dist/langchain/middleware/index.mjs.map +1 -1
- package/dist/openai/index.cjs +5 -3
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +5 -3
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +1 -1
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.mjs +1 -1
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +3 -3
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +3 -3
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -182,6 +182,7 @@ declare class Prompts {
|
|
|
182
182
|
* Does NOT handle the string `fallback` option — callers handle that.
|
|
183
183
|
*/
|
|
184
184
|
private getInternal;
|
|
185
|
+
private readCacheEntry;
|
|
185
186
|
/**
|
|
186
187
|
* Compile a prompt template with variable substitution
|
|
187
188
|
*
|
|
@@ -222,6 +223,9 @@ interface CaptureAiGenerationOptions {
|
|
|
222
223
|
output: unknown;
|
|
223
224
|
/** Maps to `$ai_model_parameters` (temperature, max_tokens, top_p, …). */
|
|
224
225
|
modelParameters?: Record<string, unknown>;
|
|
226
|
+
/** The service tier the provider reported serving the request on. Response-derived only:
|
|
227
|
+
* this becomes $ai_service_tier, the explicit served-tier signal cost processing prices from. */
|
|
228
|
+
servedServiceTier?: string | null;
|
|
225
229
|
/**
|
|
226
230
|
* `null` explicitly signals no base URL and omits `$ai_base_url` from the event.
|
|
227
231
|
* `undefined`/omitted keeps the existing `''` default for backward compatibility.
|
package/dist/index.mjs
CHANGED
|
@@ -272,8 +272,7 @@ const getModelParams = (params, responseServiceTier) => {
|
|
|
272
272
|
"streaming",
|
|
273
273
|
"language",
|
|
274
274
|
"response_format",
|
|
275
|
-
"timestamp_granularities"
|
|
276
|
-
"service_tier"
|
|
275
|
+
"timestamp_granularities"
|
|
277
276
|
]) if (key in params && params[key] !== void 0) modelParams[key] = params[key];
|
|
278
277
|
if (responseServiceTier != null) modelParams.service_tier = responseServiceTier;
|
|
279
278
|
return modelParams;
|
|
@@ -392,7 +391,7 @@ function sanitizeValues(obj) {
|
|
|
392
391
|
}
|
|
393
392
|
//#endregion
|
|
394
393
|
//#region package.json
|
|
395
|
-
var version = "8.10.
|
|
394
|
+
var version = "8.10.4";
|
|
396
395
|
//#endregion
|
|
397
396
|
//#region src/serializeError.ts
|
|
398
397
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -524,6 +523,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
524
523
|
$ai_provider: options.providerOverride ?? options.provider,
|
|
525
524
|
$ai_model: options.modelOverride ?? options.model,
|
|
526
525
|
$ai_model_parameters: options.modelParameters ?? {},
|
|
526
|
+
...options.servedServiceTier != null ? { $ai_service_tier: options.servedServiceTier } : {},
|
|
527
527
|
$ai_input: safeInput,
|
|
528
528
|
$ai_output_choices: safeOutput,
|
|
529
529
|
$ai_http_status: httpStatus,
|
|
@@ -1083,6 +1083,8 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
1083
1083
|
//#region src/prompts.ts
|
|
1084
1084
|
const DEFAULT_CACHE_TTL_SECONDS = 300;
|
|
1085
1085
|
const DEFAULT_PROMPTS_HOST = "https://us.posthog.com";
|
|
1086
|
+
const DEFAULT_REFETCH_COOLDOWN_SECONDS = 60;
|
|
1087
|
+
const MAX_REFETCH_COOLDOWN_SECONDS = 3600;
|
|
1086
1088
|
function normalizeApiKey(value) {
|
|
1087
1089
|
return typeof value === "string" ? value.trim() : "";
|
|
1088
1090
|
}
|
|
@@ -1098,6 +1100,21 @@ function extractConfig(value) {
|
|
|
1098
1100
|
function cloneConfig(config) {
|
|
1099
1101
|
return config === null ? null : structuredClone(config);
|
|
1100
1102
|
}
|
|
1103
|
+
/** Reads a Retry-After header of the delta-seconds form the API sends. */
|
|
1104
|
+
function parseRetryAfterSeconds(value) {
|
|
1105
|
+
if (!value) return;
|
|
1106
|
+
const seconds = Number(value.trim());
|
|
1107
|
+
if (!Number.isFinite(seconds) || seconds <= 0) return;
|
|
1108
|
+
return Math.min(seconds, MAX_REFETCH_COOLDOWN_SECONDS);
|
|
1109
|
+
}
|
|
1110
|
+
/** Carries the server's own cooldown so a rate-limited client waits as long as it was told to. */
|
|
1111
|
+
var PromptFetchError = class extends Error {
|
|
1112
|
+
constructor(message, retryAfterSeconds) {
|
|
1113
|
+
super(message);
|
|
1114
|
+
this.name = "PromptFetchError";
|
|
1115
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
1116
|
+
}
|
|
1117
|
+
};
|
|
1101
1118
|
function isPromptApiResponse(data) {
|
|
1102
1119
|
if (typeof data !== "object" || data === null) return false;
|
|
1103
1120
|
const record = data;
|
|
@@ -1215,14 +1232,14 @@ var Prompts = class {
|
|
|
1215
1232
|
const cached = this.getPromptCache(name)?.get(cacheEntryKey);
|
|
1216
1233
|
const now = Date.now();
|
|
1217
1234
|
if (cached) {
|
|
1218
|
-
if (now - cached.fetchedAt < cacheTtlSeconds * 1e3) {
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
}
|
|
1235
|
+
if (now - cached.fetchedAt < cacheTtlSeconds * 1e3) return {
|
|
1236
|
+
source: "cache",
|
|
1237
|
+
...this.readCacheEntry(cached)
|
|
1238
|
+
};
|
|
1239
|
+
if (cached.retryNotBefore !== void 0 && now < cached.retryNotBefore) return {
|
|
1240
|
+
source: "stale_cache",
|
|
1241
|
+
...this.readCacheEntry(cached)
|
|
1242
|
+
};
|
|
1226
1243
|
}
|
|
1227
1244
|
try {
|
|
1228
1245
|
const fetched = await this.fetchPromptFromApi(name, version, label);
|
|
@@ -1238,17 +1255,24 @@ var Prompts = class {
|
|
|
1238
1255
|
};
|
|
1239
1256
|
} catch (error) {
|
|
1240
1257
|
if (cached) {
|
|
1241
|
-
const
|
|
1258
|
+
const cooldownSeconds = (error instanceof PromptFetchError ? error.retryAfterSeconds : void 0) ?? DEFAULT_REFETCH_COOLDOWN_SECONDS;
|
|
1259
|
+
cached.retryNotBefore = Math.max(cached.retryNotBefore ?? 0, Date.now() + cooldownSeconds * 1e3);
|
|
1242
1260
|
console.warn(`[PostHog Prompts] Failed to fetch prompt ${promptReference}, using stale cache:`, error);
|
|
1243
1261
|
return {
|
|
1244
1262
|
source: "stale_cache",
|
|
1245
|
-
...
|
|
1246
|
-
config: cloneConfig(cached.config)
|
|
1263
|
+
...this.readCacheEntry(cached)
|
|
1247
1264
|
};
|
|
1248
1265
|
}
|
|
1249
1266
|
throw error;
|
|
1250
1267
|
}
|
|
1251
1268
|
}
|
|
1269
|
+
readCacheEntry(cached) {
|
|
1270
|
+
const { fetchedAt: _fetchedAt, retryNotBefore: _retryNotBefore, ...cachedResult } = cached;
|
|
1271
|
+
return {
|
|
1272
|
+
...cachedResult,
|
|
1273
|
+
config: cloneConfig(cached.config)
|
|
1274
|
+
};
|
|
1275
|
+
}
|
|
1252
1276
|
/**
|
|
1253
1277
|
* Compile a prompt template with variable substitution
|
|
1254
1278
|
*
|
|
@@ -1301,7 +1325,7 @@ var Prompts = class {
|
|
|
1301
1325
|
if (!response.ok) {
|
|
1302
1326
|
if (response.status === 404) throw new Error(`[PostHog Prompts] Prompt ${promptReference} not found`);
|
|
1303
1327
|
if (response.status === 403) throw new Error(`[PostHog Prompts] Access denied for prompt ${promptReference}. Check that your personalApiKey has the correct permissions and the LLM prompts feature is enabled.`);
|
|
1304
|
-
throw new
|
|
1328
|
+
throw new PromptFetchError(`[PostHog Prompts] Failed to fetch prompt ${promptReference}: HTTP ${response.status}`, response.status === 429 ? parseRetryAfterSeconds(response.headers?.get("Retry-After")) : void 0);
|
|
1305
1329
|
}
|
|
1306
1330
|
const data = await response.json();
|
|
1307
1331
|
if (!isPromptApiResponse(data)) throw new Error(`[PostHog Prompts] Invalid response format for prompt ${promptReference}`);
|