@chatpanel/events 0.372.0 → 0.374.0

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/event-sync.js CHANGED
@@ -1 +1 @@
1
- import{toCloudEvent as g,fromCloudEvent as m}from"./cloudevents.js";const d=Object.freeze({maxEvents:500,maxBytes:512*1024});function h({events:o=[],cursor:t={},host:e,maxEvents:r=d.maxEvents,maxBytes:n=d.maxBytes}={}){const c=Number.isInteger(t?.[e])?t[e]:-1,u=o.filter(s=>s&&s.host===e&&s.seq>c).sort((s,p)=>s.seq-p.seq),i=[];let f=0,a=null;for(const s of u){const p=g(s),l=JSON.stringify(p).length;if(i.length&&(i.length>=r||f+l>n))break;i.push(p),f+=l,a=s.seq}return{batch:i,seq:a,remaining:u.length-i.length}}function v(o,t){const e={appended:0,duplicates:0,rejected:[],events:[]};for(const r of Array.isArray(t)?t:[]){let n;try{n=m(r),o.append(n).appended?(e.appended+=1,e.events.push(n)):e.duplicates+=1}catch(c){e.rejected.push({id:typeof r?.id=="string"?r.id:null,code:c?.code||"INGEST",message:String(c?.message||c)})}}return e}function x(o){const t=o?.cursor??o;if(!t||typeof t!="object"||Array.isArray(t))return{};const e={};for(const[r,n]of Object.entries(t))typeof r=="string"&&Number.isInteger(n)&&n>=0&&(e[r]=n);return e}export{d as PUSH_LIMITS,x as cursorOf,v as ingestCloudEvents,h as planEventPush};
1
+ import{toCloudEvent as g,fromCloudEvent as m}from"./cloudevents.js";const d=Object.freeze({maxEvents:500,maxBytes:512*1024});function h({events:s=[],cursor:t={},host:e,maxEvents:n=d.maxEvents,maxBytes:r=d.maxBytes}={}){const i=Number.isInteger(t?.[e])?t[e]:-1,a=s.filter(o=>o&&o.host===e&&o.seq>i).sort((o,u)=>o.seq-u.seq),c=[];let l=0,f=null;for(const o of a){const u=g(o),p=JSON.stringify(u).length;if(c.length&&(c.length>=n||l+p>r))break;c.push(u),l+=p,f=o.seq}return{batch:c,seq:f,remaining:a.length-c.length}}function v({cursor:s={},host:t,top:e=-1}={}){const n=Number.isInteger(s?.[t])?s[t]:-1;return(Number.isInteger(e)?e:-1)<n?{base:n+1}:null}function b(s,t){const e={appended:0,duplicates:0,rejected:[],events:[]};for(const n of Array.isArray(t)?t:[]){let r;try{r=m(n),s.append(r).appended?(e.appended+=1,e.events.push(r)):e.duplicates+=1}catch(i){e.rejected.push({id:typeof n?.id=="string"?n.id:null,code:i?.code||"INGEST",message:String(i?.message||i)})}}return e}function x(s){const t=s?.cursor??s;if(!t||typeof t!="object"||Array.isArray(t))return{};const e={};for(const[n,r]of Object.entries(t))typeof n=="string"&&Number.isInteger(r)&&r>=0&&(e[n]=r);return e}export{d as PUSH_LIMITS,x as cursorOf,b as ingestCloudEvents,h as planEventPush,v as seqStall};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatpanel/events",
3
- "version": "0.372.0",
3
+ "version": "0.374.0",
4
4
  "description": "The canonical ChatPanel event-log and capability contracts — typed durable facts, clock-free deterministic linearization, schema upcasting, and the invariants the replay harness asserts. Pure, dependency-free ESM shared by the ChatPanel extension, gateway and bridge.",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/voice-scribe.js CHANGED
