is there a js or ts version of warm_transfer.py
# ask-ai
l
is there a js or ts version of warm_transfer.py
t
Thanks for reaching out, Chris. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thank you for reaching out! I wanted to let you know that there isn't a direct JavaScript or TypeScript version of
warm_transfer.py
available in LiveKit's repositories or documentation just yet. The example we have for agent-assisted warm transfers is written in Python, and it beautifully shows how to manage a warm transfer workflow using two agent sessions. That said, you absolutely can create a similar workflow in JavaScript or TypeScript by using LiveKit's Node.js SDKs. Here’s a friendly overview of how you might approach it: • Utilize the
@livekit/rtc-node
package to set up and manage rooms, connect your agents, and handle media workflows smoothly. • Make use of the
CreateSIPParticipantRequest
API to dial the supervisor when needed. • Implement hold music by publishing local audio files during the hold phase, keeping the experience seamless. • Pass conversation history between sessions, just like the Python example demonstrates, to maintain context. If you'd like, I’m more than happy to guide you through this or even provide a code outline to get you started with TypeScript. Just let me know if you want help with any particular step—whether it’s creating agent sessions, managing conversation history, or working with the APIs. I’m here to support you every step of the way! Sources: examples/warm-transfer/hold_music.mp3 | Agent-assisted warm transfer | LiveKit Docs | docs/index.yaml
l
provide code for the warm transfer please
t
I'm happy to share a TypeScript implementation outline for a warm transfer workflow using LiveKit's Node.js SDK. I hope this helps guide you through the process smoothly:
Copy code
import { LiveKitAPI, AccessToken, VideoGrants } from '@livekit/server-sdk';
import { Room } from '@livekit/rtc-node';

// Initialize LiveKit API client
const livekitApi = new LiveKitAPI(
  process.env.LIVEKIT_URL!,
  process.env.LIVEKIT_API_KEY!,
  process.env.LIVEKIT_API_SECRET!
);

interface WarmTransferService {
  startTransfer(callerRoomName: string, supervisorContact: string, conversationHistory: string): Promise<void>;
}

class SessionManager implements WarmTransferService {
  private callerRoom: Room;
  private consultationRoom: Room | null = null;
  private callerStatus: 'active' | 'escalated' | 'inactive' = 'active';
  private supervisorStatus: 'inactive' | 'summarizing' | 'merged' | 'failed' = 'inactive';

  constructor(private callerRoomName: string, private supervisorContact: string, private conversationHistory: string) {
    this.callerRoom = new Room();
  }

  async startTransfer(): Promise<void> {
    if (this.callerStatus !== 'active') return;

    this.callerStatus = 'escalated';
    await this.placeCallerOnHold();

    try {
      // Step 1: Create consultation room for supervisor
      const consultationRoomName = `${this.callerRoomName}-supervisor`;
      this.consultationRoom = new Room();

      // Generate token for transfer agent
      const token = new AccessToken(this.supervisorContact, 'summary-agent');
      token.addGrant({
        roomJoin: true,
        room: consultationRoomName,
        canUpdateOwnMetadata: true,
        canPublish: true,
        canSubscribe: true,
      } as VideoGrants);

      // Connect transfer agent to consultation room
      await this.consultationRoom.connect(process.env.LIVEKIT_URL!, token.toJwt());

      // Dial supervisor using SIP
      await livekitApi.sip.createSIPParticipant({
        sipTrunkId: process.env.LIVEKIT_SIP_OUTBOUND_TRUNK!,
        sipCallTo: this.supervisorContact,
        roomName: consultationRoomName,
        participantIdentity: 'supervisor-sip',
        waitUntilAnswered: true,
      });

      this.supervisorStatus = 'summarizing';
      
      // Here you would implement logic for the summary agent to brief the supervisor
      await this.briefSupervisor();
      
    } catch (error) {
      console.error('Could not start transfer:', error);
      this.callerStatus = 'active';
      await this.handleSupervisorFailure();
    }
  }

  private async placeCallerOnHold(): Promise<void> {
    console.log('Placing caller on hold');
  }

  private async briefSupervisor(): Promise<void> {
    console.log('Briefing supervisor with conversation history:', this.conversationHistory);
  }

  async mergeCalls(): Promise<void> {
    if (this.supervisorStatus !== 'summarizing' || !this.consultationRoom) return;

    try {
      await livekitApi.room.moveParticipant({
        room: this.consultationRoom.name,
        identity: 'supervisor-sip',
        destinationRoom: this.callerRoomName,
      });

      this.resumeCallerAudio();
      console.log('Calls merged successfully');

      if (this.consultationRoom) {
        await this.consultationRoom.disconnect();
      }

    } catch (error) {
      console.error('Could not merge calls:', error);
      await this.handleSupervisorFailure();
    }
  }

  private resumeCallerAudio(): void {
    console.log('Resuming caller audio');
  }

  private async handleSupervisorFailure(): Promise<void> {
    this.supervisorStatus = 'failed';
    console.log('Transfer failed, informing caller');
  }
}
This implementation thoughtfully includes: • Placing the caller on hold to ensure a smooth transition • Creating a dedicated consultation room for the supervisor • Dialing the supervisor using SIP for seamless connection • Providing a clear conversation summary to keep everyone on the same page • Merging the supervisor into the caller's room to complete the transfer If you'd like, I’m here to help walk you through any specific parts of this setup—whether it's configuring environment variables or managing audio for hold music. Just let me know how I can support you! Sources: Agent-assisted warm transfer | LiveKit Docs | examples/README.md
l
this part is performed by the AI agent right?: Providing a clear conversation summary to keep everyone on the same page
t
Absolutely! Usually, the AI agent takes care of providing a clear conversation summary during the warm transfer process. It reviews the conversation history and creates a brief summary to update the supervisor before they join the call. This way, the supervisor is well-informed and can smoothly continue the conversation without any hiccups. If you'd like, I’m here to help you with implementing this feature—whether it’s using an AI service to generate those summaries or figuring out how to integrate it into your workflow. Just let me know, and we’ll work through it together!
l
is it possible to force the summary to include the transferee's name, address city and other data?
t
Absolutely, you can definitely guide the AI agent-generated summary to include specific details like the transferee's name, address, city, and more. There are a few ways to make this happen: • Job Metadata: You can pass user-specific information (like name or city) as metadata right when the session starts. This helps the agent personalize the summary with those details. • Dynamic Data Collection: During the conversation, the agent can use tool calls to gather any missing info and keep it handy in the session context for the summary later on. • Room/Participant Metadata: Setting metadata when creating or joining a room is another way to make sure the agent has access to the right info throughout the session. • External Data Retrieval: You might also connect to external databases or knowledge bases to pull in user-specific details dynamically and include them in the summary. If you’d like, I’m more than happy to walk you through any of these methods with step-by-step guidance or examples. Just let me know which one sounds best for you, and we’ll take it from there! Sources: Job lifecycle | LiveKit Docs | External data and RAG | LiveKit Docs | basics/context_variables.py | examples/voice_agents/realtime_joke_teller.py | examples/src/restaurant_agent.ts