@plume-canvas/cloud-server 0.0.0-stage → 0.1.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/builtin-manifest.json +28 -0
- package/dist/cli.js +81 -0
- package/dist/index.d.ts +3555 -0
- package/dist/index.js +38 -0
- package/package.json +56 -4
- package/providers/seed-audio-volc.js +402 -0
- package/providers/seed-audio.js +376 -0
- package/providers/web2api-audio.js +500 -0
- package/providers/web2api-image.js +497 -0
- package/providers/web2api-video.js +490 -0
- package/README.md +0 -3
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 51KIK(Router Brain)· seed-audio-1.0 音频生成(provider,自包含单文件)。
|
|
3
|
+
*
|
|
4
|
+
* 只走「参考音频 + 自然语言描述」这一条路:
|
|
5
|
+
* - 参考音频按连接顺序编号,提示词里写 @音频1 / @音频2 引用(平台规定,顺序必须一致);
|
|
6
|
+
* - 描述直接写在提示词里,例如「一个四十岁低沉沙哑的男人,语速很慢」。
|
|
7
|
+
* 两者可以混用:@音频1 定音色,描述定情绪 / 语速 / 场景。
|
|
8
|
+
*
|
|
9
|
+
* 接口:POST https://51kik.com/audio/v1/generations(同步返回音频地址,约 10~20 秒)。
|
|
10
|
+
* 实测:references 里放 data URL 可以直接用,不需要先上传。
|
|
11
|
+
*
|
|
12
|
+
* 环境变量:KIK_BASE / KIK_API_KEY(平台管理页里存的配置优先)。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const MAX_PROMPT = 3000
|
|
16
|
+
const MAX_REFS = 3
|
|
17
|
+
const MAX_REF_BYTES = 10 * 1024 * 1024
|
|
18
|
+
|
|
19
|
+
function injected() {
|
|
20
|
+
return typeof __mcProviderConfig === 'undefined' ? { accounts: [], values: {}, byId: {} } : __mcProviderConfig
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function accountValues(values) {
|
|
24
|
+
const inj = injected()
|
|
25
|
+
const preferred = values && typeof values.__accountId === 'string' ? values.__accountId : ''
|
|
26
|
+
if (preferred && inj.byId && inj.byId[preferred]) return inj.byId[preferred]
|
|
27
|
+
const fallback = inj.defaultId && inj.byId && inj.byId[inj.defaultId]
|
|
28
|
+
if (fallback) return fallback
|
|
29
|
+
const accounts = inj.accounts || []
|
|
30
|
+
if (accounts.length > 0) return accounts[0].values || {}
|
|
31
|
+
return inj.values || {}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function baseOf(values) {
|
|
35
|
+
const b = values && typeof values.baseUrl === 'string' && values.baseUrl.trim()
|
|
36
|
+
? values.baseUrl.trim()
|
|
37
|
+
: process.env.KIK_BASE || 'https://51kik.com'
|
|
38
|
+
return String(b).replace(/\/+$/, '')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function keyOf(values) {
|
|
42
|
+
if (values && typeof values.apiKey === 'string' && values.apiKey.trim()) return values.apiKey.trim()
|
|
43
|
+
return process.env.KIK_API_KEY || ''
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function callJson(url, init, timeoutMs, values) {
|
|
47
|
+
const ac = new AbortController()
|
|
48
|
+
const timer = setTimeout(() => ac.abort(), timeoutMs)
|
|
49
|
+
try {
|
|
50
|
+
const headers = {
|
|
51
|
+
'Content-Type': 'application/json',
|
|
52
|
+
Accept: 'application/json',
|
|
53
|
+
...(init.headers || {}),
|
|
54
|
+
}
|
|
55
|
+
const key = keyOf(values)
|
|
56
|
+
if (key) headers.Authorization = 'Bearer ' + key
|
|
57
|
+
const res = await fetch(url, { ...init, headers, signal: ac.signal })
|
|
58
|
+
const text = await res.text()
|
|
59
|
+
let body = null
|
|
60
|
+
try {
|
|
61
|
+
body = text ? JSON.parse(text) : null
|
|
62
|
+
} catch {
|
|
63
|
+
throw new Error('51KIK 返回的不是 JSON(HTTP ' + res.status + ')')
|
|
64
|
+
}
|
|
65
|
+
if (!res.ok) {
|
|
66
|
+
const msg = body && body.error && body.error.message
|
|
67
|
+
throw new Error(translateError(msg || 'HTTP ' + res.status, res.status))
|
|
68
|
+
}
|
|
69
|
+
return body || {}
|
|
70
|
+
} catch (err) {
|
|
71
|
+
if (err instanceof Error && err.name === 'AbortError') {
|
|
72
|
+
throw new Error('51KIK 请求超时(' + Math.round(timeoutMs / 1000) + ' 秒)')
|
|
73
|
+
}
|
|
74
|
+
const msg = err instanceof Error ? err.message : String(err)
|
|
75
|
+
if (/fetch failed|ECONNREFUSED|Failed to fetch|NetworkError/i.test(msg)) {
|
|
76
|
+
throw new Error('连不上 51KIK(' + baseOf(values) + '),请检查网络或平台里填的服务地址')
|
|
77
|
+
}
|
|
78
|
+
throw new Error(msg || '51KIK 调用失败')
|
|
79
|
+
} finally {
|
|
80
|
+
clearTimeout(timer)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 把网关的英文报错翻成能照做的中文 */
|
|
85
|
+
function translateError(msg, status) {
|
|
86
|
+
const m = String(msg)
|
|
87
|
+
if (status === 401) return 'API Key 无效,请到「平台」页重新填 51KIK 的 Key'
|
|
88
|
+
if (status === 403) return '这个 API Key 被限制了(可能是 IP 白名单)'
|
|
89
|
+
if (status === 404) return '模型不存在或没有可用线路:' + m
|
|
90
|
+
if (status === 429) return '请求太频繁了,等一会儿再试'
|
|
91
|
+
// 余额不足是 400 里最常见的一种:原话是 Insufficient available credits for precharge
|
|
92
|
+
if (/insufficient .*credit/i.test(m)) {
|
|
93
|
+
const need = /required:?\s*([\d.]+)/i.exec(m)?.[1]
|
|
94
|
+
const have = /available:?\s*(-?[\d.]+)/i.exec(m)?.[1]
|
|
95
|
+
return (
|
|
96
|
+
'51KIK 账户余额不够了' +
|
|
97
|
+
(have !== undefined ? '(可用 ' + have + ' 积分)' : '') +
|
|
98
|
+
(need ? ',这次大约需要 ' + need + ' 积分' : '') +
|
|
99
|
+
'。充值后再试;有任务还没结算时也可以等一会儿。'
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
if (/prompt/i.test(m) && /empty|required/i.test(m)) return '提示词不能为空'
|
|
103
|
+
if (/reference|audio/i.test(m) && /too (long|large)|exceed/i.test(m)) {
|
|
104
|
+
return '参考音频超限:最多 3 段,每段不超过 30 秒、10 MB(' + m + ')'
|
|
105
|
+
}
|
|
106
|
+
return m
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function dataUrlInfo(url) {
|
|
110
|
+
const m = /^data:([^;,]*)(;base64)?,([\s\S]*)$/i.exec(String(url || ''))
|
|
111
|
+
if (!m) return null
|
|
112
|
+
const bytes = m[2] ? Math.floor((m[3].length * 3) / 4) : decodeURIComponent(m[3]).length
|
|
113
|
+
return { mime: m[1] || '', bytes }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 连进来的音频 → references 数组,并按数组顺序编号。
|
|
118
|
+
*
|
|
119
|
+
* 平台只认序号(@音频1 对应 references[0]),所以编号和数组必须是同一次遍历的产物;
|
|
120
|
+
* 分成两遍很容易错位(用户实测过:错一位就变成别人在说话)。
|
|
121
|
+
*/
|
|
122
|
+
function collectAudios(input) {
|
|
123
|
+
const audios = []
|
|
124
|
+
const indexById = new Map()
|
|
125
|
+
const skipped = []
|
|
126
|
+
for (const r of input.resources || []) {
|
|
127
|
+
if (!r) continue
|
|
128
|
+
if (r.kind !== 'audio') {
|
|
129
|
+
if (r.kind) skipped.push(r.kind)
|
|
130
|
+
continue
|
|
131
|
+
}
|
|
132
|
+
const url = typeof r.url === 'string' ? r.url.trim() : ''
|
|
133
|
+
if (!url) {
|
|
134
|
+
skipped.push('空音频')
|
|
135
|
+
continue
|
|
136
|
+
}
|
|
137
|
+
const n = audios.length + 1
|
|
138
|
+
if (n > MAX_REFS) break
|
|
139
|
+
const info = dataUrlInfo(url)
|
|
140
|
+
if (info && info.bytes > MAX_REF_BYTES) {
|
|
141
|
+
throw new Error('参考音频「' + (r.name || '未命名') + '」超过 10 MB,请换短一点的片段')
|
|
142
|
+
}
|
|
143
|
+
audios.push({ type: 'audio', url })
|
|
144
|
+
if (typeof r.id === 'string' && r.id) indexById.set(r.id, n)
|
|
145
|
+
}
|
|
146
|
+
if (skipped.length > 0) {
|
|
147
|
+
console.warn('[seed-audio] 这个模型只收参考音频,已忽略:' + [...new Set(skipped)].join('、'))
|
|
148
|
+
}
|
|
149
|
+
return { audios, indexById }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 提示词分段 → 平台文本。
|
|
154
|
+
*
|
|
155
|
+
* 输入框里点选音频节点会留下 { type:'tag', value:'资源id' },这里换成 @音频N;
|
|
156
|
+
* 普通文字原样保留。不在音频列表里的 tag 换成空串(不能留个指向不存在音频的 @音频9)。
|
|
157
|
+
*/
|
|
158
|
+
function buildPrompt(input, indexById) {
|
|
159
|
+
let out = ''
|
|
160
|
+
for (const seg of input.prompts || []) {
|
|
161
|
+
if (!seg || typeof seg.value !== 'string') continue
|
|
162
|
+
if (seg.type === 'tag') {
|
|
163
|
+
const n = indexById.get(seg.value)
|
|
164
|
+
if (n) out += '@音频' + n
|
|
165
|
+
else console.warn('[seed-audio] 提示词里的素材 ' + seg.value + ' 不是参考音频,已省略')
|
|
166
|
+
} else {
|
|
167
|
+
out += seg.value
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return out
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function collectUrls(body) {
|
|
174
|
+
const out = []
|
|
175
|
+
const push = (v) => {
|
|
176
|
+
if (typeof v === 'string') {
|
|
177
|
+
if (/^(https?:|data:)/i.test(v)) out.push(v)
|
|
178
|
+
} else if (Array.isArray(v)) {
|
|
179
|
+
for (const item of v) push(item)
|
|
180
|
+
} else if (v && typeof v === 'object') {
|
|
181
|
+
push(v.url)
|
|
182
|
+
push(v.audioUrl)
|
|
183
|
+
push(v.audio)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
push(body && body.data)
|
|
187
|
+
return [...new Set(out)]
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function collectDuration(body) {
|
|
191
|
+
const u = body && body.usage
|
|
192
|
+
if (u && typeof u.duration === 'number') return u.duration
|
|
193
|
+
const d = body && body.data && body.data[0]
|
|
194
|
+
if (d && typeof d.duration === 'number') return d.duration
|
|
195
|
+
return null
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const action = {
|
|
199
|
+
id: 'seed-audio:generate',
|
|
200
|
+
// 标题要短:底栏那格只有 200 多像素,长标题会被截成「…音频生成」
|
|
201
|
+
title: 'Seed Audio 1.0',
|
|
202
|
+
description: '参考音色 + 自然语言描述。写 @音频1 用这段声音,描述里写情绪、语速、场景;纯描述也能生成音效',
|
|
203
|
+
group: '51kik · 音频',
|
|
204
|
+
nodeTypes: ['audio'],
|
|
205
|
+
needsPrompt: true,
|
|
206
|
+
order: 30,
|
|
207
|
+
model: 'seed-audio-1.0',
|
|
208
|
+
pollIntervalMs: 3000,
|
|
209
|
+
pollTimeoutMs: 600000,
|
|
210
|
+
fields: [
|
|
211
|
+
{
|
|
212
|
+
key: 'format', label: '格式', type: 'grid', default: 'mp3',
|
|
213
|
+
options: [
|
|
214
|
+
{ label: 'MP3', value: 'mp3' },
|
|
215
|
+
{ label: 'WAV', value: 'wav' },
|
|
216
|
+
{ label: 'PCM', value: 'pcm' },
|
|
217
|
+
{ label: 'OGG', value: 'ogg_opus' },
|
|
218
|
+
],
|
|
219
|
+
description: 'MP3 体积小、到处都能放;WAV 音质原始但文件大;OGG 只支持 48k 采样率。',
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
key: 'sampleRate', label: '采样率', type: 'select', default: 24000,
|
|
223
|
+
options: [
|
|
224
|
+
{ label: '16 kHz', value: 16000 },
|
|
225
|
+
{ label: '24 kHz', value: 24000 },
|
|
226
|
+
{ label: '32 kHz', value: 32000 },
|
|
227
|
+
{ label: '44.1 kHz', value: 44100 },
|
|
228
|
+
{ label: '48 kHz', value: 48000 },
|
|
229
|
+
],
|
|
230
|
+
description: '越高越清晰、文件越大。人声用 24 kHz 就够,音乐 / 音效用 44.1 kHz。',
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
key: 'speechRate', label: '语速', type: 'number', min: -50, max: 100, step: 5, default: 0,
|
|
234
|
+
description: '0 是原速;-50 是 0.5 倍慢,100 是 2 倍快。',
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
key: 'pitchRate', label: '音调', type: 'number', min: -12, max: 12, step: 1, default: 0,
|
|
238
|
+
description: '往下调声音更低沉,往上调更尖。',
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
key: 'loudnessRate', label: '音量', type: 'number', min: -50, max: 100, step: 5, default: 0,
|
|
242
|
+
description: '0 是原始音量;-50 减半,100 加倍。',
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
templates: [
|
|
246
|
+
{
|
|
247
|
+
id: 'seed-audio:tpl-ref',
|
|
248
|
+
name: '参考音色读台词',
|
|
249
|
+
description: '选一段音频,用它本来的声音说话',
|
|
250
|
+
prompt: '@音频1 用这个声音说:今天的雨下得好大啊,你带伞了吗?',
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
id: 'seed-audio:tpl-dialog',
|
|
254
|
+
name: '两人对话',
|
|
255
|
+
description: '参考音色 + 描述出来的男声,一来一回',
|
|
256
|
+
prompt:
|
|
257
|
+
'@音频1 用这个甜美轻快的少女声说:你终于回来啦,我等了你好久。\n' +
|
|
258
|
+
'然后一个四十多岁的低沉沙哑中年男人,语速很慢、带着一点疲惫,接着说:路上堵车,我也没办法啊。',
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
id: 'seed-audio:tpl-scene',
|
|
262
|
+
name: '纯描述音效',
|
|
263
|
+
description: '不连参考音频也能用:雨夜、街道、远处车流',
|
|
264
|
+
prompt: '雨夜的街道,雨点密密地打在铁皮雨棚上,远处有汽车驶过溅起水花,偶尔一声闷雷。',
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
id: 'seed-audio:tpl-narrator',
|
|
268
|
+
name: '旁白男声',
|
|
269
|
+
description: '纯描述的音色,适合纪录片旁白',
|
|
270
|
+
prompt: '一个三十多岁的男中音做纪录片旁白,语速平稳、声音厚实、略带克制,说:这里是长江的源头,海拔六千米,风从冰川上吹过。',
|
|
271
|
+
},
|
|
272
|
+
],
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export default {
|
|
276
|
+
id: 'seed-audio',
|
|
277
|
+
title: '51KIK · 音频生成',
|
|
278
|
+
description: '豆包 seed-audio-1.0:参考音色 + 自然语言描述,生成对白、旁白和音效',
|
|
279
|
+
// 图标写名字就够了:底栏会去 Agent 的图标表里查,和豆包语音合成显示同一颗
|
|
280
|
+
// (用户 2026-11-16:「你的图标可以直接使用 doubao 那个平台的图标」)。
|
|
281
|
+
// 写成 SVG 会盖住平台品牌图,所以这里不再自己画。
|
|
282
|
+
icon: 'doubao',
|
|
283
|
+
config: [
|
|
284
|
+
{
|
|
285
|
+
key: 'baseUrl', label: '服务地址', type: 'string', default: 'https://51kik.com',
|
|
286
|
+
placeholder: 'https://51kik.com', description: '51KIK 网关的根地址,留空用默认。',
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
key: 'apiKey', label: 'API Key', type: 'secret',
|
|
290
|
+
description: '在 51KIK 控制台创建的 sk- 开头密钥,只存在服务端。',
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
actions: [action],
|
|
294
|
+
|
|
295
|
+
async submit(actionDef, input) {
|
|
296
|
+
try {
|
|
297
|
+
const values = accountValues(input.values)
|
|
298
|
+
const v = input.values || {}
|
|
299
|
+
const { audios, indexById } = collectAudios(input)
|
|
300
|
+
|
|
301
|
+
let prompt = buildPrompt(input, indexById)
|
|
302
|
+
// 连了参考音频但提示词没写 @音频N:补一句引用,否则模型不知道要学哪段声音
|
|
303
|
+
if (audios.length > 0 && !/@音频\d/.test(prompt)) {
|
|
304
|
+
prompt = '参考 @音频1 的音色、语气和说话方式。\n' + prompt
|
|
305
|
+
}
|
|
306
|
+
prompt = prompt.trim()
|
|
307
|
+
if (!prompt) {
|
|
308
|
+
return { error: '请写一句台词或描述,例如:@音频1 用这个声音说:你好呀。' }
|
|
309
|
+
}
|
|
310
|
+
if ([...prompt].length > MAX_PROMPT) {
|
|
311
|
+
return { error: '提示词太长了(' + [...prompt].length + ' 字),最多 ' + MAX_PROMPT + ' 字' }
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const audioConfig = {
|
|
315
|
+
format: typeof v.format === 'string' && v.format ? v.format : 'mp3',
|
|
316
|
+
sample_rate: num(v.sampleRate, 24000),
|
|
317
|
+
speech_rate: num(v.speechRate, 0),
|
|
318
|
+
pitch_rate: num(v.pitchRate, 0),
|
|
319
|
+
loudness_rate: num(v.loudnessRate, 0),
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const body = {
|
|
323
|
+
model: actionDef.model || 'seed-audio-1.0',
|
|
324
|
+
prompt,
|
|
325
|
+
stream: false,
|
|
326
|
+
task: false,
|
|
327
|
+
upstream_options: { audio_config: audioConfig },
|
|
328
|
+
}
|
|
329
|
+
if (audios.length > 0) body.references = audios
|
|
330
|
+
|
|
331
|
+
const res = await callJson(baseOf(values) + '/audio/v1/generations', {
|
|
332
|
+
method: 'POST',
|
|
333
|
+
body: JSON.stringify(body),
|
|
334
|
+
}, 300000, values)
|
|
335
|
+
|
|
336
|
+
if (res && typeof res.task_id === 'string' && res.task_id) {
|
|
337
|
+
return { taskId: res.task_id }
|
|
338
|
+
}
|
|
339
|
+
const urls = collectUrls(res)
|
|
340
|
+
if (urls.length === 0) {
|
|
341
|
+
const e = res && res.error && res.error.message
|
|
342
|
+
return { error: e ? translateError(e, 502) : '生成完成但没有拿到音频地址' }
|
|
343
|
+
}
|
|
344
|
+
console.log('[seed-audio] 生成完成 ' + (collectDuration(res) ?? '?') + ' 秒,参考音频 ' + audios.length + ' 段')
|
|
345
|
+
return { done: true, urls }
|
|
346
|
+
} catch (err) {
|
|
347
|
+
return { error: err instanceof Error ? err.message : String(err) }
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
|
|
351
|
+
async poll(taskId, actionDef) {
|
|
352
|
+
try {
|
|
353
|
+
const values = accountValues()
|
|
354
|
+
const body = await callJson(baseOf(values) + '/audio/v1/tasks/' + encodeURIComponent(taskId), {}, 60000, values)
|
|
355
|
+
const status = String((body && body.task_status) || '')
|
|
356
|
+
if (status === 'success') {
|
|
357
|
+
const urls = collectUrls(body)
|
|
358
|
+
if (urls.length === 0) return { status: 'error', error: '任务成功但没有音频地址' }
|
|
359
|
+
return { status: 'done', urls }
|
|
360
|
+
}
|
|
361
|
+
if (status === 'failed') {
|
|
362
|
+
const msg = body && body.error && body.error.message
|
|
363
|
+
return { status: 'error', error: msg || '生成失败' }
|
|
364
|
+
}
|
|
365
|
+
if (status === 'cancelled') return { status: 'error', error: '任务已取消' }
|
|
366
|
+
return { status: 'running', message: '生成中…', taskId }
|
|
367
|
+
} catch (err) {
|
|
368
|
+
return { status: 'running', message: err instanceof Error ? err.message : '查询失败', taskId }
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function num(v, fallback) {
|
|
374
|
+
const n = typeof v === 'number' ? v : Number(v)
|
|
375
|
+
return Number.isFinite(n) ? n : fallback
|
|
376
|
+
}
|