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.
42 lines
1.7 KiB
Python
42 lines
1.7 KiB
Python
"""Fixed Chat Completions policy, applied before upstream setup/warmup."""
|
|
from types import SimpleNamespace
|
|
|
|
|
|
class LimitedChatClient:
|
|
def __init__(self, client, token_limit):
|
|
self._client, self.token_limit = client, token_limit
|
|
self.max_retries = 0
|
|
self.chat = SimpleNamespace(completions=SimpleNamespace(create=self._create))
|
|
|
|
def _create(self, **kwargs):
|
|
kwargs.pop('max_completion_tokens', None)
|
|
kwargs['max_tokens'] = self.token_limit
|
|
return self._client.chat.completions.create(**kwargs)
|
|
|
|
def with_options(self, **kwargs):
|
|
# Pinned warmup explicitly asks for WARMUP_MAX_RETRIES. Never honor it.
|
|
kwargs['max_retries'] = 0
|
|
return LimitedChatClient(self._client.with_options(**kwargs), self.token_limit)
|
|
|
|
def __getattr__(self, name):
|
|
return getattr(self._client, name)
|
|
|
|
|
|
def install_client_policy(base_module, config):
|
|
import httpx
|
|
native = base_module.OpenAI
|
|
def create(*args, **kwargs):
|
|
kwargs['max_retries'] = 0
|
|
kwargs['timeout'] = httpx.Timeout(config.local_s2s_request_timeout_seconds, connect=5)
|
|
return LimitedChatClient(native(*args, **kwargs), config.local_s2s_max_output_tokens)
|
|
base_module.OpenAI = create
|
|
# The pinned base defaults to 20 seconds. Apply the configured deadline
|
|
# before setup computes request_timeout and invokes its warmup.
|
|
base = base_module.BaseOpenAICompatibleHandler
|
|
native_setup = base.setup
|
|
def setup(self, *args, **kwargs):
|
|
kwargs['request_timeout_s'] = config.local_s2s_request_timeout_seconds
|
|
return native_setup(self, *args, **kwargs)
|
|
base.setup = setup
|
|
return native
|