@ai-matrx/browser-audio

中文文档
0.5.17发布于 2026/10/28MITTypeScriptESM + CJS零依赖近 30 天下载 5.7万

Durable, framework-light browser voice input with IndexedDB recovery, pluggable transcription, and optional React controls.

原始使用文档
文档目录8 节
  1. Install
  2. Framework-free core
  3. AI Matrx adapter
  4. React
  5. Runtime support
  6. Reliability contract
  7. Exports
  8. Development

@ai-matrx/browser-audio

Durable browser voice input with a framework-light core, IndexedDB recovery, pluggable transcription, an optional AI Matrx adapter, and optional React controls.

The package owns the difficult browser lifecycle: microphone permission, MediaRecorder, audio-format negotiation, chunk rotation, local safety writes, ordered live transcription, whole-recording fallback, and recovery after a page closes or a network call fails.

Install

pnpm add @ai-matrx/browser-audio

React controls are optional. Applications importing /react must also install React 18 or newer.

Framework-free core

import {
  BrowserAudioKernel,
  type TranscriptionTransport,
} from "@ai-matrx/browser-audio/core";

const transport: TranscriptionTransport = {
  async transcribe(file, signal) {
    const form = new FormData();
    form.append("file", file);
    const response = await fetch("/api/transcribe", {
      method: "POST",
      body: form,
      signal,
    });
    if (!response.ok) throw new Error("Transcription failed");
    return response.json();
  },
};

export const audio = new BrowserAudioKernel({
  transport,
  onDiagnostic: (diagnostic) => reportDiagnostic(diagnostic),
});

Create one kernel per application. Inputs subscribe to the kernel and identify their recording with a stable ownerId.

AI Matrx adapter

import { BrowserAudioKernel } from "@ai-matrx/browser-audio/core";
import { createMatrxAudioClient } from "@ai-matrx/browser-audio/matrx";

const client = createMatrxAudioClient({
  baseUrl: "https://server.example.com/api",
  getAccessToken: () => auth.getAccessToken(),
  // Optional when /auth/whoami returns organization_id. Supply this when the
  // host already owns an explicit active-organization selector.
  getOrganizationId: () => auth.getActiveOrganizationId(),
});

export const audio = new BrowserAudioKernel({
  transport: client,
  persistence: client,
});

The adapter receives the API base URL and access-token resolver from the host. It imports no application code and stores no credentials. For durable Matrx operations it sends an explicit X-Organization-Id: the host may inject the resolver, otherwise the adapter resolves it through /auth/whoami and caches it only for the current access token.

React

import { VoiceInputButton } from "@ai-matrx/browser-audio/react";

<VoiceInputButton
  kernel={audio}
  ownerId="message-composer"
  value={draft}
  onValueChange={setDraft}
  append
  onBusyChange={setVoiceBusy}
  onError={showVoiceError}
/>

VoiceInputButton is style-neutral, accepts className and style, and allows hosts to replace its icon with renderIcon. useVoiceInput is available when a product needs completely custom presentation.

Runtime support

  • Modern browser applications, including Vite and client-side Next.js code.
  • WebViews that provide MediaRecorder, MediaDevices, IndexedDB, Web Audio, Blob, File, and Fetch.
  • Browser extensions when capture runs in a DOM/offscreen document. Manifest V3 service workers require a host messaging adapter.
  • React Native and native processes require a media/storage adapter; browser capture APIs do not exist there.

Importing the package is SSR-safe. Construct and start the browser kernel in a client runtime.

Reliability contract

  • Every complete audio chunk is written to the safety store before network work.
  • Every displayed live transcript fragment is written to IndexedDB before it is published to the host UI.
  • The first three and six seconds become visible quickly; at ten seconds they are atomically replaced by one higher-context transcription of the opening.
  • AI Matrx remote recovery uses an indexed, owner-scoped journal and rejects incomplete chunk sets instead of transcribing partial audio as complete.
  • Failed live chunks trigger whole-recording transcription.
  • A partial transcript is never delivered as final after a failed fallback.
  • Closing the page leaves a recoverable record.
  • Transcription and persistence are injected contracts; the core never assumes an API, authentication system, database, or framework.
  • Background failures reach onDiagnostic; user-affecting failures reach the recording request and React onError callbacks.

Exports

  • @ai-matrx/browser-audio — framework-free core.
  • @ai-matrx/browser-audio/core — explicit core entry point.
  • @ai-matrx/browser-audio/matrx — AI Matrx HTTP and durability adapter.
  • @ai-matrx/browser-audio/react — React hook and controls.

Development

pnpm typecheck
pnpm test
pnpm check:package

check:package builds ESM JavaScript and declarations, validates the npm surface with Publint, packs the publish artifact, validates that tarball's declarations with Are the Types Wrong, installs it into an empty project, and imports every public entry point.

Never run npm publish from this working directory: workspace development uses source exports that pnpm replaces with dist/ exports while packing. Release the verified .tgz produced by pnpm pack; prepublishOnly blocks the unsafe path.

Automated gates do not substitute for a deployed authenticated microphone canary. Version 0.2.0 was published only after the matching AI Dream recording-journal routes were deployed and that canary passed. Every future release must repeat the deployed canary before registry publication.

版本与发布记录

@ai-matrx/browser-audio 共发布 38 个版本,首次发布于 2026/08/23;过去 12 个月发布了 38 个版本。

版本发布日期距今
0.5.17latest2026/10/288 天前
0.5.162026/10/2611 天前
0.5.152026/10/2512 天前
0.5.142026/10/2512 天前
0.5.132026/10/2512 天前
0.5.122026/10/2412 天前

关于 @ai-matrx/browser-audio 的常见问题

@ai-matrx/browser-audio 是什么?

Durable, framework-light browser voice input with IndexedDB recovery, pluggable transcription, and optional React controls.

如何安装 @ai-matrx/browser-audio?

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

@ai-matrx/browser-audio 支持 TypeScript 吗?

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

@ai-matrx/browser-audio 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ai-matrx/browser-audio 需要什么版本的 Node.js?

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

@ai-matrx/browser-audio 可以商用吗?

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

@ai-matrx/browser-audio 的下载量有多少?

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

@ai-matrx/browser-audio 还在维护吗?

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