File size: 1,948 Bytes
13c6cad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import annotations

"""Shared helpers for configuring SEA-Lion access.

Supports both the public SEA-Lion API and the Amazon Bedrock access gateway
that exposes an OpenAI-compatible endpoint. Configuration is driven entirely by
environment variables so individual agents do not have to duplicate the same
initialisation logic.
"""

from dataclasses import dataclass
import hashlib
import os
from typing import Optional


@dataclass
class SeaLionSettings:
    base_url: str
    token: Optional[str]
    model: str
    mode: str  # "gateway", "api-key", or "disabled"


def _strip_or_none(value: Optional[str]) -> Optional[str]:
    if value is None:
        return None
    value = value.strip()
    return value or None


def fingerprint_secret(secret: Optional[str]) -> str:
    """Return a short fingerprint for a secret without exposing it."""
    if not secret:
        return "unset"
    try:
        return hashlib.sha256(secret.encode("utf-8")).hexdigest()[:10]
    except Exception:
        return "error"


def load_sea_lion_settings(*, default_model: str) -> SeaLionSettings:
    """Load SEA-Lion configuration from environment variables."""
    gateway_url = _strip_or_none(os.getenv("SEA_LION_GATEWAY_URL"))
    direct_url = _strip_or_none(os.getenv("SEA_LION_BASE_URL"))
    base_url = gateway_url or direct_url or "https://api.sea-lion.ai/v1"
    base_url = base_url.rstrip("/")

    gateway_token = _strip_or_none(os.getenv("SEA_LION_GATEWAY_TOKEN"))
    api_key = _strip_or_none(os.getenv("SEA_LION_API_KEY"))

    if gateway_token:
        mode = "gateway"
        token = gateway_token
    elif api_key:
        mode = "api-key"
        token = api_key
    else:
        mode = "disabled"
        token = None

    model = _strip_or_none(os.getenv("SEA_LION_MODEL_ID")) or default_model

    return SeaLionSettings(
        base_url=base_url,
        token=token,
        model=model,
        mode=mode,
    )