@posthog/ai 8.12.1 → 8.13.1

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/index.d.ts CHANGED
@@ -154,6 +154,9 @@ type PromptsOptions = PromptsWithPostHogOptions | PromptsDirectOptions;
154
154
  * // Or fetch all prompts at a label in one request and warm the cache
155
155
  * const prodPrompts = await prompts.getAll({ label: 'production' })
156
156
  *
157
+ * // Or fetch the latest version of every prompt in one request
158
+ * const allPrompts = await prompts.getAll()
159
+ *
157
160
  * // Compile with variables
158
161
  * const systemPrompt = prompts.compile(result.prompt, {
159
162
  * company: 'Acme Corp',
@@ -181,26 +184,28 @@ declare class Prompts {
181
184
  */
182
185
  get(name: string, options?: GetPromptOptions): Promise<PromptResult>;
183
186
  /**
184
- * Fetch every prompt that carries a label, in one batch.
187
+ * Fetch every prompt in one batch.
185
188
  *
186
- * Returns an object mapping prompt name to `PromptRemoteResult`, with each
187
- * prompt at the version the label points to. Prompts without the label are
188
- * not included.
189
+ * Returns an object mapping prompt name to `PromptRemoteResult`. With a
190
+ * label, each prompt is at the version the label points to, and prompts
191
+ * without the label are not included. Without a label, every prompt is
192
+ * included at its latest version, matching what `get(name)` returns.
189
193
  *
190
194
  * Each fetched prompt is stored in the cache, so later
191
- * `get(name, { label })` calls are served from cache within the TTL. An app
192
- * with many prompts can call this once per cache cycle instead of making one
193
- * `get()` request per prompt.
195
+ * `get(name, { label })` (or plain `get(name)`) calls are served from cache
196
+ * within the TTL. An app with many prompts can call this once per cache
197
+ * cycle instead of making one `get()` request per prompt.
194
198
  *
195
199
  * Unlike `get()`, there is no `fallback` option, so callers should handle
196
200
  * the error themselves.
197
201
  *
198
202
  * @throws {Error} When the request fails, when the response is malformed,
199
- * or when the server does not support fetching prompts by label on the list
200
- * endpoint (PostHog releases from before September 2026).
203
+ * or when a label was passed and the server does not support fetching
204
+ * prompts by label on the list endpoint (PostHog releases from before
205
+ * September 2026).
201
206
  */
202
- getAll(options: {
203
- label: string;
207
+ getAll(options?: {
208
+ label?: string;
204
209
  }): Promise<Record<string, PromptRemoteResult>>;
205
210
  /**
206
211
  * Internal method that handles cache + fetch logic, returning full metadata.
@@ -228,7 +233,12 @@ declare class Prompts {
228
233
  clearCache(name?: string, version?: number): void;
229
234
  private requireCredentials;
230
235
  /**
231
- * Fetch all prompts at a label from the paginated list endpoint.
236
+ * Fetch all prompts from the paginated list endpoint.
237
+ *
238
+ * Without a label the endpoint serves the latest version of every prompt;
239
+ * the param is then omitted entirely, since the server treats any value as
240
+ * a label name to filter by.
241
+ *
232
242
  * Follows pagination links until the last page and returns the raw rows.
233
243
  */
234
244
  private fetchPromptListFromApi;
package/dist/index.mjs CHANGED
@@ -395,7 +395,7 @@ function sanitizeValues(obj) {
395
395
  }
396
396
  //#endregion
397
397
  //#region package.json
398
- var version = "8.12.1";
398
+ var version = "8.13.1";
399
399
  //#endregion
400
400
  //#region src/serializeError.ts
401
401
  const DEFAULT_MAX_DEPTH = 3;
@@ -1132,6 +1132,10 @@ function isSameOrigin(url, host) {
1132
1132
  return false;
1133
1133
  }
1134
1134
  }
1135
+ /** Formats a batch-fetch reference for logs and errors. */
1136
+ function promptListReference(label) {
1137
+ return label === void 0 ? "all prompts" : `prompts with label "${label}"`;
1138
+ }
1135
1139
  /**
1136
1140
  * Classify how a list row relates to the requested label, via its all_labels
1137
1141
  * field.
@@ -1187,6 +1191,9 @@ function isPromptsWithPostHog(options) {
1187
1191
  * // Or fetch all prompts at a label in one request and warm the cache
1188
1192
  * const prodPrompts = await prompts.getAll({ label: 'production' })
1189
1193
  *
1194
+ * // Or fetch the latest version of every prompt in one request
1195
+ * const allPrompts = await prompts.getAll()
1196
+ *
1190
1197
  * // Compile with variables
1191
1198
  * const systemPrompt = prompts.compile(result.prompt, {
1192
1199
  * company: 'Acme Corp',
@@ -1253,31 +1260,38 @@ var Prompts = class {
1253
1260
  }
1254
1261
  }
1255
1262
  /**
1256
- * Fetch every prompt that carries a label, in one batch.
1263
+ * Fetch every prompt in one batch.
1257
1264
  *
1258
- * Returns an object mapping prompt name to `PromptRemoteResult`, with each
1259
- * prompt at the version the label points to. Prompts without the label are
1260
- * not included.
1265
+ * Returns an object mapping prompt name to `PromptRemoteResult`. With a
1266
+ * label, each prompt is at the version the label points to, and prompts
1267
+ * without the label are not included. Without a label, every prompt is
1268
+ * included at its latest version, matching what `get(name)` returns.
1261
1269
  *
1262
1270
  * Each fetched prompt is stored in the cache, so later
1263
- * `get(name, { label })` calls are served from cache within the TTL. An app
1264
- * with many prompts can call this once per cache cycle instead of making one
1265
- * `get()` request per prompt.
1271
+ * `get(name, { label })` (or plain `get(name)`) calls are served from cache
1272
+ * within the TTL. An app with many prompts can call this once per cache
1273
+ * cycle instead of making one `get()` request per prompt.
1266
1274
  *
1267
1275
  * Unlike `get()`, there is no `fallback` option, so callers should handle
1268
1276
  * the error themselves.
1269
1277
  *
1270
1278
  * @throws {Error} When the request fails, when the response is malformed,
1271
- * or when the server does not support fetching prompts by label on the list
1272
- * endpoint (PostHog releases from before September 2026).
1279
+ * or when a label was passed and the server does not support fetching
1280
+ * prompts by label on the list endpoint (PostHog releases from before
1281
+ * September 2026).
1273
1282
  */
1274
1283
  async getAll(options) {
1275
- const label = options.label;
1284
+ const label = options?.label;
1276
1285
  const rows = await this.fetchPromptListFromApi(label);
1286
+ const reference = promptListReference(label);
1277
1287
  const resolvedRows = [];
1278
1288
  const skipped = [];
1279
1289
  for (const row of rows) {
1280
- if (!isPromptApiResponse(row)) throw new Error(`[PostHog Prompts] Invalid response format for prompts with label "${label}"`);
1290
+ if (!isPromptApiResponse(row)) throw new Error(`[PostHog Prompts] Invalid response format for ${reference}`);
1291
+ if (label === void 0) {
1292
+ resolvedRows.push(row);
1293
+ continue;
1294
+ }
1281
1295
  const labelState = rowLabelState(row, label);
1282
1296
  if (labelState === "absent") throw new Error(`[PostHog Prompts] The server returned a prompt that does not carry label "${label}". It may not support fetching prompts by label on the list endpoint yet. Upgrade PostHog, or fetch prompts one by one with get().`);
1283
1297
  if (labelState === "moved") {
@@ -1286,7 +1300,7 @@ var Prompts = class {
1286
1300
  }
1287
1301
  resolvedRows.push(row);
1288
1302
  }
1289
- if (rows.length > 0 && resolvedRows.length === 0) throw new Error(`[PostHog Prompts] The server returned prompts, but none resolve label "${label}". It may not support fetching prompts by label on the list endpoint yet. Upgrade PostHog, or fetch prompts one by one with get().`);
1303
+ if (label !== void 0 && rows.length > 0 && resolvedRows.length === 0) throw new Error(`[PostHog Prompts] The server returned prompts, but none resolve label "${label}". It may not support fetching prompts by label on the list endpoint yet. Upgrade PostHog, or fetch prompts one by one with get().`);
1290
1304
  const now = Date.now();
1291
1305
  const results = /* @__PURE__ */ new Map();
1292
1306
  for (const row of resolvedRows) {
@@ -1406,14 +1420,20 @@ var Prompts = class {
1406
1420
  if (!this.projectApiKey) throw new Error("[PostHog Prompts] projectApiKey is required to fetch prompts. Please provide it when initializing the Prompts instance.");
1407
1421
  }
1408
1422
  /**
1409
- * Fetch all prompts at a label from the paginated list endpoint.
1423
+ * Fetch all prompts from the paginated list endpoint.
1424
+ *
1425
+ * Without a label the endpoint serves the latest version of every prompt;
1426
+ * the param is then omitted entirely, since the server treats any value as
1427
+ * a label name to filter by.
1428
+ *
1410
1429
  * Follows pagination links until the last page and returns the raw rows.
1411
1430
  */
1412
1431
  async fetchPromptListFromApi(label) {
1413
1432
  this.requireCredentials();
1414
- const query = `token=${encodeURIComponent(this.projectApiKey)}&label=${encodeURIComponent(label)}&content=full`;
1433
+ const labelQuery = label === void 0 ? "" : `&label=${encodeURIComponent(label)}`;
1434
+ const query = `token=${encodeURIComponent(this.projectApiKey)}${labelQuery}&content=full`;
1415
1435
  let url = `${this.host}/api/environments/@current/llm_prompts/?${query}`;
1416
- const reference = `prompts with label "${label}"`;
1436
+ const reference = promptListReference(label);
1417
1437
  const rows = [];
1418
1438
  let pages = 0;
1419
1439
  while (url !== void 0) {