import {z} from 'zod' export type AiPromptCompletionResourcesOverridePromptValuesEnterprisePreTranslate = {sourceLanguage?: string | undefined, targetLanguage?: string | undefined, strings?: string | undefined, tm?: string | undefined, terms?: string | undefined, fileName?: string | undefined, fileContext?: string | undefined, siblingsStrings?: string | undefined, projectName?: string | undefined, projectDescription?: string | undefined, projectPublicDescription?: string | undefined, pluralForms?: string | undefined, fileContent?: string | undefined, organizationName?: string | undefined, organizationDescription?: string | undefined} | {[key: string]: string}; export type AiPromptCompletionResourcesEnterprisePreTranslate = {projectId: number, sourceLanguageId?: string | undefined, targetLanguageId?: string | undefined, stringIds?: Array | undefined, overridePromptValues?: AiPromptCompletionResourcesOverridePromptValuesEnterprisePreTranslate | undefined}; export type AiPromptCompletionResourcesOverridePromptValuesEnterpriseAlignment = {sourceLanguage?: string | undefined, targetLanguage?: string | undefined, alignmentPairs?: string | undefined, projectDescription?: string | undefined, projectPublicDescription?: string | undefined, organizationName?: string | undefined, organizationDescription?: string | undefined} | {[key: string]: string}; export type AiPromptCompletionResourcesEnterpriseAlignment = {projectId: number, sourceLanguageId?: string | undefined, targetLanguageId?: string | undefined, stringIds?: Array | undefined, overridePromptValues?: AiPromptCompletionResourcesOverridePromptValuesEnterpriseAlignment | undefined}; export type AiPromptCompletionResourcesOverridePromptValuesEnterpriseQaCheck = {sourceLanguage?: string | undefined, targetLanguage?: string | undefined, translationUnits?: string | undefined, tm?: string | undefined, terms?: string | undefined, fileName?: string | undefined, fileContext?: string | undefined, projectName?: string | undefined, projectDescription?: string | undefined, projectPublicDescription?: string | undefined, organizationName?: string | undefined, organizationDescription?: string | undefined} | {[key: string]: string}; export type AiPromptCompletionResourcesEnterpriseQaCheck = {projectId: number, sourceLanguageId?: string | undefined, targetLanguageId?: string | undefined, stringIds?: Array | undefined, overridePromptValues?: AiPromptCompletionResourcesOverridePromptValuesEnterpriseQaCheck | undefined}; export type AiPromptCompletionResourcesOverridePromptValuesEnterpriseCustom = {sourceLanguage?: string | undefined, targetLanguage?: string | undefined, strings?: string | undefined, tm?: string | undefined, terms?: string | undefined, fileName?: string | undefined, fileContext?: string | undefined, siblingsStrings?: string | undefined, projectName?: string | undefined, projectDescription?: string | undefined, projectPublicDescription?: string | undefined, fileContent?: string | undefined, filteredStrings?: string | undefined, organizationName?: string | undefined, organizationDescription?: string | undefined} | {[key: string]: string}; export type AiPromptCompletionResourcesEnterpriseCustom = {projectId: number, sourceLanguageId?: string | undefined, targetLanguageId?: string | undefined, stringIds?: Array | undefined, overridePromptValues?: AiPromptCompletionResourcesOverridePromptValuesEnterpriseCustom | undefined, customInstruction?: string | undefined}; export type EnterpriseAiPromptCompletionCreateForm = {resources: AiPromptCompletionResourcesEnterprisePreTranslate | AiPromptCompletionResourcesEnterpriseAlignment | AiPromptCompletionResourcesEnterpriseQaCheck | AiPromptCompletionResourcesEnterpriseCustom, tools?: (Array<{tool?: ({type: 'function', function: {description?: string | undefined, name: string, parameters?: Record | undefined}}) | undefined}>) | undefined, tool_choice?: (string | Record) | undefined}; export type CreateApiAiPromptsAiPromptIdCompletionsArgs = {body: EnterpriseAiPromptCompletionCreateForm}; export type CreateApiAiPromptsAiPromptIdCompletionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const aiPromptCompletion = z.object({identifier: z.string().optional(), status: z.string().optional(), progress: z.number().int().optional(), attributes: z.object({aiPromptId: z.number().int().optional()}).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), startedAt: z.string().optional(), finishedAt: z.string().optional(), eta: z.string().optional()}); export const aiPromptCompletionResource = z.object({data: aiPromptCompletion}); 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 }; /** * Generate AI Prompt Completion * * POST /ai/prompts/{aiPromptId}/completions */ export const createApiAiPromptsAiPromptIdCompletions = async (args: CreateApiAiPromptsAiPromptIdCompletionsArgs, options?: CreateApiAiPromptsAiPromptIdCompletionsOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/ai/prompts/${encodeURIComponent(String(args.aiPromptId))}/completions${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(aiPromptCompletionResource, await res.json()) };