import {z} from 'zod' export type SarvamModelAPISpeechToTextTranslateModel = 'saaras:v2.5'; export type SarvamModelAPIInputAudioCodec = 'wav' | 'x-wav' | 'wave' | 'mp3' | 'mpeg' | 'mpeg3' | 'x-mp3' | 'x-mpeg-3' | 'aac' | 'x-aac' | 'aiff' | 'x-aiff' | 'ogg' | 'opus' | 'flac' | 'x-flac' | 'mp4' | 'x-m4a' | 'amr' | 'x-ms-wma' | 'webm' | 'pcm_s16le' | 'pcm_l16' | 'pcm_raw'; export type SpeechToTextTranslateJobParameters = {prompt?: (string | null) | undefined, model?: SarvamModelAPISpeechToTextTranslateModel | undefined, with_diarization?: boolean | undefined, num_speakers?: (number | null) | undefined, input_audio_codec?: SarvamModelAPIInputAudioCodec | undefined}; export type BulkJobCallback = {url: string, auth_token?: string | undefined}; export type BulkJobInitRequestV1SpeechToTextTranslateJobParameters = {job_parameters: SpeechToTextTranslateJobParameters, callback?: (BulkJobCallback | null) | undefined}; export type CreateApiSpeechToTextTranslateJobV1Args = {ptu_id?: (number | null) | undefined, 'api-subscription-key': string, body: BulkJobInitRequestV1SpeechToTextTranslateJobParameters}; export type CreateApiSpeechToTextTranslateJobV1Options = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const storageContainerType = z.enum(["Azure", "Local", "Google", "Azure_V1"]); export const baseJobParameters = z.object({}); export const jobState = z.enum(["Accepted", "Pending", "Running", "Completed", "Failed"]); export const bulkJobInitResponse = z.object({job_id: z.string(), storage_container_type: storageContainerType, job_parameters: baseJobParameters, job_state: jobState}); 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 }; /** * Initiate Speech to Text Translate Bulk Job V1 * * POST /speech-to-text-translate/job/v1 */ export const createApiSpeechToTextTranslateJobV1 = async (args: CreateApiSpeechToTextTranslateJobV1Args, options?: CreateApiSpeechToTextTranslateJobV1Options) => { const {baseUrl = "https://api.sarvam.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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-translate/job/v1${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(bulkJobInitResponse, await res.json()) };