import {z} from 'zod' export type SarvamModelAPISpeechToTextLanguage = 'unknown' | 'hi-IN' | 'bn-IN' | 'kn-IN' | 'ml-IN' | 'mr-IN' | 'od-IN' | 'pa-IN' | 'ta-IN' | 'te-IN' | 'en-IN' | 'gu-IN' | 'as-IN' | 'ur-IN' | 'ne-IN' | 'kok-IN' | 'ks-IN' | 'sd-IN' | 'sa-IN' | 'sat-IN' | 'mni-IN' | 'brx-IN' | 'mai-IN' | 'doi-IN'; export type SarvamModelAPISpeechToTextModel = 'saaras:v3' | 'saarika:v2.5'; export type SarvamModelAPIMode = 'transcribe' | 'translate' | 'verbatim' | 'translit' | 'codemix'; 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 SpeechToTextJobParameters = {language_code?: (SarvamModelAPISpeechToTextLanguage | null) | undefined, model?: SarvamModelAPISpeechToTextModel | undefined, mode?: (SarvamModelAPIMode | null) | undefined, with_timestamps?: boolean | 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 BulkJobInitRequestV1SpeechToTextJobParameters = {job_parameters: SpeechToTextJobParameters, callback?: (BulkJobCallback | null) | undefined}; export type CreateApiSpeechToTextJobV1Args = {'api-subscription-key': string, body: BulkJobInitRequestV1SpeechToTextJobParameters}; export type CreateApiSpeechToTextJobV1Options = {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 Bulk Job V1 * * POST /speech-to-text/job/v1 */ export const createApiSpeechToTextJobV1 = async (args: CreateApiSpeechToTextJobV1Args, options?: CreateApiSpeechToTextJobV1Options) => { 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['api-subscription-key'] != null) headers.set("api-subscription-key", String(args['api-subscription-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/speech-to-text/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()) };