import {z} from 'zod' export type CreateApiSpeechToTextJobV1JobIdStartArgs = {job_id: string, ptu_id?: (number | null) | undefined, 'api-subscription-key': string}; export type CreateApiSpeechToTextJobV1JobIdStartOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const jobState = z.enum(["Accepted", "Pending", "Running", "Completed", "Failed"]); export const storageContainerType = z.enum(["Azure", "Local", "Google", "Azure_V1"]); export const taskFileDetails = z.object({file_name: z.string(), file_id: z.string()}); export const taskState = z.enum(["Success", "API Error", "Internal Server Error"]); export const taskDetail = z.object({inputs: z.array(taskFileDetails).optional(), outputs: z.array(taskFileDetails).optional(), state: taskState.optional(), error_message: z.string().nullable().optional(), exception_name: z.string().nullable().optional()}); export const jobStatusResponse = z.object({job_state: jobState, created_at: z.string(), updated_at: z.string(), job_id: z.string(), total_files: z.number().int().optional(), successful_files_count: z.number().int().optional(), failed_files_count: z.number().int().optional(), storage_container_type: storageContainerType, error_message: z.string().optional(), job_details: z.array(taskDetail).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 }; /** * Start Speech to Text Bulk Job V1 * * POST /speech-to-text/job/v1/{job_id}/start */ export const createApiSpeechToTextJobV1JobIdStart = async (args: CreateApiSpeechToTextJobV1JobIdStartArgs, options?: CreateApiSpeechToTextJobV1JobIdStartOptions) => { const {baseUrl = "https://api.sarvam.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ptu_id != null) query.append("ptu_id", String(args.ptu_id)) if (args['api-subscription-key'] != null) headers.set("api-subscription-key", String(args['api-subscription-key'])) if (args['api-subscription-key'] != null) headers.set("api-subscription-key", String(args['api-subscription-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/speech-to-text/job/v1/${encodeURIComponent(String(args.job_id))}/start${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(jobStatusResponse, await res.json()) };