Skip to main content

Overview

Replay’s AI agents support Remote Procedure Call (RPC) methods through LiveKit’s RPC framework. These methods allow you to request data from the agent during an active conversation, such as retrieving the conversation transcript.
Want to learn more about LiveKit RPC? Check out the official LiveKit RPC documentation for in-depth information about the underlying technology.

Available RPC Methods

get_agent_transcript

Retrieves the full conversation transcript from the AI agent, including all messages exchanged between the user and the agent.

Use Cases

  • Retrieving a full transcript at any point during the call
  • Saving conversation records when a user ends the call

How to Invoke

Use LiveKit’s native performRpc method on the local participant:
// Get the agent's identity from the room participants
// The agent is typically identified by a participant with isAgent = true
const agent = room.remoteParticipants.values().next().value;

if (!agent) {
  throw new Error('Agent not found in room');
}

// Perform the RPC call
const response = await room.localParticipant.performRpc({
  destinationIdentity: agent.identity,
  method: 'get_agent_transcript',
  payload: '', // Empty payload for this method
});

// Parse the response
const parsedResponse = JSON.parse(response);

if (parsedResponse.error) {
  throw new Error(parsedResponse.error);
}

console.log('Transcript:', parsedResponse);
// Handle the transcript data
Method Name: The RPC method name is get_agent_transcript (all lowercase with underscores).

Return Signature

The method returns a TranscriptResponse object with the following structure:
interface TranscriptResponse {
  snippets: TranscriptSnippet[];
  createdAt: number;
}

interface TranscriptSnippet {
  speakerName: string;  // Display name of speaker (e.g., "John Doe", "Karen - Investor @ Qualtrics")
  text: string;          // The transcribed text content
  createdAt: number;     // Unix timestamp in milliseconds when message was created
  isUser: boolean;       // true if speaker is user, false if assistant
}

Response Fields

FieldTypeDescription
snippetsTranscriptSnippet[]Array of conversation messages in chronological order
createdAtnumberUnix timestamp (milliseconds) when the conversation was started

TranscriptSnippet Fields

FieldTypeDescription
speakerNamestringDisplay name of the speaker
textstringThe actual transcribed message content
createdAtnumberUnix timestamp (milliseconds) when the completed message was added to the transcript array. Typically corresponds to the time a user or agent finished speaking their snippet.
isUserbooleantrue if the message is from the user, false if from the agent

Example Response

{
  "snippets": [
    {
      "speakerName": "Bobby the Buyer (PM @ Google)",
      "text": "Hello, I'd like to learn more about your product.",
      "createdAt": 1730304000000,
      "isUser": false
    },
    {
      "speakerName": "Sarah the Seller",
      "text": "Of course! I'd be happy to help. What specific features are you interested in?",
      "createdAt": 1730304003000,
      "isUser": true
    },
    {
      "speakerName": "Bobby the Buyer (PM @ Google)",
      "text": "I'm particularly interested in the analytics capabilities.",
      "createdAt": 1730304008000,
      "isUser": false
    }
  ],
  "createdAt": 1730304010000
}

end_room

Ends the current LiveKit room session, stopping all active egresses (audio/video recordings) before deleting the room.

Use Cases

  • Programmatically ending a roleplay session when a user clicks an end call button
  • Gracefully shutting down a session with proper cleanup
  • Stopping recordings and freeing up resources

How to Invoke

Use LiveKit’s native performRpc method on the local participant:
// Get the agent's identity from the room participants
// The agent identity follows the pattern: {display_name}::{roleplay_id}::{timestamp}
// You can find the agent's identity from room.remoteParticipants - look for the participant that matches your agent's display name
const agent = Array.from(room.remoteParticipants.values()).find(p => p.isAgent);

if (!agent) {
  throw new Error('Agent not found in room');
}

// Perform the RPC call with optional reason
const response = await room.localParticipant.performRpc({
  destinationIdentity: agent.identity,
  method: 'end_room',
  payload: JSON.stringify({ reason: 'User clicked end call button' }), // Optional - can also use empty string ""
});

