gladia
Version:
Official TypeScript SDK for Gladia - State-of-the-art Speech to Text API
1,037 lines (1,033 loc) • 36.6 kB
JavaScript
function asyncGeneratorStep(n, t, e, r, o, a, c) {
try {
var i = n[a](c),
u = i.value;
} catch (n) {
return void e(n);
}
i.done ? t(u) : Promise.resolve(u).then(r, o);
}
function _asyncToGenerator(n) {
return function () {
var t = this,
e = arguments;
return new Promise(function (r, o) {
var a = n.apply(t, e);
function _next(n) {
asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
}
function _throw(n) {
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
}
_next(void 0);
});
};
}
function _construct(t, e, r) {
if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
var o = [null];
o.push.apply(o, e);
var p = new (t.bind.apply(t, o))();
return r && _setPrototypeOf(p, r.prototype), p;
}
function _extends() {
return _extends = Object.assign ? Object.assign.bind() : function (n) {
for (var e = 1; e < arguments.length; e++) {
var t = arguments[e];
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
}
return n;
}, _extends.apply(null, arguments);
}
function _getPrototypeOf(t) {
return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
return t.__proto__ || Object.getPrototypeOf(t);
}, _getPrototypeOf(t);
}
function _inheritsLoose(t, o) {
t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
}
function _isNativeFunction(t) {
try {
return -1 !== Function.toString.call(t).indexOf("[native code]");
} catch (n) {
return "function" == typeof t;
}
}
function _isNativeReflectConstruct() {
try {
var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
} catch (t) {}
return (_isNativeReflectConstruct = function () {
return !!t;
})();
}
function _regeneratorRuntime() {
_regeneratorRuntime = function () {
return e;
};
var t,
e = {},
r = Object.prototype,
n = r.hasOwnProperty,
o = Object.defineProperty || function (t, e, r) {
t[e] = r.value;
},
i = "function" == typeof Symbol ? Symbol : {},
a = i.iterator || "@@iterator",
c = i.asyncIterator || "@@asyncIterator",
u = i.toStringTag || "@@toStringTag";
function define(t, e, r) {
return Object.defineProperty(t, e, {
value: r,
enumerable: !0,
configurable: !0,
writable: !0
}), t[e];
}
try {
define({}, "");
} catch (t) {
define = function (t, e, r) {
return t[e] = r;
};
}
function wrap(t, e, r, n) {
var i = e && e.prototype instanceof Generator ? e : Generator,
a = Object.create(i.prototype),
c = new Context(n || []);
return o(a, "_invoke", {
value: makeInvokeMethod(t, r, c)
}), a;
}
function tryCatch(t, e, r) {
try {
return {
type: "normal",
arg: t.call(e, r)
};
} catch (t) {
return {
type: "throw",
arg: t
};
}
}
e.wrap = wrap;
var h = "suspendedStart",
l = "suspendedYield",
f = "executing",
s = "completed",
y = {};
function Generator() {}
function GeneratorFunction() {}
function GeneratorFunctionPrototype() {}
var p = {};
define(p, a, function () {
return this;
});
var d = Object.getPrototypeOf,
v = d && d(d(values([])));
v && v !== r && n.call(v, a) && (p = v);
var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
function defineIteratorMethods(t) {
["next", "throw", "return"].forEach(function (e) {
define(t, e, function (t) {
return this._invoke(e, t);
});
});
}
function AsyncIterator(t, e) {
function invoke(r, o, i, a) {
var c = tryCatch(t[r], t, o);
if ("throw" !== c.type) {
var u = c.arg,
h = u.value;
return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
invoke("next", t, i, a);
}, function (t) {
invoke("throw", t, i, a);
}) : e.resolve(h).then(function (t) {
u.value = t, i(u);
}, function (t) {
return invoke("throw", t, i, a);
});
}
a(c.arg);
}
var r;
o(this, "_invoke", {
value: function (t, n) {
function callInvokeWithMethodAndArg() {
return new e(function (e, r) {
invoke(t, n, e, r);
});
}
return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
}
});
}
function makeInvokeMethod(e, r, n) {
var o = h;
return function (i, a) {
if (o === f) throw Error("Generator is already running");
if (o === s) {
if ("throw" === i) throw a;
return {
value: t,
done: !0
};
}
for (n.method = i, n.arg = a;;) {
var c = n.delegate;
if (c) {
var u = maybeInvokeDelegate(c, n);
if (u) {
if (u === y) continue;
return u;
}
}
if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
if (o === h) throw o = s, n.arg;
n.dispatchException(n.arg);
} else "return" === n.method && n.abrupt("return", n.arg);
o = f;
var p = tryCatch(e, r, n);
if ("normal" === p.type) {
if (o = n.done ? s : l, p.arg === y) continue;
return {
value: p.arg,
done: n.done
};
}
"throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
}
};
}
function maybeInvokeDelegate(e, r) {
var n = r.method,
o = e.iterator[n];
if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
var i = tryCatch(o, e.iterator, r.arg);
if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
var a = i.arg;
return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
}
function pushTryEntry(t) {
var e = {
tryLoc: t[0]
};
1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
}
function resetTryEntry(t) {
var e = t.completion || {};
e.type = "normal", delete e.arg, t.completion = e;
}
function Context(t) {
this.tryEntries = [{
tryLoc: "root"
}], t.forEach(pushTryEntry, this), this.reset(!0);
}
function values(e) {
if (e || "" === e) {
var r = e[a];
if (r) return r.call(e);
if ("function" == typeof e.next) return e;
if (!isNaN(e.length)) {
var o = -1,
i = function next() {
for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
return next.value = t, next.done = !0, next;
};
return i.next = i;
}
}
throw new TypeError(typeof e + " is not iterable");
}
return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
value: GeneratorFunctionPrototype,
configurable: !0
}), o(GeneratorFunctionPrototype, "constructor", {
value: GeneratorFunction,
configurable: !0
}), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
var e = "function" == typeof t && t.constructor;
return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
}, e.mark = function (t) {
return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
}, e.awrap = function (t) {
return {
__await: t
};
}, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
return this;
}), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
void 0 === i && (i = Promise);
var a = new AsyncIterator(wrap(t, r, n, o), i);
return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
return t.done ? t.value : a.next();
});
}, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
return this;
}), define(g, "toString", function () {
return "[object Generator]";
}), e.keys = function (t) {
var e = Object(t),
r = [];
for (var n in e) r.push(n);
return r.reverse(), function next() {
for (; r.length;) {
var t = r.pop();
if (t in e) return next.value = t, next.done = !1, next;
}
return next.done = !0, next;
};
}, e.values = values, Context.prototype = {
constructor: Context,
reset: function (e) {
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(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
},
stop: function () {
this.done = !0;
var t = this.tryEntries[0].completion;
if ("throw" === t.type) throw t.arg;
return this.rval;
},
dispatchException: function (e) {
if (this.done) throw e;
var r = this;
function handle(n, o) {
return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
}
for (var o = this.tryEntries.length - 1; o >= 0; --o) {
var i = this.tryEntries[o],
a = i.completion;
if ("root" === i.tryLoc) return handle("end");
if (i.tryLoc <= this.prev) {
var c = n.call(i, "catchLoc"),
u = n.call(i, "finallyLoc");
if (c && u) {
if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
} else if (c) {
if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
} else {
if (!u) throw Error("try statement without catch or finally");
if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
}
}
}
},
abrupt: function (t, e) {
for (var r = this.tryEntries.length - 1; r >= 0; --r) {
var o = this.tryEntries[r];
if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
var i = o;
break;
}
}
i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
var a = i ? i.completion : {};
return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
},
complete: function (t, e) {
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 && e && (this.next = e), y;
},
finish: function (t) {
for (var e = this.tryEntries.length - 1; e >= 0; --e) {
var r = this.tryEntries[e];
if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
}
},
catch: function (t) {
for (var e = this.tryEntries.length - 1; e >= 0; --e) {
var r = this.tryEntries[e];
if (r.tryLoc === t) {
var n = r.completion;
if ("throw" === n.type) {
var o = n.arg;
resetTryEntry(r);
}
return o;
}
}
throw Error("illegal catch attempt");
},
delegateYield: function (e, r, n) {
return this.delegate = {
iterator: values(e),
resultName: r,
nextLoc: n
}, "next" === this.method && (this.arg = t), y;
}
}, e;
}
function _setPrototypeOf(t, e) {
return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
return t.__proto__ = e, t;
}, _setPrototypeOf(t, e);
}
function _wrapNativeSuper(t) {
var r = "function" == typeof Map ? new Map() : void 0;
return _wrapNativeSuper = function (t) {
if (null === t || !_isNativeFunction(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, Wrapper);
}
function Wrapper() {
return _construct(t, arguments, _getPrototypeOf(this).constructor);
}
return Wrapper.prototype = Object.create(t.prototype, {
constructor: {
value: Wrapper,
enumerable: !1,
writable: !0,
configurable: !0
}
}), _setPrototypeOf(Wrapper, t);
}, _wrapNativeSuper(t);
}
var GladiaError = /*#__PURE__*/function (_Error) {
function GladiaError(message, statusCode, code) {
var _this;
_this = _Error.call(this, message) || this;
_this.statusCode = statusCode;
_this.code = code;
_this.name = 'GladiaError';
return _this;
}
_inheritsLoose(GladiaError, _Error);
return GladiaError;
}(/*#__PURE__*/_wrapNativeSuper(Error));
var GladiaClient = /*#__PURE__*/function () {
function GladiaClient(config) {
if (!config.apiKey) {
throw new GladiaError('API key is required');
}
this.apiKey = config.apiKey;
this.baseUrl = config.baseUrl || 'https://api.gladia.io';
this.defaultOptions = {
language: config.language,
diarization: config.diarization,
diarization_config: config.diarizationConfig
};
}
var _proto = GladiaClient.prototype;
_proto.request = /*#__PURE__*/function () {
var _request = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(endpoint, options) {
var url, response, errorMessage, errorCode, errorData;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
if (options === void 0) {
options = {};
}
url = endpoint.startsWith('http') ? endpoint : "" + this.baseUrl + endpoint;
_context.prev = 2;
_context.next = 5;
return fetch(url, _extends({}, options, {
headers: _extends({
'x-gladia-key': this.apiKey,
'Content-Type': 'application/json'
}, options.headers)
}));
case 5:
response = _context.sent;
if (response.ok) {
_context.next = 20;
break;
}
errorMessage = "API Error: " + response.status + " " + response.statusText;
errorCode = null;
_context.prev = 9;
_context.next = 12;
return response.json();
case 12:
errorData = _context.sent;
errorMessage = errorData.message || errorMessage;
errorCode = errorData.code || null;
_context.next = 19;
break;
case 17:
_context.prev = 17;
_context.t0 = _context["catch"](9);
case 19:
throw new GladiaError(errorMessage, response.status, errorCode);
case 20:
_context.next = 22;
return response.json();
case 22:
return _context.abrupt("return", _context.sent);
case 25:
_context.prev = 25;
_context.t1 = _context["catch"](2);
if (!(_context.t1 instanceof GladiaError)) {
_context.next = 29;
break;
}
throw _context.t1;
case 29:
throw new GladiaError(_context.t1 instanceof Error ? _context.t1.message : 'Network error occurred', 0, 'NETWORK_ERROR');
case 30:
case "end":
return _context.stop();
}
}, _callee, this, [[2, 25], [9, 17]]);
}));
function request(_x, _x2) {
return _request.apply(this, arguments);
}
return request;
}();
_proto.waitForResult = /*#__PURE__*/function () {
var _waitForResult = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(resultUrl, options) {
var _options, _options$pollingInter, pollingInterval, _options$maxRetries, maxRetries, retries, response, _response$result, _response$error, _response$error2;
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
if (options === void 0) {
options = {};
}
_options = options, _options$pollingInter = _options.pollingInterval, pollingInterval = _options$pollingInter === void 0 ? 2000 : _options$pollingInter, _options$maxRetries = _options.maxRetries, maxRetries = _options$maxRetries === void 0 ? 60 : _options$maxRetries; // Default 2 minutes max waiting time
retries = 0;
case 3:
if (!(retries < maxRetries)) {
_context2.next = 33;
break;
}
_context2.prev = 4;
_context2.next = 7;
return this.request(resultUrl);
case 7:
response = _context2.sent;
if (!(response.status === 'done')) {
_context2.next = 13;
break;
}
// Success case - the API returns a valid result
if (!((_response$result = response.result) != null && (_response$result = _response$result.transcription) != null && _response$result.full_transcript)) {
console.warn('Warning: Response missing expected transcription data');
}
return _context2.abrupt("return", response);
case 13:
if (!(response.status === 'error')) {
_context2.next = 15;
break;
}
throw new GladiaError("Transcription failed: " + (((_response$error = response.error) == null ? void 0 : _response$error.message) || 'Unknown error'), undefined, (_response$error2 = response.error) == null ? void 0 : _response$error2.code);
case 15:
// Still processing - log and wait
console.log("Transcription status: " + response.status + " (attempt " + (retries + 1) + "/" + maxRetries + ")");
_context2.next = 18;
return new Promise(function (resolve) {
return setTimeout(resolve, pollingInterval);
});
case 18:
retries++;
_context2.next = 31;
break;
case 21:
_context2.prev = 21;
_context2.t0 = _context2["catch"](4);
if (!(_context2.t0 instanceof GladiaError && _context2.t0.statusCode === 404)) {
_context2.next = 30;
break;
}
// Resource not found yet, might need a moment to propagate
console.log("Waiting for resource to be available (attempt " + (retries + 1) + "/" + maxRetries + ")");
_context2.next = 27;
return new Promise(function (resolve) {
return setTimeout(resolve, pollingInterval);
});
case 27:
retries++;
_context2.next = 31;
break;
case 30:
throw _context2.t0;
case 31:
_context2.next = 3;
break;
case 33:
throw new GladiaError("Transcription timed out after " + maxRetries + " attempts", 408, 'TIMEOUT');
case 34:
case "end":
return _context2.stop();
}
}, _callee2, this, [[4, 21]]);
}));
function waitForResult(_x3, _x4) {
return _waitForResult.apply(this, arguments);
}
return waitForResult;
}()
/**
* Transcribe audio from a URL
*/
;
_proto.transcribeAudio =
/*#__PURE__*/
function () {
var _transcribeAudio = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(audioUrl, options, waitOptions) {
var mergedOptions, response, resultUrl;
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
if (options === void 0) {
options = {};
}
if (waitOptions === void 0) {
waitOptions = {};
}
mergedOptions = _extends({}, this.defaultOptions, options);
_context3.prev = 3;
_context3.next = 6;
return this.request('/v2/transcription', {
method: 'POST',
body: JSON.stringify(_extends({
audio_url: audioUrl
}, mergedOptions))
});
case 6:
response = _context3.sent;
if (response.id) {
_context3.next = 9;
break;
}
throw new GladiaError('Invalid response: missing id');
case 9:
if (!options.callback) {
_context3.next = 11;
break;
}
return _context3.abrupt("return", {
id: response.id,
status: response.status
});
case 11:
// For non-callback case, we need to poll for results
resultUrl = response.result_url || this.baseUrl + "/v2/transcription/" + response.id;
return _context3.abrupt("return", this.waitForResult(resultUrl, waitOptions));
case 15:
_context3.prev = 15;
_context3.t0 = _context3["catch"](3);
if (!(_context3.t0 instanceof GladiaError)) {
_context3.next = 24;
break;
}
if (!(_context3.t0.statusCode === 413)) {
_context3.next = 22;
break;
}
throw new GladiaError('File too large. Maximum file size is 500MB.', 413, 'PAYLOAD_TOO_LARGE');
case 22:
if (!(_context3.t0.statusCode === 415)) {
_context3.next = 24;
break;
}
throw new GladiaError('Unsupported file format. Please use a supported audio format (mp3, wav, mp4, etc.).', 415, 'UNSUPPORTED_MEDIA_TYPE');
case 24:
throw _context3.t0;
case 25:
case "end":
return _context3.stop();
}
}, _callee3, this, [[3, 15]]);
}));
function transcribeAudio(_x5, _x6, _x7) {
return _transcribeAudio.apply(this, arguments);
}
return transcribeAudio;
}()
/**
* Transcribe video from a URL (same as transcribeAudio)
*/
;
_proto.transcribeVideo =
/*#__PURE__*/
function () {
var _transcribeVideo = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(videoUrl, options, waitOptions) {
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
while (1) switch (_context4.prev = _context4.next) {
case 0:
if (options === void 0) {
options = {};
}
if (waitOptions === void 0) {
waitOptions = {};
}
return _context4.abrupt("return", this.transcribeAudio(videoUrl, options, waitOptions));
case 3:
case "end":
return _context4.stop();
}
}, _callee4, this);
}));
function transcribeVideo(_x8, _x9, _x10) {
return _transcribeVideo.apply(this, arguments);
}
return transcribeVideo;
}()
/**
* Upload and transcribe a file
*/
;
_proto.transcribeFile =
/*#__PURE__*/
function () {
var _transcribeFile = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(file, options, waitOptions) {
var _fileName$split$pop;
var fileName, fileExtension, namedFile, formData, uploadResponse, errorText, errorMessage, errorData, uploadData;
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
while (1) switch (_context5.prev = _context5.next) {
case 0:
if (options === void 0) {
options = {};
}
if (waitOptions === void 0) {
waitOptions = {};
}
// Create a properly named file to ensure correct format detection
fileName = file.name || "audio_" + Date.now() + ".mp3";
fileExtension = ((_fileName$split$pop = fileName.split('.').pop()) == null ? void 0 : _fileName$split$pop.toLowerCase()) || 'mp3';
namedFile = new File([file], "audio." + fileExtension, {
type: file.type || "audio/" + fileExtension
}); // First, upload the file
formData = new FormData();
formData.append('audio', namedFile);
_context5.prev = 7;
_context5.next = 10;
return fetch(this.baseUrl + "/v2/upload", {
method: 'POST',
headers: {
'x-gladia-key': this.apiKey
},
body: formData
});
case 10:
uploadResponse = _context5.sent;
if (uploadResponse.ok) {
_context5.next = 19;
break;
}
_context5.next = 14;
return uploadResponse.text();
case 14:
errorText = _context5.sent;
errorMessage = "Upload failed: " + uploadResponse.status + " " + uploadResponse.statusText;
errorData = {};
try {
errorData = JSON.parse(errorText);
errorMessage = errorData.message || errorMessage;
} catch (e) {
// If not JSON, use the raw text
errorMessage = errorText || errorMessage;
}
throw new GladiaError(errorMessage, uploadResponse.status, errorData.code);
case 19:
_context5.next = 21;
return uploadResponse.json();
case 21:
uploadData = _context5.sent;
if (uploadData.audio_url) {
_context5.next = 24;
break;
}
throw new GladiaError('Upload successful but no audio URL returned');
case 24:
return _context5.abrupt("return", this.transcribeAudio(uploadData.audio_url, options, waitOptions));
case 27:
_context5.prev = 27;
_context5.t0 = _context5["catch"](7);
if (!(_context5.t0 instanceof GladiaError)) {
_context5.next = 31;
break;
}
throw _context5.t0;
case 31:
throw new GladiaError(_context5.t0 instanceof Error ? "File upload failed: " + _context5.t0.message : 'File upload failed', 0, 'UPLOAD_ERROR');
case 32:
case "end":
return _context5.stop();
}
}, _callee5, this, [[7, 27]]);
}));
function transcribeFile(_x11, _x12, _x13) {
return _transcribeFile.apply(this, arguments);
}
return transcribeFile;
}()
/**
* Get a previously created transcription by ID
*/
;
_proto.getTranscription =
/*#__PURE__*/
function () {
var _getTranscription = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(id) {
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
while (1) switch (_context6.prev = _context6.next) {
case 0:
return _context6.abrupt("return", this.request("/v2/transcription/" + id));
case 1:
case "end":
return _context6.stop();
}
}, _callee6, this);
}));
function getTranscription(_x14) {
return _getTranscription.apply(this, arguments);
}
return getTranscription;
}()
/**
* Delete a transcription by ID
*/
;
_proto.deleteTranscription =
/*#__PURE__*/
function () {
var _deleteTranscription = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(id) {
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
while (1) switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return this.request("/v2/transcription/" + id, {
method: 'DELETE'
});
case 2:
case "end":
return _context7.stop();
}
}, _callee7, this);
}));
function deleteTranscription(_x15) {
return _deleteTranscription.apply(this, arguments);
}
return deleteTranscription;
}()
/**
* Create a WebSocket connection for real-time transcription
*/
;
_proto.createRealTimeTranscription = function createRealTimeTranscription(options) {
if (options === void 0) {
options = {};
}
var wsUrl = new URL(this.baseUrl + "/v2/live");
wsUrl.searchParams.append('x-gladia-key', this.apiKey);
// Add options as query parameters
Object.entries(options).forEach(function (_ref) {
var key = _ref[0],
value = _ref[1];
if (typeof value === 'object') {
wsUrl.searchParams.append(key, JSON.stringify(value));
} else if (value !== undefined) {
wsUrl.searchParams.append(key, String(value));
}
});
var socket = new WebSocket(wsUrl.toString());
// Add convenience methods to the socket
var enhancedSocket = socket;
enhancedSocket.sendAudio = function (audioChunk) {
if (socket.readyState === WebSocket.OPEN) {
if (audioChunk instanceof Blob) {
// If we have a Blob, we need to convert it to ArrayBuffer
var reader = new FileReader();
reader.onload = function () {
socket.send(reader.result);
};
reader.readAsArrayBuffer(audioChunk);
} else {
socket.send(audioChunk);
}
}
};
enhancedSocket.stopRecording = function () {
if (socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({
action: 'stop_recording'
}));
}
};
return enhancedSocket;
}
/**
* Translate audio/video from a URL
*/;
_proto.translateAudio =
/*#__PURE__*/
function () {
var _translateAudio = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(audioUrl, targetLanguages, options) {
var languages;
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
while (1) switch (_context8.prev = _context8.next) {
case 0:
if (options === void 0) {
options = {};
}
languages = Array.isArray(targetLanguages) ? targetLanguages : [targetLanguages];
return _context8.abrupt("return", this.transcribeAudio(audioUrl, _extends({}, options, {
translation: true,
translation_config: {
target_languages: languages
}
})));
case 3:
case "end":
return _context8.stop();
}
}, _callee8, this);
}));
function translateAudio(_x16, _x17, _x18) {
return _translateAudio.apply(this, arguments);
}
return translateAudio;
}()
/**
* Generate a summary of audio/video
*/
;
_proto.summarizeAudio =
/*#__PURE__*/
function () {
var _summarizeAudio = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(audioUrl, options) {
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
while (1) switch (_context9.prev = _context9.next) {
case 0:
if (options === void 0) {
options = {};
}
return _context9.abrupt("return", this.transcribeAudio(audioUrl, _extends({}, options, {
summarization: true
})));
case 2:
case "end":
return _context9.stop();
}
}, _callee9, this);
}));
function summarizeAudio(_x19, _x20) {
return _summarizeAudio.apply(this, arguments);
}
return summarizeAudio;
}()
/**
* Analyze sentiment in audio/video
*/
;
_proto.analyzeSentiment =
/*#__PURE__*/
function () {
var _analyzeSentiment = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(audioUrl, options) {
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
while (1) switch (_context10.prev = _context10.next) {
case 0:
if (options === void 0) {
options = {};
}
return _context10.abrupt("return", this.transcribeAudio(audioUrl, _extends({}, options, {
sentiment_analysis: true
})));
case 2:
case "end":
return _context10.stop();
}
}, _callee10, this);
}));
function analyzeSentiment(_x21, _x22) {
return _analyzeSentiment.apply(this, arguments);
}
return analyzeSentiment;
}()
/**
* Recognize named entities in audio/video
*/
;
_proto.detectEntities =
/*#__PURE__*/
function () {
var _detectEntities = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(audioUrl, options) {
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
while (1) switch (_context11.prev = _context11.next) {
case 0:
if (options === void 0) {
options = {};
}
return _context11.abrupt("return", this.transcribeAudio(audioUrl, _extends({}, options, {
named_entity_recognition: true
})));
case 2:
case "end":
return _context11.stop();
}
}, _callee11, this);
}));
function detectEntities(_x23, _x24) {
return _detectEntities.apply(this, arguments);
}
return detectEntities;
}()
/**
* Apply content moderation to audio/video
*/
;
_proto.moderateContent =
/*#__PURE__*/
function () {
var _moderateContent = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12(audioUrl, options) {
return _regeneratorRuntime().wrap(function _callee12$(_context12) {
while (1) switch (_context12.prev = _context12.next) {
case 0:
if (options === void 0) {
options = {};
}
return _context12.abrupt("return", this.transcribeAudio(audioUrl, _extends({}, options, {
content_moderation: true
})));
case 2:
case "end":
return _context12.stop();
}
}, _callee12, this);
}));
function moderateContent(_x25, _x26) {
return _moderateContent.apply(this, arguments);
}
return moderateContent;
}()
/**
* Generate chapters for audio/video
*/
;
_proto.generateChapters =
/*#__PURE__*/
function () {
var _generateChapters = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13(audioUrl, options) {
return _regeneratorRuntime().wrap(function _callee13$(_context13) {
while (1) switch (_context13.prev = _context13.next) {
case 0:
if (options === void 0) {
options = {};
}
return _context13.abrupt("return", this.transcribeAudio(audioUrl, _extends({}, options, {
chapterization: true
})));
case 2:
case "end":
return _context13.stop();
}
}, _callee13, this);
}));
function generateChapters(_x27, _x28) {
return _generateChapters.apply(this, arguments);
}
return generateChapters;
}();
return GladiaClient;
}();
export { GladiaClient, GladiaError };
//# sourceMappingURL=gladia.esm.js.map