Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/send-dtmf-active-room.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/agents': minor
---

Add a beta DTMF sending tool that publishes to the active agent session room.
2 changes: 2 additions & 0 deletions agents/src/beta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export { Instructions } from '../llm/index.js';
export {
END_CALL_DESCRIPTION,
createEndCallTool,
sendDtmfEvents,
type DtmfEvent,
type EndCallToolCalledEvent,
type EndCallToolCompletedEvent,
type EndCallToolOptions,
Expand Down
1 change: 1 addition & 0 deletions agents/src/beta/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export {
type EndCallToolCompletedEvent,
type EndCallToolOptions,
} from './end_call.js';
export { sendDtmfEvents, type DtmfEvent } from './send_dtmf.js';
77 changes: 77 additions & 0 deletions agents/src/beta/tools/send_dtmf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { setTimeout as waitFor } from 'node:timers/promises';
import { z } from 'zod';
import { getJobContext } from '../../job.js';
import { tool } from '../../llm/index.js';

const DEFAULT_DTMF_PUBLISH_DELAY = 300;

const DTMF_EVENTS = [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'0',
'*',
'#',
'A',
'B',
'C',
'D',
] as const;

export type DtmfEvent = (typeof DTMF_EVENTS)[number];

function dtmfEventToCode(event: DtmfEvent): number {
if (/^\d$/.test(event)) {
return Number(event);
}
if (event === '*') {
return 10;
}
if (event === '#') {
return 11;
}

return event.charCodeAt(0) - 'A'.charCodeAt(0) + 12;
}

export const sendDtmfEvents = tool({
name: 'send_dtmf_events',
description: `
Send a list of DTMF events to the telephony provider.

Call when:
- User wants to send DTMF events
`,
parameters: z.object({
events: z.array(z.enum(DTMF_EVENTS)).describe('The DTMF events to send.'),
}),
execute: async ({ events }, { ctx }) => {
const room = ctx.session._roomIO?.rtcRoom ?? getJobContext().room;

for (const event of events) {
try {
const code = dtmfEventToCode(event);
if (!room.localParticipant) {
throw new Error('room local participant is not available');
}

await room.localParticipant.publishDtmf(code, event);
await waitFor(DEFAULT_DTMF_PUBLISH_DELAY);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return `Failed to send DTMF event: ${event}. Error: ${message}`;
}
}

return `Successfully sent DTMF events: ${events.join(', ')}`;
},
});
Loading