run_tools, run_tool, and finish_round functions (runTools, runTool, and finishRound in Node.js).
Speak while a slow tool runs
A few seconds of silence on a phone call feels like a dropped line. When a lookup runs longer than that, ask for nudges that say a short line, and report the result with one more nudge:- Start the lookup. If the result comes back within a few seconds, submit it, and
finish_roundcontinues the reply as usual. - Otherwise, ask for a nudge that tells the caller that you are still working on it. Repeat it about every 10 seconds, at most two or three times, because the lines start to sound alike.
- When the result arrives, submit it, and ask for a nudge that reports it, as in Report a result later.
continue_reply (continueReply) raises RimeInputError, which finish_round catches, so always report the result with a nudge. Prism also sends no tool_result_timeout for that round, so set your own deadline from the start of the lookup. When the lookup takes too long, submit an error result and report it.
run_slow_tool (runSlowTool in Node.js) says a line after 4 seconds and another 10 seconds after the first line starts. It submits an error result 30 seconds after the lookup starts. The lines run on their own task, so a slow line cannot delay that deadline, and the lines stop if Prism refuses one. Start it in run_tools in place of run_tool. lookup is your own async function, which takes the arguments of the call and returns the result. At the deadline, run_slow_tool cancels the lookup, and runSlowTool aborts the AbortSignal that it passes to lookup as a second argument.
- Python
- Node.js
Delegate long tasks to your agent
A single tool with a clear description works in one call, such as a lookup or a booking. A reply can make several calls, and a continuation can make more, so short chains work too. For work with many steps or long reasoning, give Prism one tool, such asdelegate, that hands the task to your agent. Its description says what your agent can do, and its one argument is the task:
- Python
- Node.js
delegate call with run_slow_tool (runSlowTool in Node.js), and pass your agent as lookup. Raise its 30-second deadline to the longest time that your agent needs. Plan for five things while your agent works:
- Lower the wait: start your agent as soon as the
tool.callevent arrives, asrun_toolsdoes. The tool’s own phrase plays meanwhile. - Keep the caller company: nudges fill the wait, as Speak while a slow tool runs shows. A last nudge reports the outcome.
- Keep task state in your code: your agent and your database hold each task, its progress, and its outcome. Prism’s history is a conversation, not a task log.
- Handle a change of plan: the caller may change the request while your agent works. Prism may then make a new
delegatecall, or only reply, for example to confirm the details. Prism leaves the running task to your code, so cancel or redirect it there. Report only an outcome that matches the latest request. - Check the outcome in code: treat the
tool.callevent and your agent’s result as the record of each action. Confirm outcomes from your agent, not from the reply text.
Handle edge cases
Timeouts and late results
Prism waits for every result of a round, and then for the continuation. Each session publishes both waits insession.info, so read them there instead of hard-coding them. This line prints 90.0 60.0 in Python and 90 60 in Node.js:
- Python
- Node.js
None (null in Node.js) means that Prism did not publish the wait. The SDK cannot tell a missing value from no deadline.
Prism still records a late result, but the result does not start a reply, and continue_reply raises RimeRealtimeError with code tool_continuation_unavailable.
Report a result later
A round can end before it uses a result, because the result comes too late or the caller speaks first. Prism keeps that result in the conversation history, but it mentions the result only if the caller asks. To tell the caller, submit the result, and ask for a nudge that reports it. Like any nudge, it waits until Prism is idle. In this code,call is the tool.call event, and result is the return value of your lookup:
- Python
- Node.js
Next
Errors and limits
Look up each error code of a tool round, such as
tool_result_timeout, and what to do about it.
