实现 SFX V2 T1 契约与提示词基础
统一前后端 SFX Prompt Unicode canonicalization、字符计数和 2048 边界 固化 52 个音效预设、追加规则及跨语言测试向量 演进共享音频请求响应、duration、Loop 和 V2 metadata 契约 增加 External model 输入矩阵纯函数并保持正式接线归属 T5 补齐前端提交、读取、深拷贝与请求边界回归测试 更新共享计划并标记 T1 完成且当前不可发布
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
export const SOUND_EFFECT_PROMPT_MAX_CODE_POINTS = 2048;
|
||||
|
||||
const LEADING_UNICODE_WHITE_SPACE = /^\p{White_Space}+/u;
|
||||
const TRAILING_UNICODE_WHITE_SPACE = /\p{White_Space}+$/u;
|
||||
|
||||
export type SoundEffectPromptValidationResult =
|
||||
| {
|
||||
ok: true;
|
||||
prompt: string;
|
||||
charCount: number;
|
||||
}
|
||||
| {
|
||||
ok: false;
|
||||
prompt: string;
|
||||
charCount: number;
|
||||
reason: 'empty' | 'too-long';
|
||||
};
|
||||
|
||||
/**
|
||||
* 只删除首尾 Unicode `White_Space`。内部空白、组合字符、ZWJ、U+200B、U+FEFF
|
||||
* 及其它 code point 均原样保留;不能替换为语义不同的原生 `trim()`。
|
||||
*/
|
||||
export function canonicalizeSoundEffectPrompt(prompt: string) {
|
||||
return prompt
|
||||
.replace(LEADING_UNICODE_WHITE_SPACE, '')
|
||||
.replace(TRAILING_UNICODE_WHITE_SPACE, '');
|
||||
}
|
||||
|
||||
export function countSoundEffectPromptCodePoints(prompt: string) {
|
||||
return Array.from(canonicalizeSoundEffectPrompt(prompt)).length;
|
||||
}
|
||||
|
||||
export function validateSoundEffectPrompt(
|
||||
prompt: string,
|
||||
): SoundEffectPromptValidationResult {
|
||||
const canonicalPrompt = canonicalizeSoundEffectPrompt(prompt);
|
||||
const charCount = Array.from(canonicalPrompt).length;
|
||||
if (charCount === 0) {
|
||||
return {
|
||||
ok: false,
|
||||
prompt: canonicalPrompt,
|
||||
charCount,
|
||||
reason: 'empty',
|
||||
};
|
||||
}
|
||||
if (charCount > SOUND_EFFECT_PROMPT_MAX_CODE_POINTS) {
|
||||
return {
|
||||
ok: false,
|
||||
prompt: canonicalPrompt,
|
||||
charCount,
|
||||
reason: 'too-long',
|
||||
};
|
||||
}
|
||||
return { ok: true, prompt: canonicalPrompt, charCount };
|
||||
}
|
||||
Reference in New Issue
Block a user