import {z} from 'zod' export type GetApiApiV0AgentTracesFeedbackArgs = {limit?: number | undefined, offset?: number | undefined, rating?: (string | null) | undefined, department?: (string | null) | undefined, class_name?: (string | null) | undefined, q?: (string | null) | undefined}; export type GetApiApiV0AgentTracesFeedbackOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const traceUserResponse = z.object({id: z.string(), name: z.string().nullable().optional(), email: z.string().nullable().optional(), department: z.string().nullable().optional(), is_viewer: z.boolean().optional()}); export const answerFeedbackResponse = z.object({id: z.string(), session_id: z.string(), message_id: z.string(), session_name: z.string().nullable().optional(), question: z.string(), answer: z.string(), rating: z.string(), reason: z.string().nullable().optional(), comment: z.string().nullable().optional(), classes: z.array(z.string()), user: traceUserResponse, created_at: z.string()}); export const paginationMetadata = z.object({total: z.number().int(), limit: z.number().int(), offset: z.number().int(), has_more: z.boolean()}); export const answerFeedbackListResponse = z.object({items: z.array(answerFeedbackResponse), pagination: paginationMetadata, departments: z.array(z.string()), classes: z.array(z.string())}); 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 }; /** * List rated answers * * GET /api/v0/agent/traces/feedback */ export const getApiApiV0AgentTracesFeedback = async (args: GetApiApiV0AgentTracesFeedbackArgs, options: GetApiApiV0AgentTracesFeedbackOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.rating != null) query.append("rating", String(args.rating)) if (args.department != null) query.append("department", String(args.department)) if (args.class_name != null) query.append("class_name", String(args.class_name)) if (args.q != null) query.append("q", String(args.q)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/agent/traces/feedback${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(answerFeedbackListResponse, await res.json()) };