@posthog/ai 8.22.5 → 8.23.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/README.md CHANGED
@@ -258,7 +258,7 @@ polarity; categorical inputs are lists of keys, usable as `"safe" in categories`
258
258
  arithmetic, comparisons, membership, Boolean operators, conditionals, `number`, `min`, `max`, and
259
259
  `abs`. They accept 1–16 inputs and up to 1,024 characters. Aliases must be unique lowercase
260
260
  identifiers (`[a-z][a-z0-9_]{0,31}`), cannot be formula keywords/functions, and must occur in the
261
- expression. Nested composites and numeric composite `step` values are unsupported.
261
+ expression. Item formulas require reported item scorers. Numeric composite `step` values are unsupported.
262
262
 
263
263
  Calculation mode and output kind are fixed after creation. Pass the complete `composition` to
264
264
  `createVersion`, alongside `config` and optionally `baseVersion`. Source updates never change
@@ -284,6 +284,15 @@ is open creates it again and counts as new usage for a reported result. Older re
284
284
  without outcomes expose `status: null` and `errorCode: null`. Save and restore calculation
285
285
  declarations with the same serialization and recovery methods as reported results.
286
286
 
287
+ ### Experiment scores
288
+
289
+ Use a composite scorer with `scope: 'experiment'` to calculate one score for a completed
290
+ run. Each input references an item scorer version and specifies a statistic such as
291
+ `pass_rate` or `mean`. To set a default for future runs, create or publish the scorer
292
+ with `applyToSuiteKeys: ['agent-ci']` and create runs with `suiteKey: 'agent-ci'`.
293
+ Pass `experimentScorerVersionId` to select a specific version for a run. PostHog pins
294
+ the version on first creation and computes the score from accepted item results.
295
+
287
296
  | Operation | Required scopes |
288
297
  | -------------------------------------- | -------------------------------------------- |
289
298
  | Dataset reads/writes | `dataset:read` / `dataset:write` |
@@ -3,7 +3,7 @@ let _google_adk = require("@google/adk");
3
3
  let uuid = require("uuid");
4
4
  let _posthog_core = require("@posthog/core");
5
5
  //#region package.json
6
- var version = "8.22.5";
6
+ var version = "8.23.1";
7
7
  //#endregion
8
8
  //#region src/typeGuards.ts
9
9
  const isObject = (value) => {
@@ -2,7 +2,7 @@ import { BasePlugin } from "@google/adk";
2
2
  import { v4 } from "uuid";
3
3
  import { acceptsEventOptions, eventOptionsAsLegacyProperties, toJsonSafeValue, uuidv7 } from "@posthog/core";
4
4
  //#region package.json
5
- var version = "8.22.5";
5
+ var version = "8.23.1";
6
6
  //#endregion
7
7
  //#region src/typeGuards.ts
8
8
  const isObject = (value) => {
@@ -601,7 +601,7 @@ function addDefaults(params) {
601
601
  }
602
602
  //#endregion
603
603
  //#region package.json
604
- var version = "8.22.5";
604
+ var version = "8.23.1";
605
605
  //#endregion
606
606
  //#region src/serializeError.ts
607
607
  const DEFAULT_MAX_DEPTH = 3;
@@ -574,7 +574,7 @@ function addDefaults(params) {
574
574
  }
575
575
  //#endregion
576
576
  //#region package.json
577
- var version = "8.22.5";
577
+ var version = "8.23.1";
578
578
  //#endregion
579
579
  //#region src/serializeError.ts
580
580
  const DEFAULT_MAX_DEPTH = 3;
@@ -3,7 +3,7 @@ let _anthropic_ai_claude_agent_sdk = require("@anthropic-ai/claude-agent-sdk");
3
3
  let uuid = require("uuid");
4
4
  let _posthog_core = require("@posthog/core");
5
5
  //#region package.json
6
- var version = "8.22.5";
6
+ var version = "8.23.1";
7
7
  //#endregion
8
8
  //#region src/captureAiEvent.ts
9
9
  /** @internal */
@@ -2,7 +2,7 @@ import { SYSTEM_PROMPT_DYNAMIC_BOUNDARY, query as query$1 } from "@anthropic-ai/
2
2
  import { v4 } from "uuid";
3
3
  import { acceptsEventOptions, eventOptionsAsLegacyProperties, toJsonSafeValue, uuidv7 } from "@posthog/core";
4
4
  //#region package.json
5
- var version = "8.22.5";
5
+ var version = "8.23.1";
6
6
  //#endregion
7
7
  //#region src/captureAiEvent.ts
8
8
  /** @internal */
@@ -921,44 +921,86 @@ function alias(value) {
921
921
  ].includes(name)) throw new TypeError("Invalid composite input alias.");
922
922
  return name;
923
923
  }
