Skip to content

Agent stops randomly while being stuck in listening mode #336

@toto1384

Description

@toto1384

I've got an 11labs agent that gets stuck in listening mode and stops without any warnings

Image

The code

    const conversation = useConversation({

        clientTools: { ...setPropertyFiltersFunctions, ...postPropertyFunctions, },
        dynamicVariables,
        volume: 0.9,
        onDebug: (message) => console.log('Debug:', message),

        onStatusChange(prop) {
            console.log('Status changed:', prop)
        },
        overrides: {
            agent: {
                language: locale ? ((locale.includes('-') ? locale.split('-')[0] : locale) as 'en' | 'ro') : 'ro',
            },
        },
    });


    const startConversation = useCallback(async () => {

        async function start(token: string) {
            const str = await conversation.startSession({
                onMessage: (message) => {
                    setMessages((prev) => [...prev, { ...message, id: nanoid() }])
                },
                // agentId: process.env.AGENT_ID!, // Replace with your agent I
                conversationToken: token,
                connectionType: 'webrtc', // either "webrtc" or "websocket"
                onError(message, context) {
                    console.log('Error:', message, context)
                },
                onConnect({ conversationId }) {
                    console.log('Connected', conversationId)
                },
                onStatusChange(prop) {
                    console.log('Status changed:', prop)
                },
                onAgentToolResponse(message: any) {
                    console.log('Agent tool response:', message)
                },
                onDisconnect: () => {
                    console.log('Disconnected')
                },
                onModeChange(prop) {
                    console.log('Mode changed:', prop)
                },

                // userId: 'YOUR_CUSTOMER_USER_ID' // Optional field for tracking your end user IDs
            });

            console.log(str)
        }
        console.log('start conversation', conversationToken)
        try {
            // Request microphone permission
            await navigator.mediaDevices.getUserMedia({ audio: true });
            await start(tokenQuery.data.token);
            // Start the conversation with your agent
        } catch (error) {
            console.log('Failed to start conversation:', { error });
            if ((error as any).status == 401) {
                tokenQuery.refetch().then(_ => start(_.data?.token ?? ''));
            }
        }
    }, [conversation]);


// somewhere else in the code :  `await startConversation()`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions