@joplo-app/server 0.2.9 → 0.2.12
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/dist/build.d.ts +1 -0
- package/dist/build.js +25 -3
- package/dist/cloud.d.ts +85 -0
- package/dist/cloud.js +110 -0
- package/dist/contract.d.ts +28 -1
- package/dist/dev.d.ts +7 -1
- package/dist/dev.js +79 -5
- package/dist/index.d.ts +1 -1
- package/dist/local-review.d.ts +18 -0
- package/dist/local-review.js +133 -0
- package/dist/render.js +6 -3
- package/dist/voice-http.d.ts +71 -0
- package/dist/voice-http.js +245 -0
- package/dist/voice.d.ts +253 -0
- package/dist/voice.js +637 -0
- package/package.json +4 -4
package/dist/voice.d.ts
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { DEFAULT_STYLE, type Project, type VoiceConfig, type VoiceIndex } from '@joplo-app/core';
|
|
2
|
+
export { DEFAULT_STYLE };
|
|
3
|
+
export type ProfileKind = 'openai' | 'elevenlabs' | 'http';
|
|
4
|
+
/** 返回内容:音频文件本身;JSON 里的 base64;JSON 里的音频链接。 */
|
|
5
|
+
export type ResponseMode = 'audio' | 'json-base64' | 'json-url';
|
|
6
|
+
export type PresetId = 'openai' | 'elevenlabs' | 'aihubmix';
|
|
7
|
+
export type VoiceProfile = {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
kind: ProfileKind;
|
|
11
|
+
preset?: PresetId;
|
|
12
|
+
/** openai / elevenlabs:接口根地址(例如 https://api.openai.com/v1);http:完整的请求地址。 */
|
|
13
|
+
baseUrl: string;
|
|
14
|
+
model: string;
|
|
15
|
+
/** 默认音色(旁白);elevenlabs 是 voice_id。 */
|
|
16
|
+
voice: string;
|
|
17
|
+
response: ResponseMode;
|
|
18
|
+
/** response 是 JSON 时音频所在的字段路径,例如 data.audio、choices.0.audio。 */
|
|
19
|
+
path?: string;
|
|
20
|
+
/** 额外请求头;值里的 {{key}} 换成 Key。http 类型不写时用 Authorization: Bearer {{key}}。 */
|
|
21
|
+
headers?: Record<string, string>;
|
|
22
|
+
/** http 类型的请求体模板(JSON),{{text}} {{voice}} {{model}} 换成对应的值(已按 JSON 字符串转义)。 */
|
|
23
|
+
body?: string;
|
|
24
|
+
/** 说话风格(OpenAI 兼容接口的 instructions;tts-1 系列不支持,不发)。缺省用 DEFAULT_STYLE。 */
|
|
25
|
+
style?: string;
|
|
26
|
+
key?: string;
|
|
27
|
+
};
|
|
28
|
+
/** 预设音色:名字、性别、口音(BCP-47,没有表示多语言通用)、是否推荐。看板按旁白语言排序、按界面语言显示说明。 */
|
|
29
|
+
export type VoiceChoice = {
|
|
30
|
+
id: string;
|
|
31
|
+
name: string;
|
|
32
|
+
gender: 'female' | 'male' | 'neutral';
|
|
33
|
+
accent?: string;
|
|
34
|
+
recommended?: boolean;
|
|
35
|
+
};
|
|
36
|
+
export type PublicProfile = Omit<VoiceProfile, 'key'> & {
|
|
37
|
+
key: {
|
|
38
|
+
saved: boolean;
|
|
39
|
+
hint: string | null;
|
|
40
|
+
env: string | null;
|
|
41
|
+
envSet: boolean;
|
|
42
|
+
};
|
|
43
|
+
/** 这个接口的预设音色(看板里作为候选;可以填别的)。 */
|
|
44
|
+
voices: VoiceChoice[];
|
|
45
|
+
/** 可以删除(自建的);预设只能恢复默认。 */
|
|
46
|
+
removable: boolean;
|
|
47
|
+
};
|
|
48
|
+
type Preset = Omit<VoiceProfile, 'key' | 'id'> & {
|
|
49
|
+
id: PresetId;
|
|
50
|
+
env: string;
|
|
51
|
+
voices: VoiceChoice[];
|
|
52
|
+
};
|
|
53
|
+
export declare const PRESETS: Preset[];
|
|
54
|
+
export declare class VoiceError extends Error {
|
|
55
|
+
readonly code: 'NO_KEY' | 'HTTP' | 'NOT_AUDIO' | 'BAD_RESPONSE' | 'NETWORK' | 'TOO_LARGE' | 'INVALID' | 'NOT_FOUND' | 'BUSY';
|
|
56
|
+
readonly hint?: {
|
|
57
|
+
response: ResponseMode;
|
|
58
|
+
path: string;
|
|
59
|
+
} | undefined;
|
|
60
|
+
constructor(code: 'NO_KEY' | 'HTTP' | 'NOT_AUDIO' | 'BAD_RESPONSE' | 'NETWORK' | 'TOO_LARGE' | 'INVALID' | 'NOT_FOUND' | 'BUSY', message: string, hint?: {
|
|
61
|
+
response: ResponseMode;
|
|
62
|
+
path: string;
|
|
63
|
+
} | undefined);
|
|
64
|
+
}
|
|
65
|
+
/** 校验看板提交的 profile(不含 id / key)。返回规整后的字段,或抛 VoiceError('INVALID')。 */
|
|
66
|
+
export declare function normalizeProfile(input: unknown, base?: VoiceProfile): Omit<VoiceProfile, 'id' | 'key' | 'preset'>;
|
|
67
|
+
/** `<JOPLO_HOME>/voice/profiles.json`:自建的 profile 与改过的预设(含 Key),权限 0600。 */
|
|
68
|
+
export declare class ProfileStore {
|
|
69
|
+
private readonly env;
|
|
70
|
+
readonly file: string;
|
|
71
|
+
constructor(home: string, env?: Record<string, string | undefined>);
|
|
72
|
+
private read;
|
|
73
|
+
private write;
|
|
74
|
+
/** 预设(合并本机覆盖)在前,自建的在后。 */
|
|
75
|
+
list(): VoiceProfile[];
|
|
76
|
+
get(id: string): VoiceProfile | null;
|
|
77
|
+
/** 按 id 或名字(不区分大小写)找,config({ voice: { profile } }) 用。 */
|
|
78
|
+
find(ref: string): VoiceProfile | null;
|
|
79
|
+
/** Key:保存的优先,其次是预设对应的环境变量。 */
|
|
80
|
+
keyOf(p: VoiceProfile): string | null;
|
|
81
|
+
/**
|
|
82
|
+
* 新建或修改。id 为 null 时新建(id 由名字生成)。key:undefined = 不改;null 或 '' = 删掉保存的 Key;字符串 = 保存。
|
|
83
|
+
*/
|
|
84
|
+
upsert(id: string | null, input: unknown, key: string | null | undefined): VoiceProfile;
|
|
85
|
+
/** 自建的删除;预设恢复默认(去掉本机覆盖,包括保存的 Key)。 */
|
|
86
|
+
remove(id: string): void;
|
|
87
|
+
publicOf(p: VoiceProfile): PublicProfile;
|
|
88
|
+
}
|
|
89
|
+
/** JSON 字符串转义后替换 {{text}} {{voice}} {{model}}(模板本身是 JSON,值放在引号里)。 */
|
|
90
|
+
export declare function fillTemplate(tpl: string, v: {
|
|
91
|
+
text: string;
|
|
92
|
+
voice: string;
|
|
93
|
+
model: string;
|
|
94
|
+
key?: string;
|
|
95
|
+
}): string;
|
|
96
|
+
/** 发给模型的说话风格:只有 OpenAI 兼容接口、且不是 tts-1 系列(不支持 instructions)时才发。 */
|
|
97
|
+
export declare const styleOf: (p: VoiceProfile) => string | null;
|
|
98
|
+
/** 这次合成的 HTTP 请求(不发出)。 */
|
|
99
|
+
export declare function buildRequest(p: VoiceProfile, key: string | null, text: string, voice: string): {
|
|
100
|
+
url: string;
|
|
101
|
+
init: RequestInit;
|
|
102
|
+
};
|
|
103
|
+
export type AudioType = {
|
|
104
|
+
ext: 'mp3' | 'wav' | 'ogg' | 'flac' | 'm4a' | 'aac';
|
|
105
|
+
contentType: string;
|
|
106
|
+
};
|
|
107
|
+
export declare const AUDIO_TYPES: Record<AudioType['ext'], string>;
|
|
108
|
+
/** 按文件头识别音频格式;不是音频返回 null。 */
|
|
109
|
+
export declare function sniffAudio(b: Uint8Array): AudioType | null;
|
|
110
|
+
/** 按 a.b.0.c 取值。 */
|
|
111
|
+
export declare function getPath(v: unknown, p: string): unknown;
|
|
112
|
+
/** 返回的是 JSON 时,找出像音频的字段,给出“返回内容 + 字段路径”的建议。 */
|
|
113
|
+
export declare function suggestFromJson(v: unknown): {
|
|
114
|
+
response: ResponseMode;
|
|
115
|
+
path: string;
|
|
116
|
+
} | null;
|
|
117
|
+
export type FetchLike = (url: string, init?: RequestInit) => Promise<Response>;
|
|
118
|
+
/** 合成一段:返回音频字节与格式。失败抛 VoiceError(信息里不含 Key)。 */
|
|
119
|
+
export declare function synthesize(p: VoiceProfile, key: string | null, text: string, voice: string, opts?: {
|
|
120
|
+
fetch?: FetchLike;
|
|
121
|
+
signal?: AbortSignal;
|
|
122
|
+
}): Promise<{
|
|
123
|
+
bytes: Buffer;
|
|
124
|
+
} & AudioType>;
|
|
125
|
+
/** 声音指纹:接口、地址、模型、音色、返回格式、请求体模板、说话风格(不含 Key 与请求头)。 */
|
|
126
|
+
export declare function fingerprint(p: VoiceProfile, voice: string): string;
|
|
127
|
+
export type ResolvedVoices = {
|
|
128
|
+
profile: VoiceProfile | null;
|
|
129
|
+
/** 选用来源:config = joplo.config.ts 的 voice.profile;board = 看板浮层里选的;null = 只用浏览器朗读。 */
|
|
130
|
+
source: 'config' | 'board' | null;
|
|
131
|
+
/** config 里写了、但本机没有的 profile 名。 */
|
|
132
|
+
missing: string | null;
|
|
133
|
+
narrator: string | null;
|
|
134
|
+
roles: Record<string, string>;
|
|
135
|
+
/** 旁白与各角色的声音指纹(没有 profile 时都是 null / 空)。 */
|
|
136
|
+
voices: {
|
|
137
|
+
narrator: string | null;
|
|
138
|
+
roles: Record<string, string>;
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
export declare function resolveVoices(config: VoiceConfig | undefined, selected: string | null, store: ProfileStore): ResolvedVoices;
|
|
142
|
+
export type ClipRecord = {
|
|
143
|
+
file: string;
|
|
144
|
+
stepId: string;
|
|
145
|
+
fp: string;
|
|
146
|
+
text: string;
|
|
147
|
+
locale: string;
|
|
148
|
+
bytes: number;
|
|
149
|
+
at: string;
|
|
150
|
+
};
|
|
151
|
+
export type GeneratedClip = {
|
|
152
|
+
key: string;
|
|
153
|
+
file: string;
|
|
154
|
+
generated: boolean;
|
|
155
|
+
};
|
|
156
|
+
/** 正在生成的片段:片段键 → 结果(每个项目一张,见 VoiceCache.generateOne)。 */
|
|
157
|
+
export type Inflight = Map<string, Promise<GeneratedClip>>;
|
|
158
|
+
type IndexFile = {
|
|
159
|
+
version: 1;
|
|
160
|
+
profileId: string | null;
|
|
161
|
+
clips: Record<string, ClipRecord>;
|
|
162
|
+
failures: Record<string, {
|
|
163
|
+
stepId: string;
|
|
164
|
+
message: string;
|
|
165
|
+
at: string;
|
|
166
|
+
}>;
|
|
167
|
+
};
|
|
168
|
+
export type Segment = {
|
|
169
|
+
stepId: string;
|
|
170
|
+
text: string;
|
|
171
|
+
role: string | null;
|
|
172
|
+
};
|
|
173
|
+
export type GenerateSummary = {
|
|
174
|
+
total: number;
|
|
175
|
+
generated: number;
|
|
176
|
+
skipped: number;
|
|
177
|
+
failed: {
|
|
178
|
+
stepId: string;
|
|
179
|
+
message: string;
|
|
180
|
+
}[];
|
|
181
|
+
noProfile?: boolean;
|
|
182
|
+
};
|
|
183
|
+
/** `<项目>/.joplo/voice/`:index.json(选用的 profile、片段、失败)与 clips/。 */
|
|
184
|
+
export declare class VoiceCache {
|
|
185
|
+
readonly projectDir: string;
|
|
186
|
+
readonly root: string;
|
|
187
|
+
constructor(projectDir: string);
|
|
188
|
+
get clipsDir(): string;
|
|
189
|
+
read(): IndexFile;
|
|
190
|
+
write(idx: IndexFile): void;
|
|
191
|
+
select(profileId: string | null): void;
|
|
192
|
+
/** 片段文件的绝对路径(文件名不合法或不存在时为 null)。 */
|
|
193
|
+
fileOf(name: string): string | null;
|
|
194
|
+
/** 当前声音下已有的片段:键 → 文件名;以及失败的键 → 原因。 */
|
|
195
|
+
current(voices: ResolvedVoices['voices']): {
|
|
196
|
+
clips: Record<string, string>;
|
|
197
|
+
failures: Record<string, {
|
|
198
|
+
stepId: string;
|
|
199
|
+
message: string;
|
|
200
|
+
}>;
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* 生成缺的片段(已有的跳过),并发 2。每段完成后调 onProgress。失败记进 failures,成功时清掉这个键的失败。
|
|
204
|
+
*/
|
|
205
|
+
/** 记一段成功:重新读索引再写(看板的批量生成与边放边生成可能同时进行,不能互相覆盖)。 */
|
|
206
|
+
private record;
|
|
207
|
+
/** 记一段失败;这一段已经有音频(别的请求先成功了)时不记。 */
|
|
208
|
+
private recordFailure;
|
|
209
|
+
/** 这一段的键与要用的音色;文字为空或没有供应商时为 null。 */
|
|
210
|
+
plan(seg: Segment, resolved: ResolvedVoices): {
|
|
211
|
+
key: string;
|
|
212
|
+
text: string;
|
|
213
|
+
voice: string;
|
|
214
|
+
fp: string;
|
|
215
|
+
} | null;
|
|
216
|
+
/** 已有的片段文件名(文件还在时)。 */
|
|
217
|
+
clipOf(key: string): string | null;
|
|
218
|
+
/**
|
|
219
|
+
* 生成一段(已有就直接返回):返回文件名;失败记进 failures 并抛 VoiceError。
|
|
220
|
+
* inflight:同一个项目共用的“正在生成”表(片段键 → 结果)。批量生成与边放边生成传同一张表,同一段同时只请求一次,后来的等先来的结果(generated 为 false)。
|
|
221
|
+
*/
|
|
222
|
+
generateOne(seg: Segment, locale: string, resolved: ResolvedVoices, store: ProfileStore, opts?: {
|
|
223
|
+
fetch?: FetchLike;
|
|
224
|
+
signal?: AbortSignal;
|
|
225
|
+
inflight?: Inflight;
|
|
226
|
+
}): Promise<GeneratedClip>;
|
|
227
|
+
private synthesizeOne;
|
|
228
|
+
/**
|
|
229
|
+
* 生成缺的片段(已有的跳过),并发 2。每段完成后调 onProgress。失败记进 failures,成功时清掉这个键的失败。
|
|
230
|
+
*/
|
|
231
|
+
generate(segments: Segment[], locale: string, resolved: ResolvedVoices, store: ProfileStore, opts?: {
|
|
232
|
+
fetch?: FetchLike;
|
|
233
|
+
signal?: AbortSignal;
|
|
234
|
+
inflight?: Inflight;
|
|
235
|
+
onProgress?: (done: number, total: number, failed: number) => void;
|
|
236
|
+
}): Promise<GenerateSummary>;
|
|
237
|
+
}
|
|
238
|
+
export type BundleReport = {
|
|
239
|
+
clips: number;
|
|
240
|
+
bytes: number;
|
|
241
|
+
missing: number;
|
|
242
|
+
profile: string | null;
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* 发布(joplo build / share):把发布 IR 里步骤用到的、当前声音的音频拷进 `<outDir>/voice/`,返回 IR 的 voiceIndex。
|
|
246
|
+
* 同一步同一声音同一语言只留最新的一段(改过字之后旧的不再带上)。
|
|
247
|
+
* missing:当前声音下还没有音频的步骤数(文字与语言无关的步骤按键判断;自动字幕的步骤只要有任意语言的音频就算有)。
|
|
248
|
+
* 没有选用 profile 时不带任何音频(观众端用浏览器朗读)。
|
|
249
|
+
*/
|
|
250
|
+
export declare function bundleVoice(projectDir: string, project: Project, outDir: string, store: ProfileStore, devConfig?: VoiceConfig): {
|
|
251
|
+
index: VoiceIndex | null;
|
|
252
|
+
report: BundleReport;
|
|
253
|
+
};
|