mirror of
https://github.com/dograh-hq/dograh.git
synced 2026-07-13 11:22:14 +02:00
feat: add transcript panel during live call for better visibility (#116)
* chore: remove old signaling route * Show real time feedbacks
This commit is contained in:
parent
ad4cff73c8
commit
e7712474c1
15 changed files with 599 additions and 469 deletions
|
|
@ -10,7 +10,6 @@ from api.routes.organization_usage import router as organization_usage_router
|
|||
from api.routes.public_agent import router as public_agent_router
|
||||
from api.routes.public_embed import router as public_embed_router
|
||||
from api.routes.reports import router as reports_router
|
||||
from api.routes.rtc_offer import router as rtc_offer_router
|
||||
from api.routes.s3_signed_url import router as s3_router
|
||||
from api.routes.service_keys import router as service_keys_router
|
||||
from api.routes.superuser import router as superuser_router
|
||||
|
|
@ -27,7 +26,6 @@ router = APIRouter(
|
|||
)
|
||||
|
||||
router.include_router(telephony_router)
|
||||
router.include_router(rtc_offer_router)
|
||||
router.include_router(superuser_router)
|
||||
router.include_router(workflow_router)
|
||||
router.include_router(user_router)
|
||||
|
|
|
|||
|
|
@ -1,77 +0,0 @@
|
|||
from typing import Dict
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends
|
||||
from loguru import logger
|
||||
from pydantic import BaseModel
|
||||
|
||||
from api.db.models import UserModel
|
||||
from api.services.auth.depends import get_user
|
||||
from api.services.pipecat.run_pipeline import run_pipeline_smallwebrtc
|
||||
from pipecat.transports.smallwebrtc.connection import SmallWebRTCConnection
|
||||
from pipecat.utils.context import set_current_run_id
|
||||
|
||||
router = APIRouter(prefix="/pipecat")
|
||||
|
||||
pcs_map: Dict[str, SmallWebRTCConnection] = {}
|
||||
ice_servers = ["stun:stun.l.google.com:19302"]
|
||||
|
||||
|
||||
class RTCOfferRequest(BaseModel):
|
||||
pc_id: str | None
|
||||
sdp: str
|
||||
type: str
|
||||
workflow_id: int
|
||||
workflow_run_id: int
|
||||
restart_pc: bool = False
|
||||
call_context_vars: dict | None = None
|
||||
|
||||
|
||||
@router.post("/rtc-offer")
|
||||
async def offer(
|
||||
request: RTCOfferRequest,
|
||||
background_tasks: BackgroundTasks,
|
||||
user: UserModel = Depends(get_user),
|
||||
):
|
||||
pc_id = request.pc_id
|
||||
|
||||
if pc_id and pc_id in pcs_map:
|
||||
# Ensure run_id context is available for logs even when reusing an existing PC.
|
||||
set_current_run_id(request.workflow_run_id)
|
||||
|
||||
pipecat_connection = pcs_map[pc_id]
|
||||
logger.info(f"Reusing existing connection for pc_id: {pc_id}")
|
||||
await pipecat_connection.renegotiate(
|
||||
sdp=request.sdp,
|
||||
type=request.type,
|
||||
restart_pc=request.restart_pc,
|
||||
)
|
||||
else:
|
||||
# Set the run_id *before* creating the SmallWebRTCConnection so that all
|
||||
# async tasks and event-handler coroutines spawned inside the
|
||||
# constructor inherit the correct context variable value. Otherwise the
|
||||
# default ("NA") leaks into the log output produced by those tasks.
|
||||
set_current_run_id(request.workflow_run_id)
|
||||
|
||||
pipecat_connection = SmallWebRTCConnection(ice_servers)
|
||||
await pipecat_connection.initialize(sdp=request.sdp, type=request.type)
|
||||
|
||||
@pipecat_connection.event_handler("closed")
|
||||
async def handle_disconnected(webrtc_connection: SmallWebRTCConnection):
|
||||
logger.info(
|
||||
f"In pipecat connection closed handler. Popping peer connection pc_id: {webrtc_connection.pc_id} from pcs_map"
|
||||
)
|
||||
pcs_map.pop(webrtc_connection.pc_id, None)
|
||||
|
||||
background_tasks.add_task(
|
||||
run_pipeline_smallwebrtc,
|
||||
pipecat_connection,
|
||||
request.workflow_id,
|
||||
request.workflow_run_id,
|
||||
user.id,
|
||||
request.call_context_vars or {},
|
||||
)
|
||||
|
||||
answer = pipecat_connection.get_answer()
|
||||
pcs_map[answer["pc_id"]] = pipecat_connection
|
||||
|
||||
return answer
|
||||
|
|
@ -18,11 +18,16 @@ from aiortc import RTCIceServer
|
|||
from aiortc.sdp import candidate_from_sdp
|
||||
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
|
||||
from loguru import logger
|
||||
from starlette.websockets import WebSocketState
|
||||
|
||||
from api.db import db_client
|
||||
from api.db.models import UserModel
|
||||
from api.services.auth.depends import get_user_ws
|
||||
from api.services.pipecat.run_pipeline import run_pipeline_smallwebrtc
|
||||
from api.services.pipecat.ws_sender_registry import (
|
||||
register_ws_sender,
|
||||
unregister_ws_sender,
|
||||
)
|
||||
from api.services.quota_service import check_dograh_quota
|
||||
from pipecat.transports.smallwebrtc.connection import SmallWebRTCConnection
|
||||
from pipecat.utils.context import set_current_run_id
|
||||
|
|
@ -92,6 +97,9 @@ class SignalingManager:
|
|||
# Cleanup
|
||||
self._connections.pop(connection_id, None)
|
||||
|
||||
# Unregister WebSocket sender for real-time feedback
|
||||
unregister_ws_sender(workflow_run_id)
|
||||
|
||||
# Clean up all peer connections for this workflow run
|
||||
# Note: In a WebSocket-based signaling approach (vs HTTP PATCH),
|
||||
# we maintain our own connection map instead of relying on
|
||||
|
|
@ -182,6 +190,13 @@ class SignalingManager:
|
|||
# Store peer connection using client's pc_id
|
||||
self._peer_connections[pc_id] = pc
|
||||
|
||||
# Register WebSocket sender for real-time feedback
|
||||
async def ws_sender(message: dict):
|
||||
if ws.application_state == WebSocketState.CONNECTED:
|
||||
await ws.send_json(message)
|
||||
|
||||
register_ws_sender(workflow_run_id, ws_sender)
|
||||
|
||||
# Setup closed handler
|
||||
@pc.event_handler("closed")
|
||||
async def handle_disconnected(webrtc_connection: SmallWebRTCConnection):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue