UNPKG

gladia

Version:

Official TypeScript SDK for Gladia - State-of-the-art Speech to Text API

3 lines (2 loc) 17.2 kB
"use strict";function t(t,r,e,n,o,i,a){try{var u=t[i](a),s=u.value}catch(t){return void e(t)}u.done?r(s):Promise.resolve(s).then(n,o)}function r(r){return function(){var e=this,n=arguments;return new Promise((function(o,i){var a=r.apply(e,n);function u(r){t(a,o,i,u,s,"next",r)}function s(r){t(a,o,i,u,s,"throw",r)}u(void 0)}))}}function e(t,r,e){if(i())return Reflect.construct.apply(null,arguments);var n=[null];n.push.apply(n,r);var o=new(t.bind.apply(t,n));return e&&u(o,e.prototype),o}function n(){return(n=Object.assign?Object.assign.bind():function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)({}).hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t}).apply(null,arguments)}function o(t){return(o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function i(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(t){}return(i=function(){return!!t})()}function a(){a=function(){return r};var t,r={},e=Object.prototype,n=e.hasOwnProperty,o=Object.defineProperty||function(t,r,e){t[r]=e.value},i="function"==typeof Symbol?Symbol:{},u=i.iterator||"@@iterator",s=i.asyncIterator||"@@asyncIterator",c=i.toStringTag||"@@toStringTag";function f(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{f({},"")}catch(t){f=function(t,r,e){return t[r]=e}}function p(t,r,e,n){var i=Object.create((r&&r.prototype instanceof w?r:w).prototype),a=new A(n||[]);return o(i,"_invoke",{value:L(t,e,a)}),i}function l(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}r.wrap=p;var h="suspendedStart",d="executing",v="completed",y={};function w(){}function m(){}function g(){}var b={};f(b,u,(function(){return this}));var x=Object.getPrototypeOf,O=x&&x(x(S([])));O&&O!==e&&n.call(O,u)&&(b=O);var k=g.prototype=w.prototype=Object.create(b);function E(t){["next","throw","return"].forEach((function(r){f(t,r,(function(t){return this._invoke(r,t)}))}))}function _(t,r){function e(o,i,a,u){var s=l(t[o],t,i);if("throw"!==s.type){var c=s.arg,f=c.value;return f&&"object"==typeof f&&n.call(f,"__await")?r.resolve(f.__await).then((function(t){e("next",t,a,u)}),(function(t){e("throw",t,a,u)})):r.resolve(f).then((function(t){c.value=t,a(c)}),(function(t){return e("throw",t,a,u)}))}u(s.arg)}var i;o(this,"_invoke",{value:function(t,n){function o(){return new r((function(r,o){e(t,n,r,o)}))}return i=i?i.then(o,o):o()}})}function L(r,e,n){var o=h;return function(i,a){if(o===d)throw Error("Generator is already running");if(o===v){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var u=n.delegate;if(u){var s=P(u,n);if(s){if(s===y)continue;return s}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===h)throw o=v,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=d;var c=l(r,e,n);if("normal"===c.type){if(o=n.done?v:"suspendedYield",c.arg===y)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=v,n.method="throw",n.arg=c.arg)}}}function P(r,e){var n=e.method,o=r.iterator[n];if(o===t)return e.delegate=null,"throw"===n&&r.iterator.return&&(e.method="return",e.arg=t,P(r,e),"throw"===e.method)||"return"!==n&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var i=l(o,r.iterator,e.arg);if("throw"===i.type)return e.method="throw",e.arg=i.arg,e.delegate=null,y;var a=i.arg;return a?a.done?(e[r.resultName]=a.value,e.next=r.nextLoc,"return"!==e.method&&(e.method="next",e.arg=t),e.delegate=null,y):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,y)}function j(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function T(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function A(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(j,this),this.reset(!0)}function S(r){if(r||""===r){var e=r[u];if(e)return e.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var o=-1,i=function e(){for(;++o<r.length;)if(n.call(r,o))return e.value=r[o],e.done=!1,e;return e.value=t,e.done=!0,e};return i.next=i}}throw new TypeError(typeof r+" is not iterable")}return m.prototype=g,o(k,"constructor",{value:g,configurable:!0}),o(g,"constructor",{value:m,configurable:!0}),m.displayName=f(g,c,"GeneratorFunction"),r.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===m||"GeneratorFunction"===(r.displayName||r.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,f(t,c,"GeneratorFunction")),t.prototype=Object.create(k),t},r.awrap=function(t){return{__await:t}},E(_.prototype),f(_.prototype,s,(function(){return this})),r.AsyncIterator=_,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new _(p(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},E(k),f(k,c,"Generator"),f(k,u,(function(){return this})),f(k,"toString",(function(){return"[object Generator]"})),r.keys=function(t){var r=Object(t),e=[];for(var n in r)e.push(n);return e.reverse(),function t(){for(;e.length;){var n=e.pop();if(n in r)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=S,A.prototype={constructor:A,reset:function(r){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(T),!r)for(var e in this)"t"===e.charAt(0)&&n.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(r){if(this.done)throw r;var e=this;function o(n,o){return u.type="throw",u.arg=r,e.next=n,o&&(e.method="next",e.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],u=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var s=n.call(a,"catchLoc"),c=n.call(a,"finallyLoc");if(s&&c){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(s){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!c)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var o=this.tryEntries[e];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),y},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),T(e),y}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;T(e)}return o}}throw Error("illegal catch attempt")},delegateYield:function(r,e,n){return this.delegate={iterator:S(r),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=t),y}},r}function u(t,r){return(u=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,r){return t.__proto__=r,t})(t,r)}function s(t){var r="function"==typeof Map?new Map:void 0;return(s=function(t){if(null===t||!function(t){try{return-1!==Function.toString.call(t).indexOf("[native code]")}catch(r){return"function"==typeof t}}(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(t))return r.get(t);r.set(t,n)}function n(){return e(t,arguments,o(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),u(n,t)})(t)}Object.defineProperty(exports,"__esModule",{value:!0});var c=function(t){function r(r,e,n){var o;return(o=t.call(this,r)||this).statusCode=e,o.code=n,o.name="GladiaError",o}var e,n;return n=t,(e=r).prototype=Object.create(n.prototype),e.prototype.constructor=e,u(e,n),r}(s(Error));exports.GladiaClient=function(){function t(t){if(!t.apiKey)throw new c("API key is required");this.apiKey=t.apiKey,this.baseUrl=t.baseUrl||"https://api.gladia.io",this.defaultOptions={language:t.language,diarization:t.diarization,diarization_config:t.diarizationConfig}}var e=t.prototype;return e.request=function(){var t=r(a().mark((function t(r,e){var o,i,u,s,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===e&&(e={}),o=r.startsWith("http")?r:""+this.baseUrl+r,t.prev=2,t.next=5,fetch(o,n({},e,{headers:n({"x-gladia-key":this.apiKey,"Content-Type":"application/json"},e.headers)}));case 5:if((i=t.sent).ok){t.next=20;break}return u="API Error: "+i.status+" "+i.statusText,s=null,t.prev=9,t.next=12,i.json();case 12:u=(f=t.sent).message||u,s=f.code||null,t.next=19;break;case 17:t.prev=17,t.t0=t.catch(9);case 19:throw new c(u,i.status,s);case 20:return t.next=22,i.json();case 22:return t.abrupt("return",t.sent);case 25:if(t.prev=25,t.t1=t.catch(2),!(t.t1 instanceof c)){t.next=29;break}throw t.t1;case 29:throw new c(t.t1 instanceof Error?t.t1.message:"Network error occurred",0,"NETWORK_ERROR");case 30:case"end":return t.stop()}}),t,this,[[2,25],[9,17]])})));return function(r,e){return t.apply(this,arguments)}}(),e.waitForResult=function(){var t=r(a().mark((function t(r,e){var n,o,i,u,s,f,p,l,h,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:void 0===e&&(e={}),i=void 0===(o=(n=e).pollingInterval)?2e3:o,s=void 0===(u=n.maxRetries)?60:u,f=0;case 3:if(!(f<s)){t.next=33;break}return t.prev=4,t.next=7,this.request(r);case 7:if("done"!==(p=t.sent).status){t.next=13;break}return null!=(l=p.result)&&null!=(l=l.transcription)&&l.full_transcript||console.warn("Warning: Response missing expected transcription data"),t.abrupt("return",p);case 13:if("error"!==p.status){t.next=15;break}throw new c("Transcription failed: "+((null==(h=p.error)?void 0:h.message)||"Unknown error"),void 0,null==(d=p.error)?void 0:d.code);case 15:return console.log("Transcription status: "+p.status+" (attempt "+(f+1)+"/"+s+")"),t.next=18,new Promise((function(t){return setTimeout(t,i)}));case 18:f++,t.next=31;break;case 21:if(t.prev=21,t.t0=t.catch(4),!(t.t0 instanceof c&&404===t.t0.statusCode)){t.next=30;break}return console.log("Waiting for resource to be available (attempt "+(f+1)+"/"+s+")"),t.next=27,new Promise((function(t){return setTimeout(t,i)}));case 27:f++,t.next=31;break;case 30:throw t.t0;case 31:t.next=3;break;case 33:throw new c("Transcription timed out after "+s+" attempts",408,"TIMEOUT");case 34:case"end":return t.stop()}}),t,this,[[4,21]])})));return function(r,e){return t.apply(this,arguments)}}(),e.transcribeAudio=function(){var t=r(a().mark((function t(r,e,o){var i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===e&&(e={}),void 0===o&&(o={}),i=n({},this.defaultOptions,e),t.prev=3,t.next=6,this.request("/v2/transcription",{method:"POST",body:JSON.stringify(n({audio_url:r},i))});case 6:if((u=t.sent).id){t.next=9;break}throw new c("Invalid response: missing id");case 9:if(!e.callback){t.next=11;break}return t.abrupt("return",{id:u.id,status:u.status});case 11:return t.abrupt("return",this.waitForResult(u.result_url||this.baseUrl+"/v2/transcription/"+u.id,o));case 15:if(t.prev=15,t.t0=t.catch(3),!(t.t0 instanceof c)){t.next=24;break}if(413!==t.t0.statusCode){t.next=22;break}throw new c("File too large. Maximum file size is 500MB.",413,"PAYLOAD_TOO_LARGE");case 22:if(415!==t.t0.statusCode){t.next=24;break}throw new c("Unsupported file format. Please use a supported audio format (mp3, wav, mp4, etc.).",415,"UNSUPPORTED_MEDIA_TYPE");case 24:throw t.t0;case 25:case"end":return t.stop()}}),t,this,[[3,15]])})));return function(r,e,n){return t.apply(this,arguments)}}(),e.transcribeVideo=function(){var t=r(a().mark((function t(r,e,n){return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===e&&(e={}),void 0===n&&(n={}),t.abrupt("return",this.transcribeAudio(r,e,n));case 3:case"end":return t.stop()}}),t,this)})));return function(r,e,n){return t.apply(this,arguments)}}(),e.transcribeFile=function(){var t=r(a().mark((function t(r,e,n){var o,i,u,s,f,p,l,h,d,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===e&&(e={}),void 0===n&&(n={}),i=r.name||"audio_"+Date.now()+".mp3",u=(null==(o=i.split(".").pop())?void 0:o.toLowerCase())||"mp3",s=new File([r],"audio."+u,{type:r.type||"audio/"+u}),(f=new FormData).append("audio",s),t.prev=7,t.next=10,fetch(this.baseUrl+"/v2/upload",{method:"POST",headers:{"x-gladia-key":this.apiKey},body:f});case 10:if((p=t.sent).ok){t.next=19;break}return t.next=14,p.text();case 14:l=t.sent,h="Upload failed: "+p.status+" "+p.statusText,d={};try{d=JSON.parse(l),h=d.message||h}catch(t){h=l||h}throw new c(h,p.status,d.code);case 19:return t.next=21,p.json();case 21:if((v=t.sent).audio_url){t.next=24;break}throw new c("Upload successful but no audio URL returned");case 24:return t.abrupt("return",this.transcribeAudio(v.audio_url,e,n));case 27:if(t.prev=27,t.t0=t.catch(7),!(t.t0 instanceof c)){t.next=31;break}throw t.t0;case 31:throw new c(t.t0 instanceof Error?"File upload failed: "+t.t0.message:"File upload failed",0,"UPLOAD_ERROR");case 32:case"end":return t.stop()}}),t,this,[[7,27]])})));return function(r,e,n){return t.apply(this,arguments)}}(),e.getTranscription=function(){var t=r(a().mark((function t(r){return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",this.request("/v2/transcription/"+r));case 1:case"end":return t.stop()}}),t,this)})));return function(r){return t.apply(this,arguments)}}(),e.deleteTranscription=function(){var t=r(a().mark((function t(r){return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.request("/v2/transcription/"+r,{method:"DELETE"});case 2:case"end":return t.stop()}}),t,this)})));return function(r){return t.apply(this,arguments)}}(),e.createRealTimeTranscription=function(t){void 0===t&&(t={});var r=new URL(this.baseUrl+"/v2/live");r.searchParams.append("x-gladia-key",this.apiKey),Object.entries(t).forEach((function(t){var e=t[0],n=t[1];"object"==typeof n?r.searchParams.append(e,JSON.stringify(n)):void 0!==n&&r.searchParams.append(e,String(n))}));var e=new WebSocket(r.toString()),n=e;return n.sendAudio=function(t){if(e.readyState===WebSocket.OPEN)if(t instanceof Blob){var r=new FileReader;r.onload=function(){e.send(r.result)},r.readAsArrayBuffer(t)}else e.send(t)},n.stopRecording=function(){e.readyState===WebSocket.OPEN&&e.send(JSON.stringify({action:"stop_recording"}))},n},e.translateAudio=function(){var t=r(a().mark((function t(r,e,o){var i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===o&&(o={}),i=Array.isArray(e)?e:[e],t.abrupt("return",this.transcribeAudio(r,n({},o,{translation:!0,translation_config:{target_languages:i}})));case 3:case"end":return t.stop()}}),t,this)})));return function(r,e,n){return t.apply(this,arguments)}}(),e.summarizeAudio=function(){var t=r(a().mark((function t(r,e){return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===e&&(e={}),t.abrupt("return",this.transcribeAudio(r,n({},e,{summarization:!0})));case 2:case"end":return t.stop()}}),t,this)})));return function(r,e){return t.apply(this,arguments)}}(),e.analyzeSentiment=function(){var t=r(a().mark((function t(r,e){return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===e&&(e={}),t.abrupt("return",this.transcribeAudio(r,n({},e,{sentiment_analysis:!0})));case 2:case"end":return t.stop()}}),t,this)})));return function(r,e){return t.apply(this,arguments)}}(),e.detectEntities=function(){var t=r(a().mark((function t(r,e){return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===e&&(e={}),t.abrupt("return",this.transcribeAudio(r,n({},e,{named_entity_recognition:!0})));case 2:case"end":return t.stop()}}),t,this)})));return function(r,e){return t.apply(this,arguments)}}(),e.moderateContent=function(){var t=r(a().mark((function t(r,e){return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===e&&(e={}),t.abrupt("return",this.transcribeAudio(r,n({},e,{content_moderation:!0})));case 2:case"end":return t.stop()}}),t,this)})));return function(r,e){return t.apply(this,arguments)}}(),e.generateChapters=function(){var t=r(a().mark((function t(r,e){return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===e&&(e={}),t.abrupt("return",this.transcribeAudio(r,n({},e,{chapterization:!0})));case 2:case"end":return t.stop()}}),t,this)})));return function(r,e){return t.apply(this,arguments)}}(),t}(),exports.GladiaError=c; //# sourceMappingURL=gladia.cjs.production.min.js.map