@chatpanel/gateway 0.121.5 → 0.136.2

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/src/stt-remote.js CHANGED
@@ -1 +1 @@
1
- const L=16e3;function b(t,a=16e3){const o=t.length,e=Buffer.alloc(44+o*2);e.write("RIFF",0),e.writeUInt32LE(36+o*2,4),e.write("WAVE",8),e.write("fmt ",12),e.writeUInt32LE(16,16),e.writeUInt16LE(1,20),e.writeUInt16LE(1,22),e.writeUInt32LE(a,24),e.writeUInt32LE(a*2,28),e.writeUInt16LE(2,32),e.writeUInt16LE(16,34),e.write("data",36),e.writeUInt32LE(o*2,40);for(let r=0;r<o;r++){const i=Math.max(-1,Math.min(1,t[r]));e.writeInt16LE(i<0?i*32768:i*32767,44+r*2)}return e}const _=Object.freeze(["openai","elevenlabs"]),E=Object.freeze({openai:"/audio/transcriptions",elevenlabs:"/speech-to-text"});function x(t){const a=String(t||"").trim();if(!a)return null;try{return new Intl.Locale(a).language.toLowerCase()||null}catch{return null}}function S(t,{fetchImpl:a,now:o=()=>performance.now()}={}){if(!t||!["remote","container"].includes(t.kind)||!/^https?:\/\//.test(t.url||""))throw new Error("stt-remote: a remote provider needs a url");const e=t.adapter||"openai";if(!_.includes(e))throw new Error(`stt-remote: unknown adapter ${e}`);const r=t.url.replace(/\/+$/,"");return{provider:{kind:t.kind,url:t.url,adapter:e,model:t.model||(e==="elevenlabs"?"scribe_v1":"whisper-1")},async transcribe(i,{lang:c=null,langs:h=null,timeoutMs:p=3e4}={}){const d=o();if(e==="elevenlabs"){const n=new FormData;n.append("file",new Blob([b(i)],{type:"audio/wav"}),"segment.wav"),n.append("model_id",t.model||"scribe_v1"),c&&!h?.length&&n.append("language_code",c),n.append("tag_audio_events","false"),n.append("diarize","false");const u=await a(`${r}${E.elevenlabs}`,{method:"POST",headers:{accept:"application/json",...t.token?{"xi-api-key":t.token}:{}},body:n,signal:AbortSignal.timeout(p)});if(!u.ok){const f=await u.text().catch(()=>"");throw new Error(`elevenlabs transcription ${u.status}${f?`: ${f.slice(0,200)}`:""}`)}const w=await u.json().catch(()=>({})),m=x(w?.language_code);return{text:String(w?.text||"").trim(),ms:Math.round(o()-d),...m?{lang:m,confidence:Number(w?.language_probability)||null}:{}}}const s=new FormData;s.append("file",new Blob([b(i)],{type:"audio/wav"}),"segment.wav"),s.append("model",t.model||"whisper-1"),s.append("response_format","json"),c&&s.append("language",c);const l=await a(`${r}${E.openai}`,{method:"POST",headers:{accept:"application/json",...t.token?{authorization:`Bearer ${t.token}`}:{}},body:s,signal:AbortSignal.timeout(p)});if(!l.ok){const n=await l.text().catch(()=>"");throw new Error(`${e} transcription ${l.status}${n?`: ${n.slice(0,200)}`:""}`)}const g=await l.json().catch(()=>({}));return{text:String(g?.text||"").trim(),ms:Math.round(o()-d)}}}}export{_ as STT_REMOTE_ADAPTERS,E as STT_REMOTE_PATHS,S as createRemoteTranscriber,x as languageFromService,b as wavFromFloat32};
1
+ const _=16e3;function h(e,a=16e3){const o=e.length,t=Buffer.alloc(44+o*2);t.write("RIFF",0),t.writeUInt32LE(36+o*2,4),t.write("WAVE",8),t.write("fmt ",12),t.writeUInt32LE(16,16),t.writeUInt16LE(1,20),t.writeUInt16LE(1,22),t.writeUInt32LE(a,24),t.writeUInt32LE(a*2,28),t.writeUInt16LE(2,32),t.writeUInt16LE(16,34),t.write("data",36),t.writeUInt32LE(o*2,40);for(let r=0;r<o;r++){const i=Math.max(-1,Math.min(1,e[r]));t.writeInt16LE(i<0?i*32768:i*32767,44+r*2)}return t}const x=Object.freeze(["openai","elevenlabs","cloudflare"]),g=Object.freeze({openai:"/audio/transcriptions",elevenlabs:"/speech-to-text",cloudflare:"/run/<model>"});function S(e){const a=String(e||"").trim();if(!a)return null;try{return new Intl.Locale(a).language.toLowerCase()||null}catch{return null}}function k(e,{fetchImpl:a,now:o=()=>performance.now()}={}){if(!e||!["remote","container"].includes(e.kind)||!/^https?:\/\//.test(e.url||""))throw new Error("stt-remote: a remote provider needs a url");const t=e.adapter||"openai";if(!x.includes(t))throw new Error(`stt-remote: unknown adapter ${t}`);const r=e.url.replace(/\/+$/,"");return{provider:{kind:e.kind,url:e.url,adapter:t,model:e.model||(t==="elevenlabs"?"scribe_v1":t==="cloudflare"?"@cf/openai/whisper":"whisper-1")},async transcribe(i,{lang:l=null,langs:E=null,timeoutMs:f=3e4}={}){const m=o();if(t==="elevenlabs"){const n=new FormData;n.append("file",new Blob([h(i)],{type:"audio/wav"}),"segment.wav"),n.append("model_id",e.model||"scribe_v1"),l&&!E?.length&&n.append("language_code",l),n.append("tag_audio_events","false"),n.append("diarize","false");const s=await a(`${r}${g.elevenlabs}`,{method:"POST",headers:{accept:"application/json",...e.token?{"xi-api-key":e.token}:{}},body:n,signal:AbortSignal.timeout(f)});if(!s.ok){const p=await s.text().catch(()=>"");throw new Error(`elevenlabs transcription ${s.status}${p?`: ${p.slice(0,200)}`:""}`)}const u=await s.json().catch(()=>({})),c=S(u?.language_code);return{text:String(u?.text||"").trim(),ms:Math.round(o()-m),...c?{lang:c,confidence:Number(u?.language_probability)||null}:{}}}if(t==="cloudflare"){const n=e.model||"@cf/openai/whisper",s=h(i),u=/large-v3-turbo/.test(n),c=await a(`${r}/run/${n}`,{method:"POST",headers:{accept:"application/json","content-type":u?"application/json":"application/octet-stream",...e.token?{authorization:`Bearer ${e.token}`}:{}},body:u?JSON.stringify({audio:Buffer.from(s).toString("base64"),...l?{language:l}:{}}):s,signal:AbortSignal.timeout(f)});if(!c.ok){const b=await c.text().catch(()=>"");throw new Error(`cloudflare transcription ${c.status}${b?`: ${b.slice(0,200)}`:""}`)}const p=await c.json().catch(()=>({}));return{text:String(p?.result?.text||"").trim(),ms:Math.round(o()-m)}}const w=new FormData;w.append("file",new Blob([h(i)],{type:"audio/wav"}),"segment.wav"),w.append("model",e.model||"whisper-1"),w.append("response_format","json"),l&&w.append("language",l);const d=await a(`${r}${g.openai}`,{method:"POST",headers:{accept:"application/json",...e.token?{authorization:`Bearer ${e.token}`}:{}},body:w,signal:AbortSignal.timeout(f)});if(!d.ok){const n=await d.text().catch(()=>"");throw new Error(`${t} transcription ${d.status}${n?`: ${n.slice(0,200)}`:""}`)}const $=await d.json().catch(()=>({}));return{text:String($?.text||"").trim(),ms:Math.round(o()-m)}}}}export{x as STT_REMOTE_ADAPTERS,g as STT_REMOTE_PATHS,k as createRemoteTranscriber,S as languageFromService,h as wavFromFloat32};
@@ -0,0 +1 @@
1
+ const h=1e4,f=1e3;function S({total:u=0}={}){let t=[];return(a,{now:l=Date.now(),skipped:M=!1,total:s=u}={})=>{(M||t.length&&a<t.at(-1).bytes)&&(t=[]);const e=t.at(-1);for((!e||l-e.at>=1e3)&&t.push({at:l,bytes:a});t.length>2&&l-t[0].at>1e4;)t.shift();const r=t[0],o=(l-r.at)/1e3,n=o>=1?Math.max(0,(a-r.bytes)/o):null,c=n&&s>a?Math.round((s-a)/n):n&&s?0:null;return{rate:n===null?null:Math.round(n),etaS:c}}}export{S as transferRate};
package/src/tts-remote.js CHANGED
@@ -1 +1 @@
1
- import{secureFetch as k}from"./secure-fetch.js";import{redactSegments as w,segment as S}from"./redact.js";const $=["local","openai","elevenlabs"];function b(e){return $.includes(String(e||""))}function D(e={}){const r=e.tts||{},t=e.capabilities?.tts;if(t&&typeof t=="object"){if(t.provider==="embedded")return null;const i=t.adapter||"openai";if(t.url&&b(i)){const c=r.remote||{};return{kind:i,baseUrl:String(t.url).replace(/\/+$/,""),model:t.model||c.model||(i==="elevenlabs"?"eleven_multilingual_v2":"tts-1"),voice:c.voice||(i==="elevenlabs"?"21m00Tcm4TlvDq8ikWAM":"alloy"),redact:t.redact!==!1,token:t.token||""}}}const n=r.provider||"local";if(n==="local"||!b(n))return null;const a=r.remote||{};return{kind:n,baseUrl:String(a.baseUrl||(n==="elevenlabs"?"https://api.elevenlabs.io/v1":"https://api.openai.com/v1")).replace(/\/+$/,""),model:a.model||(n==="elevenlabs"?"eleven_multilingual_v2":"tts-1"),voice:a.voice||(n==="elevenlabs"?"21m00Tcm4TlvDq8ikWAM":"alloy"),redact:a.redact!==!1}}function _(e){return/^[A-Za-z0-9_-]{1,64}$/.test(String(e||""))}async function E({dest:e,text:r,voice:t,speed:n=1,auth:a,redaction:i,isPro:c=!0,onEgress:h=null,fetchImpl:y=null}={}){const l=a||e?.token||"";if(!e)throw new Error("no remote TTS destination configured");const u=t||e.voice;if(!_(u))throw new Error(`invalid remote voice id: ${u}`);let d=String(r),v=0;if(e.redact&&i){const o={text:d},T=await w([S(()=>o.text,g=>{o.text=g})],i,{isPro:c,onEgress:h});d=o.text,v=T?.count||0}const x=y||k,m={"Content-Type":"application/json"};let p,f;if(e.kind==="elevenlabs"){p=`${e.baseUrl}/text-to-speech/${encodeURIComponent(u)}`,f={text:d,model_id:e.model};const o=U(l);o&&(m["xi-api-key"]=o)}else p=`${e.baseUrl}/audio/speech`,f={model:e.model,input:d,voice:u,response_format:"wav",speed:n},l&&(m.Authorization=l.startsWith("Bearer ")?l:`Bearer ${l}`);const s=await x(p,{method:"POST",headers:m,body:JSON.stringify(f)});if(!s.ok){let o=`HTTP ${s.status}`;try{o=(await s.text()).slice(0,300)||o}catch{}throw new Error(`remote tts failed: ${o}`)}return{audio:Buffer.from(await s.arrayBuffer()),contentType:s.headers.get("content-type")||"audio/mpeg",redacted:v}}function U(e){const r=String(e||"").trim();return r.toLowerCase().startsWith("bearer ")?r.slice(7).trim():r}export{$ as TTS_PROVIDERS,_ as isValidRemoteVoice,b as isValidTtsProvider,E as synthesizeRemote,D as ttsDestination};
1
+ import{secureFetch as g}from"./secure-fetch.js";import{redactSegments as T,segment as w}from"./redact.js";import{primaryProvider as $}from"./capability-config.js";const _=["local","openai","elevenlabs"];function b(e){return _.includes(String(e||""))}function z(e={}){const o=e.tts||{},t=$(e.capabilities?.tts,"tts",{destinations:e.destinations||[]});if(t&&typeof t=="object"){if(t.provider==="embedded")return null;const i=t.adapter||"openai";if(t.url&&b(i)){const c=o.remote||{};return{kind:i,baseUrl:String(t.url).replace(/\/+$/,""),model:t.model||c.model||(i==="elevenlabs"?"eleven_multilingual_v2":"tts-1"),voice:c.voice||(i==="elevenlabs"?"21m00Tcm4TlvDq8ikWAM":"alloy"),redact:t.redact!==!1,token:t.token||"",...t.endpoint?{endpoint:t.endpoint}:{}}}}const r=o.provider||"local";if(r==="local"||!b(r))return null;const a=o.remote||{};return{kind:r,baseUrl:String(a.baseUrl||(r==="elevenlabs"?"https://api.elevenlabs.io/v1":"https://api.openai.com/v1")).replace(/\/+$/,""),model:a.model||(r==="elevenlabs"?"eleven_multilingual_v2":"tts-1"),voice:a.voice||(r==="elevenlabs"?"21m00Tcm4TlvDq8ikWAM":"alloy"),redact:a.redact!==!1}}function A(e){return/^[A-Za-z0-9_-]{1,64}$/.test(String(e||""))}const E=new Set(["alloy","ash","ballad","coral","echo","fable","nova","onyx","sage","shimmer","verse"]);function P(e,o){const t=String(o||"");return t?e.endpoint?e.kind==="elevenlabs"?/^[A-Za-z0-9]{20}$/.test(t)?t:e.voice:E.has(t)?t:e.voice:t:e.voice}async function C({dest:e,text:o,voice:t,speed:r=1,auth:a,redaction:i,isPro:c=!0,onEgress:h=null,fetchImpl:y=null}={}){if(!e)throw new Error("no remote TTS destination configured");const l=e.endpoint&&e.token?e.token:a||e.token||"",u=P(e,t);if(!A(u))throw new Error(`invalid remote voice id: ${u}`);let m=String(o),v=0;if(e.redact&&i){const n={text:m},k=await T([w(()=>n.text,S=>{n.text=S})],i,{isPro:c,onEgress:h});m=n.text,v=k?.count||0}const x=y||g,p={"Content-Type":"application/json"};let d,f;if(e.kind==="elevenlabs"){d=`${e.baseUrl}/text-to-speech/${encodeURIComponent(u)}`,f={text:m,model_id:e.model};const n=U(l);n&&(p["xi-api-key"]=n)}else d=`${e.baseUrl}/audio/speech`,f={model:e.model,input:m,voice:u,response_format:"wav",speed:r},l&&(p.Authorization=l.startsWith("Bearer ")?l:`Bearer ${l}`);const s=await x(d,{method:"POST",headers:p,body:JSON.stringify(f)});if(!s.ok){let n=`HTTP ${s.status}`;try{n=(await s.text()).slice(0,300)||n}catch{}throw new Error(`remote tts failed: ${n}`)}return{audio:Buffer.from(await s.arrayBuffer()),contentType:s.headers.get("content-type")||"audio/mpeg",redacted:v}}function U(e){const o=String(e||"").trim();return o.toLowerCase().startsWith("bearer ")?o.slice(7).trim():o}export{_ as TTS_PROVIDERS,A as isValidRemoteVoice,b as isValidTtsProvider,P as remoteVoiceFor,C as synthesizeRemote,z as ttsDestination};
package/src/version.js CHANGED
@@ -1 +1 @@
1
- const o="0.121.5";export{o as VERSION};
1
+ const o="0.136.2";export{o as VERSION};