Im noticing an issue in your new Nodejs Agent-js (...
# agents
p
Im noticing an issue in your new Nodejs Agent-js (or im completely just missing the correct way of implementing this), but if you implement a
generateReply()
inside a Tool
execute
, and then return some instructions out of the tool, if you interrupt between the generateReply and the return, the agent will completely freeze and there appears to be no way of getting the Agent back. Its dead at that point. If you interrupt the agent between the
generateReply
and the tool return instructions, the whole agent is unrecoverable. Heres an example:
Copy code
execute: async (_params, context) => {
        const { utterance } = _params as { utterance: string };
        const handle = context.sessionManager.session?.generateReply({
          instructions: `Agent Instructions: You need to say this verbatim to the caller: ${utterance}`,
        });
        await handle?.waitForPlayout();

        // Have the caller interrupt and say something after the generateReply utterance and between the tool return instruction utterance
        return `Agent Instructions: Now inform the caller you are looking into their account settings.`;
      },
➕ 1
Actually its deeper than this, if an Agent speaks, and then listens, and then is thinking how to process what the caller said, So
AgentStateChanged
goes from
speaking ->  listening -> thinking -> Executing LLM tool call -> user interrupts/interjects something during the  Execution
the entire Agent freezes, and is unrecoverable. This is irregardless of using the
generateReply
Copy code
execute: async ({ location }, { ctx }) => {
//simulating an api call            
await new Promise(resolve => setTimeout(resolve, 5000));
            return { weather: 'sunny', temperatureF: 70 };
          },
After further investigating, if i remove Tools altogether and just have a free flowing conversation with many interruptions, the Realtime Agent has no problems, its precisely with how tools are being handled
r
@aloof-notebook-98064 any thoughts on this one?
a
yeah I'm tracking on this one. I'll get back to you once I have a fix
🙏🏼 1
🙏 2
p
@aloof-notebook-98064 Is there a chance that the readlock stream fix that got pushed up today could have anything to do with it?
a
I don't think so . Is it fixed after the latest release?
p
Not entirely sure. I only mentioned because it seemed to throw the stream lock error after every agent utterance, and didn’t know if somehow that could have contributed. This is a pretty big issue, imo, because it renders agents completely useless with tools and interruptions. An unrecoverable agent puts us dead in the water. I appreciate you guys looking into this. Please update us with fix, or further information.
@aloof-notebook-98064 I can confirm the altest Stream lock patch, did NOT fix this issue. We are still experiencing Agent going dead.
a
I just finished fixing this. Will ping you when I merge the fix
p
Greatly appreciate the hard work!
a
Should be out shortly with
1.0.4
🎉 1
p
Thank you
@aloof-notebook-98064 @refined-appointment-81829 Ive been playing around with 1.0.4, maybe you can help me out. It doesnt appear we are able to call generateReply inside a tool now. Docs say we should be able to, but if we do, the generateReply never gets uttered by the Agent, its simply skipped over. If we try to waitForPlayout, on the generateReply, it will throw
Copy code
"Cannot call 'SpeechHandle.waitForPlayout()' from inside the function tool 'submit_answer'. This creates a circular wait: the speech handle is waiting for the function tool to complete, while the function tool is simultaneously waiting for the speech handle.\nTo wait for the assistant's spoken response prior to running this tool, use RunContext.wait_for_playout() instead."
If We then swap out the SpeechHandle for the suggested RunContext, this also never utters the speech. This is especially noticeable if you have some sort of API call inside the Tool after the generateReply. Any thoughts or help?
a
Can you share your tool call code?
p
Yes one sec.
Copy code
execute: async ({ location }, { ctx }) => {
            const handle = ctx.session?.generateReply({
              instructions:
                'AGENT INSTRUCTIONS: You must strictly say, "Got it Batman"',
              toolChoice: 'none',
              allowInterruptions: true,
            });

            await handle?.waitForPlayout();

            return { weather: 'sunny', temperatureF: 70 };
          },
So if i run this, then it throws,
Copy code
function: "lookupWeather"
    speech_id: "speech_cf618276-10b"
    error: "Cannot call 'SpeechHandle.waitForPlayout()' from inside the function tool 'lookupWeather'. This creates a circular wait: the speech handle is waiting for the function tool to complete, while the function tool is simultaneously waiting for the speech handle.\nTo wait for the assistant's spoken response prior to running this tool, use RunContext.wait_for_playout() instead."
So then I try to swap it for
voice.RunContext.waitForPlayout
Copy code
execute: async ({ location }, { ctx }) => {
            ctx.session?.generateReply({
              instructions:
                'AGENT INSTRUCTIONS: You must strictly say, "Got it Batman"',
              toolChoice: 'none',
              allowInterruptions: true,
            });

            await voice.RunContext?.waitForPlayout();

            return { weather: 'sunny', temperatureF: 70 };
          },
But this throws:
Copy code
function: "lookupWeather"
    speech_id: "speech_2107302e-ee6"
    error: "voice.RunContext?.waitForPlayout is not a function"
Guess im lost with how we should be able to have the Agent Generate speech within a tool call now
await voice.RunContext?.waitForPlayout();
->
await ctx.waitForPlayout();
p
This is for a Pipeline flow, not for a realtime flow. So using
say()
will throw error
Copy code
exception occurred while executing tool 
    function: "lookupWeather"
    speech_id: "speech_9a04c081-52b"
    error: "trying to generate speech from text without a TTS model"
Also just swapping our
RunContext
for
ctx.waitForPlayout()
also does not work and skips generateReply speech
Copy code
execute: async ({ location }, { ctx }) => {
            ctx.session?.generateReply({
              instructions:
                'AGENT INSTRUCTIONS: You must strictly say, "Got it Batman"',
              toolChoice: 'none',
              allowInterruptions: true,
            });
            await ctx?.waitForPlayout();

            return { weather: 'sunny', temperatureF: 70 };
          }
Have you all confirmed that you can
generateReply
within a tool post
1.0.4
?
b
Hi, just wanted to throw in here since im facing a very similar (potentiall same issue) here - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1758209628516449 I have tried this and the function response would override the agents reply, so if its a long running function the agent might say the generateReply. A short quick function as above it would likely skip and jump straight to the function response. So i guess the answer is no, the waitForPlayout doesnt actually await
p
This is my finding as well. There also seems to be some nuance with
preambles
no longer being spoken, Tool call return taking precedence over it.
a
@powerful-dog-59275 you mean you're trying with the realtime model?
This example I made with pipeline model works fine.
(thanks for bringing this to our attention btw 🙂 )
p
Yes, we are using the Realtime model.
Appreciate yall looking into this
See Davids Thread above as well, seems like similiar things
a
will do