File size: 12,884 Bytes
b7a3e32
 
 
 
 
 
 
 
a546147
6cbca40
c0827a3
7f15e1c
6cbca40
4909ef6
 
b9b869f
b7a3e32
a546147
6cbca40
 
 
 
 
 
 
 
 
 
 
 
 
 
b9b869f
6cbca40
 
 
 
b7a3e32
 
 
 
 
 
 
 
 
 
 
4909ef6
b7a3e32
 
 
 
 
 
 
 
 
 
 
 
 
6cbca40
 
b7a3e32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9b869f
b7a3e32
c0827a3
b7a3e32
 
c0827a3
 
 
 
 
b7a3e32
 
 
c0827a3
b7a3e32
 
 
c0827a3
b7a3e32
 
 
 
 
a546147
 
 
 
 
 
 
 
 
 
 
 
7f15e1c
a546147
 
 
61e4b1e
a546147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f15e1c
61e4b1e
a546147
 
 
 
 
 
 
 
 
 
 
 
 
61e4b1e
a546147
 
 
 
 
 
 
 
61e4b1e
a546147
 
61e4b1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f15e1c
 
61e4b1e
a546147
 
 
 
 
61e4b1e
 
 
 
 
 
a546147
 
61e4b1e
a546147
 
61e4b1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a546147
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from fastapi import (
    APIRouter,
    status,
    HTTPException,
    File,
    UploadFile,
    Form,
)
from fastapi.responses import JSONResponse, StreamingResponse
from src.utils.logger import logger
from src.agents.role_play.flow import role_play_agent
from src.services.tts_service import tts_service
from pydantic import BaseModel, Field
from typing import Dict, Any, Optional
from src.agents.role_play.scenarios import get_scenarios
import json
import base64
import asyncio

router = APIRouter(prefix="/ai", tags=["AI"])


class RoleplayRequest(BaseModel):
    query: str = Field(..., description="User's query for the AI agent")
    session_id: str = Field(
        ..., description="Session ID for tracking user interactions"
    )
    scenario: Dict[str, Any] = Field(..., description="The scenario for the roleplay")


@router.get("/scenarios", status_code=status.HTTP_200_OK)
async def list_scenarios():
    """Get all available scenarios"""
    return JSONResponse(content=get_scenarios())


@router.post("/roleplay", status_code=status.HTTP_200_OK)
async def roleplay(
    session_id: str = Form(
        ..., description="Session ID for tracking user interactions"
    ),
    scenario: str = Form(
        ..., description="The scenario for the roleplay as JSON string"
    ),
    text_message: Optional[str] = Form(None, description="Text message from user"),
    audio_file: Optional[UploadFile] = File(None, description="Audio file from user"),
):
    """Send a message (text or audio) to the roleplay agent"""
    logger.info(f"Received roleplay request: {session_id}")
    # Validate that at least one input is provided
    if not text_message and not audio_file:
        raise HTTPException(
            status_code=400, detail="Either text_message or audio_file must be provided"
        )

    # Parse scenario from JSON string
    try:
        scenario_dict = json.loads(scenario)
    except json.JSONDecodeError:
        raise HTTPException(status_code=400, detail="Invalid scenario JSON format")

    if not scenario_dict:
        raise HTTPException(status_code=400, detail="Scenario not provided")

    # Prepare message content
    message_content = []

    # Handle text input
    if text_message:
        message_content.append({"type": "text", "text": text_message})

    # Handle audio input
    if audio_file:
        try:
            # Read audio file content
            audio_data = await audio_file.read()

            # Convert to base64
            audio_base64 = base64.b64encode(audio_data).decode("utf-8")

            # Determine mime type based on file extension
            file_extension = (
                audio_file.filename.split(".")[-1].lower()
                if audio_file.filename
                else "wav"
            )
            mime_type_map = {
                "wav": "audio/wav",
                "mp3": "audio/mpeg",
                "ogg": "audio/ogg",
                "webm": "audio/webm",
                "m4a": "audio/mp4",
            }
            mime_type = mime_type_map.get(file_extension, "audio/wav")

            message_content.append(
                {
                    "type": "audio",
                    "source_type": "base64",
                    "data": audio_base64,
                    "mime_type": mime_type,
                }
            )

        except Exception as e:
            logger.error(f"Error processing audio file: {str(e)}")
            raise HTTPException(
                status_code=400, detail=f"Error processing audio file: {str(e)}"
            )

    # Create message in the required format
    message = {"role": "user", "content": message_content}

    try:
        response = await role_play_agent().ainvoke(
            {
                "messages": [message],
                "scenario_title": scenario_dict["scenario_title"],
                "scenario_description": scenario_dict["scenario_description"],
                "scenario_context": scenario_dict["scenario_context"],
                "your_role": scenario_dict["your_role"],
                "key_vocabulary": scenario_dict["key_vocabulary"],
            },
            {"configurable": {"thread_id": session_id}},
        )

        # Extract AI response content
        ai_response = response["messages"][-1].content
        logger.info(f"AI response: {ai_response}")

        return JSONResponse(content={"response": ai_response})

    except Exception as e:
        logger.error(f"Error in roleplay: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")


