feat(voice): implement local S2S retained history and provider selection
Add pinned CPU engine dependencies, retained interrupted-response history, provider admission, offline evaluation and isolated browser tooling. Record Phase 2 acceptance and Phase 3/4 preparation, bounded paid-test accounting and interruption incident findings. Human interruption acceptance, paired OpenAI evaluation and rollback remain pending.
This commit is contained in:
@@ -91,6 +91,7 @@ class OpenAIInputSettings(BaseModel):
|
||||
class VoiceSettings(LocalS2SSettings, OpenAIInputSettings, BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file='.env', extra='ignore', hide_input_in_errors=True, frozen=True, populate_by_name=True)
|
||||
provider: Literal['gemini', 'openai', 'local_s2s'] = Field(default='gemini', validation_alias=AliasChoices('VOICE_PROVIDER', 'PROVIDER', 'provider'))
|
||||
voice_selectable_providers: tuple[Literal['gemini', 'openai', 'local_s2s'], ...] = ()
|
||||
gemini_api_key: SecretStr = SecretStr('')
|
||||
gemini_model: str = Field(default='gemini-2.0-flash-exp', min_length=1, max_length=200)
|
||||
gemini_voice: str = Field(default='Aoede', min_length=1, max_length=80)
|
||||
@@ -227,7 +228,8 @@ class VoiceSettings(LocalS2SSettings, OpenAIInputSettings, BaseSettings):
|
||||
return dict(provider='local_s2s', model=self.selected_model, voice=self.selected_voice,
|
||||
input_sample_rate=24000, output_sample_rate=24000,
|
||||
evidence_policy_revision='same-session-evidence-retained-v5',
|
||||
development_only=True, interruption_policy='close_until_phase3',
|
||||
development_only=True, interruption_policy=('retain_unheard_draft_v1'
|
||||
if self.local_s2s_retain_interrupted_answers else 'close_until_phase3'),
|
||||
local_s2s=effective_identity(self.components(), setup_session(self)))
|
||||
profile = dict(provider=self.provider, model=self.selected_model, voice=self.selected_voice,
|
||||
input_sample_rate=16000 if self.provider == 'gemini' else 24000, output_sample_rate=24000,
|
||||
@@ -245,3 +247,13 @@ class VoiceSettings(LocalS2SSettings, OpenAIInputSettings, BaseSettings):
|
||||
from app.modules.voice.openai_style import instruction_profile
|
||||
profile.update(instruction_profile(POLICY))
|
||||
return profile
|
||||
|
||||
|
||||
def select_voice_provider(config, requested=None):
|
||||
selected = requested or config.provider
|
||||
if selected not in {config.provider, *config.voice_selectable_providers}:
|
||||
raise ValueError('LIVE_PROVIDER_UNAVAILABLE')
|
||||
# All secrets, models, speed and audio policy remain server-owned.
|
||||
result = config.model_copy(update={'provider': selected})
|
||||
result.require_voice()
|
||||
return result
|
||||
|
||||
@@ -87,7 +87,7 @@ class LiveLifecycleService:
|
||||
@staticmethod
|
||||
def _shape(row, *, replayed=False, eligible=True):
|
||||
sid = str(row['id'])
|
||||
return dict(session_id=sid, conversation_id=str(row['conversation_id']), status=row['status'],
|
||||
return dict(session_id=sid, conversation_id=str(row['conversation_id']), status=row['status'], provider=row.get('provider'),
|
||||
stream_path=f'/api/v1/voice/sessions/{sid}/stream' if eligible else None,
|
||||
expires_at=row['expires_at'].isoformat(), poll_url=f'/api/v1/voice/sessions/{sid}', replayed=replayed,
|
||||
calibration_status=LiveLifecycleService._calibration_status(row))
|
||||
|
||||
@@ -9,9 +9,19 @@ UPSTREAM_REVISION = '619e3fe4f71d1c6f35c15870d588a3638de34722'
|
||||
CONTRACT_REVISION = 'rag-local-s2s-phase2-v1'
|
||||
PATCH_REVISION = 'rag-local-s2s-phase2-v3'
|
||||
|
||||
RETENTION_POLICY = 'retain_unheard_draft_v1'
|
||||
DRAFT_MARKER = '[LOCAL_S2S_UNHEARD_DRAFT_V1]'
|
||||
RETENTION_INSTRUCTIONS = (DRAFT_MARKER + ' Interrupted assistant output is a saved draft, not proof the user heard it. '
|
||||
'For the same question you may answer from the draft again. Before using document facts, '
|
||||
'call reuse_last_evidence to revalidate evidence, then report_used_sources with current labels. '
|
||||
'Never treat a saved draft as fresh evidence or as a completed spoken answer. '
|
||||
'Do not speak draft markers or internal status labels.')
|
||||
|
||||
|
||||
|
||||
class LocalS2SSettings(BaseModel):
|
||||
local_s2s_enabled: bool = False
|
||||
local_s2s_retain_interrupted_answers: bool = False
|
||||
local_s2s_endpoint: str = 'ws://local-s2s-engine:8765/v1/realtime'
|
||||
local_s2s_token: SecretStr = SecretStr('')
|
||||
local_s2s_engine_revision: Literal[UPSTREAM_REVISION] = UPSTREAM_REVISION
|
||||
@@ -63,12 +73,15 @@ def setup_session(config):
|
||||
session['audio']['input']['transcription'] = dict(model=config.local_s2s_stt_model, language='fa')
|
||||
session['audio']['output']['voice'] = config.local_s2s_tts_voice
|
||||
session['audio']['output']['speed'] = 1.0
|
||||
if config.local_s2s_retain_interrupted_answers:
|
||||
session['instructions'] += '\n' + RETENTION_INSTRUCTIONS
|
||||
return session
|
||||
|
||||
|
||||
def effective_identity(components, session):
|
||||
return dict(engine_revision=UPSTREAM_REVISION, patch_revision=PATCH_REVISION,
|
||||
contract_revision=CONTRACT_REVISION, components=components,
|
||||
retention = session.get('instructions', '').endswith(RETENTION_INSTRUCTIONS)
|
||||
return dict(engine_revision=UPSTREAM_REVISION, patch_revision='rag-local-s2s-phase3-retain-v1' if retention else PATCH_REVISION,
|
||||
contract_revision='rag-local-s2s-phase3-retain-v1' if retention else CONTRACT_REVISION, components=components,
|
||||
effective_config_sha256=sha256(canonical_json(dict(components=components, session=session)).encode()).hexdigest())
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user