Skip to content

Commit 6b7802c

Browse files
committed
Handled user_name and merchant_id in db calls
1 parent dfaee8a commit 6b7802c

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

app/agents/voice/automatic/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ async def on_function_calls_started(service, function_calls):
192192
# If not found in memory, try loading from database
193193
if not existing_conversation:
194194
try:
195-
existing_conversation = await conversation_manager.load_conversation_from_db(args.session_id)
195+
existing_conversation = await conversation_manager.load_conversation_from_db(args.session_id, args.user_name, args.merchant_id)
196196
if existing_conversation:
197197
logger.info(f"Loaded conversation from database for session {args.session_id}")
198198
except Exception as e:
@@ -262,7 +262,7 @@ async def on_function_calls_started(service, function_calls):
262262

263263
# Add processors for conversation tracking
264264
user_message_capture = UserMessageCaptureProcessor(args.session_id)
265-
tool_call_processor = LLMSpyProcessor(rtvi, args.session_id)
265+
tool_call_processor = LLMSpyProcessor(rtvi, args.session_id, args.user_name, args.merchant_id)
266266

267267
# Build pipeline components
268268
pipeline_components = [

app/agents/voice/automatic/conversation_manager.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async def stop(self):
6464
await self._cleanup_task
6565
logger.info("ConversationManager stopped")
6666

67-
def get_or_create_conversation(self, session_id: str) -> ConversationDebugData:
67+
def get_or_create_conversation(self, session_id: str, user_id: Optional[str] = None, merchant_id: Optional[str] = None) -> ConversationDebugData:
6868
"""Get existing conversation or create a new one for the session"""
6969
with self._lock:
7070
# Update access time
@@ -80,6 +80,8 @@ def get_or_create_conversation(self, session_id: str) -> ConversationDebugData:
8080
conversation = ConversationDebugData(
8181
session_id=session_id,
8282
conversation_id=conversation_id,
83+
user_id=user_id,
84+
merchant_id=merchant_id,
8385
metadata={
8486
"created_by": "conversation_manager",
8587
"max_turns": self.max_turns_per_session
@@ -346,11 +348,11 @@ async def _save_conversation_to_db(self, conversation: 'ConversationDebugData',
346348
create_conversation, get_conversation_by_session, save_message, save_tool_call
347349
)
348350

349-
# Use fallback values if user context not provided
351+
# Use values from conversation object if not provided as parameters
350352
if not user_id:
351-
user_id = "unknown_user" # This should be improved to get from session context
353+
user_id = conversation.user_id or "unknown_user"
352354
if not merchant_id:
353-
merchant_id = "unknown_merchant" # This should be improved to get from session context
355+
merchant_id = conversation.merchant_id or "unknown_merchant"
354356

355357
# Check if conversation already exists in database
356358
existing_conv = await get_conversation_by_session(conversation.session_id, user_id, merchant_id)

app/agents/voice/automatic/processors/llm_spy.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import time
7+
from typing import Optional
78

89
from pipecat.frames.frames import (
910
Frame,
@@ -54,10 +55,12 @@ class LLMSpyProcessor(FrameProcessor):
5455
4. Processes highlight text for timing correlation
5556
"""
5657

57-
def __init__(self, rtvi: RTVIProcessor, session_id: str, name: str = "LLMSpyProcessor"):
58+
def __init__(self, rtvi: RTVIProcessor, session_id: str, user_id: Optional[str] = None, merchant_id: Optional[str] = None, name: str = "LLMSpyProcessor"):
5859
super().__init__(name=name)
5960
self._rtvi = rtvi
6061
self._session_id = session_id
62+
self._user_id = user_id
63+
self._merchant_id = merchant_id
6164

6265
# LLM response collection
6366
self._accumulated_text = ""
@@ -81,6 +84,8 @@ async def process_frame(self, frame: Frame, direction: FrameDirection):
8184
user_content = _session_user_messages.get(self._session_id, "[Inferred from voice]")
8285

8386
# Start conversation turn via ConversationManager with actual user message
87+
# Ensure conversation is created with user_id and merchant_id
88+
self._conversation_manager.get_or_create_conversation(self._session_id, self._user_id, self._merchant_id)
8489
event = await self._conversation_manager.start_turn_with_events(self._session_id, user_content)
8590
if event:
8691
await self._emit_rtvi_event(event)
@@ -205,4 +210,3 @@ async def _emit_chart_components(self, function_name: str) -> None:
205210
pass
206211
except Exception as e:
207212
logger.error(f"Error emitting chart components for session {self._session_id}: {e}")
208-

app/agents/voice/automatic/services/conversation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class ConversationDebugData(BaseModel):
140140
"""Complete conversation data for debugging"""
141141
session_id: str
142142
conversation_id: str
143+
merchant_id: Optional[str] = None
144+
user_id: Optional[str] = None
143145
turns: List[ConversationTurn] = Field(default_factory=list)
144146
summary: Optional[ConversationSummary] = None
145147
metadata: Optional[Dict[str, Any]] = None

0 commit comments

Comments
 (0)