assemblyai 4.0.0-beta.2 → 4.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/dist/assemblyai.umd.js +10 -42
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/bun.mjs +7 -2
- package/dist/deno.mjs +7 -2
- package/dist/index.cjs +8 -3
- package/dist/index.mjs +7 -2
- package/dist/node.cjs +3 -3
- package/dist/node.mjs +2 -2
- package/dist/polyfills/streams/index.d.ts +4 -0
- package/dist/polyfills/streams/node.d.ts +1 -0
- package/docs/compat.md +1 -1
- package/package.json +23 -13
- package/src/polyfills/streams/index.ts +6 -0
- package/src/polyfills/streams/node.ts +1 -0
- package/src/polyfills/ws/browser.mjs +15 -0
- package/src/polyfills/ws/index.cjs +1 -0
- package/src/polyfills/ws/index.d.ts +2 -0
- package/src/polyfills/ws/index.mjs +2 -0
- package/src/services/realtime/service.ts +2 -2
- /package/dist/{fs → polyfills/fs}/bun.d.ts +0 -0
- /package/dist/{fs → polyfills/fs}/deno.d.ts +0 -0
- /package/dist/{fs → polyfills/fs}/index.d.ts +0 -0
- /package/dist/{fs → polyfills/fs}/node.d.ts +0 -0
- /package/src/{fs → polyfills/fs}/bun.ts +0 -0
- /package/src/{fs → polyfills/fs}/deno.ts +0 -0
- /package/src/{fs → polyfills/fs}/index.ts +0 -0
- /package/src/{fs → polyfills/fs}/node.ts +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
### Added
|
|
6
6
|
|
|
7
|
-
- Add `
|
|
7
|
+
- Add `node`, `deno`, `bun`, `browser`, and `workerd` (Cloudflare Workers) exports to package.json. These exports are compatible versions of the SDK, with a few limitations in some cases. For more details, consult the [SDK Compatibility document](./docs/compat.md).
|
|
8
8
|
- Add `dist/assemblyai.umd.js` and `dist/assemblyai.umd.min.js`. You can reference these script files directly in the browser and the SDK will be available at the global `assemblyai` variable.
|
|
9
9
|
|
|
10
10
|
### Changed
|
package/dist/assemblyai.umd.js
CHANGED
|
@@ -131,55 +131,23 @@
|
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
const {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
ReadableByteStreamController,
|
|
140
|
-
ReadableStreamDefaultController,
|
|
141
|
-
TransformStream,
|
|
142
|
-
TransformStreamDefaultController,
|
|
143
|
-
WritableStream,
|
|
144
|
-
WritableStreamDefaultWriter,
|
|
145
|
-
WritableStreamDefaultController,
|
|
146
|
-
ByteLengthQueuingStrategy,
|
|
147
|
-
CountQueuingStrategy,
|
|
148
|
-
TextEncoderStream,
|
|
149
|
-
TextDecoderStream
|
|
150
|
-
} = typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : globalThis;
|
|
151
|
-
(function(ReadableStream2) {
|
|
152
|
-
if (!ReadableStream2.prototype[Symbol.asyncIterator]) {
|
|
153
|
-
async function* streamAsyncIterator() {
|
|
154
|
-
const reader = this.getReader();
|
|
155
|
-
try {
|
|
156
|
-
while (true) {
|
|
157
|
-
const { done, value } = await reader.read();
|
|
158
|
-
if (done)
|
|
159
|
-
return;
|
|
160
|
-
yield value;
|
|
161
|
-
}
|
|
162
|
-
} finally {
|
|
163
|
-
reader.releaseLock();
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
ReadableStream2.prototype[Symbol.asyncIterator] = streamAsyncIterator;
|
|
167
|
-
}
|
|
168
|
-
})(ReadableStream);
|
|
169
|
-
|
|
170
|
-
// https://github.com/maxogden/websocket-stream/blob/48dc3ddf943e5ada668c31ccd94e9186f02fafbd/ws-fallback.js
|
|
134
|
+
const { WritableStream } = typeof window !== "undefined"
|
|
135
|
+
? window
|
|
136
|
+
: typeof global !== "undefined"
|
|
137
|
+
? global
|
|
138
|
+
: globalThis;
|
|
171
139
|
|
|
172
140
|
var ws = null;
|
|
173
141
|
|
|
174
|
-
if (typeof WebSocket !==
|
|
142
|
+
if (typeof WebSocket !== "undefined") {
|
|
175
143
|
ws = WebSocket;
|
|
176
|
-
} else if (typeof MozWebSocket !==
|
|
144
|
+
} else if (typeof MozWebSocket !== "undefined") {
|
|
177
145
|
ws = MozWebSocket;
|
|
178
|
-
} else if (typeof global !==
|
|
146
|
+
} else if (typeof global !== "undefined") {
|
|
179
147
|
ws = global.WebSocket || global.MozWebSocket;
|
|
180
|
-
} else if (typeof window !==
|
|
148
|
+
} else if (typeof window !== "undefined") {
|
|
181
149
|
ws = window.WebSocket || window.MozWebSocket;
|
|
182
|
-
} else if (typeof self !==
|
|
150
|
+
} else if (typeof self !== "undefined") {
|
|
183
151
|
ws = self.WebSocket || self.MozWebSocket;
|
|
184
152
|
}
|
|
185
153
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).assemblyai={})}(this,(function(e){"use strict";function t(e,t,o,s){return new(o||(o=Promise))((function(i,r){function n(e){try{l(s.next(e))}catch(e){r(e)}}function a(e){try{l(s.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(n,a)}l((s=s.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class o{constructor(e){this.params=e}fetch(e,o){var s;return t(this,void 0,void 0,(function*(){(o=null!=o?o:{}).headers=null!==(s=o.headers)&&void 0!==s?s:{},o.headers=Object.assign({Authorization:this.params.apiKey,"Content-Type":"application/json"},o.headers),e.startsWith("http")||(e=this.params.baseUrl+e);const t=yield fetch(e,o);if(t.status>=400){let e;const o=yield t.text();if(o){try{e=JSON.parse(o)}catch(e){}if(null==e?void 0:e.error)throw new Error(e.error);throw new Error(o)}throw new Error(`HTTP Error: ${t.status} ${t.statusText}`)}return t}))}fetchJson(e,o){return t(this,void 0,void 0,(function*(){return(yield this.fetch(e,o)).json()}))}}class s extends o{summary(e){return this.fetchJson("/lemur/v3/generate/summary",{method:"POST",body:JSON.stringify(e)})}questionAnswer(e){return this.fetchJson("/lemur/v3/generate/question-answer",{method:"POST",body:JSON.stringify(e)})}actionItems(e){return this.fetchJson("/lemur/v3/generate/action-items",{method:"POST",body:JSON.stringify(e)})}task(e){return this.fetchJson("/lemur/v3/generate/task",{method:"POST",body:JSON.stringify(e)})}purgeRequestData(e){return this.fetchJson(`/lemur/v3/${e}`,{method:"DELETE"})}}const{ReadableStream:i,ReadableStreamDefaultReader:r,ReadableStreamBYOBReader:n,ReadableStreamBYOBRequest:a,ReadableByteStreamController:l,ReadableStreamDefaultController:c,TransformStream:d,TransformStreamDefaultController:u,WritableStream:h,WritableStreamDefaultWriter:f,WritableStreamDefaultController:p,ByteLengthQueuingStrategy:m,CountQueuingStrategy:y,TextEncoderStream:S,TextDecoderStream:v}="undefined"!=typeof window?window:"undefined"!=typeof global?global:globalThis;!function(e){if(!e.prototype[Symbol.asyncIterator]){async function*t(){const e=this.getReader();try{for(;;){const{done:t,value:o}=await e.read();if(t)return;yield o}}finally{e.releaseLock()}}e.prototype[Symbol.asyncIterator]=t}}(i);var w=null;"undefined"!=typeof WebSocket?w=WebSocket:"undefined"!=typeof MozWebSocket?w=MozWebSocket:"undefined"!=typeof global?w=global.WebSocket||global.MozWebSocket:"undefined"!=typeof window?w=window.WebSocket||window.MozWebSocket:"undefined"!=typeof self&&(w=self.WebSocket||self.MozWebSocket);var b,g=w;!function(e){e[e.BadSampleRate=4e3]="BadSampleRate",e[e.AuthFailed=4001]="AuthFailed",e[e.InsufficientFundsOrFreeAccount=4002]="InsufficientFundsOrFreeAccount",e[e.NonexistentSessionId=4004]="NonexistentSessionId",e[e.SessionExpired=4008]="SessionExpired",e[e.ClosedSession=4010]="ClosedSession",e[e.RateLimited=4029]="RateLimited",e[e.UniqueSessionViolation=4030]="UniqueSessionViolation",e[e.SessionTimeout=4031]="SessionTimeout",e[e.AudioTooShort=4032]="AudioTooShort",e[e.AudioTooLong=4033]="AudioTooLong",e[e.BadJson=4100]="BadJson",e[e.BadSchema=4101]="BadSchema",e[e.TooManyStreams=4102]="TooManyStreams",e[e.Reconnected=4103]="Reconnected",e[e.ReconnectAttemptsExhausted=1013]="ReconnectAttemptsExhausted"}(b||(b={}));const k={[b.BadSampleRate]:"Sample rate must be a positive integer",[b.AuthFailed]:"Not Authorized",[b.InsufficientFundsOrFreeAccount]:"Insufficient funds or you are using a free account. This feature is paid-only and requires you to add a credit card. Please visit https://assemblyai.com/dashboard/ to add a credit card to your account.",[b.NonexistentSessionId]:"Session ID does not exist",[b.SessionExpired]:"Session has expired",[b.ClosedSession]:"Session is closed",[b.RateLimited]:"Rate limited",[b.UniqueSessionViolation]:"Unique session violation",[b.SessionTimeout]:"Session Timeout",[b.AudioTooShort]:"Audio too short",[b.AudioTooLong]:"Audio too long",[b.BadJson]:"Bad JSON",[b.BadSchema]:"Bad schema",[b.TooManyStreams]:"Too many streams",[b.Reconnected]:"Reconnected",[b.ReconnectAttemptsExhausted]:"Reconnect attempts exhausted"};class T extends Error{}class R{constructor(e){var t,o;if(this.listeners={},this.realtimeUrl=null!==(t=e.realtimeUrl)&&void 0!==t?t:"wss://api.assemblyai.com/v2/realtime/ws",this.sampleRate=null!==(o=e.sampleRate)&&void 0!==o?o:16e3,this.wordBoost=e.wordBoost,"apiKey"in e&&e.apiKey&&(this.apiKey=e.apiKey),"token"in e&&e.token&&(this.token=e.token),!this.apiKey&&!this.token)throw new Error("API key or temporary token is required.")}connectionUrl(){const e=new URL(this.realtimeUrl);if("wss:"!==e.protocol)throw new Error("Invalid protocol, must be wss");const t=new URLSearchParams;return this.token&&t.set("token",this.token),t.set("sample_rate",this.sampleRate.toString()),this.wordBoost&&this.wordBoost.length>0&&t.set("word_boost",JSON.stringify(this.wordBoost)),e.search=t.toString(),e}on(e,t){this.listeners[e]=t}connect(){return new Promise((e=>{if(this.socket)throw new Error("Already connected");const t=this.connectionUrl();this.token?this.socket=new g(t.toString()):this.socket=new g(t.toString(),{headers:{Authorization:this.apiKey}}),this.socket.onclose=({code:e,reason:t})=>{var o,s;t||e in b&&(t=k[e]),null===(s=(o=this.listeners).close)||void 0===s||s.call(o,e,t)},this.socket.onerror=e=>{var t,o,s,i;e.error?null===(o=(t=this.listeners).error)||void 0===o||o.call(t,e.error):null===(i=(s=this.listeners).error)||void 0===i||i.call(s,new Error(e.message))},this.socket.onmessage=({data:t})=>{var o,s,i,r,n,a,l,c,d,u,h,f,p;const m=JSON.parse(t.toString());if("error"in m)null===(s=(o=this.listeners).error)||void 0===s||s.call(o,new T(m.error));else switch(m.message_type){case"SessionBegins":{const t={sessionId:m.session_id,expiresAt:new Date(m.expires_at)};e(t),null===(r=(i=this.listeners).open)||void 0===r||r.call(i,t);break}case"PartialTranscript":m.created=new Date(m.created),null===(a=(n=this.listeners).transcript)||void 0===a||a.call(n,m),null===(c=(l=this.listeners)["transcript.partial"])||void 0===c||c.call(l,m);break;case"FinalTranscript":m.created=new Date(m.created),null===(u=(d=this.listeners).transcript)||void 0===u||u.call(d,m),null===(f=(h=this.listeners)["transcript.final"])||void 0===f||f.call(h,m);break;case"SessionTerminated":null===(p=this.sessionTerminatedResolve)||void 0===p||p.call(this)}}}))}sendAudio(e){if(!this.socket||this.socket.readyState!==g.OPEN)throw new Error("Socket is not open for communication");let t;t="undefined"!=typeof Buffer?Buffer.from(e).toString("base64"):btoa(new Uint8Array(e).reduce(((e,t)=>e+String.fromCharCode(t)),""));const o={audio_data:t};this.socket.send(JSON.stringify(o))}stream(){return new h({write:e=>{this.sendAudio(e)}})}close(e=!0){return t(this,void 0,void 0,(function*(){if(this.socket){if(this.socket.readyState===g.OPEN){const t='{"terminate_session": true}';if(e){const e=new Promise((e=>{this.sessionTerminatedResolve=e}));this.socket.send(t),yield e}else this.socket.send(t)}"removeAllListeners"in this.socket&&this.socket.removeAllListeners(),this.socket.close()}this.listeners={},this.socket=void 0}))}}class O extends o{constructor(e){super(e),this.rtFactoryParams=e}createService(e){return e?"token"in e||e.apiKey||(e.apiKey=this.rtFactoryParams.apiKey):e={apiKey:this.rtFactoryParams.apiKey},new R(e)}createTemporaryToken(e){return t(this,void 0,void 0,(function*(){return(yield this.fetchJson("/v2/realtime/token",{method:"POST",body:JSON.stringify(e)})).token}))}}class x extends o{constructor(e,t){super(e),this.files=t}transcribe(e,o){return t(this,void 0,void 0,(function*(){const t=yield this.submit(e);return yield this.waitUntilReady(t.id,o)}))}submit(e){return t(this,void 0,void 0,(function*(){const{audio:t}=e,o=function(e,t){var o={};for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.indexOf(s)<0&&(o[s]=e[s]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(s=Object.getOwnPropertySymbols(e);i<s.length;i++)t.indexOf(s[i])<0&&Object.prototype.propertyIsEnumerable.call(e,s[i])&&(o[s[i]]=e[s[i]])}return o}(e,["audio"]);let s;if("string"==typeof t){const e=J(t);s=null!==e?yield this.files.upload(e):t}else s=yield this.files.upload(t);return yield this.fetchJson("/v2/transcript",{method:"POST",body:JSON.stringify(Object.assign(Object.assign({},o),{audio_url:s}))})}))}create(e,o){var s;return t(this,void 0,void 0,(function*(){const t=J(e.audio_url);if(null!==t){const o=yield this.files.upload(t);e.audio_url=o}const i=yield this.fetchJson("/v2/transcript",{method:"POST",body:JSON.stringify(e)});return null===(s=null==o?void 0:o.poll)||void 0===s||s?yield this.waitUntilReady(i.id,o):i}))}waitUntilReady(e,o){var s,i;return t(this,void 0,void 0,(function*(){const t=null!==(s=null==o?void 0:o.pollingInterval)&&void 0!==s?s:3e3,r=null!==(i=null==o?void 0:o.pollingTimeout)&&void 0!==i?i:-1,n=Date.now();for(;;){const o=yield this.get(e);if("completed"===o.status||"error"===o.status)return o;if(r>0&&Date.now()-n>r)throw new Error("Polling timeout");yield new Promise((e=>setTimeout(e,t)))}}))}get(e){return this.fetchJson(`/v2/transcript/${e}`)}list(e){return t(this,void 0,void 0,(function*(){let t="/v2/transcript";"string"==typeof e?t=e:e&&(t=`${t}?${new URLSearchParams(Object.keys(e).map((t=>{var o;return[t,(null===(o=e[t])||void 0===o?void 0:o.toString())||""]})))}`);const o=yield this.fetchJson(t);for(const e of o.transcripts)e.created=new Date(e.created),e.completed&&(e.completed=new Date(e.completed));return o}))}delete(e){return this.fetchJson(`/v2/transcript/${e}`,{method:"DELETE"})}wordSearch(e,t){const o=new URLSearchParams({words:t.join(",")});return this.fetchJson(`/v2/transcript/${e}/word-search?${o.toString()}`)}sentences(e){return this.fetchJson(`/v2/transcript/${e}/sentences`)}paragraphs(e){return this.fetchJson(`/v2/transcript/${e}/paragraphs`)}subtitles(e,o="srt",s){return t(this,void 0,void 0,(function*(){let t=`/v2/transcript/${e}/${o}`;if(s){const e=new URLSearchParams;e.set("chars_per_caption",s.toString()),t+=`?${e.toString()}`}const i=yield this.fetch(t);return yield i.text()}))}redactions(e){return this.fetchJson(`/v2/transcript/${e}/redacted-audio`)}}function J(e){let t;try{return t=new URL(e),"file:"===t.protocol?t.pathname:null}catch(t){return e}}class A extends o{upload(e){return t(this,void 0,void 0,(function*(){let o;o="string"==typeof e?yield function(e){return t(this,void 0,void 0,(function*(){throw new Error("Interacting with the file system is not supported in this environment.")}))}():e;return(yield this.fetchJson("/v2/upload",{method:"POST",body:o,headers:{"Content-Type":"application/octet-stream"},duplex:"half"})).upload_url}))}}e.AssemblyAI=class{constructor(e){e.baseUrl=e.baseUrl||"https://api.assemblyai.com",e.baseUrl&&e.baseUrl.endsWith("/")&&(e.baseUrl=e.baseUrl.slice(0,-1)),this.files=new A(e),this.transcripts=new x(e,this.files),this.lemur=new s(e),this.realtime=new O(e)}},e.FileService=A,e.LemurService=s,e.RealtimeService=R,e.RealtimeServiceFactory=O,e.TranscriptService=x}));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).assemblyai={})}(this,(function(e){"use strict";function t(e,t,s,o){return new(s||(s=Promise))((function(i,n){function r(e){try{c(o.next(e))}catch(e){n(e)}}function a(e){try{c(o.throw(e))}catch(e){n(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(r,a)}c((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class s{constructor(e){this.params=e}fetch(e,s){var o;return t(this,void 0,void 0,(function*(){(s=null!=s?s:{}).headers=null!==(o=s.headers)&&void 0!==o?o:{},s.headers=Object.assign({Authorization:this.params.apiKey,"Content-Type":"application/json"},s.headers),e.startsWith("http")||(e=this.params.baseUrl+e);const t=yield fetch(e,s);if(t.status>=400){let e;const s=yield t.text();if(s){try{e=JSON.parse(s)}catch(e){}if(null==e?void 0:e.error)throw new Error(e.error);throw new Error(s)}throw new Error(`HTTP Error: ${t.status} ${t.statusText}`)}return t}))}fetchJson(e,s){return t(this,void 0,void 0,(function*(){return(yield this.fetch(e,s)).json()}))}}class o extends s{summary(e){return this.fetchJson("/lemur/v3/generate/summary",{method:"POST",body:JSON.stringify(e)})}questionAnswer(e){return this.fetchJson("/lemur/v3/generate/question-answer",{method:"POST",body:JSON.stringify(e)})}actionItems(e){return this.fetchJson("/lemur/v3/generate/action-items",{method:"POST",body:JSON.stringify(e)})}task(e){return this.fetchJson("/lemur/v3/generate/task",{method:"POST",body:JSON.stringify(e)})}purgeRequestData(e){return this.fetchJson(`/lemur/v3/${e}`,{method:"DELETE"})}}const{WritableStream:i}="undefined"!=typeof window?window:"undefined"!=typeof global?global:globalThis;var n=null;"undefined"!=typeof WebSocket?n=WebSocket:"undefined"!=typeof MozWebSocket?n=MozWebSocket:"undefined"!=typeof global?n=global.WebSocket||global.MozWebSocket:"undefined"!=typeof window?n=window.WebSocket||window.MozWebSocket:"undefined"!=typeof self&&(n=self.WebSocket||self.MozWebSocket);var r,a=n;!function(e){e[e.BadSampleRate=4e3]="BadSampleRate",e[e.AuthFailed=4001]="AuthFailed",e[e.InsufficientFundsOrFreeAccount=4002]="InsufficientFundsOrFreeAccount",e[e.NonexistentSessionId=4004]="NonexistentSessionId",e[e.SessionExpired=4008]="SessionExpired",e[e.ClosedSession=4010]="ClosedSession",e[e.RateLimited=4029]="RateLimited",e[e.UniqueSessionViolation=4030]="UniqueSessionViolation",e[e.SessionTimeout=4031]="SessionTimeout",e[e.AudioTooShort=4032]="AudioTooShort",e[e.AudioTooLong=4033]="AudioTooLong",e[e.BadJson=4100]="BadJson",e[e.BadSchema=4101]="BadSchema",e[e.TooManyStreams=4102]="TooManyStreams",e[e.Reconnected=4103]="Reconnected",e[e.ReconnectAttemptsExhausted=1013]="ReconnectAttemptsExhausted"}(r||(r={}));const c={[r.BadSampleRate]:"Sample rate must be a positive integer",[r.AuthFailed]:"Not Authorized",[r.InsufficientFundsOrFreeAccount]:"Insufficient funds or you are using a free account. This feature is paid-only and requires you to add a credit card. Please visit https://assemblyai.com/dashboard/ to add a credit card to your account.",[r.NonexistentSessionId]:"Session ID does not exist",[r.SessionExpired]:"Session has expired",[r.ClosedSession]:"Session is closed",[r.RateLimited]:"Rate limited",[r.UniqueSessionViolation]:"Unique session violation",[r.SessionTimeout]:"Session Timeout",[r.AudioTooShort]:"Audio too short",[r.AudioTooLong]:"Audio too long",[r.BadJson]:"Bad JSON",[r.BadSchema]:"Bad schema",[r.TooManyStreams]:"Too many streams",[r.Reconnected]:"Reconnected",[r.ReconnectAttemptsExhausted]:"Reconnect attempts exhausted"};class l extends Error{}class d{constructor(e){var t,s;if(this.listeners={},this.realtimeUrl=null!==(t=e.realtimeUrl)&&void 0!==t?t:"wss://api.assemblyai.com/v2/realtime/ws",this.sampleRate=null!==(s=e.sampleRate)&&void 0!==s?s:16e3,this.wordBoost=e.wordBoost,"apiKey"in e&&e.apiKey&&(this.apiKey=e.apiKey),"token"in e&&e.token&&(this.token=e.token),!this.apiKey&&!this.token)throw new Error("API key or temporary token is required.")}connectionUrl(){const e=new URL(this.realtimeUrl);if("wss:"!==e.protocol)throw new Error("Invalid protocol, must be wss");const t=new URLSearchParams;return this.token&&t.set("token",this.token),t.set("sample_rate",this.sampleRate.toString()),this.wordBoost&&this.wordBoost.length>0&&t.set("word_boost",JSON.stringify(this.wordBoost)),e.search=t.toString(),e}on(e,t){this.listeners[e]=t}connect(){return new Promise((e=>{if(this.socket)throw new Error("Already connected");const t=this.connectionUrl();this.token?this.socket=new a(t.toString()):this.socket=new a(t.toString(),{headers:{Authorization:this.apiKey}}),this.socket.onclose=({code:e,reason:t})=>{var s,o;t||e in r&&(t=c[e]),null===(o=(s=this.listeners).close)||void 0===o||o.call(s,e,t)},this.socket.onerror=e=>{var t,s,o,i;e.error?null===(s=(t=this.listeners).error)||void 0===s||s.call(t,e.error):null===(i=(o=this.listeners).error)||void 0===i||i.call(o,new Error(e.message))},this.socket.onmessage=({data:t})=>{var s,o,i,n,r,a,c,d,u,h,f,p,m;const y=JSON.parse(t.toString());if("error"in y)null===(o=(s=this.listeners).error)||void 0===o||o.call(s,new l(y.error));else switch(y.message_type){case"SessionBegins":{const t={sessionId:y.session_id,expiresAt:new Date(y.expires_at)};e(t),null===(n=(i=this.listeners).open)||void 0===n||n.call(i,t);break}case"PartialTranscript":y.created=new Date(y.created),null===(a=(r=this.listeners).transcript)||void 0===a||a.call(r,y),null===(d=(c=this.listeners)["transcript.partial"])||void 0===d||d.call(c,y);break;case"FinalTranscript":y.created=new Date(y.created),null===(h=(u=this.listeners).transcript)||void 0===h||h.call(u,y),null===(p=(f=this.listeners)["transcript.final"])||void 0===p||p.call(f,y);break;case"SessionTerminated":null===(m=this.sessionTerminatedResolve)||void 0===m||m.call(this)}}}))}sendAudio(e){if(!this.socket||this.socket.readyState!==a.OPEN)throw new Error("Socket is not open for communication");let t;t="undefined"!=typeof Buffer?Buffer.from(e).toString("base64"):btoa(new Uint8Array(e).reduce(((e,t)=>e+String.fromCharCode(t)),""));const s={audio_data:t};this.socket.send(JSON.stringify(s))}stream(){return new i({write:e=>{this.sendAudio(e)}})}close(e=!0){return t(this,void 0,void 0,(function*(){if(this.socket){if(this.socket.readyState===a.OPEN){const t='{"terminate_session": true}';if(e){const e=new Promise((e=>{this.sessionTerminatedResolve=e}));this.socket.send(t),yield e}else this.socket.send(t)}"removeAllListeners"in this.socket&&this.socket.removeAllListeners(),this.socket.close()}this.listeners={},this.socket=void 0}))}}class u extends s{constructor(e){super(e),this.rtFactoryParams=e}createService(e){return e?"token"in e||e.apiKey||(e.apiKey=this.rtFactoryParams.apiKey):e={apiKey:this.rtFactoryParams.apiKey},new d(e)}createTemporaryToken(e){return t(this,void 0,void 0,(function*(){return(yield this.fetchJson("/v2/realtime/token",{method:"POST",body:JSON.stringify(e)})).token}))}}class h extends s{constructor(e,t){super(e),this.files=t}transcribe(e,s){return t(this,void 0,void 0,(function*(){const t=yield this.submit(e);return yield this.waitUntilReady(t.id,s)}))}submit(e){return t(this,void 0,void 0,(function*(){const{audio:t}=e,s=function(e,t){var s={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(s[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(o=Object.getOwnPropertySymbols(e);i<o.length;i++)t.indexOf(o[i])<0&&Object.prototype.propertyIsEnumerable.call(e,o[i])&&(s[o[i]]=e[o[i]])}return s}(e,["audio"]);let o;if("string"==typeof t){const e=f(t);o=null!==e?yield this.files.upload(e):t}else o=yield this.files.upload(t);return yield this.fetchJson("/v2/transcript",{method:"POST",body:JSON.stringify(Object.assign(Object.assign({},s),{audio_url:o}))})}))}create(e,s){var o;return t(this,void 0,void 0,(function*(){const t=f(e.audio_url);if(null!==t){const s=yield this.files.upload(t);e.audio_url=s}const i=yield this.fetchJson("/v2/transcript",{method:"POST",body:JSON.stringify(e)});return null===(o=null==s?void 0:s.poll)||void 0===o||o?yield this.waitUntilReady(i.id,s):i}))}waitUntilReady(e,s){var o,i;return t(this,void 0,void 0,(function*(){const t=null!==(o=null==s?void 0:s.pollingInterval)&&void 0!==o?o:3e3,n=null!==(i=null==s?void 0:s.pollingTimeout)&&void 0!==i?i:-1,r=Date.now();for(;;){const s=yield this.get(e);if("completed"===s.status||"error"===s.status)return s;if(n>0&&Date.now()-r>n)throw new Error("Polling timeout");yield new Promise((e=>setTimeout(e,t)))}}))}get(e){return this.fetchJson(`/v2/transcript/${e}`)}list(e){return t(this,void 0,void 0,(function*(){let t="/v2/transcript";"string"==typeof e?t=e:e&&(t=`${t}?${new URLSearchParams(Object.keys(e).map((t=>{var s;return[t,(null===(s=e[t])||void 0===s?void 0:s.toString())||""]})))}`);const s=yield this.fetchJson(t);for(const e of s.transcripts)e.created=new Date(e.created),e.completed&&(e.completed=new Date(e.completed));return s}))}delete(e){return this.fetchJson(`/v2/transcript/${e}`,{method:"DELETE"})}wordSearch(e,t){const s=new URLSearchParams({words:t.join(",")});return this.fetchJson(`/v2/transcript/${e}/word-search?${s.toString()}`)}sentences(e){return this.fetchJson(`/v2/transcript/${e}/sentences`)}paragraphs(e){return this.fetchJson(`/v2/transcript/${e}/paragraphs`)}subtitles(e,s="srt",o){return t(this,void 0,void 0,(function*(){let t=`/v2/transcript/${e}/${s}`;if(o){const e=new URLSearchParams;e.set("chars_per_caption",o.toString()),t+=`?${e.toString()}`}const i=yield this.fetch(t);return yield i.text()}))}redactions(e){return this.fetchJson(`/v2/transcript/${e}/redacted-audio`)}}function f(e){let t;try{return t=new URL(e),"file:"===t.protocol?t.pathname:null}catch(t){return e}}class p extends s{upload(e){return t(this,void 0,void 0,(function*(){let s;s="string"==typeof e?yield function(e){return t(this,void 0,void 0,(function*(){throw new Error("Interacting with the file system is not supported in this environment.")}))}():e;return(yield this.fetchJson("/v2/upload",{method:"POST",body:s,headers:{"Content-Type":"application/octet-stream"},duplex:"half"})).upload_url}))}}e.AssemblyAI=class{constructor(e){e.baseUrl=e.baseUrl||"https://api.assemblyai.com",e.baseUrl&&e.baseUrl.endsWith("/")&&(e.baseUrl=e.baseUrl.slice(0,-1)),this.files=new p(e),this.transcripts=new h(e,this.files),this.lemur=new o(e),this.realtime=new u(e)}},e.FileService=p,e.LemurService=o,e.RealtimeService=d,e.RealtimeServiceFactory=u,e.TranscriptService=h}));
|
package/dist/bun.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import WebSocket from 'isomorphic-ws';
|
|
1
|
+
import WebSocket from '#ws';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Base class for services that communicate with the API.
|
|
@@ -83,6 +82,12 @@ class LemurService extends BaseService {
|
|
|
83
82
|
}
|
|
84
83
|
}
|
|
85
84
|
|
|
85
|
+
const { WritableStream } = typeof window !== "undefined"
|
|
86
|
+
? window
|
|
87
|
+
: typeof global !== "undefined"
|
|
88
|
+
? global
|
|
89
|
+
: globalThis;
|
|
90
|
+
|
|
86
91
|
var RealtimeErrorType;
|
|
87
92
|
(function (RealtimeErrorType) {
|
|
88
93
|
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
package/dist/deno.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import WebSocket from 'isomorphic-ws';
|
|
1
|
+
import WebSocket from '#ws';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Base class for services that communicate with the API.
|
|
@@ -83,6 +82,12 @@ class LemurService extends BaseService {
|
|
|
83
82
|
}
|
|
84
83
|
}
|
|
85
84
|
|
|
85
|
+
const { WritableStream } = typeof window !== "undefined"
|
|
86
|
+
? window
|
|
87
|
+
: typeof global !== "undefined"
|
|
88
|
+
? global
|
|
89
|
+
: globalThis;
|
|
90
|
+
|
|
86
91
|
var RealtimeErrorType;
|
|
87
92
|
(function (RealtimeErrorType) {
|
|
88
93
|
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var WebSocket = require('isomorphic-ws');
|
|
3
|
+
var WebSocket = require('#ws');
|
|
5
4
|
|
|
6
5
|
/******************************************************************************
|
|
7
6
|
Copyright (c) Microsoft Corporation.
|
|
@@ -130,6 +129,12 @@ class LemurService extends BaseService {
|
|
|
130
129
|
}
|
|
131
130
|
}
|
|
132
131
|
|
|
132
|
+
const { WritableStream } = typeof window !== "undefined"
|
|
133
|
+
? window
|
|
134
|
+
: typeof global !== "undefined"
|
|
135
|
+
? global
|
|
136
|
+
: globalThis;
|
|
137
|
+
|
|
133
138
|
var RealtimeErrorType;
|
|
134
139
|
(function (RealtimeErrorType) {
|
|
135
140
|
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
|
@@ -295,7 +300,7 @@ class RealtimeService {
|
|
|
295
300
|
this.socket.send(JSON.stringify(payload));
|
|
296
301
|
}
|
|
297
302
|
stream() {
|
|
298
|
-
return new
|
|
303
|
+
return new WritableStream({
|
|
299
304
|
write: (chunk) => {
|
|
300
305
|
this.sendAudio(chunk);
|
|
301
306
|
},
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import WebSocket from 'isomorphic-ws';
|
|
1
|
+
import WebSocket from '#ws';
|
|
3
2
|
|
|
4
3
|
/******************************************************************************
|
|
5
4
|
Copyright (c) Microsoft Corporation.
|
|
@@ -128,6 +127,12 @@ class LemurService extends BaseService {
|
|
|
128
127
|
}
|
|
129
128
|
}
|
|
130
129
|
|
|
130
|
+
const { WritableStream } = typeof window !== "undefined"
|
|
131
|
+
? window
|
|
132
|
+
: typeof global !== "undefined"
|
|
133
|
+
? global
|
|
134
|
+
: globalThis;
|
|
135
|
+
|
|
131
136
|
var RealtimeErrorType;
|
|
132
137
|
(function (RealtimeErrorType) {
|
|
133
138
|
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
package/dist/node.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var WebSocket = require('
|
|
3
|
+
var web = require('stream/web');
|
|
4
|
+
var WebSocket = require('#ws');
|
|
5
5
|
var fs = require('fs');
|
|
6
6
|
var stream = require('stream');
|
|
7
7
|
|
|
@@ -248,7 +248,7 @@ class RealtimeService {
|
|
|
248
248
|
this.socket.send(JSON.stringify(payload));
|
|
249
249
|
}
|
|
250
250
|
stream() {
|
|
251
|
-
return new
|
|
251
|
+
return new web.WritableStream({
|
|
252
252
|
write: (chunk) => {
|
|
253
253
|
this.sendAudio(chunk);
|
|
254
254
|
},
|
package/dist/node.mjs
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WritableStream } from "stream/web";
|
package/docs/compat.md
CHANGED
|
@@ -40,7 +40,7 @@ However, there are still incompatibilities between Node.js and the browser.
|
|
|
40
40
|
});
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
- You can't pass local audio file paths to `client.files.upload`, `client.transcripts.transcribe`, and `client.transcripts.submit`. If you do, you'll get the following error: "
|
|
43
|
+
- You can't pass local audio file paths to `client.files.upload`, `client.transcripts.transcribe`, and `client.transcripts.submit`. If you do, you'll get the following error: "Interacting with the file system is not supported in this environment.".
|
|
44
44
|
If you want to transcribe audio files, you must use a public URL, a stream, or a buffer.
|
|
45
45
|
|
|
46
46
|
> [!WARNING]
|
package/package.json
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assemblyai",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.3",
|
|
4
4
|
"description": "The AssemblyAI JavaScript SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription, as well as the latest LeMUR models.",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
-
"node": {
|
|
9
|
-
"types": "./dist/index.d.ts",
|
|
10
|
-
"import": "./dist/node.mjs",
|
|
11
|
-
"require": "./dist/node.cjs"
|
|
12
|
-
},
|
|
13
8
|
"bun": {
|
|
14
9
|
"types": "./dist/index.d.ts",
|
|
15
10
|
"default": "./dist/bun.mjs"
|
|
@@ -20,6 +15,11 @@
|
|
|
20
15
|
},
|
|
21
16
|
"workerd": "./dist/index.mjs",
|
|
22
17
|
"browser": "./dist/index.mjs",
|
|
18
|
+
"node": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/node.mjs",
|
|
21
|
+
"require": "./dist/node.cjs"
|
|
22
|
+
},
|
|
23
23
|
"import": "./dist/index.mjs",
|
|
24
24
|
"require": "./dist/index.cjs",
|
|
25
25
|
"default": "./dist/index.cjs"
|
|
@@ -28,10 +28,23 @@
|
|
|
28
28
|
},
|
|
29
29
|
"imports": {
|
|
30
30
|
"#fs": {
|
|
31
|
-
"node": "./src/fs/node.ts",
|
|
32
|
-
"bun": "./src/fs/bun.ts",
|
|
33
|
-
"deno": "./src/fs/deno.ts",
|
|
34
|
-
"default": "./src/fs/index.ts"
|
|
31
|
+
"node": "./src/polyfills/fs/node.ts",
|
|
32
|
+
"bun": "./src/polyfills/fs/bun.ts",
|
|
33
|
+
"deno": "./src/polyfills/fs/deno.ts",
|
|
34
|
+
"default": "./src/polyfills/fs/index.ts"
|
|
35
|
+
},
|
|
36
|
+
"#streams": {
|
|
37
|
+
"node": "./src/polyfills/streams/node.ts",
|
|
38
|
+
"default": "./src/polyfills/streams/index.ts"
|
|
39
|
+
},
|
|
40
|
+
"#ws": {
|
|
41
|
+
"types": "./src/polyfills/ws/index.d.ts",
|
|
42
|
+
"browser": "./src/polyfills/ws/browser.mjs",
|
|
43
|
+
"default": {
|
|
44
|
+
"types": "./src/polyfills/ws/index.d.ts",
|
|
45
|
+
"import": "./src/polyfills/ws/index.mjs",
|
|
46
|
+
"require": "./src/polyfills/ws/index.cjs"
|
|
47
|
+
}
|
|
35
48
|
}
|
|
36
49
|
},
|
|
37
50
|
"type": "commonjs",
|
|
@@ -103,15 +116,12 @@
|
|
|
103
116
|
"publint": "^0.2.5",
|
|
104
117
|
"rimraf": "^5.0.1",
|
|
105
118
|
"rollup": "^3.25.1",
|
|
106
|
-
"rollup-plugin-typescript2": "^0.34.1",
|
|
107
119
|
"ts-jest": "^29.1.0",
|
|
108
120
|
"ts-node": "^10.9.1",
|
|
109
121
|
"tslib": "^2.5.3",
|
|
110
122
|
"typescript": "^5.2.2"
|
|
111
123
|
},
|
|
112
124
|
"dependencies": {
|
|
113
|
-
"@swimburger/isomorphic-streams": "^1.1.1",
|
|
114
|
-
"isomorphic-ws": "^5.0.0",
|
|
115
125
|
"ws": "^8.13.0"
|
|
116
126
|
}
|
|
117
127
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WritableStream } from "stream/web";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var ws = null;
|
|
2
|
+
|
|
3
|
+
if (typeof WebSocket !== "undefined") {
|
|
4
|
+
ws = WebSocket;
|
|
5
|
+
} else if (typeof MozWebSocket !== "undefined") {
|
|
6
|
+
ws = MozWebSocket;
|
|
7
|
+
} else if (typeof global !== "undefined") {
|
|
8
|
+
ws = global.WebSocket || global.MozWebSocket;
|
|
9
|
+
} else if (typeof window !== "undefined") {
|
|
10
|
+
ws = window.WebSocket || window.MozWebSocket;
|
|
11
|
+
} else if (typeof self !== "undefined") {
|
|
12
|
+
ws = self.WebSocket || self.MozWebSocket;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default ws;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("ws");
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { WritableStream } from "
|
|
2
|
-
import WebSocket from "
|
|
1
|
+
import { WritableStream } from "#streams";
|
|
2
|
+
import WebSocket from "#ws";
|
|
3
3
|
import { ErrorEvent, MessageEvent, CloseEvent } from "ws";
|
|
4
4
|
import {
|
|
5
5
|
RealtimeEvents,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|