caelum0x ab9e54a189 feat(relay): per-key trace view and OD-RY2 trace retention (RY-RL3)
Implements OSS9. New relay-traces crate reads chat/json inferences by the
GIN-indexed tensorzero::api_key_public_id tag, joined with model_inferences
for model, provider, tokens, latency, cost and finish reason.

- GET /relay/traces: caller's own traces, newest first, keyset paging,
  window bounded (1-2160 h). Operator keys may use scope=all and key=.
- GET /relay/traces/{inference_id}: model calls plus stored bodies; a trace
  owned by another key returns 404, same as a missing one.
- relay-traces-retention: 14 d bodies, 90 d metadata (OD-RY2 defaults, env
  overridable), batched deletes against one cutoff, dry-run mode. Shipped in
  the relay image; systemd service/timer in crates/relay-traces/deploy (not
  installed).
- Relay UI: /observability/keys list (live refresh on the first page) and
  detail page; sidebar "Key Traces".
- No new tables or migrations.

Tests: 8 unit tests; live test on a scratch copy of the inference tables
proves key isolation, operator scope, paging and window-exact retention;
UI vitest 7/7, tsc and eslint clean; cargo check -p gateway clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 02:12:42 +03:00

2468 lines
88 KiB
TypeScript

/*
TensorZero Client (for internal use only for now)
TODO(shuyangli): Figure out a way to generate the HTTP client, possibly from Schema.
*/
import { z } from "zod";
import { BaseTensorZeroClient } from "./base-client";
import type {
BillingScope,
BillingStatus,
BillingSummary,
CostEventPage,
CostEventRecord,
CostEventState,
KeyBudget,
KeySpendSeries,
SettlementHistory,
TopUpCheckout,
WalletBalance,
WalletCredit,
} from "~/types/billing";
import {
traceListQuery,
type TraceDetail,
type TraceListOptions,
type TracePage,
type TraceScope,
} from "~/types/traces";
import type {
CatalogModel,
CatalogSnapshot,
PriceEditorState,
PriceQuote,
ProviderHealthReport,
ProviderKey,
ProviderKeysState,
ProviderKeyTestResult,
} from "~/types/catalog";
import { ZodJsonValueSchema } from "~/utils/clickhouse/common";
import { logger } from "~/utils/logger";
import type {
ApplyConfigTomlRequest,
ApplyConfigTomlResponse,
AutopilotStatusResponse,
CacheEnabledMode,
CloneDatapointsResponse,
CountFeedbackByTargetIdResponse,
CountInferencesRequest,
CountInferencesResponse,
CountModelsResponse,
CountWorkflowEvaluationRunEpisodesByTaskNameResponse,
CountWorkflowEvaluationRunEpisodesResponse,
CountWorkflowEvaluationRunsResponse,
CreateDatapointsFromInferenceRequest,
CreateDatapointsRequest,
CreateDatapointsResponse,
CumulativeFeedbackTimeSeriesPoint,
Datapoint,
DatapointStatsResponse,
DeleteDatapointsRequest,
DeleteDatapointsResponse,
DemonstrationFeedbackRow,
EvaluationConfig,
EvaluationFunctionConfig,
EvaluationRunEvent,
EvaluationRunStatsResponse,
FeedbackRow,
FunctionInferenceCount,
GetConfigTomlResponse,
GetDatapointCountResponse,
GetDatapointsRequest,
GetDatapointsResponse,
GetDemonstrationFeedbackResponse,
GetEpisodeInferenceCountResponse,
GetEvaluationResultsResponse,
GetEvaluationRunInfosResponse,
GetEvaluationStatisticsResponse,
GetEvaluationUsageStatisticsResponse,
GetEvaluationRunMetadataResponse,
GetFeedbackBoundsResponse,
GetFeedbackByTargetIdResponse,
GetFunctionThroughputByVariantResponse,
GetInferencesRequest,
GetInferencesResponse,
GetModelInferencesResponse,
GetModelLatencyResponse,
GetModelUsageResponse,
GetVariantSamplingProbabilitiesResponse,
GetVariantUsageResponse,
GetWorkflowEvaluationProjectCountResponse,
GetWorkflowEvaluationProjectsResponse,
GetWorkflowEvaluationRunEpisodesWithFeedbackResponse,
GetWorkflowEvaluationRunStatisticsResponse,
GetWorkflowEvaluationRunsResponse,
InferenceCountByVariant,
LaunchOptimizationWorkflowParams,
OptimizationJobHandle,
OptimizationJobInfo,
InferenceCountResponse,
InferenceWithFeedbackCountResponse,
LatestFeedbackIdByMetricResponse,
ListDatapointsRequest,
ListDatasetsResponse,
ListEpisodesResponse,
ListEvaluationRunsResponse,
ListFunctionsWithInferenceCountResponse,
ListInferenceMetadataResponse,
ListInferencesRequest,
ListWorkflowEvaluationRunEpisodesByTaskNameResponse,
ListWorkflowEvaluationRunsResponse,
MetricsWithFeedbackResponse,
RunEvaluationRequest,
SearchEvaluationRunsResponse,
SearchWorkflowEvaluationRunsResponse,
StatusResponse,
TableBoundsWithCount,
TimeWindow,
UiConfig,
UninitializedVariantInfo,
UpdateDatapointRequest,
UpdateDatapointsMetadataRequest,
UpdateDatapointsRequest,
UpdateDatapointsResponse,
ValidateConfigTomlRequest,
ValidateConfigTomlResponse,
VariantPerformancesResponse,
ClientInferenceParams,
InferenceResponse,
ResolveUuidResponse,
} from "~/types/tensorzero";
/**
* Feedback requests attach a metric value to a given inference or episode.
*/
export const FeedbackRequestSchema = z.object({
dryrun: z.boolean().optional(),
episode_id: z.string().nullable(),
inference_id: z.string().nullable(),
metric_name: z.string(),
tags: z.record(z.string()).optional(),
value: ZodJsonValueSchema,
internal: z.boolean().optional(),
});
export type FeedbackRequest = z.infer<typeof FeedbackRequestSchema>;
export const FeedbackResponseSchema = z.object({
feedback_id: z.string(),
});
export type FeedbackResponse = z.infer<typeof FeedbackResponseSchema>;
/**
* Schema for datapoint response
*/
export const DatapointResponseSchema = z.object({
id: z.string(),
});
export type DatapointResponse = z.infer<typeof DatapointResponseSchema>;
/**
* Response type for getCumulativeFeedbackTimeseries endpoint
*/
export interface GetCumulativeFeedbackTimeseriesResponse {
timeseries: CumulativeFeedbackTimeSeriesPoint[];
}
/**
* A client for calling the TensorZero Gateway inference and feedback endpoints.
*/
export class TensorZeroClient extends BaseTensorZeroClient {
/**
* Performs an inference request.
* @param request - The inference request payload.
* @returns A promise that resolves with the inference response.
* @throws Error if streaming is requested (not supported) or if the request fails.
*/
async inference(request: ClientInferenceParams): Promise<InferenceResponse> {
if (request.stream) {
// TODO(#5394): support streaming inference.
throw new Error("Streaming inference is not supported from the UI");
}
const response = await this.fetch("/inference", {
method: "POST",
body: JSON.stringify(request),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as InferenceResponse;
return body;
}
/**
* Sends feedback for a particular inference or episode.
* @param request - The feedback request payload.
* @returns A promise that resolves with the feedback response.
*/
async feedback(request: FeedbackRequest): Promise<FeedbackResponse> {
const response = await this.fetch("/feedback", {
method: "POST",
body: JSON.stringify(request),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as FeedbackResponse;
}
/**
* Queries feedback for a given target ID with pagination support.
* @param targetId - The target ID (inference_id or episode_id) to query feedback for
* @param options - Optional pagination parameters
* @returns A promise that resolves with a list of feedback rows
* @throws Error if the request fails
*/
async getFeedbackByTargetId(
targetId: string,
options?: {
before?: string;
after?: string;
limit?: number;
},
): Promise<FeedbackRow[]> {
const params = new URLSearchParams();
if (options?.before) params.set("before", options.before);
if (options?.after) params.set("after", options.after);
if (options?.limit) params.set("limit", options.limit.toString());
const queryString = params.toString();
const endpoint = `/internal/feedback/${encodeURIComponent(targetId)}${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as GetFeedbackByTargetIdResponse;
return body.feedback;
}
/**
* Gets demonstration feedback for a given inference.
* @param inferenceId - The inference ID to get demonstration feedback for
* @param options - Optional pagination parameters
* @returns A promise that resolves with a list of demonstration feedback rows
* @throws Error if the request fails
*/
async getDemonstrationFeedback(
inferenceId: string,
options?: {
before?: string;
after?: string;
limit?: number;
},
): Promise<DemonstrationFeedbackRow[]> {
const params = new URLSearchParams();
if (options?.before) params.set("before", options.before);
if (options?.after) params.set("after", options.after);
if (options?.limit) params.set("limit", options.limit.toString());
const queryString = params.toString();
const endpoint = `/internal/feedback/${encodeURIComponent(inferenceId)}/demonstrations${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as GetDemonstrationFeedbackResponse;
return body.feedback;
}
/**
* Gets model usage timeseries data.
* @param timeWindow The time window granularity for grouping data
* @param maxPeriods Maximum number of time periods to return
* @returns A promise that resolves with the model usage timeseries data
* @throws Error if the request fails
*/
async getModelUsageTimeseries(
timeWindow: TimeWindow,
maxPeriods: number,
): Promise<GetModelUsageResponse> {
const params = new URLSearchParams({
time_window: timeWindow,
max_periods: maxPeriods.toString(),
});
const response = await this.fetch(
`/internal/models/usage?${params.toString()}`,
{
method: "GET",
},
);
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetModelUsageResponse;
}
/**
* Gets model latency quantile distributions.
* @param timeWindow The time window for aggregating latency data
* @returns A promise that resolves with the model latency quantiles
* @throws Error if the request fails
*/
async getModelLatencyQuantiles(
timeWindow: TimeWindow,
): Promise<GetModelLatencyResponse> {
const params = new URLSearchParams({
time_window: timeWindow,
});
const response = await this.fetch(
`/internal/models/latency?${params.toString()}`,
{
method: "GET",
},
);
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetModelLatencyResponse;
}
/**
* Updates an existing datapoint in a dataset.
* This operation creates a new datapoint with a new ID and marks the old one as stale.
* The v1 endpoint automatically handles both creating the new version and staling the old one.
* @param datasetName - The name of the dataset containing the datapoint
* @param updateDatapointRequest - The update request containing type, id, input, output, and optional fields
* @returns A promise that resolves with the response containing the new datapoint ID
* @throws Error if the dataset name is invalid or the request fails
*/
async updateDatapoint(
datasetName: string,
updateDatapointRequest: UpdateDatapointRequest,
): Promise<DatapointResponse> {
const endpoint = `/v1/datasets/${encodeURIComponent(datasetName)}/datapoints`;
const requestBody: UpdateDatapointsRequest = {
datapoints: [updateDatapointRequest],
};
const response = await this.fetch(endpoint, {
method: "PATCH",
body: JSON.stringify(requestBody),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as UpdateDatapointsResponse;
return { id: body.ids[0] };
}
async getDatapoint(
datapointId: string,
datasetName?: string,
): Promise<Datapoint | undefined> {
// We currently maintain 2 endpoints for getting a datapoint, with/without the dataset name.
const endpoint = datasetName
? `/v1/datasets/${encodeURIComponent(datasetName)}/get_datapoints`
: `/v1/datasets/get_datapoints`;
const requestBody: GetDatapointsRequest = {
ids: [datapointId],
};
const response = await this.fetch(endpoint, {
method: "POST",
body: JSON.stringify(requestBody),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as GetDatapointsResponse;
if (body.datapoints.length === 0) {
return undefined;
}
return body.datapoints[0];
}
async listDatapoints(
dataset_name: string,
params: ListDatapointsRequest,
): Promise<GetDatapointsResponse> {
const endpoint = `/v1/datasets/${encodeURIComponent(dataset_name)}/list_datapoints`;
const response = await this.fetch(endpoint, {
method: "POST",
body: JSON.stringify(params),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as GetDatapointsResponse;
return body;
}
async listDatasets(params: {
function_name?: string;
limit?: number;
offset?: number;
}): Promise<ListDatasetsResponse> {
const searchParams = new URLSearchParams();
if (params.function_name) {
searchParams.append("function_name", params.function_name);
}
if (params.limit !== undefined) {
searchParams.append("limit", params.limit.toString());
}
if (params.offset !== undefined) {
searchParams.append("offset", params.offset.toString());
}
const queryString = searchParams.toString();
const endpoint = `/internal/datasets${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return await response.json();
}
async updateDatapointsMetadata(
datasetName: string,
datapoints: UpdateDatapointsMetadataRequest,
): Promise<UpdateDatapointsResponse> {
const endpoint = `/v1/datasets/${encodeURIComponent(datasetName)}/datapoints/metadata`;
const response = await this.fetch(endpoint, {
method: "PATCH",
body: JSON.stringify(datapoints),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as UpdateDatapointsResponse;
return body;
}
/**
* Marks datapoints as deleted in a dataset by setting their `staled_at` timestamp.
* Marked datapoints will no longer appear in default queries, but are preserved in the database for auditing or recovery purposes.
* @param datasetName - The name of the dataset containing the datapoints
* @param datapointIds - Array of datapoint UUIDs to mark as deleted
* @returns A promise that resolves with the response containing the number of marked datapoints
* @throws Error if the dataset name is invalid, the IDs array is empty, or the request fails
*/
async deleteDatapoints(
datasetName: string,
datapointIds: string[],
): Promise<DeleteDatapointsResponse> {
const endpoint = `/v1/datasets/${encodeURIComponent(datasetName)}/datapoints`;
const requestBody: DeleteDatapointsRequest = {
ids: datapointIds,
};
const response = await this.fetch(endpoint, {
method: "DELETE",
body: JSON.stringify(requestBody),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as DeleteDatapointsResponse;
return body;
}
/**
* Marks all datapoints in a dataset as deleted in a dataset by setting their `staled_at` timestamp.
* @param datasetName - The name of the dataset containing the datapoints
* @returns A promise that resolves with the response containing the number of marked datapoints
* @throws Error if the dataset name is invalid, the IDs array is empty, or the request fails
*/
async deleteDataset(datasetName: string): Promise<DeleteDatapointsResponse> {
const endpoint = `/v1/datasets/${encodeURIComponent(datasetName)}`;
const response = await this.fetch(endpoint, {
method: "DELETE",
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as DeleteDatapointsResponse;
return body;
}
/**
* Clones datapoints to a target dataset, preserving all fields except id and dataset_name.
* @param targetDatasetName - The name of the target dataset to clone datapoints to
* @param datapointIds - Array of datapoint UUIDs to clone
* @returns A promise that resolves with the response containing the new datapoint IDs (null if source not found)
* @throws Error if the dataset name is invalid or the request fails
*/
async cloneDatapoints(
targetDatasetName: string,
datapointIds: string[],
): Promise<CloneDatapointsResponse> {
const endpoint = `/internal/datasets/${encodeURIComponent(targetDatasetName)}/datapoints/clone`;
const response = await this.fetch(endpoint, {
method: "POST",
body: JSON.stringify({ datapoint_ids: datapointIds }),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as CloneDatapointsResponse;
}
/**
* Creates new datapoints in a dataset manually.
* @param datasetName - The name of the dataset to create datapoints in
* @param request - The request containing the datapoints to create
* @returns A promise that resolves with the response containing the new datapoint IDs
* @throws Error if the dataset name is invalid or the request fails
*/
async createDatapoints(
datasetName: string,
request: CreateDatapointsRequest,
): Promise<CreateDatapointsResponse> {
const endpoint = `/v1/datasets/${encodeURIComponent(datasetName)}/datapoints`;
const response = await this.fetch(endpoint, {
method: "POST",
body: JSON.stringify(request),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as CreateDatapointsResponse;
}
/**
* Fetches datapoint count for a dataset, optionally filtered by function name.
* @param datasetName - The name of the dataset to get count for
* @param options - Optional parameters for filtering
* @param options.functionName - Optional function name to filter by
* @returns A promise that resolves with the datapoint count
* @throws Error if the request fails
*/
async getDatapointCount(
datasetName: string,
options?: { functionName?: string },
): Promise<GetDatapointCountResponse> {
const searchParams = new URLSearchParams();
if (options?.functionName) {
searchParams.append("function_name", options.functionName);
}
const queryString = searchParams.toString();
const endpoint = `/internal/datasets/${encodeURIComponent(datasetName)}/datapoints/count${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetDatapointCountResponse;
}
/**
* Gets the authenticated principal's Relay billing summary: wallet rollup,
* per-API-key spend, per-model spend, spend/token totals, and recent
* settlements. Aggregated from the durable cost-event and settlement-evidence
* tables and scoped to the caller.
* @param options.settlements - Number of recent settlement receipts to return.
* @returns A promise that resolves with the billing summary.
* @throws Error if the request fails.
*/
async getBillingSummary(options?: {
settlements?: number;
scope?: BillingScope;
}): Promise<BillingSummary> {
const searchParams = new URLSearchParams();
if (options?.settlements != null) {
searchParams.append("settlements", String(options.settlements));
}
if (options?.scope === "all") {
searchParams.append("scope", "all");
}
const queryString = searchParams.toString();
const endpoint = `/relay/billing/summary${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const envelope = (await response.json()) as { data: BillingSummary };
return envelope.data;
}
/**
* Gets the authenticated principal's detailed settlement-evidence history:
* one row per signed settlement receipt, newest first, each carrying the
* immutable receipt JSON for proof-of-payment drill-down.
* @param options.settlements - Number of settlement receipts to return.
* @returns A promise that resolves with the settlement history.
* @throws Error if the request fails.
*/
async getBillingSettlements(options?: {
settlements?: number;
scope?: BillingScope;
before?: string;
}): Promise<SettlementHistory> {
const searchParams = new URLSearchParams();
if (options?.settlements != null) {
searchParams.append("settlements", String(options.settlements));
}
if (options?.before) {
searchParams.append("before", options.before);
}
if (options?.scope === "all") {
searchParams.append("scope", "all");
}
const queryString = searchParams.toString();
const endpoint = `/relay/billing/settlements${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const envelope = (await response.json()) as { data: SettlementHistory };
return envelope.data;
}
/**
* Gets the authenticated principal's per-API-key spend over time. Spend is
* bucketed into UTC calendar days from the durable cost-event table and
* returned oldest-first so the dashboard can plot a trend.
* @returns A promise that resolves with the per-key spend series.
* @throws Error if the request fails.
*/
async getBillingKeySpend(options?: {
scope?: BillingScope;
}): Promise<KeySpendSeries> {
const endpoint =
options?.scope === "all"
? "/relay/billing/keys?scope=all"
: "/relay/billing/keys";
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const envelope = (await response.json()) as { data: KeySpendSeries };
return envelope.data;
}
/**
* Gets the Relay model catalog snapshot: the configured models and providers
* with their capabilities and pricing.
* @returns A promise that resolves with the catalog snapshot.
* @throws Error if the request fails.
*/
async getRelayCatalog(): Promise<CatalogSnapshot> {
const response = await this.fetch("/v1/relay/catalog", { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const envelope = (await response.json()) as { data: CatalogSnapshot };
return envelope.data;
}
/**
* Gets the gateway's billing wiring (prepaid billing, postpaid metering,
* auth, Postgres, cost-feed target) plus whether the UI's key is an operator.
* Used to explain unconfigured deployments instead of rendering zeros.
*/
async getBillingStatus(): Promise<BillingStatus> {
return this.getRelayData<BillingStatus>("/relay/billing/status");
}
/**
* Gets the live Mint balance of the calling key's payer account. Only
* populated in prepaid (Mint-settled) mode.
*/
async getWalletBalance(options?: {
principal?: string;
}): Promise<WalletBalance> {
const query = options?.principal
? `?principal=${encodeURIComponent(options.principal)}`
: "";
return this.getRelayData<WalletBalance>(`/relay/billing/wallet${query}`);
}
/**
* Creates a provider-hosted checkout that funds a key's Mint payer wallet.
* Operators may pass `principal` to fund another key.
*/
async createWalletTopUp(request: {
amount_micros: number;
provider?: string;
success_url?: string;
cancel_url?: string;
principal?: string;
}): Promise<TopUpCheckout> {
return this.sendRelayData<TopUpCheckout>(
"/relay/billing/wallet/top_up",
"POST",
request,
);
}
/**
* Credits a key's wallet from the operator treasury (operator keys only).
* `idempotency_key` makes a retried submit safe.
*/
async creditWallet(request: {
principal: string;
amount_micros: number;
idempotency_key: string;
note?: string;
}): Promise<WalletCredit> {
return this.sendRelayData<WalletCredit>(
"/relay/billing/wallet/credit",
"POST",
request,
);
}
/** Lists persisted operator price edits and whether prices are editable. */
async getPriceEditor(): Promise<PriceEditorState> {
return this.getRelayData<PriceEditorState>("/v1/relay/catalog/prices");
}
/** Sets a catalog model's retail price (operator keys only). */
async setModelPrice(request: {
model: string;
input_micro_usd_per_million: number;
output_micro_usd_per_million: number;
note?: string | null;
}): Promise<CatalogModel> {
return this.sendRelayData<CatalogModel>(
"/v1/relay/catalog/prices",
"PUT",
request,
);
}
/** Drops a price edit so the deployed catalog price applies again. */
async resetModelPrice(model: string): Promise<CatalogModel> {
return this.sendRelayData<CatalogModel>(
"/v1/relay/catalog/prices/reset",
"POST",
{ model },
);
}
/** Lists provider credential slots (hints only, never secrets). */
async listProviderKeys(): Promise<ProviderKeysState> {
return this.getRelayData<ProviderKeysState>("/v1/relay/provider_keys");
}
/** Stores or rotates a provider API key (operator keys only). */
async setProviderKey(name: string, apiKey: string): Promise<ProviderKey> {
return this.sendRelayData<ProviderKey>(
`/v1/relay/provider_keys/${encodeURIComponent(name)}`,
"PUT",
{ api_key: apiKey },
);
}
/** Removes a stored provider key (falls back to the env variable). */
async deleteProviderKey(name: string): Promise<ProviderKey> {
return this.sendRelayData<ProviderKey>(
`/v1/relay/provider_keys/${encodeURIComponent(name)}`,
"DELETE",
);
}
/** Verifies a provider key against the provider (operator keys only). */
async testProviderKey(name: string): Promise<ProviderKeyTestResult> {
return this.sendRelayData<ProviderKeyTestResult>(
`/v1/relay/provider_keys/${encodeURIComponent(name)}/test`,
"POST",
);
}
/**
* Lists durable cost events (the metering outbox), newest first.
* @param options.state - Optional delivery-state filter.
* @param options.before - Cursor: only events older than this event id.
* @param options.limit - Page size (1-100).
* @param options.scope - `all` for every API key (operator keys only).
*/
async listCostEvents(options?: {
state?: CostEventState;
before?: string;
limit?: number;
scope?: BillingScope;
}): Promise<CostEventPage> {
const searchParams = new URLSearchParams();
if (options?.state) {
searchParams.append("state", options.state);
}
if (options?.before) {
searchParams.append("before", options.before);
}
if (options?.limit != null) {
searchParams.append("limit", String(options.limit));
}
if (options?.scope === "all") {
searchParams.append("scope", "all");
}
const queryString = searchParams.toString();
return this.getRelayData<CostEventPage>(
`/v1/billing/cost-events${queryString ? `?${queryString}` : ""}`,
);
}
/**
* Re-arms a dead-lettered cost event for delivery to the cost feed.
*/
async replayCostEvent(
eventId: string,
options?: { scope?: BillingScope },
): Promise<CostEventRecord> {
const scope = options?.scope === "all" ? "?scope=all" : "";
const response = await this.fetch(
`/v1/billing/cost-events/${encodeURIComponent(eventId)}/replay${scope}`,
{ method: "POST" },
);
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const envelope = (await response.json()) as { data: CostEventRecord };
return envelope.data;
}
/**
* Lists per-key monthly spend caps with month-to-date consumption.
*/
async listBudgets(options?: { scope?: BillingScope }): Promise<KeyBudget[]> {
return this.getRelayData<KeyBudget[]>(
options?.scope === "all"
? "/relay/billing/budgets?scope=all"
: "/relay/billing/budgets",
);
}
/**
* Creates or replaces an API key's monthly spend cap (operator keys only).
*/
async upsertBudget(
keyId: string,
request: {
monthly_limit_micros: number;
daily_limit_micros?: number | null;
note?: string | null;
},
): Promise<KeyBudget> {
const response = await this.fetch(
`/relay/billing/budgets/${encodeURIComponent(keyId)}`,
{ method: "PUT", body: JSON.stringify(request) },
);
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const envelope = (await response.json()) as { data: KeyBudget };
return envelope.data;
}
/**
* Removes an API key's monthly spend cap (operator keys only).
*/
async deleteBudget(keyId: string): Promise<void> {
const response = await this.fetch(
`/relay/billing/budgets/${encodeURIComponent(keyId)}`,
{ method: "DELETE" },
);
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
}
/**
* Prices a hypothetical request against the live Relay catalog.
*/
async getRelayQuote(request: {
model: string;
input_tokens: number;
output_tokens: number;
}): Promise<PriceQuote> {
const response = await this.fetch("/v1/relay/quotes", {
method: "POST",
body: JSON.stringify(request),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const envelope = (await response.json()) as { data: PriceQuote };
return envelope.data;
}
/**
* Gets configured model routing joined with live provider health
* (latency, error rate, circuit-breaker state).
*/
async getProviderHealth(): Promise<ProviderHealthReport> {
return this.getRelayData<ProviderHealthReport>("/v1/relay/provider_health");
}
/**
* Lists traces for the authenticated API key, newest first (RY-RL3).
* Operator keys may pass `scope: "all"`, optionally narrowed by `key`.
*/
async listRelayTraces(options?: TraceListOptions): Promise<TracePage> {
return this.getRelayData<TracePage>(
`/relay/traces${traceListQuery(options)}`,
);
}
/**
* Reads one trace with its model calls and stored bodies. The gateway
* answers 404 for a trace owned by another key.
*/
async getRelayTrace(
inferenceId: string,
options?: { scope?: TraceScope },
): Promise<TraceDetail> {
const scope = options?.scope === "all" ? "?scope=all" : "";
return this.getRelayData<TraceDetail>(
`/relay/traces/${encodeURIComponent(inferenceId)}${scope}`,
);
}
/**
* Checks gateway readiness via its `/health` endpoint.
*/
async gatewayHealth(): Promise<{
ok: boolean;
status: number;
body: string;
}> {
const response = await this.fetch("/health", { method: "GET" });
const body = await response.text();
return { ok: response.ok, status: response.status, body };
}
private async sendRelayData<T>(
endpoint: string,
method: "POST" | "PUT" | "DELETE",
body?: unknown,
): Promise<T> {
const response = await this.fetch(endpoint, {
method,
body: body === undefined ? undefined : JSON.stringify(body),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const envelope = (await response.json()) as { data: T };
return envelope.data;
}
private async getRelayData<T>(endpoint: string): Promise<T> {
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const envelope = (await response.json()) as { data: T };
return envelope.data;
}
async getObject(path: string): Promise<string> {
const endpoint = `/internal/object_storage?path=${encodeURIComponent(path)}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return response.text();
}
async status(): Promise<StatusResponse> {
const response = await this.fetch("/status", { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as StatusResponse;
}
/**
* Gets the autopilot configuration status.
* Returns whether autopilot is configured (i.e., whether TENSORZERO_AUTOPILOT_API_KEY is set).
* This endpoint does not require authentication.
* @returns A promise that resolves with the autopilot status
* @throws Error if the request fails
*/
async getAutopilotStatus(): Promise<AutopilotStatusResponse> {
const response = await this.fetch("/internal/autopilot/status", {
method: "GET",
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as AutopilotStatusResponse;
}
/**
* Lists inferences with optional filtering, pagination, and sorting.
* Uses the public v1 API endpoint.
* @param request - The list inferences request parameters
* @returns A promise that resolves with the inferences response
* @throws Error if the request fails
*/
async listInferences(
request: ListInferencesRequest,
): Promise<GetInferencesResponse> {
const response = await this.fetch("/v1/inferences/list_inferences", {
method: "POST",
body: JSON.stringify(request),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetInferencesResponse;
}
/**
* Retrieves specific inferences by their IDs.
* Uses the public v1 API endpoint.
* @param request - The get inferences request containing IDs and optional filters
* @returns A promise that resolves with the inferences response
* @throws Error if the request fails
*/
async getInferences(
request: GetInferencesRequest,
): Promise<GetInferencesResponse> {
const response = await this.fetch("/v1/inferences/get_inferences", {
method: "POST",
body: JSON.stringify(request),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetInferencesResponse;
}
/**
* Fetches the gateway configuration for the UI.
* @returns A promise that resolves with the UiConfig object
* @throws Error if the request fails
*/
async getUiConfig(): Promise<UiConfig> {
const response = await this.fetch("/internal/ui_config", { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as UiConfig;
}
async getUiConfigByHash(hash: string): Promise<UiConfig> {
const response = await this.fetch(
`/internal/ui_config/${encodeURIComponent(hash)}`,
{ method: "GET" },
);
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as UiConfig;
}
async getConfigToml(): Promise<GetConfigTomlResponse> {
const response = await this.fetch("/internal/config_toml", {
method: "GET",
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetConfigTomlResponse;
}
async applyConfigToml(
request: ApplyConfigTomlRequest,
): Promise<ApplyConfigTomlResponse> {
const response = await this.fetch("/internal/config_toml/apply", {
method: "POST",
body: JSON.stringify(request),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as ApplyConfigTomlResponse;
}
async validateConfigToml(
request: ValidateConfigTomlRequest,
): Promise<ValidateConfigTomlResponse> {
const response = await this.fetch("/internal/config_toml/validate", {
method: "POST",
body: JSON.stringify(request),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as ValidateConfigTomlResponse;
}
/**
* Fetches inference count for a function, optionally filtered by variant or grouped by variant.
* @param functionName - The name of the function to get count for
* @param options - Optional parameters for filtering or grouping
* @param options.variantName - Optional variant name to filter by
* @param options.groupBy - Optional grouping (e.g., "variant" to get counts per variant)
* @returns A promise that resolves with the inference count
* @throws Error if the request fails
*/
async getInferenceCount(
functionName: string,
options?: { variantName?: string; groupBy?: "variant" },
): Promise<InferenceCountResponse> {
const searchParams = new URLSearchParams();
if (options?.variantName) {
searchParams.append("variant_name", options.variantName);
}
if (options?.groupBy) {
searchParams.append("group_by", options.groupBy);
}
const queryString = searchParams.toString();
const endpoint = `/internal/functions/${encodeURIComponent(functionName)}/inference_count${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as InferenceCountResponse;
}
/**
* Fetches the variants used for a function.
* @param functionName - The name of the function to get variants for
* @returns A promise that resolves with the variants used for the function
* @throws Error if the request fails
*/
async getUsedVariants(functionName: string): Promise<string[]> {
const response = await this.getInferenceCount(functionName, {
groupBy: "variant",
});
return (response.count_by_variant ?? []).map(
(v: InferenceCountByVariant) => v.variant_name,
);
}
/**
* Lists all functions with their inference counts, ordered by most recent inference.
* @returns A promise that resolves with the function inference counts
* @throws Error if the request fails
*/
async listFunctionsWithInferenceCount(): Promise<FunctionInferenceCount[]> {
const endpoint = `/internal/functions/inference_counts`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body =
(await response.json()) as ListFunctionsWithInferenceCountResponse;
return body.functions;
}
/**
* Fetches feedback counts for a function and metric.
* @param functionName - The name of the function to get count for
* @param metricName - The name of the metric to get count for (or "demonstration")
* @param threshold - Optional threshold for float metrics (defaults to 0)
* @returns A promise that resolves with the feedback and curated inference counts
* @throws Error if the request fails
*/
async getFeedbackCount(
functionName: string,
metricName: string,
threshold?: number,
): Promise<InferenceWithFeedbackCountResponse> {
const searchParams = new URLSearchParams();
if (threshold !== undefined) {
searchParams.append("threshold", threshold.toString());
}
const queryString = searchParams.toString();
const endpoint = `/internal/functions/${encodeURIComponent(functionName)}/inference_count/${encodeURIComponent(metricName)}${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as InferenceWithFeedbackCountResponse;
}
/**
* Fetches function throughput data grouped by variant and time period.
* @param functionName - The name of the function to get throughput data for
* @param timeWindow - The time granularity for grouping data (minute, hour, day, week, month, cumulative)
* @param maxPeriods - Maximum number of time periods to return
* @returns A promise that resolves with the throughput data
* @throws Error if the request fails
*/
async getFunctionThroughputByVariant(
functionName: string,
timeWindow: TimeWindow,
maxPeriods: number,
): Promise<GetFunctionThroughputByVariantResponse> {
const searchParams = new URLSearchParams({
time_window: timeWindow,
max_periods: maxPeriods.toString(),
});
const endpoint = `/internal/functions/${encodeURIComponent(functionName)}/throughput_by_variant?${searchParams.toString()}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetFunctionThroughputByVariantResponse;
}
/**
* Gets variant usage timeseries data for a function.
* @param functionName - The function to get variant usage for
* @param timeWindow - The time window granularity
* @param maxPeriods - Maximum number of periods to return
* @returns A promise that resolves with variant usage data
* @throws Error if the request fails
*/
async getVariantUsageTimeseries(
functionName: string,
timeWindow: TimeWindow,
maxPeriods: number,
): Promise<GetVariantUsageResponse> {
const searchParams = new URLSearchParams({
time_window: timeWindow,
max_periods: maxPeriods.toString(),
});
const endpoint = `/internal/functions/${encodeURIComponent(functionName)}/variant_usage?${searchParams.toString()}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetVariantUsageResponse;
}
/**
* Fetches metrics with feedback for a function, optionally filtered by variant.
* @param functionName - The name of the function to get metrics for
* @param variantName - Optional variant name to filter by
* @returns A promise that resolves with metrics and their feedback counts
* @throws Error if the request fails
*/
async getFunctionMetricsWithFeedback(
functionName: string,
variantName?: string,
): Promise<MetricsWithFeedbackResponse> {
const searchParams = new URLSearchParams();
if (variantName) {
searchParams.append("variant_name", variantName);
}
const queryString = searchParams.toString();
const endpoint = `/internal/functions/${encodeURIComponent(functionName)}/metrics${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as MetricsWithFeedbackResponse;
}
/**
* Fetches variant sampling probabilities for a function from the gateway.
* @param functionName - The name of the function to get variant sampling probabilities for
* @returns A promise that resolves with variant sampling probabilities
* @throws Error if the request fails
*/
async getVariantSamplingProbabilities(
functionName: string,
): Promise<GetVariantSamplingProbabilitiesResponse> {
const endpoint = `/internal/functions/${encodeURIComponent(functionName)}/variant_sampling_probabilities`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetVariantSamplingProbabilitiesResponse;
}
/**
* Fetches variant performance statistics for a function and metric.
* @param functionName - The name of the function to get performance stats for
* @param metricName - The name of the metric to compute performance for
* @param timeWindow - Time granularity for grouping performance data
* @param variantName - Optional variant name to filter by
* @returns A promise that resolves with variant performance statistics
* @throws Error if the request fails
*/
async getVariantPerformances(
functionName: string,
metricName: string,
timeWindow: TimeWindow,
variantName?: string,
): Promise<VariantPerformancesResponse> {
const searchParams = new URLSearchParams();
searchParams.append("metric_name", metricName);
searchParams.append("time_window", timeWindow);
if (variantName) {
searchParams.append("variant_name", variantName);
}
const queryString = searchParams.toString();
const endpoint = `/internal/functions/${encodeURIComponent(functionName)}/variant_performances?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as VariantPerformancesResponse;
}
/**
* Fetches model inferences for a given inference ID.
* @param inferenceId - The UUID of the inference to get model inferences for
* @returns A promise that resolves with the model inferences response
* @throws Error if the request fails
*/
async getModelInferences(
inferenceId: string,
): Promise<GetModelInferencesResponse> {
const endpoint = `/internal/model_inferences/${encodeURIComponent(inferenceId)}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetModelInferencesResponse;
}
/**
* Counts the number of distinct models used.
* @returns A promise that resolves with the count of distinct models
* @throws Error if the request fails
*/
async countDistinctModelsUsed(): Promise<CountModelsResponse> {
const response = await this.fetch("/internal/models/count", {
method: "GET",
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as CountModelsResponse;
}
/**
* Lists evaluation runs with pagination.
* @param limit - Maximum number of evaluation runs to return (default: 100)
* @param offset - Number of evaluation runs to skip for pagination (default: 0)
* @returns A promise that resolves with the list of evaluation runs
* @throws Error if the request fails
*/
async listEvaluationRuns(
limit: number = 100,
offset: number = 0,
): Promise<ListEvaluationRunsResponse> {
const searchParams = new URLSearchParams();
searchParams.append("limit", limit.toString());
searchParams.append("offset", offset.toString());
const queryString = searchParams.toString();
const endpoint = `/internal/evaluations/runs${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as ListEvaluationRunsResponse;
}
/**
* Counts the total number of evaluation runs.
* @returns A promise that resolves with the evaluation run count
* @throws Error if the request fails
*/
async countEvaluationRuns(): Promise<number> {
const response = await this.fetch("/internal/evaluations/runs/count", {
method: "GET",
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const count_response =
(await response.json()) as EvaluationRunStatsResponse;
return Number(count_response.count);
}
/**
* Creates datapoints from inferences based on either specific inference IDs or an inference query.
* @param datasetName - The name of the dataset to create datapoints in
* @param request - The request containing either inference IDs or an inference query with filters
* @returns A promise that resolves with the response containing the new datapoint IDs
* @throws Error if the request fails
*/
async createDatapointsFromInferences(
datasetName: string,
request: CreateDatapointsFromInferenceRequest,
): Promise<CreateDatapointsResponse> {
const endpoint = `/v1/datasets/${encodeURIComponent(datasetName)}/from_inferences`;
const response = await this.fetch(endpoint, {
method: "POST",
body: JSON.stringify(request),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as CreateDatapointsResponse;
}
/**
* Counts unique datapoints across specified evaluation runs.
* @param functionName - The name of the function being evaluated
* @param evaluationRunIds - Array of evaluation run IDs
* @returns A promise that resolves with the datapoint count
* @throws Error if the request fails
*/
async countDatapointsForEvaluation(
functionName: string,
evaluationRunIds: string[],
): Promise<number> {
const searchParams = new URLSearchParams();
searchParams.append("function_name", functionName);
searchParams.append("evaluation_run_ids", evaluationRunIds.join(","));
const queryString = searchParams.toString();
const endpoint = `/internal/evaluations/datapoint_count?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const result = (await response.json()) as DatapointStatsResponse;
return Number(result.count);
}
/**
* Gets workflow evaluation projects with pagination.
* @param limit - Maximum number of projects to return (default: 100)
* @param offset - Number of projects to skip (default: 0)
* @returns A promise that resolves with the workflow evaluation projects response
* @throws Error if the request fails
*/
async getWorkflowEvaluationProjects(
limit: number = 100,
offset: number = 0,
): Promise<GetWorkflowEvaluationProjectsResponse> {
const searchParams = new URLSearchParams();
searchParams.append("limit", limit.toString());
searchParams.append("offset", offset.toString());
const queryString = searchParams.toString();
const endpoint = `/internal/workflow_evaluations/projects${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetWorkflowEvaluationProjectsResponse;
}
/**
* Counts workflow evaluation projects.
* @returns A promise that resolves with the workflow evaluation project count
* @throws Error if the request fails
*/
async countWorkflowEvaluationProjects(): Promise<number> {
const response = await this.fetch(
"/internal/workflow_evaluations/projects/count",
{ method: "GET" },
);
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body =
(await response.json()) as GetWorkflowEvaluationProjectCountResponse;
return body.count;
}
/**
* Searches workflow evaluation runs by project name and/or search query.
* @param limit - Maximum number of runs to return (default: 100)
* @param offset - Number of runs to skip (default: 0)
* @param projectName - Optional project name to filter by
* @param searchQuery - Optional search query to filter by (searches run name and ID)
* @returns A promise that resolves with the search results
* @throws Error if the request fails
*/
async searchWorkflowEvaluationRuns(
limit: number = 100,
offset: number = 0,
projectName?: string,
searchQuery?: string,
): Promise<SearchWorkflowEvaluationRunsResponse> {
const searchParams = new URLSearchParams();
searchParams.append("limit", limit.toString());
searchParams.append("offset", offset.toString());
if (projectName) {
searchParams.append("project_name", projectName);
}
if (searchQuery) {
searchParams.append("q", searchQuery);
}
const queryString = searchParams.toString();
const endpoint = `/internal/workflow_evaluations/runs/search?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as SearchWorkflowEvaluationRunsResponse;
}
/**
* Lists workflow evaluation runs with episode counts.
* @param limit - Maximum number of runs to return (default: 100)
* @param offset - Number of runs to skip (default: 0)
* @param runId - Optional run ID to filter by
* @param projectName - Optional project name to filter by
* @returns A promise that resolves with the workflow evaluation runs response
* @throws Error if the request fails
*/
async listWorkflowEvaluationRuns(
limit: number = 100,
offset: number = 0,
runId?: string,
projectName?: string,
): Promise<ListWorkflowEvaluationRunsResponse> {
const searchParams = new URLSearchParams();
searchParams.append("limit", limit.toString());
searchParams.append("offset", offset.toString());
if (runId) {
searchParams.append("run_id", runId);
}
if (projectName) {
searchParams.append("project_name", projectName);
}
const queryString = searchParams.toString();
const endpoint = `/internal/workflow_evaluations/list_runs?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as ListWorkflowEvaluationRunsResponse;
}
/**
* Counts workflow evaluation runs.
* @returns A promise that resolves with the workflow evaluation run count
* @throws Error if the request fails
*/
async countWorkflowEvaluationRuns(): Promise<number> {
const response = await this.fetch(
"/internal/workflow_evaluations/runs/count",
{ method: "GET" },
);
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as CountWorkflowEvaluationRunsResponse;
return body.count;
}
/**
* Gets workflow evaluation runs by their IDs.
* @param runIds - Array of run IDs to fetch
* @param projectName - Optional project name to filter by
* @returns A promise that resolves with the workflow evaluation runs response
* @throws Error if the request fails
*/
async getWorkflowEvaluationRuns(
runIds: string[],
projectName?: string,
): Promise<GetWorkflowEvaluationRunsResponse> {
const searchParams = new URLSearchParams();
searchParams.append("run_ids", runIds.join(","));
if (projectName) {
searchParams.append("project_name", projectName);
}
const queryString = searchParams.toString();
const endpoint = `/internal/workflow_evaluations/get_runs?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetWorkflowEvaluationRunsResponse;
}
/**
* Fetches statistics for a workflow evaluation run, grouped by metric name.
* @param runId - The ID of the workflow evaluation run
* @param metricName - Optional metric name to filter by
* @returns A promise that resolves with the workflow evaluation run statistics response
* @throws Error if the request fails
*/
async getWorkflowEvaluationRunStatistics(
runId: string,
metricName?: string,
): Promise<GetWorkflowEvaluationRunStatisticsResponse> {
const searchParams = new URLSearchParams();
searchParams.append("run_id", runId);
if (metricName) {
searchParams.append("metric_name", metricName);
}
const queryString = searchParams.toString();
const endpoint = `/internal/workflow_evaluations/run_statistics?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetWorkflowEvaluationRunStatisticsResponse;
}
/**
* Lists workflow evaluation run episodes grouped by task name.
*
* Returns episodes grouped by task_name. Episodes with NULL task_name are grouped
* individually using a generated key based on their episode_id.
*
* @param runIds - List of run IDs to filter episodes by
* @param limit - Maximum number of groups to return (default: 15)
* @param offset - Number of groups to skip (default: 0)
* @returns A promise that resolves with the grouped episodes response
* @throws Error if the request fails
*/
async listWorkflowEvaluationRunEpisodesByTaskName(
runIds: string[],
limit: number = 15,
offset: number = 0,
): Promise<ListWorkflowEvaluationRunEpisodesByTaskNameResponse> {
const searchParams = new URLSearchParams();
if (runIds.length > 0) {
searchParams.append("run_ids", runIds.join(","));
}
searchParams.append("limit", limit.toString());
searchParams.append("offset", offset.toString());
const queryString = searchParams.toString();
const endpoint = `/internal/workflow_evaluations/episodes_by_task_name${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as ListWorkflowEvaluationRunEpisodesByTaskNameResponse;
}
/**
* Counts the number of distinct episode groups (by task_name) for the given run IDs.
*
* Episodes with NULL task_name are counted as individual groups.
*
* @param runIds - List of run IDs to filter episodes by
* @returns A promise that resolves with the count of episode groups
* @throws Error if the request fails
*/
async countWorkflowEvaluationRunEpisodeGroupsByTaskName(
runIds: string[],
): Promise<number> {
const searchParams = new URLSearchParams();
if (runIds.length > 0) {
searchParams.append("run_ids", runIds.join(","));
}
const queryString = searchParams.toString();
const endpoint = `/internal/workflow_evaluations/episodes_by_task_name/count${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body =
(await response.json()) as CountWorkflowEvaluationRunEpisodesByTaskNameResponse;
return body.count;
}
/**
* Gets workflow evaluation run episodes with their feedback for a specific run.
* @param runId - The run ID to get episodes for
* @param limit - Maximum number of episodes to return (default: 15)
* @param offset - Offset for pagination (default: 0)
* @returns A promise that resolves with the workflow evaluation run episodes response
* @throws Error if the request fails
*/
async getWorkflowEvaluationRunEpisodesWithFeedback(
runId: string,
limit: number = 15,
offset: number = 0,
): Promise<GetWorkflowEvaluationRunEpisodesWithFeedbackResponse> {
const searchParams = new URLSearchParams();
searchParams.append("run_id", runId);
searchParams.append("limit", limit.toString());
searchParams.append("offset", offset.toString());
const queryString = searchParams.toString();
const endpoint = `/internal/workflow_evaluations/run_episodes?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetWorkflowEvaluationRunEpisodesWithFeedbackResponse;
}
/**
* Counts the total number of episodes for a workflow evaluation run.
* @param runId - The run ID to count episodes for
* @returns A promise that resolves with the count of episodes
* @throws Error if the request fails
*/
async countWorkflowEvaluationRunEpisodes(runId: string): Promise<number> {
const searchParams = new URLSearchParams();
searchParams.append("run_id", runId);
const queryString = searchParams.toString();
const endpoint = `/internal/workflow_evaluations/run_episodes/count?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body =
(await response.json()) as CountWorkflowEvaluationRunEpisodesResponse;
return body.count;
}
/**
* Lists inference metadata with optional cursor-based pagination and filtering.
* @param params - Optional pagination and filter parameters
* @param params.before - Cursor to fetch records before this ID (mutually exclusive with after)
* @param params.after - Cursor to fetch records after this ID (mutually exclusive with before)
* @param params.limit - Maximum number of records to return
* @param params.function_name - Optional function name to filter by
* @param params.variant_name - Optional variant name to filter by
* @param params.episode_id - Optional episode ID to filter by
* @returns A promise that resolves with the inference metadata response
* @throws Error if the request fails
*/
async listInferenceMetadata(params?: {
before?: string;
after?: string;
limit?: number;
function_name?: string | null;
variant_name?: string | null;
episode_id?: string | null;
}): Promise<ListInferenceMetadataResponse> {
const searchParams = new URLSearchParams();
if (params?.before) {
searchParams.append("before", params.before);
}
if (params?.after) {
searchParams.append("after", params.after);
}
if (params?.limit !== undefined) {
searchParams.append("limit", params.limit.toString());
}
if (params?.function_name) {
searchParams.append("function_name", params.function_name);
}
if (params?.variant_name) {
searchParams.append("variant_name", params.variant_name);
}
if (params?.episode_id) {
searchParams.append("episode_id", params.episode_id);
}
const queryString = searchParams.toString();
const endpoint = `/internal/inference_metadata${queryString ? `?${queryString}` : ""}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as ListInferenceMetadataResponse;
}
/**
* Lists episodes with pagination support.
* @param limit - Maximum number of episodes to return
* @param before - Return episodes before this episode_id (for pagination)
* @param after - Return episodes after this episode_id (for pagination)
* @returns A promise that resolves with an array of episodes
* @throws Error if the request fails
*/
async listEpisodes(
limit: number,
before?: string,
after?: string,
): Promise<ListEpisodesResponse> {
const searchParams = new URLSearchParams();
searchParams.append("limit", limit.toString());
if (before) {
searchParams.append("before", before);
}
if (after) {
searchParams.append("after", after);
}
const queryString = searchParams.toString();
const endpoint = `/internal/episodes?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as ListEpisodesResponse;
}
/**
* Queries episode table bounds (first_id, last_id, and count).
* @returns A promise that resolves with the bounds information
* @throws Error if the request fails
*/
async queryEpisodeTableBounds(): Promise<TableBoundsWithCount> {
const endpoint = `/internal/episodes/bounds`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as TableBoundsWithCount;
}
/**
* Counts inferences matching the given parameters.
* When output_source is "demonstration", only inferences with demonstration feedback are counted.
* @param request - The count inferences request parameters
* @returns A promise that resolves with the count of matching inferences
* @throws Error if the request fails
*/
async countInferences(request: CountInferencesRequest): Promise<number> {
const endpoint = "/internal/inferences/count";
const response = await this.fetch(endpoint, {
method: "POST",
body: JSON.stringify(request),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const result = (await response.json()) as CountInferencesResponse;
return Number(result.count);
}
/**
* Gets inference counts for a specific episode.
* @param episode_id - The UUID of the episode
* @returns A promise that resolves with the inference counts
* @throws Error if the request fails
*/
async getEpisodeInferenceCount(
episode_id: string,
): Promise<GetEpisodeInferenceCountResponse> {
const endpoint = `/internal/episodes/${episode_id}/inference_count`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetEpisodeInferenceCountResponse;
}
/**
* Searches evaluation runs by run ID, evaluation name, dataset name, or variant name.
* @param query - The search query (case-insensitive)
* @param functionName - Function name filter
* @param evaluationName - Optional evaluation name filter
* @param limit - Maximum number of results to return (default: 100)
* @param offset - Number of results to skip (default: 0)
* @returns A promise that resolves with the search results
* @throws Error if the request fails
*/
async searchEvaluationRuns(
query: string,
functionName: string,
evaluationName?: string,
limit: number = 100,
offset: number = 0,
): Promise<SearchEvaluationRunsResponse> {
const searchParams = new URLSearchParams();
searchParams.append("function_name", functionName);
if (evaluationName) {
searchParams.append("evaluation_name", evaluationName);
}
searchParams.append("query", query);
searchParams.append("limit", limit.toString());
searchParams.append("offset", offset.toString());
const queryString = searchParams.toString();
const endpoint = `/internal/evaluations/runs/search?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as SearchEvaluationRunsResponse;
}
/**
* Queries feedback bounds for a given target ID.
* @param targetId - The target ID (inference_id or episode_id) to query feedback bounds for
* @returns A promise that resolves with the feedback bounds across all feedback types
* @throws Error if the request fails
*/
async getFeedbackBoundsByTargetId(
targetId: string,
): Promise<GetFeedbackBoundsResponse> {
const endpoint = `/internal/feedback/${encodeURIComponent(targetId)}/bounds`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetFeedbackBoundsResponse;
}
/**
* Queries the latest feedback ID for each metric for a given target.
* @param targetId - The target ID (inference_id or episode_id) to query feedback for
* @returns A promise that resolves with a mapping of metric names to their latest feedback IDs
* @throws Error if the request fails
*/
async getLatestFeedbackIdByMetric(
targetId: string,
): Promise<Record<string, string>> {
const endpoint = `/internal/feedback/${encodeURIComponent(targetId)}/latest_id_by_metric`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as LatestFeedbackIdByMetricResponse;
// Convert optional values to non-optional (ts-rs generates HashMap as optional, but values are always present)
return Object.fromEntries(
Object.entries(body.feedback_id_by_metric).filter(
(entry): entry is [string, string] => entry[1] !== undefined,
),
);
}
/**
* Queries the count of feedback for a given target ID.
* @param targetId - The target ID (inference_id or episode_id) to count feedback for
* @returns A promise that resolves with the feedback count
* @throws Error if the request fails
*/
async countFeedbackByTargetId(targetId: string): Promise<number> {
const endpoint = `/internal/feedback/${encodeURIComponent(targetId)}/count`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body = (await response.json()) as CountFeedbackByTargetIdResponse;
return Number(body.count);
}
/**
* Gets cumulative feedback time series for a function and metric.
* @param functionName - The name of the function to get feedback for
* @param metricName - The name of the metric to get feedback for
* @param timeWindow - The time window granularity for grouping data
* @param maxPeriods - Maximum number of time periods to return
* @param variantNames - Optional array of variant names to filter by
* @returns A promise that resolves with cumulative feedback time series data
* @throws Error if the request fails
*/
async getCumulativeFeedbackTimeseries(params: {
function_name: string;
metric_name: string;
time_window: TimeWindow;
max_periods: number;
variant_names?: string[];
}): Promise<CumulativeFeedbackTimeSeriesPoint[]> {
const searchParams = new URLSearchParams({
function_name: params.function_name,
metric_name: params.metric_name,
time_window: params.time_window,
max_periods: params.max_periods.toString(),
});
if (params.variant_names && params.variant_names.length > 0) {
searchParams.append("variant_names", params.variant_names.join(","));
}
const endpoint = `/internal/feedback/timeseries?${searchParams.toString()}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const body =
(await response.json()) as GetCumulativeFeedbackTimeseriesResponse;
return body.timeseries;
}
/**
* Gets information about specific evaluation runs.
* @param evaluationRunIds - Array of evaluation run UUIDs to query
* @param functionName - The name of the function being evaluated
* @returns A promise that resolves with information about the evaluation runs
* @throws Error if the request fails
*/
async getEvaluationRunInfos(
evaluationRunIds: string[],
functionName: string,
): Promise<GetEvaluationRunInfosResponse> {
const searchParams = new URLSearchParams();
searchParams.append("evaluation_run_ids", evaluationRunIds.join(","));
searchParams.append("function_name", functionName);
const queryString = searchParams.toString();
const endpoint = `/internal/evaluations/run_infos?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetEvaluationRunInfosResponse;
}
/**
* Gets evaluation run infos for a specific datapoint.
* @param datapointId - The UUID of the datapoint to query
* @param functionName - The name of the function being evaluated
* @returns A promise that resolves with information about evaluation runs that include this datapoint
* @throws Error if the request fails
*/
async getEvaluationRunInfosForDatapoint(
datapointId: string,
functionName: string,
): Promise<GetEvaluationRunInfosResponse> {
const searchParams = new URLSearchParams();
searchParams.append("function_name", functionName);
const queryString = searchParams.toString();
const endpoint = `/internal/evaluations/datapoints/${encodeURIComponent(datapointId)}/run_infos?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetEvaluationRunInfosResponse;
}
/**
* Gets evaluation statistics (aggregated metrics) for specified evaluation runs.
* @param functionName - The name of the function being evaluated
* @param functionType - The type of function: "chat" or "json"
* @param metricNames - Array of metric names to query
* @param evaluationRunIds - Array of evaluation run UUIDs to query
* @returns A promise that resolves with aggregated statistics for each run/metric
* @throws Error if the request fails
*/
async getEvaluationStatistics(
functionName: string,
functionType: "chat" | "json",
metricNames: string[],
evaluationRunIds: string[],
): Promise<GetEvaluationStatisticsResponse> {
const searchParams = new URLSearchParams();
searchParams.append("function_name", functionName);
searchParams.append("function_type", functionType);
searchParams.append("metric_names", metricNames.join(","));
searchParams.append("evaluation_run_ids", evaluationRunIds.join(","));
const queryString = searchParams.toString();
const endpoint = `/internal/evaluations/statistics?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetEvaluationStatisticsResponse;
}
/**
* Gets aggregated usage statistics (tokens, cost, processing time) for evaluation runs.
* @param functionName - The name of the function being evaluated
* @param functionType - The type of function: "chat" or "json"
* @param evaluationRunIds - Array of evaluation run UUIDs to query
* @returns A promise that resolves with aggregated usage statistics per run
* @throws Error if the request fails
*/
async getEvaluationUsageStatistics(
functionName: string,
functionType: "chat" | "json",
evaluationRunIds: string[],
): Promise<GetEvaluationUsageStatisticsResponse> {
const searchParams = new URLSearchParams();
searchParams.append("function_name", functionName);
searchParams.append("function_type", functionType);
searchParams.append("evaluation_run_ids", evaluationRunIds.join(","));
const queryString = searchParams.toString();
const endpoint = `/internal/evaluations/usage_statistics?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetEvaluationUsageStatisticsResponse;
}
/**
* Gets metadata for an evaluation run from the database.
* @param evaluationRunIds - The UUIDs of the evaluation runs
* @returns A promise that resolves with the run metadata
* @throws Error if the request fails or the run is not found
*/
async getEvaluationRunMetadata(
evaluationRunIds: string[],
): Promise<GetEvaluationRunMetadataResponse> {
const searchParams = new URLSearchParams();
searchParams.append("evaluation_run_ids", evaluationRunIds.join(","));
const endpoint = `/internal/evaluations/run_metadata?${searchParams.toString()}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetEvaluationRunMetadataResponse;
}
/**
* Gets paginated evaluation results across one or more evaluation runs.
* @param evaluationRunIds - Array of evaluation run UUIDs to query
* @param options - Optional parameters for filtering and pagination
* @param options.evaluationName - Optional name of the evaluation (resolved from DB if omitted)
* @param options.datapointId - Optional datapoint ID to filter results to a specific datapoint
* @param options.limit - Maximum number of datapoints to return (default: 100)
* @param options.offset - Number of datapoints to skip (default: 0)
* @returns A promise that resolves with the evaluation results
* @throws Error if the request fails
*/
async getEvaluationResults(
evaluationRunIds: string[],
options: {
evaluationName?: string;
datapointId?: string;
limit?: number;
offset?: number;
} = {},
): Promise<GetEvaluationResultsResponse> {
const { evaluationName, datapointId, limit = 100, offset = 0 } = options;
const searchParams = new URLSearchParams();
if (evaluationName) {
searchParams.append("evaluation_name", evaluationName);
}
searchParams.append("evaluation_run_ids", evaluationRunIds.join(","));
if (datapointId) {
searchParams.append("datapoint_id", datapointId);
}
if (limit) {
searchParams.append("limit", limit.toString());
}
if (offset) {
searchParams.append("offset", offset.toString());
}
const queryString = searchParams.toString();
const endpoint = `/internal/evaluations/results?${queryString}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as GetEvaluationResultsResponse;
}
/**
* Runs an evaluation via SSE streaming.
* @param params - The evaluation parameters
* @param params.evaluationConfig - The evaluation configuration object
* @param params.functionConfig - The function configuration for validation
* @param params.evaluationName - The name of the evaluation
* @param params.datasetName - The name of the dataset to evaluate (optional)
* @param params.datapointIds - Specific datapoint IDs to evaluate (optional)
* @param params.variantName - The name of the variant to use (optional)
* @param params.internalDynamicVariantConfig - Dynamic variant configuration (optional)
* @param params.concurrency - Number of concurrent requests
* @param params.inferenceCache - Cache mode for inference
* @param params.maxDatapoints - Maximum number of datapoints to evaluate (optional)
* @param params.precisionTargets - Per-evaluator precision targets (optional)
* @param params.onEvent - Callback for SSE events
* @returns A promise that resolves when the evaluation completes
*/
async runEvaluationStreaming(
params: RunEvaluationStreamingParams,
): Promise<void> {
const {
datasetName,
datapointIds,
variantName,
internalDynamicVariantConfig,
concurrency,
inferenceCache,
maxDatapoints,
precisionTargets,
onEvent,
signal,
} = params;
const requestBodyBase = {
dataset_name: datasetName,
datapoint_ids: datapointIds,
variant_name: variantName,
internal_dynamic_variant_config: internalDynamicVariantConfig,
concurrency,
inference_cache: inferenceCache,
max_datapoints: maxDatapoints,
precision_targets: precisionTargets,
};
const requestBody: RunEvaluationRequest =
"evaluationConfig" in params
? {
...requestBodyBase,
evaluation_config: params.evaluationConfig,
function_config: params.functionConfig,
evaluation_name: params.evaluationName,
}
: {
...requestBodyBase,
function_name: params.functionName,
evaluator_names: params.evaluatorNames,
};
const response = await this.fetch("/internal/evaluations/run", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "text/event-stream",
},
body: JSON.stringify(requestBody),
signal,
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
if (!response.body) {
throw new Error("Response body is null");
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
buffer += decoder.decode(value, { stream: true });
// Process complete SSE events in the buffer
const lines = buffer.split("\n");
buffer = lines.pop() || ""; // Keep incomplete line in buffer
for (const line of lines) {
if (line.startsWith("data:")) {
const data = line.slice(5).trim();
if (data) {
try {
const event = JSON.parse(data) as EvaluationRunEvent;
onEvent(event);
} catch (e) {
logger.error(
"Failed to parse SSE event for runEvaluationStreaming:",
e,
"Data:",
data,
);
}
}
}
}
}
// Process any remaining data in buffer
if (buffer.startsWith("data:")) {
const data = buffer.slice(5).trim();
if (data) {
try {
const event = JSON.parse(data) as EvaluationRunEvent;
onEvent(event);
} catch (e) {
logger.error(
"Failed to parse final SSE event for runEvaluationStreaming:",
e,
"Data:",
data,
);
}
}
}
} finally {
reader.releaseLock();
}
}
/**
* Launches an experimental optimization workflow.
* @param params - The optimization workflow parameters
* @returns A promise that resolves with the job handle (opaque base64-encoded string)
* @throws Error if the request fails
*/
async experimentalLaunchOptimizationWorkflow(
params: LaunchOptimizationWorkflowParams,
): Promise<OptimizationJobHandle> {
const response = await this.fetch("/experimental_optimization_workflow", {
method: "POST",
body: JSON.stringify(params),
});
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
const encodedJobHandle = await response.text();
// TODO(shuyangli): We should avoid decoding the job handle, but the UI needs to decode
// it for the job URL from the provider.
const decoded = Buffer.from(encodedJobHandle, "base64url").toString(
"utf-8",
);
return JSON.parse(decoded) as OptimizationJobHandle;
}
/**
* Polls for the status of an experimental optimization job.
* @param jobHandle - The opaque job handle string returned from experimentalLaunchOptimizationWorkflow
* @returns A promise that resolves with the optimization job info
* @throws Error if the request fails
*/
async experimentalPollOptimization(
jobHandle: OptimizationJobHandle,
): Promise<OptimizationJobInfo> {
const encodedJobHandle = Buffer.from(JSON.stringify(jobHandle)).toString(
"base64url",
);
const response = await this.fetch(
`/experimental_optimization/${encodeURIComponent(encodedJobHandle)}`,
{ method: "GET" },
);
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as OptimizationJobInfo;
}
/**
* Resolves a UUID to determine what type(s) of object it represents.
* @param id - The UUID to resolve
* @returns A promise that resolves with the resolved object types
* @throws Error if the request fails
*/
async resolveUuid(id: string): Promise<ResolveUuidResponse> {
const endpoint = `/internal/resolve_uuid/${encodeURIComponent(id)}`;
const response = await this.fetch(endpoint, { method: "GET" });
if (!response.ok) {
const message = await this.getErrorText(response);
this.handleHttpError({ message, response });
}
return (await response.json()) as ResolveUuidResponse;
}
}
/**
* Parameters for running an evaluation via SSE streaming.
*/
type RunEvaluationStreamingNamedParams = {
/** The evaluation configuration */
evaluationConfig: EvaluationConfig;
/** The function configuration for output schema validation */
functionConfig: EvaluationFunctionConfig;
/** Name of the evaluation */
evaluationName: string;
};
type RunEvaluationStreamingEvaluatorParams = {
/** Function name to evaluate */
functionName: string;
/** Top-level evaluator names to run */
evaluatorNames: string[];
};
export type RunEvaluationStreamingParams = (
| RunEvaluationStreamingNamedParams
| RunEvaluationStreamingEvaluatorParams
) & {
/** Name of the dataset to evaluate (optional) */
datasetName?: string;
/** Specific datapoint IDs to evaluate (optional) */
datapointIds?: string[];
/** Variant name to use (optional) */
variantName?: string;
/** Dynamic variant configuration (optional) */
internalDynamicVariantConfig?: UninitializedVariantInfo;
/** Number of concurrent requests */
concurrency: number;
/** Cache mode for inference requests */
inferenceCache: CacheEnabledMode;
/** Maximum number of datapoints to evaluate (optional) */
maxDatapoints?: number;
/** Per-evaluator precision targets for adaptive stopping (optional) */
precisionTargets?: Record<string, number>;
/** Callback for SSE events */
onEvent: (event: EvaluationRunEvent) => void;
/** Optional signal to abort the request */
signal?: AbortSignal;
};