import {z} from 'zod' export type FilterOperators = 'eq' | 'gt' | 'gte' | 'lt' | 'lte' | 'contains' | 'between'; export type FieldFilter = {field: string, op: FilterOperators, value: string | number | number | Array}; export type EmptyFilter = Record; export type NotFilter = {not: FieldFilter | AndFilter | OrFilter | NotFilter | EmptyFilter}; export type OrFilter = {or: Array}; export type AndFilter = {and: Array}; export type CreateApiApiV2ProjectsProjectIdSearchTracesArgs = {project_id: string, page?: number | undefined, page_size?: number | undefined, include_children?: boolean | undefined, body: {filters: FieldFilter | AndFilter | OrFilter | NotFilter | EmptyFilter}}; export type CreateApiApiV2ProjectsProjectIdSearchTracesOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const traceMetadataInfo = z.object({agent_name: z.string().nullable(), start_time: z.string().nullable(), end_time: z.string().nullable(), assignee: z.string().nullable()}); export const jSONValue: z.ZodType = z.union([z.boolean(), z.number().int(), z.number(), z.string(), z.record(z.string(), z.lazy(() => jSONValue)), z.array(z.lazy(() => jSONValue))]).nullable(); export const autoEvaluationInfo = z.object({name: z.string(), auto_score: z.union([z.number().int(), z.string()]).nullable(), auto_explanation: z.string().nullable(), manual_score: z.union([z.number().int(), z.string()]).nullable()}); export const humanLabelInfo = z.object({name: z.string(), manual_score: z.union([z.number().int(), z.string()]).nullable()}); export const evaluationResultsInfo = z.object({auto_evaluations: z.record(z.string(), autoEvaluationInfo), human_labels: z.record(z.string(), humanLabelInfo), client_evaluations: z.record(z.string(), z.union([z.boolean(), z.number()]))}); export const traceSearchInfo = z.object({session_id: z.string(), trace_id: z.string(), review_queue_id: z.string().nullable(), trace_metadata: traceMetadataInfo.nullable(), custom_metadata: z.record(z.string(), z.union([z.string(), z.number(), z.number().int(), z.boolean()])).nullable(), customer_feedback: z.record(z.string(), z.union([z.string(), z.number(), z.number().int(), z.boolean()])).nullable(), input: jSONValue.nullable(), output: jSONValue.nullable(), evaluation_results: evaluationResultsInfo}); export const completionMetadataInfo = z.object({environment: z.string().nullable(), start_time: z.string().nullable(), end_time: z.string().nullable(), model: z.string().nullable(), provider: z.string().nullable(), prompt_template: z.record(z.string(), z.union([z.string(), z.string()])).nullable(), prompt_template_version_id: z.string().nullable(), assignee: z.string().nullable(), prompt_version_name: z.string().nullable(), prompt_version_description: z.string().nullable()}); export const usageInfo = z.object({prompt_tokens: z.number().int(), completion_tokens: z.number().int()}); export const textBlock = z.object({text: z.string(), type: z.literal("text").optional()}); export const toolCallBlock = z.object({id: z.string(), name: z.string(), arguments: jSONValue, thought_signature: z.string().nullable().optional(), type: z.literal("tool_call").optional()}); export const imageBlock = z.object({media_id: z.string(), type: z.literal("image").optional()}); export const audioBlock = z.object({media_id: z.string(), type: z.literal("audio").optional()}); export const fileBlock = z.object({media_id: z.string(), filename: z.string().nullable().optional(), type: z.literal("file").optional()}); export const toolResultBlock: z.ZodType = z.object({tool_call_id: z.string(), content: z.union([z.string(), z.array(z.discriminatedUnion("type", [textBlock, z.lazy(() => toolResultBlock), toolCallBlock, imageBlock, audioBlock, fileBlock]))]), type: z.literal("tool_result").optional()}); export const userMessage = z.object({content: z.union([z.string(), z.array(z.discriminatedUnion("type", [textBlock, toolResultBlock, toolCallBlock, imageBlock, audioBlock, fileBlock]))]), role: z.literal("user").optional()}); export const systemMessage = z.object({content: z.string(), role: z.literal("system").optional()}); export const developerMessage = z.object({content: z.string(), role: z.literal("developer").optional()}); export const assistantMessage = z.object({content: z.union([z.string(), z.array(z.discriminatedUnion("type", [textBlock, toolResultBlock, toolCallBlock, imageBlock, audioBlock, fileBlock]))]), role: z.literal("assistant").optional()}); export const inputVariable: z.ZodType = z.union([z.string(), z.number().int(), z.boolean(), z.number(), z.record(z.string(), z.lazy(() => inputVariable)), z.array(z.lazy(() => inputVariable))]); export const completionSearchInfoWithChildren: z.ZodType = z.object({completion_id: z.string(), session_id: z.string(), review_queue_id: z.string().nullable(), evaluation_results: evaluationResultsInfo, completion_metadata: completionMetadataInfo, customer_feedback: z.record(z.string(), z.union([z.string(), z.number(), z.number().int(), z.boolean()])), usage: usageInfo, messages: z.array(z.discriminatedUnion("role", [userMessage, systemMessage, developerMessage, assistantMessage])), input_variables: z.record(z.string(), inputVariable), children: z.array(z.union([z.lazy(() => completionSearchInfoWithChildren), z.lazy(() => traceSearchInfoWithChildren)])).optional()}); export const traceSearchInfoWithChildren: z.ZodType = z.object({session_id: z.string(), trace_id: z.string(), review_queue_id: z.string().nullable(), trace_metadata: traceMetadataInfo.nullable(), custom_metadata: z.record(z.string(), z.union([z.string(), z.number(), z.number().int(), z.boolean()])).nullable(), customer_feedback: z.record(z.string(), z.union([z.string(), z.number(), z.number().int(), z.boolean()])).nullable(), input: jSONValue.nullable(), output: jSONValue.nullable(), evaluation_results: evaluationResultsInfo, children: z.array(z.union([completionSearchInfoWithChildren, z.lazy(() => traceSearchInfoWithChildren)])).optional()}); export const paginationInfo = z.object({page: z.number().int(), page_size: z.number().int(), has_next: z.boolean()}); export const createApiApiV2ProjectsProjectIdSearchTracesResponse = z.object({data: z.union([z.array(traceSearchInfo), z.array(traceSearchInfoWithChildren)]), pagination: paginationInfo}); 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 }; /** * Search Traces * * POST /api/v2/projects/{project_id}/search/traces */ export const createApiApiV2ProjectsProjectIdSearchTraces = async (args: CreateApiApiV2ProjectsProjectIdSearchTracesArgs, options: CreateApiApiV2ProjectsProjectIdSearchTracesOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.include_children != null) query.append("include_children", String(args.include_children)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/projects/${encodeURIComponent(String(args.project_id))}/search/traces${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(createApiApiV2ProjectsProjectIdSearchTracesResponse, await res.json()) };