@fonoster/voice

中文文档
0.61.0发布于 2026/11/15MITTypeScriptCommonJS构建来源已验证近 30 天下载 8971

Session SDK for Fonoster voice applications

原始使用文档
文档目录28 节
  1. Installation
  2. Example
  3. The session
  4. Session
  5. session.midTurn
  6. session.turnExpired
  7. session.say()
  8. session.stopSay()
  9. session.gather()
  10. session.listen()
  11. session.subscribe()
  12. session.transfer()
  13. session.completeTransfer()
  14. session.handoff()
  15. session.hangup()
  16. session.note()
  17. session.reportUsage()
  18. session.answer()
  19. session.receive()
  20. session.closed()
  21. session.command()
  22. session.bargeIn()
  23. VoiceServer
  24. voiceServer.listen()
  25. voiceServer.attach()
  26. voiceServer.handleUpgrade()
  27. SessionError
  28. SessionEndedError

voice

Voice Version Downloads/week License

The session SDK for Fonoster voice applications. For every call, Fonoster's AppBridge opens one signed WebSocket session to your app; VoiceServer accepts it and runs your handler with the session. The protocol is in docs/session-protocol.md, the full SDK guide in docs/voice-sdk.md, and runnable examples in examples/.

Installation

$ npm install --save @fonoster/voice

Example

import { VoiceServer } from "@fonoster/voice";

// AppBridge signs every connection with the app's signing secret.
new VoiceServer({ port: 3000, signingSecret: process.env.APP_SIGNING_SECRET }).listen(
  async (session) => {
    await session.say("Hi there! What's your name?");
    const { speech: name } = await session.gather();
    await session.say(`Nice to meet you ${name}!`);

    await session.say("Please enter your 4 digit pin.");
    const { digits } = await session.gather({ maxDigits: 4, finishOnKey: "#" });
    await session.say(`Your pin is ${digits}`);

    await session.hangup();
  }
);

attach(server, handler) mounts the same server on an existing HTTP or Express server.

The session

MethodDoes
say(text | asyncIterable, options?)Speaks; resolves with { interrupted, heardText, heardMs }
gather(options?)Waits for speech or keypresses (maxDigits, finishOnKey, timeout)
play(url), playDtmf(digits)Plays media or DTMF tones
transfer(to, { mode }), completeTransfer()Cold, warm or refer transfer
hold(), resume(), handoff(app, payload), hangup()Call control
audio.onFrame, audio.send, audio.mark, audio.clearRaw call audio
on(event, cb), subscribe(events)Turns, partial transcripts, keypresses

Fields: callRef, appRef, from, to, direction, metadata.

Coming from the gRPC SDK ((req, res) handlers, VoiceResponse, stream, sgather, dial)? Those are gone; see docs/migrating-to-sessions.md. @fonoster/voice/session resolves to this package for one more release.

Session

One call's session, built on the fonoster.session.v1 messages (voice-sdk S8). Verbs are conveniences: each sends one command and resolves on the event that answers it.

Kind: global class

session.midTurn

The caller is mid-turn: a turn.user_started with no turn.ended yet.

Kind: instance property of Session

session.turnExpired

A gather gave up on the current turn; its late turn.ended is dropped, not buffered.

Kind: instance property of Session

session.say()

Speaks text, or a stream of text pieces as one utterance, and reports what was heard.

Kind: instance method of Session

session.stopSay()

Cuts the agent off: every pending say and play resolves as interrupted.

Kind: instance method of Session

session.gather()

Waits for speech (turn.ended) or digits (dtmf), whichever comes first.

Kind: instance method of Session

session.listen()

Starts listening or changes how AppBridge listens (listen.update): recognition language, hints, and the turn-taking patience knob (low, normal or high).

Kind: instance method of Session

session.subscribe()

Requests opt-in events: "turn.estimate", "transcript.partial".

Kind: instance method of Session

session.transfer()

Transfers the call; resolves when AppBridge ends the session with reason transfer.

Kind: instance method of Session

session.completeTransfer()

Completes the pending warm transfer: the agent leaves, caller and new party stay. Resolves when the session ends with reason transfer; rejects with a SessionError (transfer_not_answered) when the new party has not answered yet, which leaves the transfer pending: wait for the transfer.answered event and call it again.

Kind: instance method of Session

session.handoff()

Hands the call to another app with a payload; resolves when this session ends.

Kind: instance method of Session

session.hangup()

Hangs up; resolves when the session ends. failure ends the call as failed in the call record (@internal: reserved for Fonoster's own apps (Autopilot); AppBridge ignores it from others).

Kind: instance method of Session

session.note()

Adds a note to the call record (record.note).

Kind: instance method of Session
Internal: Reserved for Fonoster's own apps (Autopilot); not part of the public SDK and may change.

session.reportUsage()

Adds model token usage to the call (usage.report, add-usage-metering).

Kind: instance method of Session
Internal: Reserved for Fonoster's own Autopilot; AppBridge refuses it from other apps.

session.answer()

Resolves at once: AppBridge answers the call on connect (voice-sdk S2).

Kind: instance method of Session

session.receive()

Feeds one frame from the socket.

Kind: instance method of Session

session.closed()

The socket closed.

Kind: instance method of Session

session.command()

Sends a command whose only answer is a possible error (logged as a session error).

Kind: instance method of Session

session.bargeIn()

The app owns interruption (appbridge B22): when the caller starts talking over an interruptible say or play, the SDK cuts it off with clear; a non-interruptible one keeps playing.

Kind: instance method of Session

VoiceServer

Accepts AppBridge sessions over WebSocket and runs the handler once per session (voice-sdk, "VoiceServer accepts AppBridge sessions over WebSocket").

Kind: global class

voiceServer.listen()

Starts a standalone server. Resolves once it is listening.

Kind: instance method of VoiceServer

voiceServer.attach()

Mounts on an existing HTTP server (Express included): sessions arrive on path.

Kind: instance method of VoiceServer

voiceServer.handleUpgrade()

Handles one upgrade request: verifies the signature, then runs a session.

Kind: instance method of VoiceServer

SessionError

A command failed: AppBridge sent error with relatesTo set to the command's id.

Kind: global class

SessionEndedError

The session ended (or a fatal error arrived) while the command was pending.

Kind: global class

版本与发布记录

@fonoster/voice 共发布 334 个版本,首次发布于 2021/11/01;过去 12 个月发布了 70 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.61.0latest2026/11/15今天
0.60.02026/11/151 天前
0.59.02026/11/141 天前
0.58.02026/11/141 天前
0.57.02026/11/114 天前
0.56.02026/11/114 天前

其他发布通道:next → 0.9.51。

关于 @fonoster/voice 的常见问题

@fonoster/voice 是什么?

Session SDK for Fonoster voice applications

如何安装 @fonoster/voice?

在项目目录运行 npm install @fonoster/voice,或使用 pnpm add / yarn add / bun add。

@fonoster/voice 支持 TypeScript 吗?

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

@fonoster/voice 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fonoster/voice 可以商用吗?

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

@fonoster/voice 的下载量有多少?

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

@fonoster/voice 还在维护吗?

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