@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.
@@ -381,15 +381,30 @@ interface ScorerConfigs {
381
381
  }
382
382
  type ScorerKind = keyof ScorerConfigs;
383
383
  type ScorerCalculation = 'reported' | 'composite';
384
+ type ScorerScope = 'item' | 'experiment';
385
+ type ExperimentScoreStatistic = 'pass_rate' | 'mean' | 'true_rate' | 'category_rate';
386
+ type ExperimentScoreVersionPolicy = 'run' | 'exact' | 'combine';
384
387
  interface ScorerCompositionInput {
385
388
  alias: string;
386
389
  scorerVersionId: string;
387
390
  }
388
- /** Server-evaluated formula with 1–16 pinned reported inputs; nested composites are unsupported. */
391
+ /** Item formula with 1–16 exact reported item scorer versions. */
389
392
  interface ScorerComposition {
390
393
  expression: string;
391
394
  inputs: readonly ScorerCompositionInput[];
392
395
  }
396
+ interface ExperimentScorerCompositionInput extends ScorerCompositionInput {
397
+ statistic: ExperimentScoreStatistic;
398
+ /** Defaults to the single scorer version that successfully scored items in the run. */
399
+ versionPolicy?: ExperimentScoreVersionPolicy;
400
+ /** Required for category_rate. */
401
+ category?: string;
402
+ }
403
+ /** Experiment formula over statistics from item scorers. */
404
+ interface ExperimentScorerComposition extends ScorerComposition {
405
+ inputs: readonly ExperimentScorerCompositionInput[];
406
+ }
407
+ type ScorerFormula = ScorerComposition | ExperimentScorerComposition;
393
408
  /** Pinned input configuration alongside the source scorer's latest version. */
