49b180922d
Use Hermes to exchange WebRTC offers for the connector profile. Keep cancellation acknowledgments private and guard replacement runs against stale delivery.
62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
"""Use Hermes's GPT-Live helper in the profile that owns this connector."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
class Unavailable(Exception):
|
|
pass
|
|
|
|
|
|
class ExchangeFailed(Exception):
|
|
pass
|
|
|
|
|
|
def _run(adapter: Any, operation: str, *args: str) -> dict:
|
|
try:
|
|
from tools import voice_live
|
|
except ImportError as exc:
|
|
raise Unavailable("This Hermes build does not support GPT-Live") from exc
|
|
|
|
from agent.secret_scope import (
|
|
build_profile_secret_scope, reset_secret_scope, set_secret_scope,
|
|
)
|
|
from hermes_constants import (
|
|
get_hermes_home, reset_hermes_home_override, set_hermes_home_override,
|
|
)
|
|
|
|
home = Path(getattr(adapter, "hermes_home", None) or get_hermes_home())
|
|
home_token = set_hermes_home_override(str(home))
|
|
try:
|
|
secret_token = set_secret_scope(build_profile_secret_scope(home))
|
|
try:
|
|
return getattr(voice_live, operation)(*args)
|
|
finally:
|
|
reset_secret_scope(secret_token)
|
|
finally:
|
|
reset_hermes_home_override(home_token)
|
|
|
|
|
|
async def status(adapter: Any) -> dict:
|
|
result = await asyncio.to_thread(_run, adapter, "resolve_gpt_live_status")
|
|
return {key: result.get(key) for key in ("available", "reason", "model", "voice")}
|
|
|
|
|
|
async def create_session(adapter: Any, offer: str) -> dict:
|
|
try:
|
|
result = await asyncio.to_thread(_run, adapter, "create_webrtc_session", offer)
|
|
session_id = result["session"]["id"]
|
|
answer = result["transport"]["sdp"]
|
|
except ValueError as exc:
|
|
if str(exc).startswith("GPT-Live needs an OpenAI API key"):
|
|
raise Unavailable("Hermes needs an OpenAI API key for GPT-Live") from exc
|
|
raise ExchangeFailed from exc
|
|
except (OSError, RuntimeError, KeyError, TypeError) as exc:
|
|
raise ExchangeFailed from exc
|
|
if not isinstance(session_id, str) or not session_id or not isinstance(answer, str) or not answer.strip():
|
|
raise ExchangeFailed
|
|
return {"session_id": session_id, "sdp": answer}
|