924
- function composition(value, fromWire = false) {
924
+ function scope(value) {
925
+ if (value !== "item" && value !== "experiment") throw new TypeError("Unsupported scorer scope.");
926
+ return value;
927
+ }
928
+ function suiteKeys(value) {
929
+ if (!Array.isArray(value)) throw new TypeError("Expected a list of suite keys.");
930
+ return Array.from(value, text);
931
+ }
932
+ function compositionInput(entry, fromWire, selectedScope) {
933
+ const input = object(entry);
934
+ const base = {
935
+ alias: alias(input.alias),
936
+ scorerVersionId: uuid(input[fromWire ? "scorer_version_id" : "scorerVersionId"])
937
+ };
938
+ const policyKey = fromWire ? "version_policy" : "versionPolicy";
939
+ const hasStatistics = [
940
+ "statistic",
941
+ policyKey,
942
+ "category"
943
+ ].some((key) => input[key] !== void 0);
944
+ if (selectedScope !== "experiment" && !hasStatistics) return base;
945
+ if (selectedScope === "item") throw new TypeError("Statistics apply to experiment scorers.");
946
+ const statistic = input.statistic;
947
+ if (statistic !== "pass_rate" && statistic !== "mean" && statistic !== "true_rate" && statistic !== "category_rate") throw new TypeError("Unsupported experiment score statistic.");
948
+ const parsed = {
949
+ ...base,
950
+ statistic
951
+ };
952
+ const policy = input[policyKey];
953
+ if (policy !== void 0) {
954
+ if (policy !== "run" && policy !== "exact" && policy !== "combine") throw new TypeError("Unsupported experiment score version policy.");
955
+ parsed.versionPolicy = policy;
956
+ }
957
+ if (statistic === "category_rate") parsed.category = identifier(input.category, 128);
958
+ else if (input.category !== void 0) throw new TypeError("Only category_rate accepts a category key.");
959
+ return parsed;
960
+ }
961
+ function composition(value, fromWire = false, selectedScope) {
925
962
  const data = object(value);
926
963
  const expression = text(data.expression);
927
964
  if (!expression.trim() || Array.from(expression).length > 1024) throw new TypeError("A formula must contain 1–1024 characters.");
928
- if (!Array.isArray(data.inputs) || data.inputs.length < 1 || data.inputs.length > 16) throw new TypeError("A composite requires 1–16 reported inputs.");
929
- const inputs = Array.from(data.inputs, (entry) => {
930
- const input = object(entry);
931
- return {
932
- alias: alias(input.alias),
933
- scorerVersionId: uuid(input[fromWire ? "scorer_version_id" : "scorerVersionId"])
934
- };
935
- });
965
+ if (!Array.isArray(data.inputs) || data.inputs.length < 1 || data.inputs.length > 16) throw new TypeError("A composite requires 1–16 inputs.");
966
+ const inputs = Array.from(data.inputs, (entry) => compositionInput(entry, fromWire, selectedScope));
936
967
  if (new Set(inputs.map((input) => input.alias)).size !== inputs.length) throw new TypeError("Composite input aliases must be unique.");
937
968
  return {
938
969
  expression,
939
970
  inputs
940
971
  };
941
972
  }
942
- function compositionBody(value) {
943
- const parsed = composition(value);
973
+ function compositionBody(value, selectedScope) {
974
+ const parsed = composition(value, false, selectedScope);
944
975
  return {
945
976
  expression: parsed.expression,
946
977
  inputs: parsed.inputs.map((input) => ({
947
978
  alias: input.alias,
948
- scorer_version_id: input.scorerVersionId
979
+ scorer_version_id: input.scorerVersionId,
980
+ ..."statistic" in input ? defined({
981
+ statistic: input.statistic,
982
+ version_policy: input.versionPolicy,
983
+ category: input.category
984
+ }) : {}
949
985
  }))
950
986
  };
951
987
  }
952
988
  function scoring(data) {
953
989
  const selected = calculation(data.calculation === void 0 ? "reported" : data.calculation);
990
+ const selectedScope = scope(data.scope === void 0 ? "item" : data.scope);
991
+ const suites = suiteKeys(data.suite_keys === void 0 ? [] : data.suite_keys);
954
992
  if (selected === "composite") return {
955
993
  calculation: selected,
956
- composition: composition(data.composition, true)
994
+ composition: composition(data.composition, true, selectedScope),
995
+ scope: selectedScope,
996
+ suiteKeys: suites
957
997
  };
958
- if (data.composition != null) throw new TypeError("Reported scorers cannot contain a composition.");
998
+ if (selectedScope === "experiment" || data.composition != null) throw new TypeError("Expected a formula-based scorer.");
959
999
  return {
960
1000
  calculation: selected,
961
- composition: null
1001
+ composition: null,
1002
+ scope: selectedScope,
1003
+ suiteKeys: suites
962
1004
  };
963
1005
  }
964
1006
  function compositeConfig(value) {
@@ -1026,6 +1068,7 @@ function scorer(data, project, id) {
1026
1068
  return {
1027
1069
  id: identity,
1028
1070
  ...scoring(data),
1071
+ ...data.assigned_suite_keys === void 0 ? {} : { assignedSuiteKeys: suiteKeys(data.assigned_suite_keys) },
1029
1072
  inputScorers: inputScorers(data.input_scorers === void 0 ? [] : data.input_scorers),
1030
1073
  dependentCompositeCount: data.dependent_composite_count === void 0 ? 0 : integer(data.dependent_composite_count, 0),
1031
1074
  name: text(data.name),
@@ -1075,7 +1118,8 @@ function fields(options) {
1075
1118
  name: options.name === void 0 ? void 0 : identifier(options.name, 400),
1076
1119
  description: options.description === void 0 ? void 0 : nullable(options.description, text),
1077
1120
  archived: options.archived === void 0 ? void 0 : boolean(options.archived),
1078
- tags: options.tags === void 0 ? void 0 : tags(options.tags)
1121
+ tags: options.tags === void 0 ? void 0 : tags(options.tags),
1122
+ apply_to_suite_keys: options.applyToSuiteKeys === void 0 ? void 0 : suiteKeys(options.applyToSuiteKeys)
1079
1123
  });
1080
1124
  }
1081
1125
  /** Versioned scorer definitions. Reported evaluators run in the caller; composites calculate on the server. */
@@ -1097,9 +1141,13 @@ var Scorers = class {
1097
1141
  "description",
1098
1142
  "tags",
1099
1143
  "calculation",
1100
- "composition"
1144
+ "composition",
1145
+ "scope",
1146
+ "applyToSuiteKeys"
1101
1147
  ]);
1102
1148
  const selectedCalculation = calculation(options.calculation === void 0 ? "reported" : options.calculation);
1149
+ const selectedScope = scope(options.scope === void 0 ? "item" : options.scope);
1150
+ if (selectedScope === "experiment" && selectedCalculation !== "composite") throw new TypeError("Experiment scorers require a composite formula.");
1103
1151
  if (selectedCalculation === "composite" !== (options.composition != null)) throw new TypeError("Provide composition only for a composite scorer.");
1104
1152
  const selectedKind = kind(options.kind);
1105
1153
  const outputConfig = config(options.config, true);
@@ -1109,15 +1157,17 @@ var Scorers = class {
1109
1157
  name: identifier(options.name, 400),
1110
1158
  kind: selectedKind,
1111
1159
  config: outputConfig,
1160
+ ...selectedScope === "experiment" ? { scope: selectedScope } : {},
1112
1161
  ...selectedCalculation === "composite" ? {
1113
1162
  calculation: selectedCalculation,
1114
- composition: compositionBody(options.composition)
1163
+ composition: compositionBody(options.composition, selectedScope)
1115
1164
  } : {}
1116
1165
  }));