394
409
  type ScorerInput = { [K in ScorerKind]: {
395
410
  alias: string;
@@ -405,9 +420,13 @@ type ScorerInput = { [K in ScorerKind]: {
405
420
  type ScorerConfig = ScorerConfigs[ScorerKind];
406
421
  interface ScorerRecord {
407
422
  calculation: ScorerCalculation;
408
- composition: ScorerComposition | null;
423
+ composition: ScorerFormula | null;
424
+ scope: ScorerScope;
425
+ suiteKeys: string[];
409
426
  inputScorers: ScorerInput[];
410
427
  dependentCompositeCount: number;
428
+ /** Suite defaults across visible versions, returned on detail reads. */
429
+ assignedSuiteKeys?: string[];
411
430
  id: string;
412
431
  name: string;
413
432
  description: string;
@@ -422,7 +441,9 @@ interface ScorerRecord {
422
441
  }
423
442
  interface VersionRecord {
424
443
  calculation: ScorerCalculation;
425
- composition: ScorerComposition | null;
444
+ composition: ScorerFormula | null;
445
+ scope: ScorerScope;
446
+ suiteKeys: string[];
426
447
  id: string;
427
448
  definitionId: string;
428
449
  version: number;
@@ -448,6 +469,7 @@ interface ScorerVersionPage {
448
469
  }
449
470
  type ScorerOrder = 'name' | '-name' | 'kind' | '-kind' | 'created_at' | '-created_at' | 'updated_at' | '-updated_at' | 'current_version' | '-current_version';
450
471
  interface ScorerListOptions extends PageOptions {
472
+ scope?: ScorerScope;
451
473
  calculation?: ScorerCalculation;
452
474
  inputDefinitionId?: string;
453
475
  search?: string;
@@ -462,20 +484,27 @@ interface ScorerMetadata {
462
484
  description?: string | null;
463
485
  archived?: boolean;
464
486
  tags?: readonly string[];
487
+ /** An empty list clears the selected version's suite defaults. */
488
+ applyToSuiteKeys?: readonly string[];
489
+ /** Exact version to assign. Defaults to the current version. */
490
+ suiteVersionId?: string;
465
491
  }
466
492
  type CreateScorerOptions = { [K in ScorerKind]: {
467
493
  name: string;
468
494
  kind: K;
469
495
  config: ScorerConfigs[K];
470
496
  calculation?: ScorerCalculation;
471
- composition?: ScorerComposition | null;
497
+ scope?: ScorerScope;
498
+ applyToSuiteKeys?: readonly string[];
499
+ composition?: ScorerFormula | null;
472
500
  description?: string | null;
473
501
  tags?: readonly string[];
474
502
  }; }[ScorerKind];
475
503
  type ValidateCompositionOptions = { [K in ScorerKind]: {
476
504
  kind: K;
477
- composition: ScorerComposition;
505
+ composition: ScorerFormula;
478
506
  config?: ScorerConfigs[K];
507
+ scope?: ScorerScope;
479
508
  }; }[ScorerKind];
480
509
  /** Versioned scorer definitions. Reported evaluators run in the caller; composites calculate on the server. */
481
510
  declare class Scorers {
@@ -490,8 +519,9 @@ declare class Scorers {
490
519
  /** Composite versions require a complete composition, even when only changing output config. */
491
520
  createVersion(scorerId: string, options: {
492
521
  config: ScorerConfig;
493
- composition?: ScorerComposition | null;
522
+ composition?: ScorerFormula | null;
494
523
  baseVersion?: number;
524
+ applyToSuiteKeys?: readonly string[];
495
525
  name?: string;
496
526
  description?: string | null;
497
527
  }, request?: RequestOptions): Promise<Scorer>;
@@ -513,6 +543,8 @@ interface ExperimentOptions {
513
543
  expectedItemCount?: EvaluationCount | null;
514
544
  expectedResultCount?: EvaluationCount | null;
515
545
  suiteKey?: string | null;
546
+ /** Exact scorer version for the run. Omission or null uses the suite default. */
547
+ experimentScorerVersionId?: string | null;
516
548
  datasetSource?: string | null;
517
549
  datasetIdentifier?: string | null;
518
550
  datasetRevisionIdentifier?: string | null;
@@ -622,5 +654,5 @@ declare class OfflineEvaluations {
622
654
  close(): void;
623
655
  }
624
656
  //#endregion
625
- export { type BooleanScorerConfig, BulkUploadError, type BulkUploadReceipt, type CategoricalScorerConfig, type CategoricalScorerOption, type CreateDatasetItemOptions, type CreateScorerOptions, type Dataset, type DatasetItem, type DatasetItemPage, type DatasetListOptions, type DatasetMetadata, type DatasetOrder, type DatasetPage, type DatasetRevision, type DatasetRevisionPage, DatasetSnapshot, Datasets, EvaluationAPIError, type EvaluationCount, type EvaluationErrorFields, EvaluationItem, type EvaluationItemOptions, EvaluationResult, type EvaluationResultOptions, Experiment, ExperimentCreationAbortedError, type ExperimentOptions, type ExperimentReceipt, type ItemIdentifiers, type ItemListOptions, type ItemPayload, type ItemReceipt, type ItemUpdate, type JSONObject, type JSONValue, type NumericPassingRule, type NumericScorerConfig, OfflineEvaluations, type OfflineEvaluationsOptions, type PageOptions, type Persistence, type RequestOptions, type ResultPayload, type ResultReceipt, type ResultStatus, type ResultUpdate, type RevisionOptions, type ScoreInput, type ScoreValue, type Scorer, type ScorerCalculation, type ScorerComposition, type ScorerCompositionInput, type ScorerConfig, type ScorerConfigs, type ScorerInput, type ScorerKind, type ScorerListOptions, type ScorerMetadata, type ScorerOrder, type ScorerPage, type ScorerVersion, type ScorerVersionPage, Scorers, type UpdateDatasetItemOptions, type UploadReceipt, type ValidateCompositionOptions };
657
+ export { type BooleanScorerConfig, BulkUploadError, type BulkUploadReceipt, type CategoricalScorerConfig, type CategoricalScorerOption, type CreateDatasetItemOptions, type CreateScorerOptions, type Dataset, type DatasetItem, type DatasetItemPage, type DatasetListOptions, type DatasetMetadata, type DatasetOrder, type DatasetPage, type DatasetRevision, type DatasetRevisionPage, DatasetSnapshot, Datasets, EvaluationAPIError, type EvaluationCount, type EvaluationErrorFields, EvaluationItem, type EvaluationItemOptions, EvaluationResult, type EvaluationResultOptions, Experiment, ExperimentCreationAbortedError, type ExperimentOptions, type ExperimentReceipt, type ExperimentScoreStatistic, type ExperimentScoreVersionPolicy, type ExperimentScorerComposition, type ExperimentScorerCompositionInput, type ItemIdentifiers, type ItemListOptions, type ItemPayload, type ItemReceipt, type ItemUpdate, type JSONObject, type JSONValue, type NumericPassingRule, type NumericScorerConfig, OfflineEvaluations, type OfflineEvaluationsOptions, type PageOptions, type Persistence, type RequestOptions, type ResultPayload, type ResultReceipt, type ResultStatus, type ResultUpdate, type RevisionOptions, type ScoreInput, type ScoreValue, type Scorer, type ScorerCalculation, type ScorerComposition, type ScorerCompositionInput, type ScorerConfig, type ScorerConfigs, type ScorerInput, type ScorerKind, type ScorerListOptions, type ScorerMetadata, type ScorerOrder, type ScorerPage, type ScorerScope, type ScorerVersion, type ScorerVersionPage, Scorers, type UpdateDatasetItemOptions, type UploadReceipt, type ValidateCompositionOptions };
626
658
  //# sourceMappingURL=index.d.ts.map
@@ -920,44 +920,86 @@ function alias(value) {
920
920
  ].includes(name)) throw new TypeError("Invalid composite input alias.");
921
921
  return name;
922
922
  }
923
- function composition(value, fromWire = false) {
923
+ function scope(value) {
924
+ if (value !== "item" && value !== "experiment") throw new TypeError("Unsupported scorer scope.");
925
+ return value;
926
+ }
927
+ function suiteKeys(value) {
928
+ if (!Array.isArray(value)) throw new TypeError("Expected a list of suite keys.");
929
+ return Array.from(value, text);
930
+ }
931
+ function compositionInput(entry, fromWire, selectedScope) {
932
+ const input = object(entry);
933
+ const base = {
934
+ alias: alias(input.alias),
935
+ scorerVersionId: uuid(input[fromWire ? "scorer_version_id" : "scorerVersionId"])
936
+ };
937
+ const policyKey = fromWire ? "version_policy" : "versionPolicy";
938
+ const hasStatistics = [
939
+ "statistic",
940
+ policyKey,
941
+ "category"
942
+ ].some((key) => input[key] !== void 0);
943
+ if (selectedScope !== "experiment" && !hasStatistics) return base;
944
+ if (selectedScope === "item") throw new TypeError("Statistics apply to experiment scorers.");
945
+ const statistic = input.statistic;
946
+ if (statistic !== "pass_rate" && statistic !== "mean" && statistic !== "true_rate" && statistic !== "category_rate") throw new TypeError("Unsupported experiment score statistic.");
947
+ const parsed = {
948
+ ...base,
949
+ statistic
950
+ };
951
+ const policy = input[policyKey];
952
+ if (policy !== void 0) {
953
+ if (policy !== "run" && policy !== "exact" && policy !== "combine") throw new TypeError("Unsupported experiment score version policy.");
954
+ parsed.versionPolicy = policy;
955
+ }
956
+ if (statistic === "category_rate") parsed.category = identifier(input.category, 128);
957
+ else if (input.category !== void 0) throw new TypeError("Only category_rate accepts a category key.");
958
+ return parsed;
959
+ }
960
+ function composition(value, fromWire = false, selectedScope) {
924
961
  const data = object(value);
925
962
  const expression = text(data.expression);
926
963
  if (!expression.trim() || Array.from(expression).length > 1024) throw new TypeError("A formula must contain 1–1024 characters.");
927
- if (!Array.isArray(data.inputs) || data.inputs.length < 1 || data.inputs.length > 16) throw new TypeError("A composite requires 1–16 reported inputs.");
928
- const inputs = Array.from(data.inputs, (entry) => {
929
- const input = object(entry);
930
- return {
931
- alias: alias(input.alias),
932
- scorerVersionId: uuid(input[fromWire ? "scorer_version_id" : "scorerVersionId"])
933
- };
934
- });
964
+ if (!Array.isArray(data.inputs) || data.inputs.length < 1 || data.inputs.length > 16) throw new TypeError("A composite requires 1–16 inputs.");
965
+ const inputs = Array.from(data.inputs, (entry) => compositionInput(entry, fromWire, selectedScope));
935
966
  if (new Set(inputs.map((input) => input.alias)).size !== inputs.length) throw new TypeError("Composite input aliases must be unique.");
936
967
  return {
937
968
  expression,
938
969
  inputs
939
970
  };
940
971
  }
941
- function compositionBody(value) {
942
- const parsed = composition(value);
972
+ function compositionBody(value, selectedScope) {
973
+ const parsed = composition(value, false, selectedScope);
943
974
  return {
944
975
  expression: parsed.expression,
945
976
  inputs: parsed.inputs.map((input) => ({
946
977
  alias: input.alias,
947
- scorer_version_id: input.scorerVersionId
978
+ scorer_version_id: input.scorerVersionId,
979
+ ..."statistic" in input ? defined({
980
+ statistic: input.statistic,
981
+ version_policy: input.versionPolicy,
982
+ category: input.category
983
+ }) : {}
948
984
  }))
949
985
  };
950
986
  }
951
987
  function scoring(data) {
952
988
  const selected = calculation(data.calculation === void 0 ? "reported" : data.calculation);
989
+ const selectedScope = scope(data.scope === void 0 ? "item" : data.scope);
990
+ const suites = suiteKeys(data.suite_keys === void 0 ? [] : data.suite_keys);
953
991
  if (selected === "composite") return {
954
992
  calculation: selected,
955
- composition: composition(data.composition, true)
993
+ composition: composition(data.composition, true, selectedScope),
994
+ scope: selectedScope,
995
+ suiteKeys: suites
956
996
  };
957
- if (data.composition != null) throw new TypeError("Reported scorers cannot contain a composition.");
997
+ if (selectedScope === "experiment" || data.composition != null) throw new TypeError("Expected a formula-based scorer.");
958
998
  return {
959
999
  calculation: selected,
960
- composition: null
1000
+ composition: null,
1001
+ scope: selectedScope,
1002
+ suiteKeys: suites
961
1003
  };
962
1004
  }
963
1005
  function compositeConfig(value) {
@@ -1025,6 +1067,7 @@ function scorer(data, project, id) {
1025
1067
  return {
1026
1068
  id: identity,
1027
1069
  ...scoring(data),
1070
+ ...data.assigned_suite_keys === void 0 ? {} : { assignedSuiteKeys: suiteKeys(data.assigned_suite_keys) },
1028
1071
  inputScorers: inputScorers(data.input_scorers === void 0 ? [] : data.input_scorers),
1029
1072
  dependentCompositeCount: data.dependent_composite_count === void 0 ? 0 : integer(data.dependent_composite_count, 0),
1030
1073
  name: text(data.name),
@@ -1074,7 +1117,8 @@ function fields(options) {
1074
1117
  name: options.name === void 0 ? void 0 : identifier(options.name, 400),
1075
1118
  description: options.description === void 0 ? void 0 : nullable(options.description, text),
1076
1119
  archived: options.archived === void 0 ? void 0 : boolean(options.archived),
1077
- tags: options.tags === void 0 ? void 0 : tags(options.tags)
1120
+ tags: options.tags === void 0 ? void 0 : tags(options.tags),
1121
+ apply_to_suite_keys: options.applyToSuiteKeys === void 0 ? void 0 : suiteKeys(options.applyToSuiteKeys)
1078
1122
  });
1079
1123
  }
1080
1124
  /** Versioned scorer definitions. Reported evaluators run in the caller; composites calculate on the server. */
@@ -1096,9 +1140,13 @@ var Scorers = class {
1096
1140
  "description",
1097
1141
  "tags",
1098
1142
  "calculation",
1099
- "composition"
1143
+ "composition",
1144
+ "scope",
1145
+ "applyToSuiteKeys"
1100
1146
  ]);
1101
1147
  const selectedCalculation = calculation(options.calculation === void 0 ? "reported" : options.calculation);
1148
+ const selectedScope = scope(options.scope === void 0 ? "item" : options.scope);
1149
+ if (selectedScope === "experiment" && selectedCalculation !== "composite") throw new TypeError("Experiment scorers require a composite formula.");
1102
1150
  if (selectedCalculation === "composite" !== (options.composition != null)) throw new TypeError("Provide composition only for a composite scorer.");
1103
1151
  const selectedKind = kind(options.kind);
1104
1152
  const outputConfig = config(options.config, true);
@@ -1108,15 +1156,17 @@ var Scorers = class {
1108
1156
  name: identifier(options.name, 400),
1109
1157
  kind: selectedKind,
1110
1158
  config: outputConfig,
1159
+ ...selectedScope === "experiment" ? { scope: selectedScope } : {},
1111
1160
  ...selectedCalculation === "composite" ? {
1112
1161
  calculation: selectedCalculation,
1113
- composition: compositionBody(options.composition)
1162
+ composition: compositionBody(options.composition, selectedScope)
1114
1163
  } : {}
1115
1164
  }));
1116
1165
  return this.#request("POST", this.#base, (data) => {
1117
1166
  const result = scorer(data, this.#transport.projectId);
1118
1167
  if (result.kind !== selectedKind) throw new TypeError("Mismatched scorer kind.");
1119
1168
  if (result.calculation !== selectedCalculation) throw new TypeError("Mismatched scorer calculation.");
1169
+ if (result.scope !== selectedScope) throw new TypeError("Mismatched scorer scope.");
1120
1170
  return result;
1121
1171
  }, {
1122
1172
  ...request,
@@ -1148,6 +1198,7 @@ var Scorers = class {
1148
1198
  search: options.search === void 0 ? void 0 : text(options.search),
1149
1199
  kind: options.kind === void 0 ? void 0 : kind(options.kind),
1150
1200
  calculation: options.calculation === void 0 ? void 0 : calculation(options.calculation),
1201
+ scope: options.scope === void 0 ? void 0 : scope(options.scope),
1151
1202
  input_definition_id: options.inputDefinitionId === void 0 ? void 0 : uuid(options.inputDefinitionId),
1152
1203
  archived: options.archived === void 0 ? void 0 : String(boolean(options.archived)),
1153
1204
  order_by: options.orderBy
@@ -1167,12 +1218,17 @@ var Scorers = class {
1167
1218
  "name",
1168
1219
  "description",
1169
1220
  "archived",
1170
- "tags"
1221
+ "tags",
1222
+ "applyToSuiteKeys",
1223
+ "suiteVersionId"
1171
1224
  ]);
1172
1225
  const id = uuid(scorerId);
1173
1226
  return this.#request("PATCH", `${this.#base}${id}/`, (data) => scorer(data, this.#transport.projectId, id), {
1174
1227
  ...request,
1175
- body: encode(json(fields(options)))
1228
+ body: encode(json({
1229
+ ...fields(options),
1230
+ ...options.suiteVersionId === void 0 ? {} : { suite_version_id: uuid(options.suiteVersionId) }
1231
+ }))
1176
1232
  });
1177
1233
  }
1178
1234
  /** Composite versions require a complete composition, even when only changing output config. */
@@ -1182,7 +1238,8 @@ var Scorers = class {
1182
1238
  "composition",
1183
1239
  "baseVersion",
1184
1240
  "name",
1185
- "description"
1241
+ "description",
1242
+ "applyToSuiteKeys"
1186
1243
  ]);
1187
1244
  const id = uuid(scorerId);
1188
1245
  const outputConfig = config(options.config, true);
@@ -1205,13 +1262,16 @@ var Scorers = class {
1205
1262
  keys(options, [
1206
1263
  "kind",
1207
1264
  "composition",
1208
- "config"
1265
+ "config",
1266
+ "scope"
1209
1267
  ]);
1268
+ const selectedScope = scope(options.scope === void 0 ? "item" : options.scope);
1210
1269
  const outputConfig = options.config === void 0 ? void 0 : config(options.config, true);
1211
1270
  if (outputConfig) compositeConfig(outputConfig);
1212
1271
  const body = encode(json(defined({
1213
1272
  kind: kind(options.kind),
1214
- composition: compositionBody(options.composition),
1273
+ composition: compositionBody(options.composition, selectedScope),
1274
+ scope: selectedScope === "experiment" ? selectedScope : void 0,
1215
1275
  config: outputConfig
1216
1276
  })));
1217
1277
  return this.#request("POST", `${this.#base}validate_composition/`, (data) => {
@@ -1451,6 +1511,7 @@ function experimentBody(options) {
1451
1511
  "expectedItemCount",
1452
1512
  "expectedResultCount",
1453
1513
  "datasetRevisionId",
1514
+ "experimentScorerVersionId",
1454
1515
  ...Object.keys(EXPERIMENT_TEXT)
1455
1516
  ]);
1456
1517
  const body = {
@@ -1473,6 +1534,7 @@ function experimentBody(options) {
1473
1534
  if (options.expectedItemCount !== void 0) body.expected_item_count = nullable(options.expectedItemCount, count);
1474
1535
  if (options.expectedResultCount !== void 0) body.expected_result_count = nullable(options.expectedResultCount, count);
1475
1536
  if (options.datasetRevisionId !== void 0) body.dataset_revision_id = nullable(options.datasetRevisionId, uuid);
1537
+ if (options.experimentScorerVersionId !== void 0) body.experiment_scorer_version_id = nullable(options.experimentScorerVersionId, uuid);
1476
1538
  return encode(body);
1477
1539
  }
1478
1540
  function experimentReceipt(data, id) {
@@ -1811,7 +1873,8 @@ function submissionOptions(data) {
1811
1873
  runSource: "run_source",
1812
1874
  expectedItemCount: "expected_item_count",
1813
1875
  expectedResultCount: "expected_result_count",
1814
- datasetRevisionId: "dataset_revision_id"
1876
+ datasetRevisionId: "dataset_revision_id",
1877
+ experimentScorerVersionId: "experiment_scorer_version_id"
1815
1878
  })) if (Object.hasOwn(data, wire)) result[key] = data[wire];
1816
1879
  return result;
1817
1880
  }