refactor(adapters): add gateway/platforms/_shared.py (scoped-secret / profile-scope / port helpers) and lift shared text-batching into BasePlatformAdapter
Unifies 25 per-adapter copies of _get_scoped_secret/_get_wsecret/_resolve_qq_secret/_sig_secret/_yb_secret, 4+ _profile_scoped*, 3 _coerce_port, 5 _text_batch_key and 4 _enqueue_text_event.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
"""Cross-adapter helpers shared by gateway/platforms/* and plugins/platforms/*.
|
||||
|
||||
Kept dependency-light (stdlib + ``agent.secret_scope``) so every adapter can
|
||||
import it at module top level without cycles.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
from agent.secret_scope import UnscopedSecretError as _UnscopedSecretError
|
||||
from agent.secret_scope import get_secret as _scoped_get_secret
|
||||
|
||||
|
||||
def get_scoped_secret(name: str, default: Any = None) -> Any:
|
||||
"""Scope-aware credential read with the default-profile startup fallback.
|
||||
|
||||
An installed profile secret scope is authoritative: a scoped miss returns
|
||||
``default`` (never borrow another profile's value from ``os.environ``).
|
||||
The DEFAULT profile constructs and sends *unscoped* under multiplexing,
|
||||
where a bare ``get_secret`` raises ``UnscopedSecretError``; there
|
||||
``os.environ`` is that profile's own value, so fall back to it.
|
||||
"""
|
||||
try:
|
||||
val = _scoped_get_secret(name, default)
|
||||
except _UnscopedSecretError:
|
||||
val = os.getenv(name)
|
||||
return val if val is not None else default
|
||||
|
||||
|
||||
def profile_scoped() -> bool:
|
||||
"""True when running inside a multiplexed secondary profile's scope.
|
||||
|
||||
Secondary-profile adapters are constructed/connected inside
|
||||
``_profile_runtime_scope`` (secret scope installed + multiplex active).
|
||||
The DEFAULT profile under multiplexing runs unscoped and keeps the legacy
|
||||
``os.environ`` precedence, so YAML->env bridges must skip only when True.
|
||||
"""
|
||||
try:
|
||||
from agent.secret_scope import current_secret_scope, is_multiplex_active
|
||||
|
||||
return bool(is_multiplex_active() and current_secret_scope() is not None)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def coerce_port(value: Any, default: int) -> int:
|
||||
"""``int(value)`` or ``default`` when unparseable."""
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
Reference in New Issue
Block a user