npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@shulex/voice-client

v0.1.0

Published

Shulex Twilio Voice 客户端封装

Readme

@shulex/voice-client

Shulex Twilio Voice 客户端封装,提供开箱即用的 UI(来电通知 / 拨打面板 / 通话中浮窗),支持 qiankun 主子应用、多 tab、多浏览器协作。

特性

  • 上线 / 下线 / 拨打 / 接听 一站式 API
  • 跨 tab 单 leader 选主(Web Locks),只在 leader tab 持有 Twilio Device
  • 跨 qiankun 主子应用单例,任意应用 import 都拿到同一对象
  • UI 全部在 Shadow DOM 内渲染(react-shadow + tailwind),不污染主项目样式
  • 三个内置面板:来电通知、拨打面板、通话中浮窗

安装

monorepo 内已通过 yarn workspaces 关联,直接 import 即可:

import { voiceClient, VoiceProvider, VoiceRoot } from '@shulex/voice-client';

快速开始

1. 主应用顶层挂 Provider

// src/layouts/root/index.tsx
import { VoiceProvider, VoiceRoot } from '@shulex/voice-client';

function Root() {
  const supportPhone = useUserStore((s) => s.supportPhone);
  const userId = useUserStore((s) => s.userId);

  return (
    <VoiceProvider config={{ supportPhone, agentId: userId }}>
      <Outlet />
      <VoiceRoot />
    </VoiceProvider>
  );
}

2. 任意子应用 / 任意位置发起拨打

import { voiceClient } from '@shulex/voice-client';

await voiceClient.call({ customerPhone: '+13109876543' });

3. 打开拨打面板让用户输入号码

voiceClient.openDialer({ defaultPhone: '+1' });

API

<VoiceProvider config>

只在主应用挂一次,初始化单例。

| 字段 | 类型 | 默认 | 说明 | | ------------------- | -------------------- | ------- | --------------------- | | supportPhone | string | — | 必填,坐席的支持电话号 | | agentId | number | — | 当前坐席 ID | | autoOnline | boolean | true | Provider 挂载即上线 | | ringtone | boolean | true | 来电铃声 | | heartbeatInterval | number | 30000 | 心跳间隔(ms) | | debug | boolean | false | 调试日志 | | onCallStateChange | (snapshot) => void | — | 通话状态变化回调 |

<VoiceRoot/>

UI 容器,挂在主应用顶层。三个面板(来电通知 / 拨打面板 / 通话中浮窗)的渲染入口。

voiceClient

全局单例,所有方法跨 tab / 跨 qiankun 一致。

| 方法 | 返回 | 说明 | | ------------------------------- | ----------------------- | ------------------------- | | online() | Promise<void> | 主动上线 | | offline() | Promise<void> | 主动下线 | | call(params) | Promise<{ callSid? }> | 外呼 | | accept() | void | 接听当前来电 | | reject() | void | 拒接 | | hangup() | void | 挂断 | | toggleMute() | boolean | 静音切换,返回切换后状态 | | toggleSpeaker() | boolean | 免提切换 | | openDialer({ defaultPhone? }) | void | 打开拨打面板 | | closeDialer() | void | 关闭拨打面板 | | on(event, handler) | () => void | 订阅事件,返回取消订阅函数 |

call 入参:

{
  customerPhone: string;       // 必填
  supportPhone?: string;       // 默认从 Provider config 取
  agentId?: number;            // 默认从 Provider config 取
  meta?: Record<string, any>;  // 扩展位
}

call 内部只串「置忙 → connect」两步,不参与任何业务侧 API(包括创建工单)。 对方接通时触发 call:outboundAnswered 事件;上层在事件回调里自己调业务接口(如外呼工单创建),voice-client 不感知 host 的业务模型。响铃但未接通不会触发该事件,因此「振铃即建工单」之类的副作用要避免,应在接通后再做。

Hooks

  • useVoiceState() — { status, isLeader, error }
  • useCallState() — { status, call?, durationMs, isLeader }
  • useIncomingCall() — Call | null
  • useDialerOpen() — boolean

事件

| 事件 | 数据 | | ----------------------- | --------------------------------------------------------------- | | state | CallSnapshot | | call:incoming | Call | | call:answered | Call | | call:ended | Call & { durationMs } | | call:outboundAnswered | { callSid, customerPhone, supportPhone, agentId?, startedAt } | | online | — | | offline | { reason? } | | leader:changed | { isLeader } | | error | Error |

错误类

  • VoiceBusyError — 置忙接口返回 false
  • VoiceTokenError — 获取 token 失败
  • VoiceDeviceError — Twilio Device 错误
  • VoiceUnsupportedError — 当前环境不支持
  • VoiceApiError — 接口业务错误,带 code

跨 qiankun 主子应用使用

子应用拨打 + 主应用接听是典型场景:

// Desk 子应用某个按钮
import { voiceClient } from '@shulex/voice-client';

<Button onClick={() => voiceClient.call({ customerPhone })}>联系客户</Button>;
// 主应用 layouts/root 监听对方接通,自行调业务侧接口(如创建工单)
import { voiceClient } from '@shulex/voice-client';
import { outboundCreateTicket } from '@/services/voice'; // host 自己的 service

voiceClient.on(
  'call:outboundAnswered',
  async ({ callSid, customerPhone, supportPhone, agentId }) => {
    const { ticketNumber } = await outboundCreateTicket({
      supportPhone,
      externalId: callSid,
      customerPhone,
      agentId,
    });
    refreshTicketList(ticketNumber);
  },
);

由于 voiceClient 是 window.__voice_client__ 全局单例,主子应用 import 拿到同一对象,直接调用即可。

多 tab / 多浏览器协作

  • 同浏览器多 tab: 通过 Web Locks 选主,只有 leader tab 注册 Twilio Device。Follower tab 通过 BroadcastChannel 转发命令、同步状态。Leader tab 关闭后下一个 tab 自动接管。
  • 同用户多浏览器: Twilio 服务端最后注册者覆盖,前者收到 unregistered,UI 显示「已在他处登录」。本子包不做主动协调。
  • 通话中切换 tab: WebRTC 媒体连接是 tab 级,关闭 leader tab 通话立即结束。子包在 beforeunload 主动 hangup。

浏览器兼容

  • 需要 WebRTC、Web Locks、BroadcastChannel
  • 现代 Chromium / Firefox / Safari ≥ 15 支持
  • 不支持环境进入 unsupported 状态,UI 全部隐藏

License

MIT