@neuvybe/jarvis 0.46.1 → 0.46.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/dist/index.js +8 -8
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -575,23 +575,23 @@ ${z.bold("jarvis models")}
|
|
|
575
575
|
`),1;if(t.textOnly||!t.voiceConfigured)return t.runText();if(!await t.micAvailable())return t.io.err(z.warn("no microphone detected - check it is plugged in and mic access / privacy is enabled. starting in text mode.")+`
|
|
576
576
|
`),t.runText();let e;try{e=await t.openVoiceSession()}catch{return t.io.err(z.fail("could not start jarvis - your config may be invalid. run `jarvis doctor`.")+`
|
|
577
577
|
`),1}try{return await t.runVoice(e),0}catch(r){if(r instanceof ds)return t.io.err(z.warn("could not start the microphone - check mic access / privacy. starting in text mode.")+`
|
|
578
|
-
`),t.runText();throw r}}import{randomUUID as bI}from"node:crypto";function sR({onlyFirst:t=!1}={}){let n="(?:\\u001B\\][\\s\\S]*?(?:\\u0007|\\u001B\\u005C|\\u009C))|[\\u001B\\u009B][[\\]()#;?]*(?:\\d{1,4}(?:[;:]\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]";return new RegExp(n,t?void 0:"g")}var USe=sR();function oR(t){if(typeof t!="string")throw new TypeError(`Expected a \`string\`, got \`${typeof t}\``);return!t.includes("\x1B")&&!t.includes("\x9B")?t:t.replace(USe,"")}var q8=[161,161,164,164,167,168,170,170,173,174,176,180,182,186,188,191,198,198,208,208,215,216,222,225,230,230,232,234,236,237,240,240,242,243,247,250,252,252,254,254,257,257,273,273,275,275,283,283,294,295,299,299,305,307,312,312,319,322,324,324,328,331,333,333,338,339,358,359,363,363,462,462,464,464,466,466,468,468,470,470,472,472,474,474,476,476,593,593,609,609,708,708,711,711,713,715,717,717,720,720,728,731,733,733,735,735,768,879,913,929,931,937,945,961,963,969,1025,1025,1040,1103,1105,1105,8208,8208,8211,8214,8216,8217,8220,8221,8224,8226,8228,8231,8240,8240,8242,8243,8245,8245,8251,8251,8254,8254,8308,8308,8319,8319,8321,8324,8364,8364,8451,8451,8453,8453,8457,8457,8467,8467,8470,8470,8481,8482,8486,8486,8491,8491,8531,8532,8539,8542,8544,8555,8560,8569,8585,8585,8592,8601,8632,8633,8658,8658,8660,8660,8679,8679,8704,8704,8706,8707,8711,8712,8715,8715,8719,8719,8721,8721,8725,8725,8730,8730,8733,8736,8739,8739,8741,8741,8743,8748,8750,8750,8756,8759,8764,8765,8776,8776,8780,8780,8786,8786,8800,8801,8804,8807,8810,8811,8814,8815,8834,8835,8838,8839,8853,8853,8857,8857,8869,8869,8895,8895,8978,8978,9312,9449,9451,9547,9552,9587,9600,9615,9618,9621,9632,9633,9635,9641,9650,9651,9654,9655,9660,9661,9664,9665,9670,9672,9675,9675,9678,9681,9698,9701,9711,9711,9733,9734,9737,9737,9742,9743,9756,9756,9758,9758,9792,9792,9794,9794,9824,9825,9827,9829,9831,9834,9836,9837,9839,9839,9886,9887,9919,9919,9926,9933,9935,9939,9941,9953,9955,9955,9960,9961,9963,9969,9972,9972,9974,9977,9979,9980,9982,9983,10045,10045,10102,10111,11094,11097,12872,12879,57344,63743,65024,65039,65533,65533,127232,127242,127248,127277,127280,127337,127344,127373,127375,127376,127387,127404,917760,917999,983040,1048573,1048576,1114109],z8=12288,j8=65510,W8=[12288,12288,65281,65376,65504,65510];var J8=4352,Z8=262141,aR=[4352,4447,8986,8987,9001,9002,9193,9196,9200,9200,9203,9203,9725,9726,9748,9749,9776,9783,9800,9811,9855,9855,9866,9871,9875,9875,9889,9889,9898,9899,9917,9918,9924,9925,9934,9934,9940,9940,9962,9962,9970,9971,9973,9973,9978,9978,9981,9981,9989,9989,9994,9995,10024,10024,10060,10060,10062,10062,10067,10069,10071,10071,10133,10135,10160,10160,10175,10175,11035,11036,11088,11088,11093,11093,11904,11929,11931,12019,12032,12245,12272,12287,12289,12350,12353,12438,12441,12543,12549,12591,12593,12686,12688,12773,12783,12830,12832,12871,12880,42124,42128,42182,43360,43388,44032,55203,63744,64255,65040,65049,65072,65106,65108,65126,65128,65131,94176,94180,94192,94198,94208,101589,101631,101662,101760,101874,110576,110579,110581,110587,110589,110590,110592,110882,110898,110898,110928,110930,110933,110933,110948,110951,110960,111355,119552,119638,119648,119670,126980,126980,127183,127183,127374,127374,127377,127386,127488,127490,127504,127547,127552,127560,127568,127569,127584,127589,127744,127776,127789,127797,127799,127868,127870,127891,127904,127946,127951,127955,127968,127984,127988,127988,127992,128062,128064,128064,128066,128252,128255,128317,128331,128334,128336,128359,128378,128378,128405,128406,128420,128420,128507,128591,128640,128709,128716,128716,128720,128722,128725,128728,128732,128735,128747,128748,128756,128764,128992,129003,129008,129008,129292,129338,129340,129349,129351,129535,129648,129660,129664,129674,129678,129734,129736,129736,129741,129756,129759,129770,129775,129784,131072,196605,196608,262141];var BI=(t,e)=>{let r=0,i=Math.floor(t.length/2)-1;for(;r<=i;){let n=Math.floor((r+i)/2),s=n*2;if(e<t[s])i=n-1;else if(e>t[s+1])r=n+1;else return!0}return!1};var X8=19968,[PSe,HSe]=VSe(aR);function VSe(t){let e=t[0],r=t[1];for(let i=0;i<t.length;i+=2){let n=t[i],s=t[i+1];if(X8>=n&&X8<=s)return[n,s];s-n>r-e&&(e=n,r=s)}return[e,r]}var K8=t=>t<161||t>1114109?!1:BI(q8,t),eZ=t=>t<z8||t>j8?!1:BI(W8,t);var tZ=t=>t>=PSe&&t<=HSe?!0:t<J8||t>Z8?!1:BI(aR,t);function GSe(t){if(!Number.isSafeInteger(t))throw new TypeError(`Expected a code point, got \`${typeof t}\`.`)}function _h(t,{ambiguousAsWide:e=!1}={}){return GSe(t),eZ(t)||tZ(t)||e&&K8(t)?2:1}var YSe=new Intl.Segmenter,rZ=/^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Format}|\p{Mark}|\p{Surrogate})+$/v,qSe=/^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Mark}\p{Surrogate}]+/v,zSe=/^\p{RGI_Emoji}$/v,jSe=/^[\d#*]\u20E3$/,WSe=/\p{Extended_Pictographic}/gu;function JSe(t){if(t.length>50)return!1;if(jSe.test(t))return!0;if(t.includes("\u200D")){let e=t.match(WSe);return e!==null&&e.length>=2}return!1}function ZSe(t){return t.replace(qSe,"")}function XSe(t){return rZ.test(t)}function nZ(t){return t>=4352&&t<=4447||t>=43360&&t<=43388}function iZ(t){return t>=4448&&t<=4519||t>=55216&&t<=55238}function sZ(t){return t>=4520&&t<=4607||t>=55243&&t<=55291}function KSe(t){return nZ(t)||iZ(t)||sZ(t)}function eDe(t,e){let r=[];for(let n of t)rZ.test(n)||r.push(n.codePointAt(0));if(r.length===0)return;let i=0;for(let n=0;n<r.length;n++){let s=r[n];if(!KSe(s)){if(i===0)return;for(let o=n;o<r.length;o++)i+=_h(r[o],e);return i}if(nZ(s)&&iZ(r[n+1])){i+=2,n+=sZ(r[n+2])?2:1;continue}i+=_h(s,e)}return i}function tDe(t,e){let r=0,i=!0;for(let n of t){if(i){i=!1;continue}n>="\uFF00"&&n<="\uFFEF"&&(r+=_h(n.codePointAt(0),e))}return r}function xI(t,e={}){if(typeof t!="string"||t.length===0)return 0;let{ambiguousIsNarrow:r=!0,countAnsiEscapeCodes:i=!1}=e,n=t;if(!i&&(n.includes("\x1B")||n.includes("\x9B"))&&(n=oR(n)),n.length===0)return 0;if(/^[\u0020-\u007E]*$/.test(n))return n.length;let s=0,o={ambiguousAsWide:!r};for(let{segment:a}of YSe.segment(n)){if(XSe(a))continue;if(zSe.test(a)||JSe(a)){s+=2;continue}let c=ZSe(a),l=eDe(c,o);if(l!==void 0){s+=l;continue}let A=c.codePointAt(0);s+=_h(A,o),s+=tDe(c,o)}return s}hA();Tt();yI();var QI=class{now;refRing=new Float32Array(4096);sessionGain=2;sessionDelayMs;sessionMargin=2;gain=2;delayMs;corr;margin=2;frozen=!1;micHops=[];refObserved=!1;inExceed=!1;exceedRunMs=0;calmRunMs=0;lastRefResumeAtMs;prevNarrowActive=!1;narrowQuietFrames=0;closest;closestOffsetMs;replyMicStartMs;windowExceedMs=0;ratioP10;ratioP90;onsetVetoes=0;onsetVetoesInGap=0;firedRead;suppTrend=[];residRatios=[];suppressedRunFrames=0;sessionProven=!1;quietFloorRms=[];clampAdmitted=!0;floorEverWarm=!1;residualSeen=!1;constructor(e={}){this.now=e.now??Date.now}beginReply(){this.refRing.fill(0),this.micHops.length=0,this.frozen=!1,this.gain=this.sessionGain,this.delayMs=this.sessionDelayMs,this.margin=this.sessionMargin,this.corr=void 0,this.refObserved=!1,this.inExceed=!1,this.exceedRunMs=0,this.calmRunMs=0,this.lastRefResumeAtMs=void 0,this.prevNarrowActive=!1,this.narrowQuietFrames=0,this.closest=void 0,this.closestOffsetMs=void 0,this.replyMicStartMs=void 0,this.windowExceedMs=0,this.ratioP10=void 0,this.ratioP90=void 0,this.onsetVetoes=0,this.onsetVetoesInGap=0,this.firedRead=void 0,this.suppTrend.length=0,this.residualSeen=!1,this.suppressedRunFrames=0}ingestReference(e,r){e.byteLength<2||(this.refObserved=!0,aZ(e,r,(i,n)=>{let s=fu(i);n>this.refRing[s]&&(this.refRing[s]=n)}))}feedMic(e,r,i){let n=e.byteLength>>1;if(n===0)return!1;let s=n/16,o=r-s;this.replyMicStartMs??=o;let a=!this.frozen;!this.frozen&&this.refObserved&&(this.micHops.length<200&&aZ(e,o,(v,S)=>{this.micHops.length<200&&this.micHops.push({slot:v,rms:S})}),this.micHops.length>=80&&this.tryFreeze());let c=this.alignedRefMax(o,r),l=i??null,A=l!==null&&l.length>0?rDe(l):null;A!==null&&(this.residualSeen=!0);let d=A!==null&&this.residualConverged,h=cZ(e),u=d&&A!==null?A:h,f=this.gain*c*this.margin,m=d?.01:Math.max(.01,f),E=this.delayMs===void 0?c>=.005:this.narrowAlignedMax(o,r,this.delayMs)>=.005;E?(!this.prevNarrowActive&&this.narrowQuietFrames>=2&&(this.lastRefResumeAtMs=r),this.narrowQuietFrames=0):this.narrowQuietFrames++,this.prevNarrowActive=E;let I=d&&this.lastRefResumeAtMs!==void 0&&r-this.lastRefResumeAtMs<700,C=this.exceedanceArmed&&f>.01&&u>m&&!I;if(c<.005&&!C&&!this.inExceed&&(this.quietFloorRms.push(h),this.quietFloorRms.length>150&&this.quietFloorRms.shift()),this.updateClampAdmission(),l!==null&&A!==null&&!C&&!this.inExceed){if(this.frozen&&this.delayMs!==void 0){let v=Math.round(this.delayMs/20);nDe(l,o,(S,O)=>{let U=this.refRing[fu(S-v)];U<.005||(this.residRatios.push(O/U),this.residRatios.length>120&&this.residRatios.shift())})}if(c>=.005&&h>.02){let v=A/h<=.25&&A<=.01;this.suppressedRunFrames=v?this.suppressedRunFrames+1:0,this.suppressedRunFrames>=40&&(this.sessionProven=!0),this.suppTrend.length<2048&&this.replyMicStartMs!==void 0&&this.suppTrend.push({offsetMs:r-this.replyMicStartMs,ratio:A/h})}}if(this.exceedanceArmed&&f>.01){let v=u/m;(this.closest===void 0||v>this.closest)&&(this.closest=v,this.closestOffsetMs=r-this.replyMicStartMs),a&&C&&(this.windowExceedMs+=s)}let b=this.applyVerdict(C,s,d?150:64);return b&&(this.firedRead=d?"clamped":"raw"),b}get exceedanceArmed(){return this.refObserved&&this.gain>.02}observeQuietFrame(e){e.byteLength>>1!==0&&(this.quietFloorRms.push(cZ(e)),this.quietFloorRms.length>150&&this.quietFloorRms.shift())}shouldFireOnSpeechOnset(){if(!this.exceedanceArmed)return!0;let e=this.now(),r=this.alignedRefMax(e,e),i=this.gain*r*this.margin<=.01;return i||(this.onsetVetoes++,r<.005&&this.onsetVetoesInGap++),i}replyEnded(e){!this.frozen&&this.micHops.length>0&&this.tryFreeze();let r={refObserved:this.refObserved,gain:this.gain,gainSource:this.frozen?"learned":"seed",margin:this.margin,delayMs:this.delayMs,corr:this.corr,closest:this.closest,closestOffsetMs:this.closestOffsetMs,windowExceedMs:this.windowExceedMs,ratioP10:this.ratioP10,ratioP90:this.ratioP90,quietFloor:this.quietFloorEstimate(),onsetVetoes:this.onsetVetoes,onsetVetoesInGap:this.onsetVetoesInGap,fired:e,firedRead:e==="exceedance"?this.firedRead:void 0,residTrend:this.computeResidTrend(),aec:this.residualSeen?{converged:this.residualConverged,residualGain:this.residualGain()}:void 0};return this.residualSeen&&e==="exceedance"&&(this.residRatios.length=0,this.suppressedRunFrames=0,this.sessionProven=!1),r}computeResidTrend(){if(this.suppTrend.length<8)return;let e=this.suppTrend[this.suppTrend.length-1].offsetMs;if(e<=0)return;let r=[[],[],[],[]];for(let{offsetMs:n,ratio:s}of this.suppTrend)r[Math.min(3,Math.max(0,Math.floor(n/e*4)))].push(s);let i=n=>n.length>0?oZ(n):void 0;return[i(r[0]),i(r[1]),i(r[2]),i(r[3])]}residualGain(){return this.residRatios.length<50?this.gain:Math.min(4,oZ(this.residRatios))}get residualConverged(){return this.quietFloorAllowsClamp&&this.sessionProven&&this.suppressedRunFrames>=10&&this.residRatios.length>=50&&this.residualGain()<=this.gain/4}get quietFloorAllowsClamp(){return this.clampAdmitted}updateClampAdmission(){let e=this.quietFloorEstimate();e!==void 0&&(e<=.008?this.clampAdmitted=!0:(e>.01||!this.floorEverWarm)&&(this.clampAdmitted=!1),this.floorEverWarm=!0)}quietFloorEstimate(){if(this.quietFloorRms.length<20)return;let e=[...this.quietFloorRms].sort((r,i)=>r-i);return e[Math.floor(.1*(e.length-1))]}alignedRefMax(e,r){let i=this.frozen?this.delayMs:void 0,n=i===void 0?e-700-300:e-i-300,s=i===void 0?r+300:r-i+300,o=0;for(let a=Math.floor(n/20);a<=Math.floor(s/20);a++){let c=this.refRing[fu(a)];c>o&&(o=c)}return o}narrowAlignedMax(e,r,i){let n=0,s=Math.floor((e-i)/20),o=Math.floor((r-i)/20);for(let a=s;a<=o;a++){let c=this.refRing[fu(a)];c>n&&(n=c)}return n}tryFreeze(){let{lag:e,corr:r}=this.bestLag(),i=[];for(let{slot:a,rms:c}of this.micHops){let l=this.refRing[fu(a-e)];l>=.005&&i.push(c/l)}if(i.length<25)return;i.sort((a,c)=>a-c);let n=i.length>>1,s=i.length%2===1?i[n]:(i[n-1]+i[n])/2;this.gain=Math.min(4,s),this.delayMs=e*20,this.corr=r,this.ratioP10=i[Math.floor(.1*(i.length-1))],this.ratioP90=i[Math.floor(.9*(i.length-1))];let o=s>0?this.ratioP90/s:1;this.margin=Math.min(6,Math.max(2,1.25*o)),this.frozen=!0,this.sessionGain=this.gain,this.sessionDelayMs=this.delayMs,this.sessionMargin=this.margin}bestLag(){let e=Math.floor(35),r=0,i=-1/0;for(let n=0;n<=e;n++){let s=0,o=0,a=0,c=0,l=0,A=0;for(let{slot:m,rms:E}of this.micHops){let I=this.refRing[fu(m-n)];s++,o+=E,a+=I,c+=E*E,l+=I*I,A+=E*I}let d=s*A-o*a,h=s*c-o*o,u=s*l-a*a,f=h>0&&u>0?d/Math.sqrt(h*u):0;f>i&&(i=f,r=n)}return{lag:r,corr:i===-1/0?0:i}}applyVerdict(e,r,i){if(e){if(this.calmRunMs=0,this.exceedRunMs+=r,!this.inExceed&&this.exceedRunMs>=i)return this.inExceed=!0,!0}else this.exceedRunMs=0,this.inExceed&&(this.calmRunMs+=r,this.calmRunMs>=300&&(this.inExceed=!1,this.calmRunMs=0));return!1}};function fu(t){return(t%4096+4096)%4096}function oZ(t){let e=[...t].sort((i,n)=>i-n),r=e.length>>1;return e.length%2===1?e[r]:(e[r-1]+e[r])/2}function aZ(t,e,r){let i=t.byteLength>>1,n=new DataView(t.buffer,t.byteOffset,t.byteLength),s=0;for(;s<i;){let o=Math.floor((e+s/16)/20),a=(o+1)*20,c=Math.min(i,Math.ceil((a-e)*16-1e-6));c<=s&&(c=s+1);let l=0;for(let A=s;A<c;A++){let d=n.getInt16(A*2,!0)/32768;l+=d*d}r(o,Math.sqrt(l/(c-s))),s=c}}function rDe(t){let e=0;for(let r of t)e+=r*r;return Math.sqrt(e/t.length)}function nDe(t,e,r){let i=0;for(;i<t.length;){let n=Math.floor((e+i/16)/20),s=(n+1)*20,o=Math.min(t.length,Math.ceil((s-e)*16-1e-6));o<=i&&(o=i+1);let a=0;for(let c=i;c<o;c++)a+=t[c]*t[c];r(n,Math.sqrt(a/(o-i))),i=o}}function cZ(t){let e=t.byteLength>>1;if(e===0)return 0;let r=new DataView(t.buffer,t.byteOffset,t.byteLength),i=0;for(let n=0;n<e;n++){let s=r.getInt16(n*2,!0)/32768;i+=s*s}return Math.sqrt(i/e)}var iDe=5e3;function sDe(){let e=Math.round(1440),r=Math.round(16e3*.008),i=new Int16Array(e);for(let n=0;n<e;n++){let s=Math.min(1,n/r,(e-1-n)/r);i[n]=Math.round(Math.sin(2*Math.PI*880*n/16e3)*.1*32767*s)}return new Uint8Array(i.buffer)}var lZ=sDe(),oDe=2e3,uZ=32;function xo(t){return t<1e3?`${Math.round(t)}ms`:`${(t/1e3).toFixed(1)}s`}var kh="\x1B[2K\r",aDe=new Intl.Segmenter(void 0,{granularity:"grapheme"}),wI=class{session;capture;playback;io;terminalWidth;ptt;halfDuplex;signal;phase="listening";echo=new QI;aec;labelShown=!1;partialShown=!1;statusShown=!1;listenEpoch=0;asideActive=!1;voiceDownStatus=null;micLost=!1;micReconnectTimer=null;replyArmed=!1;delivered=!1;commandSeen=!1;responseDoneSeen=!1;commandsOpen=0;turnToolDisplays=new Map;jobsView=new Map;jobsTicker=null;livenessTimer=null;turnStats={};replyStartFeed=null;talking;currentTurnId;cleanedUp=!1;onAbort=()=>{this.session.close()};constructor(e){this.session=e.session,this.capture=e.capture,this.playback=e.playback,this.io=e.io??dt,this.terminalWidth=e.terminalWidth??(()=>process.stdout.columns),this.ptt=e.pushToTalk,this.halfDuplex=e.options?.halfDuplex??!1,this.signal=e.signal,this.aec=this.bargeInCapable?e.echoCanceller??null:null,this.talking=this.ptt===void 0}async run(){if(this.signal){if(this.signal.aborted){await this.cleanup();return}this.signal.addEventListener("abort",this.onAbort,{once:!0})}this.playback.setPlayoutTap((e,r)=>{if(e===lZ){this.aec?.ingestReference(e,r);return}this.replyArmed=!0,this.markDelivered(),this.turnStats.audioAdmittedMs=(this.turnStats.audioAdmittedMs??0)+e.byteLength/uZ,this.bargeInCapable&&(this.echo.ingestReference(e,r),this.aec?.ingestReference(e,r))});try{await this.capture.start(this.captureHandlers())}catch(e){throw await this.cleanup(),new ds(e instanceof Error?e.message:String(e))}this.ptt?.listen({onStart:()=>{this.talking=!0},onEnd:()=>this.commitPushToTalkTurn()});try{for await(let e of this.session.events())this.handle(e)}finally{await this.cleanup()}}get bargeInCapable(){return!this.halfDuplex&&this.ptt===void 0}handleFrame(e,r){if(this.phase!=="listening"){this.bargeInCapable&&this.echo.feedMic(e,r,this.aec?.process(e,r))&&this.replyArmed&&this.triggerBargeIn("exceedance");return}this.bargeInCapable&&this.echo.observeQuietFrame(e),this.aec?.process(e,r),this.talking&&this.sendFrame(e,r)}async sendFrame(e,r){try{await this.session.send({type:"input.audio.frame",sessionId:this.session.sessionId,commandId:bI(),atMs:r,audio:e})}catch(i){if(i instanceof br&&i.reason==="closed"){this.capture.stop();return}ie.debug("dropped an audio frame",{error:i})}}handleSpeechStart(){this.phase==="responding"&&this.bargeInCapable&&this.echo.shouldFireOnSpeechOnset()&&this.triggerBargeIn("onset")}triggerBargeIn(e){let r=this.currentTurnId,i=this.playedMs();this.playback.flush(),this.aec?.dropQueuedReference(),this.closeOpenLine();let n=!this.responseDoneSeen&&[...this.jobsView.values()].some(s=>s.turnId!==void 0&&s.turnId===r);this.printTurnSummary(this.commandsOpen>0&&!this.responseDoneSeen||n?"detached":i>0||this.commandSeen?"barged":"superseded",{playedMs:i}),this.printEchoSummary(this.echo.replyEnded(e)),this.toListening(),this.cancelTurn(r,"barge_in").then(()=>this.reportPlayout(r,i,"cut"))}drainedMs(){return Math.min(this.turnStats.audioReceivedMs??0,this.turnStats.audioAdmittedMs??0)}playedMs(){return Math.max(0,(this.turnStats.audioAdmittedMs??0)-this.playback.remainingMs())}async reportPlayout(e,r,i){if(e!==void 0)try{await this.session.send({type:"turn.playout",sessionId:this.session.sessionId,commandId:bI(),atMs:Date.now(),turnId:e,playedMs:Math.round(r),end:i})}catch{}}async cancelTurn(e,r){try{await this.session.send({type:"turn.cancel",sessionId:this.session.sessionId,commandId:bI(),atMs:Date.now(),reason:r,...e!==void 0&&{turnId:e}})}catch{}}commitPushToTalkTurn(){this.talking=!1,this.endAudioInput()}async endAudioInput(){try{await this.session.send({type:"input.audio.end",sessionId:this.session.sessionId,commandId:bI(),atMs:Date.now()})}catch{}}captureHandlers(){return{onFrame:(e,r)=>this.handleFrame(e,r),onSpeechStart:()=>this.handleSpeechStart(),onError:e=>this.handleCaptureError(e)}}handleCaptureError(e){if(this.cleanedUp||this.micLost)return;this.micLost=!0,ie.debug("the capture stream failed mid-session",{error:e}),this.capture.stop().catch(()=>{}),this.closeOpenLine(),this.io.out(z.fail("the microphone stopped - watching for it to return. press ctrl-c to exit.")+`
|
|
579
|
-
`);let r=this.currentTurnId!==void 0&&this.turnStats.finalAtMs!==void 0;if(this.phase==="listening"&&!r){let i=this.currentTurnId;this.toListening(),this.cancelTurn(i,"user_cancel")}else this.renderStatus(this.
|
|
580
|
-
`),this.phase==="listening"?this.renderStatus("listening"):this.renderStatus(this.
|
|
578
|
+
`),t.runText();throw r}}import{randomUUID as bI}from"node:crypto";function sR({onlyFirst:t=!1}={}){let n="(?:\\u001B\\][\\s\\S]*?(?:\\u0007|\\u001B\\u005C|\\u009C))|[\\u001B\\u009B][[\\]()#;?]*(?:\\d{1,4}(?:[;:]\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]";return new RegExp(n,t?void 0:"g")}var USe=sR();function oR(t){if(typeof t!="string")throw new TypeError(`Expected a \`string\`, got \`${typeof t}\``);return!t.includes("\x1B")&&!t.includes("\x9B")?t:t.replace(USe,"")}var q8=[161,161,164,164,167,168,170,170,173,174,176,180,182,186,188,191,198,198,208,208,215,216,222,225,230,230,232,234,236,237,240,240,242,243,247,250,252,252,254,254,257,257,273,273,275,275,283,283,294,295,299,299,305,307,312,312,319,322,324,324,328,331,333,333,338,339,358,359,363,363,462,462,464,464,466,466,468,468,470,470,472,472,474,474,476,476,593,593,609,609,708,708,711,711,713,715,717,717,720,720,728,731,733,733,735,735,768,879,913,929,931,937,945,961,963,969,1025,1025,1040,1103,1105,1105,8208,8208,8211,8214,8216,8217,8220,8221,8224,8226,8228,8231,8240,8240,8242,8243,8245,8245,8251,8251,8254,8254,8308,8308,8319,8319,8321,8324,8364,8364,8451,8451,8453,8453,8457,8457,8467,8467,8470,8470,8481,8482,8486,8486,8491,8491,8531,8532,8539,8542,8544,8555,8560,8569,8585,8585,8592,8601,8632,8633,8658,8658,8660,8660,8679,8679,8704,8704,8706,8707,8711,8712,8715,8715,8719,8719,8721,8721,8725,8725,8730,8730,8733,8736,8739,8739,8741,8741,8743,8748,8750,8750,8756,8759,8764,8765,8776,8776,8780,8780,8786,8786,8800,8801,8804,8807,8810,8811,8814,8815,8834,8835,8838,8839,8853,8853,8857,8857,8869,8869,8895,8895,8978,8978,9312,9449,9451,9547,9552,9587,9600,9615,9618,9621,9632,9633,9635,9641,9650,9651,9654,9655,9660,9661,9664,9665,9670,9672,9675,9675,9678,9681,9698,9701,9711,9711,9733,9734,9737,9737,9742,9743,9756,9756,9758,9758,9792,9792,9794,9794,9824,9825,9827,9829,9831,9834,9836,9837,9839,9839,9886,9887,9919,9919,9926,9933,9935,9939,9941,9953,9955,9955,9960,9961,9963,9969,9972,9972,9974,9977,9979,9980,9982,9983,10045,10045,10102,10111,11094,11097,12872,12879,57344,63743,65024,65039,65533,65533,127232,127242,127248,127277,127280,127337,127344,127373,127375,127376,127387,127404,917760,917999,983040,1048573,1048576,1114109],z8=12288,j8=65510,W8=[12288,12288,65281,65376,65504,65510];var J8=4352,Z8=262141,aR=[4352,4447,8986,8987,9001,9002,9193,9196,9200,9200,9203,9203,9725,9726,9748,9749,9776,9783,9800,9811,9855,9855,9866,9871,9875,9875,9889,9889,9898,9899,9917,9918,9924,9925,9934,9934,9940,9940,9962,9962,9970,9971,9973,9973,9978,9978,9981,9981,9989,9989,9994,9995,10024,10024,10060,10060,10062,10062,10067,10069,10071,10071,10133,10135,10160,10160,10175,10175,11035,11036,11088,11088,11093,11093,11904,11929,11931,12019,12032,12245,12272,12287,12289,12350,12353,12438,12441,12543,12549,12591,12593,12686,12688,12773,12783,12830,12832,12871,12880,42124,42128,42182,43360,43388,44032,55203,63744,64255,65040,65049,65072,65106,65108,65126,65128,65131,94176,94180,94192,94198,94208,101589,101631,101662,101760,101874,110576,110579,110581,110587,110589,110590,110592,110882,110898,110898,110928,110930,110933,110933,110948,110951,110960,111355,119552,119638,119648,119670,126980,126980,127183,127183,127374,127374,127377,127386,127488,127490,127504,127547,127552,127560,127568,127569,127584,127589,127744,127776,127789,127797,127799,127868,127870,127891,127904,127946,127951,127955,127968,127984,127988,127988,127992,128062,128064,128064,128066,128252,128255,128317,128331,128334,128336,128359,128378,128378,128405,128406,128420,128420,128507,128591,128640,128709,128716,128716,128720,128722,128725,128728,128732,128735,128747,128748,128756,128764,128992,129003,129008,129008,129292,129338,129340,129349,129351,129535,129648,129660,129664,129674,129678,129734,129736,129736,129741,129756,129759,129770,129775,129784,131072,196605,196608,262141];var BI=(t,e)=>{let r=0,i=Math.floor(t.length/2)-1;for(;r<=i;){let n=Math.floor((r+i)/2),s=n*2;if(e<t[s])i=n-1;else if(e>t[s+1])r=n+1;else return!0}return!1};var X8=19968,[PSe,HSe]=VSe(aR);function VSe(t){let e=t[0],r=t[1];for(let i=0;i<t.length;i+=2){let n=t[i],s=t[i+1];if(X8>=n&&X8<=s)return[n,s];s-n>r-e&&(e=n,r=s)}return[e,r]}var K8=t=>t<161||t>1114109?!1:BI(q8,t),eZ=t=>t<z8||t>j8?!1:BI(W8,t);var tZ=t=>t>=PSe&&t<=HSe?!0:t<J8||t>Z8?!1:BI(aR,t);function GSe(t){if(!Number.isSafeInteger(t))throw new TypeError(`Expected a code point, got \`${typeof t}\`.`)}function _h(t,{ambiguousAsWide:e=!1}={}){return GSe(t),eZ(t)||tZ(t)||e&&K8(t)?2:1}var YSe=new Intl.Segmenter,rZ=/^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Format}|\p{Mark}|\p{Surrogate})+$/v,qSe=/^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Mark}\p{Surrogate}]+/v,zSe=/^\p{RGI_Emoji}$/v,jSe=/^[\d#*]\u20E3$/,WSe=/\p{Extended_Pictographic}/gu;function JSe(t){if(t.length>50)return!1;if(jSe.test(t))return!0;if(t.includes("\u200D")){let e=t.match(WSe);return e!==null&&e.length>=2}return!1}function ZSe(t){return t.replace(qSe,"")}function XSe(t){return rZ.test(t)}function nZ(t){return t>=4352&&t<=4447||t>=43360&&t<=43388}function iZ(t){return t>=4448&&t<=4519||t>=55216&&t<=55238}function sZ(t){return t>=4520&&t<=4607||t>=55243&&t<=55291}function KSe(t){return nZ(t)||iZ(t)||sZ(t)}function eDe(t,e){let r=[];for(let n of t)rZ.test(n)||r.push(n.codePointAt(0));if(r.length===0)return;let i=0;for(let n=0;n<r.length;n++){let s=r[n];if(!KSe(s)){if(i===0)return;for(let o=n;o<r.length;o++)i+=_h(r[o],e);return i}if(nZ(s)&&iZ(r[n+1])){i+=2,n+=sZ(r[n+2])?2:1;continue}i+=_h(s,e)}return i}function tDe(t,e){let r=0,i=!0;for(let n of t){if(i){i=!1;continue}n>="\uFF00"&&n<="\uFFEF"&&(r+=_h(n.codePointAt(0),e))}return r}function xI(t,e={}){if(typeof t!="string"||t.length===0)return 0;let{ambiguousIsNarrow:r=!0,countAnsiEscapeCodes:i=!1}=e,n=t;if(!i&&(n.includes("\x1B")||n.includes("\x9B"))&&(n=oR(n)),n.length===0)return 0;if(/^[\u0020-\u007E]*$/.test(n))return n.length;let s=0,o={ambiguousAsWide:!r};for(let{segment:a}of YSe.segment(n)){if(XSe(a))continue;if(zSe.test(a)||JSe(a)){s+=2;continue}let c=ZSe(a),l=eDe(c,o);if(l!==void 0){s+=l;continue}let A=c.codePointAt(0);s+=_h(A,o),s+=tDe(c,o)}return s}hA();Tt();yI();var QI=class{now;refRing=new Float32Array(4096);sessionGain=2;sessionDelayMs;sessionMargin=2;gain=2;delayMs;corr;margin=2;frozen=!1;micHops=[];refObserved=!1;inExceed=!1;exceedRunMs=0;calmRunMs=0;lastRefResumeAtMs;prevNarrowActive=!1;narrowQuietFrames=0;closest;closestOffsetMs;replyMicStartMs;windowExceedMs=0;ratioP10;ratioP90;onsetVetoes=0;onsetVetoesInGap=0;firedRead;suppTrend=[];residRatios=[];suppressedRunFrames=0;sessionProven=!1;quietFloorRms=[];clampAdmitted=!0;floorEverWarm=!1;residualSeen=!1;constructor(e={}){this.now=e.now??Date.now}beginReply(){this.refRing.fill(0),this.micHops.length=0,this.frozen=!1,this.gain=this.sessionGain,this.delayMs=this.sessionDelayMs,this.margin=this.sessionMargin,this.corr=void 0,this.refObserved=!1,this.inExceed=!1,this.exceedRunMs=0,this.calmRunMs=0,this.lastRefResumeAtMs=void 0,this.prevNarrowActive=!1,this.narrowQuietFrames=0,this.closest=void 0,this.closestOffsetMs=void 0,this.replyMicStartMs=void 0,this.windowExceedMs=0,this.ratioP10=void 0,this.ratioP90=void 0,this.onsetVetoes=0,this.onsetVetoesInGap=0,this.firedRead=void 0,this.suppTrend.length=0,this.residualSeen=!1,this.suppressedRunFrames=0}ingestReference(e,r){e.byteLength<2||(this.refObserved=!0,aZ(e,r,(i,n)=>{let s=fu(i);n>this.refRing[s]&&(this.refRing[s]=n)}))}feedMic(e,r,i){let n=e.byteLength>>1;if(n===0)return!1;let s=n/16,o=r-s;this.replyMicStartMs??=o;let a=!this.frozen;!this.frozen&&this.refObserved&&(this.micHops.length<200&&aZ(e,o,(v,S)=>{this.micHops.length<200&&this.micHops.push({slot:v,rms:S})}),this.micHops.length>=80&&this.tryFreeze());let c=this.alignedRefMax(o,r),l=i??null,A=l!==null&&l.length>0?rDe(l):null;A!==null&&(this.residualSeen=!0);let d=A!==null&&this.residualConverged,h=cZ(e),u=d&&A!==null?A:h,f=this.gain*c*this.margin,m=d?.01:Math.max(.01,f),E=this.delayMs===void 0?c>=.005:this.narrowAlignedMax(o,r,this.delayMs)>=.005;E?(!this.prevNarrowActive&&this.narrowQuietFrames>=2&&(this.lastRefResumeAtMs=r),this.narrowQuietFrames=0):this.narrowQuietFrames++,this.prevNarrowActive=E;let I=d&&this.lastRefResumeAtMs!==void 0&&r-this.lastRefResumeAtMs<700,C=this.exceedanceArmed&&f>.01&&u>m&&!I;if(c<.005&&!C&&!this.inExceed&&(this.quietFloorRms.push(h),this.quietFloorRms.length>150&&this.quietFloorRms.shift()),this.updateClampAdmission(),l!==null&&A!==null&&!C&&!this.inExceed){if(this.frozen&&this.delayMs!==void 0){let v=Math.round(this.delayMs/20);nDe(l,o,(S,O)=>{let U=this.refRing[fu(S-v)];U<.005||(this.residRatios.push(O/U),this.residRatios.length>120&&this.residRatios.shift())})}if(c>=.005&&h>.02){let v=A/h<=.25&&A<=.01;this.suppressedRunFrames=v?this.suppressedRunFrames+1:0,this.suppressedRunFrames>=40&&(this.sessionProven=!0),this.suppTrend.length<2048&&this.replyMicStartMs!==void 0&&this.suppTrend.push({offsetMs:r-this.replyMicStartMs,ratio:A/h})}}if(this.exceedanceArmed&&f>.01){let v=u/m;(this.closest===void 0||v>this.closest)&&(this.closest=v,this.closestOffsetMs=r-this.replyMicStartMs),a&&C&&(this.windowExceedMs+=s)}let b=this.applyVerdict(C,s,d?150:64);return b&&(this.firedRead=d?"clamped":"raw"),b}get exceedanceArmed(){return this.refObserved&&this.gain>.02}observeQuietFrame(e){e.byteLength>>1!==0&&(this.quietFloorRms.push(cZ(e)),this.quietFloorRms.length>150&&this.quietFloorRms.shift())}shouldFireOnSpeechOnset(){if(!this.exceedanceArmed)return!0;let e=this.now(),r=this.alignedRefMax(e,e),i=this.gain*r*this.margin<=.01;return i||(this.onsetVetoes++,r<.005&&this.onsetVetoesInGap++),i}replyEnded(e){!this.frozen&&this.micHops.length>0&&this.tryFreeze();let r={refObserved:this.refObserved,gain:this.gain,gainSource:this.frozen?"learned":"seed",margin:this.margin,delayMs:this.delayMs,corr:this.corr,closest:this.closest,closestOffsetMs:this.closestOffsetMs,windowExceedMs:this.windowExceedMs,ratioP10:this.ratioP10,ratioP90:this.ratioP90,quietFloor:this.quietFloorEstimate(),onsetVetoes:this.onsetVetoes,onsetVetoesInGap:this.onsetVetoesInGap,fired:e,firedRead:e==="exceedance"?this.firedRead:void 0,residTrend:this.computeResidTrend(),aec:this.residualSeen?{converged:this.residualConverged,residualGain:this.residualGain()}:void 0};return this.residualSeen&&e==="exceedance"&&(this.residRatios.length=0,this.suppressedRunFrames=0,this.sessionProven=!1),r}computeResidTrend(){if(this.suppTrend.length<8)return;let e=this.suppTrend[this.suppTrend.length-1].offsetMs;if(e<=0)return;let r=[[],[],[],[]];for(let{offsetMs:n,ratio:s}of this.suppTrend)r[Math.min(3,Math.max(0,Math.floor(n/e*4)))].push(s);let i=n=>n.length>0?oZ(n):void 0;return[i(r[0]),i(r[1]),i(r[2]),i(r[3])]}residualGain(){return this.residRatios.length<50?this.gain:Math.min(4,oZ(this.residRatios))}get residualConverged(){return this.quietFloorAllowsClamp&&this.sessionProven&&this.suppressedRunFrames>=10&&this.residRatios.length>=50&&this.residualGain()<=this.gain/4}get quietFloorAllowsClamp(){return this.clampAdmitted}updateClampAdmission(){let e=this.quietFloorEstimate();e!==void 0&&(e<=.008?this.clampAdmitted=!0:(e>.01||!this.floorEverWarm)&&(this.clampAdmitted=!1),this.floorEverWarm=!0)}quietFloorEstimate(){if(this.quietFloorRms.length<20)return;let e=[...this.quietFloorRms].sort((r,i)=>r-i);return e[Math.floor(.1*(e.length-1))]}alignedRefMax(e,r){let i=this.frozen?this.delayMs:void 0,n=i===void 0?e-700-300:e-i-300,s=i===void 0?r+300:r-i+300,o=0;for(let a=Math.floor(n/20);a<=Math.floor(s/20);a++){let c=this.refRing[fu(a)];c>o&&(o=c)}return o}narrowAlignedMax(e,r,i){let n=0,s=Math.floor((e-i)/20),o=Math.floor((r-i)/20);for(let a=s;a<=o;a++){let c=this.refRing[fu(a)];c>n&&(n=c)}return n}tryFreeze(){let{lag:e,corr:r}=this.bestLag(),i=[];for(let{slot:a,rms:c}of this.micHops){let l=this.refRing[fu(a-e)];l>=.005&&i.push(c/l)}if(i.length<25)return;i.sort((a,c)=>a-c);let n=i.length>>1,s=i.length%2===1?i[n]:(i[n-1]+i[n])/2;this.gain=Math.min(4,s),this.delayMs=e*20,this.corr=r,this.ratioP10=i[Math.floor(.1*(i.length-1))],this.ratioP90=i[Math.floor(.9*(i.length-1))];let o=s>0?this.ratioP90/s:1;this.margin=Math.min(6,Math.max(2,1.25*o)),this.frozen=!0,this.sessionGain=this.gain,this.sessionDelayMs=this.delayMs,this.sessionMargin=this.margin}bestLag(){let e=Math.floor(35),r=0,i=-1/0;for(let n=0;n<=e;n++){let s=0,o=0,a=0,c=0,l=0,A=0;for(let{slot:m,rms:E}of this.micHops){let I=this.refRing[fu(m-n)];s++,o+=E,a+=I,c+=E*E,l+=I*I,A+=E*I}let d=s*A-o*a,h=s*c-o*o,u=s*l-a*a,f=h>0&&u>0?d/Math.sqrt(h*u):0;f>i&&(i=f,r=n)}return{lag:r,corr:i===-1/0?0:i}}applyVerdict(e,r,i){if(e){if(this.calmRunMs=0,this.exceedRunMs+=r,!this.inExceed&&this.exceedRunMs>=i)return this.inExceed=!0,!0}else this.exceedRunMs=0,this.inExceed&&(this.calmRunMs+=r,this.calmRunMs>=300&&(this.inExceed=!1,this.calmRunMs=0));return!1}};function fu(t){return(t%4096+4096)%4096}function oZ(t){let e=[...t].sort((i,n)=>i-n),r=e.length>>1;return e.length%2===1?e[r]:(e[r-1]+e[r])/2}function aZ(t,e,r){let i=t.byteLength>>1,n=new DataView(t.buffer,t.byteOffset,t.byteLength),s=0;for(;s<i;){let o=Math.floor((e+s/16)/20),a=(o+1)*20,c=Math.min(i,Math.ceil((a-e)*16-1e-6));c<=s&&(c=s+1);let l=0;for(let A=s;A<c;A++){let d=n.getInt16(A*2,!0)/32768;l+=d*d}r(o,Math.sqrt(l/(c-s))),s=c}}function rDe(t){let e=0;for(let r of t)e+=r*r;return Math.sqrt(e/t.length)}function nDe(t,e,r){let i=0;for(;i<t.length;){let n=Math.floor((e+i/16)/20),s=(n+1)*20,o=Math.min(t.length,Math.ceil((s-e)*16-1e-6));o<=i&&(o=i+1);let a=0;for(let c=i;c<o;c++)a+=t[c]*t[c];r(n,Math.sqrt(a/(o-i))),i=o}}function cZ(t){let e=t.byteLength>>1;if(e===0)return 0;let r=new DataView(t.buffer,t.byteOffset,t.byteLength),i=0;for(let n=0;n<e;n++){let s=r.getInt16(n*2,!0)/32768;i+=s*s}return Math.sqrt(i/e)}var iDe=5e3;function sDe(){let e=Math.round(1440),r=Math.round(16e3*.008),i=new Int16Array(e);for(let n=0;n<e;n++){let s=Math.min(1,n/r,(e-1-n)/r);i[n]=Math.round(Math.sin(2*Math.PI*880*n/16e3)*.1*32767*s)}return new Uint8Array(i.buffer)}var lZ=sDe(),oDe=2e3,uZ=32;function xo(t){return t<1e3?`${Math.round(t)}ms`:`${(t/1e3).toFixed(1)}s`}var kh="\x1B[2K\r",aDe=new Intl.Segmenter(void 0,{granularity:"grapheme"}),wI=class{session;capture;playback;io;terminalWidth;ptt;halfDuplex;signal;phase="listening";echo=new QI;aec;labelShown=!1;partialShown=!1;statusShown=!1;lastResponseStatus;listenEpoch=0;asideActive=!1;voiceDownStatus=null;micLost=!1;micReconnectTimer=null;replyArmed=!1;delivered=!1;commandSeen=!1;responseDoneSeen=!1;commandsOpen=0;turnToolDisplays=new Map;jobsView=new Map;jobsTicker=null;livenessTimer=null;turnStats={};replyStartFeed=null;talking;currentTurnId;cleanedUp=!1;onAbort=()=>{this.session.close()};constructor(e){this.session=e.session,this.capture=e.capture,this.playback=e.playback,this.io=e.io??dt,this.terminalWidth=e.terminalWidth??(()=>process.stdout.columns),this.ptt=e.pushToTalk,this.halfDuplex=e.options?.halfDuplex??!1,this.signal=e.signal,this.aec=this.bargeInCapable?e.echoCanceller??null:null,this.talking=this.ptt===void 0}async run(){if(this.signal){if(this.signal.aborted){await this.cleanup();return}this.signal.addEventListener("abort",this.onAbort,{once:!0})}this.playback.setPlayoutTap((e,r)=>{if(e===lZ){this.aec?.ingestReference(e,r);return}this.replyArmed=!0,this.markDelivered(),this.turnStats.audioAdmittedMs=(this.turnStats.audioAdmittedMs??0)+e.byteLength/uZ,this.bargeInCapable&&(this.echo.ingestReference(e,r),this.aec?.ingestReference(e,r))});try{await this.capture.start(this.captureHandlers())}catch(e){throw await this.cleanup(),new ds(e instanceof Error?e.message:String(e))}this.ptt?.listen({onStart:()=>{this.talking=!0},onEnd:()=>this.commitPushToTalkTurn()});try{for await(let e of this.session.events())this.handle(e)}finally{await this.cleanup()}}get bargeInCapable(){return!this.halfDuplex&&this.ptt===void 0}handleFrame(e,r){if(this.phase!=="listening"){this.bargeInCapable&&this.echo.feedMic(e,r,this.aec?.process(e,r))&&this.replyArmed&&this.triggerBargeIn("exceedance");return}this.bargeInCapable&&this.echo.observeQuietFrame(e),this.aec?.process(e,r),this.talking&&this.sendFrame(e,r)}async sendFrame(e,r){try{await this.session.send({type:"input.audio.frame",sessionId:this.session.sessionId,commandId:bI(),atMs:r,audio:e})}catch(i){if(i instanceof br&&i.reason==="closed"){this.capture.stop();return}ie.debug("dropped an audio frame",{error:i})}}handleSpeechStart(){this.phase==="responding"&&this.bargeInCapable&&this.echo.shouldFireOnSpeechOnset()&&this.triggerBargeIn("onset")}triggerBargeIn(e){let r=this.currentTurnId,i=this.playedMs();this.playback.flush(),this.aec?.dropQueuedReference(),this.closeOpenLine();let n=!this.responseDoneSeen&&[...this.jobsView.values()].some(s=>s.turnId!==void 0&&s.turnId===r);this.printTurnSummary(this.commandsOpen>0&&!this.responseDoneSeen||n?"detached":i>0||this.commandSeen?"barged":"superseded",{playedMs:i}),this.printEchoSummary(this.echo.replyEnded(e)),this.toListening(),this.cancelTurn(r,"barge_in").then(()=>this.reportPlayout(r,i,"cut"))}drainedMs(){return Math.min(this.turnStats.audioReceivedMs??0,this.turnStats.audioAdmittedMs??0)}playedMs(){return Math.max(0,(this.turnStats.audioAdmittedMs??0)-this.playback.remainingMs())}async reportPlayout(e,r,i){if(e!==void 0)try{await this.session.send({type:"turn.playout",sessionId:this.session.sessionId,commandId:bI(),atMs:Date.now(),turnId:e,playedMs:Math.round(r),end:i})}catch{}}async cancelTurn(e,r){try{await this.session.send({type:"turn.cancel",sessionId:this.session.sessionId,commandId:bI(),atMs:Date.now(),reason:r,...e!==void 0&&{turnId:e}})}catch{}}commitPushToTalkTurn(){this.talking=!1,this.endAudioInput()}async endAudioInput(){try{await this.session.send({type:"input.audio.end",sessionId:this.session.sessionId,commandId:bI(),atMs:Date.now()})}catch{}}captureHandlers(){return{onFrame:(e,r)=>this.handleFrame(e,r),onSpeechStart:()=>this.handleSpeechStart(),onError:e=>this.handleCaptureError(e)}}handleCaptureError(e){if(this.cleanedUp||this.micLost)return;this.micLost=!0,ie.debug("the capture stream failed mid-session",{error:e}),this.capture.stop().catch(()=>{}),this.closeOpenLine(),this.io.out(z.fail("the microphone stopped - watching for it to return. press ctrl-c to exit.")+`
|
|
579
|
+
`);let r=this.currentTurnId!==void 0&&this.turnStats.finalAtMs!==void 0;if(this.phase==="listening"&&!r){let i=this.currentTurnId;this.toListening(),this.cancelTurn(i,"user_cancel")}else this.renderStatus(this.lastResponseStatus??"thinking");this.armMicReconnect()}armMicReconnect(){this.cleanedUp||this.micReconnectTimer!==null||(this.micReconnectTimer=setTimeout(()=>{this.micReconnectTimer=null,this.tryMicReconnect()},oDe))}async tryMicReconnect(){if(!(this.cleanedUp||!this.micLost)){try{await this.capture.start(this.captureHandlers())}catch{this.armMicReconnect();return}if(this.cleanedUp){this.capture.stop().catch(()=>{});return}this.micLost=!1,this.closeOpenLine(),this.io.out(z.dim("the microphone is back.")+`
|
|
580
|
+
`),this.phase==="listening"?this.renderStatus("listening"):this.renderStatus(this.lastResponseStatus??"thinking")}}clearMicReconnect(){this.micReconnectTimer!==null&&(clearTimeout(this.micReconnectTimer),this.micReconnectTimer=null)}handle(e){switch(e.type){case"session.ready":this.greet();return;case"turn.accepted":this.currentTurnId=e.turnId,this.labelShown=!1,this.turnStats={},this.turnToolDisplays.clear();return;case"transcript.partial":if(e.turnId!==this.currentTurnId)return;this.renderPartial(e.text);return;case"turn.final":if(e.turn.turnId!==this.currentTurnId)return;if(e.turn.disposition==="absorbed"){this.renderAside(e.turn),this.toListening();return}this.renderFinalUser(e.turn),this.turnStats.finalAtMs=e.atMs,this.renderStatus("thinking"),this.armLiveness();return;case"state":if(e.state==="responding"&&e.turnId!==void 0&&e.turnId!==this.currentTurnId)return;e.state==="responding"&&(this.phase="responding",this.replyArmed=!1,this.delivered=!1,this.commandSeen=!1,this.commandsOpen=0,this.responseDoneSeen=!1,this.asideActive=!1,this.bargeInCapable&&(this.echo.beginReply(),this.replyStartFeed=this.aec?.diagnostics()??null,this.aec?.ensureStarted())),e.state==="aside"&&(this.asideActive=!0,this.phase==="listening"&&this.renderStatus("listening"));return;case"response.delta":if(this.phase!=="responding")return;this.replyArmed=!0,this.markDelivered(),this.turnStats.firstTokenAtMs??=e.atMs,this.showLabel(),this.io.out(e.text);return;case"response.audio":if(this.phase!=="responding")return;this.replyArmed=!0,this.markDelivered(),this.turnStats.audioReceivedMs=(this.turnStats.audioReceivedMs??0)+e.audio.byteLength/uZ,this.turnStats.firstAudioAtMs===void 0?(this.turnStats.firstAudioAtMs=e.atMs,this.turnStats.playbackStartAtMs=Date.now(),this.renderStatus("speaking")):this.lastResponseStatus!=="speaking"&&this.commandsOpen===0&&this.renderStatus("speaking"),this.playback.play(e.audio);return;case"act.start":if(this.phase!=="responding")return;this.markDelivered(),e.act==="no_response"&&this.renderStatus("quiet");return;case"tool.start":if(this.phase!=="responding")return;this.replyArmed=!0,this.markDelivered(),this.commandSeen=!0,this.commandsOpen+=1,this.turnToolDisplays.set(e.toolId,e.summary),this.closeOpenLine(),this.io.out(z.dim(` ${II(e.name,e.summary)}`)+`
|
|
581
581
|
`),this.renderStatus("running");return;case"tool.result":if(this.phase!=="responding")return;this.commandsOpen=Math.max(0,this.commandsOpen-1),this.markDelivered(),this.closeOpenLine(),this.io.out(z.dim(` ${e.summary}`)+`
|
|
582
582
|
`),this.renderStatus("thinking");return;case"response.done":if(this.phase!=="responding")return;if(!this.delivered){this.playback.flush(),this.aec?.dropQueuedReference(),this.showLabel(),this.io.out(z.dim("(no reply came through)")+`
|
|
583
583
|
`),this.labelShown=!1,this.printTurnSummary("lost"),this.reportPlayout(this.currentTurnId,0,"drained"),this.toListening();return}this.responseDoneSeen=!0,this.closeOpenLine(),this.turnStats.playbackStartAtMs!==void 0&&this.renderStatus("speaking"),this.awaitPlaybackDrain();return;case"error":{let r=this.currentTurnId,i=this.phase==="responding",n=this.playedMs();this.playback.flush(),this.aec?.dropQueuedReference(),this.closeOpenLine(),this.renderError(e),i&&this.printTurnSummary("failed",{reason:e.reason}),this.toListening(),this.cancelTurn(r,"user_cancel").then(()=>i?this.reportPlayout(r,n,"cut"):void 0);return}case"act.end":if(this.phase!=="responding")return;this.markDelivered();return;case"job.detached":{let r=this.turnToolDisplays.get(e.jobId)??e.tool;this.jobsView.set(e.jobId,{display:r,startedAtMs:Date.now(),...e.turnId!==void 0&&{turnId:e.turnId}}),e.turnId===this.currentTurnId&&(this.commandsOpen=Math.max(0,this.commandsOpen-1)),this.ensureJobsTicker(),this.closeOpenLine(),this.io.out(z.dim(" running in the background")+`
|
|
584
|
-
`),this.renderStatus(this.phase==="responding"?"thinking":"listening");return}case"job.result":{let r=this.jobsView.get(e.jobId);this.jobsView.delete(e.jobId),this.jobsView.size===0&&this.clearJobsTicker();let i=r?.display??e.tool,n=e.outcome==="ok"?`ok in ${xo(e.durationMs)}`:e.outcome==="failed"?`failed after ${xo(e.durationMs)}`:`cancelled after ${xo(e.durationMs)}`;this.closeOpenLine(),this.io.out(z.dim(` ${i} ${n}`)+`
|
|
585
|
-
`),this.renderStatus(this.phase==="responding"?
|
|
584
|
+
`),this.renderStatus(this.phase==="responding"?this.lastResponseStatus??"thinking":"listening");return}case"job.result":{let r=this.jobsView.get(e.jobId);this.jobsView.delete(e.jobId),this.jobsView.size===0&&this.clearJobsTicker();let i=r?.display??e.tool,n=e.outcome==="ok"?`ok in ${xo(e.durationMs)}`:e.outcome==="failed"?`failed after ${xo(e.durationMs)}`:`cancelled after ${xo(e.durationMs)}`;this.closeOpenLine(),this.io.out(z.dim(` ${i} ${n}`)+`
|
|
585
|
+
`);let s=this.lastResponseStatus??"thinking",o=s==="running"&&this.commandsOpen===0?"thinking":s;this.renderStatus(this.phase==="responding"?o:"listening");return}default:return}}greet(){let e=this.ptt?"jarvis is ready (push-to-talk). press space to talk, again to send.":"jarvis is listening. just speak.",r=this.halfDuplex?" half-duplex: barge-in is off.":"";this.io.out(z.dim(`${e}${r} press ctrl-c to exit.`)+`
|
|
586
586
|
`),this.renderStatus("listening")}renderPartial(e){this.io.out(`${kh}you ${z.dim(this.clampPartialToOneRow(e))}`),this.partialShown=!0,this.statusShown=!1}clampPartialToOneRow(e){let r=this.visibleColumns()-5;if(xI(e)<=r)return e;let i=r-3;if(i<=0)return"...";let n=[...aDe.segment(e)],s=n.length,o=0;for(;s>0;){let a=o+xI(n[s-1].segment);if(a>i)break;o=a,s-=1}return s<n.length?"..."+e.slice(n[s].index):"..."}renderFinalUser(e){this.io.out(`${kh}you ${e.text}
|
|
587
587
|
`),this.partialShown=!1,this.statusShown=!1}renderAside(e){this.io.out(`${kh}${z.dim(`you (aside) ${e.text}`)}
|
|
588
|
-
`),this.partialShown=!1,this.statusShown=!1}showLabel(){this.labelShown||(this.eraseStatusLine(),this.io.out(z.dim("jarvis")+" "),this.labelShown=!0)}renderStatus(e){this.partialShown||this.labelShown
|
|
588
|
+
`),this.partialShown=!1,this.statusShown=!1}showLabel(){this.labelShown||(this.eraseStatusLine(),this.io.out(z.dim("jarvis")+" "),this.labelShown=!0)}renderStatus(e){e!=="listening"&&(this.lastResponseStatus=e),!(this.partialShown||this.labelShown)&&(this.io.out(kh+z.dim(this.statusText(e))),this.statusShown=!0)}statusText(e){if(e!=="listening")return`${e}...`;if(this.voiceDownStatus!==null)return this.voiceDownStatus;if(this.micLost){let r="mic lost - reconnecting...";return r.length<=this.visibleColumns()?r:"mic lost..."}if(this.asideActive){let r=this.ptt?"listening (aside - press to talk to resume)":'listening (aside - say "Jarvis" to resume)',i=this.visibleColumns();if(r.length<=i)return r;let n="listening (aside)";return n.length<=i?n:`${e}...`}if(this.jobsView.size>0){let r=Math.min(...[...this.jobsView.values()].map(c=>c.startedAtMs)),i=Math.max(0,Math.round((Date.now()-r)/1e3)),n=`${this.jobsView.size} job${this.jobsView.size===1?"":"s"}`,s=`listening (${n}, ${i}s)...`,o=this.visibleColumns();if(s.length<=o)return s;let a=`listening (${n})...`;return a.length<=o?a:`${e}...`}return`${e}...`}visibleColumns(){let e=this.terminalWidth();return e!==void 0&&e>0?e:80}ensureJobsTicker(){this.jobsTicker!==null||this.cleanedUp||(this.jobsTicker=setInterval(()=>{this.phase==="listening"&&this.renderStatus("listening")},5e3),this.jobsTicker.unref?.())}clearJobsTicker(){this.jobsTicker!==null&&(clearInterval(this.jobsTicker),this.jobsTicker=null)}eraseStatusLine(){this.statusShown&&(this.io.out(kh),this.statusShown=!1)}renderError(e){if(e.reason==="no_voice_route"){this.io.out(z.warn("no voice configured yet - run `jarvis init` to add speech routes.")+`
|
|
589
589
|
`);return}if(e.reason==="no_text_route"){this.io.out(z.warn("no brain configured yet - run `jarvis init` to add one.")+`
|
|
590
590
|
`);return}if(e.reason==="asr_unavailable"){this.renderAsrUnavailable(e.message);return}this.io.out(z.fail(`something went wrong - ${e.message}`)+`
|
|
591
591
|
`)}renderAsrUnavailable(e){let r=e.includes("quota_exceeded"),i=e.includes("auth_error")||e.includes("rejected credential"),n=r?"ElevenLabs reports the quota exhausted. check your plan/usage, then restart jarvis.":i?"ElevenLabs rejected the API key. run `jarvis init` to update it, then restart jarvis.":`${e}. restart jarvis once it's fixed.`;this.io.out(z.warn(`voice input is off - ${n} text chat still works.`)+`
|
|
592
592
|
`),this.voiceDownStatus=r?"voice off (quota)":i?"voice off (key)":"voice off"}closeOpenLine(){if(this.labelShown||this.partialShown){this.io.out(`
|
|
593
|
-
`),this.labelShown=!1,this.partialShown=!1;return}this.eraseStatusLine()}markDelivered(){this.delivered=!0,this.clearLiveness()}armLiveness(){this.clearLiveness(),this.livenessTimer=setTimeout(()=>this.livenessElapsed(),iDe)}clearLiveness(){this.livenessTimer!==null&&(clearTimeout(this.livenessTimer),this.livenessTimer=null)}livenessElapsed(){this.livenessTimer=null,!(this.cleanedUp||this.phase!=="responding"||this.delivered)&&(this.renderStatus("still thinking"),this.playback.play(lZ))}awaitPlaybackDrain(){let e=this.listenEpoch;this.playback.drained().then(()=>{this.cleanedUp||e!==this.listenEpoch||(this.printTurnSummary("completed"),this.bargeInCapable&&this.printEchoSummary(this.echo.replyEnded()),this.reportPlayout(this.currentTurnId,this.drainedMs(),"drained"),this.toListening())})}printTurnSummary(e,r={}){if(ie.getLevel()!=="debug")return;let{finalAtMs:i,firstTokenAtMs:n,firstAudioAtMs:s,playbackStartAtMs:o,audioReceivedMs:a}=this.turnStats,c=[r.reason===void 0?e:`${e} (${r.reason})`];i!==void 0&&n!==void 0&&c.push(`commit->token ${xo(n-i)}`),n!==void 0&&s!==void 0&&c.push(`token->audio ${xo(s-n)}`),r.playedMs!==void 0&&a!==void 0?c.push(`spoke ${xo(r.playedMs)} of ${xo(a)}`):o!==void 0&&c.push(`spoke ${xo(Date.now()-o)}`),this.eraseStatusLine(),this.io.out(z.dim(`turn ${c.join(", ")}`)+`
|
|
593
|
+
`),this.labelShown=!1,this.partialShown=!1;return}this.eraseStatusLine()}markDelivered(){this.delivered=!0,this.clearLiveness(),this.lastResponseStatus==="still thinking"&&(this.lastResponseStatus="thinking")}armLiveness(){this.clearLiveness(),this.livenessTimer=setTimeout(()=>this.livenessElapsed(),iDe)}clearLiveness(){this.livenessTimer!==null&&(clearTimeout(this.livenessTimer),this.livenessTimer=null)}livenessElapsed(){this.livenessTimer=null,!(this.cleanedUp||this.phase!=="responding"||this.delivered)&&(this.renderStatus("still thinking"),this.playback.play(lZ))}awaitPlaybackDrain(){let e=this.listenEpoch;this.playback.drained().then(()=>{this.cleanedUp||e!==this.listenEpoch||(this.printTurnSummary("completed"),this.bargeInCapable&&this.printEchoSummary(this.echo.replyEnded()),this.reportPlayout(this.currentTurnId,this.drainedMs(),"drained"),this.toListening())})}printTurnSummary(e,r={}){if(ie.getLevel()!=="debug")return;let{finalAtMs:i,firstTokenAtMs:n,firstAudioAtMs:s,playbackStartAtMs:o,audioReceivedMs:a}=this.turnStats,c=[r.reason===void 0?e:`${e} (${r.reason})`];i!==void 0&&n!==void 0&&c.push(`commit->token ${xo(n-i)}`),n!==void 0&&s!==void 0&&c.push(`token->audio ${xo(s-n)}`),r.playedMs!==void 0&&a!==void 0?c.push(`spoke ${xo(r.playedMs)} of ${xo(a)}`):o!==void 0&&c.push(`spoke ${xo(Date.now()-o)}`),this.eraseStatusLine(),this.io.out(z.dim(`turn ${c.join(", ")}`)+`
|
|
594
594
|
`)}printEchoSummary(e){if(ie.getLevel()!=="debug"||!e.refObserved)return;let r=[`gain ${e.gain.toFixed(2)} ${e.gainSource}`];if(e.margin>2&&r.push(`margin x${e.margin.toFixed(1)}`),r.push(e.delayMs===void 0?"delay unlearned":`delay ${Math.round(e.delayMs)}ms`),e.corr!==void 0&&r.push(`corr ${e.corr.toFixed(2)}`),e.ratioP10!==void 0&&e.ratioP90!==void 0&&r.push(`ratio ${e.ratioP10.toFixed(2)}-${e.ratioP90.toFixed(2)}`),e.closest!==void 0&&e.closestOffsetMs!==void 0&&r.push(`closest ${e.closest.toFixed(2)}x@${(e.closestOffsetMs/1e3).toFixed(1)}s`),r.push(`window-exceed ${Math.round(e.windowExceedMs)}ms`),e.quietFloor!==void 0&&r.push(`quiet-floor ${e.quietFloor.toFixed(3)}`),r.push(`onset-veto ${e.onsetVetoes} (${e.onsetVetoesInGap} gap)`),e.aec){let s=e.aec.converged?"converged":"converging";r.push(`aec on (${s}, resid ${e.aec.residualGain.toFixed(2)})`)}else this.aec&&r.push(`aec off (${this.aec.status()})`);e.residTrend&&r.push(`sup ${e.residTrend.map(s=>s===void 0?"-":s.toFixed(2)).join("/")}`);let i=this.aec?.diagnostics();if(i){let s=`feed skew ${i.skewMs.toFixed(1)}ms`,o=this.replyStartFeed,a=o?i.sinceAnchorMs-o.sinceAnchorMs:0;if(o&&a>=15e3){let c=i.skewMs+i.slewMs-(o.skewMs+o.slewMs),l=Math.round(c/a*1e6),A=c>=0?"+":"";r.push(`${s} (${A}${c.toFixed(1)}ms this reply, ${l}ppm)`)}else r.push(s);Math.abs(i.slewMs)>=.5&&r.push(`slew ${i.slewMs.toFixed(0)}ms`),r.push(`resync ${i.resyncs}`)}let n=e.fired==="exceedance"&&e.firedRead!==void 0?`exceedance ${e.firedRead}`:e.fired;r.push(e.fired===void 0?"no barge":`barge (${n})`),this.eraseStatusLine(),this.io.out(z.dim(`echo ${r.join(", ")}`)+`
|
|
595
|
-
`)}toListening(){this.listenEpoch++,this.phase="listening",this.replyArmed=!1,this.delivered=!1,this.commandSeen=!1,this.commandsOpen=0,this.responseDoneSeen=!1,this.clearLiveness(),this.labelShown=!1,this.partialShown=!1,this.currentTurnId=void 0,this.renderStatus("listening")}async cleanup(){this.cleanedUp||(this.cleanedUp=!0,this.clearLiveness(),this.clearJobsTicker(),this.clearMicReconnect(),this.closeOpenLine(),this.signal?.removeEventListener("abort",this.onAbort),this.ptt?.close(),this.playback.setPlayoutTap(null),this.aec?.close(),await this.capture.stop().catch(()=>{}),await this.playback.close().catch(()=>{}),await this.session.close().catch(()=>{}))}};Tt();var dZ=16e3,Si=16,Qo=160,cDe=120*dZ,lDe=1e3,uDe=.1,ADe=30,dDe=16,hDe=.1,fDe=500,vI=class{load;log;blockTap;state="idle";startPromise=null;aec=null;closed=!1;refQueue=[];refQueuedSamples=0;cursorSample=null;pendingMic=new Float32Array(0);anchorSample=0;lastStampSample=0;skewEmaMs=0;resyncs=0;slewSamples=0;constructor(e={}){this.load=e.load??gDe,this.log=e.log??ie,this.blockTap=e.blockTap}ensureStarted(){return this.startPromise??=this.start(),this.startPromise}async start(){this.state="loading";try{let e=await this.load();if(this.closed)return;this.aec=new e.AEC3(dZ,1,1),this.state="active"}catch(e){this.fail(e)}}ingestReference(e,r){if(!(this.closed||this.state==="unavailable")&&!(e.byteLength<2))for(this.refQueue.push({startSample:Math.round(r*Si),samples:AZ(e)}),this.refQueuedSamples+=e.byteLength>>1;this.refQueuedSamples>cDe&&this.refQueue.length>1;)this.refQueuedSamples-=this.refQueue[0].samples.length,this.refQueue.shift()}process(e,r){if(this.closed||this.state!=="active"||this.aec===null)return null;let i=e.byteLength>>1;if(i===0)return null;let n=Math.round((r-i/Si)*Si);if(this.cursorSample===null)this.cursorSample=n,this.anchorSample=n,this.skewEmaMs=0;else{let c=this.cursorSample+this.pendingMic.length,l=(n-c)/Si;if(Math.abs(n-c)>lDe*Si)this.pendingMic=new Float32Array(0),this.cursorSample=n,this.anchorSample=n,this.skewEmaMs=0,this.slewSamples=0,this.resyncs++;else{this.skewEmaMs+=uDe*(l-this.skewEmaMs);let A=Math.abs(this.skewEmaMs)-ADe;if(A>0){let d=Math.min(dDe,Math.max(1,Math.round(A*Si*hDe))),h=this.skewEmaMs>0?d:-d;Math.abs(this.slewSamples+h)<=fDe*Si&&(this.cursorSample+=h,this.slewSamples+=h,this.skewEmaMs-=h/Si)}}}this.lastStampSample=n+i;let s=pDe(this.pendingMic,AZ(e)),o=Math.floor(s.length/Qo);if(o===0)return this.pendingMic=s,null;let a=[];try{for(let c=0;c<o;c++){let l=s.subarray(c*Qo,(c+1)*Qo),A=this.readReference(this.cursorSample+c*Qo);this.aec.analyze([A]);let d=this.aec.processSize([l]),h=new Float32Array(d);this.aec.process([h],[l]),a.push(h),this.blockTap?.(l,A,h)}}catch(c){return this.fail(c),null}return this.pendingMic=s.slice(o*Qo),this.cursorSample+=o*Qo,this.dropConsumedReference(),a.length===1?a[0]:mDe(a)}dropQueuedReference(){this.refQueue.length=0,this.refQueuedSamples=0}diagnostics(){return this.cursorSample===null?null:{skewMs:this.skewEmaMs,sinceAnchorMs:(this.lastStampSample-this.anchorSample)/Si,resyncs:this.resyncs,slewMs:this.slewSamples/Si}}status(){return this.state}close(){this.closed=!0,this.dropQueuedReference(),this.pendingMic=new Float32Array(0),this.freeInstance()}readReference(e){let r=new Float32Array(Qo),i=e+Qo;for(let n of this.refQueue){if(n.startSample>=i)break;let s=n.startSample+n.samples.length;if(s<=e)continue;let o=Math.max(e,n.startSample),a=Math.min(i,s);for(let c=o;c<a;c++){let l=n.samples[c-n.startSample],A=c-e;Math.abs(l)>Math.abs(r[A])&&(r[A]=l)}}return r}dropConsumedReference(){let e=this.cursorSample??0;for(;this.refQueue.length>0&&this.refQueue[0].startSample+this.refQueue[0].samples.length<=e;)this.refQueuedSamples-=this.refQueue[0].samples.length,this.refQueue.shift()}fail(e){this.state!=="unavailable"&&(this.state="unavailable",this.freeInstance(),this.dropQueuedReference(),this.pendingMic=new Float32Array(0),this.log.debug("echo canceller unavailable; barge-in stays on reference-aware gating",{error:e instanceof Error?e.message:String(e)}))}freeInstance(){let e=this.aec;this.aec=null;try{e?.free()}catch{}}};async function gDe(){let t=await import("@ennuicastr/webrtcaec3.js");return("default"in t?t.default:t)()}function AZ(t){let e=t.byteLength>>1,r=new DataView(t.buffer,t.byteOffset,t.byteLength),i=new Float32Array(e);for(let n=0;n<e;n++)i[n]=r.getInt16(n*2,!0)/32768;return i}function pDe(t,e){if(t.length===0)return e;let r=new Float32Array(t.length+e.length);return r.set(t,0),r.set(e,t.length),r}function mDe(t){let e=new Float32Array(t.reduce((i,n)=>i+n.length,0)),r=0;for(let i of t)e.set(i,r),r+=i.length;return e}import{emitKeypressEvents as EDe}from"node:readline";var SI=class{constructor(e,r,i=IDe){this.input=e;this.onQuit=r;this.prepare=i}input;onQuit;prepare;handlers=null;talking=!1;listening=!1;onKeypress=(e,r)=>{if(r.ctrl&&r.name==="c"){this.onQuit();return}r.name==="space"&&this.toggle()};listen(e){this.listening||(this.listening=!0,this.handlers=e,this.prepare(this.input),this.input.isTTY&&this.input.setRawMode&&this.input.setRawMode(!0),this.input.resume?.(),this.input.on("keypress",this.onKeypress))}close(){this.listening&&(this.listening=!1,this.input.removeListener("keypress",this.onKeypress),this.input.isTTY&&this.input.setRawMode&&this.input.setRawMode(!1),this.input.pause?.(),this.handlers=null)}toggle(){this.talking=!this.talking,this.talking?this.handlers?.onStart():this.handlers?.onEnd()}};function IDe(t){EDe(t)}var NDe={start:"starting...",stop:"stopping...",restart:"restarting...",devices:"listing devices..."};function RDe(){try{let t=kDe(new URL("../package.json",import.meta.url),"utf8"),e=JSON.parse(t);return typeof e.version=="string"?e.version:"0.0.0"}catch{return"0.0.0"}}function MDe(t,e){let r=new rI(t),i=r.notice();i!==null&&dt.err(`${i}
|
|
595
|
+
`)}toListening(){this.listenEpoch++,this.phase="listening",this.replyArmed=!1,this.delivered=!1,this.commandSeen=!1,this.commandsOpen=0,this.responseDoneSeen=!1,this.clearLiveness(),this.labelShown=!1,this.partialShown=!1,this.lastResponseStatus=void 0,this.currentTurnId=void 0,this.renderStatus("listening")}async cleanup(){this.cleanedUp||(this.cleanedUp=!0,this.clearLiveness(),this.clearJobsTicker(),this.clearMicReconnect(),this.closeOpenLine(),this.signal?.removeEventListener("abort",this.onAbort),this.ptt?.close(),this.playback.setPlayoutTap(null),this.aec?.close(),await this.capture.stop().catch(()=>{}),await this.playback.close().catch(()=>{}),await this.session.close().catch(()=>{}))}};Tt();var dZ=16e3,Si=16,Qo=160,cDe=120*dZ,lDe=1e3,uDe=.1,ADe=30,dDe=16,hDe=.1,fDe=500,vI=class{load;log;blockTap;state="idle";startPromise=null;aec=null;closed=!1;refQueue=[];refQueuedSamples=0;cursorSample=null;pendingMic=new Float32Array(0);anchorSample=0;lastStampSample=0;skewEmaMs=0;resyncs=0;slewSamples=0;constructor(e={}){this.load=e.load??gDe,this.log=e.log??ie,this.blockTap=e.blockTap}ensureStarted(){return this.startPromise??=this.start(),this.startPromise}async start(){this.state="loading";try{let e=await this.load();if(this.closed)return;this.aec=new e.AEC3(dZ,1,1),this.state="active"}catch(e){this.fail(e)}}ingestReference(e,r){if(!(this.closed||this.state==="unavailable")&&!(e.byteLength<2))for(this.refQueue.push({startSample:Math.round(r*Si),samples:AZ(e)}),this.refQueuedSamples+=e.byteLength>>1;this.refQueuedSamples>cDe&&this.refQueue.length>1;)this.refQueuedSamples-=this.refQueue[0].samples.length,this.refQueue.shift()}process(e,r){if(this.closed||this.state!=="active"||this.aec===null)return null;let i=e.byteLength>>1;if(i===0)return null;let n=Math.round((r-i/Si)*Si);if(this.cursorSample===null)this.cursorSample=n,this.anchorSample=n,this.skewEmaMs=0;else{let c=this.cursorSample+this.pendingMic.length,l=(n-c)/Si;if(Math.abs(n-c)>lDe*Si)this.pendingMic=new Float32Array(0),this.cursorSample=n,this.anchorSample=n,this.skewEmaMs=0,this.slewSamples=0,this.resyncs++;else{this.skewEmaMs+=uDe*(l-this.skewEmaMs);let A=Math.abs(this.skewEmaMs)-ADe;if(A>0){let d=Math.min(dDe,Math.max(1,Math.round(A*Si*hDe))),h=this.skewEmaMs>0?d:-d;Math.abs(this.slewSamples+h)<=fDe*Si&&(this.cursorSample+=h,this.slewSamples+=h,this.skewEmaMs-=h/Si)}}}this.lastStampSample=n+i;let s=pDe(this.pendingMic,AZ(e)),o=Math.floor(s.length/Qo);if(o===0)return this.pendingMic=s,null;let a=[];try{for(let c=0;c<o;c++){let l=s.subarray(c*Qo,(c+1)*Qo),A=this.readReference(this.cursorSample+c*Qo);this.aec.analyze([A]);let d=this.aec.processSize([l]),h=new Float32Array(d);this.aec.process([h],[l]),a.push(h),this.blockTap?.(l,A,h)}}catch(c){return this.fail(c),null}return this.pendingMic=s.slice(o*Qo),this.cursorSample+=o*Qo,this.dropConsumedReference(),a.length===1?a[0]:mDe(a)}dropQueuedReference(){this.refQueue.length=0,this.refQueuedSamples=0}diagnostics(){return this.cursorSample===null?null:{skewMs:this.skewEmaMs,sinceAnchorMs:(this.lastStampSample-this.anchorSample)/Si,resyncs:this.resyncs,slewMs:this.slewSamples/Si}}status(){return this.state}close(){this.closed=!0,this.dropQueuedReference(),this.pendingMic=new Float32Array(0),this.freeInstance()}readReference(e){let r=new Float32Array(Qo),i=e+Qo;for(let n of this.refQueue){if(n.startSample>=i)break;let s=n.startSample+n.samples.length;if(s<=e)continue;let o=Math.max(e,n.startSample),a=Math.min(i,s);for(let c=o;c<a;c++){let l=n.samples[c-n.startSample],A=c-e;Math.abs(l)>Math.abs(r[A])&&(r[A]=l)}}return r}dropConsumedReference(){let e=this.cursorSample??0;for(;this.refQueue.length>0&&this.refQueue[0].startSample+this.refQueue[0].samples.length<=e;)this.refQueuedSamples-=this.refQueue[0].samples.length,this.refQueue.shift()}fail(e){this.state!=="unavailable"&&(this.state="unavailable",this.freeInstance(),this.dropQueuedReference(),this.pendingMic=new Float32Array(0),this.log.debug("echo canceller unavailable; barge-in stays on reference-aware gating",{error:e instanceof Error?e.message:String(e)}))}freeInstance(){let e=this.aec;this.aec=null;try{e?.free()}catch{}}};async function gDe(){let t=await import("@ennuicastr/webrtcaec3.js");return("default"in t?t.default:t)()}function AZ(t){let e=t.byteLength>>1,r=new DataView(t.buffer,t.byteOffset,t.byteLength),i=new Float32Array(e);for(let n=0;n<e;n++)i[n]=r.getInt16(n*2,!0)/32768;return i}function pDe(t,e){if(t.length===0)return e;let r=new Float32Array(t.length+e.length);return r.set(t,0),r.set(e,t.length),r}function mDe(t){let e=new Float32Array(t.reduce((i,n)=>i+n.length,0)),r=0;for(let i of t)e.set(i,r),r+=i.length;return e}import{emitKeypressEvents as EDe}from"node:readline";var SI=class{constructor(e,r,i=IDe){this.input=e;this.onQuit=r;this.prepare=i}input;onQuit;prepare;handlers=null;talking=!1;listening=!1;onKeypress=(e,r)=>{if(r.ctrl&&r.name==="c"){this.onQuit();return}r.name==="space"&&this.toggle()};listen(e){this.listening||(this.listening=!0,this.handlers=e,this.prepare(this.input),this.input.isTTY&&this.input.setRawMode&&this.input.setRawMode(!0),this.input.resume?.(),this.input.on("keypress",this.onKeypress))}close(){this.listening&&(this.listening=!1,this.input.removeListener("keypress",this.onKeypress),this.input.isTTY&&this.input.setRawMode&&this.input.setRawMode(!1),this.input.pause?.(),this.handlers=null)}toggle(){this.talking=!this.talking,this.talking?this.handlers?.onStart():this.handlers?.onEnd()}};function IDe(t){EDe(t)}var NDe={start:"starting...",stop:"stopping...",restart:"restarting...",devices:"listing devices..."};function RDe(){try{let t=kDe(new URL("../package.json",import.meta.url),"utf8"),e=JSON.parse(t);return typeof e.version=="string"?e.version:"0.0.0"}catch{return"0.0.0"}}function MDe(t,e){let r=new rI(t),i=r.notice();i!==null&&dt.err(`${i}
|
|
596
596
|
`),r.isStale(Date.now())&&r.refreshInBackground();let n=!1,s=!1;try{let l=new Qr().resolved();n=G8(l),s=l.defaults.modality==="text"}catch{}let o=TDe(),a=l=>(ie.debug("opening interactive session",{project:e.project,capabilities:l}),mN().openSession({deviceId:o,projectId:e.project,clientCapabilities:l,launchDir:process.cwd()})),c=()=>V8({stdin:process.stdin,io:dt,openSession:()=>a(["text"]),createReader:()=>new CI(process.stdin,process.stdout)});return Y8({stdin:process.stdin,io:dt,textOnly:e.textOnly||s,voiceConfigured:n,micAvailable:LDe,openVoiceSession:()=>a(["text","audio-in","audio-out"]),runVoice:l=>FDe(l,e),runText:c})}async function FDe(t,e){let{DecibriCapture:r,DecibriPlayback:i}=await Promise.resolve().then(()=>(AR(),uR)),n=new AbortController,s=e.ptt?new SI(process.stdin,()=>n.abort()):void 0,o=()=>n.abort();process.once("SIGINT",o);try{await new wI({session:t,capture:new r,playback:new i,io:dt,echoCanceller:new vI,...s&&{pushToTalk:s},options:{halfDuplex:e.halfDuplex},signal:n.signal}).run()}finally{process.removeListener("SIGINT",o)}}async function LDe(){try{let{hasInputDevice:t}=await Promise.resolve().then(()=>(AR(),uR));return t()}catch(t){return ie.debug("audio module unavailable; treating as no microphone",{error:t}),!1}}function UDe(t){return{activate:e=>MDe(t,e),status:()=>new Ja(t).status(),upgrade:()=>new Ja(t).upgrade(),init:()=>new mI({config:new Qr,probe:new Jo,catalogue:new ls}).run(),doctor:()=>new nI({config:new Qr,probe:new Jo,version:t}).run(),models:e=>new EI({config:new Qr,catalogue:new ls,probe:new Jo}).run(e),configList:()=>new Xa(new Qr).list(),configGet:e=>new Xa(new Qr).get(e),configSet:(e,r)=>new Xa(new Qr,dt,new ls).set(e,r),configUnset:e=>new Xa(new Qr).unset(e),stub:e=>(dt.err(`${NDe[e]??`${e}: not implemented yet`}
|
|
597
597
|
`),0)}}try{let t=RDe();process.exitCode=await Z9(process.argv.slice(2),t,UDe(t))}catch(t){ie.error("jarvis failed",{error:t}),process.exitCode=1}
|