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.
65 lines
3.0 KiB
Python
65 lines
3.0 KiB
Python
"""Pinned local engine GA transport; no invented native input/history receipts."""
|
|
from app.infrastructure.realtime import RealtimeAdapter
|
|
from app.modules.voice.provider import ProviderProtocolError
|
|
from app.modules.voice.local_s2s import setup_session, effective_identity
|
|
|
|
|
|
class LocalS2SContextError(ProviderProtocolError):
|
|
context_sync = True
|
|
|
|
|
|
class LocalS2SAdapter(RealtimeAdapter):
|
|
def __init__(self, transport, *, config, settings):
|
|
# The local text model is not an OpenAI Realtime model. Keep its config
|
|
# independent of OpenAI reasoning/transcription settings.
|
|
self.transport, self.config, self.settings = transport, config, settings
|
|
self.model, self.voice = config.selected_model, config.selected_voice
|
|
self._setup = setup_session(config)
|
|
self._identity = effective_identity(config.components(), self._setup)
|
|
self._confirmed = False
|
|
|
|
def setup(self):
|
|
from copy import deepcopy
|
|
return dict(type='session.update', session=deepcopy(self._setup))
|
|
|
|
def acknowledged(self, session):
|
|
# A session echo cannot certify a fixed global engine's real components.
|
|
# The versioned engine extension derives this from initialized handlers.
|
|
if not isinstance(session, dict) or session.get('s2s_effective') != self._identity:
|
|
return False
|
|
def matches(actual, expected):
|
|
if isinstance(expected, dict):
|
|
return isinstance(actual, dict) and all(k in actual and matches(actual[k], v) for k, v in expected.items())
|
|
if isinstance(expected, list):
|
|
return isinstance(actual, list) and len(actual) == len(expected) and all(matches(a, b) for a, b in zip(actual, expected))
|
|
return type(actual) is type(expected) and actual == expected
|
|
self._confirmed = matches(session, self._setup)
|
|
return self._confirmed
|
|
|
|
def token_limited(self, response):
|
|
# No unverified local terminal signature is accepted as a successful answer.
|
|
return False
|
|
|
|
def is_no_active(self, error):
|
|
return False
|
|
|
|
def confirm_history_repair(self, repair, event):
|
|
raise LocalS2SContextError('Local history repair is not available in Phase 2')
|
|
|
|
async def send_serialized(self, wire):
|
|
from app.modules.voice.provider import strict_json
|
|
event = strict_json(wire)
|
|
if event.get('type') in ('response.cancel', 'conversation.item.truncate'):
|
|
raise LocalS2SContextError('Local interruption requires Phase 3 history repair')
|
|
await self.transport.send(wire)
|
|
|
|
|
|
async def connect_local(config, limits):
|
|
from websockets.asyncio.client import connect
|
|
token = config.local_s2s_token.get_secret_value()
|
|
return await connect(config.local_s2s_endpoint,
|
|
additional_headers={'Authorization': 'Bearer ' + token} if token else {},
|
|
proxy=None, compression=None, max_size=limits.max_provider_message_bytes, max_queue=16,
|
|
open_timeout=limits.connect_timeout_seconds, close_timeout=limits.send_timeout_seconds,
|
|
ping_interval=20, ping_timeout=20)
|