Skip to content

feat(react): add useDeepgramAgent hook for Voice Agent API#481

Open
peteroyce wants to merge 1 commit intodeepgram:mainfrom
peteroyce:feat/use-deepgram-agent-hook
Open

feat(react): add useDeepgramAgent hook for Voice Agent API#481
peteroyce wants to merge 1 commit intodeepgram:mainfrom
peteroyce:feat/use-deepgram-agent-hook

Conversation

@peteroyce
Copy link
Copy Markdown

Closes #477

Adds a useDeepgramAgent React hook that wraps the Voice Agent WebSocket API, giving React developers a declarative interface without manual WebSocket management.

Usage

import { useDeepgramAgent } from "@deepgram/sdk/react";

function VoiceAgent() {
  const { connect, disconnect, isConnected, transcript, agentText, error, sendFunctionResult } =
    useDeepgramAgent({
      apiKey: process.env.DEEPGRAM_API_KEY,
      agent: {
        think: { provider: { type: "open_ai" }, model: "gpt-4o-mini" },
        speak: { model: "aura-asteria-en" },
      },
      onFunctionCall: async (name, params) => ({ result: "done" }),
      onTranscript: (text, isFinal) => console.log("User:", text),
      onAgentResponse: (text) => console.log("Agent:", text),
    });

  return (
    <button onClick={isConnected ? disconnect : connect}>
      {isConnected ? "End" : "Start"} Conversation
    </button>
  );
}

What's included

  • src/react/useDeepgramAgent.ts — hook implementation
  • src/react/index.ts — barrel export
  • package.json./react export path + React as optional peer dependency (≥16.8)

State exposed

Value Type Description
connect () => void Opens the WebSocket session
disconnect () => void Closes the session
isConnected boolean WebSocket open and ready
isListening boolean User is speaking
isSpeaking boolean Agent audio is playing
transcript string Latest user transcript
agentText string Latest agent response text
error Error | null Last error, if any
sendFunctionResult (id, result) => void Manual function call response

Notes

  • React is an optional peer dependency — the ./react sub-path is tree-shaken; non-React consumers are unaffected
  • Callback refs are used to avoid stale closure issues with onFunctionCall, onTranscript, and onAgentResponse
  • The hook sends the initial Settings message automatically on open
  • Cleanup on unmount closes the WebSocket automatically

@peteroyce peteroyce requested a review from lukeocodes as a code owner April 10, 2026 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] Add useDeepgramAgent() React hook for Voice Agent API

1 participant