Add typing support for session_args, fix debug logging in cloud.py
This commit is contained in:
@@ -12,16 +12,25 @@ from loguru import logger
|
||||
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
||||
from pipecat.pipeline.pipeline import Pipeline
|
||||
from pipecat.pipeline.runner import PipelineRunner
|
||||
from pipecat.pipeline.task import PipelineTask
|
||||
from pipecat.pipeline.task import PipelineParams, PipelineTask
|
||||
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
|
||||
from pipecat.runner.cloud import SmallWebRTCSessionArguments
|
||||
from pipecat.services.cartesia.tts import CartesiaTTSService
|
||||
from pipecat.services.deepgram.stt import DeepgramSTTService
|
||||
from pipecat.services.openai.llm import OpenAILLMService
|
||||
|
||||
try:
|
||||
from pipecatcloud.agent import DailySessionArguments, WebSocketSessionArguments
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"pipecatcloud package is required for cloud-compatible bots. "
|
||||
"Install with: pip install pipecat-ai[[pipecatcloud]]"
|
||||
)
|
||||
|
||||
load_dotenv(override=True)
|
||||
|
||||
|
||||
async def run_bot_logic(transport, handle_sigint: bool = True):
|
||||
async def run_bot(transport):
|
||||
"""Main bot logic that works with any transport."""
|
||||
logger.info(f"Starting bot")
|
||||
|
||||
@@ -56,7 +65,13 @@ async def run_bot_logic(transport, handle_sigint: bool = True):
|
||||
]
|
||||
)
|
||||
|
||||
task = PipelineTask(pipeline)
|
||||
task = PipelineTask(
|
||||
pipeline,
|
||||
params=PipelineParams(
|
||||
enable_metrics=True,
|
||||
enable_usage_metrics=True,
|
||||
),
|
||||
)
|
||||
|
||||
@transport.event_handler("on_client_connected")
|
||||
async def on_client_connected(transport, client):
|
||||
@@ -69,18 +84,16 @@ async def run_bot_logic(transport, handle_sigint: bool = True):
|
||||
logger.info("Client disconnected")
|
||||
await task.cancel()
|
||||
|
||||
runner = PipelineRunner(handle_sigint=handle_sigint)
|
||||
runner = PipelineRunner(handle_sigint=False)
|
||||
await runner.run(task)
|
||||
|
||||
|
||||
async def bot(session_args):
|
||||
async def bot(
|
||||
session_args: DailySessionArguments | SmallWebRTCSessionArguments | WebSocketSessionArguments,
|
||||
):
|
||||
"""Main bot entry point compatible with Pipecat Cloud."""
|
||||
|
||||
# Get handle_sigint from session_args, default to True for Daily
|
||||
handle_sigint = getattr(session_args, "handle_sigint", True)
|
||||
|
||||
if hasattr(session_args, "room_url") and hasattr(session_args, "token"):
|
||||
# Daily session arguments (cloud or local)
|
||||
if isinstance(session_args, DailySessionArguments):
|
||||
from pipecat.transports.services.daily import DailyParams, DailyTransport
|
||||
|
||||
transport = DailyTransport(
|
||||
@@ -94,8 +107,7 @@ async def bot(session_args):
|
||||
),
|
||||
)
|
||||
|
||||
elif hasattr(session_args, "webrtc_connection"):
|
||||
# WebRTC session arguments (local only, created by server.py)
|
||||
elif isinstance(session_args, SmallWebRTCSessionArguments):
|
||||
from pipecat.transports.base_transport import TransportParams
|
||||
from pipecat.transports.network.small_webrtc import SmallWebRTCTransport
|
||||
|
||||
@@ -108,14 +120,13 @@ async def bot(session_args):
|
||||
webrtc_connection=session_args.webrtc_connection,
|
||||
)
|
||||
|
||||
elif hasattr(session_args, "websocket"):
|
||||
# WebSocket session arguments (for telephony providers)
|
||||
elif isinstance(session_args, WebSocketSessionArguments):
|
||||
from pipecat.transports.network.fastapi_websocket import (
|
||||
FastAPIWebsocketParams,
|
||||
FastAPIWebsocketTransport,
|
||||
)
|
||||
|
||||
# Create appropriate serializer based on transport type
|
||||
# Create base parameters for telephony
|
||||
params = FastAPIWebsocketParams(
|
||||
audio_in_enabled=True,
|
||||
audio_out_enabled=True,
|
||||
@@ -123,43 +134,44 @@ async def bot(session_args):
|
||||
add_wav_header=False,
|
||||
)
|
||||
|
||||
if session_args.transport_type == "twilio":
|
||||
# Create appropriate serializer based on transport type
|
||||
transport_type = getattr(session_args, "transport_type", "unknown")
|
||||
call_info = getattr(session_args, "call_info", {})
|
||||
|
||||
if transport_type == "twilio":
|
||||
from pipecat.serializers.twilio import TwilioFrameSerializer
|
||||
|
||||
call_info = session_args.call_info
|
||||
params.serializer = TwilioFrameSerializer(
|
||||
stream_sid=call_info["stream_sid"],
|
||||
call_sid=call_info["call_sid"],
|
||||
account_sid=os.getenv("TWILIO_ACCOUNT_SID", ""),
|
||||
auth_token=os.getenv("TWILIO_AUTH_TOKEN", ""),
|
||||
)
|
||||
elif session_args.transport_type == "telnyx":
|
||||
elif transport_type == "telnyx":
|
||||
from pipecat.serializers.telnyx import TelnyxFrameSerializer
|
||||
|
||||
call_info = session_args.call_info
|
||||
params.serializer = TelnyxFrameSerializer(
|
||||
stream_id=call_info["stream_id"],
|
||||
call_control_id=call_info["call_control_id"],
|
||||
outbound_encoding=call_info["outbound_encoding"],
|
||||
inbound_encoding="PCMU",
|
||||
)
|
||||
elif session_args.transport_type == "plivo":
|
||||
elif transport_type == "plivo":
|
||||
from pipecat.serializers.plivo import PlivoFrameSerializer
|
||||
|
||||
call_info = session_args.call_info
|
||||
params.serializer = PlivoFrameSerializer(
|
||||
stream_id=call_info["stream_id"],
|
||||
call_id=call_info["call_id"],
|
||||
)
|
||||
else:
|
||||
raise ValueError(f"Unsupported WebSocket transport type: {session_args.transport_type}")
|
||||
raise ValueError(f"Unsupported WebSocket transport type: {transport_type}")
|
||||
|
||||
transport = FastAPIWebsocketTransport(websocket=session_args.websocket, params=params)
|
||||
|
||||
else:
|
||||
raise ValueError(f"Unknown session arguments: {session_args}")
|
||||
raise ValueError(f"Unsupported session arguments type: {type(session_args)}")
|
||||
|
||||
await run_bot_logic(transport, handle_sigint)
|
||||
await run_bot(transport)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user