import {z} from 'zod' export type StructuredFilter = {field: 'pl_id' | 'prompt_id' | 'engine' | 'provider_type' | 'input_text' | 'output_text' | 'prompt_version_number' | 'input_tokens' | 'output_tokens' | 'cost' | 'latency_ms' | 'request_start_time' | 'request_end_time' | 'status' | 'is_json' | 'is_tool_call' | 'is_plain_text' | 'tags' | 'metadata_keys' | 'metadata' | 'tool_names' | 'output' | 'output_keys' | 'input_variables' | 'input_variable_keys' | 'user_intent' | 'agent_intent', operator: 'is' | 'is_not' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'starts_with' | 'ends_with' | 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'between' | 'before' | 'after' | 'is_true' | 'is_false' | 'is_empty' | 'is_not_empty' | 'is_null' | 'is_not_null' | 'key_equals' | 'key_not_equals' | 'key_contains', value?: (string | number | boolean | Array | null) | undefined, nested_key?: (string | null) | undefined}; export type StructuredFilterGroup = {logic?: ('AND' | 'OR') | undefined, filters: Array}; export type SearchRequestLogsRequest = {filter_group?: StructuredFilterGroup | undefined, q?: (string | null) | undefined, sort_by?: ('request_start_time' | 'input_tokens' | 'output_tokens' | 'cost' | 'latency_ms' | 'status' | null) | undefined, sort_order?: ('asc' | 'desc' | null) | undefined, metadata_cost_breakdown_key?: (string | null) | undefined, page?: (number | null) | undefined, per_page?: (number | null) | undefined, include_prompt_name?: (boolean | null) | undefined}; export type CreateApiApiPublicV2RequestsSearchArgs = {body: SearchRequestLogsRequest}; export type CreateApiApiPublicV2RequestsSearchOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const requestLogSearchResult = z.object({id: z.number().int().optional(), engine: z.string().optional(), provider_type: z.string().optional(), input_tokens: z.number().int().optional(), output_tokens: z.number().int().optional(), price: z.number().optional(), request_start_time: z.string().optional(), request_end_time: z.string().optional(), latency: z.number().optional(), tags_array: z.array(z.string()).optional(), metadata: z.array(z.record(z.string(), z.unknown())).optional(), scores: z.array(z.record(z.string(), z.unknown())).optional(), prompt_name: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const searchRequestLogsResponse = z.object({success: z.boolean().optional(), items: z.array(requestLogSearchResult).optional(), page: z.number().int().optional(), pages: z.number().int().optional(), per_page: z.number().int().optional(), total: z.number().int().optional(), has_next: z.boolean().optional(), has_prev: z.boolean().optional(), next_num: z.number().int().nullable().optional(), prev_num: z.number().int().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 }; /** * Search Request Logs * * POST /api/public/v2/requests/search */ export const createApiApiPublicV2RequestsSearch = async (args: CreateApiApiPublicV2RequestsSearchArgs, options: CreateApiApiPublicV2RequestsSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/v2/requests/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(searchRequestLogsResponse, await res.json()) };