arcane-os 0.5.14 → 0.5.16

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.
@@ -289,6 +289,90 @@ function queueFor(speech){
289
289
  return queue;
290
290
  }
291
291
 
292
+ function providerSpeechCapacity(speech){
293
+ if(typeof speech?.fetchTTS!=='function')return null;
294
+ try{
295
+ const providerRuntime=speech.providerRuntime;
296
+ if(!providerRuntime||typeof providerRuntime.status!=='function')return null;
297
+ const capacity=providerRuntime.status(
298
+ 'tts',
299
+ {execution:true}
300
+ )?.execution?.maxConcurrentRequests;
301
+ return Number.isSafeInteger(capacity)&&capacity>0?capacity:null;
302
+ }catch{
303
+ // Clients without inspectable provider capacity retain serialized admission.
304
+ return null;
305
+ }
306
+ }
307
+
308
+ function startProviderSegment(playback,index,providerSynthesisBatch){
309
+ if(
310
+ providerSynthesisBatch!==playback.providerSynthesisBatch
311
+ ||index<0
312
+ ||index>=playback.parts.length
313
+ ||playback.urls[index]
314
+ )return null;
315
+ const existing=playback.segmentRequests.get(index);
316
+ if(existing?.providerSynthesisBatch===providerSynthesisBatch){
317
+ return existing;
318
+ }
319
+
320
+ playback.segmentErrors.delete(index);
321
+ const record={providerSynthesisBatch,index,promise:null};
322
+ record.promise=playback.synthesizeSegment(
323
+ index,
324
+ playback.generation,
325
+ index===0,
326
+ providerSynthesisBatch
327
+ ).then(
328
+ function storeProviderSynthesizedSegment(outcome){
329
+ if(outcome===SUPERSEDED)return {ready:false,superseded:true};
330
+ if(providerSynthesisBatch!==playback.providerSynthesisBatch){
331
+ playback.revokeObjectURL(outcome.url);
332
+ return {ready:false,superseded:true};
333
+ }
334
+ playback.urls[index]=outcome.url;
335
+ return {ready:true};
336
+ },
337
+ function handleProviderSegmentFailure(error){
338
+ if(providerSynthesisBatch===playback.providerSynthesisBatch){
339
+ playback.segmentErrors.set(
340
+ index,
341
+ {providerSynthesisBatch,index,error}
342
+ );
343
+ }
344
+ return {ready:false,error};
345
+ }
346
+ ).finally(function clearSettledProviderSegment(){
347
+ if(playback.segmentRequests.get(index)===record){
348
+ playback.segmentRequests.delete(index);
349
+ }
350
+ }).catch(function observeProviderSegmentHandlingFailure(error){
351
+ if(providerSynthesisBatch===playback.providerSynthesisBatch){
352
+ playback.segmentErrors.set(
353
+ index,
354
+ {providerSynthesisBatch,index,error}
355
+ );
356
+ }
357
+ return {ready:false,error};
358
+ });
359
+ playback.segmentRequests.set(index,record);
360
+ return record;
361
+ }
362
+
363
+ function startProviderSynthesis(playback,providerSynthesisBatch){
364
+ let first=null;
365
+ for(let index=0;index<playback.parts.length;index+=1){
366
+ const record=startProviderSegment(
367
+ playback,
368
+ index,
369
+ providerSynthesisBatch
370
+ );
371
+ if(index===0)first=record;
372
+ }
373
+ return first;
374
+ }
375
+
292
376
  const DEFAULT_MESSAGES={
293
377
  idle:'Speech is ready when visual content is available.',
294
378
  queued:'Waiting for the current local speech request. The latest narration will begin next.',
@@ -364,6 +448,9 @@ class SpeechPlayback{
364
448
  this.pendingGenerations=new Set();
365
449
  this.lookahead=null;
366
450
  this.lookaheadError=null;
451
+ this.providerSynthesisBatch=null;
452
+ this.segmentRequests=new Map();
453
+ this.segmentErrors=new Map();
367
454
  this.model=model===null||model===undefined?null:String(model);
368
455
  this.voice=voice===null||voice===undefined?null:String(voice);
369
456
  this.responseFormat=responseFormat===null||responseFormat===undefined
@@ -441,7 +528,12 @@ class SpeechPlayback{
441
528
  );
442
529
  }
443
530
 
444
- hasAudio(key=this.key){return Boolean(this.urls.some(Boolean)&&(!key||key===this.key));}
531
+ hasAudio(key=this.key){
532
+ const playable=this.providerSynthesisBatch
533
+ ?Boolean(this.urls[0])
534
+ :this.urls.some(Boolean);
535
+ return Boolean(playable&&(!key||key===this.key));
536
+ }
445
537
 
446
538
  releaseURLs(){
447
539
  for(const url of this.urls){
@@ -451,6 +543,9 @@ class SpeechPlayback{
451
543
  this.parts=[];
452
544
  this.lookahead=null;
453
545
  this.lookaheadError=null;
546
+ this.providerSynthesisBatch=null;
547
+ this.segmentRequests.clear();
548
+ this.segmentErrors.clear();
454
549
  }
455
550
 
456
551
  releaseUrls(){this.releaseURLs();}
@@ -496,39 +591,55 @@ class SpeechPlayback{
496
591
  throw error;
497
592
  }
498
593
 
499
- async synthesizeSegment(index,generation,announce=false){
594
+ async synthesizeSegment(
595
+ index,
596
+ generation,
597
+ announce=false,
598
+ providerSynthesisBatch=null
599
+ ){
500
600
  const playback=this;
501
601
  const controller=new AbortController();
502
602
  const token={generation,index,controller};
503
- const queue=queueFor(this.speech);
603
+ const queue=providerSynthesisBatch?null:queueFor(this.speech);
504
604
  this.pendingGenerations.add(token);
505
605
  this.abortControllers.add(controller);
606
+ function segmentRequestIsCurrent(){
607
+ return providerSynthesisBatch
608
+ ?providerSynthesisBatch===playback.providerSynthesisBatch
609
+ :generation===playback.generation;
610
+ }
611
+ async function synthesizeQueuedSegment(){
612
+ const part=playback.parts[index];
613
+ if(!segmentRequestIsCurrent()||!part){
614
+ controller.abort();
615
+ return SUPERSEDED;
616
+ }
617
+ if(announce){
618
+ playback.emit('synthesizing',playback.message('preparing',{count:playback.parts.length}));
619
+ }
620
+ const audio=await playback.requestSpeech(
621
+ part,
622
+ controller.signal
623
+ );
624
+ if(!segmentRequestIsCurrent()){
625
+ controller.abort();
626
+ return SUPERSEDED;
627
+ }
628
+ const url=playback.createObjectURL(audio);
629
+ if(!segmentRequestIsCurrent()){
630
+ playback.revokeObjectURL(url);
631
+ controller.abort();
632
+ return SUPERSEDED;
633
+ }
634
+ return {url};
635
+ }
506
636
  try{
507
- return await queue.enqueue(async function synthesizeQueuedSegment(){
508
- const part=playback.parts[index];
509
- if(generation!==playback.generation||!part){
510
- controller.abort();
511
- return SUPERSEDED;
512
- }
513
- if(announce){
514
- playback.emit('synthesizing',playback.message('preparing',{count:playback.parts.length}));
515
- }
516
- const audio=await playback.requestSpeech(
517
- part,
518
- controller.signal
637
+ return providerSynthesisBatch
638
+ ?await Promise.resolve().then(synthesizeQueuedSegment)
639
+ :await queue.enqueue(
640
+ synthesizeQueuedSegment,
641
+ {speculative:!announce}
519
642
  );
520
- if(generation!==playback.generation){
521
- controller.abort();
522
- return SUPERSEDED;
523
- }
524
- const url=playback.createObjectURL(audio);
525
- if(generation!==playback.generation){
526
- playback.revokeObjectURL(url);
527
- controller.abort();
528
- return SUPERSEDED;
529
- }
530
- return {url};
531
- },{speculative:!announce});
532
643
  }finally{
533
644
  this.pendingGenerations.delete(token);
534
645
  this.abortControllers.delete(controller);
@@ -576,6 +687,19 @@ class SpeechPlayback{
576
687
  async waitForSegment(index,generation){
577
688
  if(generation!==this.generation)return false;
578
689
  if(this.urls[index])return true;
690
+ if(this.providerSynthesisBatch){
691
+ const segmentError=this.segmentErrors.get(index);
692
+ if(segmentError?.providerSynthesisBatch===this.providerSynthesisBatch){
693
+ return false;
694
+ }
695
+ const record=this.segmentRequests.get(index);
696
+ if(!record)return Boolean(this.urls[index]);
697
+ const result=await record.promise;
698
+ return generation===this.generation
699
+ &&record.providerSynthesisBatch===this.providerSynthesisBatch
700
+ &&result.ready===true
701
+ &&Boolean(this.urls[index]);
702
+ }
579
703
  if(
580
704
  this.lookaheadError
581
705
  &&this.lookaheadError.generation===generation
@@ -666,13 +790,28 @@ class SpeechPlayback{
666
790
  throw error;
667
791
  }
668
792
 
669
- const queue=queueFor(this.speech);
670
- if(queue.busy)this.emit('synthesizing',this.message('queued',{count:normalized.length}));
671
793
  let outcome;
672
794
  try{
673
- outcome=await this.synthesizeSegment(0,generation,true);
795
+ const providerCapacity=providerSpeechCapacity(this.speech);
796
+ if(providerCapacity===null){
797
+ const queue=queueFor(this.speech);
798
+ if(queue.busy)this.emit('synthesizing',this.message('queued',{count:normalized.length}));
799
+ outcome=await this.synthesizeSegment(0,generation,true);
800
+ }else{
801
+ const providerSynthesisBatch={capacity:providerCapacity};
802
+ this.providerSynthesisBatch=providerSynthesisBatch;
803
+ const first=startProviderSynthesis(
804
+ this,
805
+ providerSynthesisBatch
806
+ );
807
+ const result=await first.promise;
808
+ if(Object.hasOwn(result,'error'))throw result.error;
809
+ outcome=result.ready?{url:this.urls[0]}:SUPERSEDED;
810
+ }
674
811
  }catch(error){
675
812
  if(generation!==this.generation)return {ready:false,played:false};
813
+ for(const controller of this.abortControllers)controller.abort();
814
+ this.abortControllers.clear();
676
815
  this.releaseURLs();
677
816
  this.fail(error);
678
817
  throw error;
@@ -693,7 +832,9 @@ class SpeechPlayback{
693
832
  this.loadCurrent();
694
833
  this.emit('ready',this.message('ready'));
695
834
  const played=autoplay?await this.play():false;
696
- if(generation===this.generation)this.startLookahead(1,generation);
835
+ if(generation===this.generation&&!this.providerSynthesisBatch){
836
+ this.startLookahead(1,generation);
837
+ }
697
838
  return {ready:true,played};
698
839
  }
699
840
 
@@ -723,14 +864,34 @@ class SpeechPlayback{
723
864
  if(!this.hasAudio())return false;
724
865
  this.generation+=1;
725
866
  const generation=this.generation;
726
- this.lookahead=null;
727
- this.lookaheadError=null;
867
+ if(this.providerSynthesisBatch){
868
+ const providerSynthesisBatch=this.providerSynthesisBatch;
869
+ for(const [index,segmentError] of this.segmentErrors){
870
+ if(
871
+ segmentError?.providerSynthesisBatch
872
+ ===providerSynthesisBatch
873
+ &&!this.urls[index]
874
+ &&!this.segmentRequests.has(index)
875
+ ){
876
+ startProviderSegment(
877
+ this,
878
+ index,
879
+ providerSynthesisBatch
880
+ );
881
+ }
882
+ }
883
+ }else{
884
+ this.lookahead=null;
885
+ this.lookaheadError=null;
886
+ }
728
887
  this.audio.pause();
729
888
  this.index=0;
730
889
  this.loadCurrent();
731
890
  this.audio.currentTime=0;
732
891
  const played=await this.play();
733
- if(generation===this.generation)this.startLookahead(1,generation);
892
+ if(generation===this.generation&&!this.providerSynthesisBatch){
893
+ this.startLookahead(1,generation);
894
+ }
734
895
  return played;
735
896
  }
736
897
 
@@ -772,12 +933,15 @@ class SpeechPlayback{
772
933
  const ready=await this.waitForSegment(nextIndex,generation);
773
934
  if(!ready||generation!==this.generation){
774
935
  if(generation===this.generation&&this.state==='buffering'){
775
- const bufferedError=(
776
- this.lookaheadError
936
+ const providerError=this.segmentErrors.get(nextIndex);
937
+ const providerFailed=this.providerSynthesisBatch
938
+ &&providerError?.providerSynthesisBatch
939
+ ===this.providerSynthesisBatch;
940
+ const lookaheadFailed=this.lookaheadError
777
941
  &&this.lookaheadError.generation===generation
778
- &&this.lookaheadError.index===nextIndex
779
- )?this.lookaheadError.error:null;
780
- if(bufferedError)this.fail(bufferedError);
942
+ &&this.lookaheadError.index===nextIndex;
943
+ if(providerFailed)this.fail(providerError.error);
944
+ else if(lookaheadFailed)this.fail(this.lookaheadError.error);
781
945
  else this.emit('ready',this.message('ready'));
782
946
  }
783
947
  return false;
@@ -786,7 +950,9 @@ class SpeechPlayback{
786
950
  this.index=nextIndex;
787
951
  this.loadCurrent();
788
952
  const played=await this.play();
789
- if(generation===this.generation)this.startLookahead(nextIndex+1,generation);
953
+ if(generation===this.generation&&!this.providerSynthesisBatch){
954
+ this.startLookahead(nextIndex+1,generation);
955
+ }
790
956
  return played;
791
957
  }
792
958