import {z} from 'zod' export type RunTypeEnum = 'tool' | 'chain' | 'llm' | 'retriever' | 'embedding' | 'prompt' | 'parser'; export type RunsFilterDataSourceTypeEnum = 'current' | 'historical' | 'lite' | 'root_lite' | 'runs_feedbacks_rmt_wide'; export type RunStatsGroupBy = {attribute: 'name' | 'run_type' | 'tag' | 'metadata', path?: (string | null) | undefined, max_groups?: number | undefined}; export type RunStatsSelect = 'run_count' | 'latency_p50' | 'latency_p99' | 'latency_avg' | 'first_token_p50' | 'first_token_p99' | 'total_tokens' | 'prompt_tokens' | 'completion_tokens' | 'median_tokens' | 'completion_tokens_p50' | 'prompt_tokens_p50' | 'tokens_p99' | 'completion_tokens_p99' | 'prompt_tokens_p99' | 'last_run_start_time' | 'feedback_stats' | 'thread_feedback_stats' | 'run_facets' | 'error_rate' | 'streaming_rate' | 'total_cost' | 'prompt_cost' | 'completion_cost' | 'cost_p50' | 'cost_p99' | 'session_feedback_stats' | 'all_run_stats' | 'all_token_stats' | 'group_count' | 'prompt_token_details' | 'completion_token_details' | 'prompt_cost_details' | 'completion_cost_details'; export type RunStatsQueryParamsPublic = {id?: (Array | null) | undefined, trace?: (string | null) | undefined, parent_run?: (string | null) | undefined, run_type?: (RunTypeEnum | null) | undefined, session?: (Array | null) | undefined, reference_example?: (Array | null) | undefined, execution_order?: (number | null) | undefined, start_time?: (string | null) | undefined, end_time?: (string | null) | undefined, error?: (boolean | null) | undefined, query?: (string | null) | undefined, filter?: (string | null) | undefined, trace_filter?: (string | null) | undefined, tree_filter?: (string | null) | undefined, is_root?: (boolean | null) | undefined, data_source_type?: (RunsFilterDataSourceTypeEnum | null) | undefined, skip_pagination?: (boolean | null) | undefined, search_filter?: (string | null) | undefined, use_experimental_search?: boolean | undefined, group_by?: (RunStatsGroupBy | null) | undefined, groups?: (Array | null) | undefined, select?: (Array | null) | undefined, reference_dataset_id?: (string | null) | undefined, include_details?: boolean | undefined}; export type CreateApiApiV1PublicShareTokenDatasetsRunsStatsArgs = {share_token: string, body: RunStatsQueryParamsPublic}; export type CreateApiApiV1PublicShareTokenDatasetsRunsStatsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const runStats = z.object({run_count: z.number().int().nullable().optional(), latency_p50: z.number().nullable().optional(), latency_p99: z.number().nullable().optional(), first_token_p50: z.number().nullable().optional(), first_token_p99: z.number().nullable().optional(), total_tokens: z.number().int().nullable().optional(), prompt_tokens: z.number().int().nullable().optional(), completion_tokens: z.number().int().nullable().optional(), median_tokens: z.number().int().nullable().optional(), completion_tokens_p50: z.number().int().nullable().optional(), prompt_tokens_p50: z.number().int().nullable().optional(), tokens_p99: z.number().int().nullable().optional(), completion_tokens_p99: z.number().int().nullable().optional(), prompt_tokens_p99: z.number().int().nullable().optional(), last_run_start_time: z.string().nullable().optional(), feedback_stats: z.record(z.string(), z.unknown()).nullable().optional(), run_facets: z.array(z.record(z.string(), z.unknown())).nullable().optional(), error_rate: z.number().nullable().optional(), streaming_rate: z.number().nullable().optional(), total_cost: z.number().nullable().optional(), prompt_cost: z.number().nullable().optional(), completion_cost: z.number().nullable().optional(), cost_p50: z.number().nullable().optional(), cost_p99: z.number().nullable().optional(), prompt_token_details: z.record(z.string(), z.unknown()).nullable().optional(), completion_token_details: z.record(z.string(), z.unknown()).nullable().optional(), prompt_cost_details: z.record(z.string(), z.unknown()).nullable().optional(), completion_cost_details: z.record(z.string(), z.unknown()).nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Stats Shared Dataset Runs * * POST /api/v1/public/{share_token}/datasets/runs/stats */ export const createApiApiV1PublicShareTokenDatasetsRunsStats = async (args: CreateApiApiV1PublicShareTokenDatasetsRunsStatsArgs, options: CreateApiApiV1PublicShareTokenDatasetsRunsStatsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/public/${encodeURIComponent(String(args.share_token))}/datasets/runs/stats${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(runStats, await res.json()) };