import {z} from 'zod' export type GetApiAiProvidersArgs = {limit?: number | undefined, offset?: number | undefined}; export type GetApiAiProvidersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const aiProviderEnterpriseConfigurationActionRule = z.object({action: z.enum(["pre_translate", "alignment", "qa_check"]).optional(), availableAiModelIds: z.array(z.string()).optional()}); export const enterpriseAiProvider = z.object({id: z.number().int().optional(), name: z.string().optional(), type: z.string().optional(), credentials: z.union([z.object({apiKey: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({resourceName: z.string(), apiKey: z.string(), deploymentName: z.string(), apiVersion: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({project: z.string(), region: z.string(), serviceAccountKey: z.object({}).optional(), workloadIdentityFederationAudience: z.string().optional(), serviceAccountEmail: z.string().optional(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({apiKey: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({apiKey: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({apiKey: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({apiKey: z.string(), projectId: z.string(), region: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({apiKey: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({deployments: z.array(z.object({deploymentName: z.string(), targetUri: z.string(), apiKey: z.string()})), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({identifier: z.string(), key: z.string()})]).nullable().optional(), config: z.object({actionRules: z.array(aiProviderEnterpriseConfigurationActionRule).optional()}).optional(), isEnabled: z.boolean().optional(), useSystemCredentials: z.boolean().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), promptsCount: z.number().optional()}); export const enterpriseAiProviderResource = z.object({data: enterpriseAiProvider}); export const pagination = z.object({offset: z.number().int().optional(), limit: z.number().int().optional()}); export const enterpriseAiProviderCollectionResource = z.object({data: z.array(enterpriseAiProviderResource), 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 Providers * * GET /ai/providers */ export const getApiAiProviders = async (args: GetApiAiProvidersArgs, options?: GetApiAiProvidersOptions) => { 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)) const url = `${baseUrl.replace(/\/+$/, '')}/ai/providers${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(enterpriseAiProviderCollectionResource, await res.json()) };