feat(voice): enhance streaming engine and improve system reliability
Improve the PCM playback worklet with robust resampling and buffer management. Refactor the voice-stream service for better session teardown and error handling. Fix authentication issues by enabling credentials in HttpService and using relative URLs. Add Jalali date support and an AI model configuration tab in settings. Enhance diagnostics for voice transport and the audio engine.
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { firstValueFrom, race, throwError, Observable } from 'rxjs';
|
||||
import { delay } from 'rxjs/operators';
|
||||
import { firstValueFrom, Observable } from 'rxjs';
|
||||
import { VOICE_STREAM_CONFIG, VoiceSessionStatus } from './voice-stream.types';
|
||||
|
||||
/** The voice helper's HTTP deadline: a client timeout never means the server rolled back. */
|
||||
const REQUEST_DEADLINE_MS = 5000;
|
||||
/**
|
||||
* The voice helper's HTTP deadline: a client timeout never means the server rolled back.
|
||||
* Generous on purpose — the UI runs against a remote backend through the dev-server proxy,
|
||||
* and the first call after a reload pays the proxy connection setup.
|
||||
*/
|
||||
const REQUEST_DEADLINE_MS = 15000;
|
||||
|
||||
/** Error envelope from the contract: { error: { code, message, request_id, retryable, details } }. */
|
||||
export class VoiceApiError extends Error {
|
||||
@@ -199,14 +202,52 @@ export class VoiceApiService {
|
||||
}
|
||||
}
|
||||
|
||||
/** A short deadline keeps the UI responsive; the caller reconciles the uncertain outcome. */
|
||||
/**
|
||||
* A deadline that never aborts the request.
|
||||
*
|
||||
* `race()` is not usable here: when the timer wins, `race` unsubscribes the loser, so the
|
||||
* HttpClient would abort its XHR. An aborted XHR is reported by the browser as
|
||||
* "(canceled)" — with no status code at all — which is exactly what was observed for
|
||||
* `/api/v1/me`. It also makes a reserve ambiguous ("did the server create it?") instead of
|
||||
* letting it settle, which defeats the V03 reconciliation this flow is built around.
|
||||
*
|
||||
* Here the request subscription is deliberately kept alive: the UI still gets the timeout,
|
||||
* the XHR runs to completion, and a late response is simply discarded.
|
||||
*/
|
||||
private withDeadline<T>(request: Observable<T>): Observable<T> {
|
||||
return race(
|
||||
request,
|
||||
throwError(() => new VoiceApiError(0, 'TIMEOUT', 'request-deadline')).pipe(
|
||||
delay(REQUEST_DEADLINE_MS),
|
||||
),
|
||||
);
|
||||
return new Observable<T>((observer) => {
|
||||
let settled = false;
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
observer.error(new VoiceApiError(0, 'TIMEOUT', 'request-deadline'));
|
||||
}, REQUEST_DEADLINE_MS);
|
||||
|
||||
request.subscribe({
|
||||
next: (value) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
observer.next(value);
|
||||
},
|
||||
error: (err: unknown) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
observer.error(this.toApiError(err));
|
||||
},
|
||||
complete: () => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
observer.complete();
|
||||
},
|
||||
});
|
||||
|
||||
// Only the timer is torn down: the in-flight request is left to finish naturally.
|
||||
return () => clearTimeout(timer);
|
||||
});
|
||||
}
|
||||
|
||||
private toApiError(err: unknown): VoiceApiError {
|
||||
|
||||
Reference in New Issue
Block a user