@@ -1 +1 @@
1
- import{createChannelTranscript as W}from"./voice-channels.js";const X=4e3,Y=3e4,F=3e3,Z={setTimeout:(r,n)=>setTimeout(r,n),clearTimeout:r=>clearTimeout(r),setInterval:(r,n)=>setInterval(r,n),clearInterval:r=>clearInterval(r)},G=()=>Math.random().toString(36).slice(2,8),U=r=>{const n=new Date(r);return`${n.getHours()}:${String(n.getMinutes()).padStart(2,"0")}`};function et({mic:r="self",playback:n=!0,resume:o=null,live:v=!1,echoGuard:A=!1,openEar:y,capturePlayback:R,persist:w,onUpdate:m,onEnd:P,onNotice:C,onLine:_,now:f=Date.now,newId:j=G,timers:b=Z,flushDebounceMs:D=X,heartbeatMs:L=Y}={}){if(typeof y!="function")throw new Error("createAudioScribe: openEar is required");if(typeof w!="function")throw new Error("createAudioScribe: persist is required");if(n&&typeof R!="function")throw new Error("createAudioScribe: capturePlayback is required when playback is on");return{async start(){let i=null;if(n)try{i=await R({onEnded:()=>I("ended")})}catch(t){return t?.code!=="cancelled"&&C?.({level:"error",text:t?.message||"Could not capture what is playing"}),null}const E=o?.startedAt||f(),$=o?.meetingId||`mtg_${i?"audio":"room"}_${E.toString(36)}_${j()}`,l=W({now:f,mic:r,playback:!!i,seed:o?.segments||[],echoGuard:A});let h=!0,H=!1,q=o?.segments?.length?Math.max(...o.segments.map(t=>Number(t.t)||0))+1:0,z=0,T=null,O=null,x=!1;const g={mic:null,playback:null};let p=[],k=null;const J=t=>l.segments().some(s=>s.sid===t);function M(t=!1){const s=t?p:p.filter(e=>f()-e.at>=F);p=t?[]:p.filter(e=>f()-e.at<F);for(const e of s)J(e.line.sid)&&_?.(e.line);k&&(b.clearTimeout(k),k=null),p.length&&(k=b.setTimeout(()=>{k=null,M()},Math.max(0,F-(f()-p[0].at))))}function K(t,s,e){if(!_)return;const a={sid:s.sid,t:s.t,channel:t,speaker:s.speaker,text:e};if(t==="mic"&&A&&i){p.push({at:f(),line:a}),k||M();return}_(a)}const u={mic:"starting",playback:i?"starting":"off",lang:null},Q=()=>{if(o?.title)return o.title;const t=U(E);return i?i.surface==="screen"?`System audio · ${t}`:i.surface==="tab"?`Tab audio · ${t}`:`Audio · ${t}`:`Room · ${t}`};async function S(t="live",s){if(x)return;const e=l.record({id:$,startedAt:E,status:t,endReason:s,title:Q()}),a=l.delta(q);let c=null;try{c=await w(e,a)}catch{return}if(c?.limit){x=!0,I("limit");return}if(c?.ok===!1)return;z=f();const N=e.segments[e.segments.length-1];N&&(q=N.t)}function V(){if(h){if(f()-z>=D){S("live");return}T||(T=b.setTimeout(()=>{T=null,h&&S("live")},D))}}const B=(t,s)=>y(t,{...s,on:{onStart:()=>{u[t]="listening",m?.(d())},onInterim:e=>{l.interim(t,e),m?.(d())},onFinal:(e,a)=>{const c=l.final(t,e,a||{});c&&(K(t,c,String(e).replace(/\s+/g," ").trim()),V(),m?.(d()))},onStatus:e=>{e?.state==="language"&&e.lang?u.lang=e.lang:e?.state&&e.state!=="ready"&&(u[t]=e.state==="diarize"?"loading speaker model":e.state),m?.(d())},onError:({message:e,fatal:a}={})=>{u[t]=a?"failed":"listening",C?.({level:"error",text:`${t==="mic"?"Microphone":"Playback"}: ${e||"failed"}`});const c=t==="mic"?u.playback:u.mic;a&&(c==="failed"||c==="off")&&I("ended"),m?.(d())},onEnd:()=>{h&&u[t]!=="failed"&&(u[t]="stopped"),m?.(d())}}});g.mic=B("mic",{diarize:r==="room",interims:!!v,stream:null}),g.mic.start(),i&&(g.playback=B("playback",{diarize:!0,interims:!!v,stream:i.stream}),g.playback.start()),O=b.setInterval(()=>{h&&S("live")},L);function d(){return{meetingId:$,startedAt:E,mic:r,surface:i?i.surface:"mic",status:{...u},audio:{mic:g.mic?.stats?.()||null,playback:g.playback?.stats?.()||null},speakers:l.speakers(),interims:l.interims(),lines:l.segments().length,last:l.last("playback")||l.last("mic")}}function I(t="user"){if(H)return;H=!0,h=!1,b.clearInterval(O),T&&(b.clearTimeout(T),T=null),M(!0);for(const a of Object.values(g))try{a?.stop()}catch{}try{i?.stop()}catch{}(x?Promise.resolve():S("ended",t==="user"?"user":t==="interrupted"?"interrupted":"ended")).finally(()=>P?.({meetingId:$,reason:t}))}return m?.(d()),{meetingId:$,isRunning:()=>h,snapshot:d,stop:I,flush:()=>S("live")}}}}function rt(r,{now:n=Date.now()}={}){if(!r?.id)return null;const{id:o,title:v,tags:A,...y}=r;return{id:`meeting:${o}`,title:v||"Audio transcript",...A?{tags:A}:{},createdAt:r.startedAt||n,updatedAt:r.endedAt||n,body:{...y,segments:Array.isArray(y.segments)?y.segments:[]},meta:{origin:"capture",readOnly:!1,live:r.status!=="ended"}}}export{X as FLUSH_DEBOUNCE_MS,Y as HEARTBEAT_MS,F as LINE_SETTLE_MS,et as createAudioScribe,rt as meetingRecordFromCapture};
1
+ import{createChannelTranscript as Y}from"./voice-channels.js";const Z=4e3,U=3e4,R=3e3,tt={setTimeout:(i,s)=>setTimeout(i,s),clearTimeout:i=>clearTimeout(i),setInterval:(i,s)=>setInterval(i,s),clearInterval:i=>clearInterval(i)},et=()=>Math.random().toString(36).slice(2,8),N=new Set(["loading","downloading","loading speaker model"]);function st(i,{now:s=Date.now()}={}){if(!i)return null;const r=Math.max(0,Math.round((s-i.since)/1e3)),f=r>=5?` · ${r>=60?`${Math.floor(r/60)}m ${String(r%60).padStart(2,"0")}s`:`${r}s`}`:"";return i.state==="loading speaker model"?`preparing speaker labels${f}`:i.state==="downloading"?`downloading the speech model${Number.isFinite(i.pct)?` ${Math.round(i.pct)}%`:""}${f} — once, then it works offline; what is said meanwhile is kept`:`preparing the speech model${f}${r>=15?" — the first load after an update can take a few minutes":""}; what is said meanwhile is kept`}const it=i=>{const s=new Date(i);return`${s.getHours()}:${String(s.getMinutes()).padStart(2,"0")}`};function at({mic:i="self",playback:s=!0,resume:r=null,live:f=!1,echoGuard:A=!1,openEar:h,capturePlayback:D,persist:C,onUpdate:p,onEnd:G,onNotice:H,onLine:x,now:c=Date.now,newId:J=et,timers:k=tt,flushDebounceMs:P=Z,heartbeatMs:K=U}={}){if(typeof h!="function")throw new Error("createAudioScribe: openEar is required");if(typeof C!="function")throw new Error("createAudioScribe: persist is required");if(s&&typeof D!="function")throw new Error("createAudioScribe: capturePlayback is required when playback is on");return{async start(){let n=null;if(s)try{n=await D({onEnded:()=>I("ended")})}catch(t){return t?.code!=="cancelled"&&H?.({level:"error",text:t?.message||"Could not capture what is playing"}),null}const w=r?.startedAt||c(),E=r?.meetingId||`mtg_${n?"audio":"room"}_${w.toString(36)}_${J()}`,u=Y({now:c,mic:i,playback:!!n,seed:r?.segments||[],echoGuard:A});let b=!0,q=!1,z=r?.segments?.length?Math.max(...r.segments.map(t=>Number(t.t)||0))+1:0,O=0,$=null,B=null,M=!1;const g={mic:null,playback:null};let y=[],S=null;const Q=t=>u.segments().some(l=>l.sid===t);function _(t=!1){const l=t?y:y.filter(e=>c()-e.at>=R);y=t?[]:y.filter(e=>c()-e.at<R);for(const e of l)Q(e.line.sid)&&x?.(e.line);S&&(k.clearTimeout(S),S=null),y.length&&(S=k.setTimeout(()=>{S=null,_()},Math.max(0,R-(c()-y[0].at))))}function V(t,l,e){if(!x)return;const o={sid:l.sid,t:l.t,channel:t,speaker:l.speaker,text:e};if(t==="mic"&&A&&n){y.push({at:c(),line:o}),S||_();return}x(o)}const a={mic:"starting",playback:n?"starting":"off",lang:null},T={mic:null,playback:null},F=t=>{N.has(a[t])&&(a[t]="listening"),T[t]=null},W=()=>{if(r?.title)return r.title;const t=it(w);return n?n.surface==="screen"?`System audio · ${t}`:n.surface==="tab"?`Tab audio · ${t}`:`Audio · ${t}`:`Room · ${t}`};async function v(t="live",l){if(M)return;const e=u.record({id:E,startedAt:w,status:t,endReason:l,title:W()}),o=u.delta(z);let d=null;try{d=await C(e,o)}catch{return}if(d?.limit){M=!0,I("limit");return}if(d?.ok===!1)return;O=c();const L=e.segments[e.segments.length-1];L&&(z=L.t)}function X(){if(b){if(c()-O>=P){v("live");return}$||($=k.setTimeout(()=>{$=null,b&&v("live")},P))}}const j=(t,l)=>h(t,{...l,on:{onStart:()=>{N.has(a[t])||(a[t]="listening"),p?.(m())},onInterim:e=>{F(t),u.interim(t,e),p?.(m())},onFinal:(e,o)=>{F(t);const d=u.final(t,e,o||{});d&&(V(t,d,String(e).replace(/\s+/g," ").trim()),X(),p?.(m()))},onStatus:e=>{e?.state==="language"&&e.lang?a.lang=e.lang:e?.state==="ready"?F(t):e?.state&&(a[t]=e.state==="diarize"?"loading speaker model":e.state,N.has(a[t])&&(T[t]={state:a[t],pct:Number.isFinite(e.pct)?e.pct:null,since:T[t]?.since??c()})),p?.(m())},onError:({message:e,fatal:o}={})=>{a[t]=o?"failed":"listening",H?.({level:"error",text:`${t==="mic"?"Microphone":"Playback"}: ${e||"failed"}`});const d=t==="mic"?a.playback:a.mic;o&&(d==="failed"||d==="off")&&I("ended"),p?.(m())},onEnd:()=>{b&&a[t]!=="failed"&&(a[t]="stopped"),p?.(m())}}});g.mic=j("mic",{diarize:i==="room",interims:!!f,stream:null}),g.mic.start(),n&&(g.playback=j("playback",{diarize:!0,interims:!!f,stream:n.stream}),g.playback.start()),B=k.setInterval(()=>{b&&v("live")},K);function m(){return{meetingId:E,startedAt:w,mic:i,surface:n?n.surface:"mic",status:{...a},preparing:{mic:T.mic,playback:T.playback},audio:{mic:g.mic?.stats?.()||null,playback:g.playback?.stats?.()||null},speakers:u.speakers(),interims:u.interims(),lines:u.segments().length,last:u.last("playback")||u.last("mic")}}function I(t="user"){if(q)return;q=!0,b=!1,k.clearInterval(B),$&&(k.clearTimeout($),$=null),_(!0);for(const o of Object.values(g))try{o?.stop()}catch{}try{n?.stop()}catch{}(M?Promise.resolve():v("ended",t==="user"?"user":t==="interrupted"?"interrupted":"ended")).finally(()=>G?.({meetingId:E,reason:t}))}return p?.(m()),{meetingId:E,isRunning:()=>b,snapshot:m,stop:I,flush:()=>v("live")}}}}function nt(i,{now:s=Date.now()}={}){if(!i?.id)return null;const{id:r,title:f,tags:A,...h}=i;return{id:`meeting:${r}`,title:f||"Audio transcript",...A?{tags:A}:{},createdAt:i.startedAt||s,updatedAt:i.endedAt||s,body:{...h,segments:Array.isArray(h.segments)?h.segments:[]},meta:{origin:"capture",readOnly:!1,live:i.status!=="ended"}}}export{Z as FLUSH_DEBOUNCE_MS,U as HEARTBEAT_MS,R as LINE_SETTLE_MS,at as createAudioScribe,nt as meetingRecordFromCapture,st as preparingNote};