import {z} from 'zod' export type TraceFilter = {field?: string | undefined, op?: string | undefined, values?: Array | undefined}; export type TraceSort = {field?: string | undefined, order?: string | undefined}; export type SearchTracesRequest = {from: string, to: string, filters?: Array | undefined, filter_operator?: string | undefined, query?: string | undefined, sort?: Array | undefined, limit?: number | undefined, page_token?: string | undefined}; export type CreateApiV3TracesSearchArgs = {body: SearchTracesRequest}; export type CreateApiV3TracesSearchOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const traceAgent = z.object({id: z.string().optional(), name: z.string().optional()}); export const traceUsage = z.object({prompt_tokens: z.number().int().optional(), completion_tokens: z.number().int().optional(), total_tokens: z.number().int().optional(), prompt_cached_tokens: z.number().int().optional(), prompt_audio_tokens: z.number().int().optional(), completion_reasoning_tokens: z.number().int().optional(), completion_audio_tokens: z.number().int().optional(), completion_accepted_prediction_tokens: z.number().int().optional(), completion_rejected_prediction_tokens: z.number().int().optional(), prompt_cache_creation_tokens: z.number().int().optional()}); export const traceCost = z.object({input: z.number().optional(), output: z.number().optional(), cache_read: z.number().optional(), cache_write: z.number().optional(), reasoning: z.number().optional(), web_search: z.number().optional(), total: z.number().optional(), currency: z.string().optional(), cached: z.number().optional(), audio_input: z.number().optional(), audio_output: z.number().optional(), accepted_prediction: z.number().optional(), rejected_prediction: z.number().optional(), billable: z.boolean().optional(), pricing_tier: z.string().optional(), threshold_exceeded: z.boolean().optional(), integration_id: z.string().optional()}); export const traceContext = z.object({trace_id: z.string().optional(), span_id: z.string().optional()}); export const traceSummary = z.object({object: z.literal("trace").optional(), trace_id: z.string().optional(), root_span_id: z.string().optional(), leading_span_id: z.string().optional(), name: z.string().optional(), operation: z.string().optional(), status: z.string().optional(), started_at: z.string().optional(), ended_at: z.string().optional(), duration_ms: z.number().optional(), project_id: z.string().optional(), identity_id: z.string().optional(), session_id: z.string().optional(), thread_id: z.string().optional(), product: z.string().optional(), providers: z.array(z.string()).optional(), models: z.array(z.string()).optional(), agent: traceAgent.optional(), usage: traceUsage.optional(), cost: traceCost.optional(), id: z.string().optional(), span_id: z.string().optional(), parent_id: z.string().optional(), type: z.string().optional(), context: traceContext.optional(), attributes: z.object({}).optional(), start_time: z.string().optional(), end_time: z.string().optional(), duration: z.number().optional()}); export const traceSearchMeta = z.object({request_id: z.string().optional(), from: z.string().optional(), to: z.string().optional(), row_count: z.number().int().optional()}); export const searchTracesResponse = z.object({object: z.literal("list").optional(), data: z.array(traceSummary).optional(), has_more: z.boolean().optional(), next_page_token: z.string().optional(), meta: traceSearchMeta.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 }; /** * Search traces * * POST /v3/traces/search */ export const createApiV3TracesSearch = async (args: CreateApiV3TracesSearchArgs, options?: CreateApiV3TracesSearchOptions) => { const {baseUrl = "https://my.orq.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/traces/search${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(searchTracesResponse, await res.json()) };