kivii-public-components
kivii-public-components
VoiceInputButton
语音输入按钮集成了浏览器录音、Sherpa-ONNX Silero VAD 静音检测和音频转写。
基本用法
<script setup lang="ts">
import { ref } from 'vue'
import { VoiceInputButton } from 'kivii-public-components'
const value = ref('')
const transcriptionEndpoint = import.meta.env.DEV
? '/asr-api/audio/transcriptions'
: 'https://api.kivii.com/v1/audio/transcriptions'
</script>
<template>
<VoiceInputButton
v-model="value"
:endpoint="transcriptionEndpoint"
:auto-stop-seconds="6"
@error="console.error"
@transcribed="text => console.log('转写完成', text)"
/>
</template>
Vite 资源插件
语音组件依赖 sherpa-vad 运行资源。在宿主项目的 vite.config.ts 注册资源插件,
插件会在开发环境直接提供文件,并在生产构建时自动复制到 /sherpa-vad/:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { kiviiVoiceAssets } from 'kivii-public-components/vite'
export default defineConfig({
plugins: [vue(), kiviiVoiceAssets()],
})
插件在 Vite 开发和预览服务器中默认添加 SharedArrayBuffer、WASM Worker 所需的
跨域隔离响应头,VAD 资源响应也会包含这些标头:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: credentialless
Cross-Origin-Resource-Policy: same-origin
无法提供 SharedArrayBuffer 或跨域隔离的浏览器会自动切换为 Web Audio
兼容录音模式,麦克风采集和转写仍可使用;Sherpa VAD 可用时则继续提供更准确的语音分段。
如果部署平台统一管理这些响应头,可以使用
kiviiVoiceAssets({ crossOriginHeaders: false })。自定义资源路径时,需要让插件的
publicPath 与组件的 vad-base-url 保持一致。
| 插件配置 | 类型 | 默认值 | 说明 |
|---|---|---|---|
publicPath |
string |
'/sherpa-vad/' |
开发服务器访问路径及生产构建输出子目录 |
crossOriginHeaders |
boolean |
true |
是否为 Vite dev/preview 及 VAD 资源响应添加跨域隔离标头 |
自定义路径示例:
// vite.config.ts
kiviiVoiceAssets({ publicPath: '/assets/kivii-vad/' })
<VoiceInputButton vad-base-url="/assets/kivii-vad/" />
生产部署注意:插件负责把 VAD 文件复制进构建产物,但无法修改 Nginx、IIS 或 CDN 的 HTTP 响应配置。生产服务器必须为页面及
/sherpa-vad/资源配置上述三个响应头, 否则浏览器会拦截 WASM Worker,组件将显示错误状态。
VoiceInputButton Props
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
v-model |
string |
'' |
输入框文本;转写成功后自动更新 |
append |
boolean |
true |
是否把新转写文本追加到现有 v-model 内容后 |
separator |
string |
' ' |
追加文本时使用的分隔符 |
autoStopSeconds |
number |
6 |
检测到连续静音多少秒后自动停止录音 |
disabled |
boolean |
false |
禁用按钮;录音期间变为 true 时会取消本次录音 |
endpoint |
string |
生产接口 | 音频转写 API 地址;开发环境使用代理时应显式传入代理路径 |
model |
string |
'local-qwen3-asr-1.7b' |
提交给转写接口的模型名称 |
apiKey |
string |
undefined |
可选 Bearer Token;不建议在生产浏览器代码中直接传入密钥 |
vadBaseUrl |
string |
'/sherpa-vad/' |
Sherpa VAD 静态资源地址,必须与插件 publicPath 一致 |
默认生产转写接口为 https://api.kivii.com/v1/audio/transcriptions。开发环境如需通过
Vite 代理隐藏 API Key,应将 endpoint 设置为代理路径,并由 server.proxy 注入认证头。
Events
| 事件 | 参数 | 说明 |
|---|---|---|
transcribed |
text: string |
单段语音转写成功 |
recording-change |
recording: boolean |
录音状态变化 |
speech-change |
detected: boolean |
VAD 检测到语音/静音状态变化 |
transcribing-change |
transcribing: boolean |
转写请求状态变化 |
error |
message: string |
初始化、录音或转写失败;按钮的 title 同时显示错误信息 |
组件通过模板 ref 暴露 cancel() 方法,可主动取消当前录音:
<script setup lang="ts">
import { ref } from 'vue'
import { VoiceInputButton } from 'kivii-public-components'
const voiceButton = ref<InstanceType<typeof VoiceInputButton>>()
function cancelVoiceInput() {
voiceButton.value?.cancel()
}
</script>
<template>
<VoiceInputButton ref="voiceButton" />
</template>
运行要求
- Vue 3.5 或更高版本。
- 支持 Web Audio API 和
getUserMedia的现代浏览器。 - 除
localhost外,麦克风访问需要 HTTPS 安全上下文。 SharedArrayBuffer与跨域隔离用于启用 Sherpa VAD;不可用时自动进入兼容录音模式。- 浏览器首次使用时,用户必须授权麦克风权限。
基于 Vue 3 + Naive UI 的公共组件库,提供开箱即用的数据表格组件,支持搜索、分页、列配置、增删改等能力。
特性
- 自包含设计,无需宿主安装或配置 Naive UI
- 支持动态列配置、搜索字段配置
- 内置分页、排序、筛选
- 支持自定义动作、数据转换规则
- 支持暗色/亮色主题
安装
# npm
npm install kivii-public-components
# pnpm
pnpm add kivii-public-components
# yarn
yarn add kivii-public-components
前置依赖:宿主项目需要安装 Vue 3(组件本身已内置 Naive UI,无需额外安装)。
pnpm add vue
快速开始
1. 引入样式
在项目入口文件(main.ts 或根组件)中引入 CSS:
import 'kivii-public-components/style'
2. 使用组件
<template>
<TableIndex :uiConfig="config" />
</template>
<script setup lang="ts">
import { TableIndex } from 'kivii-public-components'
const config = {
Type: 'YourApp.Entities.Invoice',
InternalCode: '发票管理',
IsDefault: true,
InitQuery: '/api/Invoice/Query.json',
GetUrl: '/api/UiConfig/Get.json',
SetUrl: '/api/UiConfig/Set.json',
}
</script>
API
TableIndex Props
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
uiConfig |
UiConfig |
否 | 表格配置对象,包含接口地址和实体类型 |
UiConfig 配置项
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
Type |
string |
是 | 实体类型标识,用于获取列结构,例如 'YourApp.Entities.Invoice' |
InternalCode |
string |
是 | 当前视图的唯一标识,用于保存/读取列配置 |
IsDefault |
boolean |
否 | 是否使用默认列配置,默认 true |
InitQuery |
string |
是 | 数据查询接口地址(GET),例如 '/api/Invoice/Query.json' |
GetUrl |
string |
是 | 读取 UI 配置接口地址(GET) |
SetUrl |
string |
是 | 保存 UI 配置接口地址(POST) |
InitDelete |
string |
否 | 删除接口地址(POST),不配置则禁用删除功能 |
CreateVueUrl |
string |
否 | 新建表单动态组件的 URL,用于加载远程 Vue 组件 |
接口约定
组件内部使用 axios 调用接口,需后端按以下格式返回数据。
查询接口 InitQuery(GET)
{
"Data": [...],
"Total": 100
}
列结构接口 /Server/Entity/{Type}(GET)
[
{ "Field": "Name", "Label": "名称", "DataType": "string" },
{ "Field": "Amount", "Label": "金额", "DataType": "number" }
]
完整示例
<template>
<div style="padding: 16px;">
<TableIndex :uiConfig="tableConfig" />
</div>
</template>
<script setup lang="ts">
import { TableIndex } from 'kivii-public-components'
import 'kivii-public-components/style'
const tableConfig = {
Type: 'YourApp.Entities.Order',
InternalCode: '订单列表',
IsDefault: true,
InitQuery: '/api/Order/Query.json',
GetUrl: '/api/UiConfig/Get.json',
SetUrl: '/api/UiConfig/Set.json',
InitDelete: '/api/Order/Delete.json',
CreateVueUrl: '/forms/orderForm.vue',
}
</script>
注意事项
接口代理
组件内部接口请求使用相对路径,本地开发时请在宿主项目的 Vite 配置中设置代理:
// vite.config.ts
export default defineConfig({
server: {
proxy: {
'/api': {
target: 'https://your-api-server.com',
changeOrigin: true,
},
},
},
})
无需配置 Naive UI Provider
本组件已在内部封装了 NConfigProvider、NMessageProvider、NDialogProvider,宿主项目无需安装或注册 Naive UI,直接使用即可。
版本记录
| 版本 | 说明 |
|---|---|
| 0.1.2 | 组件自包含 Provider,宿主无需 Naive UI |
| 0.1.1 | 修复发布配置,排除无关文件 |
| 0.1.0 | 初始版本 |
License
MIT
Related Packages
Version History
| Version | Released | Vue | License |
|---|---|---|---|
| 0.2.51 | ^3.5.0 | MIT | |
| 0.2.50 | ^3.5.0 | MIT | |
| 0.2.49 | ^3.5.0 | MIT | |
| 0.2.48 | ^3.5.0 | MIT | |
| 0.2.47 | ^3.5.0 | MIT | |
| 0.2.45 | ^3.5.0 | MIT | |
| 0.2.44 | ^3.5.0 | MIT | |
| 0.2.42 | ^3.5.0 | MIT | |
| 0.2.41 | ^3.5.0 | MIT | |
| 0.2.40 | ^3.5.0 | MIT | |
| 0.2.39 | ^3.5.0 | MIT | |
| 0.2.38 | ^3.5.0 | MIT | |
| 0.2.37 | ^3.5.0 | MIT | |
| 0.2.36 | ^3.5.0 | MIT | |
| 0.2.35 | ^3.5.0 | MIT | |
| 0.2.34 | ^3.5.0 | MIT | |
| 0.2.33 | ^3.5.0 | MIT | |
| 0.2.32 | ^3.5.0 | MIT | |
| 0.2.31 | ^3.5.0 | MIT | |
| 0.2.30 | ^3.5.0 | MIT | |
| 0.2.29 | ^3.5.0 | MIT | |
| 0.2.27 | ^3.5.0 | MIT | |
| 0.2.26 | ^3.5.0 | MIT | |
| 0.2.25 | ^3.5.0 | MIT | |
| 0.2.24 | ^3.5.0 | MIT | |
| 0.2.23 | ^3.5.0 | MIT | |
| 0.2.22 | ^3.5.0 | MIT | |
| 0.2.21 | ^3.5.0 | MIT | |
| 0.2.20 | ^3.5.0 | MIT | |
| 0.2.19 | ^3.5.0 | MIT |
Showing the latest 30 of 59. See every release on npm