Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

@braintrust/core

Package Overview
Dependencies
Maintainers
1
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@braintrust/core - npm Package Compare versions

Comparing version
0.0.15
to
0.0.16
+173
dist/main/index.d.mts
declare const TRANSACTION_ID_FIELD = "_xact_id";
declare const OBJECT_DELETE_FIELD = "_object_delete";
declare const CREATED_FIELD = "created";
declare const ID_FIELD = "id";
declare const IS_MERGE_FIELD = "_is_merge";
declare const MERGE_PATHS_FIELD = "_merge_paths";
declare const AUDIT_SOURCE_FIELD = "_audit_source";
declare const AUDIT_METADATA_FIELD = "_audit_metadata";
declare const VALID_SOURCES: readonly ["app", "api", "external"];
type Source = (typeof VALID_SOURCES)[number];
declare const PARENT_ID_FIELD = "_parent_id";
type TransactionId = bigint;
declare const ALL_ROW_ID_FIELDS: string[];
declare function mergeRowBatch<T extends {
id: string;
[IS_MERGE_FIELD]?: boolean;
}>(rows: T[]): T[];
type IdField = {
id: string;
};
type InputField = {
input: unknown;
};
type InputsField = {
inputs: unknown;
};
type OtherExperimentLogFields = {
output: unknown;
expected: unknown;
scores: Record<string, number | null>;
metadata: Record<string, unknown>;
metrics: Record<string, unknown>;
datasetRecordId: string;
};
type ExperimentLogPartialArgs = Partial<OtherExperimentLogFields> & Partial<InputField | InputsField>;
type ExperimentLogFullArgs = Partial<Omit<OtherExperimentLogFields, "output" | "scores">> & Required<Pick<OtherExperimentLogFields, "output" | "scores">> & Partial<InputField | InputsField> & Partial<IdField>;
type LogFeedbackFullArgs = IdField & Partial<Omit<OtherExperimentLogFields, "output" | "metrics" | "datasetRecordId"> & {
comment: string;
source: Source;
}>;
interface ParentExperimentIds {
kind: "experiment";
project_id: string;
experiment_id: string;
}
interface ParentProjectLogIds {
kind: "project_log";
org_id: string;
project_id: string;
log_id: "g";
}
type LogCommentFullArgs = IdField & {
created: string;
origin: {
id: string;
};
comment: {
text: string;
};
[AUDIT_SOURCE_FIELD]: Source;
[AUDIT_METADATA_FIELD]?: Record<string, unknown>;
} & Omit<ParentExperimentIds | ParentProjectLogIds, "kind">;
type SanitizedExperimentLogPartialArgs = Partial<OtherExperimentLogFields> & Partial<InputField>;
type ExperimentEvent = Partial<InputField> & Partial<OtherExperimentLogFields> & {
id: string;
span_id?: string;
root_span_id?: string;
project_id: string;
experiment_id: string;
[IS_MERGE_FIELD]: boolean;
} & Partial<{
created: string;
span_parents: string[];
span_attributes: Record<string, unknown>;
context: Record<string, unknown>;
[PARENT_ID_FIELD]: string;
[AUDIT_SOURCE_FIELD]: Source;
[AUDIT_METADATA_FIELD]?: Record<string, unknown>;
}>;
type DatasetEvent = {
input?: unknown;
metadata?: unknown;
id: string;
project_id: string;
dataset_id: string;
created: string;
} & ({
expected?: unknown;
} | {
output?: unknown;
});
type LoggingEvent = Omit<ExperimentEvent, "experiment_id"> & {
org_id: string;
log_id: "g";
};
type CommentEvent = IdField & {
created: string;
origin: {
id: string;
};
comment: {
text: string;
};
[AUDIT_SOURCE_FIELD]: Source;
[AUDIT_METADATA_FIELD]?: Record<string, unknown>;
} & Omit<ParentExperimentIds | ParentProjectLogIds, "kind">;
type BackgroundLogEvent = ExperimentEvent | DatasetEvent | LoggingEvent | CommentEvent;
declare const DEFAULT_IS_LEGACY_DATASET = true;
interface LegacyDatasetRecord {
id: string;
input: any;
output: any;
metadata: any;
}
interface NewDatasetRecord {
id: string;
input: any;
expected: any;
metadata: any;
}
type DatasetRecord<IsLegacyDataset extends boolean = typeof DEFAULT_IS_LEGACY_DATASET> = IsLegacyDataset extends true ? LegacyDatasetRecord : NewDatasetRecord;
type AnyDatasetRecord = DatasetRecord<boolean>;
declare function ensureDatasetRecord<IsLegacyDataset extends boolean = typeof DEFAULT_IS_LEGACY_DATASET>(r: AnyDatasetRecord, legacy: IsLegacyDataset): DatasetRecord<IsLegacyDataset>;
declare function ensureLegacyDatasetRecord(r: AnyDatasetRecord): DatasetRecord<true>;
declare function ensureNewDatasetRecord(r: AnyDatasetRecord): DatasetRecord<false>;
declare function makeLegacyEvent(e: BackgroundLogEvent): BackgroundLogEvent;
interface Score {
name: string;
score: number | null;
metadata?: Record<string, unknown>;
error?: unknown;
}
type ScorerArgs<Output, Extra> = {
output: Output;
expected?: Output;
} & Extra;
type Scorer<Output, Extra> = (args: ScorerArgs<Output, Extra>) => Score | Promise<Score>;
declare function mergeDicts(mergeInto: Record<string, unknown>, mergeFrom: Record<string, unknown>): Record<string, unknown>;
declare function capitalize(s: string, sep?: string): string;
declare enum SpanTypeAttribute {
LLM = "llm",
SCORE = "score",
FUNCTION = "function",
EVAL = "eval",
TASK = "task",
TOOL = "tool"
}
interface RepoInfo {
commit?: string;
branch?: string;
tag?: string;
dirty?: boolean;
author_name?: string;
author_email?: string;
commit_message?: string;
commit_time?: string;
git_diff?: string;
}
type GitFields = Array<keyof RepoInfo>;
type CollectMetadata = "all" | "none" | "some";
type GitMetadataSettings = {
collect: CollectMetadata;
fields?: GitFields;
};
declare function mergeGitMetadataSettings(s1: GitMetadataSettings, s2: GitMetadataSettings): GitMetadataSettings;
export { ALL_ROW_ID_FIELDS, AUDIT_METADATA_FIELD, AUDIT_SOURCE_FIELD, type AnyDatasetRecord, type BackgroundLogEvent, CREATED_FIELD, type CollectMetadata, type CommentEvent, DEFAULT_IS_LEGACY_DATASET, type DatasetEvent, type DatasetRecord, type ExperimentEvent, type ExperimentLogFullArgs, type ExperimentLogPartialArgs, type GitFields, type GitMetadataSettings, ID_FIELD, IS_MERGE_FIELD, type IdField, type InputField, type InputsField, type LogCommentFullArgs, type LogFeedbackFullArgs, type LoggingEvent, MERGE_PATHS_FIELD, OBJECT_DELETE_FIELD, type OtherExperimentLogFields, PARENT_ID_FIELD, type ParentExperimentIds, type ParentProjectLogIds, type RepoInfo, type SanitizedExperimentLogPartialArgs, type Score, type Scorer, type ScorerArgs, type Source, SpanTypeAttribute, TRANSACTION_ID_FIELD, type TransactionId, VALID_SOURCES, capitalize, ensureDatasetRecord, ensureLegacyDatasetRecord, ensureNewDatasetRecord, makeLegacyEvent, mergeDicts, mergeGitMetadataSettings, mergeRowBatch };
declare const TRANSACTION_ID_FIELD = "_xact_id";
declare const OBJECT_DELETE_FIELD = "_object_delete";
declare const CREATED_FIELD = "created";
declare const ID_FIELD = "id";
declare const IS_MERGE_FIELD = "_is_merge";
declare const MERGE_PATHS_FIELD = "_merge_paths";
declare const AUDIT_SOURCE_FIELD = "_audit_source";
declare const AUDIT_METADATA_FIELD = "_audit_metadata";
declare const VALID_SOURCES: readonly ["app", "api", "external"];
type Source = (typeof VALID_SOURCES)[number];
declare const PARENT_ID_FIELD = "_parent_id";
type TransactionId = bigint;
declare const ALL_ROW_ID_FIELDS: string[];
declare function mergeRowBatch<T extends {
id: string;
[IS_MERGE_FIELD]?: boolean;
}>(rows: T[]): T[];
type IdField = {
id: string;
};
type InputField = {
input: unknown;
};
type InputsField = {
inputs: unknown;
};
type OtherExperimentLogFields = {
output: unknown;
expected: unknown;
scores: Record<string, number | null>;
metadata: Record<string, unknown>;
metrics: Record<string, unknown>;
datasetRecordId: string;
};
type ExperimentLogPartialArgs = Partial<OtherExperimentLogFields> & Partial<InputField | InputsField>;
type ExperimentLogFullArgs = Partial<Omit<OtherExperimentLogFields, "output" | "scores">> & Required<Pick<OtherExperimentLogFields, "output" | "scores">> & Partial<InputField | InputsField> & Partial<IdField>;
type LogFeedbackFullArgs = IdField & Partial<Omit<OtherExperimentLogFields, "output" | "metrics" | "datasetRecordId"> & {
comment: string;
source: Source;
}>;
interface ParentExperimentIds {
kind: "experiment";
project_id: string;
experiment_id: string;
}
interface ParentProjectLogIds {
kind: "project_log";
org_id: string;
project_id: string;
log_id: "g";
}
type LogCommentFullArgs = IdField & {
created: string;
origin: {
id: string;
};
comment: {
text: string;
};
[AUDIT_SOURCE_FIELD]: Source;
[AUDIT_METADATA_FIELD]?: Record<string, unknown>;
} & Omit<ParentExperimentIds | ParentProjectLogIds, "kind">;
type SanitizedExperimentLogPartialArgs = Partial<OtherExperimentLogFields> & Partial<InputField>;
type ExperimentEvent = Partial<InputField> & Partial<OtherExperimentLogFields> & {
id: string;
span_id?: string;
root_span_id?: string;
project_id: string;
experiment_id: string;
[IS_MERGE_FIELD]: boolean;
} & Partial<{
created: string;
span_parents: string[];
span_attributes: Record<string, unknown>;
context: Record<string, unknown>;
[PARENT_ID_FIELD]: string;
[AUDIT_SOURCE_FIELD]: Source;
[AUDIT_METADATA_FIELD]?: Record<string, unknown>;
}>;
type DatasetEvent = {
input?: unknown;
metadata?: unknown;
id: string;
project_id: string;
dataset_id: string;
created: string;
} & ({
expected?: unknown;
} | {
output?: unknown;
});
type LoggingEvent = Omit<ExperimentEvent, "experiment_id"> & {
org_id: string;
log_id: "g";
};
type CommentEvent = IdField & {
created: string;
origin: {
id: string;
};
comment: {
text: string;
};
[AUDIT_SOURCE_FIELD]: Source;
[AUDIT_METADATA_FIELD]?: Record<string, unknown>;
} & Omit<ParentExperimentIds | ParentProjectLogIds, "kind">;
type BackgroundLogEvent = ExperimentEvent | DatasetEvent | LoggingEvent | CommentEvent;
declare const DEFAULT_IS_LEGACY_DATASET = true;
interface LegacyDatasetRecord {
id: string;
input: any;
output: any;
metadata: any;
}
interface NewDatasetRecord {
id: string;
input: any;
expected: any;
metadata: any;
}
type DatasetRecord<IsLegacyDataset extends boolean = typeof DEFAULT_IS_LEGACY_DATASET> = IsLegacyDataset extends true ? LegacyDatasetRecord : NewDatasetRecord;
type AnyDatasetRecord = DatasetRecord<boolean>;
declare function ensureDatasetRecord<IsLegacyDataset extends boolean = typeof DEFAULT_IS_LEGACY_DATASET>(r: AnyDatasetRecord, legacy: IsLegacyDataset): DatasetRecord<IsLegacyDataset>;
declare function ensureLegacyDatasetRecord(r: AnyDatasetRecord): DatasetRecord<true>;
declare function ensureNewDatasetRecord(r: AnyDatasetRecord): DatasetRecord<false>;
declare function makeLegacyEvent(e: BackgroundLogEvent): BackgroundLogEvent;
interface Score {
name: string;
score: number | null;
metadata?: Record<string, unknown>;
error?: unknown;
}
type ScorerArgs<Output, Extra> = {
output: Output;
expected?: Output;
} & Extra;
type Scorer<Output, Extra> = (args: ScorerArgs<Output, Extra>) => Score | Promise<Score>;
declare function mergeDicts(mergeInto: Record<string, unknown>, mergeFrom: Record<string, unknown>): Record<string, unknown>;
declare function capitalize(s: string, sep?: string): string;
declare enum SpanTypeAttribute {
LLM = "llm",
SCORE = "score",
FUNCTION = "function",
EVAL = "eval",
TASK = "task",
TOOL = "tool"
}
interface RepoInfo {
commit?: string;
branch?: string;
tag?: string;
dirty?: boolean;
author_name?: string;
author_email?: string;
commit_message?: string;
commit_time?: string;
git_diff?: string;
}
type GitFields = Array<keyof RepoInfo>;
type CollectMetadata = "all" | "none" | "some";
type GitMetadataSettings = {
collect: CollectMetadata;
fields?: GitFields;
};
declare function mergeGitMetadataSettings(s1: GitMetadataSettings, s2: GitMetadataSettings): GitMetadataSettings;
export { ALL_ROW_ID_FIELDS, AUDIT_METADATA_FIELD, AUDIT_SOURCE_FIELD, type AnyDatasetRecord, type BackgroundLogEvent, CREATED_FIELD, type CollectMetadata, type CommentEvent, DEFAULT_IS_LEGACY_DATASET, type DatasetEvent, type DatasetRecord, type ExperimentEvent, type ExperimentLogFullArgs, type ExperimentLogPartialArgs, type GitFields, type GitMetadataSettings, ID_FIELD, IS_MERGE_FIELD, type IdField, type InputField, type InputsField, type LogCommentFullArgs, type LogFeedbackFullArgs, type LoggingEvent, MERGE_PATHS_FIELD, OBJECT_DELETE_FIELD, type OtherExperimentLogFields, PARENT_ID_FIELD, type ParentExperimentIds, type ParentProjectLogIds, type RepoInfo, type SanitizedExperimentLogPartialArgs, type Score, type Scorer, type ScorerArgs, type Source, SpanTypeAttribute, TRANSACTION_ID_FIELD, type TransactionId, VALID_SOURCES, capitalize, ensureDatasetRecord, ensureLegacyDatasetRecord, ensureNewDatasetRecord, makeLegacyEvent, mergeDicts, mergeGitMetadataSettings, mergeRowBatch };
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
ALL_ROW_ID_FIELDS: () => ALL_ROW_ID_FIELDS,
AUDIT_METADATA_FIELD: () => AUDIT_METADATA_FIELD,
AUDIT_SOURCE_FIELD: () => AUDIT_SOURCE_FIELD,
CREATED_FIELD: () => CREATED_FIELD,
DEFAULT_IS_LEGACY_DATASET: () => DEFAULT_IS_LEGACY_DATASET,
ID_FIELD: () => ID_FIELD,
IS_MERGE_FIELD: () => IS_MERGE_FIELD,
MERGE_PATHS_FIELD: () => MERGE_PATHS_FIELD,
OBJECT_DELETE_FIELD: () => OBJECT_DELETE_FIELD,
PARENT_ID_FIELD: () => PARENT_ID_FIELD,
SpanTypeAttribute: () => SpanTypeAttribute,
TRANSACTION_ID_FIELD: () => TRANSACTION_ID_FIELD,
VALID_SOURCES: () => VALID_SOURCES,
capitalize: () => capitalize,
ensureDatasetRecord: () => ensureDatasetRecord,
ensureLegacyDatasetRecord: () => ensureLegacyDatasetRecord,
ensureNewDatasetRecord: () => ensureNewDatasetRecord,
makeLegacyEvent: () => makeLegacyEvent,
mergeDicts: () => mergeDicts,
mergeGitMetadataSettings: () => mergeGitMetadataSettings,
mergeRowBatch: () => mergeRowBatch
});
module.exports = __toCommonJS(src_exports);
// src/db_fields.ts
var TRANSACTION_ID_FIELD = "_xact_id";
var OBJECT_DELETE_FIELD = "_object_delete";
var CREATED_FIELD = "created";
var ID_FIELD = "id";
var IS_MERGE_FIELD = "_is_merge";
var MERGE_PATHS_FIELD = "_merge_paths";
var AUDIT_SOURCE_FIELD = "_audit_source";
var AUDIT_METADATA_FIELD = "_audit_metadata";
var VALID_SOURCES = ["app", "api", "external"];
var PARENT_ID_FIELD = "_parent_id";
var ALL_ROW_ID_FIELDS = [
ID_FIELD,
TRANSACTION_ID_FIELD,
CREATED_FIELD,
IS_MERGE_FIELD,
MERGE_PATHS_FIELD,
OBJECT_DELETE_FIELD
];
// src/util.ts
function mergeDicts(mergeInto, mergeFrom) {
for (const [k, mergeFromV] of Object.entries(mergeFrom)) {
const mergeIntoV = mergeInto[k];
if (mergeIntoV instanceof Object && !Array.isArray(mergeIntoV) && mergeFrom instanceof Object && !Array.isArray(mergeFromV)) {
mergeDicts(
mergeIntoV,
mergeFromV
);
} else {
mergeInto[k] = mergeFromV;
}
}
return mergeInto;
}
function capitalize(s, sep) {
const items = sep ? s.split(sep) : [s];
return items.map((s2) => s2 ? s2.charAt(0).toUpperCase() + s2.slice(1) : s2).join(sep || "");
}
// src/merge_row_batch.ts
function generateMergedRowKey(row) {
return JSON.stringify(
[
"org_id",
"project_id",
"experiment_id",
"dataset_id",
"prompt_session_id",
"log_id",
"id"
].map((k) => row[k])
);
}
function mergeRowBatch(rows) {
const out = [];
const remainingRows = [];
for (const row of rows) {
if (row.id === void 0) {
out.push(row);
} else {
remainingRows.push(row);
}
}
const rowGroups = {};
for (const row of remainingRows) {
const key = generateMergedRowKey(row);
const existingRow = rowGroups[key];
if (existingRow !== void 0 && row[IS_MERGE_FIELD]) {
const preserveNoMerge = !existingRow[IS_MERGE_FIELD];
mergeDicts(existingRow, row);
if (preserveNoMerge) {
delete existingRow[IS_MERGE_FIELD];
}
} else {
rowGroups[key] = row;
}
}
out.push(...Object.values(rowGroups));
return out;
}
// src/object.ts
var DEFAULT_IS_LEGACY_DATASET = true;
function ensureDatasetRecord(r, legacy) {
if (legacy) {
return ensureLegacyDatasetRecord(r);
} else {
return ensureNewDatasetRecord(r);
}
}
function ensureLegacyDatasetRecord(r) {
if ("output" in r) {
return r;
}
const row = {
...r,
output: r.expected
};
delete row.expected;
return row;
}
function ensureNewDatasetRecord(r) {
if ("expected" in r) {
return r;
}
const row = {
...r,
expected: r.output
};
delete row.output;
return row;
}
function makeLegacyEvent(e) {
if (!("dataset_id" in e) || !("expected" in e)) {
return e;
}
const event = {
...e,
output: e.expected
};
delete event.expected;
if (MERGE_PATHS_FIELD in event) {
for (const path of event[MERGE_PATHS_FIELD] || []) {
if (path.length > 0 && path[0] === "expected") {
path[0] = "output";
}
}
}
return event;
}
// src/span_types.ts
var SpanTypeAttribute = /* @__PURE__ */ ((SpanTypeAttribute2) => {
SpanTypeAttribute2["LLM"] = "llm";
SpanTypeAttribute2["SCORE"] = "score";
SpanTypeAttribute2["FUNCTION"] = "function";
SpanTypeAttribute2["EVAL"] = "eval";
SpanTypeAttribute2["TASK"] = "task";
SpanTypeAttribute2["TOOL"] = "tool";
return SpanTypeAttribute2;
})(SpanTypeAttribute || {});
// src/git_fields.ts
function mergeGitMetadataSettings(s1, s2) {
var _a;
if (s1.collect === "all") {
return s2;
} else if (s2.collect === "all") {
return s1;
} else if (s1.collect === "none") {
return s1;
} else if (s2.collect === "none") {
return s2;
}
const fields = ((_a = s1.fields) != null ? _a : []).filter((f) => {
var _a2;
return ((_a2 = s2.fields) != null ? _a2 : []).includes(f);
});
const collect = fields.length > 0 ? "some" : "none";
return { collect, fields };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ALL_ROW_ID_FIELDS,
AUDIT_METADATA_FIELD,
AUDIT_SOURCE_FIELD,
CREATED_FIELD,
DEFAULT_IS_LEGACY_DATASET,
ID_FIELD,
IS_MERGE_FIELD,
MERGE_PATHS_FIELD,
OBJECT_DELETE_FIELD,
PARENT_ID_FIELD,
SpanTypeAttribute,
TRANSACTION_ID_FIELD,
VALID_SOURCES,
capitalize,
ensureDatasetRecord,
ensureLegacyDatasetRecord,
ensureNewDatasetRecord,
makeLegacyEvent,
mergeDicts,
mergeGitMetadataSettings,
mergeRowBatch
});
// src/db_fields.ts
var TRANSACTION_ID_FIELD = "_xact_id";
var OBJECT_DELETE_FIELD = "_object_delete";
var CREATED_FIELD = "created";
var ID_FIELD = "id";
var IS_MERGE_FIELD = "_is_merge";
var MERGE_PATHS_FIELD = "_merge_paths";
var AUDIT_SOURCE_FIELD = "_audit_source";
var AUDIT_METADATA_FIELD = "_audit_metadata";
var VALID_SOURCES = ["app", "api", "external"];
var PARENT_ID_FIELD = "_parent_id";
var ALL_ROW_ID_FIELDS = [
ID_FIELD,
TRANSACTION_ID_FIELD,
CREATED_FIELD,
IS_MERGE_FIELD,
MERGE_PATHS_FIELD,
OBJECT_DELETE_FIELD
];
// src/util.ts
function mergeDicts(mergeInto, mergeFrom) {
for (const [k, mergeFromV] of Object.entries(mergeFrom)) {
const mergeIntoV = mergeInto[k];
if (mergeIntoV instanceof Object && !Array.isArray(mergeIntoV) && mergeFrom instanceof Object && !Array.isArray(mergeFromV)) {
mergeDicts(
mergeIntoV,
mergeFromV
);
} else {
mergeInto[k] = mergeFromV;
}
}
return mergeInto;
}
function capitalize(s, sep) {
const items = sep ? s.split(sep) : [s];
return items.map((s2) => s2 ? s2.charAt(0).toUpperCase() + s2.slice(1) : s2).join(sep || "");
}
// src/merge_row_batch.ts
function generateMergedRowKey(row) {
return JSON.stringify(
[
"org_id",
"project_id",
"experiment_id",
"dataset_id",
"prompt_session_id",
"log_id",
"id"
].map((k) => row[k])
);
}
function mergeRowBatch(rows) {
const out = [];
const remainingRows = [];
for (const row of rows) {
if (row.id === void 0) {
out.push(row);
} else {
remainingRows.push(row);
}
}
const rowGroups = {};
for (const row of remainingRows) {
const key = generateMergedRowKey(row);
const existingRow = rowGroups[key];
if (existingRow !== void 0 && row[IS_MERGE_FIELD]) {
const preserveNoMerge = !existingRow[IS_MERGE_FIELD];
mergeDicts(existingRow, row);
if (preserveNoMerge) {
delete existingRow[IS_MERGE_FIELD];
}
} else {
rowGroups[key] = row;
}
}
out.push(...Object.values(rowGroups));
return out;
}
// src/object.ts
var DEFAULT_IS_LEGACY_DATASET = true;
function ensureDatasetRecord(r, legacy) {
if (legacy) {
return ensureLegacyDatasetRecord(r);
} else {
return ensureNewDatasetRecord(r);
}
}
function ensureLegacyDatasetRecord(r) {
if ("output" in r) {
return r;
}
const row = {
...r,
output: r.expected
};
delete row.expected;
return row;
}
function ensureNewDatasetRecord(r) {
if ("expected" in r) {
return r;
}
const row = {
...r,
expected: r.output
};
delete row.output;
return row;
}
function makeLegacyEvent(e) {
if (!("dataset_id" in e) || !("expected" in e)) {
return e;
}
const event = {
...e,
output: e.expected
};
delete event.expected;
if (MERGE_PATHS_FIELD in event) {
for (const path of event[MERGE_PATHS_FIELD] || []) {
if (path.length > 0 && path[0] === "expected") {
path[0] = "output";
}
}
}
return event;
}
// src/span_types.ts
var SpanTypeAttribute = /* @__PURE__ */ ((SpanTypeAttribute2) => {
SpanTypeAttribute2["LLM"] = "llm";
SpanTypeAttribute2["SCORE"] = "score";
SpanTypeAttribute2["FUNCTION"] = "function";
SpanTypeAttribute2["EVAL"] = "eval";
SpanTypeAttribute2["TASK"] = "task";
SpanTypeAttribute2["TOOL"] = "tool";
return SpanTypeAttribute2;
})(SpanTypeAttribute || {});
// src/git_fields.ts
function mergeGitMetadataSettings(s1, s2) {
var _a;
if (s1.collect === "all") {
return s2;
} else if (s2.collect === "all") {
return s1;
} else if (s1.collect === "none") {
return s1;
} else if (s2.collect === "none") {
return s2;
}
const fields = ((_a = s1.fields) != null ? _a : []).filter((f) => {
var _a2;
return ((_a2 = s2.fields) != null ? _a2 : []).includes(f);
});
const collect = fields.length > 0 ? "some" : "none";
return { collect, fields };
}
export {
ALL_ROW_ID_FIELDS,
AUDIT_METADATA_FIELD,
AUDIT_SOURCE_FIELD,
CREATED_FIELD,
DEFAULT_IS_LEGACY_DATASET,
ID_FIELD,
IS_MERGE_FIELD,
MERGE_PATHS_FIELD,
OBJECT_DELETE_FIELD,
PARENT_ID_FIELD,
SpanTypeAttribute,
TRANSACTION_ID_FIELD,
VALID_SOURCES,
capitalize,
ensureDatasetRecord,
ensureLegacyDatasetRecord,
ensureNewDatasetRecord,
makeLegacyEvent,
mergeDicts,
mergeGitMetadataSettings,
mergeRowBatch
};

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/typespecs/index.ts
var typespecs_exports = {};
__export(typespecs_exports, {
allFetchFiltersSchema: () => allFetchFiltersSchema,
apiKeySchema: () => apiKeySchema,
appLimitSchema: () => appLimitSchema,
auditSourcesSchema: () => auditSourcesSchema,
comparisonExperimentIdParamSchema: () => comparisonExperimentIdParamSchema,
crossObjectInsertRequestSchema: () => crossObjectInsertRequestSchema,
crossObjectInsertResponseSchema: () => crossObjectInsertResponseSchema,
datasetSchema: () => datasetSchema,
datetimeStringSchema: () => datetimeStringSchema,
endingBeforeSchema: () => endingBeforeSchema,
eventObjectSchemas: () => eventObjectSchemas,
experimentSchema: () => experimentSchema,
fetchEventsRequestSchema: () => fetchEventsRequestSchema,
fetchFiltersSchema: () => fetchFiltersSchema,
fetchLimitSchema: () => fetchLimitSchema,
getEventObjectArticle: () => getEventObjectArticle,
getEventObjectDescription: () => getEventObjectDescription,
getEventObjectType: () => getEventObjectType,
insertEventsResponseSchema: () => insertEventsResponseSchema,
jsonSchema: () => jsonSchema,
literalSchema: () => literalSchema,
maxRootSpanIdSchema: () => maxRootSpanIdSchema,
maxXactIdSchema: () => maxXactIdSchema,
meSchema: () => meSchema,
memberSchema: () => memberSchema,
objectSchemas: () => objectSchemas,
objectTypeSummarizeResponseSchemas: () => objectTypeSummarizeResponseSchemas,
objectTypes: () => objectTypes,
organizationSchema: () => organizationSchema,
projectSchema: () => projectSchema,
startingAfterSchema: () => startingAfterSchema,
summarizeDataParamSchema: () => summarizeDataParamSchema,
summarizeScoresParamSchema: () => summarizeScoresParamSchema,
userSchema: () => userSchema,
versionSchema: () => versionSchema
});
module.exports = __toCommonJS(typespecs_exports);
// src/typespecs/api_types.ts
var import_zod_to_openapi3 = require("@asteasolutions/zod-to-openapi");
var import_zod4 = require("zod");
// src/typespecs/app_types.ts
var import_zod_to_openapi2 = require("@asteasolutions/zod-to-openapi");
var import_zod3 = require("zod");
// src/typespecs/common_types.ts
var import_zod = require("zod");
var literalSchema = import_zod.z.union([
import_zod.z.string(),
import_zod.z.number(),
import_zod.z.boolean(),
import_zod.z.null()
]);
var jsonSchema = import_zod.z.lazy(
() => import_zod.z.union([literalSchema, import_zod.z.array(jsonSchema), import_zod.z.record(jsonSchema)])
);
var datetimeStringSchema = import_zod.z.string().datetime({ offset: true });
var objectTypes = ["project", "experiment", "dataset"];
function getEventObjectType(objectType) {
return objectType === "project" ? "project_logs" : objectType;
}
function getEventObjectDescription(objectType) {
return getEventObjectType(objectType).replace("_", " ");
}
function getEventObjectArticle(objectType) {
return objectType === "experiment" ? "an" : "a";
}
// src/typespecs/custom_types.ts
var import_zod_to_openapi = require("@asteasolutions/zod-to-openapi");
var import_zod2 = require("zod");
(0, import_zod_to_openapi.extendZodWithOpenApi)(import_zod2.z);
var modeSchema = import_zod2.z.enum(["default", "stainless"]);
var mode = modeSchema.parse(
typeof process === "undefined" ? "default" : "stainless"
);
var modeToTypes = {
default: {
any: import_zod2.z.any(),
literalTrue: import_zod2.z.literal(true),
literalFalse: import_zod2.z.literal(false)
},
stainless: {
// Stainless requires schemas which are completely permissive to be
// tagged.
any: import_zod2.z.any().openapi({ ["x-stainless-any"]: true }),
// Stainless does not support boolean literals in all SDKs.
literalTrue: import_zod2.z.boolean(),
literalFalse: import_zod2.z.boolean()
}
};
var customTypes = modeToTypes[mode];
// src/typespecs/app_types.ts
(0, import_zod_to_openapi2.extendZodWithOpenApi)(import_zod3.z);
function generateBaseTableSchema(objectName, opts) {
let nameDescription = `Name of the ${objectName}`;
if (opts == null ? void 0 : opts.underProject) {
nameDescription += `. Within a project, ${objectName} names are unique`;
}
return import_zod3.z.object({
id: import_zod3.z.string().uuid().describe(`Unique identifier for the ${objectName}`),
project_id: import_zod3.z.string().uuid().describe(
`Unique identifier for the project that the ${objectName} belongs under`
),
name: import_zod3.z.string().describe(nameDescription),
description: import_zod3.z.string().nullish().describe(`Textual description of the ${objectName}`),
created: datetimeStringSchema.nullish().describe(`Date of ${objectName} creation`),
deleted_at: datetimeStringSchema.nullish().describe(
`Date of ${objectName} deletion, or null if the ${objectName} is still active`
),
user_id: import_zod3.z.string().uuid().nullish().describe(`Identifies the user who created the ${objectName}`),
metadata: import_zod3.z.record(customTypes.any).nullish().describe(`User-controlled metadata about the ${objectName}`)
});
}
var userBaseSchema = generateBaseTableSchema("user");
var userSchema = import_zod3.z.object({
id: userBaseSchema.shape.id,
auth_id: import_zod3.z.string().uuid().nullish().describe("Internal authentication token used to identify the user"),
given_name: import_zod3.z.string().nullish().describe("Given name of the user"),
family_name: import_zod3.z.string().nullish().describe("Family name of the user"),
email: import_zod3.z.string().nullish().describe("The user's email"),
avatar_url: import_zod3.z.string().nullish().describe("URL of the user's Avatar image"),
created: userBaseSchema.shape.created
}).strict().openapi("User");
var organizationBaseSchema = generateBaseTableSchema("organization");
var organizationSchema = import_zod3.z.object({
id: organizationBaseSchema.shape.id,
name: organizationBaseSchema.shape.name.nullish(),
api_url: import_zod3.z.string().nullish(),
created: organizationBaseSchema.shape.created
}).strict().openapi("Organization");
var memberSchema = import_zod3.z.object({
org_id: organizationSchema.shape.id,
user_id: userSchema.shape.id
}).strict().openapi("Member");
var meSchema = import_zod3.z.object({
id: userSchema.shape.id,
// By filtering by auth_id equality, we will ensure this is not-null.
auth_id: userSchema.shape.auth_id.unwrap().unwrap(),
organizations: import_zod3.z.object({
id: memberSchema.shape.org_id,
name: organizationSchema.shape.name
}).array()
}).strict().openapi("Me");
var apiKeyBaseSchema = generateBaseTableSchema("api key");
var apiKeySchema = import_zod3.z.object({
id: apiKeyBaseSchema.shape.id,
created: apiKeyBaseSchema.shape.created,
key_hash: import_zod3.z.string(),
name: apiKeyBaseSchema.shape.name,
preview_name: import_zod3.z.string(),
user_id: userSchema.shape.id.nullish(),
org_id: organizationSchema.shape.id.nullish()
}).strict().openapi("ApiKey");
var projectBaseSchema = generateBaseTableSchema("project");
var projectSchema = import_zod3.z.object({
id: projectBaseSchema.shape.id,
org_id: import_zod3.z.string().uuid().describe(
"Unique id for the organization that the project belongs under"
),
name: projectBaseSchema.shape.name,
created: projectBaseSchema.shape.created,
deleted_at: projectBaseSchema.shape.deleted_at,
user_id: projectBaseSchema.shape.user_id
}).strict().openapi("Project");
var datasetBaseSchema = generateBaseTableSchema("dataset", {
underProject: true
});
var datasetSchema = import_zod3.z.object({
id: datasetBaseSchema.shape.id,
project_id: datasetBaseSchema.shape.project_id.nullish(),
name: datasetBaseSchema.shape.name,
description: datasetBaseSchema.shape.description,
created: datasetBaseSchema.shape.created,
deleted_at: datasetBaseSchema.shape.deleted_at,
user_id: datasetBaseSchema.shape.user_id
}).strict().openapi("Dataset");
var repoInfoSchema = import_zod3.z.object({
commit: import_zod3.z.string().nullish().describe("SHA of most recent commit"),
branch: import_zod3.z.string().nullish().describe("Name of the branch the most recent commit belongs to"),
tag: import_zod3.z.string().nullish().describe("Name of the tag on the most recent commit"),
dirty: import_zod3.z.boolean().nullish().describe(
"Whether or not the repo had uncommitted changes when snapshotted"
),
author_name: import_zod3.z.string().nullish().describe("Name of the author of the most recent commit"),
author_email: import_zod3.z.string().nullish().describe("Email of the author of the most recent commit"),
commit_message: import_zod3.z.string().nullish().describe("Most recent commit message"),
commit_time: import_zod3.z.string().nullish().describe("Time of the most recent commit"),
git_diff: import_zod3.z.string().nullish().describe(
"If the repo was dirty when run, this includes the diff between the current state of the repo and the most recent commit."
)
}).describe(
"Metadata about the state of the repo when the experiment was created"
).openapi("RepoInfo");
var experimentBaseSchema = generateBaseTableSchema("experiment", {
underProject: true
});
var experimentSchema = import_zod3.z.object({
id: experimentBaseSchema.shape.id,
project_id: experimentBaseSchema.shape.project_id,
name: experimentBaseSchema.shape.name,
description: experimentBaseSchema.shape.description,
created: experimentBaseSchema.shape.created,
repo_info: repoInfoSchema.nullish(),
commit: import_zod3.z.string().nullish().describe("Commit, taken directly from `repo_info.commit`"),
base_exp_id: import_zod3.z.string().uuid().nullish().describe(
"Id of default base experiment to compare against when viewing this experiment"
),
deleted_at: experimentBaseSchema.shape.deleted_at,
dataset_id: import_zod3.z.string().uuid().nullish().describe(
"Identifier of the linked dataset, or null if the experiment is not linked to a dataset"
),
dataset_version: import_zod3.z.string().nullish().describe(
"Version number of the linked dataset the experiment was run against. This can be used to reproduce the experiment after the dataset has been modified."
),
public: import_zod3.z.boolean().describe(
"Whether or not the experiment is public. Public experiments can be viewed by anybody inside or outside the organization"
),
user_id: experimentBaseSchema.shape.user_id,
metadata: experimentBaseSchema.shape.metadata
}).strict().openapi("Experiment");
var appLimitSchema = import_zod3.z.number().int().nonnegative().describe("Limit the number of objects to return");
function generateBaseTableOpSchema(objectName) {
return import_zod3.z.object({
org_name: import_zod3.z.string().nullish().describe(
`For nearly all users, this parameter should be unnecessary. But in the rare case that your API key belongs to multiple organizations, you may specify the name of the organization the ${objectName} belongs in.`
)
});
}
var startingAfterSchema = import_zod3.z.string().uuid().describe(
[
"Pagination cursor id.",
"For example, if the final item in the last page you fetched had an id of `foo`, pass `starting_after=foo` to fetch the next page. Note: you may only pass one of `starting_after` and `ending_before`"
].join("\n\n")
).openapi("StartingAfter");
var endingBeforeSchema = import_zod3.z.string().uuid().describe(
[
"Pagination cursor id.",
"For example, if the initial item in the last page you fetched had an id of `foo`, pass `ending_before=foo` to fetch the previous page. Note: you may only pass one of `starting_after` and `ending_before`"
].join("\n\n")
).openapi("EndingBefore");
var createProjectBaseSchema = generateBaseTableOpSchema("project");
var createProjectSchema = import_zod3.z.object({
name: projectSchema.shape.name,
org_name: createProjectBaseSchema.shape.org_name
}).strict().openapi("CreateProject");
var patchProjectSchema = import_zod3.z.object({
name: projectSchema.shape.name.nullish()
}).strict().openapi("PatchProject");
var createExperimentSchema = import_zod3.z.object({
project_id: experimentSchema.shape.project_id,
name: experimentSchema.shape.name.nullish(),
description: experimentSchema.shape.description,
repo_info: experimentSchema.shape.repo_info,
base_exp_id: experimentSchema.shape.base_exp_id,
dataset_id: experimentSchema.shape.dataset_id,
dataset_version: experimentSchema.shape.dataset_version,
public: experimentSchema.shape.public.nullish(),
metadata: experimentSchema.shape.metadata
}).strict().openapi("CreateExperiment");
var patchExperimentSchema = createExperimentSchema.omit({ project_id: true }).strict().openapi("PatchExperiment");
var createDatasetSchema = import_zod3.z.object({
project_id: datasetSchema.shape.project_id,
name: datasetSchema.shape.name,
description: datasetSchema.shape.description
}).strict().openapi("CreateDataset");
var patchDatasetSchema = createDatasetSchema.omit({ project_id: true }).strict().openapi("PatchDataset");
var objectSchemas = {
experiment: {
create: createExperimentSchema,
patch: patchExperimentSchema,
object: experimentSchema
},
dataset: {
create: createDatasetSchema,
patch: patchDatasetSchema,
object: datasetSchema
},
project: {
create: createProjectSchema,
patch: patchProjectSchema,
object: projectSchema
}
};
// src/util.ts
function capitalize(s, sep) {
const items = sep ? s.split(sep) : [s];
return items.map((s2) => s2 ? s2.charAt(0).toUpperCase() + s2.slice(1) : s2).join(sep || "");
}
// src/db_fields.ts
var TRANSACTION_ID_FIELD = "_xact_id";
var OBJECT_DELETE_FIELD = "_object_delete";
var IS_MERGE_FIELD = "_is_merge";
var MERGE_PATHS_FIELD = "_merge_paths";
var VALID_SOURCES = ["app", "api", "external"];
var PARENT_ID_FIELD = "_parent_id";
// src/span_types.ts
var SpanTypeAttribute = /* @__PURE__ */ ((SpanTypeAttribute2) => {
SpanTypeAttribute2["LLM"] = "llm";
SpanTypeAttribute2["SCORE"] = "score";
SpanTypeAttribute2["FUNCTION"] = "function";
SpanTypeAttribute2["EVAL"] = "eval";
SpanTypeAttribute2["TASK"] = "task";
SpanTypeAttribute2["TOOL"] = "tool";
return SpanTypeAttribute2;
})(SpanTypeAttribute || {});
// src/typespecs/api_types.ts
(0, import_zod_to_openapi3.extendZodWithOpenApi)(import_zod4.z);
var auditSourcesSchema = import_zod4.z.enum(VALID_SOURCES);
function generateBaseEventOpSchema(objectType) {
const eventDescription = getEventObjectDescription(objectType);
return import_zod4.z.object({
id: import_zod4.z.string().describe(
`A unique identifier for the ${eventDescription} event. If you don't provide one, BrainTrust will generate one for you`
),
[TRANSACTION_ID_FIELD]: import_zod4.z.bigint().describe(
`The transaction id of an event is unique to the network operation that processed the event insertion. Transaction ids are monotonically increasing over time and can be used to retrieve a versioned snapshot of the ${eventDescription} (see the \`version\` parameter)`
),
created: datetimeStringSchema.nullish().describe(`The timestamp the ${eventDescription} event was created`),
input: customTypes.any,
output: customTypes.any,
expected: customTypes.any,
scores: import_zod4.z.record(import_zod4.z.number().min(0).max(1).nullish()).nullish(),
metadata: import_zod4.z.record(customTypes.any).nullish().describe(
"A dictionary with additional data about the test example, model outputs, or just about anything else that's relevant, that you can use to help find and analyze examples later. For example, you could log the `prompt`, example's `id`, or anything else that would be useful to slice/dice later. The values in `metadata` can be any JSON-serializable type, but its keys must be strings"
),
metrics: import_zod4.z.object({
start: import_zod4.z.number().nullish().describe(
`A unix timestamp recording when the section of code which produced the ${eventDescription} event started`
),
end: import_zod4.z.number().nullish().describe(
`A unix timestamp recording when the section of code which produced the ${eventDescription} event finished`
)
}).catchall(customTypes.any).nullish().describe(
`Metrics are numerical measurements tracking the execution of the code that produced the ${eventDescription} event. Use "start" and "end" to track the time span over which the ${eventDescription} event was produced`
),
context: import_zod4.z.object({
caller_functionname: import_zod4.z.string().nullish().describe(
`The function in code which created the ${eventDescription} event`
),
caller_filename: import_zod4.z.string().nullish().describe(
`Name of the file in code where the ${eventDescription} event was created`
),
caller_lineno: import_zod4.z.number().int().nullish().describe(
`Line of code where the ${eventDescription} event was created`
)
}).catchall(customTypes.any).nullish().describe(
`Context is additional information about the code that produced the ${eventDescription} event. It is essentially the textual counterpart to \`metrics\`. Use the \`caller_*\` attributes to track the location in code which produced the ${eventDescription} event`
),
span_id: import_zod4.z.string().describe(
`A unique identifier used to link different ${eventDescription} events together as part of a full trace. See the [tracing guide](https://www.braintrustdata.com/docs/guides/tracing) for full details on tracing`
),
span_parents: import_zod4.z.string().array().nullish().describe(
`An array of the parent \`span_ids\` of this ${eventDescription} event. This should be empty for the root span of a trace, and should most often contain just one parent element for subspans`
),
root_span_id: import_zod4.z.string().describe(
`The \`span_id\` of the root of the trace this ${eventDescription} event belongs to`
),
span_attributes: import_zod4.z.object({
name: import_zod4.z.string().nullish().describe("Name of the span, for display purposes only"),
type: import_zod4.z.nativeEnum(SpanTypeAttribute).nullish().describe("Type of the span, for display purposes only")
}).catchall(customTypes.any).nullish().describe(
"Human-identifying attributes of the span, such as name, type, etc."
),
[OBJECT_DELETE_FIELD]: import_zod4.z.boolean().nullish().describe(
`Pass \`${OBJECT_DELETE_FIELD}=true\` to mark the ${eventDescription} event deleted. Deleted events will not show up in subsequent fetches for this ${eventDescription}`
)
});
}
function generateBaseEventFeedbackSchema(objectType) {
const eventObjectType = getEventObjectType(objectType);
const eventDescription = getEventObjectDescription(objectType);
return import_zod4.z.object({
id: import_zod4.z.string().describe(
`The id of the ${eventDescription} event to log feedback for. This is the row \`id\` returned by \`POST /v1/${eventObjectType}/{${objectType}_id}/insert\``
),
scores: import_zod4.z.record(import_zod4.z.number().min(0).max(1).nullish()).nullish().describe(
`A dictionary of numeric values (between 0 and 1) to log. These scores will be merged into the existing scores for the ${eventDescription} event`
),
expected: customTypes.any.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not"
),
comment: import_zod4.z.string().nullish().describe(
`An optional comment string to log about the ${eventDescription} event`
),
metadata: import_zod4.z.record(customTypes.any).nullish().describe(
"A dictionary with additional data about the feedback. If you have a `user_id`, you can log it here and access it in the Braintrust UI."
),
source: auditSourcesSchema.nullish().describe(
'The source of the feedback. Must be one of "external" (default), "app", or "api"'
)
});
}
var fetchLimitSchema = import_zod4.z.number().int().nonnegative().describe(
[
"limit the number of traces fetched",
`Fetch queries may be paginated if the total result size is expected to be large (e.g. project_logs which accumulate over a long time). Note that fetch queries only support pagination in descending time order (from latest to earliest \`${TRANSACTION_ID_FIELD}\`. Furthermore, later pages may return rows which showed up in earlier pages, except with an earlier \`${TRANSACTION_ID_FIELD}\`. This happens because pagination occurs over the whole version history of the event log. You will most likely want to exclude any such duplicate, outdated rows (by \`id\`) from your combined result set.`,
`The \`limit\` parameter controls the number of full traces to return. So you may end up with more individual rows than the specified limit if you are fetching events containing traces.`
].join("\n\n")
);
var maxXactIdSchema = import_zod4.z.bigint().describe(
[
"Pagination cursor transaction ID, combined with `max_root_span_id`",
`Given a previous fetch with a list of rows, you can determine \`max_xact_id\` as the maximum of the \`${TRANSACTION_ID_FIELD}\` field over all rows. See the documentation for \`limit\` for an overview of paginating fetch queries.`
].join("\n\n")
);
var maxRootSpanIdSchema = import_zod4.z.string().describe(
[
"Pagination cursor transaction root span ID, combined with `max_xact_id`",
`Given a previous fetch with a list of rows, you can determine \`max_root_span_id\` as the maximum of the \`root_span_id\` field over all rows. See the documentation for \`limit\` for an overview of paginating fetch queries.`
].join("\n\n")
);
var versionSchema = import_zod4.z.bigint().describe(
[
"Retrieve a snapshot of events from a past time",
"The version id is essentially a filter on the latest event transaction id. You can use the `max_xact_id` returned by a past fetch as the version to reproduce that exact fetch."
].join("\n\n")
);
var pathTypeFilterSchema = import_zod4.z.object({
type: import_zod4.z.literal("path_lookup").describe("Denotes the type of filter as a path-lookup filter"),
path: import_zod4.z.string().array().describe(
'List of fields describing the path to the value to be checked against. For instance, if you wish to filter on the value of `c` in `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `path=["input", "a", "b", "c"]`'
),
value: customTypes.any.describe(
'The value to compare equality-wise against the event value at the specified `path`. The value must be a "primitive", that is, any JSON-serializable object except for objects and arrays. For instance, if you wish to filter on the value of "input.a.b.c" in the object `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `value="hello"`'
)
}).describe(
'A path-lookup filter describes an equality comparison against a specific sub-field in the event row. For instance, if you wish to filter on the value of `c` in `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `path=["input", "a", "b", "c"]` and `value="hello"`'
).openapi("PathLookupFilter");
var sqlTypeFilterSchema = import_zod4.z.object({
type: import_zod4.z.literal("sql_filter").describe("Denotes the type of filter as a sql-type filter"),
expr: import_zod4.z.string().describe(
`A SQL expression in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). For instance, if you wish to fuzzy-match the value of \`c\` in \`{"input": {"a": {"b": {"c": "hello"}}}}\`, pass \`expr="input->'a'->'b'->>'c' LIKE '%el%'"\`.`
)
}).describe(
`A sql-type filter describes a general filter over an individual row in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). For instance, if you wish to fuzzy-match the value of \`c\` in \`{"input": {"a": {"b": {"c": "hello"}}}}\`, pass \`expr="input->'a'->'b'->>'c' LIKE '%el%'"\`.`
).openapi("SQLFilter");
var allFetchFiltersSchema = import_zod4.z.union([pathTypeFilterSchema, sqlTypeFilterSchema]).array().describe(
"A list of filters on the events to fetch. Filters can either be specialized `path=value` expressions or general SQL expressions in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). When possible, prefer path-lookup type filters over general SQL-type filters, as they are likely to activate indices in the DB and run faster"
).openapi("AllFetchEventsFilters");
var fetchFiltersSchema = pathTypeFilterSchema.array().describe(
"A list of filters on the events to fetch. Currently, only path-lookup type filters are supported, but we may add more in the future"
).openapi("FetchEventsFilters");
var fetchEventsRequestSchema = import_zod4.z.object({
limit: fetchLimitSchema.nullish(),
max_xact_id: maxXactIdSchema.nullish(),
max_root_span_id: maxRootSpanIdSchema.nullish(),
filters: fetchFiltersSchema.nullish(),
version: versionSchema.nullish()
}).strict().openapi("FetchEventsRequest");
function makeFetchEventsResponseSchema(objectType, eventSchema) {
const eventName = capitalize(getEventObjectType(objectType), "_").replace(
"_",
""
);
return import_zod4.z.object({
events: eventSchema.array().describe("A list of fetched events")
}).strict().openapi(`Fetch${eventName}EventsResponse`);
}
var experimentEventBaseSchema = generateBaseEventOpSchema("experiment");
var experimentEventSchema = import_zod4.z.object({
id: experimentEventBaseSchema.shape.id,
dataset_record_id: import_zod4.z.string().nullish().describe(
"If the experiment is associated to a dataset, this is the event-level dataset id this experiment event is tied to"
),
[TRANSACTION_ID_FIELD]: experimentEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: experimentEventBaseSchema.shape.created,
project_id: experimentSchema.shape.project_id,
experiment_id: experimentSchema.shape.id,
input: experimentEventBaseSchema.shape.input.describe(
"The arguments that uniquely define a test case (an arbitrary, JSON serializable object). Later on, Braintrust will use the `input` to know whether two test cases are the same between experiments, so they should not contain experiment-specific state. A simple rule of thumb is that if you run the same experiment twice, the `input` should be identical"
),
output: experimentEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object), that allows you to determine whether the result is correct or not. For example, in an app that generates SQL queries, the `output` should be the _result_ of the SQL query generated by the model, not the query itself, because there may be multiple valid queries that answer a single question"
),
expected: experimentEventBaseSchema.shape.expected.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not. Braintrust currently does not compare `output` to `expected` for you, since there are so many different ways to do that correctly. Instead, these values are just used to help you navigate your experiments while digging into analyses. However, we may later use these values to re-score outputs or fine-tune your models"
),
scores: experimentEventBaseSchema.shape.scores.describe(
"A dictionary of numeric values (between 0 and 1) to log. The scores should give you a variety of signals that help you determine how accurate the outputs are compared to what you expect and diagnose failures. For example, a summarization app might have one score that tells you how accurate the summary is, and another that measures the word similarity between the generated and grouth truth summary. The word similarity score could help you determine whether the summarization was covering similar concepts or not. You can use these scores to help you sort, filter, and compare experiments"
),
metadata: experimentEventBaseSchema.shape.metadata,
metrics: experimentEventBaseSchema.shape.metrics,
context: experimentEventBaseSchema.shape.context,
span_id: experimentEventBaseSchema.shape.span_id,
span_parents: experimentEventBaseSchema.shape.span_parents,
root_span_id: experimentEventBaseSchema.shape.root_span_id,
span_attributes: experimentEventBaseSchema.shape.span_attributes
}).strict().openapi("ExperimentEvent");
var datasetEventBaseSchema = generateBaseEventOpSchema("dataset");
var datasetEventSchema = import_zod4.z.object({
id: datasetEventBaseSchema.shape.id,
[TRANSACTION_ID_FIELD]: datasetEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: datasetEventBaseSchema.shape.created,
project_id: datasetSchema.shape.project_id,
dataset_id: datasetSchema.shape.id,
input: datasetEventBaseSchema.shape.input.describe(
"The argument that uniquely define an input case (an arbitrary, JSON serializable object)"
),
output: datasetEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object)"
),
metadata: datasetEventBaseSchema.shape.metadata,
span_id: datasetEventBaseSchema.shape.span_id,
root_span_id: datasetEventBaseSchema.shape.root_span_id
}).strict().openapi("DatasetEvent");
var projectLogsEventBaseSchema = generateBaseEventOpSchema("project");
var projectLogsEventSchema = import_zod4.z.object({
id: projectLogsEventBaseSchema.shape.id,
[TRANSACTION_ID_FIELD]: projectLogsEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: projectLogsEventBaseSchema.shape.created,
org_id: projectSchema.shape.org_id,
project_id: projectSchema.shape.id,
log_id: import_zod4.z.literal("g").describe("A literal 'g' which identifies the log as a project log"),
input: projectLogsEventBaseSchema.shape.input.describe(
"The arguments that uniquely define a user input(an arbitrary, JSON serializable object)."
),
output: projectLogsEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object), that allows you to determine whether the result is correct or not. For example, in an app that generates SQL queries, the `output` should be the _result_ of the SQL query generated by the model, not the query itself, because there may be multiple valid queries that answer a single question."
),
expected: projectLogsEventBaseSchema.shape.expected.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not. Braintrust currently does not compare `output` to `expected` for you, since there are so many different ways to do that correctly. Instead, these values are just used to help you navigate while digging into analyses. However, we may later use these values to re-score outputs or fine-tune your models."
),
scores: projectLogsEventBaseSchema.shape.scores.describe(
"A dictionary of numeric values (between 0 and 1) to log. The scores should give you a variety of signals that help you determine how accurate the outputs are compared to what you expect and diagnose failures. For example, a summarization app might have one score that tells you how accurate the summary is, and another that measures the word similarity between the generated and grouth truth summary. The word similarity score could help you determine whether the summarization was covering similar concepts or not. You can use these scores to help you sort, filter, and compare logs."
),
metadata: projectLogsEventBaseSchema.shape.metadata,
metrics: projectLogsEventBaseSchema.shape.metrics,
context: projectLogsEventBaseSchema.shape.context,
span_id: projectLogsEventBaseSchema.shape.span_id,
span_parents: projectLogsEventBaseSchema.shape.span_parents,
root_span_id: projectLogsEventBaseSchema.shape.root_span_id,
span_attributes: projectLogsEventBaseSchema.shape.span_attributes
}).strict().openapi("ProjectLogsEvent");
var isMergeDescription = [
"The `_is_merge` field controls how the row is merged with any existing row with the same id in the DB. By default (or when set to `false`), the existing row is completely replaced by the new row. When set to `true`, the new row is deep-merged into the existing row",
'For example, say there is an existing row in the DB `{"id": "foo", "input": {"a": 5, "b": 10}}`. If we merge a new row as `{"_is_merge": true, "id": "foo", "input": {"b": 11, "c": 20}}`, the new row will be `{"id": "foo", "input": {"a": 5, "b": 11, "c": 20}}`. If we replace the new row as `{"id": "foo", "input": {"b": 11, "c": 20}}`, the new row will be `{"id": "foo", "input": {"b": 11, "c": 20}}`'
].join("\n\n");
var mergeEventSchema = import_zod4.z.object({
[IS_MERGE_FIELD]: customTypes.literalTrue.describe(isMergeDescription),
[MERGE_PATHS_FIELD]: import_zod4.z.string().array().array().nullish().describe(
[
"The `_merge_paths` field allows controlling the depth of the merge. It can only be specified alongside `_is_merge=true`. `_merge_paths` is a list of paths, where each path is a list of field names. The deep merge will not descend below any of the specified merge paths.",
'For example, say there is an existing row in the DB `{"id": "foo", "input": {"a": {"b": 10}, "c": {"d": 20}}, "output": {"a": 20}}`. If we merge a new row as `{"_is_merge": true, "_merge_paths": [["input", "a"], ["output"]], "input": {"a": {"q": 30}, "c": {"e": 30}, "bar": "baz"}, "output": {"d": 40}}`, the new row will be `{"id": "foo": "input": {"a": {"q": 30}, "c": {"d": 20, "e": 30}, "bar": "baz"}, "output": {"d": 40}}`. In this case, due to the merge paths, we have replaced `input.a` and `output`, but have still deep-merged `input` and `input.c`.'
].join("\n\n")
)
});
var replacementEventSchema = import_zod4.z.object({
[IS_MERGE_FIELD]: customTypes.literalFalse.nullish().describe(isMergeDescription),
[PARENT_ID_FIELD]: import_zod4.z.string().nullish().describe(
[
"Use the `_parent_id` field to create this row as a subspan of an existing row. It cannot be specified alongside `_is_merge=true`. Tracking hierarchical relationships are important for tracing (see the [guide](https://www.braintrustdata.com/docs/guides/tracing) for full details).",
'For example, say we have logged a row `{"id": "abc", "input": "foo", "output": "bar", "expected": "boo", "scores": {"correctness": 0.33}}`. We can create a sub-span of the parent row by logging `{"_parent_id": "abc", "id": "llm_call", "input": {"prompt": "What comes after foo?"}, "output": "bar", "metrics": {"tokens": 1}}`. In the webapp, only the root span row `"abc"` will show up in the summary view. You can view the full trace hierarchy (in this case, the `"llm_call"` row) by clicking on the "abc" row.'
].join("\n\n")
)
});
function makeInsertEventSchemas(objectType, insertSchema) {
const eventDescription = getEventObjectDescription(objectType);
const article = getEventObjectArticle(objectType);
const eventSchemaName = capitalize(
getEventObjectType(objectType),
"_"
).replace("_", "");
const replaceVariantSchema = insertSchema.merge(replacementEventSchema).strict().openapi(`Insert${eventSchemaName}EventReplace`);
const mergeVariantSchema = insertSchema.merge(mergeEventSchema).strict().openapi(`Insert${eventSchemaName}EventMerge`);
const eventSchema = import_zod4.z.union([replaceVariantSchema, mergeVariantSchema]).describe(
`${capitalize(article)} ${eventDescription} event`
).openapi(`Insert${eventSchemaName}Event`);
const requestSchema = import_zod4.z.object({
events: eventSchema.array().describe(`A list of ${eventDescription} events to insert`)
}).strict().openapi(`Insert${eventSchemaName}EventRequest`);
return { eventSchema, requestSchema };
}
var insertEventsResponseSchema = import_zod4.z.object({
row_ids: import_zod4.z.string().array().describe(
"The ids of all rows that were inserted, aligning one-to-one with the rows provided as input"
)
}).strict().openapi("InsertEventsResponse");
var {
eventSchema: insertExperimentEventSchema,
requestSchema: insertExperimentEventsRequestSchema
} = makeInsertEventSchemas(
"experiment",
import_zod4.z.object({
input: experimentEventSchema.shape.input,
output: experimentEventSchema.shape.output,
expected: experimentEventSchema.shape.expected,
scores: experimentEventSchema.shape.scores,
metadata: experimentEventSchema.shape.metadata,
metrics: experimentEventSchema.shape.metrics,
context: experimentEventSchema.shape.context,
span_attributes: experimentEventSchema.shape.span_attributes,
id: experimentEventSchema.shape.id.nullish(),
dataset_record_id: experimentEventSchema.shape.dataset_record_id,
[OBJECT_DELETE_FIELD]: experimentEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
var {
eventSchema: insertDatasetEventSchema,
requestSchema: insertDatasetEventsRequestSchema
} = makeInsertEventSchemas(
"dataset",
import_zod4.z.object({
input: datasetEventSchema.shape.input,
output: datasetEventSchema.shape.output,
metadata: datasetEventSchema.shape.metadata,
id: datasetEventSchema.shape.id.nullish(),
[OBJECT_DELETE_FIELD]: datasetEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
var {
eventSchema: insertProjectLogsEventSchema,
requestSchema: insertProjectLogsEventsRequestSchema
} = makeInsertEventSchemas(
"project",
import_zod4.z.object({
input: projectLogsEventSchema.shape.input,
output: projectLogsEventSchema.shape.output,
expected: projectLogsEventSchema.shape.expected,
scores: projectLogsEventSchema.shape.scores,
metadata: projectLogsEventSchema.shape.metadata,
metrics: projectLogsEventSchema.shape.metrics,
context: projectLogsEventSchema.shape.context,
span_attributes: projectLogsEventSchema.shape.span_attributes,
id: projectLogsEventSchema.shape.id.nullish(),
[OBJECT_DELETE_FIELD]: projectLogsEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
function makeFeedbackRequestSchema(objectType, feedbackSchema) {
const eventDescription = getEventObjectDescription(objectType);
const eventSchemaName = capitalize(
getEventObjectType(objectType),
"_"
).replace("_", "");
return import_zod4.z.object({
feedback: feedbackSchema.array().describe(`A list of ${eventDescription} feedback items`)
}).strict().openapi(`Feedback${eventSchemaName}EventRequest`);
}
var feedbackExperimentRequestBaseSchema = generateBaseEventFeedbackSchema("experiment");
var feedbackExperimentItemSchema = import_zod4.z.object({
id: feedbackExperimentRequestBaseSchema.shape.id,
scores: feedbackExperimentRequestBaseSchema.shape.scores,
expected: feedbackExperimentRequestBaseSchema.shape.expected,
comment: feedbackExperimentRequestBaseSchema.shape.comment,
metadata: feedbackExperimentRequestBaseSchema.shape.metadata,
source: feedbackExperimentRequestBaseSchema.shape.source
}).strict().openapi("FeedbackExperimentItem");
var feedbackExperimentRequestSchema = makeFeedbackRequestSchema(
"experiment",
feedbackExperimentItemSchema
);
var feedbackDatasetRequestBaseSchema = generateBaseEventFeedbackSchema("dataset");
var feedbackDatasetItemSchema = import_zod4.z.object({
id: feedbackDatasetRequestBaseSchema.shape.id,
comment: feedbackDatasetRequestBaseSchema.shape.comment,
metadata: feedbackDatasetRequestBaseSchema.shape.metadata,
source: feedbackDatasetRequestBaseSchema.shape.source
}).strict().openapi("FeedbackDatasetItem");
var feedbackDatasetRequestSchema = makeFeedbackRequestSchema(
"dataset",
feedbackDatasetItemSchema
);
var feedbackProjectLogsRequestBaseSchema = generateBaseEventFeedbackSchema("project");
var feedbackProjectLogsItemSchema = import_zod4.z.object({
id: feedbackProjectLogsRequestBaseSchema.shape.id,
scores: feedbackProjectLogsRequestBaseSchema.shape.scores,
expected: feedbackProjectLogsRequestBaseSchema.shape.expected,
comment: feedbackProjectLogsRequestBaseSchema.shape.comment,
metadata: feedbackProjectLogsRequestBaseSchema.shape.metadata,
source: feedbackProjectLogsRequestBaseSchema.shape.source
}).strict().openapi("FeedbackProjectLogsItem");
var feedbackProjectLogsRequestSchema = makeFeedbackRequestSchema(
"project",
feedbackProjectLogsItemSchema
);
var eventObjectSchemas = {
experiment: {
fetchResponse: makeFetchEventsResponseSchema(
"experiment",
experimentEventSchema
),
insertEvent: insertExperimentEventSchema,
insertRequest: insertExperimentEventsRequestSchema,
feedbackItem: feedbackExperimentItemSchema,
feedbackRequest: feedbackExperimentRequestSchema
},
dataset: {
fetchResponse: makeFetchEventsResponseSchema("dataset", datasetEventSchema),
insertEvent: insertDatasetEventSchema,
insertRequest: insertDatasetEventsRequestSchema,
feedbackItem: feedbackDatasetItemSchema,
feedbackRequest: feedbackDatasetRequestSchema
},
project_logs: {
fetchResponse: makeFetchEventsResponseSchema(
"project",
projectLogsEventSchema
),
insertEvent: insertProjectLogsEventSchema,
insertRequest: insertProjectLogsEventsRequestSchema,
feedbackItem: feedbackProjectLogsItemSchema,
feedbackRequest: feedbackProjectLogsRequestSchema
}
};
function makeCrossObjectIndividualRequestSchema(objectType) {
const eventObjectType = getEventObjectType(objectType);
const eventDescription = getEventObjectDescription(objectType);
const eventObjectSchema = eventObjectSchemas[eventObjectType];
const insertObject = import_zod4.z.object({
events: eventObjectSchema.insertEvent.array().nullish().describe(`A list of ${eventDescription} events to insert`),
feedback: eventObjectSchema.feedbackItem.array().nullish().describe(`A list of ${eventDescription} feedback items`)
}).strict();
return import_zod4.z.record(import_zod4.z.string().uuid(), insertObject).nullish().describe(`A mapping from ${objectType} id to a set of log events and feedback items to insert`);
}
function makeCrossObjectIndividualResponseSchema(objectType) {
return import_zod4.z.record(import_zod4.z.string().uuid(), insertEventsResponseSchema).nullish().describe(`A mapping from ${objectType} id to row ids for inserted \`events\``);
}
var crossObjectInsertRequestSchema = import_zod4.z.object({
experiment: makeCrossObjectIndividualRequestSchema("experiment"),
dataset: makeCrossObjectIndividualRequestSchema("dataset"),
project_logs: makeCrossObjectIndividualRequestSchema("project")
}).strict().openapi("CrossObjectInsertRequest");
var crossObjectInsertResponseSchema = import_zod4.z.object({
experiment: makeCrossObjectIndividualResponseSchema("experiment"),
dataset: makeCrossObjectIndividualResponseSchema("dataset"),
project_logs: makeCrossObjectIndividualResponseSchema("project")
}).strict().openapi("CrossObjectInsertResponse");
var summarizeScoresParamSchema = import_zod4.z.boolean().describe("Whether to summarize the scores and metrics. If false (or omitted), only the metadata will be returned.");
var comparisonExperimentIdParamSchema = import_zod4.z.string().uuid().describe("The experiment to compare against, if summarizing scores and metrics. If omitted, will fall back to the `base_exp_id` stored in the experiment metadata, and then to the most recent experiment run in the same project. Must pass `summarize_scores=true` for this id to be used");
var summarizeDataParamSchema = import_zod4.z.boolean().describe("Whether to summarize the data. If false (or omitted), only the metadata will be returned.");
var summarizeExperimentResponseSchema = import_zod4.z.object({
project_name: import_zod4.z.string().describe("Name of the project that the experiment belongs to"),
experiment_name: import_zod4.z.string().describe("Name of the experiment"),
project_url: import_zod4.z.string().url().describe("URL to the project's page in the Braintrust app"),
experiment_url: import_zod4.z.string().url().describe("URL to the experiment's page in the Braintrust app"),
comparison_experiment_name: import_zod4.z.string().nullish().describe("The experiment which scores are baselined against"),
scores: import_zod4.z.record(import_zod4.z.object({
name: import_zod4.z.string().describe("Name of the score"),
score: import_zod4.z.number().min(0).max(1).describe("Average score across all examples"),
diff: import_zod4.z.number().min(-1).max(1).describe("Difference in score between the current and comparison experiment"),
improvements: import_zod4.z.number().int().min(0).describe("Number of improvements in the score"),
regressions: import_zod4.z.number().int().min(0).describe("Number of regressions in the score")
}).describe("Summary of a score's performance").openapi("ScoreSummary")).nullish().describe("Summary of the experiment's scores"),
metrics: import_zod4.z.record(import_zod4.z.object({
name: import_zod4.z.string().describe("Name of the metric"),
metric: import_zod4.z.number().describe("Average metric across all examples"),
unit: import_zod4.z.string().describe("Unit label for the metric"),
diff: import_zod4.z.number().describe("Difference in metric between the current and comparison experiment"),
improvements: import_zod4.z.number().int().min(0).describe("Number of improvements in the metric"),
regressions: import_zod4.z.number().int().min(0).describe("Number of regressions in the metric")
}).describe("Summary of a metric's performance").openapi("MetricSummary")).nullish().describe("Summary of the experiment's metrics")
}).strict().describe("Summary of an experiment").openapi("SummarizeExperimentResponse");
var summarizeDatasetResponseSchema = import_zod4.z.object({
project_name: import_zod4.z.string().describe("Name of the project that the dataset belongs to"),
dataset_name: import_zod4.z.string().describe("Name of the dataset"),
project_url: import_zod4.z.string().url().describe("URL to the project's page in the Braintrust app"),
dataset_url: import_zod4.z.string().url().describe("URL to the dataset's page in the Braintrust app"),
data_summary: import_zod4.z.object({
total_records: import_zod4.z.number().int().min(0).describe("Total number of records in the dataset")
}).nullish().describe("Summary of a dataset's data").openapi("DataSummary")
}).strict().describe("Summary of a dataset").openapi("SummarizeDatasetResponse");
var objectTypeSummarizeResponseSchemas = {
experiment: summarizeExperimentResponseSchema,
dataset: summarizeDatasetResponseSchema,
project: void 0
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
allFetchFiltersSchema,
apiKeySchema,
appLimitSchema,
auditSourcesSchema,
comparisonExperimentIdParamSchema,
crossObjectInsertRequestSchema,
crossObjectInsertResponseSchema,
datasetSchema,
datetimeStringSchema,
endingBeforeSchema,
eventObjectSchemas,
experimentSchema,
fetchEventsRequestSchema,
fetchFiltersSchema,
fetchLimitSchema,
getEventObjectArticle,
getEventObjectDescription,
getEventObjectType,
insertEventsResponseSchema,
jsonSchema,
literalSchema,
maxRootSpanIdSchema,
maxXactIdSchema,
meSchema,
memberSchema,
objectSchemas,
objectTypeSummarizeResponseSchemas,
objectTypes,
organizationSchema,
projectSchema,
startingAfterSchema,
summarizeDataParamSchema,
summarizeScoresParamSchema,
userSchema,
versionSchema
});
// src/typespecs/api_types.ts
import { extendZodWithOpenApi as extendZodWithOpenApi3 } from "@asteasolutions/zod-to-openapi";
import { z as z4 } from "zod";
// src/typespecs/app_types.ts
import { extendZodWithOpenApi as extendZodWithOpenApi2 } from "@asteasolutions/zod-to-openapi";
import { z as z3 } from "zod";
// src/typespecs/common_types.ts
import { z } from "zod";
var literalSchema = z.union([
z.string(),
z.number(),
z.boolean(),
z.null()
]);
var jsonSchema = z.lazy(
() => z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
);
var datetimeStringSchema = z.string().datetime({ offset: true });
var objectTypes = ["project", "experiment", "dataset"];
function getEventObjectType(objectType) {
return objectType === "project" ? "project_logs" : objectType;
}
function getEventObjectDescription(objectType) {
return getEventObjectType(objectType).replace("_", " ");
}
function getEventObjectArticle(objectType) {
return objectType === "experiment" ? "an" : "a";
}
// src/typespecs/custom_types.ts
import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
import { z as z2 } from "zod";
extendZodWithOpenApi(z2);
var modeSchema = z2.enum(["default", "stainless"]);
var mode = modeSchema.parse(
typeof process === "undefined" ? "default" : "stainless"
);
var modeToTypes = {
default: {
any: z2.any(),
literalTrue: z2.literal(true),
literalFalse: z2.literal(false)
},
stainless: {
// Stainless requires schemas which are completely permissive to be
// tagged.
any: z2.any().openapi({ ["x-stainless-any"]: true }),
// Stainless does not support boolean literals in all SDKs.
literalTrue: z2.boolean(),
literalFalse: z2.boolean()
}
};
var customTypes = modeToTypes[mode];
// src/typespecs/app_types.ts
extendZodWithOpenApi2(z3);
function generateBaseTableSchema(objectName, opts) {
let nameDescription = `Name of the ${objectName}`;
if (opts == null ? void 0 : opts.underProject) {
nameDescription += `. Within a project, ${objectName} names are unique`;
}
return z3.object({
id: z3.string().uuid().describe(`Unique identifier for the ${objectName}`),
project_id: z3.string().uuid().describe(
`Unique identifier for the project that the ${objectName} belongs under`
),
name: z3.string().describe(nameDescription),
description: z3.string().nullish().describe(`Textual description of the ${objectName}`),
created: datetimeStringSchema.nullish().describe(`Date of ${objectName} creation`),
deleted_at: datetimeStringSchema.nullish().describe(
`Date of ${objectName} deletion, or null if the ${objectName} is still active`
),
user_id: z3.string().uuid().nullish().describe(`Identifies the user who created the ${objectName}`),
metadata: z3.record(customTypes.any).nullish().describe(`User-controlled metadata about the ${objectName}`)
});
}
var userBaseSchema = generateBaseTableSchema("user");
var userSchema = z3.object({
id: userBaseSchema.shape.id,
auth_id: z3.string().uuid().nullish().describe("Internal authentication token used to identify the user"),
given_name: z3.string().nullish().describe("Given name of the user"),
family_name: z3.string().nullish().describe("Family name of the user"),
email: z3.string().nullish().describe("The user's email"),
avatar_url: z3.string().nullish().describe("URL of the user's Avatar image"),
created: userBaseSchema.shape.created
}).strict().openapi("User");
var organizationBaseSchema = generateBaseTableSchema("organization");
var organizationSchema = z3.object({
id: organizationBaseSchema.shape.id,
name: organizationBaseSchema.shape.name.nullish(),
api_url: z3.string().nullish(),
created: organizationBaseSchema.shape.created
}).strict().openapi("Organization");
var memberSchema = z3.object({
org_id: organizationSchema.shape.id,
user_id: userSchema.shape.id
}).strict().openapi("Member");
var meSchema = z3.object({
id: userSchema.shape.id,
// By filtering by auth_id equality, we will ensure this is not-null.
auth_id: userSchema.shape.auth_id.unwrap().unwrap(),
organizations: z3.object({
id: memberSchema.shape.org_id,
name: organizationSchema.shape.name
}).array()
}).strict().openapi("Me");
var apiKeyBaseSchema = generateBaseTableSchema("api key");
var apiKeySchema = z3.object({
id: apiKeyBaseSchema.shape.id,
created: apiKeyBaseSchema.shape.created,
key_hash: z3.string(),
name: apiKeyBaseSchema.shape.name,
preview_name: z3.string(),
user_id: userSchema.shape.id.nullish(),
org_id: organizationSchema.shape.id.nullish()
}).strict().openapi("ApiKey");
var projectBaseSchema = generateBaseTableSchema("project");
var projectSchema = z3.object({
id: projectBaseSchema.shape.id,
org_id: z3.string().uuid().describe(
"Unique id for the organization that the project belongs under"
),
name: projectBaseSchema.shape.name,
created: projectBaseSchema.shape.created,
deleted_at: projectBaseSchema.shape.deleted_at,
user_id: projectBaseSchema.shape.user_id
}).strict().openapi("Project");
var datasetBaseSchema = generateBaseTableSchema("dataset", {
underProject: true
});
var datasetSchema = z3.object({
id: datasetBaseSchema.shape.id,
project_id: datasetBaseSchema.shape.project_id.nullish(),
name: datasetBaseSchema.shape.name,
description: datasetBaseSchema.shape.description,
created: datasetBaseSchema.shape.created,
deleted_at: datasetBaseSchema.shape.deleted_at,
user_id: datasetBaseSchema.shape.user_id
}).strict().openapi("Dataset");
var repoInfoSchema = z3.object({
commit: z3.string().nullish().describe("SHA of most recent commit"),
branch: z3.string().nullish().describe("Name of the branch the most recent commit belongs to"),
tag: z3.string().nullish().describe("Name of the tag on the most recent commit"),
dirty: z3.boolean().nullish().describe(
"Whether or not the repo had uncommitted changes when snapshotted"
),
author_name: z3.string().nullish().describe("Name of the author of the most recent commit"),
author_email: z3.string().nullish().describe("Email of the author of the most recent commit"),
commit_message: z3.string().nullish().describe("Most recent commit message"),
commit_time: z3.string().nullish().describe("Time of the most recent commit"),
git_diff: z3.string().nullish().describe(
"If the repo was dirty when run, this includes the diff between the current state of the repo and the most recent commit."
)
}).describe(
"Metadata about the state of the repo when the experiment was created"
).openapi("RepoInfo");
var experimentBaseSchema = generateBaseTableSchema("experiment", {
underProject: true
});
var experimentSchema = z3.object({
id: experimentBaseSchema.shape.id,
project_id: experimentBaseSchema.shape.project_id,
name: experimentBaseSchema.shape.name,
description: experimentBaseSchema.shape.description,
created: experimentBaseSchema.shape.created,
repo_info: repoInfoSchema.nullish(),
commit: z3.string().nullish().describe("Commit, taken directly from `repo_info.commit`"),
base_exp_id: z3.string().uuid().nullish().describe(
"Id of default base experiment to compare against when viewing this experiment"
),
deleted_at: experimentBaseSchema.shape.deleted_at,
dataset_id: z3.string().uuid().nullish().describe(
"Identifier of the linked dataset, or null if the experiment is not linked to a dataset"
),
dataset_version: z3.string().nullish().describe(
"Version number of the linked dataset the experiment was run against. This can be used to reproduce the experiment after the dataset has been modified."
),
public: z3.boolean().describe(
"Whether or not the experiment is public. Public experiments can be viewed by anybody inside or outside the organization"
),
user_id: experimentBaseSchema.shape.user_id,
metadata: experimentBaseSchema.shape.metadata
}).strict().openapi("Experiment");
var appLimitSchema = z3.number().int().nonnegative().describe("Limit the number of objects to return");
function generateBaseTableOpSchema(objectName) {
return z3.object({
org_name: z3.string().nullish().describe(
`For nearly all users, this parameter should be unnecessary. But in the rare case that your API key belongs to multiple organizations, you may specify the name of the organization the ${objectName} belongs in.`
)
});
}
var startingAfterSchema = z3.string().uuid().describe(
[
"Pagination cursor id.",
"For example, if the final item in the last page you fetched had an id of `foo`, pass `starting_after=foo` to fetch the next page. Note: you may only pass one of `starting_after` and `ending_before`"
].join("\n\n")
).openapi("StartingAfter");
var endingBeforeSchema = z3.string().uuid().describe(
[
"Pagination cursor id.",
"For example, if the initial item in the last page you fetched had an id of `foo`, pass `ending_before=foo` to fetch the previous page. Note: you may only pass one of `starting_after` and `ending_before`"
].join("\n\n")
).openapi("EndingBefore");
var createProjectBaseSchema = generateBaseTableOpSchema("project");
var createProjectSchema = z3.object({
name: projectSchema.shape.name,
org_name: createProjectBaseSchema.shape.org_name
}).strict().openapi("CreateProject");
var patchProjectSchema = z3.object({
name: projectSchema.shape.name.nullish()
}).strict().openapi("PatchProject");
var createExperimentSchema = z3.object({
project_id: experimentSchema.shape.project_id,
name: experimentSchema.shape.name.nullish(),
description: experimentSchema.shape.description,
repo_info: experimentSchema.shape.repo_info,
base_exp_id: experimentSchema.shape.base_exp_id,
dataset_id: experimentSchema.shape.dataset_id,
dataset_version: experimentSchema.shape.dataset_version,
public: experimentSchema.shape.public.nullish(),
metadata: experimentSchema.shape.metadata
}).strict().openapi("CreateExperiment");
var patchExperimentSchema = createExperimentSchema.omit({ project_id: true }).strict().openapi("PatchExperiment");
var createDatasetSchema = z3.object({
project_id: datasetSchema.shape.project_id,
name: datasetSchema.shape.name,
description: datasetSchema.shape.description
}).strict().openapi("CreateDataset");
var patchDatasetSchema = createDatasetSchema.omit({ project_id: true }).strict().openapi("PatchDataset");
var objectSchemas = {
experiment: {
create: createExperimentSchema,
patch: patchExperimentSchema,
object: experimentSchema
},
dataset: {
create: createDatasetSchema,
patch: patchDatasetSchema,
object: datasetSchema
},
project: {
create: createProjectSchema,
patch: patchProjectSchema,
object: projectSchema
}
};
// src/util.ts
function capitalize(s, sep) {
const items = sep ? s.split(sep) : [s];
return items.map((s2) => s2 ? s2.charAt(0).toUpperCase() + s2.slice(1) : s2).join(sep || "");
}
// src/db_fields.ts
var TRANSACTION_ID_FIELD = "_xact_id";
var OBJECT_DELETE_FIELD = "_object_delete";
var IS_MERGE_FIELD = "_is_merge";
var MERGE_PATHS_FIELD = "_merge_paths";
var VALID_SOURCES = ["app", "api", "external"];
var PARENT_ID_FIELD = "_parent_id";
// src/span_types.ts
var SpanTypeAttribute = /* @__PURE__ */ ((SpanTypeAttribute2) => {
SpanTypeAttribute2["LLM"] = "llm";
SpanTypeAttribute2["SCORE"] = "score";
SpanTypeAttribute2["FUNCTION"] = "function";
SpanTypeAttribute2["EVAL"] = "eval";
SpanTypeAttribute2["TASK"] = "task";
SpanTypeAttribute2["TOOL"] = "tool";
return SpanTypeAttribute2;
})(SpanTypeAttribute || {});
// src/typespecs/api_types.ts
extendZodWithOpenApi3(z4);
var auditSourcesSchema = z4.enum(VALID_SOURCES);
function generateBaseEventOpSchema(objectType) {
const eventDescription = getEventObjectDescription(objectType);
return z4.object({
id: z4.string().describe(
`A unique identifier for the ${eventDescription} event. If you don't provide one, BrainTrust will generate one for you`
),
[TRANSACTION_ID_FIELD]: z4.bigint().describe(
`The transaction id of an event is unique to the network operation that processed the event insertion. Transaction ids are monotonically increasing over time and can be used to retrieve a versioned snapshot of the ${eventDescription} (see the \`version\` parameter)`
),
created: datetimeStringSchema.nullish().describe(`The timestamp the ${eventDescription} event was created`),
input: customTypes.any,
output: customTypes.any,
expected: customTypes.any,
scores: z4.record(z4.number().min(0).max(1).nullish()).nullish(),
metadata: z4.record(customTypes.any).nullish().describe(
"A dictionary with additional data about the test example, model outputs, or just about anything else that's relevant, that you can use to help find and analyze examples later. For example, you could log the `prompt`, example's `id`, or anything else that would be useful to slice/dice later. The values in `metadata` can be any JSON-serializable type, but its keys must be strings"
),
metrics: z4.object({
start: z4.number().nullish().describe(
`A unix timestamp recording when the section of code which produced the ${eventDescription} event started`
),
end: z4.number().nullish().describe(
`A unix timestamp recording when the section of code which produced the ${eventDescription} event finished`
)
}).catchall(customTypes.any).nullish().describe(
`Metrics are numerical measurements tracking the execution of the code that produced the ${eventDescription} event. Use "start" and "end" to track the time span over which the ${eventDescription} event was produced`
),
context: z4.object({
caller_functionname: z4.string().nullish().describe(
`The function in code which created the ${eventDescription} event`
),
caller_filename: z4.string().nullish().describe(
`Name of the file in code where the ${eventDescription} event was created`
),
caller_lineno: z4.number().int().nullish().describe(
`Line of code where the ${eventDescription} event was created`
)
}).catchall(customTypes.any).nullish().describe(
`Context is additional information about the code that produced the ${eventDescription} event. It is essentially the textual counterpart to \`metrics\`. Use the \`caller_*\` attributes to track the location in code which produced the ${eventDescription} event`
),
span_id: z4.string().describe(
`A unique identifier used to link different ${eventDescription} events together as part of a full trace. See the [tracing guide](https://www.braintrustdata.com/docs/guides/tracing) for full details on tracing`
),
span_parents: z4.string().array().nullish().describe(
`An array of the parent \`span_ids\` of this ${eventDescription} event. This should be empty for the root span of a trace, and should most often contain just one parent element for subspans`
),
root_span_id: z4.string().describe(
`The \`span_id\` of the root of the trace this ${eventDescription} event belongs to`
),
span_attributes: z4.object({
name: z4.string().nullish().describe("Name of the span, for display purposes only"),
type: z4.nativeEnum(SpanTypeAttribute).nullish().describe("Type of the span, for display purposes only")
}).catchall(customTypes.any).nullish().describe(
"Human-identifying attributes of the span, such as name, type, etc."
),
[OBJECT_DELETE_FIELD]: z4.boolean().nullish().describe(
`Pass \`${OBJECT_DELETE_FIELD}=true\` to mark the ${eventDescription} event deleted. Deleted events will not show up in subsequent fetches for this ${eventDescription}`
)
});
}
function generateBaseEventFeedbackSchema(objectType) {
const eventObjectType = getEventObjectType(objectType);
const eventDescription = getEventObjectDescription(objectType);
return z4.object({
id: z4.string().describe(
`The id of the ${eventDescription} event to log feedback for. This is the row \`id\` returned by \`POST /v1/${eventObjectType}/{${objectType}_id}/insert\``
),
scores: z4.record(z4.number().min(0).max(1).nullish()).nullish().describe(
`A dictionary of numeric values (between 0 and 1) to log. These scores will be merged into the existing scores for the ${eventDescription} event`
),
expected: customTypes.any.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not"
),
comment: z4.string().nullish().describe(
`An optional comment string to log about the ${eventDescription} event`
),
metadata: z4.record(customTypes.any).nullish().describe(
"A dictionary with additional data about the feedback. If you have a `user_id`, you can log it here and access it in the Braintrust UI."
),
source: auditSourcesSchema.nullish().describe(
'The source of the feedback. Must be one of "external" (default), "app", or "api"'
)
});
}
var fetchLimitSchema = z4.number().int().nonnegative().describe(
[
"limit the number of traces fetched",
`Fetch queries may be paginated if the total result size is expected to be large (e.g. project_logs which accumulate over a long time). Note that fetch queries only support pagination in descending time order (from latest to earliest \`${TRANSACTION_ID_FIELD}\`. Furthermore, later pages may return rows which showed up in earlier pages, except with an earlier \`${TRANSACTION_ID_FIELD}\`. This happens because pagination occurs over the whole version history of the event log. You will most likely want to exclude any such duplicate, outdated rows (by \`id\`) from your combined result set.`,
`The \`limit\` parameter controls the number of full traces to return. So you may end up with more individual rows than the specified limit if you are fetching events containing traces.`
].join("\n\n")
);
var maxXactIdSchema = z4.bigint().describe(
[
"Pagination cursor transaction ID, combined with `max_root_span_id`",
`Given a previous fetch with a list of rows, you can determine \`max_xact_id\` as the maximum of the \`${TRANSACTION_ID_FIELD}\` field over all rows. See the documentation for \`limit\` for an overview of paginating fetch queries.`
].join("\n\n")
);
var maxRootSpanIdSchema = z4.string().describe(
[
"Pagination cursor transaction root span ID, combined with `max_xact_id`",
`Given a previous fetch with a list of rows, you can determine \`max_root_span_id\` as the maximum of the \`root_span_id\` field over all rows. See the documentation for \`limit\` for an overview of paginating fetch queries.`
].join("\n\n")
);
var versionSchema = z4.bigint().describe(
[
"Retrieve a snapshot of events from a past time",
"The version id is essentially a filter on the latest event transaction id. You can use the `max_xact_id` returned by a past fetch as the version to reproduce that exact fetch."
].join("\n\n")
);
var pathTypeFilterSchema = z4.object({
type: z4.literal("path_lookup").describe("Denotes the type of filter as a path-lookup filter"),
path: z4.string().array().describe(
'List of fields describing the path to the value to be checked against. For instance, if you wish to filter on the value of `c` in `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `path=["input", "a", "b", "c"]`'
),
value: customTypes.any.describe(
'The value to compare equality-wise against the event value at the specified `path`. The value must be a "primitive", that is, any JSON-serializable object except for objects and arrays. For instance, if you wish to filter on the value of "input.a.b.c" in the object `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `value="hello"`'
)
}).describe(
'A path-lookup filter describes an equality comparison against a specific sub-field in the event row. For instance, if you wish to filter on the value of `c` in `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `path=["input", "a", "b", "c"]` and `value="hello"`'
).openapi("PathLookupFilter");
var sqlTypeFilterSchema = z4.object({
type: z4.literal("sql_filter").describe("Denotes the type of filter as a sql-type filter"),
expr: z4.string().describe(
`A SQL expression in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). For instance, if you wish to fuzzy-match the value of \`c\` in \`{"input": {"a": {"b": {"c": "hello"}}}}\`, pass \`expr="input->'a'->'b'->>'c' LIKE '%el%'"\`.`
)
}).describe(
`A sql-type filter describes a general filter over an individual row in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). For instance, if you wish to fuzzy-match the value of \`c\` in \`{"input": {"a": {"b": {"c": "hello"}}}}\`, pass \`expr="input->'a'->'b'->>'c' LIKE '%el%'"\`.`
).openapi("SQLFilter");
var allFetchFiltersSchema = z4.union([pathTypeFilterSchema, sqlTypeFilterSchema]).array().describe(
"A list of filters on the events to fetch. Filters can either be specialized `path=value` expressions or general SQL expressions in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). When possible, prefer path-lookup type filters over general SQL-type filters, as they are likely to activate indices in the DB and run faster"
).openapi("AllFetchEventsFilters");
var fetchFiltersSchema = pathTypeFilterSchema.array().describe(
"A list of filters on the events to fetch. Currently, only path-lookup type filters are supported, but we may add more in the future"
).openapi("FetchEventsFilters");
var fetchEventsRequestSchema = z4.object({
limit: fetchLimitSchema.nullish(),
max_xact_id: maxXactIdSchema.nullish(),
max_root_span_id: maxRootSpanIdSchema.nullish(),
filters: fetchFiltersSchema.nullish(),
version: versionSchema.nullish()
}).strict().openapi("FetchEventsRequest");
function makeFetchEventsResponseSchema(objectType, eventSchema) {
const eventName = capitalize(getEventObjectType(objectType), "_").replace(
"_",
""
);
return z4.object({
events: eventSchema.array().describe("A list of fetched events")
}).strict().openapi(`Fetch${eventName}EventsResponse`);
}
var experimentEventBaseSchema = generateBaseEventOpSchema("experiment");
var experimentEventSchema = z4.object({
id: experimentEventBaseSchema.shape.id,
dataset_record_id: z4.string().nullish().describe(
"If the experiment is associated to a dataset, this is the event-level dataset id this experiment event is tied to"
),
[TRANSACTION_ID_FIELD]: experimentEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: experimentEventBaseSchema.shape.created,
project_id: experimentSchema.shape.project_id,
experiment_id: experimentSchema.shape.id,
input: experimentEventBaseSchema.shape.input.describe(
"The arguments that uniquely define a test case (an arbitrary, JSON serializable object). Later on, Braintrust will use the `input` to know whether two test cases are the same between experiments, so they should not contain experiment-specific state. A simple rule of thumb is that if you run the same experiment twice, the `input` should be identical"
),
output: experimentEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object), that allows you to determine whether the result is correct or not. For example, in an app that generates SQL queries, the `output` should be the _result_ of the SQL query generated by the model, not the query itself, because there may be multiple valid queries that answer a single question"
),
expected: experimentEventBaseSchema.shape.expected.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not. Braintrust currently does not compare `output` to `expected` for you, since there are so many different ways to do that correctly. Instead, these values are just used to help you navigate your experiments while digging into analyses. However, we may later use these values to re-score outputs or fine-tune your models"
),
scores: experimentEventBaseSchema.shape.scores.describe(
"A dictionary of numeric values (between 0 and 1) to log. The scores should give you a variety of signals that help you determine how accurate the outputs are compared to what you expect and diagnose failures. For example, a summarization app might have one score that tells you how accurate the summary is, and another that measures the word similarity between the generated and grouth truth summary. The word similarity score could help you determine whether the summarization was covering similar concepts or not. You can use these scores to help you sort, filter, and compare experiments"
),
metadata: experimentEventBaseSchema.shape.metadata,
metrics: experimentEventBaseSchema.shape.metrics,
context: experimentEventBaseSchema.shape.context,
span_id: experimentEventBaseSchema.shape.span_id,
span_parents: experimentEventBaseSchema.shape.span_parents,
root_span_id: experimentEventBaseSchema.shape.root_span_id,
span_attributes: experimentEventBaseSchema.shape.span_attributes
}).strict().openapi("ExperimentEvent");
var datasetEventBaseSchema = generateBaseEventOpSchema("dataset");
var datasetEventSchema = z4.object({
id: datasetEventBaseSchema.shape.id,
[TRANSACTION_ID_FIELD]: datasetEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: datasetEventBaseSchema.shape.created,
project_id: datasetSchema.shape.project_id,
dataset_id: datasetSchema.shape.id,
input: datasetEventBaseSchema.shape.input.describe(
"The argument that uniquely define an input case (an arbitrary, JSON serializable object)"
),
output: datasetEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object)"
),
metadata: datasetEventBaseSchema.shape.metadata,
span_id: datasetEventBaseSchema.shape.span_id,
root_span_id: datasetEventBaseSchema.shape.root_span_id
}).strict().openapi("DatasetEvent");
var projectLogsEventBaseSchema = generateBaseEventOpSchema("project");
var projectLogsEventSchema = z4.object({
id: projectLogsEventBaseSchema.shape.id,
[TRANSACTION_ID_FIELD]: projectLogsEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: projectLogsEventBaseSchema.shape.created,
org_id: projectSchema.shape.org_id,
project_id: projectSchema.shape.id,
log_id: z4.literal("g").describe("A literal 'g' which identifies the log as a project log"),
input: projectLogsEventBaseSchema.shape.input.describe(
"The arguments that uniquely define a user input(an arbitrary, JSON serializable object)."
),
output: projectLogsEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object), that allows you to determine whether the result is correct or not. For example, in an app that generates SQL queries, the `output` should be the _result_ of the SQL query generated by the model, not the query itself, because there may be multiple valid queries that answer a single question."
),
expected: projectLogsEventBaseSchema.shape.expected.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not. Braintrust currently does not compare `output` to `expected` for you, since there are so many different ways to do that correctly. Instead, these values are just used to help you navigate while digging into analyses. However, we may later use these values to re-score outputs or fine-tune your models."
),
scores: projectLogsEventBaseSchema.shape.scores.describe(
"A dictionary of numeric values (between 0 and 1) to log. The scores should give you a variety of signals that help you determine how accurate the outputs are compared to what you expect and diagnose failures. For example, a summarization app might have one score that tells you how accurate the summary is, and another that measures the word similarity between the generated and grouth truth summary. The word similarity score could help you determine whether the summarization was covering similar concepts or not. You can use these scores to help you sort, filter, and compare logs."
),
metadata: projectLogsEventBaseSchema.shape.metadata,
metrics: projectLogsEventBaseSchema.shape.metrics,
context: projectLogsEventBaseSchema.shape.context,
span_id: projectLogsEventBaseSchema.shape.span_id,
span_parents: projectLogsEventBaseSchema.shape.span_parents,
root_span_id: projectLogsEventBaseSchema.shape.root_span_id,
span_attributes: projectLogsEventBaseSchema.shape.span_attributes
}).strict().openapi("ProjectLogsEvent");
var isMergeDescription = [
"The `_is_merge` field controls how the row is merged with any existing row with the same id in the DB. By default (or when set to `false`), the existing row is completely replaced by the new row. When set to `true`, the new row is deep-merged into the existing row",
'For example, say there is an existing row in the DB `{"id": "foo", "input": {"a": 5, "b": 10}}`. If we merge a new row as `{"_is_merge": true, "id": "foo", "input": {"b": 11, "c": 20}}`, the new row will be `{"id": "foo", "input": {"a": 5, "b": 11, "c": 20}}`. If we replace the new row as `{"id": "foo", "input": {"b": 11, "c": 20}}`, the new row will be `{"id": "foo", "input": {"b": 11, "c": 20}}`'
].join("\n\n");
var mergeEventSchema = z4.object({
[IS_MERGE_FIELD]: customTypes.literalTrue.describe(isMergeDescription),
[MERGE_PATHS_FIELD]: z4.string().array().array().nullish().describe(
[
"The `_merge_paths` field allows controlling the depth of the merge. It can only be specified alongside `_is_merge=true`. `_merge_paths` is a list of paths, where each path is a list of field names. The deep merge will not descend below any of the specified merge paths.",
'For example, say there is an existing row in the DB `{"id": "foo", "input": {"a": {"b": 10}, "c": {"d": 20}}, "output": {"a": 20}}`. If we merge a new row as `{"_is_merge": true, "_merge_paths": [["input", "a"], ["output"]], "input": {"a": {"q": 30}, "c": {"e": 30}, "bar": "baz"}, "output": {"d": 40}}`, the new row will be `{"id": "foo": "input": {"a": {"q": 30}, "c": {"d": 20, "e": 30}, "bar": "baz"}, "output": {"d": 40}}`. In this case, due to the merge paths, we have replaced `input.a` and `output`, but have still deep-merged `input` and `input.c`.'
].join("\n\n")
)
});
var replacementEventSchema = z4.object({
[IS_MERGE_FIELD]: customTypes.literalFalse.nullish().describe(isMergeDescription),
[PARENT_ID_FIELD]: z4.string().nullish().describe(
[
"Use the `_parent_id` field to create this row as a subspan of an existing row. It cannot be specified alongside `_is_merge=true`. Tracking hierarchical relationships are important for tracing (see the [guide](https://www.braintrustdata.com/docs/guides/tracing) for full details).",
'For example, say we have logged a row `{"id": "abc", "input": "foo", "output": "bar", "expected": "boo", "scores": {"correctness": 0.33}}`. We can create a sub-span of the parent row by logging `{"_parent_id": "abc", "id": "llm_call", "input": {"prompt": "What comes after foo?"}, "output": "bar", "metrics": {"tokens": 1}}`. In the webapp, only the root span row `"abc"` will show up in the summary view. You can view the full trace hierarchy (in this case, the `"llm_call"` row) by clicking on the "abc" row.'
].join("\n\n")
)
});
function makeInsertEventSchemas(objectType, insertSchema) {
const eventDescription = getEventObjectDescription(objectType);
const article = getEventObjectArticle(objectType);
const eventSchemaName = capitalize(
getEventObjectType(objectType),
"_"
).replace("_", "");
const replaceVariantSchema = insertSchema.merge(replacementEventSchema).strict().openapi(`Insert${eventSchemaName}EventReplace`);
const mergeVariantSchema = insertSchema.merge(mergeEventSchema).strict().openapi(`Insert${eventSchemaName}EventMerge`);
const eventSchema = z4.union([replaceVariantSchema, mergeVariantSchema]).describe(
`${capitalize(article)} ${eventDescription} event`
).openapi(`Insert${eventSchemaName}Event`);
const requestSchema = z4.object({
events: eventSchema.array().describe(`A list of ${eventDescription} events to insert`)
}).strict().openapi(`Insert${eventSchemaName}EventRequest`);
return { eventSchema, requestSchema };
}
var insertEventsResponseSchema = z4.object({
row_ids: z4.string().array().describe(
"The ids of all rows that were inserted, aligning one-to-one with the rows provided as input"
)
}).strict().openapi("InsertEventsResponse");
var {
eventSchema: insertExperimentEventSchema,
requestSchema: insertExperimentEventsRequestSchema
} = makeInsertEventSchemas(
"experiment",
z4.object({
input: experimentEventSchema.shape.input,
output: experimentEventSchema.shape.output,
expected: experimentEventSchema.shape.expected,
scores: experimentEventSchema.shape.scores,
metadata: experimentEventSchema.shape.metadata,
metrics: experimentEventSchema.shape.metrics,
context: experimentEventSchema.shape.context,
span_attributes: experimentEventSchema.shape.span_attributes,
id: experimentEventSchema.shape.id.nullish(),
dataset_record_id: experimentEventSchema.shape.dataset_record_id,
[OBJECT_DELETE_FIELD]: experimentEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
var {
eventSchema: insertDatasetEventSchema,
requestSchema: insertDatasetEventsRequestSchema
} = makeInsertEventSchemas(
"dataset",
z4.object({
input: datasetEventSchema.shape.input,
output: datasetEventSchema.shape.output,
metadata: datasetEventSchema.shape.metadata,
id: datasetEventSchema.shape.id.nullish(),
[OBJECT_DELETE_FIELD]: datasetEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
var {
eventSchema: insertProjectLogsEventSchema,
requestSchema: insertProjectLogsEventsRequestSchema
} = makeInsertEventSchemas(
"project",
z4.object({
input: projectLogsEventSchema.shape.input,
output: projectLogsEventSchema.shape.output,
expected: projectLogsEventSchema.shape.expected,
scores: projectLogsEventSchema.shape.scores,
metadata: projectLogsEventSchema.shape.metadata,
metrics: projectLogsEventSchema.shape.metrics,
context: projectLogsEventSchema.shape.context,
span_attributes: projectLogsEventSchema.shape.span_attributes,
id: projectLogsEventSchema.shape.id.nullish(),
[OBJECT_DELETE_FIELD]: projectLogsEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
function makeFeedbackRequestSchema(objectType, feedbackSchema) {
const eventDescription = getEventObjectDescription(objectType);
const eventSchemaName = capitalize(
getEventObjectType(objectType),
"_"
).replace("_", "");
return z4.object({
feedback: feedbackSchema.array().describe(`A list of ${eventDescription} feedback items`)
}).strict().openapi(`Feedback${eventSchemaName}EventRequest`);
}
var feedbackExperimentRequestBaseSchema = generateBaseEventFeedbackSchema("experiment");
var feedbackExperimentItemSchema = z4.object({
id: feedbackExperimentRequestBaseSchema.shape.id,
scores: feedbackExperimentRequestBaseSchema.shape.scores,
expected: feedbackExperimentRequestBaseSchema.shape.expected,
comment: feedbackExperimentRequestBaseSchema.shape.comment,
metadata: feedbackExperimentRequestBaseSchema.shape.metadata,
source: feedbackExperimentRequestBaseSchema.shape.source
}).strict().openapi("FeedbackExperimentItem");
var feedbackExperimentRequestSchema = makeFeedbackRequestSchema(
"experiment",
feedbackExperimentItemSchema
);
var feedbackDatasetRequestBaseSchema = generateBaseEventFeedbackSchema("dataset");
var feedbackDatasetItemSchema = z4.object({
id: feedbackDatasetRequestBaseSchema.shape.id,
comment: feedbackDatasetRequestBaseSchema.shape.comment,
metadata: feedbackDatasetRequestBaseSchema.shape.metadata,
source: feedbackDatasetRequestBaseSchema.shape.source
}).strict().openapi("FeedbackDatasetItem");
var feedbackDatasetRequestSchema = makeFeedbackRequestSchema(
"dataset",
feedbackDatasetItemSchema
);
var feedbackProjectLogsRequestBaseSchema = generateBaseEventFeedbackSchema("project");
var feedbackProjectLogsItemSchema = z4.object({
id: feedbackProjectLogsRequestBaseSchema.shape.id,
scores: feedbackProjectLogsRequestBaseSchema.shape.scores,
expected: feedbackProjectLogsRequestBaseSchema.shape.expected,
comment: feedbackProjectLogsRequestBaseSchema.shape.comment,
metadata: feedbackProjectLogsRequestBaseSchema.shape.metadata,
source: feedbackProjectLogsRequestBaseSchema.shape.source
}).strict().openapi("FeedbackProjectLogsItem");
var feedbackProjectLogsRequestSchema = makeFeedbackRequestSchema(
"project",
feedbackProjectLogsItemSchema
);
var eventObjectSchemas = {
experiment: {
fetchResponse: makeFetchEventsResponseSchema(
"experiment",
experimentEventSchema
),
insertEvent: insertExperimentEventSchema,
insertRequest: insertExperimentEventsRequestSchema,
feedbackItem: feedbackExperimentItemSchema,
feedbackRequest: feedbackExperimentRequestSchema
},
dataset: {
fetchResponse: makeFetchEventsResponseSchema("dataset", datasetEventSchema),
insertEvent: insertDatasetEventSchema,
insertRequest: insertDatasetEventsRequestSchema,
feedbackItem: feedbackDatasetItemSchema,
feedbackRequest: feedbackDatasetRequestSchema
},
project_logs: {
fetchResponse: makeFetchEventsResponseSchema(
"project",
projectLogsEventSchema
),
insertEvent: insertProjectLogsEventSchema,
insertRequest: insertProjectLogsEventsRequestSchema,
feedbackItem: feedbackProjectLogsItemSchema,
feedbackRequest: feedbackProjectLogsRequestSchema
}
};
function makeCrossObjectIndividualRequestSchema(objectType) {
const eventObjectType = getEventObjectType(objectType);
const eventDescription = getEventObjectDescription(objectType);
const eventObjectSchema = eventObjectSchemas[eventObjectType];
const insertObject = z4.object({
events: eventObjectSchema.insertEvent.array().nullish().describe(`A list of ${eventDescription} events to insert`),
feedback: eventObjectSchema.feedbackItem.array().nullish().describe(`A list of ${eventDescription} feedback items`)
}).strict();
return z4.record(z4.string().uuid(), insertObject).nullish().describe(`A mapping from ${objectType} id to a set of log events and feedback items to insert`);
}
function makeCrossObjectIndividualResponseSchema(objectType) {
return z4.record(z4.string().uuid(), insertEventsResponseSchema).nullish().describe(`A mapping from ${objectType} id to row ids for inserted \`events\``);
}
var crossObjectInsertRequestSchema = z4.object({
experiment: makeCrossObjectIndividualRequestSchema("experiment"),
dataset: makeCrossObjectIndividualRequestSchema("dataset"),
project_logs: makeCrossObjectIndividualRequestSchema("project")
}).strict().openapi("CrossObjectInsertRequest");
var crossObjectInsertResponseSchema = z4.object({
experiment: makeCrossObjectIndividualResponseSchema("experiment"),
dataset: makeCrossObjectIndividualResponseSchema("dataset"),
project_logs: makeCrossObjectIndividualResponseSchema("project")
}).strict().openapi("CrossObjectInsertResponse");
var summarizeScoresParamSchema = z4.boolean().describe("Whether to summarize the scores and metrics. If false (or omitted), only the metadata will be returned.");
var comparisonExperimentIdParamSchema = z4.string().uuid().describe("The experiment to compare against, if summarizing scores and metrics. If omitted, will fall back to the `base_exp_id` stored in the experiment metadata, and then to the most recent experiment run in the same project. Must pass `summarize_scores=true` for this id to be used");
var summarizeDataParamSchema = z4.boolean().describe("Whether to summarize the data. If false (or omitted), only the metadata will be returned.");
var summarizeExperimentResponseSchema = z4.object({
project_name: z4.string().describe("Name of the project that the experiment belongs to"),
experiment_name: z4.string().describe("Name of the experiment"),
project_url: z4.string().url().describe("URL to the project's page in the Braintrust app"),
experiment_url: z4.string().url().describe("URL to the experiment's page in the Braintrust app"),
comparison_experiment_name: z4.string().nullish().describe("The experiment which scores are baselined against"),
scores: z4.record(z4.object({
name: z4.string().describe("Name of the score"),
score: z4.number().min(0).max(1).describe("Average score across all examples"),
diff: z4.number().min(-1).max(1).describe("Difference in score between the current and comparison experiment"),
improvements: z4.number().int().min(0).describe("Number of improvements in the score"),
regressions: z4.number().int().min(0).describe("Number of regressions in the score")
}).describe("Summary of a score's performance").openapi("ScoreSummary")).nullish().describe("Summary of the experiment's scores"),
metrics: z4.record(z4.object({
name: z4.string().describe("Name of the metric"),
metric: z4.number().describe("Average metric across all examples"),
unit: z4.string().describe("Unit label for the metric"),
diff: z4.number().describe("Difference in metric between the current and comparison experiment"),
improvements: z4.number().int().min(0).describe("Number of improvements in the metric"),
regressions: z4.number().int().min(0).describe("Number of regressions in the metric")
}).describe("Summary of a metric's performance").openapi("MetricSummary")).nullish().describe("Summary of the experiment's metrics")
}).strict().describe("Summary of an experiment").openapi("SummarizeExperimentResponse");
var summarizeDatasetResponseSchema = z4.object({
project_name: z4.string().describe("Name of the project that the dataset belongs to"),
dataset_name: z4.string().describe("Name of the dataset"),
project_url: z4.string().url().describe("URL to the project's page in the Braintrust app"),
dataset_url: z4.string().url().describe("URL to the dataset's page in the Braintrust app"),
data_summary: z4.object({
total_records: z4.number().int().min(0).describe("Total number of records in the dataset")
}).nullish().describe("Summary of a dataset's data").openapi("DataSummary")
}).strict().describe("Summary of a dataset").openapi("SummarizeDatasetResponse");
var objectTypeSummarizeResponseSchemas = {
experiment: summarizeExperimentResponseSchema,
dataset: summarizeDatasetResponseSchema,
project: void 0
};
export {
allFetchFiltersSchema,
apiKeySchema,
appLimitSchema,
auditSourcesSchema,
comparisonExperimentIdParamSchema,
crossObjectInsertRequestSchema,
crossObjectInsertResponseSchema,
datasetSchema,
datetimeStringSchema,
endingBeforeSchema,
eventObjectSchemas,
experimentSchema,
fetchEventsRequestSchema,
fetchFiltersSchema,
fetchLimitSchema,
getEventObjectArticle,
getEventObjectDescription,
getEventObjectType,
insertEventsResponseSchema,
jsonSchema,
literalSchema,
maxRootSpanIdSchema,
maxXactIdSchema,
meSchema,
memberSchema,
objectSchemas,
objectTypeSummarizeResponseSchemas,
objectTypes,
organizationSchema,
projectSchema,
startingAfterSchema,
summarizeDataParamSchema,
summarizeScoresParamSchema,
userSchema,
versionSchema
};

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/typespecs/index.ts
var typespecs_exports = {};
__export(typespecs_exports, {
allFetchFiltersSchema: () => allFetchFiltersSchema,
apiKeySchema: () => apiKeySchema,
appLimitSchema: () => appLimitSchema,
auditSourcesSchema: () => auditSourcesSchema,
comparisonExperimentIdParamSchema: () => comparisonExperimentIdParamSchema,
crossObjectInsertRequestSchema: () => crossObjectInsertRequestSchema,
crossObjectInsertResponseSchema: () => crossObjectInsertResponseSchema,
datasetSchema: () => datasetSchema,
datetimeStringSchema: () => datetimeStringSchema,
endingBeforeSchema: () => endingBeforeSchema,
eventObjectSchemas: () => eventObjectSchemas,
experimentSchema: () => experimentSchema,
fetchEventsRequestSchema: () => fetchEventsRequestSchema,
fetchFiltersSchema: () => fetchFiltersSchema,
fetchLimitSchema: () => fetchLimitSchema,
getEventObjectArticle: () => getEventObjectArticle,
getEventObjectDescription: () => getEventObjectDescription,
getEventObjectType: () => getEventObjectType,
insertEventsResponseSchema: () => insertEventsResponseSchema,
jsonSchema: () => jsonSchema,
literalSchema: () => literalSchema,
maxRootSpanIdSchema: () => maxRootSpanIdSchema,
maxXactIdSchema: () => maxXactIdSchema,
meSchema: () => meSchema,
memberSchema: () => memberSchema,
objectSchemas: () => objectSchemas,
objectTypeSummarizeResponseSchemas: () => objectTypeSummarizeResponseSchemas,
objectTypes: () => objectTypes,
organizationSchema: () => organizationSchema,
projectSchema: () => projectSchema,
startingAfterSchema: () => startingAfterSchema,
summarizeDataParamSchema: () => summarizeDataParamSchema,
summarizeScoresParamSchema: () => summarizeScoresParamSchema,
userSchema: () => userSchema,
versionSchema: () => versionSchema
});
module.exports = __toCommonJS(typespecs_exports);
// src/typespecs/api_types.ts
var import_zod_to_openapi3 = require("@asteasolutions/zod-to-openapi");
var import_zod4 = require("zod");
// src/typespecs/app_types.ts
var import_zod_to_openapi2 = require("@asteasolutions/zod-to-openapi");
var import_zod3 = require("zod");
// src/typespecs/common_types.ts
var import_zod = require("zod");
var literalSchema = import_zod.z.union([
import_zod.z.string(),
import_zod.z.number(),
import_zod.z.boolean(),
import_zod.z.null()
]);
var jsonSchema = import_zod.z.lazy(
() => import_zod.z.union([literalSchema, import_zod.z.array(jsonSchema), import_zod.z.record(jsonSchema)])
);
var datetimeStringSchema = import_zod.z.string().datetime({ offset: true });
var objectTypes = ["project", "experiment", "dataset"];
function getEventObjectType(objectType) {
return objectType === "project" ? "project_logs" : objectType;
}
function getEventObjectDescription(objectType) {
return getEventObjectType(objectType).replace("_", " ");
}
function getEventObjectArticle(objectType) {
return objectType === "experiment" ? "an" : "a";
}
// src/typespecs/custom_types.ts
var import_zod_to_openapi = require("@asteasolutions/zod-to-openapi");
var import_zod2 = require("zod");
(0, import_zod_to_openapi.extendZodWithOpenApi)(import_zod2.z);
var modeSchema = import_zod2.z.enum(["default", "stainless"]);
var _a;
var mode = modeSchema.parse(
typeof process === "undefined" ? "default" : ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.BRAINTRUST_TYPESPECS_MODE) || "default"
);
var modeToTypes = {
default: {
any: import_zod2.z.any(),
literalTrue: import_zod2.z.literal(true),
literalFalse: import_zod2.z.literal(false)
},
stainless: {
// Stainless requires schemas which are completely permissive to be
// tagged.
any: import_zod2.z.any().openapi({ ["x-stainless-any"]: true }),
// Stainless does not support boolean literals in all SDKs.
literalTrue: import_zod2.z.boolean(),
literalFalse: import_zod2.z.boolean()
}
};
var customTypes = modeToTypes[mode];
// src/typespecs/app_types.ts
(0, import_zod_to_openapi2.extendZodWithOpenApi)(import_zod3.z);
function generateBaseTableSchema(objectName, opts) {
let nameDescription = `Name of the ${objectName}`;
if (opts == null ? void 0 : opts.underProject) {
nameDescription += `. Within a project, ${objectName} names are unique`;
}
return import_zod3.z.object({
id: import_zod3.z.string().uuid().describe(`Unique identifier for the ${objectName}`),
project_id: import_zod3.z.string().uuid().describe(
`Unique identifier for the project that the ${objectName} belongs under`
),
name: import_zod3.z.string().describe(nameDescription),
description: import_zod3.z.string().nullish().describe(`Textual description of the ${objectName}`),
created: datetimeStringSchema.nullish().describe(`Date of ${objectName} creation`),
deleted_at: datetimeStringSchema.nullish().describe(
`Date of ${objectName} deletion, or null if the ${objectName} is still active`
),
user_id: import_zod3.z.string().uuid().nullish().describe(`Identifies the user who created the ${objectName}`),
metadata: import_zod3.z.record(customTypes.any).nullish().describe(`User-controlled metadata about the ${objectName}`)
});
}
var userBaseSchema = generateBaseTableSchema("user");
var userSchema = import_zod3.z.object({
id: userBaseSchema.shape.id,
auth_id: import_zod3.z.string().uuid().nullish().describe("Internal authentication token used to identify the user"),
given_name: import_zod3.z.string().nullish().describe("Given name of the user"),
family_name: import_zod3.z.string().nullish().describe("Family name of the user"),
email: import_zod3.z.string().nullish().describe("The user's email"),
avatar_url: import_zod3.z.string().nullish().describe("URL of the user's Avatar image"),
created: userBaseSchema.shape.created
}).strict().openapi("User");
var organizationBaseSchema = generateBaseTableSchema("organization");
var organizationSchema = import_zod3.z.object({
id: organizationBaseSchema.shape.id,
name: organizationBaseSchema.shape.name.nullish(),
api_url: import_zod3.z.string().nullish(),
created: organizationBaseSchema.shape.created
}).strict().openapi("Organization");
var memberSchema = import_zod3.z.object({
org_id: organizationSchema.shape.id,
user_id: userSchema.shape.id
}).strict().openapi("Member");
var meSchema = import_zod3.z.object({
id: userSchema.shape.id,
// By filtering by auth_id equality, we will ensure this is not-null.
auth_id: userSchema.shape.auth_id.unwrap().unwrap(),
organizations: import_zod3.z.object({
id: memberSchema.shape.org_id,
name: organizationSchema.shape.name
}).array()
}).strict().openapi("Me");
var apiKeyBaseSchema = generateBaseTableSchema("api key");
var apiKeySchema = import_zod3.z.object({
id: apiKeyBaseSchema.shape.id,
created: apiKeyBaseSchema.shape.created,
key_hash: import_zod3.z.string(),
name: apiKeyBaseSchema.shape.name,
preview_name: import_zod3.z.string(),
user_id: userSchema.shape.id.nullish(),
org_id: organizationSchema.shape.id.nullish()
}).strict().openapi("ApiKey");
var projectBaseSchema = generateBaseTableSchema("project");
var projectSchema = import_zod3.z.object({
id: projectBaseSchema.shape.id,
org_id: import_zod3.z.string().uuid().describe(
"Unique id for the organization that the project belongs under"
),
name: projectBaseSchema.shape.name,
created: projectBaseSchema.shape.created,
deleted_at: projectBaseSchema.shape.deleted_at,
user_id: projectBaseSchema.shape.user_id
}).strict().openapi("Project");
var datasetBaseSchema = generateBaseTableSchema("dataset", {
underProject: true
});
var datasetSchema = import_zod3.z.object({
id: datasetBaseSchema.shape.id,
project_id: datasetBaseSchema.shape.project_id.nullish(),
name: datasetBaseSchema.shape.name,
description: datasetBaseSchema.shape.description,
created: datasetBaseSchema.shape.created,
deleted_at: datasetBaseSchema.shape.deleted_at,
user_id: datasetBaseSchema.shape.user_id
}).strict().openapi("Dataset");
var repoInfoSchema = import_zod3.z.object({
commit: import_zod3.z.string().nullish().describe("SHA of most recent commit"),
branch: import_zod3.z.string().nullish().describe("Name of the branch the most recent commit belongs to"),
tag: import_zod3.z.string().nullish().describe("Name of the tag on the most recent commit"),
dirty: import_zod3.z.boolean().nullish().describe(
"Whether or not the repo had uncommitted changes when snapshotted"
),
author_name: import_zod3.z.string().nullish().describe("Name of the author of the most recent commit"),
author_email: import_zod3.z.string().nullish().describe("Email of the author of the most recent commit"),
commit_message: import_zod3.z.string().nullish().describe("Most recent commit message"),
commit_time: import_zod3.z.string().nullish().describe("Time of the most recent commit"),
git_diff: import_zod3.z.string().nullish().describe(
"If the repo was dirty when run, this includes the diff between the current state of the repo and the most recent commit."
)
}).describe(
"Metadata about the state of the repo when the experiment was created"
).openapi("RepoInfo");
var experimentBaseSchema = generateBaseTableSchema("experiment", {
underProject: true
});
var experimentSchema = import_zod3.z.object({
id: experimentBaseSchema.shape.id,
project_id: experimentBaseSchema.shape.project_id,
name: experimentBaseSchema.shape.name,
description: experimentBaseSchema.shape.description,
created: experimentBaseSchema.shape.created,
repo_info: repoInfoSchema.nullish(),
commit: import_zod3.z.string().nullish().describe("Commit, taken directly from `repo_info.commit`"),
base_exp_id: import_zod3.z.string().uuid().nullish().describe(
"Id of default base experiment to compare against when viewing this experiment"
),
deleted_at: experimentBaseSchema.shape.deleted_at,
dataset_id: import_zod3.z.string().uuid().nullish().describe(
"Identifier of the linked dataset, or null if the experiment is not linked to a dataset"
),
dataset_version: import_zod3.z.string().nullish().describe(
"Version number of the linked dataset the experiment was run against. This can be used to reproduce the experiment after the dataset has been modified."
),
public: import_zod3.z.boolean().describe(
"Whether or not the experiment is public. Public experiments can be viewed by anybody inside or outside the organization"
),
user_id: experimentBaseSchema.shape.user_id,
metadata: experimentBaseSchema.shape.metadata
}).strict().openapi("Experiment");
var appLimitSchema = import_zod3.z.number().int().nonnegative().describe("Limit the number of objects to return");
function generateBaseTableOpSchema(objectName) {
return import_zod3.z.object({
org_name: import_zod3.z.string().nullish().describe(
`For nearly all users, this parameter should be unnecessary. But in the rare case that your API key belongs to multiple organizations, you may specify the name of the organization the ${objectName} belongs in.`
)
});
}
var startingAfterSchema = import_zod3.z.string().uuid().describe(
[
"Pagination cursor id.",
"For example, if the final item in the last page you fetched had an id of `foo`, pass `starting_after=foo` to fetch the next page. Note: you may only pass one of `starting_after` and `ending_before`"
].join("\n\n")
).openapi("StartingAfter");
var endingBeforeSchema = import_zod3.z.string().uuid().describe(
[
"Pagination cursor id.",
"For example, if the initial item in the last page you fetched had an id of `foo`, pass `ending_before=foo` to fetch the previous page. Note: you may only pass one of `starting_after` and `ending_before`"
].join("\n\n")
).openapi("EndingBefore");
var createProjectBaseSchema = generateBaseTableOpSchema("project");
var createProjectSchema = import_zod3.z.object({
name: projectSchema.shape.name,
org_name: createProjectBaseSchema.shape.org_name
}).strict().openapi("CreateProject");
var patchProjectSchema = import_zod3.z.object({
name: projectSchema.shape.name.nullish()
}).strict().openapi("PatchProject");
var createExperimentSchema = import_zod3.z.object({
project_id: experimentSchema.shape.project_id,
name: experimentSchema.shape.name.nullish(),
description: experimentSchema.shape.description,
repo_info: experimentSchema.shape.repo_info,
base_exp_id: experimentSchema.shape.base_exp_id,
dataset_id: experimentSchema.shape.dataset_id,
dataset_version: experimentSchema.shape.dataset_version,
public: experimentSchema.shape.public.nullish(),
metadata: experimentSchema.shape.metadata
}).strict().openapi("CreateExperiment");
var patchExperimentSchema = createExperimentSchema.omit({ project_id: true }).strict().openapi("PatchExperiment");
var createDatasetSchema = import_zod3.z.object({
project_id: datasetSchema.shape.project_id,
name: datasetSchema.shape.name,
description: datasetSchema.shape.description
}).strict().openapi("CreateDataset");
var patchDatasetSchema = createDatasetSchema.omit({ project_id: true }).strict().openapi("PatchDataset");
var objectSchemas = {
experiment: {
create: createExperimentSchema,
patch: patchExperimentSchema,
object: experimentSchema
},
dataset: {
create: createDatasetSchema,
patch: patchDatasetSchema,
object: datasetSchema
},
project: {
create: createProjectSchema,
patch: patchProjectSchema,
object: projectSchema
}
};
// src/util.ts
function capitalize(s, sep) {
const items = sep ? s.split(sep) : [s];
return items.map((s2) => s2 ? s2.charAt(0).toUpperCase() + s2.slice(1) : s2).join(sep || "");
}
// src/db_fields.ts
var TRANSACTION_ID_FIELD = "_xact_id";
var OBJECT_DELETE_FIELD = "_object_delete";
var IS_MERGE_FIELD = "_is_merge";
var MERGE_PATHS_FIELD = "_merge_paths";
var VALID_SOURCES = ["app", "api", "external"];
var PARENT_ID_FIELD = "_parent_id";
// src/span_types.ts
var SpanTypeAttribute = /* @__PURE__ */ ((SpanTypeAttribute2) => {
SpanTypeAttribute2["LLM"] = "llm";
SpanTypeAttribute2["SCORE"] = "score";
SpanTypeAttribute2["FUNCTION"] = "function";
SpanTypeAttribute2["EVAL"] = "eval";
SpanTypeAttribute2["TASK"] = "task";
SpanTypeAttribute2["TOOL"] = "tool";
return SpanTypeAttribute2;
})(SpanTypeAttribute || {});
// src/typespecs/api_types.ts
(0, import_zod_to_openapi3.extendZodWithOpenApi)(import_zod4.z);
var auditSourcesSchema = import_zod4.z.enum(VALID_SOURCES);
function generateBaseEventOpSchema(objectType) {
const eventDescription = getEventObjectDescription(objectType);
return import_zod4.z.object({
id: import_zod4.z.string().describe(
`A unique identifier for the ${eventDescription} event. If you don't provide one, BrainTrust will generate one for you`
),
[TRANSACTION_ID_FIELD]: import_zod4.z.bigint().describe(
`The transaction id of an event is unique to the network operation that processed the event insertion. Transaction ids are monotonically increasing over time and can be used to retrieve a versioned snapshot of the ${eventDescription} (see the \`version\` parameter)`
),
created: datetimeStringSchema.nullish().describe(`The timestamp the ${eventDescription} event was created`),
input: customTypes.any,
output: customTypes.any,
expected: customTypes.any,
scores: import_zod4.z.record(import_zod4.z.number().min(0).max(1).nullish()).nullish(),
metadata: import_zod4.z.record(customTypes.any).nullish().describe(
"A dictionary with additional data about the test example, model outputs, or just about anything else that's relevant, that you can use to help find and analyze examples later. For example, you could log the `prompt`, example's `id`, or anything else that would be useful to slice/dice later. The values in `metadata` can be any JSON-serializable type, but its keys must be strings"
),
metrics: import_zod4.z.object({
start: import_zod4.z.number().nullish().describe(
`A unix timestamp recording when the section of code which produced the ${eventDescription} event started`
),
end: import_zod4.z.number().nullish().describe(
`A unix timestamp recording when the section of code which produced the ${eventDescription} event finished`
)
}).catchall(customTypes.any).nullish().describe(
`Metrics are numerical measurements tracking the execution of the code that produced the ${eventDescription} event. Use "start" and "end" to track the time span over which the ${eventDescription} event was produced`
),
context: import_zod4.z.object({
caller_functionname: import_zod4.z.string().nullish().describe(
`The function in code which created the ${eventDescription} event`
),
caller_filename: import_zod4.z.string().nullish().describe(
`Name of the file in code where the ${eventDescription} event was created`
),
caller_lineno: import_zod4.z.number().int().nullish().describe(
`Line of code where the ${eventDescription} event was created`
)
}).catchall(customTypes.any).nullish().describe(
`Context is additional information about the code that produced the ${eventDescription} event. It is essentially the textual counterpart to \`metrics\`. Use the \`caller_*\` attributes to track the location in code which produced the ${eventDescription} event`
),
span_id: import_zod4.z.string().describe(
`A unique identifier used to link different ${eventDescription} events together as part of a full trace. See the [tracing guide](https://www.braintrustdata.com/docs/guides/tracing) for full details on tracing`
),
span_parents: import_zod4.z.string().array().nullish().describe(
`An array of the parent \`span_ids\` of this ${eventDescription} event. This should be empty for the root span of a trace, and should most often contain just one parent element for subspans`
),
root_span_id: import_zod4.z.string().describe(
`The \`span_id\` of the root of the trace this ${eventDescription} event belongs to`
),
span_attributes: import_zod4.z.object({
name: import_zod4.z.string().nullish().describe("Name of the span, for display purposes only"),
type: import_zod4.z.nativeEnum(SpanTypeAttribute).nullish().describe("Type of the span, for display purposes only")
}).catchall(customTypes.any).nullish().describe(
"Human-identifying attributes of the span, such as name, type, etc."
),
[OBJECT_DELETE_FIELD]: import_zod4.z.boolean().nullish().describe(
`Pass \`${OBJECT_DELETE_FIELD}=true\` to mark the ${eventDescription} event deleted. Deleted events will not show up in subsequent fetches for this ${eventDescription}`
)
});
}
function generateBaseEventFeedbackSchema(objectType) {
const eventObjectType = getEventObjectType(objectType);
const eventDescription = getEventObjectDescription(objectType);
return import_zod4.z.object({
id: import_zod4.z.string().describe(
`The id of the ${eventDescription} event to log feedback for. This is the row \`id\` returned by \`POST /v1/${eventObjectType}/{${objectType}_id}/insert\``
),
scores: import_zod4.z.record(import_zod4.z.number().min(0).max(1).nullish()).nullish().describe(
`A dictionary of numeric values (between 0 and 1) to log. These scores will be merged into the existing scores for the ${eventDescription} event`
),
expected: customTypes.any.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not"
),
comment: import_zod4.z.string().nullish().describe(
`An optional comment string to log about the ${eventDescription} event`
),
metadata: import_zod4.z.record(customTypes.any).nullish().describe(
"A dictionary with additional data about the feedback. If you have a `user_id`, you can log it here and access it in the Braintrust UI."
),
source: auditSourcesSchema.nullish().describe(
'The source of the feedback. Must be one of "external" (default), "app", or "api"'
)
});
}
var fetchLimitSchema = import_zod4.z.number().int().nonnegative().describe(
[
"limit the number of traces fetched",
`Fetch queries may be paginated if the total result size is expected to be large (e.g. project_logs which accumulate over a long time). Note that fetch queries only support pagination in descending time order (from latest to earliest \`${TRANSACTION_ID_FIELD}\`. Furthermore, later pages may return rows which showed up in earlier pages, except with an earlier \`${TRANSACTION_ID_FIELD}\`. This happens because pagination occurs over the whole version history of the event log. You will most likely want to exclude any such duplicate, outdated rows (by \`id\`) from your combined result set.`,
`The \`limit\` parameter controls the number of full traces to return. So you may end up with more individual rows than the specified limit if you are fetching events containing traces.`
].join("\n\n")
);
var maxXactIdSchema = import_zod4.z.bigint().describe(
[
"Pagination cursor transaction ID, combined with `max_root_span_id`",
`Given a previous fetch with a list of rows, you can determine \`max_xact_id\` as the maximum of the \`${TRANSACTION_ID_FIELD}\` field over all rows. See the documentation for \`limit\` for an overview of paginating fetch queries.`
].join("\n\n")
);
var maxRootSpanIdSchema = import_zod4.z.string().describe(
[
"Pagination cursor transaction root span ID, combined with `max_xact_id`",
`Given a previous fetch with a list of rows, you can determine \`max_root_span_id\` as the maximum of the \`root_span_id\` field over all rows. See the documentation for \`limit\` for an overview of paginating fetch queries.`
].join("\n\n")
);
var versionSchema = import_zod4.z.bigint().describe(
[
"Retrieve a snapshot of events from a past time",
"The version id is essentially a filter on the latest event transaction id. You can use the `max_xact_id` returned by a past fetch as the version to reproduce that exact fetch."
].join("\n\n")
);
var pathTypeFilterSchema = import_zod4.z.object({
type: import_zod4.z.literal("path_lookup").describe("Denotes the type of filter as a path-lookup filter"),
path: import_zod4.z.string().array().describe(
'List of fields describing the path to the value to be checked against. For instance, if you wish to filter on the value of `c` in `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `path=["input", "a", "b", "c"]`'
),
value: customTypes.any.describe(
'The value to compare equality-wise against the event value at the specified `path`. The value must be a "primitive", that is, any JSON-serializable object except for objects and arrays. For instance, if you wish to filter on the value of "input.a.b.c" in the object `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `value="hello"`'
)
}).describe(
'A path-lookup filter describes an equality comparison against a specific sub-field in the event row. For instance, if you wish to filter on the value of `c` in `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `path=["input", "a", "b", "c"]` and `value="hello"`'
).openapi("PathLookupFilter");
var sqlTypeFilterSchema = import_zod4.z.object({
type: import_zod4.z.literal("sql_filter").describe("Denotes the type of filter as a sql-type filter"),
expr: import_zod4.z.string().describe(
`A SQL expression in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). For instance, if you wish to fuzzy-match the value of \`c\` in \`{"input": {"a": {"b": {"c": "hello"}}}}\`, pass \`expr="input->'a'->'b'->>'c' LIKE '%el%'"\`.`
)
}).describe(
`A sql-type filter describes a general filter over an individual row in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). For instance, if you wish to fuzzy-match the value of \`c\` in \`{"input": {"a": {"b": {"c": "hello"}}}}\`, pass \`expr="input->'a'->'b'->>'c' LIKE '%el%'"\`.`
).openapi("SQLFilter");
var allFetchFiltersSchema = import_zod4.z.union([pathTypeFilterSchema, sqlTypeFilterSchema]).array().describe(
"A list of filters on the events to fetch. Filters can either be specialized `path=value` expressions or general SQL expressions in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). When possible, prefer path-lookup type filters over general SQL-type filters, as they are likely to activate indices in the DB and run faster"
).openapi("AllFetchEventsFilters");
var fetchFiltersSchema = pathTypeFilterSchema.array().describe(
"A list of filters on the events to fetch. Currently, only path-lookup type filters are supported, but we may add more in the future"
).openapi("FetchEventsFilters");
var fetchEventsRequestSchema = import_zod4.z.object({
limit: fetchLimitSchema.nullish(),
max_xact_id: maxXactIdSchema.nullish(),
max_root_span_id: maxRootSpanIdSchema.nullish(),
filters: fetchFiltersSchema.nullish(),
version: versionSchema.nullish()
}).strict().openapi("FetchEventsRequest");
function makeFetchEventsResponseSchema(objectType, eventSchema) {
const eventName = capitalize(getEventObjectType(objectType), "_").replace(
"_",
""
);
return import_zod4.z.object({
events: eventSchema.array().describe("A list of fetched events")
}).strict().openapi(`Fetch${eventName}EventsResponse`);
}
var experimentEventBaseSchema = generateBaseEventOpSchema("experiment");
var experimentEventSchema = import_zod4.z.object({
id: experimentEventBaseSchema.shape.id,
dataset_record_id: import_zod4.z.string().nullish().describe(
"If the experiment is associated to a dataset, this is the event-level dataset id this experiment event is tied to"
),
[TRANSACTION_ID_FIELD]: experimentEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: experimentEventBaseSchema.shape.created,
project_id: experimentSchema.shape.project_id,
experiment_id: experimentSchema.shape.id,
input: experimentEventBaseSchema.shape.input.describe(
"The arguments that uniquely define a test case (an arbitrary, JSON serializable object). Later on, Braintrust will use the `input` to know whether two test cases are the same between experiments, so they should not contain experiment-specific state. A simple rule of thumb is that if you run the same experiment twice, the `input` should be identical"
),
output: experimentEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object), that allows you to determine whether the result is correct or not. For example, in an app that generates SQL queries, the `output` should be the _result_ of the SQL query generated by the model, not the query itself, because there may be multiple valid queries that answer a single question"
),
expected: experimentEventBaseSchema.shape.expected.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not. Braintrust currently does not compare `output` to `expected` for you, since there are so many different ways to do that correctly. Instead, these values are just used to help you navigate your experiments while digging into analyses. However, we may later use these values to re-score outputs or fine-tune your models"
),
scores: experimentEventBaseSchema.shape.scores.describe(
"A dictionary of numeric values (between 0 and 1) to log. The scores should give you a variety of signals that help you determine how accurate the outputs are compared to what you expect and diagnose failures. For example, a summarization app might have one score that tells you how accurate the summary is, and another that measures the word similarity between the generated and grouth truth summary. The word similarity score could help you determine whether the summarization was covering similar concepts or not. You can use these scores to help you sort, filter, and compare experiments"
),
metadata: experimentEventBaseSchema.shape.metadata,
metrics: experimentEventBaseSchema.shape.metrics,
context: experimentEventBaseSchema.shape.context,
span_id: experimentEventBaseSchema.shape.span_id,
span_parents: experimentEventBaseSchema.shape.span_parents,
root_span_id: experimentEventBaseSchema.shape.root_span_id,
span_attributes: experimentEventBaseSchema.shape.span_attributes
}).strict().openapi("ExperimentEvent");
var datasetEventBaseSchema = generateBaseEventOpSchema("dataset");
var datasetEventSchema = import_zod4.z.object({
id: datasetEventBaseSchema.shape.id,
[TRANSACTION_ID_FIELD]: datasetEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: datasetEventBaseSchema.shape.created,
project_id: datasetSchema.shape.project_id,
dataset_id: datasetSchema.shape.id,
input: datasetEventBaseSchema.shape.input.describe(
"The argument that uniquely define an input case (an arbitrary, JSON serializable object)"
),
output: datasetEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object)"
),
metadata: datasetEventBaseSchema.shape.metadata,
span_id: datasetEventBaseSchema.shape.span_id,
root_span_id: datasetEventBaseSchema.shape.root_span_id
}).strict().openapi("DatasetEvent");
var projectLogsEventBaseSchema = generateBaseEventOpSchema("project");
var projectLogsEventSchema = import_zod4.z.object({
id: projectLogsEventBaseSchema.shape.id,
[TRANSACTION_ID_FIELD]: projectLogsEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: projectLogsEventBaseSchema.shape.created,
org_id: projectSchema.shape.org_id,
project_id: projectSchema.shape.id,
log_id: import_zod4.z.literal("g").describe("A literal 'g' which identifies the log as a project log"),
input: projectLogsEventBaseSchema.shape.input.describe(
"The arguments that uniquely define a user input(an arbitrary, JSON serializable object)."
),
output: projectLogsEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object), that allows you to determine whether the result is correct or not. For example, in an app that generates SQL queries, the `output` should be the _result_ of the SQL query generated by the model, not the query itself, because there may be multiple valid queries that answer a single question."
),
expected: projectLogsEventBaseSchema.shape.expected.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not. Braintrust currently does not compare `output` to `expected` for you, since there are so many different ways to do that correctly. Instead, these values are just used to help you navigate while digging into analyses. However, we may later use these values to re-score outputs or fine-tune your models."
),
scores: projectLogsEventBaseSchema.shape.scores.describe(
"A dictionary of numeric values (between 0 and 1) to log. The scores should give you a variety of signals that help you determine how accurate the outputs are compared to what you expect and diagnose failures. For example, a summarization app might have one score that tells you how accurate the summary is, and another that measures the word similarity between the generated and grouth truth summary. The word similarity score could help you determine whether the summarization was covering similar concepts or not. You can use these scores to help you sort, filter, and compare logs."
),
metadata: projectLogsEventBaseSchema.shape.metadata,
metrics: projectLogsEventBaseSchema.shape.metrics,
context: projectLogsEventBaseSchema.shape.context,
span_id: projectLogsEventBaseSchema.shape.span_id,
span_parents: projectLogsEventBaseSchema.shape.span_parents,
root_span_id: projectLogsEventBaseSchema.shape.root_span_id,
span_attributes: projectLogsEventBaseSchema.shape.span_attributes
}).strict().openapi("ProjectLogsEvent");
var isMergeDescription = [
"The `_is_merge` field controls how the row is merged with any existing row with the same id in the DB. By default (or when set to `false`), the existing row is completely replaced by the new row. When set to `true`, the new row is deep-merged into the existing row",
'For example, say there is an existing row in the DB `{"id": "foo", "input": {"a": 5, "b": 10}}`. If we merge a new row as `{"_is_merge": true, "id": "foo", "input": {"b": 11, "c": 20}}`, the new row will be `{"id": "foo", "input": {"a": 5, "b": 11, "c": 20}}`. If we replace the new row as `{"id": "foo", "input": {"b": 11, "c": 20}}`, the new row will be `{"id": "foo", "input": {"b": 11, "c": 20}}`'
].join("\n\n");
var mergeEventSchema = import_zod4.z.object({
[IS_MERGE_FIELD]: customTypes.literalTrue.describe(isMergeDescription),
[MERGE_PATHS_FIELD]: import_zod4.z.string().array().array().nullish().describe(
[
"The `_merge_paths` field allows controlling the depth of the merge. It can only be specified alongside `_is_merge=true`. `_merge_paths` is a list of paths, where each path is a list of field names. The deep merge will not descend below any of the specified merge paths.",
'For example, say there is an existing row in the DB `{"id": "foo", "input": {"a": {"b": 10}, "c": {"d": 20}}, "output": {"a": 20}}`. If we merge a new row as `{"_is_merge": true, "_merge_paths": [["input", "a"], ["output"]], "input": {"a": {"q": 30}, "c": {"e": 30}, "bar": "baz"}, "output": {"d": 40}}`, the new row will be `{"id": "foo": "input": {"a": {"q": 30}, "c": {"d": 20, "e": 30}, "bar": "baz"}, "output": {"d": 40}}`. In this case, due to the merge paths, we have replaced `input.a` and `output`, but have still deep-merged `input` and `input.c`.'
].join("\n\n")
)
});
var replacementEventSchema = import_zod4.z.object({
[IS_MERGE_FIELD]: customTypes.literalFalse.nullish().describe(isMergeDescription),
[PARENT_ID_FIELD]: import_zod4.z.string().nullish().describe(
[
"Use the `_parent_id` field to create this row as a subspan of an existing row. It cannot be specified alongside `_is_merge=true`. Tracking hierarchical relationships are important for tracing (see the [guide](https://www.braintrustdata.com/docs/guides/tracing) for full details).",
'For example, say we have logged a row `{"id": "abc", "input": "foo", "output": "bar", "expected": "boo", "scores": {"correctness": 0.33}}`. We can create a sub-span of the parent row by logging `{"_parent_id": "abc", "id": "llm_call", "input": {"prompt": "What comes after foo?"}, "output": "bar", "metrics": {"tokens": 1}}`. In the webapp, only the root span row `"abc"` will show up in the summary view. You can view the full trace hierarchy (in this case, the `"llm_call"` row) by clicking on the "abc" row.'
].join("\n\n")
)
});
function makeInsertEventSchemas(objectType, insertSchema) {
const eventDescription = getEventObjectDescription(objectType);
const article = getEventObjectArticle(objectType);
const eventSchemaName = capitalize(
getEventObjectType(objectType),
"_"
).replace("_", "");
const replaceVariantSchema = insertSchema.merge(replacementEventSchema).strict().openapi(`Insert${eventSchemaName}EventReplace`);
const mergeVariantSchema = insertSchema.merge(mergeEventSchema).strict().openapi(`Insert${eventSchemaName}EventMerge`);
const eventSchema = import_zod4.z.union([replaceVariantSchema, mergeVariantSchema]).describe(
`${capitalize(article)} ${eventDescription} event`
).openapi(`Insert${eventSchemaName}Event`);
const requestSchema = import_zod4.z.object({
events: eventSchema.array().describe(`A list of ${eventDescription} events to insert`)
}).strict().openapi(`Insert${eventSchemaName}EventRequest`);
return { eventSchema, requestSchema };
}
var insertEventsResponseSchema = import_zod4.z.object({
row_ids: import_zod4.z.string().array().describe(
"The ids of all rows that were inserted, aligning one-to-one with the rows provided as input"
)
}).strict().openapi("InsertEventsResponse");
var {
eventSchema: insertExperimentEventSchema,
requestSchema: insertExperimentEventsRequestSchema
} = makeInsertEventSchemas(
"experiment",
import_zod4.z.object({
input: experimentEventSchema.shape.input,
output: experimentEventSchema.shape.output,
expected: experimentEventSchema.shape.expected,
scores: experimentEventSchema.shape.scores,
metadata: experimentEventSchema.shape.metadata,
metrics: experimentEventSchema.shape.metrics,
context: experimentEventSchema.shape.context,
span_attributes: experimentEventSchema.shape.span_attributes,
id: experimentEventSchema.shape.id.nullish(),
dataset_record_id: experimentEventSchema.shape.dataset_record_id,
[OBJECT_DELETE_FIELD]: experimentEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
var {
eventSchema: insertDatasetEventSchema,
requestSchema: insertDatasetEventsRequestSchema
} = makeInsertEventSchemas(
"dataset",
import_zod4.z.object({
input: datasetEventSchema.shape.input,
output: datasetEventSchema.shape.output,
metadata: datasetEventSchema.shape.metadata,
id: datasetEventSchema.shape.id.nullish(),
[OBJECT_DELETE_FIELD]: datasetEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
var {
eventSchema: insertProjectLogsEventSchema,
requestSchema: insertProjectLogsEventsRequestSchema
} = makeInsertEventSchemas(
"project",
import_zod4.z.object({
input: projectLogsEventSchema.shape.input,
output: projectLogsEventSchema.shape.output,
expected: projectLogsEventSchema.shape.expected,
scores: projectLogsEventSchema.shape.scores,
metadata: projectLogsEventSchema.shape.metadata,
metrics: projectLogsEventSchema.shape.metrics,
context: projectLogsEventSchema.shape.context,
span_attributes: projectLogsEventSchema.shape.span_attributes,
id: projectLogsEventSchema.shape.id.nullish(),
[OBJECT_DELETE_FIELD]: projectLogsEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
function makeFeedbackRequestSchema(objectType, feedbackSchema) {
const eventDescription = getEventObjectDescription(objectType);
const eventSchemaName = capitalize(
getEventObjectType(objectType),
"_"
).replace("_", "");
return import_zod4.z.object({
feedback: feedbackSchema.array().describe(`A list of ${eventDescription} feedback items`)
}).strict().openapi(`Feedback${eventSchemaName}EventRequest`);
}
var feedbackExperimentRequestBaseSchema = generateBaseEventFeedbackSchema("experiment");
var feedbackExperimentItemSchema = import_zod4.z.object({
id: feedbackExperimentRequestBaseSchema.shape.id,
scores: feedbackExperimentRequestBaseSchema.shape.scores,
expected: feedbackExperimentRequestBaseSchema.shape.expected,
comment: feedbackExperimentRequestBaseSchema.shape.comment,
metadata: feedbackExperimentRequestBaseSchema.shape.metadata,
source: feedbackExperimentRequestBaseSchema.shape.source
}).strict().openapi("FeedbackExperimentItem");
var feedbackExperimentRequestSchema = makeFeedbackRequestSchema(
"experiment",
feedbackExperimentItemSchema
);
var feedbackDatasetRequestBaseSchema = generateBaseEventFeedbackSchema("dataset");
var feedbackDatasetItemSchema = import_zod4.z.object({
id: feedbackDatasetRequestBaseSchema.shape.id,
comment: feedbackDatasetRequestBaseSchema.shape.comment,
metadata: feedbackDatasetRequestBaseSchema.shape.metadata,
source: feedbackDatasetRequestBaseSchema.shape.source
}).strict().openapi("FeedbackDatasetItem");
var feedbackDatasetRequestSchema = makeFeedbackRequestSchema(
"dataset",
feedbackDatasetItemSchema
);
var feedbackProjectLogsRequestBaseSchema = generateBaseEventFeedbackSchema("project");
var feedbackProjectLogsItemSchema = import_zod4.z.object({
id: feedbackProjectLogsRequestBaseSchema.shape.id,
scores: feedbackProjectLogsRequestBaseSchema.shape.scores,
expected: feedbackProjectLogsRequestBaseSchema.shape.expected,
comment: feedbackProjectLogsRequestBaseSchema.shape.comment,
metadata: feedbackProjectLogsRequestBaseSchema.shape.metadata,
source: feedbackProjectLogsRequestBaseSchema.shape.source
}).strict().openapi("FeedbackProjectLogsItem");
var feedbackProjectLogsRequestSchema = makeFeedbackRequestSchema(
"project",
feedbackProjectLogsItemSchema
);
var eventObjectSchemas = {
experiment: {
fetchResponse: makeFetchEventsResponseSchema(
"experiment",
experimentEventSchema
),
insertEvent: insertExperimentEventSchema,
insertRequest: insertExperimentEventsRequestSchema,
feedbackItem: feedbackExperimentItemSchema,
feedbackRequest: feedbackExperimentRequestSchema
},
dataset: {
fetchResponse: makeFetchEventsResponseSchema("dataset", datasetEventSchema),
insertEvent: insertDatasetEventSchema,
insertRequest: insertDatasetEventsRequestSchema,
feedbackItem: feedbackDatasetItemSchema,
feedbackRequest: feedbackDatasetRequestSchema
},
project_logs: {
fetchResponse: makeFetchEventsResponseSchema(
"project",
projectLogsEventSchema
),
insertEvent: insertProjectLogsEventSchema,
insertRequest: insertProjectLogsEventsRequestSchema,
feedbackItem: feedbackProjectLogsItemSchema,
feedbackRequest: feedbackProjectLogsRequestSchema
}
};
function makeCrossObjectIndividualRequestSchema(objectType) {
const eventObjectType = getEventObjectType(objectType);
const eventDescription = getEventObjectDescription(objectType);
const eventObjectSchema = eventObjectSchemas[eventObjectType];
const insertObject = import_zod4.z.object({
events: eventObjectSchema.insertEvent.array().nullish().describe(`A list of ${eventDescription} events to insert`),
feedback: eventObjectSchema.feedbackItem.array().nullish().describe(`A list of ${eventDescription} feedback items`)
}).strict();
return import_zod4.z.record(import_zod4.z.string().uuid(), insertObject).nullish().describe(`A mapping from ${objectType} id to a set of log events and feedback items to insert`);
}
function makeCrossObjectIndividualResponseSchema(objectType) {
return import_zod4.z.record(import_zod4.z.string().uuid(), insertEventsResponseSchema).nullish().describe(`A mapping from ${objectType} id to row ids for inserted \`events\``);
}
var crossObjectInsertRequestSchema = import_zod4.z.object({
experiment: makeCrossObjectIndividualRequestSchema("experiment"),
dataset: makeCrossObjectIndividualRequestSchema("dataset"),
project_logs: makeCrossObjectIndividualRequestSchema("project")
}).strict().openapi("CrossObjectInsertRequest");
var crossObjectInsertResponseSchema = import_zod4.z.object({
experiment: makeCrossObjectIndividualResponseSchema("experiment"),
dataset: makeCrossObjectIndividualResponseSchema("dataset"),
project_logs: makeCrossObjectIndividualResponseSchema("project")
}).strict().openapi("CrossObjectInsertResponse");
var summarizeScoresParamSchema = import_zod4.z.boolean().describe("Whether to summarize the scores and metrics. If false (or omitted), only the metadata will be returned.");
var comparisonExperimentIdParamSchema = import_zod4.z.string().uuid().describe("The experiment to compare against, if summarizing scores and metrics. If omitted, will fall back to the `base_exp_id` stored in the experiment metadata, and then to the most recent experiment run in the same project. Must pass `summarize_scores=true` for this id to be used");
var summarizeDataParamSchema = import_zod4.z.boolean().describe("Whether to summarize the data. If false (or omitted), only the metadata will be returned.");
var summarizeExperimentResponseSchema = import_zod4.z.object({
project_name: import_zod4.z.string().describe("Name of the project that the experiment belongs to"),
experiment_name: import_zod4.z.string().describe("Name of the experiment"),
project_url: import_zod4.z.string().url().describe("URL to the project's page in the Braintrust app"),
experiment_url: import_zod4.z.string().url().describe("URL to the experiment's page in the Braintrust app"),
comparison_experiment_name: import_zod4.z.string().nullish().describe("The experiment which scores are baselined against"),
scores: import_zod4.z.record(import_zod4.z.object({
name: import_zod4.z.string().describe("Name of the score"),
score: import_zod4.z.number().min(0).max(1).describe("Average score across all examples"),
diff: import_zod4.z.number().min(-1).max(1).describe("Difference in score between the current and comparison experiment"),
improvements: import_zod4.z.number().int().min(0).describe("Number of improvements in the score"),
regressions: import_zod4.z.number().int().min(0).describe("Number of regressions in the score")
}).describe("Summary of a score's performance").openapi("ScoreSummary")).nullish().describe("Summary of the experiment's scores"),
metrics: import_zod4.z.record(import_zod4.z.object({
name: import_zod4.z.string().describe("Name of the metric"),
metric: import_zod4.z.number().describe("Average metric across all examples"),
unit: import_zod4.z.string().describe("Unit label for the metric"),
diff: import_zod4.z.number().describe("Difference in metric between the current and comparison experiment"),
improvements: import_zod4.z.number().int().min(0).describe("Number of improvements in the metric"),
regressions: import_zod4.z.number().int().min(0).describe("Number of regressions in the metric")
}).describe("Summary of a metric's performance").openapi("MetricSummary")).nullish().describe("Summary of the experiment's metrics")
}).strict().describe("Summary of an experiment").openapi("SummarizeExperimentResponse");
var summarizeDatasetResponseSchema = import_zod4.z.object({
project_name: import_zod4.z.string().describe("Name of the project that the dataset belongs to"),
dataset_name: import_zod4.z.string().describe("Name of the dataset"),
project_url: import_zod4.z.string().url().describe("URL to the project's page in the Braintrust app"),
dataset_url: import_zod4.z.string().url().describe("URL to the dataset's page in the Braintrust app"),
data_summary: import_zod4.z.object({
total_records: import_zod4.z.number().int().min(0).describe("Total number of records in the dataset")
}).nullish().describe("Summary of a dataset's data").openapi("DataSummary")
}).strict().describe("Summary of a dataset").openapi("SummarizeDatasetResponse");
var objectTypeSummarizeResponseSchemas = {
experiment: summarizeExperimentResponseSchema,
dataset: summarizeDatasetResponseSchema,
project: void 0
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
allFetchFiltersSchema,
apiKeySchema,
appLimitSchema,
auditSourcesSchema,
comparisonExperimentIdParamSchema,
crossObjectInsertRequestSchema,
crossObjectInsertResponseSchema,
datasetSchema,
datetimeStringSchema,
endingBeforeSchema,
eventObjectSchemas,
experimentSchema,
fetchEventsRequestSchema,
fetchFiltersSchema,
fetchLimitSchema,
getEventObjectArticle,
getEventObjectDescription,
getEventObjectType,
insertEventsResponseSchema,
jsonSchema,
literalSchema,
maxRootSpanIdSchema,
maxXactIdSchema,
meSchema,
memberSchema,
objectSchemas,
objectTypeSummarizeResponseSchemas,
objectTypes,
organizationSchema,
projectSchema,
startingAfterSchema,
summarizeDataParamSchema,
summarizeScoresParamSchema,
userSchema,
versionSchema
});
// src/typespecs/api_types.ts
import { extendZodWithOpenApi as extendZodWithOpenApi3 } from "@asteasolutions/zod-to-openapi";
import { z as z4 } from "zod";
// src/typespecs/app_types.ts
import { extendZodWithOpenApi as extendZodWithOpenApi2 } from "@asteasolutions/zod-to-openapi";
import { z as z3 } from "zod";
// src/typespecs/common_types.ts
import { z } from "zod";
var literalSchema = z.union([
z.string(),
z.number(),
z.boolean(),
z.null()
]);
var jsonSchema = z.lazy(
() => z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
);
var datetimeStringSchema = z.string().datetime({ offset: true });
var objectTypes = ["project", "experiment", "dataset"];
function getEventObjectType(objectType) {
return objectType === "project" ? "project_logs" : objectType;
}
function getEventObjectDescription(objectType) {
return getEventObjectType(objectType).replace("_", " ");
}
function getEventObjectArticle(objectType) {
return objectType === "experiment" ? "an" : "a";
}
// src/typespecs/custom_types.ts
import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
import { z as z2 } from "zod";
extendZodWithOpenApi(z2);
var modeSchema = z2.enum(["default", "stainless"]);
var _a;
var mode = modeSchema.parse(
typeof process === "undefined" ? "default" : ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.BRAINTRUST_TYPESPECS_MODE) || "default"
);
var modeToTypes = {
default: {
any: z2.any(),
literalTrue: z2.literal(true),
literalFalse: z2.literal(false)
},
stainless: {
// Stainless requires schemas which are completely permissive to be
// tagged.
any: z2.any().openapi({ ["x-stainless-any"]: true }),
// Stainless does not support boolean literals in all SDKs.
literalTrue: z2.boolean(),
literalFalse: z2.boolean()
}
};
var customTypes = modeToTypes[mode];
// src/typespecs/app_types.ts
extendZodWithOpenApi2(z3);
function generateBaseTableSchema(objectName, opts) {
let nameDescription = `Name of the ${objectName}`;
if (opts == null ? void 0 : opts.underProject) {
nameDescription += `. Within a project, ${objectName} names are unique`;
}
return z3.object({
id: z3.string().uuid().describe(`Unique identifier for the ${objectName}`),
project_id: z3.string().uuid().describe(
`Unique identifier for the project that the ${objectName} belongs under`
),
name: z3.string().describe(nameDescription),
description: z3.string().nullish().describe(`Textual description of the ${objectName}`),
created: datetimeStringSchema.nullish().describe(`Date of ${objectName} creation`),
deleted_at: datetimeStringSchema.nullish().describe(
`Date of ${objectName} deletion, or null if the ${objectName} is still active`
),
user_id: z3.string().uuid().nullish().describe(`Identifies the user who created the ${objectName}`),
metadata: z3.record(customTypes.any).nullish().describe(`User-controlled metadata about the ${objectName}`)
});
}
var userBaseSchema = generateBaseTableSchema("user");
var userSchema = z3.object({
id: userBaseSchema.shape.id,
auth_id: z3.string().uuid().nullish().describe("Internal authentication token used to identify the user"),
given_name: z3.string().nullish().describe("Given name of the user"),
family_name: z3.string().nullish().describe("Family name of the user"),
email: z3.string().nullish().describe("The user's email"),
avatar_url: z3.string().nullish().describe("URL of the user's Avatar image"),
created: userBaseSchema.shape.created
}).strict().openapi("User");
var organizationBaseSchema = generateBaseTableSchema("organization");
var organizationSchema = z3.object({
id: organizationBaseSchema.shape.id,
name: organizationBaseSchema.shape.name.nullish(),
api_url: z3.string().nullish(),
created: organizationBaseSchema.shape.created
}).strict().openapi("Organization");
var memberSchema = z3.object({
org_id: organizationSchema.shape.id,
user_id: userSchema.shape.id
}).strict().openapi("Member");
var meSchema = z3.object({
id: userSchema.shape.id,
// By filtering by auth_id equality, we will ensure this is not-null.
auth_id: userSchema.shape.auth_id.unwrap().unwrap(),
organizations: z3.object({
id: memberSchema.shape.org_id,
name: organizationSchema.shape.name
}).array()
}).strict().openapi("Me");
var apiKeyBaseSchema = generateBaseTableSchema("api key");
var apiKeySchema = z3.object({
id: apiKeyBaseSchema.shape.id,
created: apiKeyBaseSchema.shape.created,
key_hash: z3.string(),
name: apiKeyBaseSchema.shape.name,
preview_name: z3.string(),
user_id: userSchema.shape.id.nullish(),
org_id: organizationSchema.shape.id.nullish()
}).strict().openapi("ApiKey");
var projectBaseSchema = generateBaseTableSchema("project");
var projectSchema = z3.object({
id: projectBaseSchema.shape.id,
org_id: z3.string().uuid().describe(
"Unique id for the organization that the project belongs under"
),
name: projectBaseSchema.shape.name,
created: projectBaseSchema.shape.created,
deleted_at: projectBaseSchema.shape.deleted_at,
user_id: projectBaseSchema.shape.user_id
}).strict().openapi("Project");
var datasetBaseSchema = generateBaseTableSchema("dataset", {
underProject: true
});
var datasetSchema = z3.object({
id: datasetBaseSchema.shape.id,
project_id: datasetBaseSchema.shape.project_id.nullish(),
name: datasetBaseSchema.shape.name,
description: datasetBaseSchema.shape.description,
created: datasetBaseSchema.shape.created,
deleted_at: datasetBaseSchema.shape.deleted_at,
user_id: datasetBaseSchema.shape.user_id
}).strict().openapi("Dataset");
var repoInfoSchema = z3.object({
commit: z3.string().nullish().describe("SHA of most recent commit"),
branch: z3.string().nullish().describe("Name of the branch the most recent commit belongs to"),
tag: z3.string().nullish().describe("Name of the tag on the most recent commit"),
dirty: z3.boolean().nullish().describe(
"Whether or not the repo had uncommitted changes when snapshotted"
),
author_name: z3.string().nullish().describe("Name of the author of the most recent commit"),
author_email: z3.string().nullish().describe("Email of the author of the most recent commit"),
commit_message: z3.string().nullish().describe("Most recent commit message"),
commit_time: z3.string().nullish().describe("Time of the most recent commit"),
git_diff: z3.string().nullish().describe(
"If the repo was dirty when run, this includes the diff between the current state of the repo and the most recent commit."
)
}).describe(
"Metadata about the state of the repo when the experiment was created"
).openapi("RepoInfo");
var experimentBaseSchema = generateBaseTableSchema("experiment", {
underProject: true
});
var experimentSchema = z3.object({
id: experimentBaseSchema.shape.id,
project_id: experimentBaseSchema.shape.project_id,
name: experimentBaseSchema.shape.name,
description: experimentBaseSchema.shape.description,
created: experimentBaseSchema.shape.created,
repo_info: repoInfoSchema.nullish(),
commit: z3.string().nullish().describe("Commit, taken directly from `repo_info.commit`"),
base_exp_id: z3.string().uuid().nullish().describe(
"Id of default base experiment to compare against when viewing this experiment"
),
deleted_at: experimentBaseSchema.shape.deleted_at,
dataset_id: z3.string().uuid().nullish().describe(
"Identifier of the linked dataset, or null if the experiment is not linked to a dataset"
),
dataset_version: z3.string().nullish().describe(
"Version number of the linked dataset the experiment was run against. This can be used to reproduce the experiment after the dataset has been modified."
),
public: z3.boolean().describe(
"Whether or not the experiment is public. Public experiments can be viewed by anybody inside or outside the organization"
),
user_id: experimentBaseSchema.shape.user_id,
metadata: experimentBaseSchema.shape.metadata
}).strict().openapi("Experiment");
var appLimitSchema = z3.number().int().nonnegative().describe("Limit the number of objects to return");
function generateBaseTableOpSchema(objectName) {
return z3.object({
org_name: z3.string().nullish().describe(
`For nearly all users, this parameter should be unnecessary. But in the rare case that your API key belongs to multiple organizations, you may specify the name of the organization the ${objectName} belongs in.`
)
});
}
var startingAfterSchema = z3.string().uuid().describe(
[
"Pagination cursor id.",
"For example, if the final item in the last page you fetched had an id of `foo`, pass `starting_after=foo` to fetch the next page. Note: you may only pass one of `starting_after` and `ending_before`"
].join("\n\n")
).openapi("StartingAfter");
var endingBeforeSchema = z3.string().uuid().describe(
[
"Pagination cursor id.",
"For example, if the initial item in the last page you fetched had an id of `foo`, pass `ending_before=foo` to fetch the previous page. Note: you may only pass one of `starting_after` and `ending_before`"
].join("\n\n")
).openapi("EndingBefore");
var createProjectBaseSchema = generateBaseTableOpSchema("project");
var createProjectSchema = z3.object({
name: projectSchema.shape.name,
org_name: createProjectBaseSchema.shape.org_name
}).strict().openapi("CreateProject");
var patchProjectSchema = z3.object({
name: projectSchema.shape.name.nullish()
}).strict().openapi("PatchProject");
var createExperimentSchema = z3.object({
project_id: experimentSchema.shape.project_id,
name: experimentSchema.shape.name.nullish(),
description: experimentSchema.shape.description,
repo_info: experimentSchema.shape.repo_info,
base_exp_id: experimentSchema.shape.base_exp_id,
dataset_id: experimentSchema.shape.dataset_id,
dataset_version: experimentSchema.shape.dataset_version,
public: experimentSchema.shape.public.nullish(),
metadata: experimentSchema.shape.metadata
}).strict().openapi("CreateExperiment");
var patchExperimentSchema = createExperimentSchema.omit({ project_id: true }).strict().openapi("PatchExperiment");
var createDatasetSchema = z3.object({
project_id: datasetSchema.shape.project_id,
name: datasetSchema.shape.name,
description: datasetSchema.shape.description
}).strict().openapi("CreateDataset");
var patchDatasetSchema = createDatasetSchema.omit({ project_id: true }).strict().openapi("PatchDataset");
var objectSchemas = {
experiment: {
create: createExperimentSchema,
patch: patchExperimentSchema,
object: experimentSchema
},
dataset: {
create: createDatasetSchema,
patch: patchDatasetSchema,
object: datasetSchema
},
project: {
create: createProjectSchema,
patch: patchProjectSchema,
object: projectSchema
}
};
// src/util.ts
function capitalize(s, sep) {
const items = sep ? s.split(sep) : [s];
return items.map((s2) => s2 ? s2.charAt(0).toUpperCase() + s2.slice(1) : s2).join(sep || "");
}
// src/db_fields.ts
var TRANSACTION_ID_FIELD = "_xact_id";
var OBJECT_DELETE_FIELD = "_object_delete";
var IS_MERGE_FIELD = "_is_merge";
var MERGE_PATHS_FIELD = "_merge_paths";
var VALID_SOURCES = ["app", "api", "external"];
var PARENT_ID_FIELD = "_parent_id";
// src/span_types.ts
var SpanTypeAttribute = /* @__PURE__ */ ((SpanTypeAttribute2) => {
SpanTypeAttribute2["LLM"] = "llm";
SpanTypeAttribute2["SCORE"] = "score";
SpanTypeAttribute2["FUNCTION"] = "function";
SpanTypeAttribute2["EVAL"] = "eval";
SpanTypeAttribute2["TASK"] = "task";
SpanTypeAttribute2["TOOL"] = "tool";
return SpanTypeAttribute2;
})(SpanTypeAttribute || {});
// src/typespecs/api_types.ts
extendZodWithOpenApi3(z4);
var auditSourcesSchema = z4.enum(VALID_SOURCES);
function generateBaseEventOpSchema(objectType) {
const eventDescription = getEventObjectDescription(objectType);
return z4.object({
id: z4.string().describe(
`A unique identifier for the ${eventDescription} event. If you don't provide one, BrainTrust will generate one for you`
),
[TRANSACTION_ID_FIELD]: z4.bigint().describe(
`The transaction id of an event is unique to the network operation that processed the event insertion. Transaction ids are monotonically increasing over time and can be used to retrieve a versioned snapshot of the ${eventDescription} (see the \`version\` parameter)`
),
created: datetimeStringSchema.nullish().describe(`The timestamp the ${eventDescription} event was created`),
input: customTypes.any,
output: customTypes.any,
expected: customTypes.any,
scores: z4.record(z4.number().min(0).max(1).nullish()).nullish(),
metadata: z4.record(customTypes.any).nullish().describe(
"A dictionary with additional data about the test example, model outputs, or just about anything else that's relevant, that you can use to help find and analyze examples later. For example, you could log the `prompt`, example's `id`, or anything else that would be useful to slice/dice later. The values in `metadata` can be any JSON-serializable type, but its keys must be strings"
),
metrics: z4.object({
start: z4.number().nullish().describe(
`A unix timestamp recording when the section of code which produced the ${eventDescription} event started`
),
end: z4.number().nullish().describe(
`A unix timestamp recording when the section of code which produced the ${eventDescription} event finished`
)
}).catchall(customTypes.any).nullish().describe(
`Metrics are numerical measurements tracking the execution of the code that produced the ${eventDescription} event. Use "start" and "end" to track the time span over which the ${eventDescription} event was produced`
),
context: z4.object({
caller_functionname: z4.string().nullish().describe(
`The function in code which created the ${eventDescription} event`
),
caller_filename: z4.string().nullish().describe(
`Name of the file in code where the ${eventDescription} event was created`
),
caller_lineno: z4.number().int().nullish().describe(
`Line of code where the ${eventDescription} event was created`
)
}).catchall(customTypes.any).nullish().describe(
`Context is additional information about the code that produced the ${eventDescription} event. It is essentially the textual counterpart to \`metrics\`. Use the \`caller_*\` attributes to track the location in code which produced the ${eventDescription} event`
),
span_id: z4.string().describe(
`A unique identifier used to link different ${eventDescription} events together as part of a full trace. See the [tracing guide](https://www.braintrustdata.com/docs/guides/tracing) for full details on tracing`
),
span_parents: z4.string().array().nullish().describe(
`An array of the parent \`span_ids\` of this ${eventDescription} event. This should be empty for the root span of a trace, and should most often contain just one parent element for subspans`
),
root_span_id: z4.string().describe(
`The \`span_id\` of the root of the trace this ${eventDescription} event belongs to`
),
span_attributes: z4.object({
name: z4.string().nullish().describe("Name of the span, for display purposes only"),
type: z4.nativeEnum(SpanTypeAttribute).nullish().describe("Type of the span, for display purposes only")
}).catchall(customTypes.any).nullish().describe(
"Human-identifying attributes of the span, such as name, type, etc."
),
[OBJECT_DELETE_FIELD]: z4.boolean().nullish().describe(
`Pass \`${OBJECT_DELETE_FIELD}=true\` to mark the ${eventDescription} event deleted. Deleted events will not show up in subsequent fetches for this ${eventDescription}`
)
});
}
function generateBaseEventFeedbackSchema(objectType) {
const eventObjectType = getEventObjectType(objectType);
const eventDescription = getEventObjectDescription(objectType);
return z4.object({
id: z4.string().describe(
`The id of the ${eventDescription} event to log feedback for. This is the row \`id\` returned by \`POST /v1/${eventObjectType}/{${objectType}_id}/insert\``
),
scores: z4.record(z4.number().min(0).max(1).nullish()).nullish().describe(
`A dictionary of numeric values (between 0 and 1) to log. These scores will be merged into the existing scores for the ${eventDescription} event`
),
expected: customTypes.any.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not"
),
comment: z4.string().nullish().describe(
`An optional comment string to log about the ${eventDescription} event`
),
metadata: z4.record(customTypes.any).nullish().describe(
"A dictionary with additional data about the feedback. If you have a `user_id`, you can log it here and access it in the Braintrust UI."
),
source: auditSourcesSchema.nullish().describe(
'The source of the feedback. Must be one of "external" (default), "app", or "api"'
)
});
}
var fetchLimitSchema = z4.number().int().nonnegative().describe(
[
"limit the number of traces fetched",
`Fetch queries may be paginated if the total result size is expected to be large (e.g. project_logs which accumulate over a long time). Note that fetch queries only support pagination in descending time order (from latest to earliest \`${TRANSACTION_ID_FIELD}\`. Furthermore, later pages may return rows which showed up in earlier pages, except with an earlier \`${TRANSACTION_ID_FIELD}\`. This happens because pagination occurs over the whole version history of the event log. You will most likely want to exclude any such duplicate, outdated rows (by \`id\`) from your combined result set.`,
`The \`limit\` parameter controls the number of full traces to return. So you may end up with more individual rows than the specified limit if you are fetching events containing traces.`
].join("\n\n")
);
var maxXactIdSchema = z4.bigint().describe(
[
"Pagination cursor transaction ID, combined with `max_root_span_id`",
`Given a previous fetch with a list of rows, you can determine \`max_xact_id\` as the maximum of the \`${TRANSACTION_ID_FIELD}\` field over all rows. See the documentation for \`limit\` for an overview of paginating fetch queries.`
].join("\n\n")
);
var maxRootSpanIdSchema = z4.string().describe(
[
"Pagination cursor transaction root span ID, combined with `max_xact_id`",
`Given a previous fetch with a list of rows, you can determine \`max_root_span_id\` as the maximum of the \`root_span_id\` field over all rows. See the documentation for \`limit\` for an overview of paginating fetch queries.`
].join("\n\n")
);
var versionSchema = z4.bigint().describe(
[
"Retrieve a snapshot of events from a past time",
"The version id is essentially a filter on the latest event transaction id. You can use the `max_xact_id` returned by a past fetch as the version to reproduce that exact fetch."
].join("\n\n")
);
var pathTypeFilterSchema = z4.object({
type: z4.literal("path_lookup").describe("Denotes the type of filter as a path-lookup filter"),
path: z4.string().array().describe(
'List of fields describing the path to the value to be checked against. For instance, if you wish to filter on the value of `c` in `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `path=["input", "a", "b", "c"]`'
),
value: customTypes.any.describe(
'The value to compare equality-wise against the event value at the specified `path`. The value must be a "primitive", that is, any JSON-serializable object except for objects and arrays. For instance, if you wish to filter on the value of "input.a.b.c" in the object `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `value="hello"`'
)
}).describe(
'A path-lookup filter describes an equality comparison against a specific sub-field in the event row. For instance, if you wish to filter on the value of `c` in `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `path=["input", "a", "b", "c"]` and `value="hello"`'
).openapi("PathLookupFilter");
var sqlTypeFilterSchema = z4.object({
type: z4.literal("sql_filter").describe("Denotes the type of filter as a sql-type filter"),
expr: z4.string().describe(
`A SQL expression in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). For instance, if you wish to fuzzy-match the value of \`c\` in \`{"input": {"a": {"b": {"c": "hello"}}}}\`, pass \`expr="input->'a'->'b'->>'c' LIKE '%el%'"\`.`
)
}).describe(
`A sql-type filter describes a general filter over an individual row in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). For instance, if you wish to fuzzy-match the value of \`c\` in \`{"input": {"a": {"b": {"c": "hello"}}}}\`, pass \`expr="input->'a'->'b'->>'c' LIKE '%el%'"\`.`
).openapi("SQLFilter");
var allFetchFiltersSchema = z4.union([pathTypeFilterSchema, sqlTypeFilterSchema]).array().describe(
"A list of filters on the events to fetch. Filters can either be specialized `path=value` expressions or general SQL expressions in [duckDB syntax](https://duckdb.org/docs/sql/expressions/overview). When possible, prefer path-lookup type filters over general SQL-type filters, as they are likely to activate indices in the DB and run faster"
).openapi("AllFetchEventsFilters");
var fetchFiltersSchema = pathTypeFilterSchema.array().describe(
"A list of filters on the events to fetch. Currently, only path-lookup type filters are supported, but we may add more in the future"
).openapi("FetchEventsFilters");
var fetchEventsRequestSchema = z4.object({
limit: fetchLimitSchema.nullish(),
max_xact_id: maxXactIdSchema.nullish(),
max_root_span_id: maxRootSpanIdSchema.nullish(),
filters: fetchFiltersSchema.nullish(),
version: versionSchema.nullish()
}).strict().openapi("FetchEventsRequest");
function makeFetchEventsResponseSchema(objectType, eventSchema) {
const eventName = capitalize(getEventObjectType(objectType), "_").replace(
"_",
""
);
return z4.object({
events: eventSchema.array().describe("A list of fetched events")
}).strict().openapi(`Fetch${eventName}EventsResponse`);
}
var experimentEventBaseSchema = generateBaseEventOpSchema("experiment");
var experimentEventSchema = z4.object({
id: experimentEventBaseSchema.shape.id,
dataset_record_id: z4.string().nullish().describe(
"If the experiment is associated to a dataset, this is the event-level dataset id this experiment event is tied to"
),
[TRANSACTION_ID_FIELD]: experimentEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: experimentEventBaseSchema.shape.created,
project_id: experimentSchema.shape.project_id,
experiment_id: experimentSchema.shape.id,
input: experimentEventBaseSchema.shape.input.describe(
"The arguments that uniquely define a test case (an arbitrary, JSON serializable object). Later on, Braintrust will use the `input` to know whether two test cases are the same between experiments, so they should not contain experiment-specific state. A simple rule of thumb is that if you run the same experiment twice, the `input` should be identical"
),
output: experimentEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object), that allows you to determine whether the result is correct or not. For example, in an app that generates SQL queries, the `output` should be the _result_ of the SQL query generated by the model, not the query itself, because there may be multiple valid queries that answer a single question"
),
expected: experimentEventBaseSchema.shape.expected.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not. Braintrust currently does not compare `output` to `expected` for you, since there are so many different ways to do that correctly. Instead, these values are just used to help you navigate your experiments while digging into analyses. However, we may later use these values to re-score outputs or fine-tune your models"
),
scores: experimentEventBaseSchema.shape.scores.describe(
"A dictionary of numeric values (between 0 and 1) to log. The scores should give you a variety of signals that help you determine how accurate the outputs are compared to what you expect and diagnose failures. For example, a summarization app might have one score that tells you how accurate the summary is, and another that measures the word similarity between the generated and grouth truth summary. The word similarity score could help you determine whether the summarization was covering similar concepts or not. You can use these scores to help you sort, filter, and compare experiments"
),
metadata: experimentEventBaseSchema.shape.metadata,
metrics: experimentEventBaseSchema.shape.metrics,
context: experimentEventBaseSchema.shape.context,
span_id: experimentEventBaseSchema.shape.span_id,
span_parents: experimentEventBaseSchema.shape.span_parents,
root_span_id: experimentEventBaseSchema.shape.root_span_id,
span_attributes: experimentEventBaseSchema.shape.span_attributes
}).strict().openapi("ExperimentEvent");
var datasetEventBaseSchema = generateBaseEventOpSchema("dataset");
var datasetEventSchema = z4.object({
id: datasetEventBaseSchema.shape.id,
[TRANSACTION_ID_FIELD]: datasetEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: datasetEventBaseSchema.shape.created,
project_id: datasetSchema.shape.project_id,
dataset_id: datasetSchema.shape.id,
input: datasetEventBaseSchema.shape.input.describe(
"The argument that uniquely define an input case (an arbitrary, JSON serializable object)"
),
output: datasetEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object)"
),
metadata: datasetEventBaseSchema.shape.metadata,
span_id: datasetEventBaseSchema.shape.span_id,
root_span_id: datasetEventBaseSchema.shape.root_span_id
}).strict().openapi("DatasetEvent");
var projectLogsEventBaseSchema = generateBaseEventOpSchema("project");
var projectLogsEventSchema = z4.object({
id: projectLogsEventBaseSchema.shape.id,
[TRANSACTION_ID_FIELD]: projectLogsEventBaseSchema.shape[TRANSACTION_ID_FIELD],
created: projectLogsEventBaseSchema.shape.created,
org_id: projectSchema.shape.org_id,
project_id: projectSchema.shape.id,
log_id: z4.literal("g").describe("A literal 'g' which identifies the log as a project log"),
input: projectLogsEventBaseSchema.shape.input.describe(
"The arguments that uniquely define a user input(an arbitrary, JSON serializable object)."
),
output: projectLogsEventBaseSchema.shape.output.describe(
"The output of your application, including post-processing (an arbitrary, JSON serializable object), that allows you to determine whether the result is correct or not. For example, in an app that generates SQL queries, the `output` should be the _result_ of the SQL query generated by the model, not the query itself, because there may be multiple valid queries that answer a single question."
),
expected: projectLogsEventBaseSchema.shape.expected.describe(
"The ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not. Braintrust currently does not compare `output` to `expected` for you, since there are so many different ways to do that correctly. Instead, these values are just used to help you navigate while digging into analyses. However, we may later use these values to re-score outputs or fine-tune your models."
),
scores: projectLogsEventBaseSchema.shape.scores.describe(
"A dictionary of numeric values (between 0 and 1) to log. The scores should give you a variety of signals that help you determine how accurate the outputs are compared to what you expect and diagnose failures. For example, a summarization app might have one score that tells you how accurate the summary is, and another that measures the word similarity between the generated and grouth truth summary. The word similarity score could help you determine whether the summarization was covering similar concepts or not. You can use these scores to help you sort, filter, and compare logs."
),
metadata: projectLogsEventBaseSchema.shape.metadata,
metrics: projectLogsEventBaseSchema.shape.metrics,
context: projectLogsEventBaseSchema.shape.context,
span_id: projectLogsEventBaseSchema.shape.span_id,
span_parents: projectLogsEventBaseSchema.shape.span_parents,
root_span_id: projectLogsEventBaseSchema.shape.root_span_id,
span_attributes: projectLogsEventBaseSchema.shape.span_attributes
}).strict().openapi("ProjectLogsEvent");
var isMergeDescription = [
"The `_is_merge` field controls how the row is merged with any existing row with the same id in the DB. By default (or when set to `false`), the existing row is completely replaced by the new row. When set to `true`, the new row is deep-merged into the existing row",
'For example, say there is an existing row in the DB `{"id": "foo", "input": {"a": 5, "b": 10}}`. If we merge a new row as `{"_is_merge": true, "id": "foo", "input": {"b": 11, "c": 20}}`, the new row will be `{"id": "foo", "input": {"a": 5, "b": 11, "c": 20}}`. If we replace the new row as `{"id": "foo", "input": {"b": 11, "c": 20}}`, the new row will be `{"id": "foo", "input": {"b": 11, "c": 20}}`'
].join("\n\n");
var mergeEventSchema = z4.object({
[IS_MERGE_FIELD]: customTypes.literalTrue.describe(isMergeDescription),
[MERGE_PATHS_FIELD]: z4.string().array().array().nullish().describe(
[
"The `_merge_paths` field allows controlling the depth of the merge. It can only be specified alongside `_is_merge=true`. `_merge_paths` is a list of paths, where each path is a list of field names. The deep merge will not descend below any of the specified merge paths.",
'For example, say there is an existing row in the DB `{"id": "foo", "input": {"a": {"b": 10}, "c": {"d": 20}}, "output": {"a": 20}}`. If we merge a new row as `{"_is_merge": true, "_merge_paths": [["input", "a"], ["output"]], "input": {"a": {"q": 30}, "c": {"e": 30}, "bar": "baz"}, "output": {"d": 40}}`, the new row will be `{"id": "foo": "input": {"a": {"q": 30}, "c": {"d": 20, "e": 30}, "bar": "baz"}, "output": {"d": 40}}`. In this case, due to the merge paths, we have replaced `input.a` and `output`, but have still deep-merged `input` and `input.c`.'
].join("\n\n")
)
});
var replacementEventSchema = z4.object({
[IS_MERGE_FIELD]: customTypes.literalFalse.nullish().describe(isMergeDescription),
[PARENT_ID_FIELD]: z4.string().nullish().describe(
[
"Use the `_parent_id` field to create this row as a subspan of an existing row. It cannot be specified alongside `_is_merge=true`. Tracking hierarchical relationships are important for tracing (see the [guide](https://www.braintrustdata.com/docs/guides/tracing) for full details).",
'For example, say we have logged a row `{"id": "abc", "input": "foo", "output": "bar", "expected": "boo", "scores": {"correctness": 0.33}}`. We can create a sub-span of the parent row by logging `{"_parent_id": "abc", "id": "llm_call", "input": {"prompt": "What comes after foo?"}, "output": "bar", "metrics": {"tokens": 1}}`. In the webapp, only the root span row `"abc"` will show up in the summary view. You can view the full trace hierarchy (in this case, the `"llm_call"` row) by clicking on the "abc" row.'
].join("\n\n")
)
});
function makeInsertEventSchemas(objectType, insertSchema) {
const eventDescription = getEventObjectDescription(objectType);
const article = getEventObjectArticle(objectType);
const eventSchemaName = capitalize(
getEventObjectType(objectType),
"_"
).replace("_", "");
const replaceVariantSchema = insertSchema.merge(replacementEventSchema).strict().openapi(`Insert${eventSchemaName}EventReplace`);
const mergeVariantSchema = insertSchema.merge(mergeEventSchema).strict().openapi(`Insert${eventSchemaName}EventMerge`);
const eventSchema = z4.union([replaceVariantSchema, mergeVariantSchema]).describe(
`${capitalize(article)} ${eventDescription} event`
).openapi(`Insert${eventSchemaName}Event`);
const requestSchema = z4.object({
events: eventSchema.array().describe(`A list of ${eventDescription} events to insert`)
}).strict().openapi(`Insert${eventSchemaName}EventRequest`);
return { eventSchema, requestSchema };
}
var insertEventsResponseSchema = z4.object({
row_ids: z4.string().array().describe(
"The ids of all rows that were inserted, aligning one-to-one with the rows provided as input"
)
}).strict().openapi("InsertEventsResponse");
var {
eventSchema: insertExperimentEventSchema,
requestSchema: insertExperimentEventsRequestSchema
} = makeInsertEventSchemas(
"experiment",
z4.object({
input: experimentEventSchema.shape.input,
output: experimentEventSchema.shape.output,
expected: experimentEventSchema.shape.expected,
scores: experimentEventSchema.shape.scores,
metadata: experimentEventSchema.shape.metadata,
metrics: experimentEventSchema.shape.metrics,
context: experimentEventSchema.shape.context,
span_attributes: experimentEventSchema.shape.span_attributes,
id: experimentEventSchema.shape.id.nullish(),
dataset_record_id: experimentEventSchema.shape.dataset_record_id,
[OBJECT_DELETE_FIELD]: experimentEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
var {
eventSchema: insertDatasetEventSchema,
requestSchema: insertDatasetEventsRequestSchema
} = makeInsertEventSchemas(
"dataset",
z4.object({
input: datasetEventSchema.shape.input,
output: datasetEventSchema.shape.output,
metadata: datasetEventSchema.shape.metadata,
id: datasetEventSchema.shape.id.nullish(),
[OBJECT_DELETE_FIELD]: datasetEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
var {
eventSchema: insertProjectLogsEventSchema,
requestSchema: insertProjectLogsEventsRequestSchema
} = makeInsertEventSchemas(
"project",
z4.object({
input: projectLogsEventSchema.shape.input,
output: projectLogsEventSchema.shape.output,
expected: projectLogsEventSchema.shape.expected,
scores: projectLogsEventSchema.shape.scores,
metadata: projectLogsEventSchema.shape.metadata,
metrics: projectLogsEventSchema.shape.metrics,
context: projectLogsEventSchema.shape.context,
span_attributes: projectLogsEventSchema.shape.span_attributes,
id: projectLogsEventSchema.shape.id.nullish(),
[OBJECT_DELETE_FIELD]: projectLogsEventBaseSchema.shape[OBJECT_DELETE_FIELD]
}).strict()
);
function makeFeedbackRequestSchema(objectType, feedbackSchema) {
const eventDescription = getEventObjectDescription(objectType);
const eventSchemaName = capitalize(
getEventObjectType(objectType),
"_"
).replace("_", "");
return z4.object({
feedback: feedbackSchema.array().describe(`A list of ${eventDescription} feedback items`)
}).strict().openapi(`Feedback${eventSchemaName}EventRequest`);
}
var feedbackExperimentRequestBaseSchema = generateBaseEventFeedbackSchema("experiment");
var feedbackExperimentItemSchema = z4.object({
id: feedbackExperimentRequestBaseSchema.shape.id,
scores: feedbackExperimentRequestBaseSchema.shape.scores,
expected: feedbackExperimentRequestBaseSchema.shape.expected,
comment: feedbackExperimentRequestBaseSchema.shape.comment,
metadata: feedbackExperimentRequestBaseSchema.shape.metadata,
source: feedbackExperimentRequestBaseSchema.shape.source
}).strict().openapi("FeedbackExperimentItem");
var feedbackExperimentRequestSchema = makeFeedbackRequestSchema(
"experiment",
feedbackExperimentItemSchema
);
var feedbackDatasetRequestBaseSchema = generateBaseEventFeedbackSchema("dataset");
var feedbackDatasetItemSchema = z4.object({
id: feedbackDatasetRequestBaseSchema.shape.id,
comment: feedbackDatasetRequestBaseSchema.shape.comment,
metadata: feedbackDatasetRequestBaseSchema.shape.metadata,
source: feedbackDatasetRequestBaseSchema.shape.source
}).strict().openapi("FeedbackDatasetItem");
var feedbackDatasetRequestSchema = makeFeedbackRequestSchema(
"dataset",
feedbackDatasetItemSchema
);
var feedbackProjectLogsRequestBaseSchema = generateBaseEventFeedbackSchema("project");
var feedbackProjectLogsItemSchema = z4.object({
id: feedbackProjectLogsRequestBaseSchema.shape.id,
scores: feedbackProjectLogsRequestBaseSchema.shape.scores,
expected: feedbackProjectLogsRequestBaseSchema.shape.expected,
comment: feedbackProjectLogsRequestBaseSchema.shape.comment,
metadata: feedbackProjectLogsRequestBaseSchema.shape.metadata,
source: feedbackProjectLogsRequestBaseSchema.shape.source
}).strict().openapi("FeedbackProjectLogsItem");
var feedbackProjectLogsRequestSchema = makeFeedbackRequestSchema(
"project",
feedbackProjectLogsItemSchema
);
var eventObjectSchemas = {
experiment: {
fetchResponse: makeFetchEventsResponseSchema(
"experiment",
experimentEventSchema
),
insertEvent: insertExperimentEventSchema,
insertRequest: insertExperimentEventsRequestSchema,
feedbackItem: feedbackExperimentItemSchema,
feedbackRequest: feedbackExperimentRequestSchema
},
dataset: {
fetchResponse: makeFetchEventsResponseSchema("dataset", datasetEventSchema),
insertEvent: insertDatasetEventSchema,
insertRequest: insertDatasetEventsRequestSchema,
feedbackItem: feedbackDatasetItemSchema,
feedbackRequest: feedbackDatasetRequestSchema
},
project_logs: {
fetchResponse: makeFetchEventsResponseSchema(
"project",
projectLogsEventSchema
),
insertEvent: insertProjectLogsEventSchema,
insertRequest: insertProjectLogsEventsRequestSchema,
feedbackItem: feedbackProjectLogsItemSchema,
feedbackRequest: feedbackProjectLogsRequestSchema
}
};
function makeCrossObjectIndividualRequestSchema(objectType) {
const eventObjectType = getEventObjectType(objectType);
const eventDescription = getEventObjectDescription(objectType);
const eventObjectSchema = eventObjectSchemas[eventObjectType];
const insertObject = z4.object({
events: eventObjectSchema.insertEvent.array().nullish().describe(`A list of ${eventDescription} events to insert`),
feedback: eventObjectSchema.feedbackItem.array().nullish().describe(`A list of ${eventDescription} feedback items`)
}).strict();
return z4.record(z4.string().uuid(), insertObject).nullish().describe(`A mapping from ${objectType} id to a set of log events and feedback items to insert`);
}
function makeCrossObjectIndividualResponseSchema(objectType) {
return z4.record(z4.string().uuid(), insertEventsResponseSchema).nullish().describe(`A mapping from ${objectType} id to row ids for inserted \`events\``);
}
var crossObjectInsertRequestSchema = z4.object({
experiment: makeCrossObjectIndividualRequestSchema("experiment"),
dataset: makeCrossObjectIndividualRequestSchema("dataset"),
project_logs: makeCrossObjectIndividualRequestSchema("project")
}).strict().openapi("CrossObjectInsertRequest");
var crossObjectInsertResponseSchema = z4.object({
experiment: makeCrossObjectIndividualResponseSchema("experiment"),
dataset: makeCrossObjectIndividualResponseSchema("dataset"),
project_logs: makeCrossObjectIndividualResponseSchema("project")
}).strict().openapi("CrossObjectInsertResponse");
var summarizeScoresParamSchema = z4.boolean().describe("Whether to summarize the scores and metrics. If false (or omitted), only the metadata will be returned.");
var comparisonExperimentIdParamSchema = z4.string().uuid().describe("The experiment to compare against, if summarizing scores and metrics. If omitted, will fall back to the `base_exp_id` stored in the experiment metadata, and then to the most recent experiment run in the same project. Must pass `summarize_scores=true` for this id to be used");
var summarizeDataParamSchema = z4.boolean().describe("Whether to summarize the data. If false (or omitted), only the metadata will be returned.");
var summarizeExperimentResponseSchema = z4.object({
project_name: z4.string().describe("Name of the project that the experiment belongs to"),
experiment_name: z4.string().describe("Name of the experiment"),
project_url: z4.string().url().describe("URL to the project's page in the Braintrust app"),
experiment_url: z4.string().url().describe("URL to the experiment's page in the Braintrust app"),
comparison_experiment_name: z4.string().nullish().describe("The experiment which scores are baselined against"),
scores: z4.record(z4.object({
name: z4.string().describe("Name of the score"),
score: z4.number().min(0).max(1).describe("Average score across all examples"),
diff: z4.number().min(-1).max(1).describe("Difference in score between the current and comparison experiment"),
improvements: z4.number().int().min(0).describe("Number of improvements in the score"),
regressions: z4.number().int().min(0).describe("Number of regressions in the score")
}).describe("Summary of a score's performance").openapi("ScoreSummary")).nullish().describe("Summary of the experiment's scores"),
metrics: z4.record(z4.object({
name: z4.string().describe("Name of the metric"),
metric: z4.number().describe("Average metric across all examples"),
unit: z4.string().describe("Unit label for the metric"),
diff: z4.number().describe("Difference in metric between the current and comparison experiment"),
improvements: z4.number().int().min(0).describe("Number of improvements in the metric"),
regressions: z4.number().int().min(0).describe("Number of regressions in the metric")
}).describe("Summary of a metric's performance").openapi("MetricSummary")).nullish().describe("Summary of the experiment's metrics")
}).strict().describe("Summary of an experiment").openapi("SummarizeExperimentResponse");
var summarizeDatasetResponseSchema = z4.object({
project_name: z4.string().describe("Name of the project that the dataset belongs to"),
dataset_name: z4.string().describe("Name of the dataset"),
project_url: z4.string().url().describe("URL to the project's page in the Braintrust app"),
dataset_url: z4.string().url().describe("URL to the dataset's page in the Braintrust app"),
data_summary: z4.object({
total_records: z4.number().int().min(0).describe("Total number of records in the dataset")
}).nullish().describe("Summary of a dataset's data").openapi("DataSummary")
}).strict().describe("Summary of a dataset").openapi("SummarizeDatasetResponse");
var objectTypeSummarizeResponseSchemas = {
experiment: summarizeExperimentResponseSchema,
dataset: summarizeDatasetResponseSchema,
project: void 0
};
export {
allFetchFiltersSchema,
apiKeySchema,
appLimitSchema,
auditSourcesSchema,
comparisonExperimentIdParamSchema,
crossObjectInsertRequestSchema,
crossObjectInsertResponseSchema,
datasetSchema,
datetimeStringSchema,
endingBeforeSchema,
eventObjectSchemas,
experimentSchema,
fetchEventsRequestSchema,
fetchFiltersSchema,
fetchLimitSchema,
getEventObjectArticle,
getEventObjectDescription,
getEventObjectType,
insertEventsResponseSchema,
jsonSchema,
literalSchema,
maxRootSpanIdSchema,
maxXactIdSchema,
meSchema,
memberSchema,
objectSchemas,
objectTypeSummarizeResponseSchemas,
objectTypes,
organizationSchema,
projectSchema,
startingAfterSchema,
summarizeDataParamSchema,
summarizeScoresParamSchema,
userSchema,
versionSchema
};
+2
-1

@@ -33,2 +33,3 @@ declare const TRANSACTION_ID_FIELD = "_xact_id";

declare function mergeDicts(mergeInto: Record<string, unknown>, mergeFrom: Record<string, unknown>): Record<string, unknown>;
declare function capitalize(s: string, sep?: string): string;

@@ -63,2 +64,2 @@ declare enum SpanTypeAttribute {

export { ALL_ROW_ID_FIELDS, AUDIT_METADATA_FIELD, AUDIT_SOURCE_FIELD, CREATED_FIELD, type CollectMetadata, type GitFields, type GitMetadataSettings, ID_FIELD, IS_MERGE_FIELD, MERGE_PATHS_FIELD, OBJECT_DELETE_FIELD, PARENT_ID_FIELD, type RepoStatus, type Score, type Scorer, type ScorerArgs, type Source, SpanTypeAttribute, TRANSACTION_ID_FIELD, type TransactionId, VALID_SOURCES, mergeDicts, mergeGitMetadataSettings, mergeRowBatch };
export { ALL_ROW_ID_FIELDS, AUDIT_METADATA_FIELD, AUDIT_SOURCE_FIELD, CREATED_FIELD, type CollectMetadata, type GitFields, type GitMetadataSettings, ID_FIELD, IS_MERGE_FIELD, MERGE_PATHS_FIELD, OBJECT_DELETE_FIELD, PARENT_ID_FIELD, type RepoStatus, type Score, type Scorer, type ScorerArgs, type Source, SpanTypeAttribute, TRANSACTION_ID_FIELD, type TransactionId, VALID_SOURCES, capitalize, mergeDicts, mergeGitMetadataSettings, mergeRowBatch };

@@ -33,2 +33,3 @@ declare const TRANSACTION_ID_FIELD = "_xact_id";

declare function mergeDicts(mergeInto: Record<string, unknown>, mergeFrom: Record<string, unknown>): Record<string, unknown>;
declare function capitalize(s: string, sep?: string): string;

@@ -63,2 +64,2 @@ declare enum SpanTypeAttribute {

export { ALL_ROW_ID_FIELDS, AUDIT_METADATA_FIELD, AUDIT_SOURCE_FIELD, CREATED_FIELD, type CollectMetadata, type GitFields, type GitMetadataSettings, ID_FIELD, IS_MERGE_FIELD, MERGE_PATHS_FIELD, OBJECT_DELETE_FIELD, PARENT_ID_FIELD, type RepoStatus, type Score, type Scorer, type ScorerArgs, type Source, SpanTypeAttribute, TRANSACTION_ID_FIELD, type TransactionId, VALID_SOURCES, mergeDicts, mergeGitMetadataSettings, mergeRowBatch };
export { ALL_ROW_ID_FIELDS, AUDIT_METADATA_FIELD, AUDIT_SOURCE_FIELD, CREATED_FIELD, type CollectMetadata, type GitFields, type GitMetadataSettings, ID_FIELD, IS_MERGE_FIELD, MERGE_PATHS_FIELD, OBJECT_DELETE_FIELD, PARENT_ID_FIELD, type RepoStatus, type Score, type Scorer, type ScorerArgs, type Source, SpanTypeAttribute, TRANSACTION_ID_FIELD, type TransactionId, VALID_SOURCES, capitalize, mergeDicts, mergeGitMetadataSettings, mergeRowBatch };

@@ -35,2 +35,3 @@ "use strict";

VALID_SOURCES: () => VALID_SOURCES,
capitalize: () => capitalize,
mergeDicts: () => mergeDicts,

@@ -77,2 +78,6 @@ mergeGitMetadataSettings: () => mergeGitMetadataSettings,

}
function capitalize(s, sep) {
const items = sep ? s.split(sep) : [s];
return items.map((s2) => s2 ? s2.charAt(0).toUpperCase() + s2.slice(1) : s2).join(sep || "");
}

@@ -165,2 +170,3 @@ // src/merge_row_batch.ts

VALID_SOURCES,
capitalize,
mergeDicts,

@@ -167,0 +173,0 @@ mergeGitMetadataSettings,

@@ -36,2 +36,6 @@ // src/db_fields.ts

}
function capitalize(s, sep) {
const items = sep ? s.split(sep) : [s];
return items.map((s2) => s2 ? s2.charAt(0).toUpperCase() + s2.slice(1) : s2).join(sep || "");
}

@@ -123,2 +127,3 @@ // src/merge_row_batch.ts

VALID_SOURCES,
capitalize,
mergeDicts,

@@ -125,0 +130,0 @@ mergeGitMetadataSettings,

{
"name": "@braintrust/core",
"version": "0.0.15",
"version": "0.0.16",
"description": "Shared core dependencies for Braintrust packages",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"main": "./dist/main/index.js",
"module": "./dist/main/index.mjs",
"types": "./dist/main/index.d.ts",
"scripts": {

@@ -16,8 +16,19 @@ "build": "tsup",

"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"module": "./dist/index.mjs",
"require": "./dist/index.js"
"types": "./dist/main/index.d.ts",
"import": "./dist/main/index.mjs",
"module": "./dist/main/index.mjs",
"require": "./dist/main/index.js"
},
"./typespecs": {
"types": "./dist/typespecs/index.d.ts",
"import": "./dist/typespecs/index.mjs",
"module": "./dist/typespecs/index.mjs",
"require": "./dist/typespecs/index.js"
},
"./typespecs-stainless": {
"types": "./dist/typespecs-stainless/index.d.ts",
"import": "./dist/typespecs-stainless/index.mjs",
"module": "./dist/typespecs-stainless/index.mjs",
"require": "./dist/typespecs-stainless/index.js"
}

@@ -47,3 +58,7 @@ },

"typescript": "^5.3.3"
},
"dependencies": {
"@asteasolutions/zod-to-openapi": "^6.3.1",
"zod": "^3.22.4"
}
}