Files
amirhosein mazaheri 8d348f0ba1 feat(voice): add isolated Local S2S v3 candidate and acceptance evidence
Extract the provider boundary while retaining OpenAI/Gemini contracts. Add
an opt-in isolated Local S2S adapter, pinned engine extension, bounded
component gateway and frozen effective configuration profiles.

Enforce request token limits, zero retries, local asset validation,
connection deadlines, disconnect ownership, bounded cleanup and signals.
Include regression coverage, pinned source fixtures, operator guidance,
asset fingerprints and guarded disposable PostgreSQL acceptance tooling.
Preserve immutable fixture bytes across Git line-ending conversion.

Validation recorded in the reports:
- 74 repair, 107 combined and 242 preservation checks passed.
- Fresh PostgreSQL local checks: 2 passed.
- Expanded PostgreSQL: 57 passed, 6 documented baseline failures.
- Compose configurations, staged whitespace and fixture hashes passed.

Phase 1 live listening and Phase 2 complete engine/SDK/POSIX, real component
audio and Persian browser acceptance remain pending. No operational switch,
paid call, model download or Phase 3 implementation is included.
2026-10-15 12:28:17 +03:30

83 lines
4.1 KiB
Python

"""Development engine launcher; local asset preflight precedes every paid probe."""
import argparse
import json
import logging
import os
from pathlib import Path
def build_runtime(config, stop):
from services.local_s2s.extension import active_components, install_session_proof, FailClosedQueue, EngineTokenGuard
from services.local_s2s.request_policy import install_client_policy
from app.modules.voice.local_s2s import setup_session
from speech_to_speech.config import load_config, resolve_config
from speech_to_speech.configured_runtime import build_configured_server
from speech_to_speech.api.openai_realtime.handlers.session import SessionHandler
import speech_to_speech.s2s_pipeline as pipeline
import speech_to_speech.LLM.base_openai_compatible_language_model as base_llm
import speech_to_speech.api.openai_realtime.server as server_module
import torch
source = Path(os.environ.get('LOCAL_S2S_SILERO_SOURCE', '/vad/silero-vad'))
native_load = torch.hub.load
def local_vad(repo, *args, **kwargs):
if repo != 'snakers4/silero-vad:master':
raise ValueError('LOCAL_ENGINE_UNEXPECTED_MODEL_DOWNLOAD')
kwargs['source'] = 'local'
return native_load(str(source), *args, **kwargs)
torch.hub.load = local_vad
pipeline.Queue = lambda *args, **kwargs: FailClosedQueue(stop)
# Base setup invokes warmup from inside construction; install before it runs.
install_client_policy(base_llm, config)
resolved = resolve_config(load_config(Path(__file__).with_name('pipeline.yaml')), names=['local'])
runtime = build_configured_server(resolved, stop)
try:
units = runtime.server.pool
if len(units) != 1:
raise ValueError('LOCAL_ENGINE_SINGLE_PIPELINE_REQUIRED')
components = {id(unit.service): active_components(unit, config.components()) for unit in units}
install_session_proof(SessionHandler, components, setup_session(config))
create_app = server_module.create_app
def guarded_app(*args, **kwargs):
return EngineTokenGuard(create_app(*args, **kwargs), config.local_s2s_token.get_secret_value())
server_module.create_app = guarded_app
return runtime
except BaseException:
from services.local_s2s.lifecycle import bounded_call
bounded_call(runtime.stop, 2)
raise
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--allow-paid-startup', action='store_true')
args = parser.parse_args()
if not args.allow_paid_startup or os.environ.get('LOCAL_S2S_ALLOW_PAID_STARTUP') != 'true':
parser.error('Engine initialization performs paid component probes; authorize before starting.')
from app.modules.voice.configuration import VoiceSettings
from services.local_s2s.assets import validate_assets, configure_nltk, AssetError
from services.local_s2s.lifecycle import run_owned
try:
config = VoiceSettings(_env_file=None, provider='local_s2s')
config.require_voice()
root = os.environ.get('LOCAL_S2S_NLTK_DATA', '/assets/nltk')
fingerprint = validate_assets(root, os.environ.get('LOCAL_S2S_SILERO_SOURCE', '/vad/silero-vad'),
os.environ.get('LOCAL_S2S_ASSET_MANIFEST', '/assets/assets.json'))
if fingerprint != os.environ.get('LOCAL_S2S_ASSET_MANIFEST_SHA256'):
raise AssetError('LOCAL_ENGINE_ASSET_VALIDATION_FAILED')
import nltk
configure_nltk(nltk, root)
except (ValueError, OSError, ImportError):
print(json.dumps(dict(status='failed', category='LOCAL_ENGINE_ASSET_VALIDATION_FAILED')), flush=True)
return 1
logging.disable(logging.CRITICAL)
os.environ['HF_HUB_OFFLINE'] = '1'
os.environ['NLTK_DATA'] = root
key = os.environ.get('OPENROUTER_API_KEY', '')
if not key:
parser.error('OPENROUTER_API_KEY is required in the engine only.')
return run_owned(lambda stop: build_runtime(config, stop), key, config.local_s2s_request_timeout_seconds)
if __name__ == '__main__':
raise SystemExit(main())