import {z} from 'zod' export type GetApiAiRequestLogsArgs = {limit?: number | undefined, offset?: number | undefined, requestId?: string | undefined, projectId?: number | undefined, userId?: number | undefined, aiProviderId?: number | undefined, model?: string | undefined, sourceAction?: ('ai_proxy' | 'ai_gateway' | 'ai_translate_strings' | 'ai_file_translate' | 'ai_prompt_completion' | 'pre_translate:manual' | 'pre_translate:workflow' | 'ai_alignment' | 'qa_check' | 'ai_suggestion' | 'advisor') | undefined, promptAction?: string | undefined, statuses?: (Array<'pending' | 'success' | 'error' | 'timeout'>) | undefined, systemCredentials?: boolean | undefined, isAutoTriggered?: boolean | undefined, tokenName?: string | undefined, oauthClientId?: string | undefined, createdAfter?: string | undefined, createdBefore?: string | undefined}; export type GetApiAiRequestLogsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const aiRequestLog = z.object({id: z.number().int().optional(), requestId: z.string().optional(), createdAt: z.string().optional(), status: z.enum(["pending", "success", "error", "timeout"]).optional(), httpStatus: z.number().int().nullable().optional(), model: z.string().optional(), sourceAction: z.enum(["ai_proxy", "ai_gateway", "ai_translate_strings", "ai_file_translate", "ai_prompt_completion", "pre_translate:manual", "pre_translate:workflow", "ai_alignment", "qa_check", "ai_suggestion", "advisor"]).optional(), promptAction: z.string().nullable().optional(), systemCredentials: z.boolean().optional(), isAutoTriggered: z.boolean().optional(), durationMs: z.number().int().nullable().optional(), inputTokens: z.number().int().nullable().optional(), outputTokens: z.number().int().nullable().optional(), totalCost: z.number().nullable().optional(), userId: z.number().int().nullable().optional(), projectId: z.number().int().nullable().optional(), promptId: z.number().int().nullable().optional(), aiProviderId: z.number().int().optional(), tokenName: z.string().nullable().optional(), oauthClientId: z.string().nullable().optional(), oauthClientName: z.string().nullable().optional(), ip: z.string().nullable().optional(), userAgent: z.string().nullable().optional(), error: z.string().nullable().optional()}); export const aiRequestLogResource = z.object({data: aiRequestLog}); export const pagination = z.object({offset: z.number().int().optional(), limit: z.number().int().optional()}); export const aiRequestLogCollectionResource = z.object({data: z.array(aiRequestLogResource), pagination: pagination.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 }; /** * List AI Request Logs * * GET /ai/request-logs */ export const getApiAiRequestLogs = async (args: GetApiAiRequestLogsArgs, options?: GetApiAiRequestLogsOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", fetch: fetchFn = fetch} = 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.requestId != null) query.append("requestId", String(args.requestId)) if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.userId != null) query.append("userId", String(args.userId)) if (args.aiProviderId != null) query.append("aiProviderId", String(args.aiProviderId)) if (args.model != null) query.append("model", String(args.model)) if (args.sourceAction != null) query.append("sourceAction", String(args.sourceAction)) if (args.promptAction != null) query.append("promptAction", String(args.promptAction)) if (args.statuses != null) query.append("statuses", args.statuses.join(',')) if (args.systemCredentials != null) query.append("systemCredentials", String(args.systemCredentials)) if (args.isAutoTriggered != null) query.append("isAutoTriggered", String(args.isAutoTriggered)) if (args.tokenName != null) query.append("tokenName", String(args.tokenName)) if (args.oauthClientId != null) query.append("oauthClientId", String(args.oauthClientId)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) const url = `${baseUrl.replace(/\/+$/, '')}/ai/request-logs${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(aiRequestLogCollectionResource, await res.json()) };