dsh-chatbot-tools 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/LICENSE +21 -0
- package/README.md +249 -2
- package/client.js +83 -0
- package/cordis.patch.yml +20 -0
- package/docs/feedback-safety-check-exclusivity.md +82 -0
- package/docs/feedback-tts-code-switch.md +108 -0
- package/docs/incident-2026-11-17-safety-check-defer-loading.md +220 -0
- package/docs/incident-2026-11-17-tool-result-image.md +63 -0
- package/docs/verification-2026-11-17.md +471 -0
- package/lib/index.js +788 -0
- package/lib/safety.js +106 -0
- package/lib/speakable.js +162 -0
- package/lib/speech.js +94 -0
- package/package.json +70 -4
package/lib/safety.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* safety_check 注入判定(无外部依赖,便于离线测试)。
|
|
3
|
+
*
|
|
4
|
+
* 服务端规则:`defer_loading` 不得与 `deepseek_web_search`、`deepseek_read_url`、
|
|
5
|
+
* `safety_check`、`title` 共存。2026-11-17 事故就是「safety_check 常开 + 会话内工具集增量」
|
|
6
|
+
* 同时成立导致的 400,且因 developer/message 持久化而不可逆。
|
|
7
|
+
* 复现与影响面见 docs/incident-2026-11-17-safety-check-defer-loading.md。
|
|
8
|
+
*
|
|
9
|
+
* 扩展契约里工具位于 request.body.tools(DeepSeekLlmApiExtensionRequest.body),
|
|
10
|
+
* 不是 request.tools —— 文档示例里的写法是笔误,按契约实现。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 请求是否处于思考模式。
|
|
15
|
+
*
|
|
16
|
+
* 官方文档:思考模式默认打开(不传 thinking 即开启),`disabled` 才关闭。
|
|
17
|
+
* 服务端另有一条未写进文档的约束:**非思考模式**下 client tools 不能与
|
|
18
|
+
* `safety_check` / `deepseek_web_search` / `deepseek_read_url` / `title` 共存
|
|
19
|
+
* (2026-11-17 实测:thinking disabled + safety_check + function tool → 400)。
|
|
20
|
+
*/
|
|
21
|
+
export function thinkingEnabled(body) {
|
|
22
|
+
return body?.thinking?.type !== 'disabled'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 请求里是否已有非纯文本的 `tool_result` 内容块(图片/文档)。
|
|
27
|
+
*
|
|
28
|
+
* 服务端第三条约束(2026-11-17 另一会话实测):
|
|
29
|
+
* `messages.94: only text content is supported in `tool_result` blocks with
|
|
30
|
+
* `deepseek_web_search`, `deepseek_read_url`, `safety_check` or `title` yet`
|
|
31
|
+
* 也就是这四个顶层字段只要在场,tool_result 就只能是纯文本。`read_image`
|
|
32
|
+
* 这类工具会把图片块塞进 tool_result,一旦它与 safety_check 同处一个请求就 400,
|
|
33
|
+
* 而错误发生在「历史已被持久化」之后 —— 会话会像 defer_loading 事故一样被卡死。
|
|
34
|
+
*
|
|
35
|
+
* 该约束与 `defer_loading` 同族:都是服务端未文档化的互斥规则,只能由客户端让路。
|
|
36
|
+
*
|
|
37
|
+
* @param {{ body?: { messages?: unknown[] } }} request
|
|
38
|
+
* @returns {number} 含非文本内容的 tool_result 块数量
|
|
39
|
+
*/
|
|
40
|
+
export function nonTextToolResults(request) {
|
|
41
|
+
const messages = Array.isArray(request.body?.messages) ? request.body.messages : []
|
|
42
|
+
let count = 0
|
|
43
|
+
for (const message of messages) {
|
|
44
|
+
const content = message?.content
|
|
45
|
+
if (!Array.isArray(content)) continue
|
|
46
|
+
for (const block of content) {
|
|
47
|
+
if (!block || block.type !== 'tool_result') continue
|
|
48
|
+
const inner = block.content
|
|
49
|
+
if (!Array.isArray(inner)) continue
|
|
50
|
+
if (inner.some(part => part && part.type !== 'text')) count++
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return count
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {{ purpose?: string, body?: { tools?: unknown[], thinking?: { type?: string } } }} request 扩展准备请求
|
|
58
|
+
* @param {{ safetyMode?: string, safetyEnabled?: boolean, safetySkipPurposes?: string[], safetyGuardDeferLoading?: boolean }} cfg
|
|
59
|
+
* @returns {{ inject: boolean, reason: string }}
|
|
60
|
+
*/
|
|
61
|
+
export function safetyDecision(request, cfg) {
|
|
62
|
+
// 计数顺带返回给调用方做观测,这样 lib/index.js 不必新增 import(改 import 必须重启)。
|
|
63
|
+
return { ...decide(request, cfg), nonTextResults: nonTextToolResults(request) }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function decide(request, cfg) {
|
|
67
|
+
const mode = cfg.safetyMode ?? (cfg.safetyEnabled ? 'enforce' : 'off')
|
|
68
|
+
if (mode === 'off') return { inject: false, reason: 'mode-off' }
|
|
69
|
+
if ((cfg.safetySkipPurposes ?? []).includes(request.purpose)) return { inject: false, reason: 'purpose-skip' }
|
|
70
|
+
// 非思考模式:client tools 与 safety_check 互斥(实测 400),必须跳过。
|
|
71
|
+
if (!thinkingEnabled(request.body)) return { inject: false, reason: 'thinking-off' }
|
|
72
|
+
// tool_result 里有图片/文档:safety_check 在场就会被服务端拒(实测 400),必须跳过。
|
|
73
|
+
if (nonTextToolResults(request) > 0) return { inject: false, reason: 'non-text-tool-result' }
|
|
74
|
+
if (cfg.safetyGuardDeferLoading !== false) {
|
|
75
|
+
const tools = Array.isArray(request.body?.tools) ? request.body.tools : []
|
|
76
|
+
if (tools.some(tool => tool && tool.defer_loading === true)) {
|
|
77
|
+
return { inject: false, reason: 'defer-loading-guard' }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (mode === 'observe') return { inject: false, reason: 'observe' }
|
|
81
|
+
return { inject: true, reason: 'enforce' }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 一次请求里带延迟声明的工具名,用于观测日志(不记录消息内容)。 */
|
|
85
|
+
export function deferredToolNames(request, limit = 10) {
|
|
86
|
+
const tools = Array.isArray(request.body?.tools) ? request.body.tools : []
|
|
87
|
+
return tools
|
|
88
|
+
.filter(tool => tool && tool.defer_loading === true)
|
|
89
|
+
.map(tool => tool.name ?? '(anonymous)')
|
|
90
|
+
.slice(0, limit)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 判断一个错误是否来自服务端拒答。
|
|
95
|
+
*
|
|
96
|
+
* dsh-llm-deepseek 的 translate.stopReason 不认识 `refusal`,会抛
|
|
97
|
+
* `DeepSeek Messages stream: unsupported stop reason refusal`(LlmError/MALFORMED_RESPONSE)。
|
|
98
|
+
* 真实拒答形态未主动构造,这里按适配器源码的措辞匹配。
|
|
99
|
+
*
|
|
100
|
+
* @param {unknown} error 抛出的错误或错误信息
|
|
101
|
+
* @returns {boolean}
|
|
102
|
+
*/
|
|
103
|
+
export function isRefusalError(error) {
|
|
104
|
+
const message = String(error && error.message ? error.message : error ?? '')
|
|
105
|
+
return /unsupported stop reason refusal|stop_reason[^a-z]*refusal/i.test(message)
|
|
106
|
+
}
|
package/lib/speakable.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 把 Markdown 正文转成「适合朗读的纯文本」。
|
|
3
|
+
*
|
|
4
|
+
* 实测(2026-11-17):服务端 `filter_markdown: true` 会连**行内代码**一起剥掉,
|
|
5
|
+
* 例如 "`ttsFilterMarkdown`(默认 `true`)" 被读成 "(默认)",重要信息直接丢失。
|
|
6
|
+
* 所以改为本地先转成纯文本,再让服务端不要再剥一遍。
|
|
7
|
+
*
|
|
8
|
+
* 规则:
|
|
9
|
+
* - 行内代码 `x` → x(必须保留,正是被服务端吃掉的那部分)
|
|
10
|
+
* - 围栏代码块:codeMode = skip 删除 / placeholder 念占位 / read 保留内容
|
|
11
|
+
* - 图片  → alt;链接 [text](url) → text
|
|
12
|
+
* - 标题、强调、引用、列表符号、分割线、表格分隔行 → 去标记留文字
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const FENCE = /^[ \t]*(`{3,}|~{3,})[^\n]*\n[\s\S]*?^[ \t]*\1[ \t]*$/gm
|
|
16
|
+
const FENCE_MARK = /^[ \t]*(`{3,}|~{3,})[^\n]*$/gm
|
|
17
|
+
const FENCE_OPEN = /^[ \t]*(`{3,}|~{3,})[^\n]*\n/gm
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} markdown 原始 Markdown 文本
|
|
21
|
+
* @param {{ codeMode?: 'skip' | 'placeholder' | 'read' }} [options]
|
|
22
|
+
* @returns {string} 可直接送 TTS 的纯文本
|
|
23
|
+
*/
|
|
24
|
+
export function toSpeakable(markdown, options = {}) {
|
|
25
|
+
const codeMode = options.codeMode ?? 'skip'
|
|
26
|
+
let text = String(markdown ?? '')
|
|
27
|
+
|
|
28
|
+
// 1) 围栏代码块
|
|
29
|
+
if (codeMode === 'read') {
|
|
30
|
+
// 只去掉围栏行,保留代码内容
|
|
31
|
+
text = text.replace(FENCE_MARK, '')
|
|
32
|
+
} else {
|
|
33
|
+
text = text.replace(FENCE, () => (codeMode === 'placeholder' ? '\n(这里有一段代码)\n' : '\n'))
|
|
34
|
+
// 无闭合围栏的残缺代码块:去掉开围栏行,避免把 ``` 念出来
|
|
35
|
+
text = text.replace(FENCE_OPEN, '')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 2) 行内代码:去掉反引号,保留内容
|
|
39
|
+
text = text.replace(/(`+)([^`\n]+?)\1/g, '$2')
|
|
40
|
+
|
|
41
|
+
// 3) 图片与链接
|
|
42
|
+
text = text.replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1')
|
|
43
|
+
text = text.replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
|
|
44
|
+
|
|
45
|
+
// 4) 标题 / 引用 / 分割线
|
|
46
|
+
text = text.replace(/^[ \t]{0,3}#{1,6}[ \t]+/gm, '')
|
|
47
|
+
text = text.replace(/^[ \t]{0,3}>[ \t]?/gm, '')
|
|
48
|
+
text = text.replace(/^[ \t]{0,3}([-*_])[ \t]*\1[ \t]*\1[ \t]*$/gm, '')
|
|
49
|
+
|
|
50
|
+
// 5) 表格:去掉分隔行与竖线
|
|
51
|
+
text = text.replace(/^[ \t]*\|?[ \t]*:?-{2,}:?[ \t]*(\|[ \t]*:?-{2,}:?[ \t]*)+\|?[ \t]*$/gm, '')
|
|
52
|
+
text = text.replace(/^[ \t]*\|(.*)\|[ \t]*$/gm, (_match, row) => row
|
|
53
|
+
.split('|')
|
|
54
|
+
.map(cell => cell.trim())
|
|
55
|
+
.filter(cell => cell.length > 0)
|
|
56
|
+
.join(','))
|
|
57
|
+
|
|
58
|
+
// 6) 强调与删除线
|
|
59
|
+
text = text.replace(/(\*\*|__)(.*?)\1/g, '$2')
|
|
60
|
+
text = text.replace(/~~(.*?)~~/g, '$1')
|
|
61
|
+
text = text.replace(/\*([^*\n]+)\*/g, '$1')
|
|
62
|
+
text = text.replace(/(^|[\s(])_([^_\n]+)_/g, '$1$2')
|
|
63
|
+
|
|
64
|
+
// 7) 列表符号、HTML 标签
|
|
65
|
+
text = text.replace(/^[ \t]*[-*+][ \t]+/gm, '')
|
|
66
|
+
text = text.replace(/^[ \t]*\d+[.)][ \t]+/gm, '')
|
|
67
|
+
text = text.replace(/<[^>]+>/g, ' ')
|
|
68
|
+
|
|
69
|
+
// 8) 逐行去空白、丢空行,输出确定性文本
|
|
70
|
+
return text
|
|
71
|
+
.split('\n')
|
|
72
|
+
.map(line => line.replace(/[ \t]+/g, ' ').trim())
|
|
73
|
+
.filter(line => line.length > 0)
|
|
74
|
+
.join('\n')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 解析术语替换表。支持两种写法:
|
|
79
|
+
* JSON:'{"Markdown":"马克当","API":"接口"}'
|
|
80
|
+
* 键值对:'Markdown=马克当, API=接口'(也支持中文逗号与换行分隔)
|
|
81
|
+
*
|
|
82
|
+
* 用途:官方 TTS 没有 prompt / 语种参数(只有 text、voice_id、format、filter_markdown),
|
|
83
|
+
* 英文词在中文语境里可能被误判语种或同化(Markdown → Mardeano / 韩文),
|
|
84
|
+
* 只能在朗读前把易错词换成读音稳定的说法。
|
|
85
|
+
*
|
|
86
|
+
* @param {unknown} raw 配置里的原始字符串
|
|
87
|
+
* @returns {Array<[string, string]>} 按替换顺序排列的 [原文, 读法]
|
|
88
|
+
*/
|
|
89
|
+
export function parseTermMap(raw) {
|
|
90
|
+
const text = typeof raw === 'string' ? raw.trim() : ''
|
|
91
|
+
if (text.length === 0) return []
|
|
92
|
+
const pairs = []
|
|
93
|
+
if (text.startsWith('{')) {
|
|
94
|
+
try {
|
|
95
|
+
const parsed = JSON.parse(text)
|
|
96
|
+
for (const [key, value] of Object.entries(parsed ?? {})) {
|
|
97
|
+
if (typeof key === 'string' && key.length > 0 && typeof value === 'string') pairs.push([key, value])
|
|
98
|
+
}
|
|
99
|
+
return pairs
|
|
100
|
+
} catch {
|
|
101
|
+
return []
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (const chunk of text.split(/[,,\n]/)) {
|
|
105
|
+
const entry = chunk.trim()
|
|
106
|
+
if (entry.length === 0) continue
|
|
107
|
+
const index = entry.indexOf('=')
|
|
108
|
+
if (index <= 0) continue
|
|
109
|
+
const key = entry.slice(0, index).trim()
|
|
110
|
+
const value = entry.slice(index + 1).trim()
|
|
111
|
+
if (key.length > 0 && value.length > 0) pairs.push([key, value])
|
|
112
|
+
}
|
|
113
|
+
return pairs
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const PRINTABLE_ASCII_ONLY = /^[\x20-\x7e]+$/
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 短键(≤2 个 ASCII 字符,如 AI / ML / Go)要按「整词」替换。
|
|
120
|
+
*
|
|
121
|
+
* 实测(2026-11-17):纯子串替换下 `AI` 会命中 `OPENAI` → `OPEN【AI】`。
|
|
122
|
+
* 短键几乎总是缩写,撞进更长的英文单词里基本都是误伤;而长键(`Markdown`)
|
|
123
|
+
* 即使在标识符内部(`ttsFilterMarkdown`)替换也是想要的 —— 朗读文本没有语法,
|
|
124
|
+
* 替换只为发音正确,所以长键继续用子串替换。
|
|
125
|
+
*/
|
|
126
|
+
function isShortAsciiKey(from) {
|
|
127
|
+
return from.length <= 2 && PRINTABLE_ASCII_ONLY.test(from)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function escapeRegExp(value) {
|
|
131
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 按术语表替换(长词优先,避免「Markdown」被更短的词先吃掉)。
|
|
136
|
+
*
|
|
137
|
+
* 匹配语义:
|
|
138
|
+
* - 大小写敏感(`Markdown` 不命中 `markdown`)
|
|
139
|
+
* - 长词优先(`ttsFilterMarkdown` 先于 `Markdown` 命中)
|
|
140
|
+
* - ≤2 字符的 ASCII 短键按整词匹配(`AI` 不命中 `OPENAI`)
|
|
141
|
+
* - 其余键为纯子串替换(标识符内部也替换)
|
|
142
|
+
*
|
|
143
|
+
* @param {string} text 已转换的朗读文本
|
|
144
|
+
* @param {Array<[string, string]>} pairs parseTermMap 的结果
|
|
145
|
+
* @returns {string}
|
|
146
|
+
*/
|
|
147
|
+
export function applyTerms(text, pairs) {
|
|
148
|
+
if (!Array.isArray(pairs) || pairs.length === 0) return text
|
|
149
|
+
const ordered = [...pairs].sort((a, b) => b[0].length - a[0].length)
|
|
150
|
+
let out = text
|
|
151
|
+
for (const [from, to] of ordered) {
|
|
152
|
+
if (from.length === 0) continue
|
|
153
|
+
if (isShortAsciiKey(from)) {
|
|
154
|
+
// 用函数式替换,避免替换值里的 `$&`、`$1` 被当成捕获组引用
|
|
155
|
+
const pattern = new RegExp(`(?<![A-Za-z0-9_])${escapeRegExp(from)}(?![A-Za-z0-9_])`, 'g')
|
|
156
|
+
out = out.replace(pattern, () => to)
|
|
157
|
+
} else {
|
|
158
|
+
out = out.split(from).join(to)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return out
|
|
162
|
+
}
|
package/lib/speech.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeepSeek ASR provider for the official speech-to-text seam.
|
|
3
|
+
*
|
|
4
|
+
* 官方语音输入由三层组成:
|
|
5
|
+
* @deepseek-ai/dsh-experimental-speech-to-text (服务 + provider 注册表)
|
|
6
|
+
* @deepseek-ai/dsh-experimental-api-speech-to-text (Remote 控制器,客户端调用它)
|
|
7
|
+
* @deepseek-ai/dsh-experimental-client-ui-voice-input (麦克风 UI)
|
|
8
|
+
* 因此把 DeepSeek 的 /v1/tools/asr 包成一个 SpeechProvider 注册进去,
|
|
9
|
+
* 官方麦克风按钮就能直接列出并使用它,不需要自己写录音 UI。
|
|
10
|
+
*
|
|
11
|
+
* 这里只依赖 speechToText 的公开契约:
|
|
12
|
+
* { info, transcribe({ audio, language }, signal) -> { text, audioSeconds, inferenceSeconds } }
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** DeepSeek ASR 支持的语种(见官方文档「语音识别 → 语种」)。 */
|
|
16
|
+
const ASR_LANGUAGES = [
|
|
17
|
+
'auto', 'zh', 'yue', 'en', 'ja', 'ko', 'de', 'fr', 'es', 'pt', 'it', 'ru',
|
|
18
|
+
'ar', 'hi', 'id', 'vi', 'th', 'tr', 'ms', 'fil', 'pl', 'hu',
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
/** 读 16-bit PCM WAV 的时长(秒);格式不认识时返回 0。 */
|
|
22
|
+
export function wavSeconds(bytes) {
|
|
23
|
+
try {
|
|
24
|
+
if (bytes.length < 44) return 0
|
|
25
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
26
|
+
if (String.fromCharCode(bytes[0], bytes[1], bytes[2], bytes[3]) !== 'RIFF') return 0
|
|
27
|
+
let byteRate = 0
|
|
28
|
+
let offset = 12
|
|
29
|
+
while (offset + 8 <= bytes.length) {
|
|
30
|
+
const id = String.fromCharCode(bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3])
|
|
31
|
+
const size = view.getUint32(offset + 4, true)
|
|
32
|
+
if (id === 'fmt ' && offset + 16 + 12 <= bytes.length) {
|
|
33
|
+
byteRate = view.getUint32(offset + 16, true)
|
|
34
|
+
} else if (id === 'data' && byteRate > 0) {
|
|
35
|
+
return Math.min(size, bytes.length - offset - 8) / byteRate
|
|
36
|
+
}
|
|
37
|
+
offset += 8 + size + (size % 2)
|
|
38
|
+
}
|
|
39
|
+
} catch {
|
|
40
|
+
/* 解析失败按 0 处理 */
|
|
41
|
+
}
|
|
42
|
+
return 0
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 建一个 DeepSeek ASR provider。
|
|
47
|
+
*
|
|
48
|
+
* @param {object} deps
|
|
49
|
+
* @param {() => object} deps.config 现读配置(baseURL / apiKey 等)
|
|
50
|
+
* @param {(endpoint: string) => Promise<{ kind: string, headers: Record<string,string> }>} deps.auth 鉴权头解析
|
|
51
|
+
* @param {(url: string, headers: Record<string,string>, form: FormData, timeoutMs?: number) => Promise<{ ok: boolean, status: number, body?: any, buffer: Buffer }>} deps.postForm multipart POST
|
|
52
|
+
* @param {(result: any) => string} deps.detail 错误详情提取
|
|
53
|
+
* @returns {{ info: object, transcribe: Function }}
|
|
54
|
+
*/
|
|
55
|
+
export function createDeepSeekSpeechProvider({ config, auth, postForm, detail }) {
|
|
56
|
+
return {
|
|
57
|
+
info: {
|
|
58
|
+
id: 'deepseek-asr',
|
|
59
|
+
name: 'DeepSeek ASR(云端)',
|
|
60
|
+
location: 'cloud',
|
|
61
|
+
languages: ASR_LANGUAGES,
|
|
62
|
+
},
|
|
63
|
+
/**
|
|
64
|
+
* @param {{ audio: Uint8Array, language: string }} input 一次完整录音(16-bit PCM WAV)
|
|
65
|
+
* @param {AbortSignal} signal 取消
|
|
66
|
+
* @returns {Promise<{ text: string, audioSeconds: number, inferenceSeconds: number }>}
|
|
67
|
+
*/
|
|
68
|
+
async transcribe(input, signal) {
|
|
69
|
+
signal?.throwIfAborted?.()
|
|
70
|
+
const cfg = config()
|
|
71
|
+
const endpoint = `${cfg.apiBaseURL}/v1/tools/asr`
|
|
72
|
+
const startedAt = Date.now()
|
|
73
|
+
const credentials = await auth(endpoint)
|
|
74
|
+
const bytes = Buffer.from(input.audio)
|
|
75
|
+
if (bytes.length > 8 * 1024 * 1024) throw new Error(`录音过大:${bytes.length} 字节,ASR 上限 8 MiB`)
|
|
76
|
+
const form = new FormData()
|
|
77
|
+
form.append('file', new Blob([bytes], { type: 'audio/wav' }), 'recording.wav')
|
|
78
|
+
const result = await postForm(endpoint, credentials.headers, form, 120000)
|
|
79
|
+
if (!result.ok) {
|
|
80
|
+
throw new Error(`DeepSeek ASR 失败(HTTP ${result.status}):${detail(result)}`)
|
|
81
|
+
}
|
|
82
|
+
const body = result.body ?? {}
|
|
83
|
+
const text = typeof body.text === 'string' ? body.text : ''
|
|
84
|
+
const audioSeconds = Number.isFinite(body.duration_ms)
|
|
85
|
+
? body.duration_ms / 1000
|
|
86
|
+
: wavSeconds(input.audio)
|
|
87
|
+
return {
|
|
88
|
+
text,
|
|
89
|
+
audioSeconds,
|
|
90
|
+
inferenceSeconds: Math.max(0, (Date.now() - startedAt) / 1000),
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-chatbot-tools",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "把 DeepSeek Chatbot 的能力带进 DeepSeek Harness:内容安全、联网/图片搜索、OCR、TTS、ASR、文档上传",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/index.js",
|
|
9
|
+
"./client": "./client.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"lib",
|
|
14
|
+
"cordis.patch.yml",
|
|
15
|
+
"client.js",
|
|
16
|
+
"README.md",
|
|
17
|
+
"CHANGELOG.md",
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"docs"
|
|
20
|
+
],
|
|
21
|
+
"dsh": {
|
|
22
|
+
"bundle": {
|
|
23
|
+
"patch": "./cordis.patch.yml"
|
|
24
|
+
},
|
|
25
|
+
"client": {
|
|
26
|
+
"platform": "web",
|
|
27
|
+
"immediately": true,
|
|
28
|
+
"inject": [
|
|
29
|
+
"@deepseek-ai/dsh-client-ui-conversation"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"deepseek",
|
|
35
|
+
"dsh",
|
|
36
|
+
"dsh-plugin",
|
|
37
|
+
"harness",
|
|
38
|
+
"chatbot",
|
|
39
|
+
"safety-check",
|
|
40
|
+
"web-search",
|
|
41
|
+
"image-search",
|
|
42
|
+
"ocr",
|
|
43
|
+
"tts",
|
|
44
|
+
"asr",
|
|
45
|
+
"speech"
|
|
46
|
+
],
|
|
47
|
+
"author": "deepseekv41flash",
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/deepseekv41flash/dsh-chatbot-tools.git"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/deepseekv41flash/dsh-chatbot-tools#readme",
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/deepseekv41flash/dsh-chatbot-tools/issues"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=18"
|
|
59
|
+
},
|
|
60
|
+
"publishConfig": {
|
|
61
|
+
"access": "public"
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"@deepseek-ai/dsh-tools": "*",
|
|
65
|
+
"cordis": "*"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "bash scripts/build.sh",
|
|
69
|
+
"test": "node test/safety-guard.test.mjs && node test/speakable.test.mjs",
|
|
70
|
+
"typecheck": "node --check lib/index.js"
|
|
71
|
+
}
|
|
72
|
+
}
|