#Voice Agent closing the connection after pre-defined time

1 messages · Page 1 of 1 (latest)

naive geyser
#

I am using the Voice Agent API via JavascriptSDK, I want to gently close the connection after N minutes. Before closing the connection I want the agent to inform the user that the connection is about to be closed.

What would be the best way to implement this?

keen lindenBOT
#

Thanks for asking your question. Please be sure to reply with as much detail as possible so the community can assist you efficiently.
-# If you haven't done so, ensure your Discord and Github profiles are linked to Deepgram so you can earn points to redeem on cool stuff just by being active!

#

It looks like we're missing some important information to help debug your issue. Would you mind providing us with the following details in a reply?

  • A request ID that triggered your error or issue.
naive geyser
dire agateBOT
#

Here is an example to consider to create a Voice Agent client with automatic timeout and graceful closure.

const { createClient, AgentEvents } = require('@deepgram/sdk');

class VoiceAgentWithTimeout {
  constructor(apiKey, timeoutMinutes = 10) {
    this.deepgram = createClient(apiKey);
    this.connection = null;
    this.timeoutMinutes = timeoutMinutes;
    this.warningTimeoutId = null;
    this.closeTimeoutId = null;
    this.keepAliveInterval = null;
    this.isClosing = false;

    // Warning given 30 seconds before closure
    this.warningSeconds = 30;
  }

  async connect() {
    try {
      this.connection = this.deepgram.agent();

      // Set up connection event handlers
      this.connection.on(AgentEvents.Open, () => {
        console.log('Voice Agent connection opened');
        this.setupTimeouts();
        this.startKeepAlive();
        this.configureAgent();
      });

      this.connection.on(AgentEvents.Close, () => {
        console.log('Voice Agent connection closed');
        this.cleanup();
      });

      this.connection.on(AgentEvents.Error, (error) => {
        console.error('Voice Agent error:', error);
        this.cleanup();
      });

      this.connection.on(AgentEvents.ConversationText, (data) => {
        console.log(`${data.role}: ${data.content}`);
      });

      this.connection.on(AgentEvents.Audio, (audio) => {
        // Handle audio data from the agent
        this.handleAudioData(audio);
      });

      this.connection.on(AgentEvents.AgentStartedSpeaking, (data) => {
        console.log('Agent started speaking');
      });

      this.connection.on(AgentEvents.AgentAudioDone, () => {
        console.log('Agent finished speaking');
      });

    } catch (error) {
      console.error('Failed to connect to Voice Agent:', error);
      throw error;
    }
  }

  configureAgent() {
    this.connection.configure({
      audio: {
        input: {
          encoding: "linear16",
          sample_rate: 24000,
    ...
keen lindenBOT
#

Hey there @naive geyser~! Did you forget about this thread? Let us know if you still need help!

naive geyser
#

Thanks @sour smelt for the code snippet, unfortunately discord cuts the message after the configureAgent function and I can't see how the whole VoiceAgentWithTimeout works, can you share it again? Thanks!

sour smelt