```import { GlobalConfig } from '@/config/config.t...
# ask-ai
h
Copy code
import { GlobalConfig } from '@/config/config.type';
import { LivekitConfig } from '@/config/livekit/livekit-config.type';
import { generateRandomString } from '@/utils/string/utils';
import { RoomAgentDispatch, RoomConfiguration } from '@livekit/protocol';
import { Room } from '@livekit/rtc-node';
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AccessToken, VideoGrant } from 'livekit-server-sdk';
import { LiveKitDispatchMetadata } from './livekit.type';

@Injectable()
export class LiveKitService {
  private readonly logger = new Logger(LiveKitService.name);
  private readonly livekitConfig: LivekitConfig;

  constructor(private readonly configService: ConfigService<GlobalConfig>) {
    this.livekitConfig = this.configService.getOrThrow('livekit', {
      infer: true,
    });
  }

  async createRoomWithAgentDispatch(metadata: LiveKitDispatchMetadata) {
    const roomName = `room-${generateRandomString()}`;
    try {
      const at = new AccessToken(
        this.livekitConfig.apiKey,
        this.livekitConfig.apiSecret,
        {
          identity: this.livekitConfig.agentName,
        },
      );
      const videoGrant: VideoGrant = {
        room: roomName,
        roomCreate: true,
        roomJoin: true,
        canSubscribe: true,
      };
      at.addGrant(videoGrant);
      at.roomConfig = new RoomConfiguration({
        name: roomName,
        agents: [
          new RoomAgentDispatch({
            agentName: this.livekitConfig.agentName,
            metadata: JSON.stringify(metadata),
          }),
        ],
      });
      const token = await at.toJwt();

      const room = new Room();

      this.logger.debug(`Prepare to connect to room ${roomName}`);
      await room.connect(this.livekitConfig.url, token, {
        autoSubscribe: true,
        dynacast: true,
      });
      this.logger.debug(`Successfully connected to room ${roomName}`);

      return { room, roomName };
    } catch {
      this.logger.error(`Failed to create room ${roomName}`);
      return { room: null, roomName };
    }
  }
}
The createRoomWithAgentDispatch will create a room, dispatch an agent to it The agent will then call the input phone number Currently, the agent speaks and asks a question, then when the user speaks, it doesn't respond I believe this problem is related to this method, check it
t
Thanks for reaching out, tri.nguyen. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you with the issue where the agent doesn’t respond after being dispatched to a room, let’s walk through a few friendly steps together: • Verify Agent Name Configuration: It’s a good idea to double-check that the
agentName
in your configuration exactly matches what’s set in
RoomAgentDispatch
. Even a small mismatch can cause dispatch hiccups. • Check Agent Availability: Make sure your agent is up and running smoothly. Sometimes running multiple instances can help with availability, and it’s worth checking that the server isn’t feeling overwhelmed. • Monitor Logs: Taking a peek at the logs can reveal a lot! Look through the agent and deployment logs to spot any errors or warnings that might be affecting dispatch. • Handle Room Connection: It’s important to have solid room connection logic. Keep an eye on the connection state and handle any disconnections gently to keep things running smoothly. • Dispatch Mechanism: When generating tokens, try using explicit agent dispatch. This helps the agent and user join the room at the same time, which can reduce any unwanted delays. If you’d like, I’m here to help you dive deeper into any of these steps. Just let me know, and we’ll figure it out together! Sources: examples/agent_dispatch/src/main.rs | pkg/service/agent_dispatch_service.go | SIP troubleshooting guide | LiveKit Docs | examples/warm-transfer/warm_transfer.py | Community & Support | livekit/livekit_composite | DeepWiki