@router.post("/roleplay/stream", status_code=status.HTTP_200_OK)
async def roleplay_stream(
    session_id: str = Form(
        ..., description="Session ID for tracking user interactions"
    ),
    scenario: str = Form(
        ..., description="The scenario for the roleplay as JSON string"
    ),
    text_message: Optional[str] = Form(None, description="Text message from user"),
    audio_file: Optional[UploadFile] = File(None, description="Audio file from user"),
    audio: bool = Form(False, description="Whether to return TTS audio response"),
):
    """Send a message (text or audio) to the roleplay agent with streaming response"""
    logger.info(f"Received streaming roleplay request: {session_id}")

    # Validate that at least one input is provided
    if not text_message and not audio_file:
        raise HTTPException(
            status_code=400, detail="Either text_message or audio_file must be provided"
        )

    # Parse scenario from JSON string
    try:
        scenario_dict = json.loads(scenario)
    except json.JSONDecodeError:
        raise HTTPException(status_code=400, detail="Invalid scenario JSON format")

    if not scenario_dict:
        raise HTTPException(status_code=400, detail="Scenario not provided")

    # Prepare message content
    message_content = []

    # Handle text input
    if text_message:
        message_content.append({"type": "text", "text": text_message})

    # Handle audio input
    if audio_file:
        try:
            # Read audio file content
            audio_data = await audio_file.read()

            # Convert to base64
            audio_base64 = base64.b64encode(audio_data).decode("utf-8")

            # Determine mime type based on file extension
            file_extension = (
                audio_file.filename.split(".")[-1].lower()
                if audio_file.filename
                else "wav"
            )
            mime_type_map = {
                "wav": "audio/wav",
                "mp3": "audio/mpeg",
                "ogg": "audio/ogg",
                "webm": "audio/webm",
                "m4a": "audio/mp4",
            }
            mime_type = mime_type_map.get(file_extension, "audio/wav")

            message_content.append(
                {
                    "type": "audio",
                    "source_type": "base64",
                    "data": audio_base64,
                    "mime_type": mime_type,
                }
            )

        except Exception as e:
            logger.error(f"Error processing audio file: {str(e)}")
            raise HTTPException(
                status_code=400, detail=f"Error processing audio file: {str(e)}"
            )

    # Create message in the required format
    message = {"role": "user", "content": message_content}

    async def generate_stream():
        """Generator function for streaming responses"""
        accumulated_content = ""
        conversation_ended = False
        try:
            input_graph = {
                "messages": [message],
                "scenario_title": scenario_dict["scenario_title"],
                "scenario_description": scenario_dict["scenario_description"],
                "scenario_context": scenario_dict["scenario_context"],
                "your_role": scenario_dict["your_role"],
                "key_vocabulary": scenario_dict["key_vocabulary"],
            }
            config = {"configurable": {"thread_id": session_id}}

            async for event in role_play_agent().astream(
                input=input_graph,
                stream_mode=["messages", "values"],
                config=config,
                subgraphs=True,
            ):
                _, event_type, message_chunk = event
                if event_type == "messages":
                    # message_chunk is a tuple, get the first element which is the actual AIMessageChunk
                    if isinstance(message_chunk, tuple) and len(message_chunk) > 0:
                        actual_message = message_chunk[0]
                        content = getattr(actual_message, "content", "")
                    else:
                        actual_message = message_chunk
                        content = getattr(message_chunk, "content", "")

                    # Check if this is a tool call message and if it's an end conversation tool call
                    if (
                        hasattr(actual_message, "tool_calls")
                        and actual_message.tool_calls
                    ):
                        # Check if any tool call is for ending the conversation
                        for tool_call in actual_message.tool_calls:
                            if (
                                isinstance(tool_call, dict)
                                and tool_call.get("name") == "end_conversation"
                            ):
                                # Send a special termination message to the client
                                termination_data = {
                                    "type": "termination",
                                    "content": "Conversation ended",
                                    "reason": tool_call.get("args", {}).get("reason", "Unknown reason")
                                }
                                yield f"data: {json.dumps(termination_data)}\n\n"
                                conversation_ended = True
                                break

                    if content and not conversation_ended:
                        # Accumulate content for TTS
                        accumulated_content += content

                        # Create SSE-formatted response
                        response_data = {
                            "type": "message_chunk",
                            "content": content,
                            "metadata": {
                                "agent": getattr(actual_message, "name", "unknown"),
                                "id": getattr(actual_message, "id", ""),
                                "usage_metadata": getattr(
                                    actual_message, "usage_metadata", {}
                                ),
                            },
                        }
                        yield f"data: {json.dumps(response_data)}\n\n"

                        # Small delay to prevent overwhelming the client
                        await asyncio.sleep(0.01)

            # Only send completion signal if conversation wasn't ended by tool call
            if not conversation_ended:
                # Generate TTS audio if requested
                audio_data = None
                if audio and accumulated_content.strip():
                    try:
                        logger.info(
                            f"Generating TTS for accumulated content: {len(accumulated_content)} chars"
                        )
                        audio_result = await tts_service.text_to_speech(accumulated_content)
                        if audio_result:
                            audio_data = {
                                "audio_data": audio_result["audio_data"],
                                "mime_type": audio_result["mime_type"],
                                "format": audio_result["format"],
                            }
                            logger.info("TTS audio generated successfully")
                        else:
                            logger.warning("TTS generation failed")
                    except Exception as tts_error:
                        logger.error(f"TTS generation error: {str(tts_error)}")

                # Send completion signal with optional audio
                completion_data = {"type": "completion", "content": "", "audio": audio_data}
                yield f"data: {json.dumps(completion_data)}\n\n"

        except Exception as e:
            logger.error(f"Error in streaming roleplay: {str(e)}")
            error_data = {"type": "error", "content": str(e)}
            yield f"data: {json.dumps(error_data)}\n\n"

    return StreamingResponse(
        generate_stream(),
        media_type="text/plain",
        headers={
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Content-Type": "text/event-stream",
        },
    )