1117
1166
  return this.#request("POST", this.#base, (data) => {
1118
1167
  const result = scorer(data, this.#transport.projectId);
1119
1168
  if (result.kind !== selectedKind) throw new TypeError("Mismatched scorer kind.");
1120
1169
  if (result.calculation !== selectedCalculation) throw new TypeError("Mismatched scorer calculation.");
1170
+ if (result.scope !== selectedScope) throw new TypeError("Mismatched scorer scope.");
1121
1171
  return result;
1122
1172
  }, {
1123
1173
  ...request,
@@ -1149,6 +1199,7 @@ var Scorers = class {
1149
1199
  search: options.search === void 0 ? void 0 : text(options.search),
1150
1200
  kind: options.kind === void 0 ? void 0 : kind(options.kind),
1151
1201
  calculation: options.calculation === void 0 ? void 0 : calculation(options.calculation),
1202
+ scope: options.scope === void 0 ? void 0 : scope(options.scope),
1152
1203
  input_definition_id: options.inputDefinitionId === void 0 ? void 0 : uuid(options.inputDefinitionId),
1153
1204
  archived: options.archived === void 0 ? void 0 : String(boolean(options.archived)),
1154
1205
  order_by: options.orderBy
@@ -1168,12 +1219,17 @@ var Scorers = class {
1168
1219
  "name",
1169
1220
  "description",
1170
1221
  "archived",
1171
- "tags"
1222
+ "tags",
1223
+ "applyToSuiteKeys",
1224
+ "suiteVersionId"
1172
1225
  ]);
1173
1226
  const id = uuid(scorerId);
1174
1227
  return this.#request("PATCH", `${this.#base}${id}/`, (data) => scorer(data, this.#transport.projectId, id), {
1175
1228
  ...request,
1176
- body: encode(json(fields(options)))
1229
+ body: encode(json({
1230
+ ...fields(options),
1231
+ ...options.suiteVersionId === void 0 ? {} : { suite_version_id: uuid(options.suiteVersionId) }
1232
+ }))
1177
1233
  });
