Skip to content

Commit ab27e48

Browse files
authored
Ensure user agent is initialized before joining the call (#113)
* ensure user agent is initialized before joining the call * wip
1 parent 3cb339b commit ab27e48

File tree

8 files changed

+145
-122
lines changed

8 files changed

+145
-122
lines changed

agents-core/vision_agents/core/agents/agents.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def __init__(
105105
self.instructions = instructions
106106
self.edge = edge
107107
self.agent_user = agent_user
108+
self._agent_user_initialized = False
108109

109110
# only needed in case we spin threads
110111
self._root_span = trace.get_current_span()
@@ -329,6 +330,8 @@ async def on_stt_transcript_event_create_response(event: STTTranscriptEvent):
329330
await self.simple_response(event.text, event.user_metadata)
330331

331332
async def join(self, call: Call) -> "AgentSessionContextManager":
333+
await self.create_user()
334+
332335
# TODO: validation. join can only be called once
333336
with self.tracer.start_as_current_span("join"):
334337
if self._is_running:
@@ -516,16 +519,19 @@ def clear_call_logging_context(self) -> None:
516519
clear_call_context(self._call_context_token)
517520
self._call_context_token = None
518521

519-
async def create_user(self):
520-
"""Create the agent user in the edge provider, if required.
522+
async def create_user(self) -> None:
523+
"""Create the agent user in the edge provider, if required."""
524+
525+
if self._agent_user_initialized:
526+
return None
521527

522-
Returns:
523-
Provider-specific user creation response.
524-
"""
525528
with self.tracer.start_as_current_span("edge.create_user"):
526-
if self.agent_user.id == "":
527-
self.agent_user.id = str(uuid4())
528-
return await self.edge.create_user(self.agent_user)
529+
if not self.agent_user.id:
530+
self.agent_user.id = f"agent-{uuid4()}"
531+
await self.edge.create_user(self.agent_user)
532+
self._agent_user_initialized = True
533+
534+
return None
529535

530536
def _on_vad_audio(self, event: VADAudioEvent):
531537
self.logger.info(f"Vad audio event {self._truncate_for_logging(event)}")
@@ -614,7 +620,7 @@ async def say(
614620
metadata=metadata,
615621
)
616622
)
617-
# Unified API: simple non-streaming message
623+
618624
if self.conversation is not None:
619625
await self.conversation.upsert_message(
620626
role="assistant",

0 commit comments

Comments
 (0)