// Parse the response
const parsedResponse = JSON.parse(response);

if (!parsedResponse.success) {
  throw new Error('Failed to acknowledge end room request');
}

console.log('End room request acknowledged:', parsedResponse.message);
// Note: The room will be terminated asynchronously after this response

Payload

The payload is optional and can be either:
  • With reason (recommended): JSON.stringify({ reason: "User clicked end call button" })
  • Empty string: "" (if no reason is needed)

Return Signature

The method returns an EndRoomResponse object with the following structure:
interface EndRoomResponse {
  success: boolean;
  message: string;  // Confirmation message that the request was acknowledged
}

Response Fields

FieldTypeDescription
successbooleantrue if the end room request was successfully acknowledged
messagestringConfirmation message indicating the request was received (e.g., “End room request acknowledged”)

Important Note on Response Timing

Why an immediate acknowledgment? The end_room RPC method returns an immediate acknowledgment response rather than waiting for the room to actually end. This is because once the room is ended, no RPC methods can be sent through the room connection (since the room no longer exists). The actual room termination process happens asynchronously after the acknowledgment is returned, including stopping all active egresses (audio/video recordings) and deleting the room.

Example Response

Success Response:
{
  "success": true,
  "message": "End room request acknowledged"
}

Example Usage

try {
  const agent = Array.from(room.remoteParticipants.values()).find(p => p.isAgent);
  
  if (!agent) {
    throw new Error('Agent not found in room');
  }

  // End the room with a reason
  const response = await room.localParticipant.performRpc({
    destinationIdentity: agent.identity,
    method: 'end_room',
    payload: JSON.stringify({ reason: 'User ended session' }),
  });

  const result = JSON.parse(response);
  
  if (result.success) {
    console.log('End room request acknowledged:', result.message);
    // Handle cleanup, navigation, etc.
    // Note: The room will be terminated asynchronously after this response
  } else {
    console.error('Failed to acknowledge end room request');
  }
} catch (error) {
  console.error('Error ending room:', error.message);
}
Method Name: The RPC method name is end_room (all lowercase with underscores).Agent Identity: The agent identity follows the pattern: {display_name}::{roleplay_id}::{timestamp}. You can find the agent’s identity from room.remoteParticipants by looking for the participant that matches your agent’s display name or has isAgent set to true.
Important Considerations:
  • This method returns an immediate acknowledgment; the actual room termination happens asynchronously
  • The method stops all active egresses (audio/video recordings) before deleting the room (asynchronously)
  • Ensure the user is connected to a LiveKit room before calling this method
  • The AI agent must be active and connected to the room
  • After receiving the acknowledgment, you should handle cleanup and navigation in your application
  • Always implement proper error handling for production applications

Error Handling

RPC calls may fail for several reasons. Always implement proper error handling:
try {
  const agent = Array.from(room.remoteParticipants.values())[0];
  
  if (!agent) {
    throw new Error('Agent not found in room');
  }

  const response = await room.localParticipant.performRpc({
    destinationIdentity: agent.identity,
    method: 'get_agent_transcript',
    payload: '',
  });

  const parsedResponse = JSON.parse(response);
  
  if (parsedResponse.error) {
    throw new Error(parsedResponse.error);
  }

  // Handle success
  console.log('Transcript:', parsedResponse);
} catch (error) {
  // Common error scenarios:
  // - "Agent not found in room" - No agent connected
  // - "Room or local participant not found" - User not in a room
  // - Network or timeout errors
  // - JSON parsing errors
  console.error('RPC Error:', error.message);
}
Important Considerations:
  • Ensure the user is connected to a LiveKit room before calling RPC methods
  • The AI agent must be active and connected to the room
  • RPC calls are asynchronous and may take time depending on transcript length
  • Always implement timeout handling for production applications

Need Help? Contact our support team ([email protected]) for assistance with implementing RPC methods in your application.