Talker / src /ofp_client.py
BolyosCsaba
initial commit
e214abd
"""
OFP Client
Handles sending and receiving Open Floor Protocol envelopes via HTTPS
"""
import requests
import logging
import json
from typing import Dict, Optional
from .models import Envelope, DialogEvent
logger = logging.getLogger(__name__)
class OFPClient:
"""Client for sending OFP envelopes to conveners and other assistants"""
def __init__(self, speaker_uri: str, service_url: str, manifest: Dict):
self.speaker_uri = speaker_uri
self.service_url = service_url
self.manifest = manifest
logger.info(f"OFP Client initialized for {speaker_uri}")
def send_envelope(self, recipient_url: str, envelope: Envelope, timeout: int = 10) -> bool:
"""Send OFP envelope to recipient via HTTPS POST"""
try:
payload = envelope.to_payload()
logger.debug(f"Sending envelope to {recipient_url}: {json.dumps(payload, indent=2)}")
response = requests.post(
recipient_url,
json=payload,
headers={
'Content-Type': 'application/json',
'User-Agent': 'OFP-Talker-Agent/1.0'
},
timeout=timeout
)
response.raise_for_status()
logger.info(f"βœ“ Envelope sent successfully to {recipient_url}")
return True
except requests.exceptions.Timeout:
logger.error(f"βœ— Timeout sending envelope to {recipient_url}")
return False
except requests.exceptions.RequestException as e:
logger.error(f"βœ— Failed to send envelope to {recipient_url}: {e}")
return False
except Exception as e:
logger.error(f"βœ— Unexpected error sending envelope: {e}")
return False
def send_utterance(
self,
conversation_id: str,
recipient_url: str,
text: str,
to: Optional[Dict] = None
) -> bool:
"""Send utterance message to the floor"""
try:
dialog_event = DialogEvent.create_text_event(
speaker_uri=self.speaker_uri,
text=text
)
event = {
"eventType": "utterance",
"parameters": {
"dialogEvent": dialog_event.to_dict()
}
}
if to:
event["to"] = to
envelope = Envelope(
schema={"version": "1.0.0"},
conversation={"id": conversation_id},
sender={"speakerUri": self.speaker_uri},
events=[event]
)
return self.send_envelope(recipient_url, envelope)
except Exception as e:
logger.error(f"Error sending utterance: {e}")
return False
def request_floor(
self,
conversation_id: str,
convener_url: str,
convener_uri: str
) -> bool:
"""Request speaking floor from convener"""
envelope = Envelope(
schema={"version": "1.0.0"},
conversation={"id": conversation_id},
sender={"speakerUri": self.speaker_uri},
events=[{
"eventType": "floorRequest",
"to": {
"speakerUri": convener_uri
}
}]
)
return self.send_envelope(convener_url, envelope)
def get_manifest(self) -> Dict:
"""Return assistant manifest"""
return self.manifest