@deepgram/browser-agent

中文文档
0.2.1发布于 2025/10/07ISCTypeScriptES Module近 30 天下载 5599

A browser-compatible integration with the Deepgram Voice Agent API.

原始使用文档
文档目录11 节
  1. Installation
  2. Using the main component
  3. Attributes
  4. Properties
  5. Events
  6. Methods
  7. Using the hoop component
  8. Feeding audio data
  9. Contributing
  10. Prerequisites
  11. Running locally

Browser Agent

A pair of experimental web components for integrating with Deepgram's Voice Agent API in a browser environment.

  • The agent component is an all-in-one web component that manages the microphone, websocket, and animation. Add it to any page and get chatting!
  • The hoop component is the animation, standalone. More useful when you've got your own rules for socket integration, and just want the look and feel!

Installation

Install via github by adding to your package.json dependencies:

  "@deepgram/browser-agent": "deepgram/browser-agent#main",

Using the main component

Import the library anywhere for the component to be registered to deepgram-agent:

import "@deepgram/browser-agent";

Then, render it where you like!

  <body>
    <deepgram-agent
      id="dg-agent"
      url="wss://agent.deepgram.com/v1/agent/converse"
      height="300"
      width="300"
      idle-timeout-ms="10000"
      output-sample-rate="24000"
    ></deepgram-agent>
  </body>

Then add a config attribute after some user interaction to start a connection. See more in the Attributes section.

Attributes

  • config (optional): stringified json of a SettingsConfiguration to send the API on initialization
    • Adding or removing the config attribute will start or stop (respectively) the WebSocket connection to the Deepgram API.
    • Because this web component directly manages the user's microphone, it requires a user action to attempt a connection. For that reason, you most likely want to first render the element without a config.
    • For better early API flexibility, there is no validation. Use our docs to ensure your configuration matches.
    • Whenever deepgram-agent disconnects, unset the config and wait for another user interaction to set it and retrigger connection.
  • width (optional, default = "0"): the width of the canvas for agent animation
    • The animation will always take up a (roughly) square area, so this should typically be the same value as height.
  • height (optional, default = "0"): the height of the canvas for agent animation
    • The animation will always take up a (roughly) square area, so this should typically be the same value as width.
  • auth-scheme (optional, default = "bearer"): the auth scheme to use with your token
    • Use bearer for the Deepgram API when working with token-based authentication. For local development you may find it more convenient to use an API key (token scheme). Never use API keys in a production browser application!
  • url (required): The API url
    • Chances are you'll set this to "https://api.deepgram.com/v1/agent"!
  • idle-timeout-ms (optional): how long to wait for user idleness before closing the socket
    • Timer starts whenever the user is expected to speak (meaning right when opening the connection, and right after each AgentAudioDone event).
  • output-sample-rate: the output sample rate you'd like for playback
    • Should be the same as the output rate you've got in your Settings object. Unless you're trying to have a little fun.

Properties

  • token (optional): the token to use for accessing the Deepgram /agent API. See the token-based auth docs for how to create safe-for-browser tokens.
    • If not provided, the auth-scheme will also be ignored. Only makes sense if your API URL is unauthenticated.

Events

As an experimental tech, the deepgram-agent element emits a variety of events. You're more likely to run into some than others.

Common events

  • "no url": emitted when trying to connect and API url is missing
  • "no config": emitted when trying to connect and config is missing
  • "invalid auth": emitted when trying to connect and the WebSocket rejects the auth scheme or token
  • "socket open": socket successfully opened
  • "socket close": socket successfully closed
  • "connection timeout": socket failed to connect due to a timeout (10s)
  • "failed to connect user media": couldn't gain access to user's microphone, usually due to permission rejection
  • "structured message": got JSON from the API. This is the main event to pay attention to!
  • "client message": sent a JSON message to the API. Useful for debugging.

Uncommon events

  • "failed setup": some issue internal to the custom element occurred
  • "empty audio": got an empty message when expecting audio data
  • "unknown message": got a text message from the API that isn't valid JSON

Methods

sendClientMessage(message: ArrayBuffer | string): void {}

Use this to send some (stringified) JSON or binary data to the server. Ignored when the websocket is closed.

connect(): Promise<void> {}

Use this to explicitly connect. Prefer to handle this by setting the config attribute.

disconnect(reason?: string): Promise<void> {}

Use this to explicitly disconnect. Prefer to handle this by removing the config attribute.

Using the hoop component

The animation alone is available as a granular import, automatically registered as deepgram-hoop:

import "@deepgram/browser-agent/hoop";

Then, render it where you like!

  <body>
    <deepgram-hoop
      id="dg-hoop"
      height="300"
      width="300"
      status="active"
    ></deepgram-hoop>
  </body>

Feeding audio data

The hoop component applies some size oscillation based on audio information:

  • The output, i.e. agent audio (agent-volume attribute) expands
  • The input, i.e. user audio (user-volume attribute) collapses

To ease jitter, each drawn arc trails behind a leader. You must provide amplitude data for both the user and agent on a per-frame basis. See the sendVolumeUpdates function for a working example.

Contributing

Prerequisites

Running locally

Use npm run vite to start a dev server. You'll need to set a DG_API_KEY environment variable in order to open a connection.

版本与发布记录

@deepgram/browser-agent 共发布 6 个版本,首次发布于 2025/06/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.1latest2025/10/071.1 年前
0.2.02025/08/091.3 年前
0.1.02025/08/051.3 年前
0.1.0-alpha.2next2025/07/121.3 年前
0.1.0-alpha.12025/06/191.4 年前
0.1.0-alpha.02025/06/071.4 年前

关于 @deepgram/browser-agent 的常见问题

@deepgram/browser-agent 是什么?

A browser-compatible integration with the Deepgram Voice Agent API.

如何安装 @deepgram/browser-agent?

在项目目录运行 npm install @deepgram/browser-agent,或使用 pnpm add / yarn add / bun add。

@deepgram/browser-agent 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@deepgram/browser-agent 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@deepgram/browser-agent 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@deepgram/browser-agent 可以商用吗?

@deepgram/browser-agent 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@deepgram/browser-agent 的下载量有多少?

根据 npm 官方下载统计,@deepgram/browser-agent 在 2026/10/12 – 2026/11/10 这 30 天内被下载 5,599 次,日均约 187 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@deepgram/browser-agent 还在维护吗?

最新版本 0.2.1 发布于 2025/10/07(1.1 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/06/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。