Files
agent-call/agent_call/main.py
T

73 lines
2.3 KiB
Python

from __future__ import annotations
import os
from pathlib import Path
from .core import AgentCallService, ConfigurationError, _integer
from .http import make_server
from .sip_readonly import SipReadonlyClient
def build_service() -> AgentCallService:
profile = Path(
os.environ.get("AGENT_CALL_PROFILE", "docs/contracts/mock-profile.json")
)
db_path = os.environ.get("AGENT_CALL_DB", "data/agent-call.sqlite3")
object_dir = os.environ.get("AGENT_CALL_OBJECT_DIR")
provider_mode = os.environ.get("AGENT_CALL_MODE")
readonly_url = os.environ.get("AGENT_CALL_SIP_READONLY_URL")
readonly_token = os.environ.get("AGENT_CALL_SIP_READONLY_TOKEN")
sip_readonly = None
if readonly_url or readonly_token:
if not readonly_url or not readonly_token:
raise ConfigurationError(
"SIP read-only URL and token must be configured together"
)
try:
readonly_timeout = float(
os.environ.get("AGENT_CALL_SIP_READONLY_TIMEOUT_SECONDS", "5")
)
except ValueError as exc:
raise ConfigurationError(
"AGENT_CALL_SIP_READONLY_TIMEOUT_SECONDS must be a number"
) from exc
try:
sip_readonly = SipReadonlyClient(
readonly_url,
readonly_token,
ca_file=os.environ.get("AGENT_CALL_SIP_READONLY_CA_FILE") or None,
timeout=readonly_timeout,
)
except ValueError as exc:
raise ConfigurationError(str(exc)) from exc
return AgentCallService(
db_path=db_path,
profile_path=profile,
object_dir=object_dir,
mode=provider_mode,
sip_readonly=sip_readonly,
start_background=True,
)
def main() -> None:
service = build_service()
host = os.environ.get("AGENT_CALL_HOST", "127.0.0.1")
port = _integer(os.environ.get("AGENT_CALL_PORT", "8080"))
server = make_server(service, host, port)
try:
server.serve_forever()
except KeyboardInterrupt:
return
finally:
server.shutdown()
server.server_close()
service.stop()
if __name__ == "__main__":
try:
main()
except ConfigurationError as exc:
raise SystemExit(f"configuration error: {exc.detail}") from exc