1178
1234
  }
1179
1235
  /** Composite versions require a complete composition, even when only changing output config. */
@@ -1183,7 +1239,8 @@ var Scorers = class {
1183
1239
  "composition",
1184
1240
  "baseVersion",
1185
1241
  "name",
1186
- "description"
1242
+ "description",
1243
+ "applyToSuiteKeys"
1187
1244
  ]);
1188
1245
  const id = uuid(scorerId);
1189
1246
  const outputConfig = config(options.config, true);
@@ -1206,13 +1263,16 @@ var Scorers = class {
1206
1263
  keys(options, [
1207
1264
  "kind",
1208
1265
  "composition",
1209
- "config"
1266
+ "config",
1267
+ "scope"
1210
1268
  ]);
1269
+ const selectedScope = scope(options.scope === void 0 ? "item" : options.scope);
1211
1270
  const outputConfig = options.config === void 0 ? void 0 : config(options.config, true);
1212
1271
  if (outputConfig) compositeConfig(outputConfig);
1213
1272
  const body = encode(json(defined({
1214
1273
  kind: kind(options.kind),
1215
- composition: compositionBody(options.composition),
1274
+ composition: compositionBody(options.composition, selectedScope),
1275
+ scope: selectedScope === "experiment" ? selectedScope : void 0,
1216
1276
  config: outputConfig
1217
1277
  })));
1218
1278
  return this.#request("POST", `${this.#base}validate_composition/`, (data) => {
@@ -1452,6 +1512,7 @@ function experimentBody(options) {
1452
1512
  "expectedItemCount",
1453
1513
  "expectedResultCount",
1454
1514
  "datasetRevisionId",
1515
+ "experimentScorerVersionId",
1455
1516
  ...Object.keys(EXPERIMENT_TEXT)
1456
1517
  ]);
1457
1518
  const body = {
@@ -1474,6 +1535,7 @@ function experimentBody(options) {
1474
1535
  if (options.expectedItemCount !== void 0) body.expected_item_count = nullable(options.expectedItemCount, count);
1475
1536
  if (options.expectedResultCount !== void 0) body.expected_result_count = nullable(options.expectedResultCount, count);
1476
1537
  if (options.datasetRevisionId !== void 0) body.dataset_revision_id = nullable(options.datasetRevisionId, uuid);
1538
+ if (options.experimentScorerVersionId !== void 0) body.experiment_scorer_version_id = nullable(options.experimentScorerVersionId, uuid);
1477
1539
  return encode(body);
1478
1540
  }
1479
1541
  function experimentReceipt(data, id) {
@@ -1812,7 +1874,8 @@ function submissionOptions(data) {
1812
1874
  runSource: "run_source",
1813
1875
  expectedItemCount: "expected_item_count",
1814
1876
  expectedResultCount: "expected_result_count",
1815
- datasetRevisionId: "dataset_revision_id"
1877
+ datasetRevisionId: "dataset_revision_id",
1878
+ experimentScorerVersionId: "experiment_scorer_version_id"
1816
1879
  })) if (Object.hasOwn(data, wire)) result[key] = data[wire];
1817
1880
  return result;
1818
1881
  }