From f4cf6e066b191a238d190c50e7b0761230e3dc09 Mon Sep 17 00:00:00 2001 From: clh02467605 Date: Fri, 20 Nov 2026 16:04:55 +0800 Subject: [PATCH 1/3] feat(speech): add voice clone/design/manage and audio generate - Add speech voice clone/list/get/update/delete/preview and design - Add speech generate (text_prompt/references; WAV/MP3/OGG Opus only) - Wire core enrollment client, types, endpoints, and bl command map - Harden dry-run redact, save-error voice_id hints, and out-prefix checks - Cover with offline e2e plus gated live CRUD; update bailian-gen skill --- docs/agents/cli-e2e-tests.md | 19 +- packages/cli/src/commands.ts | 16 + .../commands/src/commands/speech/generate.ts | 590 +++++++++++++ .../src/commands/speech/voice/clone.ts | 178 ++++ .../src/commands/speech/voice/delete.ts | 55 ++ .../src/commands/speech/voice/design.ts | 289 +++++++ .../commands/src/commands/speech/voice/get.ts | 65 ++ .../src/commands/speech/voice/list.ts | 105 +++ .../src/commands/speech/voice/preview.ts | 101 +++ .../src/commands/speech/voice/shared.ts | 169 ++++ .../src/commands/speech/voice/update.ts | 82 ++ packages/commands/src/index.ts | 8 + packages/commands/tests/e2e/helpers.ts | 1 + .../tests/e2e/speech-generate.e2e.test.ts | 429 ++++++++++ .../tests/e2e/speech-voice-design.e2e.test.ts | 577 +++++++++++++ .../tests/e2e/speech-voice.e2e.test.ts | 779 ++++++++++++++++++ packages/commands/tests/e2e/topic-routes.ts | 23 + packages/core/src/client/endpoints.ts | 5 + packages/core/src/client/index.ts | 1 + packages/core/src/files/index.ts | 1 + packages/core/src/files/upload.ts | 12 +- packages/core/src/speech/index.ts | 20 + packages/core/src/speech/voice.ts | 191 +++++ packages/core/src/types/api.ts | 41 + packages/core/src/types/index.ts | 3 + packages/core/tests/image-input.test.ts | 12 + packages/core/tests/speech-voice.test.ts | 117 +++ packages/e2e/src/gating.ts | 12 + skills/bailian-gen/SKILL.md | 31 +- skills/bailian-gen/reference/index.md | 22 +- skills/bailian-gen/reference/speech.md | 307 +++++++ 31 files changed, 4232 insertions(+), 29 deletions(-) create mode 100644 packages/commands/src/commands/speech/generate.ts create mode 100644 packages/commands/src/commands/speech/voice/clone.ts create mode 100644 packages/commands/src/commands/speech/voice/delete.ts create mode 100644 packages/commands/src/commands/speech/voice/design.ts create mode 100644 packages/commands/src/commands/speech/voice/get.ts create mode 100644 packages/commands/src/commands/speech/voice/list.ts create mode 100644 packages/commands/src/commands/speech/voice/preview.ts create mode 100644 packages/commands/src/commands/speech/voice/shared.ts create mode 100644 packages/commands/src/commands/speech/voice/update.ts create mode 100644 packages/commands/tests/e2e/speech-generate.e2e.test.ts create mode 100644 packages/commands/tests/e2e/speech-voice-design.e2e.test.ts create mode 100644 packages/commands/tests/e2e/speech-voice.e2e.test.ts create mode 100644 packages/core/src/speech/voice.ts create mode 100644 packages/core/tests/speech-voice.test.ts diff --git a/docs/agents/cli-e2e-tests.md b/docs/agents/cli-e2e-tests.md index 20a15155f..e2a344393 100644 --- a/docs/agents/cli-e2e-tests.md +++ b/docs/agents/cli-e2e-tests.md @@ -126,15 +126,16 @@ describe.skipIf()("e2e: (DashScope …)", () => { ## skip 条件(`e2e/gating`,commands helpers re-export) -| 场景 | 条件 | -| ----------------------- | ---------------------------------------------------------------------------------------------------------- | -| 文本/搜索/配置 | `isDashScopeE2EReady()`(`.env` 中的 `DASHSCOPE_API_KEY`) | -| 图像/语音 | `isBailianE2EMediaEnabled() && isDashScopeE2EReady()` | -| 视频 | `isBailianE2EVideoEnabled() && isDashScopeE2EReady()` | -| OpenAPI AK/SK | `isOpenApiE2EReady()`(`.env` 中必须同时提供完整 AK/SK) | -| 视频 download/task | 另需 `BAILIAN_E2E_VIDEO_TASK_ID` | -| 知识库 chat/search live | `isChatE2EReady()` / `isSearchE2EReady()`(`knowledge chat/search`,需 `BAILIAN_WORKSPACE_ID` + agent ID) | -| 记忆 live | `isMemoryE2EReady()`(另需 `BAILIAN_E2E_MEMORY_LIBRARY_ID`;记忆服务需账号单独开通) | +| 场景 | 条件 | +| ----------------------- | ----------------------------------------------------------------------------------------------------------- | +| 文本/搜索/配置 | `isDashScopeE2EReady()`(`.env` 中的 `DASHSCOPE_API_KEY`) | +| 图像/语音 | `isBailianE2EMediaEnabled() && isDashScopeE2EReady()` | +| `speech generate` live | `isSpeechGenerateE2EReady()`(另需 `BAILIAN_WORKSPACE_ID`;缺 workspace 必须 skip,禁止 early-return 假绿) | +| 视频 | `isBailianE2EVideoEnabled() && isDashScopeE2EReady()` | +| OpenAPI AK/SK | `isOpenApiE2EReady()`(`.env` 中必须同时提供完整 AK/SK) | +| 视频 download/task | 另需 `BAILIAN_E2E_VIDEO_TASK_ID` | +| 知识库 chat/search live | `isChatE2EReady()` / `isSearchE2EReady()`(`knowledge chat/search`,需 `BAILIAN_WORKSPACE_ID` + agent ID) | +| 记忆 live | `isMemoryE2EReady()`(另需 `BAILIAN_E2E_MEMORY_LIBRARY_ID`;记忆服务需账号单独开通) | ## 用例类型 diff --git a/packages/cli/src/commands.ts b/packages/cli/src/commands.ts index 717f45a8e..46ca22e11 100644 --- a/packages/cli/src/commands.ts +++ b/packages/cli/src/commands.ts @@ -103,11 +103,19 @@ import { searchWeb, speechSynthesize, speechRecognize, + speechGenerate, speechVocabularyCreate, speechVocabularyList, speechVocabularyGet, speechVocabularyUpdate, speechVocabularyDelete, + speechVoiceClone, + speechVoiceDesign, + speechVoiceList, + speechVoiceGet, + speechVoiceUpdate, + speechVoiceDelete, + speechVoicePreview, fileUpload, fileUrl, consoleCall, @@ -463,11 +471,19 @@ export const commands: Record = { "search web": searchWeb, "speech synthesize": speechSynthesize, "speech recognize": speechRecognize, + "speech generate": speechGenerate, "speech vocabulary create": speechVocabularyCreate, "speech vocabulary list": speechVocabularyList, "speech vocabulary get": speechVocabularyGet, "speech vocabulary update": speechVocabularyUpdate, "speech vocabulary delete": speechVocabularyDelete, + "speech voice clone": speechVoiceClone, + "speech voice design": speechVoiceDesign, + "speech voice list": speechVoiceList, + "speech voice get": speechVoiceGet, + "speech voice update": speechVoiceUpdate, + "speech voice delete": speechVoiceDelete, + "speech voice preview": speechVoicePreview, "file upload": fileUpload, "file url": fileUrl, "console call": consoleCall, diff --git a/packages/commands/src/commands/speech/generate.ts b/packages/commands/src/commands/speech/generate.ts new file mode 100644 index 000000000..4d9cbdf69 --- /dev/null +++ b/packages/commands/src/commands/speech/generate.ts @@ -0,0 +1,590 @@ +import { existsSync, statSync } from "fs"; +import { basename, extname, join } from "path"; +import { randomBytes } from "crypto"; +import { + audioFileToDataUri, + BailianError, + defineCommand, + ExitCode, + formatCommand, + redactDataUri, + REGIONS, + resolveOutputDir, + speechSynthesizePath, + workspaceMaaSBaseUrl, + type AudioGenerationReference, + type AudioGenerationRequest, + type AudioGenerationResponse, + type FlagsDef, + type ParsedFlags, +} from "bailian-cli-core"; +import { downloadFile } from "bailian-cli-runtime"; + +const DEFAULT_MODEL = "qwen-audio-3.1-tts-next"; +const MAX_TEXT_PROMPT_CHARS = 3000; +const MAX_REFERENCES = 3; +const MAX_REFERENCE_BYTES = 10 * 1024 * 1024; +/** Output container formats (includes raw PCM). Reference inputs are stricter. */ +const SUPPORTED_FORMATS = new Set(["wav", "mp3", "pcm"]); +/** Reference audio extensions: WAV / MP3 / OGG Opus only (no bare PCM). */ +const REFERENCE_AUDIO_EXTENSIONS = new Set([".wav", ".mp3", ".ogg", ".oga", ".opus"]); +/** Data URI audio subtypes accepted for references (aligned with REFERENCE_AUDIO_EXTENSIONS). */ +const REFERENCE_AUDIO_MIME_SUBTYPES = new Set([ + "wav", + "x-wav", + "wave", + "mpeg", + "mp3", + "ogg", + "opus", +]); +const REFERENCE_AUDIO_HINT = "Use WAV, MP3, or OGG Opus reference audio."; +const SUPPORTED_SAMPLE_RATES = new Set([8000, 16000, 24000, 44100, 48000]); +const SUPPORTED_CHANNELS = new Set([1, 2]); +const SUPPORTED_CLOUD_REGIONS = new Set(["cn-beijing", "ap-southeast-1"]); +const WORKSPACE_HOST_LABEL = /^[a-zA-Z0-9_-]+$/; + +const GENERATE_FLAGS = { + textPrompt: { + type: "string", + valueHint: "", + description: { + "en-US": "Audio generation prompt (up to 3000 characters)", + "zh-CN": "音频生成提示词(最多 3000 字符)", + }, + required: true, + }, + audio: { + type: "array", + valueHint: "", + description: { + "en-US": + "Reference audio URL, local file, or Data URI (repeatable, up to 3; WAV/MP3/OGG Opus only). Referenced in order as @voice1/@voice2/@voice3", + "zh-CN": + "参考音频 URL、本地文件或 Data URI(可重复,最多 3 条;仅 WAV/MP3/OGG Opus)。按顺序对应 @voice1/@voice2/@voice3", + }, + }, + model: { + type: "string", + valueHint: "", + description: { + "en-US": `Model id (default: ${DEFAULT_MODEL})`, + "zh-CN": `模型 ID(默认:${DEFAULT_MODEL})`, + }, + }, + workspaceId: { + type: "string", + valueHint: "", + description: { + "en-US": "Workspace ID for the default MaaS endpoint (or set BAILIAN_WORKSPACE_ID)", + "zh-CN": "默认 MaaS Endpoint 使用的 Workspace ID(也可设置 BAILIAN_WORKSPACE_ID)", + }, + }, + format: { + type: "string", + valueHint: "", + description: { + "en-US": "Output format: wav / mp3 / pcm (default: wav)", + "zh-CN": "输出格式:wav / mp3 / pcm(默认:wav)", + }, + }, + sampleRate: { + type: "number", + valueHint: "", + description: { + "en-US": "Sample rate: 8000 / 16000 / 24000 / 44100 / 48000", + "zh-CN": "采样率:8000 / 16000 / 24000 / 44100 / 48000", + }, + }, + channels: { + type: "number", + valueHint: "", + description: { + "en-US": "Channels: 1 or 2", + "zh-CN": "声道数:1 或 2", + }, + }, + volume: { + type: "number", + valueHint: "", + description: { + "en-US": "Volume 0-100 (integer)", + "zh-CN": "音量 0-100(整数)", + }, + }, + rate: { + type: "number", + valueHint: "", + description: { + "en-US": "Speech rate 0.5-2", + "zh-CN": "语速 0.5-2", + }, + }, + seed: { + type: "number", + valueHint: "", + description: { + "en-US": "Random seed (integer)", + "zh-CN": "随机种子(整数)", + }, + }, + aigcTag: { + type: "boolean", + valueHint: "", + description: { + "en-US": "Enable AIGC tag (enable_aigc_tag)", + "zh-CN": "启用 AIGC 标识(enable_aigc_tag)", + }, + }, + enableCbr: { + type: "boolean", + valueHint: "", + description: { + "en-US": "Enable MP3 CBR encoding (MP3 only)", + "zh-CN": "启用 MP3 CBR 编码(仅 MP3)", + }, + }, + bitRate: { + type: "number", + valueHint: "", + description: { + "en-US": "MP3 bit rate when CBR is enabled", + "zh-CN": "启用 CBR 时的 MP3 码率", + }, + }, + quality: { + type: "number", + valueHint: "<0-9>", + description: { + "en-US": "MP3 VBR quality 0-9 (when CBR is off)", + "zh-CN": "MP3 VBR 质量 0-9(未启用 CBR 时)", + }, + }, + outDir: { + type: "string", + valueHint: "", + description: { + "en-US": "Output directory for the generated audio", + "zh-CN": "生成音频的输出目录", + }, + }, + outPrefix: { + type: "string", + valueHint: "", + description: { + "en-US": "Filename prefix (basename only; cannot escape the output directory)", + "zh-CN": "文件名前缀(仅文件名片段;不能跳出输出目录)", + }, + }, +} satisfies FlagsDef; +type GenerateFlags = ParsedFlags; + +function cloudRegionFromBaseUrl(baseUrl: string): string | undefined { + let hostname: string | undefined; + try { + hostname = new URL(baseUrl).hostname.toLowerCase(); + } catch { + return undefined; + } + if (hostname === new URL(REGIONS.cn).hostname) return "cn-beijing"; + if (hostname === new URL(REGIONS.intl).hostname) return "ap-southeast-1"; + const maasMatch = /^[a-z0-9-]+\.(cn-beijing|ap-southeast-1|us-east-1)\.maas\.aliyuncs\.com$/.exec( + hostname, + ); + return maasMatch?.[1]; +} + +function assertSupportedAudioEndpoint( + endpoint: string, + localize: (text: { "en-US": string; "zh-CN": string }) => string, +): void { + let hostname: string; + try { + hostname = new URL(endpoint).hostname.toLowerCase(); + } catch { + throw new BailianError( + localize({ + "en-US": "Invalid audio generation endpoint.", + "zh-CN": "音频生成 Endpoint 无效。", + }), + ExitCode.USAGE, + ); + } + + if (hostname === new URL(REGIONS.cn).hostname || hostname === new URL(REGIONS.intl).hostname) { + return; + } + if (/^[a-z0-9-]+\.(cn-beijing|ap-southeast-1)\.maas\.aliyuncs\.com$/.test(hostname)) { + return; + } + // Custom proxies are allowed; known unsupported official regions are rejected. + if ( + hostname === new URL(REGIONS.us).hostname || + /^[a-z0-9-]+\.us-east-1\.maas\.aliyuncs\.com$/.test(hostname) + ) { + throw new BailianError( + localize({ + "en-US": 'Audio generation is not available in region "us" / us-east-1.', + "zh-CN": "音频生成在美国地域(us / us-east-1)不可用。", + }), + ExitCode.USAGE, + localize({ + "en-US": + "Use Beijing or Singapore (cn-beijing / ap-southeast-1), or an explicit supported --base-url.", + "zh-CN": + "请使用北京或新加坡(cn-beijing / ap-southeast-1),或显式指定受支持的 --base-url。", + }), + ); + } +} + +function resolveGenerateEndpoint(ctx: { + flags: Pick; + settings: { workspaceId?: string }; + identity: { binName: string }; + client: { url: (path: string, defaultBaseUrl?: () => string) => string; baseUrl: string }; + localize: (text: { "en-US": string; "zh-CN": string }) => string; +}): string { + const endpoint = ctx.client.url(speechSynthesizePath(), () => { + const workspaceId = ctx.flags.workspaceId || ctx.settings.workspaceId; + if (!workspaceId) { + throw new BailianError( + ctx.localize({ + "en-US": "Workspace ID is required for the default audio generation endpoint.", + "zh-CN": "默认音频生成 Endpoint 需要 Workspace ID。", + }), + ExitCode.USAGE, + ctx.localize({ + "en-US": `Pass --workspace-id, set BAILIAN_WORKSPACE_ID, configure: ${formatCommand(ctx.identity, "config set workspace_id ")}, or override with --base-url.`, + "zh-CN": `请传 --workspace-id、设置 BAILIAN_WORKSPACE_ID、执行 ${formatCommand(ctx.identity, "config set workspace_id ")},或用 --base-url 覆盖。`, + }), + ); + } + if (!WORKSPACE_HOST_LABEL.test(workspaceId)) { + throw new BailianError( + ctx.localize({ + "en-US": + "Workspace ID must contain only letters, digits, hyphens or underscores (no dots, slashes, or ports).", + "zh-CN": "Workspace ID 只能包含字母、数字、连字符或下划线(不能含点号、斜杠或端口)。", + }), + ExitCode.USAGE, + ); + } + const cloudRegion = cloudRegionFromBaseUrl(ctx.client.baseUrl) ?? "cn-beijing"; + if (!SUPPORTED_CLOUD_REGIONS.has(cloudRegion)) { + throw new BailianError( + ctx.localize({ + "en-US": `Audio generation does not support cloud region "${cloudRegion}".`, + "zh-CN": `音频生成不支持云地域 "${cloudRegion}"。`, + }), + ExitCode.USAGE, + ctx.localize({ + "en-US": "Use cn-beijing or ap-southeast-1.", + "zh-CN": "请使用 cn-beijing 或 ap-southeast-1。", + }), + ); + } + return workspaceMaaSBaseUrl(workspaceId, cloudRegion); + }); + assertSupportedAudioEndpoint(endpoint, ctx.localize); + return endpoint; +} + +function assertSupportedReferenceDataUri(dataUri: string): void { + const mimeMatch = /^data:audio\/([^;,]+)/i.exec(dataUri); + if (!mimeMatch) { + throw new BailianError("Reference Data URI must use an audio/* MIME type.", ExitCode.USAGE); + } + if (!/;base64,/i.test(dataUri)) { + throw new BailianError( + "Reference Data URI must be base64-encoded (…;base64,…).", + ExitCode.USAGE, + ); + } + const subtype = mimeMatch[1]!.toLowerCase(); + if (!REFERENCE_AUDIO_MIME_SUBTYPES.has(subtype)) { + throw new BailianError( + `Unsupported reference audio MIME "audio/${subtype}".`, + ExitCode.USAGE, + REFERENCE_AUDIO_HINT, + ); + } +} + +function assertSupportedReferenceExtension(filePath: string): void { + const extension = extname(filePath).toLowerCase() || "unknown"; + if (!REFERENCE_AUDIO_EXTENSIONS.has(extension)) { + throw new BailianError( + `Unsupported reference audio format "${extension}".`, + ExitCode.USAGE, + REFERENCE_AUDIO_HINT, + ); + } +} + +function buildReference( + source: string, + options: { encodeLocal: boolean }, +): AudioGenerationReference & { localPath?: string } { + const trimmed = source.trim(); + if (trimmed.length === 0) { + throw new BailianError("--audio must not be empty.", ExitCode.USAGE); + } + if (/^data:/i.test(trimmed)) { + assertSupportedReferenceDataUri(trimmed); + return { audio_data: trimmed }; + } + if (/^https?:\/\//i.test(trimmed)) { + return { audio_url: trimmed }; + } + if (!existsSync(trimmed)) { + throw new BailianError(`Audio file not found: ${trimmed}`, ExitCode.USAGE); + } + const stat = statSync(trimmed); + if (!stat.isFile()) { + throw new BailianError(`Not a file: ${trimmed}`, ExitCode.USAGE); + } + if (stat.size > MAX_REFERENCE_BYTES) { + throw new BailianError(`Reference audio exceeds 10 MB: ${trimmed}`, ExitCode.USAGE); + } + assertSupportedReferenceExtension(trimmed); + if (!options.encodeLocal) { + return { audio_data: trimmed, localPath: trimmed }; + } + return { audio_data: audioFileToDataUri(trimmed), localPath: trimmed }; +} + +function redactReferences( + references: Array | undefined, +): AudioGenerationReference[] | undefined { + if (!references) return undefined; + return references.map((reference) => { + if (reference.audio_url) return { audio_url: reference.audio_url }; + if (reference.localPath && reference.audio_data === reference.localPath) { + return { audio_data: `` }; + } + if (reference.audio_data) return { audio_data: redactDataUri(reference.audio_data) }; + return {}; + }); +} + +function validateVoiceMentions(textPrompt: string, referenceCount: number): string | undefined { + const mentions = textPrompt.matchAll(/@voice(\d+)/gi); + for (const match of mentions) { + const index = Number(match[1]); + if (!Number.isInteger(index) || index < 1 || index > referenceCount) { + return `Prompt references @voice${index} but only ${referenceCount} --audio reference(s) were provided.`; + } + } + return undefined; +} + +function safeOutPrefix(prefix: string | undefined): string { + if (prefix === undefined || prefix.trim().length === 0) { + return `audio-gen-${randomBytes(6).toString("hex")}`; + } + const base = basename(prefix.trim()); + if (base !== prefix.trim() || base.includes("..") || base.includes("/") || base.includes("\\")) { + throw new BailianError( + "--out-prefix must be a filename fragment without directory separators.", + ExitCode.USAGE, + ); + } + return base; +} + +export default defineCommand({ + description: { + "en-US": "Generate podcast/dialogue audio content from a text prompt", + "zh-CN": "根据文本提示生成播客/对白等音频内容", + }, + auth: "apiKey", + usageArgs: "--text-prompt [--audio ] [flags]", + flags: GENERATE_FLAGS, + notes: [ + { + "en-US": + "Unlike `speech synthesize`, this command uses text_prompt/references and does not accept system or cloned voice IDs.", + "zh-CN": + "与 `speech synthesize` 不同,本命令使用 text_prompt/references,不接受系统音色或复刻 voice ID。", + }, + { + "en-US": + "Default endpoint needs a workspace ID (Beijing/Singapore). Explicit --base-url / DASHSCOPE_BASE_URL / Profile base_url overrides without requiring workspace.", + "zh-CN": + "默认 Endpoint 需要 Workspace ID(北京/新加坡)。显式 --base-url / DASHSCOPE_BASE_URL / Profile base_url 可覆盖且不必提供 workspace。", + }, + ], + exampleArgs: [ + { + "en-US": + '--workspace-id --text-prompt "Generate a short podcast about city nightscapes." --dry-run --output json', + "zh-CN": + '--workspace-id --text-prompt "生成两位主持人讨论城市夜景的短播客。" --dry-run --output json', + }, + { + "en-US": + '--workspace-id --text-prompt "Introduce today\'s weather with @voice1." --audio ./reference.wav --out-dir ./audio', + "zh-CN": + '--workspace-id --text-prompt "用 @voice1 的声音介绍今天的天气。" --audio ./reference.wav --out-dir ./audio', + }, + ], + validate: (flags: GenerateFlags) => { + const textPrompt = flags.textPrompt.trim(); + if (textPrompt.length === 0) return "--text-prompt must not be empty."; + if (textPrompt.length > MAX_TEXT_PROMPT_CHARS) { + return `--text-prompt must be at most ${MAX_TEXT_PROMPT_CHARS} characters.`; + } + if (flags.audio !== undefined && flags.audio.length > MAX_REFERENCES) { + return `Provide at most ${MAX_REFERENCES} --audio references.`; + } + const referenceCount = flags.audio?.length ?? 0; + const mentionError = validateVoiceMentions(textPrompt, referenceCount); + if (mentionError) return mentionError; + + if (flags.format !== undefined && !SUPPORTED_FORMATS.has(flags.format)) { + return "--format must be wav, mp3, or pcm."; + } + if (flags.sampleRate !== undefined && !SUPPORTED_SAMPLE_RATES.has(flags.sampleRate)) { + return "--sample-rate must be 8000, 16000, 24000, 44100, or 48000."; + } + if (flags.channels !== undefined && !SUPPORTED_CHANNELS.has(flags.channels)) { + return "--channels must be 1 or 2."; + } + if ( + flags.volume !== undefined && + (!Number.isInteger(flags.volume) || flags.volume < 0 || flags.volume > 100) + ) { + return "--volume must be an integer between 0 and 100."; + } + if (flags.rate !== undefined && (flags.rate < 0.5 || flags.rate > 2)) { + return "--rate must be between 0.5 and 2."; + } + if (flags.seed !== undefined && !Number.isInteger(flags.seed)) { + return "--seed must be an integer."; + } + if ( + flags.quality !== undefined && + (!Number.isInteger(flags.quality) || flags.quality < 0 || flags.quality > 9) + ) { + return "--quality must be an integer between 0 and 9."; + } + const format = flags.format ?? "wav"; + if (flags.enableCbr !== undefined && format !== "mp3") { + return "--enable-cbr is only valid with --format mp3."; + } + if (flags.bitRate !== undefined && (format !== "mp3" || flags.enableCbr !== true)) { + return "--bit-rate requires --format mp3 and --enable-cbr true."; + } + if (flags.quality !== undefined && (format !== "mp3" || flags.enableCbr === true)) { + return "--quality requires --format mp3 without CBR (--enable-cbr false or omitted)."; + } + return undefined; + }, + async run(ctx) { + const { settings, flags } = ctx; + const textPrompt = flags.textPrompt.trim(); + const model = flags.model || DEFAULT_MODEL; + const format = (flags.format ?? "wav") as "wav" | "mp3" | "pcm"; + const dryRun = settings.dryRun === true; + + const endpoint = resolveGenerateEndpoint(ctx); + + const rawAudios = flags.audio ?? []; + const references = rawAudios.map((source) => buildReference(source, { encodeLocal: !dryRun })); + + const input: AudioGenerationRequest["input"] = { + text_prompt: textPrompt, + }; + if (references.length > 0) { + input.references = references.map((reference) => { + const entry: AudioGenerationReference = {}; + if (reference.audio_url !== undefined) entry.audio_url = reference.audio_url; + if (reference.audio_data !== undefined) entry.audio_data = reference.audio_data; + return entry; + }); + } + input.format = format; + if (flags.sampleRate !== undefined) input.sample_rate = flags.sampleRate; + if (flags.channels !== undefined) input.channels = flags.channels; + if (flags.volume !== undefined) input.volume = flags.volume; + if (flags.rate !== undefined) input.rate = flags.rate; + if (flags.seed !== undefined) input.seed = flags.seed; + if (flags.aigcTag !== undefined) input.enable_aigc_tag = flags.aigcTag; + if (flags.enableCbr !== undefined) input.enable_cbr = flags.enableCbr; + if (flags.bitRate !== undefined) input.bit_rate = flags.bitRate; + if (flags.quality !== undefined) input.quality = flags.quality; + + const request: AudioGenerationRequest = { model, input }; + + // Validate destination naming before POST so bad --out-prefix never burns a generation. + const prefix = safeOutPrefix(flags.outPrefix); + + if (dryRun) { + const dryInput = { + ...input, + references: redactReferences(references), + }; + ctx.output.result({ + endpoint, + request: { model, input: dryInput }, + }); + return; + } + + const destDir = resolveOutputDir(settings, { + flagDir: flags.outDir, + subDir: flags.outDir ? undefined : "speech", + }); + const destPath = join(destDir, `${prefix}.${format}`); + + const response = await ctx.client.requestJson({ + path: endpoint, + method: "POST", + body: request, + }); + + const audioUrl = response.output?.audio?.url?.trim(); + if (!audioUrl) { + throw new BailianError( + ctx.localize({ + "en-US": "Audio generation response did not include output.audio.url.", + "zh-CN": "音频生成响应未包含 output.audio.url。", + }), + ExitCode.GENERAL, + ctx.localize({ + "en-US": + "This mode returns a download URL (audio.data is empty). Do not retry automatically.", + "zh-CN": "此模式返回下载 URL(audio.data 为空)。请勿自动重试。", + }), + ); + } + + await downloadFile(audioUrl, destPath, { + quiet: settings.quiet || settings.output === "json", + }); + + const result = { + ...response, + model, + url: audioUrl, + saved: destPath, + }; + + let text: string | undefined; + if (settings.output === "text") { + const duration = response.output?.audio?.duration; + const lines = [ + `model: ${model}`, + `saved: ${destPath}`, + `format: ${format}`, + flags.sampleRate !== undefined ? `sample_rate: ${flags.sampleRate}` : undefined, + flags.channels !== undefined ? `channels: ${flags.channels}` : undefined, + duration !== undefined ? `duration: ${duration}` : undefined, + response.request_id ? `request_id: ${response.request_id}` : undefined, + ]; + text = lines.filter((line): line is string => line !== undefined).join("\n"); + } + ctx.output.result(result, { text }); + }, +}); diff --git a/packages/commands/src/commands/speech/voice/clone.ts b/packages/commands/src/commands/speech/voice/clone.ts new file mode 100644 index 000000000..c8439e7f3 --- /dev/null +++ b/packages/commands/src/commands/speech/voice/clone.ts @@ -0,0 +1,178 @@ +import { + buildVoiceRequest, + createCloneVoice, + defineCommand, + speechVoiceCustomizationPath, + type FlagsDef, + type ParsedFlags, +} from "bailian-cli-core"; +import { + redactVoiceAudioInRequest, + resolveVoiceAudioSource, + validateLanguageHints, + validateVoicePrefix, + VOICE_PREFIX_FLAG, + VOICE_RIGHTS_NOTES, +} from "./shared.ts"; + +const CLONE_FLAGS = { + audio: { + type: "string", + valueHint: "", + description: { + "en-US": "Reference audio URL or local file", + "zh-CN": "参考音频 URL 或本地文件", + }, + required: true, + }, + prefix: { + ...VOICE_PREFIX_FLAG.prefix, + required: true, + }, + targetModel: { + type: "string", + valueHint: "", + description: { + "en-US": + "Target TTS model for this voice (required). Must match later `speech synthesize --model`", + "zh-CN": "该音色绑定的目标 TTS 模型(必填)。必须与后续 `speech synthesize --model` 一致", + }, + required: true, + }, + languageHint: { + type: "array", + valueHint: "", + description: { + "en-US": "Language hint for the reference audio (at most one; mapped to language_hints)", + "zh-CN": "参考音频语言提示(最多一项;映射为 language_hints)", + }, + }, + volumeNormalize: { + type: "boolean", + valueHint: "", + description: { + "en-US": 'Enable volume normalization (sent as string "true"/"false")', + "zh-CN": '启用音量归一化(以字符串 "true"/"false" 下发)', + }, + }, + maxPromptAudioLength: { + type: "number", + valueHint: "", + description: { + "en-US": "Max prompt audio length in seconds (3-30)", + "zh-CN": "参考音频最大时长(秒,范围 3-30)", + }, + }, + preprocess: { + type: "boolean", + valueHint: "", + description: { + "en-US": "Enable audio preprocess (enable_preprocess)", + "zh-CN": "启用音频预处理(enable_preprocess)", + }, + }, +} satisfies FlagsDef; +type CloneFlags = ParsedFlags; + +export default defineCommand({ + description: { + "en-US": "Clone a custom voice from reference audio", + "zh-CN": "从参考音频复刻自定义音色", + }, + auth: "apiKey", + usageArgs: "--audio --prefix --target-model [flags]", + flags: CLONE_FLAGS, + notes: [ + ...VOICE_RIGHTS_NOTES, + { + "en-US": + "Creation returns voice_id immediately. Review status is DEPLOYING/OK/UNDEPLOYED — use `speech voice get` to check; preview reference audio with `speech voice preview`.", + "zh-CN": + "创建会立即返回 voice_id。审核状态为 DEPLOYING/OK/UNDEPLOYED — 用 `speech voice get` 查询;用 `speech voice preview` 获取参考音频。", + }, + ], + exampleArgs: [ + { + "en-US": + "--audio ./reference.wav --prefix demo --target-model cosyvoice-v3-flash --dry-run --output json", + "zh-CN": + "--audio ./reference.wav --prefix demo --target-model cosyvoice-v3-flash --dry-run --output json", + }, + { + "en-US": + "--audio https://example.com/ref.wav --prefix Demo01 --target-model cosyvoice-v3-flash --language-hint zh", + "zh-CN": + "--audio https://example.com/ref.wav --prefix Demo01 --target-model cosyvoice-v3-flash --language-hint zh", + }, + ], + validate: (flags: CloneFlags) => { + const prefixError = validateVoicePrefix(flags.prefix); + if (prefixError) return prefixError; + const hintError = validateLanguageHints(flags.languageHint); + if (hintError) return hintError; + if ( + flags.maxPromptAudioLength !== undefined && + (flags.maxPromptAudioLength < 3 || flags.maxPromptAudioLength > 30) + ) { + return "--max-prompt-audio-length must be between 3 and 30."; + } + return undefined; + }, + async run(ctx) { + const { settings, flags } = ctx; + const prefix = flags.prefix as string; + + const dryRun = settings.dryRun === true; + const resolved = resolveVoiceAudioSource(flags.audio, { encodeLocal: !dryRun }); + + const input: Record = { + target_model: flags.targetModel, + prefix, + audio: resolved.audio, + }; + if (flags.languageHint !== undefined && flags.languageHint.length > 0) { + input.language_hints = [flags.languageHint[0]!]; + } + if (flags.volumeNormalize !== undefined) { + input.enable_volume_normalization = flags.volumeNormalize ? "true" : "false"; + } + if (flags.maxPromptAudioLength !== undefined) { + input.max_prompt_audio_length = flags.maxPromptAudioLength; + } + if (flags.preprocess !== undefined) { + input.enable_preprocess = flags.preprocess; + } + + const request = buildVoiceRequest("create_voice", input); + + if (dryRun) { + ctx.output.result({ + endpoint: ctx.client.url(speechVoiceCustomizationPath()), + request: redactVoiceAudioInRequest(request, resolved.localPath), + }); + return; + } + + const response = await createCloneVoice(ctx.client, { + target_model: flags.targetModel, + prefix, + audio: resolved.audio, + language_hints: + flags.languageHint !== undefined && flags.languageHint.length > 0 + ? [flags.languageHint[0]!] + : undefined, + enable_volume_normalization: + flags.volumeNormalize !== undefined + ? flags.volumeNormalize + ? "true" + : "false" + : undefined, + max_prompt_audio_length: flags.maxPromptAudioLength, + enable_preprocess: flags.preprocess, + }); + + ctx.output.result(response, { + text: settings.output === "text" ? (response.output?.voice_id ?? "") : undefined, + }); + }, +}); diff --git a/packages/commands/src/commands/speech/voice/delete.ts b/packages/commands/src/commands/speech/voice/delete.ts new file mode 100644 index 000000000..ec3cbf255 --- /dev/null +++ b/packages/commands/src/commands/speech/voice/delete.ts @@ -0,0 +1,55 @@ +import { + buildVoiceRequest, + defineCommand, + deleteVoice, + speechVoiceCustomizationPath, + type FlagsDef, + type ParsedFlags, +} from "bailian-cli-core"; +import { validateVoiceId, VOICE_ID_FLAG } from "./shared.ts"; + +const DELETE_FLAGS = { + ...VOICE_ID_FLAG, +} satisfies FlagsDef; +type DeleteFlags = ParsedFlags; + +export default defineCommand({ + description: { + "en-US": "Delete a custom voice", + "zh-CN": "删除自定义音色", + }, + auth: "apiKey", + risk: { + level: "high", + message: { + "en-US": + "This permanently deletes the voice ID. Synthesis that still references it will fail and the action cannot be undone.", + "zh-CN": "该操作会永久删除此音色 ID。仍引用它的合成会失败,且无法撤销。", + }, + }, + usageArgs: "--id ", + flags: DELETE_FLAGS, + notes: [ + { + "en-US": "Deletion is irreversible. Prefer --dry-run first to confirm the request body.", + "zh-CN": "删除不可撤销。建议先用 --dry-run 确认请求体。", + }, + ], + exampleArgs: ["--id --dry-run --output json", "--id --yes"], + validate: (flags: DeleteFlags) => validateVoiceId(flags.id), + async run(ctx) { + const { settings, flags } = ctx; + const voiceId = flags.id; + + if (settings.dryRun) { + ctx.output.result({ + endpoint: ctx.client.url(speechVoiceCustomizationPath()), + request: buildVoiceRequest("delete_voice", { voice_id: voiceId }), + }); + return; + } + + const response = await deleteVoice(ctx.client, voiceId); + ctx.output.result(response, { text: voiceId }); + }, +}); diff --git a/packages/commands/src/commands/speech/voice/design.ts b/packages/commands/src/commands/speech/voice/design.ts new file mode 100644 index 000000000..6382c8585 --- /dev/null +++ b/packages/commands/src/commands/speech/voice/design.ts @@ -0,0 +1,289 @@ +import { mkdirSync, writeFileSync } from "fs"; +import { dirname, join } from "path"; +import { randomBytes } from "crypto"; +import { + BailianError, + buildVoiceRequest, + createDesignVoice, + defineCommand, + ExitCode, + resolveOutputDir, + speechVoiceCustomizationPath, + type FlagsDef, + type ParsedFlags, +} from "bailian-cli-core"; +import { + validateLanguageHints, + validateVoicePrefix, + VOICE_PREFIX_FLAG, + VOICE_RIGHTS_NOTES, +} from "./shared.ts"; + +const DESIGN_SAMPLE_RATES = new Set([16000, 24000, 48000]); +const DESIGN_FORMATS = new Set(["pcm", "wav", "mp3"]); + +const DESIGN_FLAGS = { + prompt: { + type: "string", + valueHint: "", + description: { + "en-US": "Voice description (up to 500 characters)", + "zh-CN": "音色描述(最多 500 字符)", + }, + required: true, + }, + previewText: { + type: "string", + valueHint: "", + description: { + "en-US": "Preview text spoken in the design sample (15-200 characters)", + "zh-CN": "设计预览朗读文本(15-200 字符)", + }, + required: true, + }, + prefix: { + ...VOICE_PREFIX_FLAG.prefix, + required: true, + }, + targetModel: { + type: "string", + valueHint: "", + description: { + "en-US": + "Target TTS model for this voice (required). Must match later `speech synthesize --model`", + "zh-CN": "该音色绑定的目标 TTS 模型(必填)。必须与后续 `speech synthesize --model` 一致", + }, + required: true, + }, + languageHint: { + type: "array", + valueHint: "", + description: { + "en-US": "Language hint (zh/en; at most one; server default: zh)", + "zh-CN": "语言提示(zh/en;最多一项;服务端默认 zh)", + }, + }, + sampleRate: { + type: "number", + valueHint: "", + description: { + "en-US": "Preview sample rate: 16000 / 24000 / 48000 (server default: 24000)", + "zh-CN": "预览采样率:16000 / 24000 / 48000(服务端默认 24000)", + }, + }, + format: { + type: "string", + valueHint: "", + description: { + "en-US": "Preview format: pcm / wav / mp3 (server default: wav)", + "zh-CN": "预览格式:pcm / wav / mp3(服务端默认 wav)", + }, + }, + out: { + type: "string", + valueHint: "", + description: { + "en-US": "Local path for the preview audio (optional; defaults under the audio output dir)", + "zh-CN": "预览音频本地路径(可选;默认写入音频输出目录)", + }, + }, +} satisfies FlagsDef; +type DesignFlags = ParsedFlags; + +function extensionForFormat(format: string | undefined): string { + if (format === "mp3") return "mp3"; + if (format === "pcm") return "pcm"; + return "wav"; +} + +export default defineCommand({ + description: { + "en-US": "Design a custom voice from a text description", + "zh-CN": "用文字描述设计自定义音色", + }, + auth: "apiKey", + usageArgs: "--prompt --preview-text --prefix --target-model [flags]", + flags: DESIGN_FLAGS, + notes: [ + ...VOICE_RIGHTS_NOTES, + { + "en-US": + "The response includes a one-shot preview_audio (Base64). Querying the voice later may not return preview_audio again. Formal listen tests use `speech synthesize`.", + "zh-CN": + "响应含一次性 preview_audio(Base64)。之后查询音色不一定再次返回预览。正式试听请用 `speech synthesize`。", + }, + ], + exampleArgs: [ + { + "en-US": + '--prompt "Calm warm Chinese announcer" --preview-text "Welcome to today\'s program." --prefix demo --target-model cosyvoice-v3.5-plus --dry-run --output json', + "zh-CN": + '--prompt "沉稳温暖的中文播音员" --preview-text "各位听众朋友们大家好,欢迎收听今天的节目。" --prefix demo --target-model cosyvoice-v3.5-plus --dry-run --output json', + }, + ], + validate: (flags: DesignFlags) => { + const prefixError = validateVoicePrefix(flags.prefix); + if (prefixError) return prefixError; + + const prompt = flags.prompt.trim(); + if (prompt.length === 0) return "--prompt must not be empty."; + if (prompt.length > 500) return "--prompt must be at most 500 characters."; + + const previewText = flags.previewText.trim(); + if (previewText.length === 0) return "--preview-text must not be empty."; + if (previewText.length < 15 || previewText.length > 200) { + return "--preview-text must be between 15 and 200 characters."; + } + + const hintError = validateLanguageHints(flags.languageHint); + if (hintError) return hintError; + if (flags.languageHint !== undefined && flags.languageHint.length === 1) { + const code = flags.languageHint[0]!.trim(); + if (code !== "zh" && code !== "en") { + return "--language-hint must be zh or en."; + } + } + + if (flags.sampleRate !== undefined && !DESIGN_SAMPLE_RATES.has(flags.sampleRate)) { + return "--sample-rate must be 16000, 24000, or 48000."; + } + if (flags.format !== undefined && !DESIGN_FORMATS.has(flags.format)) { + return "--format must be pcm, wav, or mp3."; + } + return undefined; + }, + async run(ctx) { + const { settings, flags } = ctx; + const prefix = flags.prefix as string; + const voicePrompt = flags.prompt.trim(); + const previewText = flags.previewText.trim(); + + const input: Record = { + target_model: flags.targetModel, + prefix, + voice_prompt: voicePrompt, + preview_text: previewText, + }; + if (flags.languageHint !== undefined && flags.languageHint.length > 0) { + input.language_hints = [flags.languageHint[0]!.trim()]; + } + + const parameters: Record = {}; + if (flags.sampleRate !== undefined) parameters.sample_rate = flags.sampleRate; + if (flags.format !== undefined) parameters.response_format = flags.format; + + const request = buildVoiceRequest( + "create_voice", + input, + Object.keys(parameters).length > 0 ? parameters : undefined, + ); + + if (settings.dryRun) { + ctx.output.result({ + endpoint: ctx.client.url(speechVoiceCustomizationPath()), + request, + }); + return; + } + + const response = await createDesignVoice( + ctx.client, + { + target_model: flags.targetModel, + prefix, + voice_prompt: voicePrompt, + preview_text: previewText, + language_hints: + flags.languageHint !== undefined && flags.languageHint.length > 0 + ? [flags.languageHint[0]!.trim()] + : undefined, + }, + { + sample_rate: flags.sampleRate, + response_format: flags.format, + }, + ); + + const voiceId = response.output?.voice_id; + const previewAudio = response.output?.preview_audio; + const audioData = previewAudio?.data?.trim(); + if (!audioData) { + throw new BailianError( + ctx.localize({ + "en-US": "Design response did not include preview audio data.", + "zh-CN": "设计响应未包含预览音频数据。", + }), + ExitCode.GENERAL, + ctx.localize({ + "en-US": voiceId + ? `Voice was created as ${voiceId}. Use get/list/delete to inspect or clean up; do not retry create automatically.` + : "No voice_id was returned either. Inspect the raw response with --output json if available.", + "zh-CN": voiceId + ? `音色已创建:${voiceId}。请用 get/list/delete 查询或清理;不要自动重试创建。` + : "也未返回 voice_id。如有原始响应可用 --output json 排查。", + }), + ); + } + + const responseFormat = previewAudio?.response_format ?? flags.format ?? "wav"; + const extension = extensionForFormat(responseFormat); + let destPath: string; + try { + destPath = + flags.out ?? + join( + resolveOutputDir(settings, { subDir: "speech" }), + `voice-design-${randomBytes(6).toString("hex")}.${extension}`, + ); + if (flags.out) { + mkdirSync(dirname(destPath), { recursive: true }); + } + writeFileSync(destPath, Buffer.from(audioData, "base64")); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + throw new BailianError( + ctx.localize({ + "en-US": `Failed to save preview audio: ${message}`, + "zh-CN": `保存预览音频失败:${message}`, + }), + ExitCode.GENERAL, + ctx.localize({ + "en-US": voiceId + ? `Voice was created as ${voiceId}. Fix the output path and use get/delete — do not recreate.` + : "Fix the output path before retrying.", + "zh-CN": voiceId + ? `音色已创建:${voiceId}。请修正输出路径后用 get/delete 处理,不要重新创建。` + : "请先修正输出路径再重试。", + }), + ); + } + + // Omit preview bytes from JSON after save (same pattern as synthesize / omni). + const result = { + ...response, + output: response.output + ? { + ...response.output, + preview_audio: previewAudio ? { ...previewAudio, data: "" } : previewAudio, + } + : response.output, + saved: destPath, + }; + + let text: string | undefined; + if (settings.output === "text") { + const sampleRate = previewAudio?.sample_rate ?? flags.sampleRate ?? ""; + text = [ + `voice_id: ${voiceId ?? ""}`, + `target_model: ${response.output?.target_model ?? flags.targetModel}`, + `format: ${responseFormat}`, + `sample_rate: ${sampleRate}`, + `saved: ${destPath}`, + response.request_id ? `request_id: ${response.request_id}` : undefined, + ] + .filter((line): line is string => line !== undefined) + .join("\n"); + } + ctx.output.result(result, { text }); + }, +}); diff --git a/packages/commands/src/commands/speech/voice/get.ts b/packages/commands/src/commands/speech/voice/get.ts new file mode 100644 index 000000000..aa905d685 --- /dev/null +++ b/packages/commands/src/commands/speech/voice/get.ts @@ -0,0 +1,65 @@ +import { + buildVoiceRequest, + defineCommand, + queryVoice, + speechVoiceCustomizationPath, + type FlagsDef, + type ParsedFlags, +} from "bailian-cli-core"; +import { validateVoiceId, VOICE_ID_FLAG } from "./shared.ts"; + +const GET_FLAGS = { + ...VOICE_ID_FLAG, +} satisfies FlagsDef; +type GetFlags = ParsedFlags; + +export default defineCommand({ + description: { + "en-US": "Get details of a custom voice", + "zh-CN": "查看自定义音色详情", + }, + auth: "apiKey", + usageArgs: "--id ", + flags: GET_FLAGS, + notes: [ + { + "en-US": + "Status DEPLOYING/OK/UNDEPLOYED is returned as data, not as a CLI failure. Use `speech voice preview` for the reference audio URL when present.", + "zh-CN": + "状态 DEPLOYING/OK/UNDEPLOYED 作为查询结果返回,不会因此失败退出。有参考音频时用 `speech voice preview` 获取。", + }, + ], + exampleArgs: ["--id --output json", "--id --quiet"], + validate: (flags: GetFlags) => validateVoiceId(flags.id), + async run(ctx) { + const { settings, flags } = ctx; + const voiceId = flags.id; + + if (settings.dryRun) { + ctx.output.result({ + endpoint: ctx.client.url(speechVoiceCustomizationPath()), + request: buildVoiceRequest("query_voice", { voice_id: voiceId }), + }); + return; + } + + const response = await queryVoice(ctx.client, voiceId); + let text: string | undefined; + if (settings.output === "text") { + const output = response.output; + text = [ + `voice_id: ${output?.voice_id ?? voiceId}`, + `status: ${output?.status ?? ""}`, + `target_model: ${output?.target_model ?? ""}`, + `gmt_create: ${output?.gmt_create ?? ""}`, + `gmt_modified: ${output?.gmt_modified ?? ""}`, + output?.resource_link ? `resource_link: ${output.resource_link}` : undefined, + output?.voice_prompt ? `voice_prompt: ${output.voice_prompt}` : undefined, + output?.preview_text ? `preview_text: ${output.preview_text}` : undefined, + ] + .filter((line): line is string => line !== undefined) + .join("\n"); + } + ctx.output.result(response, { text }); + }, +}); diff --git a/packages/commands/src/commands/speech/voice/list.ts b/packages/commands/src/commands/speech/voice/list.ts new file mode 100644 index 000000000..8be877be7 --- /dev/null +++ b/packages/commands/src/commands/speech/voice/list.ts @@ -0,0 +1,105 @@ +import { + buildVoiceRequest, + defineCommand, + listVoices, + speechVoiceCustomizationPath, + type FlagsDef, + type ParsedFlags, +} from "bailian-cli-core"; +import { validatePositiveIntPage, validateVoicePrefix, VOICE_PREFIX_FLAG } from "./shared.ts"; + +const LIST_FLAGS = { + ...VOICE_PREFIX_FLAG, + page: { + type: "number", + valueHint: "", + description: { + "en-US": "Page number, 1-based (default: 1). Mapped to API page_index as page - 1", + "zh-CN": "页码,从 1 开始(默认:1)。映射为 API 的 page_index:page - 1", + }, + }, + pageSize: { + type: "number", + valueHint: "", + description: { + "en-US": "Results per page (default: 10)", + "zh-CN": "每页结果数(默认:10)", + }, + }, +} satisfies FlagsDef; +type ListFlags = ParsedFlags; + +export default defineCommand({ + description: { + "en-US": "List custom voices", + "zh-CN": "列出自定义音色", + }, + auth: "apiKey", + usageArgs: "[--prefix ] [--page ] [--page-size ]", + flags: LIST_FLAGS, + notes: [ + { + "en-US": + "Lists one page only (CLI defaults: page=1, page-size=10). Status values: DEPLOYING / OK / UNDEPLOYED.", + "zh-CN": + "只查询指定页(CLI 默认:page=1,page-size=10)。状态值:DEPLOYING / OK / UNDEPLOYED。", + }, + ], + exampleArgs: ["", "--prefix demo", "--page 2 --page-size 20"], + validate: (flags: ListFlags) => { + const prefixError = validateVoicePrefix(flags.prefix); + if (prefixError) return prefixError; + return validatePositiveIntPage(flags.page, flags.pageSize); + }, + async run(ctx) { + const { settings, flags } = ctx; + + const page = flags.page ?? 1; + const pageSize = flags.pageSize ?? 10; + const pageIndex = page - 1; + + const input: Record = { + page_index: pageIndex, + page_size: pageSize, + }; + if (flags.prefix !== undefined) input.prefix = flags.prefix; + + if (settings.dryRun) { + ctx.output.result({ + endpoint: ctx.client.url(speechVoiceCustomizationPath()), + request: buildVoiceRequest("list_voice", input), + }); + return; + } + + const response = await listVoices(ctx.client, { + prefix: flags.prefix, + pageIndex, + pageSize, + }); + + let text: string | undefined; + if (settings.output === "text") { + const items = response.output?.voice_list ?? []; + const lines: string[] = []; + if (items.length === 0) { + lines.push( + ctx.localize({ + "en-US": "No voices found.", + "zh-CN": "未找到音色。", + }), + ); + } else { + for (const item of items) { + const id = item.voice_id ?? ""; + const status = item.status ?? ""; + const targetModel = item.target_model ?? ""; + const modified = item.gmt_modified ?? ""; + lines.push(`[${id}] ${status} ${targetModel} ${modified}`.trimEnd()); + } + } + text = lines.join("\n"); + } + ctx.output.result(response, { text }); + }, +}); diff --git a/packages/commands/src/commands/speech/voice/preview.ts b/packages/commands/src/commands/speech/voice/preview.ts new file mode 100644 index 000000000..4f0802a50 --- /dev/null +++ b/packages/commands/src/commands/speech/voice/preview.ts @@ -0,0 +1,101 @@ +import { dirname } from "path"; +import { mkdirSync } from "fs"; +import { + BailianError, + buildVoiceRequest, + defineCommand, + ExitCode, + queryVoice, + speechVoiceCustomizationPath, + type FlagsDef, + type ParsedFlags, +} from "bailian-cli-core"; +import { downloadFile } from "bailian-cli-runtime"; +import { validateVoiceId, VOICE_ID_FLAG } from "./shared.ts"; + +const PREVIEW_FLAGS = { + ...VOICE_ID_FLAG, + out: { + type: "string", + valueHint: "", + description: { + "en-US": "Download the reference audio to this path", + "zh-CN": "将参考音频下载到此路径", + }, + }, +} satisfies FlagsDef; +type PreviewFlags = ParsedFlags; + +export default defineCommand({ + description: { + "en-US": "Fetch the reference audio URL for a cloned voice", + "zh-CN": "获取复刻音色的参考音频地址", + }, + auth: "apiKey", + usageArgs: "--id [--out ]", + flags: PREVIEW_FLAGS, + notes: [ + { + "en-US": + "Returns resource_link from query_voice. This is the enrollment reference audio, not a synthesized preview. Use `speech synthesize` to hear the voice. Design voices may not include resource_link.", + "zh-CN": + "返回 query_voice 的 resource_link。这是复刻时的参考音频,不是合成试听。试听请用 `speech synthesize`。设计音色可能不含 resource_link。", + }, + ], + exampleArgs: ["--id --output json", "--id --out reference.wav"], + validate: (flags: PreviewFlags) => validateVoiceId(flags.id), + async run(ctx) { + const { settings, flags } = ctx; + const voiceId = flags.id; + + if (settings.dryRun) { + ctx.output.result({ + endpoint: ctx.client.url(speechVoiceCustomizationPath()), + request: buildVoiceRequest("query_voice", { voice_id: voiceId }), + }); + return; + } + + const response = await queryVoice(ctx.client, voiceId); + const resourceLink = response.output?.resource_link?.trim(); + if (!resourceLink) { + throw new BailianError( + ctx.localize({ + "en-US": "No reference audio URL (resource_link) is available for this voice.", + "zh-CN": "该音色没有可用的参考音频地址(resource_link)。", + }), + ExitCode.GENERAL, + ctx.localize({ + "en-US": + "Design voices may omit resource_link. Use `speech synthesize --voice ` for a synthesized preview.", + "zh-CN": + "设计音色可能不返回 resource_link。请使用 `speech synthesize --voice ` 做合成试听。", + }), + ); + } + + let saved: string | undefined; + if (flags.out) { + mkdirSync(dirname(flags.out), { recursive: true }); + await downloadFile(resourceLink, flags.out, { + quiet: settings.quiet || settings.output === "json", + }); + saved = flags.out; + } + + const result = { + voice_id: response.output?.voice_id ?? voiceId, + resource_link: resourceLink, + saved, + request_id: response.request_id, + }; + + let text: string | undefined; + if (settings.output === "text") { + text = saved + ? `voice_id: ${result.voice_id}\nsaved: ${saved}` + : `voice_id: ${result.voice_id}\nresource_link: ${resourceLink}`; + } + ctx.output.result(result, { text }); + }, +}); diff --git a/packages/commands/src/commands/speech/voice/shared.ts b/packages/commands/src/commands/speech/voice/shared.ts new file mode 100644 index 000000000..925ea6817 --- /dev/null +++ b/packages/commands/src/commands/speech/voice/shared.ts @@ -0,0 +1,169 @@ +import { existsSync, statSync } from "fs"; +import { extname } from "path"; +import { + audioFileToDataUri, + audioMimeForPath, + BailianError, + ExitCode, + redactDataUri, + type FlagsDef, +} from "bailian-cli-core"; + +/** Shared --id flag for get / update / delete / preview. */ +export const VOICE_ID_FLAG = { + id: { + type: "string", + valueHint: "", + description: { + "en-US": "Voice ID (required)", + "zh-CN": "音色 ID(必填)", + }, + required: true, + }, +} satisfies FlagsDef; + +/** Shared --prefix flag for clone / design / list. */ +export const VOICE_PREFIX_FLAG = { + prefix: { + type: "string", + valueHint: "", + description: { + "en-US": "Custom voice prefix. Digits and letters only, 1-10 characters", + "zh-CN": "音色自定义前缀。仅允许数字和英文字母,1-10 个字符", + }, + }, +} satisfies FlagsDef; + +export const VOICE_RIGHTS_NOTES = [ + { + "en-US": + "You must have the right to use the reference audio or described voice. Do not clone or imitate a real person without authorization.", + "zh-CN": "你必须有权使用参考音频或所描述的音色。未经授权请勿复刻或模仿真实个人声音。", + }, + { + "en-US": + "A voice ID is bound to its target model. Later `speech synthesize` calls must use the same --model.", + "zh-CN": "音色 ID 绑定目标模型。后续 `speech synthesize` 必须使用相同的 --model。", + }, +] as const; + +const PREFIX_PATTERN = /^[A-Za-z0-9]{1,10}$/; + +/** Voice clone/update reference audio limit from the enrollment API docs. */ +export const MAX_VOICE_REFERENCE_BYTES = 10 * 1024 * 1024; + +/** Validate voice ID is non-empty after trim. */ +export function validateVoiceId(voiceId: string | undefined): string | undefined { + if (voiceId === undefined) return undefined; + if (voiceId.trim().length === 0) { + return "--id must not be empty."; + } + return undefined; +} + +/** Validate prefix: digits and letters, 1-10 chars. */ +export function validateVoicePrefix(prefix: string | undefined): string | undefined { + if (prefix === undefined) return undefined; + if (prefix.length === 0) { + return "--prefix must not be empty."; + } + if (!PREFIX_PATTERN.test(prefix)) { + return "--prefix must be 1-10 characters of digits or letters (A-Z, a-z, 0-9)."; + } + return undefined; +} + +/** Validate 1-based page / page-size positive integers. */ +export function validatePositiveIntPage( + page: number | undefined, + pageSize: number | undefined, +): string | undefined { + if (page !== undefined && (!Number.isInteger(page) || page < 1)) { + return "--page must be an integer >= 1."; + } + if (pageSize !== undefined && (!Number.isInteger(pageSize) || pageSize < 1)) { + return "--page-size must be an integer >= 1."; + } + return undefined; +} + +/** Validate language hint: at most one item when provided as array. */ +export function validateLanguageHints(hints: string[] | undefined): string | undefined { + if (hints === undefined) return undefined; + if (hints.length > 1) { + return "Provide at most one --language-hint."; + } + if (hints.length === 1 && hints[0]!.trim().length === 0) { + return "--language-hint must not be empty."; + } + return undefined; +} + +/** + * Resolve a reference audio argument for dry-run or live calls. + * Local paths become Data URIs (or a pending marker for dry-run); + * http(s) URLs and existing Data URIs pass through. + * Format checks run before encode so dry-run and live agree. + */ +export function resolveVoiceAudioSource( + audio: string, + options: { encodeLocal: boolean }, +): { audio: string; localPath?: string } { + const trimmed = audio.trim(); + if (trimmed.length === 0) { + throw new BailianError("--audio must not be empty.", ExitCode.USAGE); + } + if (/^data:/i.test(trimmed)) { + if (!/^data:audio\//i.test(trimmed)) { + throw new BailianError("Reference Data URI must use an audio/* MIME type.", ExitCode.USAGE); + } + if (!/;base64,/i.test(trimmed)) { + throw new BailianError( + "Reference Data URI must be base64-encoded (…;base64,…).", + ExitCode.USAGE, + ); + } + return { audio: trimmed }; + } + if (/^https?:\/\//i.test(trimmed)) { + return { audio: trimmed }; + } + if (!existsSync(trimmed)) { + throw new BailianError(`Audio file not found: ${trimmed}`, ExitCode.USAGE); + } + const stat = statSync(trimmed); + if (!stat.isFile()) { + throw new BailianError(`Not a file: ${trimmed}`, ExitCode.USAGE); + } + if (stat.size > MAX_VOICE_REFERENCE_BYTES) { + throw new BailianError(`Reference audio exceeds 10 MB: ${trimmed}`, ExitCode.USAGE); + } + if (!audioMimeForPath(trimmed)) { + const extension = extname(trimmed).toLowerCase() || "unknown"; + throw new BailianError( + `Unsupported audio format "${extension}".`, + ExitCode.USAGE, + "Use an audio file with a recognized extension.", + ); + } + if (!options.encodeLocal) { + return { audio: trimmed, localPath: trimmed }; + } + return { audio: audioFileToDataUri(trimmed), localPath: trimmed }; +} + +/** Redact Data URI payloads in dry-run request bodies. */ +export function redactVoiceAudioInRequest( + request: { input?: Record }, + localPath?: string, +): { input?: Record } & Record { + const input = request.input ? { ...request.input } : undefined; + if (input && typeof input.audio === "string") { + if (localPath && input.audio === localPath) { + input.audio = ``; + } else { + input.audio = redactDataUri(input.audio); + } + } + return { ...request, input }; +} diff --git a/packages/commands/src/commands/speech/voice/update.ts b/packages/commands/src/commands/speech/voice/update.ts new file mode 100644 index 000000000..be0b7401d --- /dev/null +++ b/packages/commands/src/commands/speech/voice/update.ts @@ -0,0 +1,82 @@ +import { + buildVoiceRequest, + defineCommand, + speechVoiceCustomizationPath, + updateVoice, + type FlagsDef, + type ParsedFlags, +} from "bailian-cli-core"; +import { + redactVoiceAudioInRequest, + resolveVoiceAudioSource, + validateVoiceId, + VOICE_ID_FLAG, + VOICE_RIGHTS_NOTES, +} from "./shared.ts"; + +const UPDATE_FLAGS = { + ...VOICE_ID_FLAG, + audio: { + type: "string", + valueHint: "", + description: { + "en-US": "New reference audio URL or local file", + "zh-CN": "新的参考音频 URL 或本地文件", + }, + required: true, + }, +} satisfies FlagsDef; +type UpdateFlags = ParsedFlags; + +export default defineCommand({ + description: { + "en-US": "Update the reference audio of a cloned voice", + "zh-CN": "更新复刻音色的参考音频", + }, + auth: "apiKey", + risk: { + level: "high", + message: { + "en-US": + "This updates the reference audio for the voice ID. All synthesis that uses this voice will be affected.", + "zh-CN": "该操作会更新此音色 ID 的参考音频。所有使用该音色的合成都会受影响。", + }, + }, + usageArgs: "--id --audio ", + flags: UPDATE_FLAGS, + notes: [ + ...VOICE_RIGHTS_NOTES, + { + "en-US": + "Only reference audio can be updated. Design voices do not support voice_prompt updates via this command.", + "zh-CN": "仅支持更新参考音频。设计音色不支持通过本命令更新 voice_prompt。", + }, + ], + exampleArgs: [ + "--id --audio ./new-reference.wav --dry-run --output json", + "--id --audio ./new-reference.wav --yes", + ], + validate: (flags: UpdateFlags) => validateVoiceId(flags.id), + async run(ctx) { + const { settings, flags } = ctx; + const voiceId = flags.id; + const dryRun = settings.dryRun === true; + const resolved = resolveVoiceAudioSource(flags.audio, { encodeLocal: !dryRun }); + + const request = buildVoiceRequest("update_voice", { + voice_id: voiceId, + audio: resolved.audio, + }); + + if (dryRun) { + ctx.output.result({ + endpoint: ctx.client.url(speechVoiceCustomizationPath()), + request: redactVoiceAudioInRequest(request, resolved.localPath), + }); + return; + } + + const response = await updateVoice(ctx.client, voiceId, resolved.audio); + ctx.output.result(response, { text: voiceId }); + }, +}); diff --git a/packages/commands/src/index.ts b/packages/commands/src/index.ts index 732502bff..ea63db17c 100644 --- a/packages/commands/src/index.ts +++ b/packages/commands/src/index.ts @@ -103,11 +103,19 @@ export { default as mcpTools } from "./commands/mcp/tools.ts"; export { default as searchWeb } from "./commands/search/web.ts"; export { default as speechSynthesize } from "./commands/speech/synthesize.ts"; export { default as speechRecognize } from "./commands/speech/recognize.ts"; +export { default as speechGenerate } from "./commands/speech/generate.ts"; export { default as speechVocabularyCreate } from "./commands/speech/vocabulary/create.ts"; export { default as speechVocabularyList } from "./commands/speech/vocabulary/list.ts"; export { default as speechVocabularyGet } from "./commands/speech/vocabulary/get.ts"; export { default as speechVocabularyUpdate } from "./commands/speech/vocabulary/update.ts"; export { default as speechVocabularyDelete } from "./commands/speech/vocabulary/delete.ts"; +export { default as speechVoiceClone } from "./commands/speech/voice/clone.ts"; +export { default as speechVoiceDesign } from "./commands/speech/voice/design.ts"; +export { default as speechVoiceList } from "./commands/speech/voice/list.ts"; +export { default as speechVoiceGet } from "./commands/speech/voice/get.ts"; +export { default as speechVoiceUpdate } from "./commands/speech/voice/update.ts"; +export { default as speechVoiceDelete } from "./commands/speech/voice/delete.ts"; +export { default as speechVoicePreview } from "./commands/speech/voice/preview.ts"; export { default as fileUpload } from "./commands/file/upload.ts"; export { default as fileUrl } from "./commands/file/url.ts"; export { default as consoleCall } from "./commands/console/call.ts"; diff --git a/packages/commands/tests/e2e/helpers.ts b/packages/commands/tests/e2e/helpers.ts index 39e4e4d94..685348c4c 100644 --- a/packages/commands/tests/e2e/helpers.ts +++ b/packages/commands/tests/e2e/helpers.ts @@ -45,6 +45,7 @@ export { isOpenApiE2EReady, isOssImportE2EReady, isSearchE2EReady, + isSpeechGenerateE2EReady, isTableKbE2EReady, isTableSearchE2EReady, } from "e2e/gating"; diff --git a/packages/commands/tests/e2e/speech-generate.e2e.test.ts b/packages/commands/tests/e2e/speech-generate.e2e.test.ts new file mode 100644 index 000000000..5b867779d --- /dev/null +++ b/packages/commands/tests/e2e/speech-generate.e2e.test.ts @@ -0,0 +1,429 @@ +import { createServer } from "node:http"; +import type { AddressInfo } from "node:net"; +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { + isSpeechGenerateE2EReady, + makeE2eOutputDir, + parseStdoutJson, + runCommandHelp, + runCommandE2e, + runCommandInProcess, +} from "./helpers.ts"; +import { SPEECH_GENERATE_ROUTES } from "./topic-routes.ts"; + +const tempDirs: string[] = []; + +afterEach(() => { + for (const tempDir of tempDirs.splice(0)) { + rmSync(tempDir, { recursive: true, force: true }); + } +}); + +function makeTempWav(size = 44): string { + const tempDir = mkdtempSync(join(tmpdir(), "bl-speech-gen-")); + tempDirs.push(tempDir); + const filePath = join(tempDir, "ref.wav"); + writeFileSync(filePath, Buffer.alloc(size, 1)); + return filePath; +} + +describe("e2e: speech generate help / validation", () => { + test("help is bilingual and has no voice/async flags", async () => { + const { stderr, exitCode } = await runCommandHelp(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--help", + ]); + expect(exitCode, stderr).toBe(0); + expect(stderr).toMatch(/--text-prompt/i); + expect(stderr).toMatch(/Audio generation prompt|音频生成提示词/); + expect(stderr).not.toMatch(/--voice\b/); + expect(stderr).not.toMatch(/--async\b/); + }); + + test("missing/blank text-prompt → 2", async () => { + const missing = await runCommandE2e(SPEECH_GENERATE_ROUTES, ["speech", "generate", "--quiet"]); + expect(missing.exitCode).toBe(2); + + const blank = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + " ", + "--quiet", + ]); + expect(blank.exitCode).toBe(2); + }); + + test("rejects overlong prompt, 4 references, missing local file, @voice overflow", async () => { + const tooLong = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + "x".repeat(3001), + "--workspace-id", + "ws_test", + "--quiet", + ]); + expect(tooLong.exitCode).toBe(2); + expect(tooLong.stderr).toMatch(/3000/); + + const fourAudio = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + "hello", + "--audio", + "https://a", + "--audio", + "https://b", + "--audio", + "https://c", + "--audio", + "https://d", + "--workspace-id", + "ws_test", + "--quiet", + ]); + expect(fourAudio.exitCode).toBe(2); + expect(fourAudio.stderr).toMatch(/at most 3|--audio/i); + + const missingFile = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + "用 @voice1 介绍天气", + "--audio", + "/tmp/bl-speech-gen-does-not-exist.wav", + "--workspace-id", + "ws_test", + "--dry-run", + "--quiet", + ]); + expect(missingFile.exitCode).toBe(2); + expect(missingFile.stderr).toMatch(/not found|找不到|Audio file/i); + + const overflow = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + "用 @voice2 说话", + "--audio", + "https://example.com/a.wav", + "--workspace-id", + "ws_test", + "--quiet", + ]); + expect(overflow.exitCode).toBe(2); + expect(overflow.stderr).toMatch(/@voice2|reference/i); + }); + + test("rejects malicious workspace id and unsupported region via us base-url", async () => { + const badWorkspace = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + "hello", + "--workspace-id", + "evil.example.com", + "--dry-run", + "--quiet", + ]); + expect(badWorkspace.exitCode).toBe(2); + expect(badWorkspace.stderr).toMatch(/Workspace ID|点号|underscores/i); + + const usBase = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + "hello", + "--base-url", + "https://dashscope-us.aliyuncs.com", + "--api-key", + "sk-e2e", + "--dry-run", + "--quiet", + ]); + expect(usBase.exitCode).toBe(2); + expect(usBase.stderr).toMatch(/us|美国|not available/i); + }); +}); + +describe("e2e: speech generate dry-run / mock HTTP", () => { + test("dry-run with workspace builds MaaS endpoint and redacts local audio", async () => { + const wav = makeTempWav(); + const { stdout, stderr, exitCode } = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--workspace-id", + "ws_test", + "--text-prompt", + "用 @voice1 的声音介绍今天的天气。", + "--audio", + wav, + "--volume", + "0", + "--aigc-tag", + "false", + "--dry-run", + "--output", + "json", + "--quiet", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + endpoint?: string; + request?: { + model?: string; + input?: { + text_prompt?: string; + references?: Array<{ audio_data?: string; audio_url?: string }>; + volume?: number; + enable_aigc_tag?: boolean; + format?: string; + }; + }; + }>(stdout); + expect(body.endpoint).toBe( + "https://ws_test.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/tts/SpeechSynthesizer", + ); + expect(body.request?.model).toBe("qwen-audio-3.1-tts-next"); + expect(body.request?.input?.text_prompt).toBe("用 @voice1 的声音介绍今天的天气。"); + expect(body.request?.input?.format).toBe("wav"); + expect(body.request?.input?.volume).toBe(0); + expect(body.request?.input?.enable_aigc_tag).toBe(false); + expect(body.request?.input?.references?.[0]?.audio_data).toMatch(/pending-encode|/); + }); + + test("explicit base-url dry-run does not require workspace", async () => { + const { stdout, stderr, exitCode } = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + "生成短播客", + "--base-url", + "https://proxy.example.test", + "--api-key", + "sk-e2e", + "--dry-run", + "--output", + "json", + "--quiet", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ endpoint?: string }>(stdout); + expect(body.endpoint).toBe( + "https://proxy.example.test/api/v1/services/audio/tts/SpeechSynthesizer", + ); + }); + + test("default missing workspace → USAGE", async () => { + const result = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + "hello", + "--dry-run", + "--quiet", + ]); + expect(result.exitCode).toBe(2); + expect(result.stderr).toMatch(/Workspace ID|工作空间/i); + }); + + test("dry-run rejects bare PCM Data URI and non-allowlisted local formats", async () => { + const pcmDataUri = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--workspace-id", + "ws_test", + "--text-prompt", + "用 @voice1 介绍", + "--audio", + "data:audio/pcm;base64,AAAA", + "--dry-run", + "--quiet", + ]); + expect(pcmDataUri.exitCode).toBe(2); + expect(pcmDataUri.stderr).toMatch(/pcm|WAV.*MP3|OGG Opus|Unsupported reference/i); + + const tempDir = mkdtempSync(join(tmpdir(), "bl-speech-gen-bad-")); + tempDirs.push(tempDir); + const flacPath = join(tempDir, "ref.flac"); + writeFileSync(flacPath, "fake-flac"); + const flac = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--workspace-id", + "ws_test", + "--text-prompt", + "用 @voice1 介绍", + "--audio", + flacPath, + "--dry-run", + "--quiet", + ]); + expect(flac.exitCode).toBe(2); + expect(flac.stderr).toMatch(/\.flac|WAV.*MP3|OGG Opus|Unsupported reference/i); + + const pcmPath = join(tempDir, "ref.pcm"); + writeFileSync(pcmPath, Buffer.alloc(16)); + const pcmFile = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--workspace-id", + "ws_test", + "--text-prompt", + "用 @voice1 介绍", + "--audio", + pcmPath, + "--dry-run", + "--quiet", + ]); + expect(pcmFile.exitCode).toBe(2); + expect(pcmFile.stderr).toMatch(/\.pcm|WAV.*MP3|OGG Opus|Unsupported reference/i); + }); + + test("mock HTTP downloads audio.url; bad out-prefix fails with zero POSTs", async () => { + const audioPayload = Buffer.from("fake-audio-bytes"); + let synthesizePosts = 0; + const server = createServer((incoming, outgoing) => { + const url = incoming.url ?? ""; + if (url.startsWith("/api/v1/services/audio/tts/SpeechSynthesizer")) { + synthesizePosts += 1; + const chunks: Buffer[] = []; + incoming.on("data", (chunk: Buffer) => chunks.push(chunk)); + incoming.on("end", () => { + const body = JSON.parse(Buffer.concat(chunks).toString("utf8")) as { + model?: string; + input?: { text_prompt?: string }; + }; + expect(body.model).toBe("qwen-audio-3.1-tts-next"); + expect(body.input?.text_prompt).toBeTruthy(); + expect(incoming.headers.authorization).toBe("Bearer sk-e2e"); + const address = server.address() as AddressInfo; + outgoing.writeHead(200, { "Content-Type": "application/json" }); + outgoing.end( + JSON.stringify({ + output: { + audio: { + url: `http://127.0.0.1:${address.port}/audio.wav`, + data: "", + id: "audio-1", + expires_at: "2099-01-01T00:00:00Z", + duration: 1.5, + }, + }, + usage: { characters: 10 }, + request_id: "req-gen", + }), + ); + }); + return; + } + if (url.startsWith("/audio.wav")) { + outgoing.writeHead(200, { "Content-Type": "audio/wav" }); + outgoing.end(audioPayload); + return; + } + outgoing.writeHead(404); + outgoing.end(); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address() as AddressInfo; + const outDir = makeE2eOutputDir("speech-generate-mock"); + + try { + const badPrefix = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + "短播客", + "--base-url", + `http://127.0.0.1:${address.port}`, + "--api-key", + "sk-e2e", + "--out-dir", + outDir, + "--out-prefix", + "../escape", + "--quiet", + ]); + expect(badPrefix.exitCode).toBe(2); + expect(badPrefix.stderr).toMatch(/out-prefix|filename/i); + expect(synthesizePosts).toBe(0); + + const ok = await runCommandInProcess(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--text-prompt", + "短播客", + "--base-url", + `http://127.0.0.1:${address.port}`, + "--api-key", + "sk-e2e", + "--out-dir", + outDir, + "--out-prefix", + "sample", + "--output", + "json", + "--quiet", + ]); + expect(ok.exitCode, ok.stderr).toBe(0); + expect(synthesizePosts).toBe(1); + const body = parseStdoutJson<{ + saved?: string; + url?: string; + model?: string; + output?: { audio?: { id?: string; duration?: number } }; + request_id?: string; + }>(ok.stdout); + expect(body.model).toBe("qwen-audio-3.1-tts-next"); + expect(body.saved).toBe(join(outDir, "sample.wav")); + expect(existsSync(body.saved!)).toBe(true); + expect(readFileSync(body.saved!)).toEqual(audioPayload); + expect(body.output?.audio?.id).toBe("audio-1"); + expect(body.output?.audio?.duration).toBe(1.5); + expect(body.request_id).toBe("req-gen"); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); +}); + +describe.skipIf(!isSpeechGenerateE2EReady())("e2e: speech generate (live)", () => { + test("generate downloads a decodable wav via workspace MaaS", async () => { + const workspaceId = process.env.BAILIAN_WORKSPACE_ID!.trim(); + const outDir = makeE2eOutputDir("speech-generate-live"); + const savedPath = join(outDir, "live-sample.wav"); + const result = await runCommandE2e(SPEECH_GENERATE_ROUTES, [ + "speech", + "generate", + "--workspace-id", + workspaceId, + "--text-prompt", + "用轻松的语气说一句:今天天气不错。", + "--out-dir", + outDir, + "--out-prefix", + "live-sample", + "--format", + "wav", + "--output", + "json", + "--quiet", + ]); + expect(result.exitCode, result.stderr).toBe(0); + const body = parseStdoutJson<{ saved?: string; model?: string }>(result.stdout); + expect(body.model).toBe("qwen-audio-3.1-tts-next"); + expect(body.saved).toBe(savedPath); + expect(existsSync(savedPath)).toBe(true); + const bytes = readFileSync(savedPath); + expect(bytes.byteLength).toBeGreaterThan(1024); + expect(bytes.subarray(0, 4).toString("ascii")).toBe("RIFF"); + expect(bytes.subarray(8, 12).toString("ascii")).toBe("WAVE"); + }, 180_000); +}); diff --git a/packages/commands/tests/e2e/speech-voice-design.e2e.test.ts b/packages/commands/tests/e2e/speech-voice-design.e2e.test.ts new file mode 100644 index 000000000..15207ecb7 --- /dev/null +++ b/packages/commands/tests/e2e/speech-voice-design.e2e.test.ts @@ -0,0 +1,577 @@ +import { createServer } from "node:http"; +import type { AddressInfo } from "node:net"; +import { existsSync, mkdirSync, readFileSync, rmSync, mkdtempSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { + isBailianE2EMediaEnabled, + isDashScopeE2EReady, + makeE2eOutputDir, + parseStdoutJson, + runCommandHelp, + runCommandE2e, + runCommandInProcess, +} from "./helpers.ts"; +import { SPEECH_VOICE_ROUTES } from "./topic-routes.ts"; + +const DESIGN_TARGET_MODEL = "cosyvoice-v3.5-plus"; + +function assertDecodableWav(filePath: string, minBytes = 1024): void { + expect(existsSync(filePath), `missing wav: ${filePath}`).toBe(true); + const bytes = readFileSync(filePath); + expect(bytes.byteLength).toBeGreaterThanOrEqual(minBytes); + expect(bytes.subarray(0, 4).toString("ascii")).toBe("RIFF"); + expect(bytes.subarray(8, 12).toString("ascii")).toBe("WAVE"); +} + +const tempDirs: string[] = []; + +afterEach(() => { + for (const tempDir of tempDirs.splice(0)) { + rmSync(tempDir, { recursive: true, force: true }); + } +}); + +const VALID_PREVIEW_TEXT = "各位听众朋友们大家好,欢迎收听今天的节目。"; + +describe("e2e: speech voice design help / validation", () => { + test("design --help shows bilingual prompt flags without --yes", async () => { + const { stderr, exitCode } = await runCommandHelp(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--help", + ]); + expect(exitCode, stderr).toBe(0); + expect(stderr).toMatch(/--prompt/i); + expect(stderr).toMatch(/--preview-text/i); + expect(stderr).toMatch(/Voice description|音色描述/); + expect(stderr).not.toMatch(/--yes/); + }); + + test("missing required flags → 2", async () => { + const { exitCode } = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--quiet", + ]); + expect(exitCode).toBe(2); + }); + + test("prompt/preview-text length bounds", async () => { + const tooLongPrompt = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--prompt", + "x".repeat(501), + "--preview-text", + VALID_PREVIEW_TEXT, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3.5-plus", + "--quiet", + ]); + expect(tooLongPrompt.exitCode).toBe(2); + expect(tooLongPrompt.stderr).toMatch(/500/); + + const shortPreview = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--prompt", + "沉稳温暖的中文播音员", + "--preview-text", + "太短了", + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3.5-plus", + "--quiet", + ]); + expect(shortPreview.exitCode).toBe(2); + expect(shortPreview.stderr).toMatch(/15 and 200|preview-text/i); + + const badFormat = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--prompt", + "沉稳温暖的中文播音员", + "--preview-text", + VALID_PREVIEW_TEXT, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3.5-plus", + "--format", + "flac", + "--quiet", + ]); + expect(badFormat.exitCode).toBe(2); + expect(badFormat.stderr).toMatch(/--format/i); + }); +}); + +describe("e2e: speech voice design dry-run / mock HTTP", () => { + test("dry-run request is exact and does not create files", async () => { + const outDir = mkdtempSync(join(tmpdir(), "bl-voice-design-dry-")); + tempDirs.push(outDir); + const outPath = join(outDir, "preview.wav"); + const { stdout, stderr, exitCode } = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--prompt", + "沉稳温暖的中文播音员", + "--preview-text", + VALID_PREVIEW_TEXT, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3.5-plus", + "--sample-rate", + "24000", + "--format", + "wav", + "--out", + outPath, + "--dry-run", + "--output", + "json", + "--quiet", + ]); + expect(exitCode, stderr).toBe(0); + expect(existsSync(outPath)).toBe(false); + const body = parseStdoutJson<{ + request?: { + model?: string; + input?: Record; + parameters?: Record; + }; + }>(stdout); + expect(body.request?.model).toBe("voice-enrollment"); + expect(body.request?.input).toEqual({ + action: "create_voice", + target_model: "cosyvoice-v3.5-plus", + prefix: "demo", + voice_prompt: "沉稳温暖的中文播音员", + preview_text: VALID_PREVIEW_TEXT, + }); + expect(body.request?.parameters).toEqual({ + sample_rate: 24000, + response_format: "wav", + }); + expect(body.request?.input).not.toHaveProperty("audio"); + }); + + test("mock HTTP decodes Base64 preview and omits data in JSON result", async () => { + const wavBytes = Buffer.from("RIFF....WAVEfmt "); + const server = createServer((incoming, outgoing) => { + const chunks: Buffer[] = []; + incoming.on("data", (chunk: Buffer) => chunks.push(chunk)); + incoming.on("end", () => { + const body = JSON.parse(Buffer.concat(chunks).toString("utf8")) as { + input?: Record; + }; + expect(body.input?.action).toBe("create_voice"); + expect(body.input).not.toHaveProperty("audio"); + outgoing.writeHead(200, { "Content-Type": "application/json" }); + outgoing.end( + JSON.stringify({ + output: { + voice_id: "demo-vd-123", + target_model: "cosyvoice-v3.5-plus", + preview_audio: { + data: wavBytes.toString("base64"), + sample_rate: 24000, + response_format: "wav", + }, + }, + usage: { count: 1 }, + request_id: "req-design", + }), + ); + }); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address() as AddressInfo; + const outDir = makeE2eOutputDir("speech-voice-design-mock"); + const outPath = join(outDir, "preview.wav"); + + try { + const { stdout, stderr, exitCode } = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--prompt", + "沉稳温暖的中文播音员", + "--preview-text", + VALID_PREVIEW_TEXT, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3.5-plus", + "--out", + outPath, + "--api-key", + "sk-e2e", + "--base-url", + `http://127.0.0.1:${address.port}`, + "--output", + "json", + "--quiet", + ]); + expect(exitCode, stderr).toBe(0); + expect(existsSync(outPath)).toBe(true); + expect(readFileSync(outPath)).toEqual(wavBytes); + const body = parseStdoutJson<{ + saved?: string; + output?: { voice_id?: string; preview_audio?: { data?: string } }; + request_id?: string; + }>(stdout); + expect(body.saved).toBe(outPath); + expect(body.output?.voice_id).toBe("demo-vd-123"); + expect(body.output?.preview_audio?.data).toBe(""); + expect(body.request_id).toBe("req-design"); + expect(stdout).not.toMatch(/RIFF/); + expect(stdout).not.toContain(wavBytes.toString("base64")); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + test("missing preview_audio reports GENERAL and keeps voice id hint", async () => { + const server = createServer((_incoming, outgoing) => { + outgoing.writeHead(200, { "Content-Type": "application/json" }); + outgoing.end( + JSON.stringify({ + output: { voice_id: "demo-vd-no-preview", target_model: "cosyvoice-v3.5-plus" }, + request_id: "req-design", + }), + ); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address() as AddressInfo; + try { + const result = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--prompt", + "沉稳温暖的中文播音员", + "--preview-text", + VALID_PREVIEW_TEXT, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3.5-plus", + "--api-key", + "sk-e2e", + "--base-url", + `http://127.0.0.1:${address.port}`, + "--quiet", + ]); + expect(result.exitCode).toBe(1); + expect(result.stderr).toMatch(/preview|预览|demo-vd-no-preview/i); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + test("--out creates missing parent directories", async () => { + const wavBytes = Buffer.from("RIFF....WAVEfmt "); + const server = createServer((_incoming, outgoing) => { + outgoing.writeHead(200, { "Content-Type": "application/json" }); + outgoing.end( + JSON.stringify({ + output: { + voice_id: "demo-vd-mkdir-ok", + target_model: "cosyvoice-v3.5-plus", + preview_audio: { + data: wavBytes.toString("base64"), + response_format: "wav", + sample_rate: 24000, + }, + }, + request_id: "req-design-mkdir-ok", + }), + ); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address() as AddressInfo; + const tempDir = mkdtempSync(join(tmpdir(), "bl-voice-design-mkdir-ok-")); + tempDirs.push(tempDir); + const nestedOut = join(tempDir, "nested", "dir", "preview.wav"); + try { + const { stdout, stderr, exitCode } = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--prompt", + "沉稳温暖的中文播音员", + "--preview-text", + VALID_PREVIEW_TEXT, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3.5-plus", + "--out", + nestedOut, + "--api-key", + "sk-e2e", + "--base-url", + `http://127.0.0.1:${address.port}`, + "--output", + "json", + "--quiet", + ]); + expect(exitCode, stderr).toBe(0); + expect(existsSync(nestedOut)).toBe(true); + expect(readFileSync(nestedOut)).toEqual(wavBytes); + const body = parseStdoutJson<{ saved?: string; output?: { voice_id?: string } }>(stdout); + expect(body.saved).toBe(nestedOut); + expect(body.output?.voice_id).toBe("demo-vd-mkdir-ok"); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + test("save path failure after create still reports voice_id", async () => { + const wavBytes = Buffer.from("RIFF....WAVEfmt "); + const server = createServer((_incoming, outgoing) => { + outgoing.writeHead(200, { "Content-Type": "application/json" }); + outgoing.end( + JSON.stringify({ + output: { + voice_id: "demo-vd-save-fail", + target_model: "cosyvoice-v3.5-plus", + preview_audio: { + data: wavBytes.toString("base64"), + response_format: "wav", + sample_rate: 24000, + }, + }, + request_id: "req-design-save", + }), + ); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address() as AddressInfo; + const tempDir = mkdtempSync(join(tmpdir(), "bl-voice-design-save-")); + tempDirs.push(tempDir); + // --out points at an existing directory so writeFileSync fails after create. + const badOut = join(tempDir, "is-a-directory"); + mkdirSync(badOut); + try { + const result = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--prompt", + "沉稳温暖的中文播音员", + "--preview-text", + VALID_PREVIEW_TEXT, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3.5-plus", + "--out", + badOut, + "--api-key", + "sk-e2e", + "--base-url", + `http://127.0.0.1:${address.port}`, + "--quiet", + ]); + expect(result.exitCode).toBe(1); + expect(result.stderr).toMatch(/demo-vd-save-fail/); + expect(result.stderr).toMatch(/save|保存|get\/delete|不要重新创建|do not recreate/i); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + test("output dir mkdir failure after create still reports voice_id", async () => { + const wavBytes = Buffer.from("RIFF....WAVEfmt "); + const server = createServer((_incoming, outgoing) => { + outgoing.writeHead(200, { "Content-Type": "application/json" }); + outgoing.end( + JSON.stringify({ + output: { + voice_id: "demo-vd-mkdir-fail", + target_model: "cosyvoice-v3.5-plus", + preview_audio: { + data: wavBytes.toString("base64"), + response_format: "wav", + sample_rate: 24000, + }, + }, + request_id: "req-design-mkdir", + }), + ); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address() as AddressInfo; + const tempDir = mkdtempSync(join(tmpdir(), "bl-voice-design-mkdir-")); + tempDirs.push(tempDir); + // Make default ~/bailian-output a file so resolveOutputDir mkdir fails. + writeFileSync(join(tempDir, "bailian-output"), "not-a-directory"); + try { + const result = await runCommandInProcess( + SPEECH_VOICE_ROUTES, + [ + "speech", + "voice", + "design", + "--prompt", + "沉稳温暖的中文播音员", + "--preview-text", + VALID_PREVIEW_TEXT, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3.5-plus", + "--api-key", + "sk-e2e", + "--base-url", + `http://127.0.0.1:${address.port}`, + "--quiet", + ], + { HOME: tempDir }, + ); + expect(result.exitCode).toBe(1); + expect(result.stderr).toMatch(/demo-vd-mkdir-fail/); + expect(result.stderr).toMatch(/save|保存|get\/delete|不要重新创建|do not recreate/i); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); +}); + +describe.skipIf(!isBailianE2EMediaEnabled() || !isDashScopeE2EReady())( + "e2e: speech voice design (live)", + () => { + test("design → get → synthesize with voice_id → delete", async () => { + const outDir = makeE2eOutputDir("speech-voice-design-live"); + const previewPath = join(outDir, "design-preview.wav"); + const synthPath = join(outDir, "design-synth.wav"); + const prefix = `bl${Date.now().toString(36).slice(-6)}`; + let voiceId = ""; + try { + const created = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "design", + "--prompt", + "Calm warm Chinese announcer voice for a short news brief", + "--preview-text", + VALID_PREVIEW_TEXT, + "--prefix", + prefix, + "--target-model", + DESIGN_TARGET_MODEL, + "--format", + "wav", + "--out", + previewPath, + "--output", + "json", + "--quiet", + ]); + expect(created.exitCode, created.stderr).toBe(0); + const body = parseStdoutJson<{ + output?: { voice_id?: string; preview_audio?: { data?: string } }; + saved?: string; + }>(created.stdout); + voiceId = body.output?.voice_id ?? ""; + expect(voiceId).toMatch(/\S+/); + expect(body.saved).toBe(previewPath); + expect(body.output?.preview_audio?.data).toBe(""); + assertDecodableWav(previewPath); + + const got = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "get", + "--id", + voiceId, + "--output", + "json", + "--quiet", + ]); + expect(got.exitCode, got.stderr).toBe(0); + expect( + parseStdoutJson<{ output?: { target_model?: string } }>(got.stdout).output?.target_model, + ).toBe(DESIGN_TARGET_MODEL); + + // Design voice may still be DEPLOYING briefly; wait before matched-model synthesize. + { + const deadline = Date.now() + 90_000; + let lastStatus = ""; + while (Date.now() < deadline) { + const statusResult = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "get", + "--id", + voiceId, + "--output", + "json", + "--quiet", + ]); + expect(statusResult.exitCode, statusResult.stderr).toBe(0); + lastStatus = + parseStdoutJson<{ output?: { status?: string } }>(statusResult.stdout).output + ?.status ?? ""; + if (lastStatus === "OK") break; + await new Promise((resolve) => setTimeout(resolve, 3000)); + } + expect(lastStatus).toBe("OK"); + } + + const withDesign = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "synthesize", + "--model", + DESIGN_TARGET_MODEL, + "--voice", + voiceId, + "--text", + "这是设计音色的试听。", + "--format", + "wav", + "--out", + synthPath, + "--output", + "json", + "--quiet", + ]); + expect(withDesign.exitCode, withDesign.stderr).toBe(0); + const synthBody = parseStdoutJson<{ saved?: string; voice?: string; model?: string }>( + withDesign.stdout, + ); + expect(synthBody.saved).toBe(synthPath); + expect(synthBody.voice).toBe(voiceId); + expect(synthBody.model).toBe(DESIGN_TARGET_MODEL); + assertDecodableWav(synthPath); + } finally { + if (voiceId) { + const deleted = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "delete", + "--id", + voiceId, + "--yes", + "--quiet", + ]); + expect(deleted.exitCode, deleted.stderr).toBe(0); + } + } + }, 300_000); + }, +); diff --git a/packages/commands/tests/e2e/speech-voice.e2e.test.ts b/packages/commands/tests/e2e/speech-voice.e2e.test.ts new file mode 100644 index 000000000..5aa41303c --- /dev/null +++ b/packages/commands/tests/e2e/speech-voice.e2e.test.ts @@ -0,0 +1,779 @@ +import { createServer } from "node:http"; +import type { AddressInfo } from "node:net"; +import { + mkdtempSync, + rmSync, + writeFileSync, + readFileSync, + existsSync, + truncateSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { + e2eLabelFromMetaUrl, + isBailianE2EMediaEnabled, + isDashScopeE2EReady, + makeE2eOutputDir, + parseStdoutJson, + runCommandHelp, + runCommandE2e, + runCommandInProcess, +} from "./helpers.ts"; +import { SPEECH_VOICE_ROUTES } from "./topic-routes.ts"; + +const tempDirs: string[] = []; + +const CLONE_TARGET_MODEL = "cosyvoice-v3-flash"; +const CLONE_SYSTEM_VOICE = "longxiaochun_v3"; + +afterEach(() => { + for (const tempDir of tempDirs.splice(0)) { + rmSync(tempDir, { recursive: true, force: true }); + } +}); + +/** Build a short mono 16-bit PCM WAV (silence) for offline dry-run / validation only. */ +function makeTempWav(seconds = 0.05): string { + const tempDir = mkdtempSync(join(tmpdir(), "bl-voice-e2e-")); + tempDirs.push(tempDir); + const filePath = join(tempDir, "reference.wav"); + const sampleRate = 16000; + const sampleCount = Math.max(1, Math.floor(sampleRate * seconds)); + const dataBytes = sampleCount * 2; + const buffer = Buffer.alloc(44 + dataBytes); + buffer.write("RIFF", 0); + buffer.writeUInt32LE(36 + dataBytes, 4); + buffer.write("WAVE", 8); + buffer.write("fmt ", 12); + buffer.writeUInt32LE(16, 16); + buffer.writeUInt16LE(1, 20); + buffer.writeUInt16LE(1, 22); + buffer.writeUInt32LE(sampleRate, 24); + buffer.writeUInt32LE(sampleRate * 2, 28); + buffer.writeUInt16LE(2, 32); + buffer.writeUInt16LE(16, 34); + buffer.write("data", 36); + buffer.writeUInt32LE(dataBytes, 40); + writeFileSync(filePath, buffer); + return filePath; +} + +/** Assert a saved audio file exists, has payload, and looks like WAV/MP3. */ +function assertDecodableAudioFile(filePath: string, minBytes = 1024): void { + expect(existsSync(filePath), `missing audio: ${filePath}`).toBe(true); + const bytes = readFileSync(filePath); + expect(bytes.byteLength, `audio too small: ${filePath}`).toBeGreaterThanOrEqual(minBytes); + const lower = filePath.toLowerCase(); + if (lower.endsWith(".wav")) { + expect(bytes.subarray(0, 4).toString("ascii")).toBe("RIFF"); + expect(bytes.subarray(8, 12).toString("ascii")).toBe("WAVE"); + return; + } + if (lower.endsWith(".mp3")) { + const hasId3 = bytes.subarray(0, 3).toString("ascii") === "ID3"; + const hasFrameSync = bytes[0] === 0xff && ((bytes[1] ?? 0) & 0xe0) === 0xe0; + expect(hasId3 || hasFrameSync).toBe(true); + } +} + +async function waitForVoiceStatus( + voiceId: string, + acceptable: ReadonlySet, + timeoutMs = 90_000, +): Promise { + const deadline = Date.now() + timeoutMs; + let lastStatus = ""; + while (Date.now() < deadline) { + const got = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "get", + "--id", + voiceId, + "--output", + "json", + "--quiet", + ]); + expect(got.exitCode, got.stderr).toBe(0); + lastStatus = parseStdoutJson<{ output?: { status?: string } }>(got.stdout).output?.status ?? ""; + if (acceptable.has(lastStatus)) return lastStatus; + await new Promise((resolve) => setTimeout(resolve, 3000)); + } + throw new Error(`voice ${voiceId} status stayed "${lastStatus}" (wanted ${[...acceptable]})`); +} + +describe("e2e: speech voice help / validation", () => { + test("speech voice --help lists leaf commands", async () => { + const { stderr, exitCode } = await runCommandHelp(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "--help", + ]); + expect(exitCode, stderr).toBe(0); + expect(stderr).toMatch(/clone/i); + expect(stderr).toMatch(/design/i); + expect(stderr).toMatch(/list/i); + expect(stderr).toMatch(/get/i); + expect(stderr).toMatch(/update/i); + expect(stderr).toMatch(/delete/i); + expect(stderr).toMatch(/preview/i); + }); + + test("clone --help shows bilingual audio / prefix flags", async () => { + const { stderr, exitCode } = await runCommandHelp(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--help", + ]); + expect(exitCode, stderr).toBe(0); + expect(stderr).toMatch(/--audio/i); + expect(stderr).toMatch(/--prefix/i); + expect(stderr).toMatch(/--target-model/i); + expect(stderr).toMatch(/Reference audio|参考音频/); + }); + + test("update/delete --help show Risk and --yes", async () => { + for (const action of ["update", "delete"] as const) { + const { stderr, exitCode } = await runCommandHelp(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + action, + "--help", + ]); + expect(exitCode, stderr).toBe(0); + expect(stderr).toMatch(/--yes/i); + expect(stderr).toMatch(/Risk|风险/); + } + }); + + test("clone missing required flags → 2", async () => { + const { exitCode } = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--quiet", + ]); + expect(exitCode).toBe(2); + }); + + test("clone rejects empty/invalid prefix and page bounds", async () => { + const wav = makeTempWav(); + const badPrefix = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--audio", + wav, + "--prefix", + "too_long_prefix", + "--target-model", + "cosyvoice-v3-flash", + "--quiet", + ]); + expect(badPrefix.exitCode).toBe(2); + expect(badPrefix.stderr).toMatch(/prefix/i); + + const uppercaseOk = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--audio", + "https://example.com/a.wav", + "--prefix", + "Demo01", + "--target-model", + "cosyvoice-v3-flash", + "--dry-run", + "--output", + "json", + "--quiet", + ]); + expect(uppercaseOk.exitCode, uppercaseOk.stderr).toBe(0); + + const badPage = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "list", + "--page", + "0", + "--quiet", + ]); + expect(badPage.exitCode).toBe(2); + expect(badPage.stderr).toMatch(/--page/i); + }); + + test("clone rejects max-prompt-audio-length outside 3-30", async () => { + const result = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--audio", + "https://example.com/a.wav", + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3-flash", + "--max-prompt-audio-length", + "2", + "--quiet", + ]); + expect(result.exitCode).toBe(2); + expect(result.stderr).toMatch(/max-prompt-audio-length|3 and 30/i); + }); + + test("get rejects blank id", async () => { + const result = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "get", + "--id", + " ", + "--quiet", + ]); + expect(result.exitCode).toBe(2); + expect(result.stderr).toMatch(/--id/i); + }); + + test("clone dry-run rejects unsupported audio extension and non-audio Data URI", async () => { + const tempDir = mkdtempSync(join(tmpdir(), "bl-voice-bad-audio-")); + tempDirs.push(tempDir); + const badPath = join(tempDir, "ref.txt"); + writeFileSync(badPath, "not-audio"); + + const badExt = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--audio", + badPath, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3-flash", + "--dry-run", + "--quiet", + ]); + expect(badExt.exitCode).toBe(2); + expect(badExt.stderr).toMatch(/Unsupported audio|\.txt/i); + + const badDataUri = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--audio", + "data:text/plain;base64,YQ==", + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3-flash", + "--dry-run", + "--quiet", + ]); + expect(badDataUri.exitCode).toBe(2); + expect(badDataUri.stderr).toMatch(/audio\/\*|MIME/i); + }); + + test("clone/update dry-run reject local audio over 10 MB", async () => { + const tempDir = mkdtempSync(join(tmpdir(), "bl-voice-oversize-")); + tempDirs.push(tempDir); + const oversizePath = join(tempDir, "huge.wav"); + writeFileSync(oversizePath, Buffer.alloc(44, 0)); + truncateSync(oversizePath, 10 * 1024 * 1024 + 1); + + const cloneResult = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--audio", + oversizePath, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3-flash", + "--dry-run", + "--quiet", + ]); + expect(cloneResult.exitCode).toBe(2); + expect(cloneResult.stderr).toMatch(/10 MB|exceeds/i); + + const updateResult = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "update", + "--id", + "voice-x", + "--audio", + oversizePath, + "--dry-run", + "--quiet", + ]); + expect(updateResult.exitCode).toBe(2); + expect(updateResult.stderr).toMatch(/10 MB|exceeds/i); + }); +}); + +describe("e2e: speech voice dry-run / confirmation", () => { + test("clone --dry-run redacts local wav and keeps false volume normalize", async () => { + const wav = makeTempWav(); + const { stdout, stderr, exitCode } = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--audio", + wav, + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3-flash", + "--volume-normalize", + "false", + "--preprocess", + "false", + "--dry-run", + "--output", + "json", + "--quiet", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + endpoint?: string; + request?: { + model?: string; + input?: Record; + }; + }>(stdout); + expect(body.endpoint).toMatch(/\/api\/v1\/services\/audio\/tts\/customization$/); + expect(body.request?.model).toBe("voice-enrollment"); + expect(body.request?.input?.action).toBe("create_voice"); + expect(body.request?.input?.enable_volume_normalization).toBe("false"); + expect(body.request?.input?.enable_preprocess).toBe(false); + expect(String(body.request?.input?.audio)).toMatch(/pending-encode|/); + }); + + test("list --page 2 --dry-run maps page_index=1 with defaults", async () => { + const { stdout, stderr, exitCode } = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "list", + "--page", + "2", + "--dry-run", + "--output", + "json", + "--quiet", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + request?: { input?: { page_index?: number; page_size?: number; action?: string } }; + }>(stdout); + expect(body.request?.input?.action).toBe("list_voice"); + expect(body.request?.input?.page_index).toBe(1); + expect(body.request?.input?.page_size).toBe(10); + }); + + test("delete/update --dry-run without --yes previews with zero side effects", async () => { + for (const args of [ + ["speech", "voice", "delete", "--id", "voice-x", "--dry-run", "--output", "json", "--quiet"], + [ + "speech", + "voice", + "update", + "--id", + "voice-x", + "--audio", + "https://example.com/a.wav", + "--dry-run", + "--output", + "json", + "--quiet", + ], + ] as const) { + const { stdout, stderr, exitCode } = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + ...args, + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ request?: { input?: { action?: string } } }>(stdout); + expect(body.request?.input?.action).toMatch(/delete_voice|update_voice/); + } + }); + + test("delete/update without --yes → 7 requires_confirmation", async () => { + for (const args of [ + [ + "speech", + "voice", + "delete", + "--id", + "voice-x", + "--api-key", + "e2e-dummy", + "--output", + "json", + ], + [ + "speech", + "voice", + "update", + "--id", + "voice-x", + "--audio", + "https://example.com/a.wav", + "--api-key", + "e2e-dummy", + "--output", + "json", + ], + ] as const) { + const { stderr, exitCode } = await runCommandE2e(SPEECH_VOICE_ROUTES, [...args]); + expect(exitCode).toBe(7); + expect(JSON.parse(stderr)).toMatchObject({ + error: { code: 7, type: "requires_confirmation" }, + }); + } + }); +}); + +describe("e2e: speech voice mock HTTP", () => { + test("clone/list/get/preview/delete loopback contract", async () => { + const requests: Array<{ path: string; auth?: string; body: Record }> = []; + const server = createServer((incoming, outgoing) => { + const requestPath = incoming.url ?? ""; + if (requestPath.startsWith("/ref.wav")) { + outgoing.writeHead(200, { "Content-Type": "audio/wav" }); + outgoing.end(Buffer.from("RIFFWAVEdata")); + return; + } + const chunks: Buffer[] = []; + incoming.on("data", (chunk: Buffer) => chunks.push(chunk)); + incoming.on("end", () => { + const body = JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record; + const input = (body.input ?? {}) as Record; + requests.push({ + path: requestPath, + auth: incoming.headers.authorization, + body, + }); + outgoing.writeHead(200, { "Content-Type": "application/json" }); + if (input.action === "create_voice") { + outgoing.end( + JSON.stringify({ + output: { voice_id: "voice-created-1" }, + request_id: "req-create", + usage: { count: 1 }, + }), + ); + return; + } + if (input.action === "list_voice") { + outgoing.end( + JSON.stringify({ + output: { + voice_list: [ + { voice_id: "voice-created-1", status: "OK", target_model: "cosyvoice-v3-flash" }, + ], + }, + request_id: "req-list", + }), + ); + return; + } + if (input.action === "query_voice") { + const address = server.address() as AddressInfo; + outgoing.end( + JSON.stringify({ + output: { + voice_id: "voice-created-1", + status: "DEPLOYING", + target_model: "cosyvoice-v3-flash", + resource_link: `http://127.0.0.1:${address.port}/ref.wav`, + }, + request_id: "req-query", + }), + ); + return; + } + if (input.action === "delete_voice") { + outgoing.end(JSON.stringify({ output: {}, request_id: "req-delete" })); + return; + } + outgoing.end(JSON.stringify({ code: "Unexpected", message: "unexpected action" })); + }); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address() as AddressInfo; + const baseUrl = `http://127.0.0.1:${address.port}`; + const outDir = makeE2eOutputDir("speech-voice-mock"); + const previewPath = join(outDir, "reference.wav"); + + try { + const cloned = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--audio", + "https://example.com/a.wav", + "--prefix", + "demo", + "--target-model", + "cosyvoice-v3-flash", + "--api-key", + "sk-e2e", + "--base-url", + baseUrl, + "--output", + "json", + "--quiet", + ]); + expect(cloned.exitCode, cloned.stderr).toBe(0); + expect( + parseStdoutJson<{ output?: { voice_id?: string } }>(cloned.stdout).output?.voice_id, + ).toBe("voice-created-1"); + + const listed = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "list", + "--api-key", + "sk-e2e", + "--base-url", + baseUrl, + "--output", + "json", + "--quiet", + ]); + expect(listed.exitCode, listed.stderr).toBe(0); + + const got = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "get", + "--id", + "voice-created-1", + "--api-key", + "sk-e2e", + "--base-url", + baseUrl, + "--output", + "json", + "--quiet", + ]); + expect(got.exitCode, got.stderr).toBe(0); + expect(parseStdoutJson<{ output?: { status?: string } }>(got.stdout).output?.status).toBe( + "DEPLOYING", + ); + + const previewed = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "preview", + "--id", + "voice-created-1", + "--out", + previewPath, + "--api-key", + "sk-e2e", + "--base-url", + baseUrl, + "--output", + "json", + "--quiet", + ]); + expect(previewed.exitCode, previewed.stderr).toBe(0); + expect(existsSync(previewPath)).toBe(true); + expect(readFileSync(previewPath).toString("utf8")).toContain("RIFF"); + + const deleted = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "delete", + "--id", + "voice-created-1", + "--yes", + "--api-key", + "sk-e2e", + "--base-url", + baseUrl, + "--output", + "json", + "--quiet", + ]); + expect(deleted.exitCode, deleted.stderr).toBe(0); + + expect(requests.some((entry) => entry.path.includes("/tts/customization"))).toBe(true); + expect( + requests.every((entry) => entry.auth === "Bearer sk-e2e" || entry.path.startsWith("/ref")), + ).toBe(true); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + test("preview without resource_link reports local GENERAL", async () => { + const server = createServer((_incoming, outgoing) => { + outgoing.writeHead(200, { "Content-Type": "application/json" }); + outgoing.end( + JSON.stringify({ + output: { voice_id: "voice-design-1", status: "OK" }, + request_id: "req-query", + }), + ); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address() as AddressInfo; + try { + const result = await runCommandInProcess(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "preview", + "--id", + "voice-design-1", + "--api-key", + "sk-e2e", + "--base-url", + `http://127.0.0.1:${address.port}`, + "--quiet", + ]); + expect(result.exitCode).toBe(1); + expect(result.stderr).toMatch(/resource_link|参考音频|synthesize/i); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); +}); + +describe.skipIf(!isBailianE2EMediaEnabled() || !isDashScopeE2EReady())( + "e2e: speech voice (DashScope media live)", + () => { + test("synthesize → clone → get/list → synthesize with voice_id → delete", async () => { + const outDir = makeE2eOutputDir(e2eLabelFromMetaUrl(import.meta.url)); + const refWav = join(outDir, "clone-ref.wav"); + const clonedWav = join(outDir, "clone-synth.wav"); + const prefix = `bl${Date.now().toString(36).slice(-6)}`; + let voiceId = ""; + + try { + // Real speech reference (not silence): enrollment rejects silent / low-SNR clips. + const reference = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "synthesize", + "--model", + CLONE_TARGET_MODEL, + "--voice", + CLONE_SYSTEM_VOICE, + "--text", + "各位听众朋友们大家好,欢迎收听今天的节目。今天我们一起聊聊城市夜景。", + "--format", + "wav", + "--out", + refWav, + "--output", + "json", + "--quiet", + ]); + expect(reference.exitCode, reference.stderr).toBe(0); + assertDecodableAudioFile(refWav); + + const created = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "clone", + "--audio", + refWav, + "--prefix", + prefix, + "--target-model", + CLONE_TARGET_MODEL, + "--output", + "json", + "--quiet", + ]); + expect(created.exitCode, created.stderr).toBe(0); + voiceId = + parseStdoutJson<{ output?: { voice_id?: string } }>(created.stdout).output?.voice_id ?? + ""; + expect(voiceId).toMatch(/\S+/); + writeFileSync(join(outDir, "voice-id.txt"), `${voiceId}\n`); + + const got = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "get", + "--id", + voiceId, + "--output", + "json", + "--quiet", + ]); + expect(got.exitCode, got.stderr).toBe(0); + const getBody = parseStdoutJson<{ + output?: { voice_id?: string; target_model?: string }; + }>(got.stdout); + expect(getBody.output?.voice_id).toBe(voiceId); + expect(getBody.output?.target_model).toBe(CLONE_TARGET_MODEL); + + const listed = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "list", + "--prefix", + prefix, + "--page-size", + "100", + "--output", + "json", + "--quiet", + ]); + expect(listed.exitCode, listed.stderr).toBe(0); + const listBody = parseStdoutJson<{ + output?: { voice_list?: Array<{ voice_id?: string }> }; + }>(listed.stdout); + expect(listBody.output?.voice_list?.some((entry) => entry.voice_id === voiceId)).toBe(true); + + await waitForVoiceStatus(voiceId, new Set(["OK"])); + + // Same target model + cloned voice id must synthesize decodable audio. + const withClone = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "synthesize", + "--model", + CLONE_TARGET_MODEL, + "--voice", + voiceId, + "--text", + "这是复刻音色的试听。", + "--format", + "wav", + "--out", + clonedWav, + "--output", + "json", + "--quiet", + ]); + expect(withClone.exitCode, withClone.stderr).toBe(0); + const synthBody = parseStdoutJson<{ saved?: string; voice?: string; model?: string }>( + withClone.stdout, + ); + expect(synthBody.saved).toBe(clonedWav); + expect(synthBody.voice).toBe(voiceId); + expect(synthBody.model).toBe(CLONE_TARGET_MODEL); + assertDecodableAudioFile(clonedWav); + } finally { + if (voiceId) { + const deleted = await runCommandE2e(SPEECH_VOICE_ROUTES, [ + "speech", + "voice", + "delete", + "--id", + voiceId, + "--yes", + "--quiet", + ]); + expect(deleted.exitCode, deleted.stderr).toBe(0); + } + } + }, 300_000); + }, +); diff --git a/packages/commands/tests/e2e/topic-routes.ts b/packages/commands/tests/e2e/topic-routes.ts index 94bc55fc7..89b2bf421 100644 --- a/packages/commands/tests/e2e/topic-routes.ts +++ b/packages/commands/tests/e2e/topic-routes.ts @@ -136,11 +136,34 @@ export const VISION_ROUTES: E2eRouteExports = { export const SPEECH_ROUTES: E2eRouteExports = { "speech synthesize": "speechSynthesize", "speech recognize": "speechRecognize", + "speech generate": "speechGenerate", "speech vocabulary create": "speechVocabularyCreate", "speech vocabulary list": "speechVocabularyList", "speech vocabulary get": "speechVocabularyGet", "speech vocabulary update": "speechVocabularyUpdate", "speech vocabulary delete": "speechVocabularyDelete", + "speech voice clone": "speechVoiceClone", + "speech voice design": "speechVoiceDesign", + "speech voice list": "speechVoiceList", + "speech voice get": "speechVoiceGet", + "speech voice update": "speechVoiceUpdate", + "speech voice delete": "speechVoiceDelete", + "speech voice preview": "speechVoicePreview", +}; + +export const SPEECH_VOICE_ROUTES: E2eRouteExports = { + "speech voice clone": "speechVoiceClone", + "speech voice design": "speechVoiceDesign", + "speech voice list": "speechVoiceList", + "speech voice get": "speechVoiceGet", + "speech voice update": "speechVoiceUpdate", + "speech voice delete": "speechVoiceDelete", + "speech voice preview": "speechVoicePreview", + "speech synthesize": "speechSynthesize", +}; + +export const SPEECH_GENERATE_ROUTES: E2eRouteExports = { + "speech generate": "speechGenerate", }; export const MCP_ROUTES: E2eRouteExports = { diff --git a/packages/core/src/client/endpoints.ts b/packages/core/src/client/endpoints.ts index aa6846112..cb014ee3d 100644 --- a/packages/core/src/client/endpoints.ts +++ b/packages/core/src/client/endpoints.ts @@ -190,6 +190,11 @@ export function speechVocabularyPath(): string { return "/api/v1/services/audio/asr/customization"; } +// ---- Voice enrollment (TTS customization: clone / design) ---- +export function speechVoiceCustomizationPath(): string { + return "/api/v1/services/audio/tts/customization"; +} + // ---- Memory Profile (long-term memory v2, workspace-based host) ---- export function profileSchemaPath(): string { return "/api/v2/apps/memory/profile_schemas"; diff --git a/packages/core/src/client/index.ts b/packages/core/src/client/index.ts index 035d9e761..eef4d20c6 100644 --- a/packages/core/src/client/index.ts +++ b/packages/core/src/client/index.ts @@ -40,6 +40,7 @@ export { speechRecognizePath, speechSynthesizePath, speechVocabularyPath, + speechVoiceCustomizationPath, taskPath, userProfilePath, userProfileValuesPath, diff --git a/packages/core/src/files/index.ts b/packages/core/src/files/index.ts index 89c55520c..692b293f4 100644 --- a/packages/core/src/files/index.ts +++ b/packages/core/src/files/index.ts @@ -4,6 +4,7 @@ export { resolveFileUrl, imageFileToDataUri, audioFileToDataUri, + audioMimeForPath, redactDataUri, } from "./upload.ts"; export { CONSOLE_FILE_URL_API, getConsoleFileUrl } from "./url.ts"; diff --git a/packages/core/src/files/upload.ts b/packages/core/src/files/upload.ts index aff7dfdea..15f621ec1 100644 --- a/packages/core/src/files/upload.ts +++ b/packages/core/src/files/upload.ts @@ -265,12 +265,20 @@ export function audioFileToDataUri(filePath: string): string { return fileToDataUri(filePath, AUDIO_MIME_TYPES, "audio"); } -/** Keep dry-run output readable and avoid echoing the complete inline image. */ +/** + * Keep dry-run output readable and avoid echoing inline media payloads. + * Accepts optional Data URI parameters (codecs, charset, …) before `;base64,`. + */ export function redactDataUri(input: string): string { - const match = /^data:([^;,]+);base64,/i.exec(input); + const match = /^data:([^;,]+).*?;base64,/i.exec(input); return match ? `data:${match[1]};base64,` : input; } +/** Return the audio MIME for a local path, or undefined when the extension is unsupported. */ +export function audioMimeForPath(filePath: string): string | undefined { + return AUDIO_MIME_TYPES[extname(filePath).toLowerCase()]; +} + /** * Upload a local file to DashScope temporary storage and return the oss:// URL. * The URL is valid for 48 hours. diff --git a/packages/core/src/speech/index.ts b/packages/core/src/speech/index.ts index 1df342e84..582d9bfd5 100644 --- a/packages/core/src/speech/index.ts +++ b/packages/core/src/speech/index.ts @@ -12,3 +12,23 @@ export { type VocabularyRequest, } from "./vocabulary.ts"; export { parseInstantVocabulary, parseVocabularyEntries } from "./vocabulary-input.ts"; +export { + VOICE_ENROLLMENT_MODEL, + buildVoiceRequest, + createCloneVoice, + createDesignVoice, + listVoices, + queryVoice, + updateVoice, + deleteVoice, + type VoiceAction, + type VoicePreviewAudio, + type VoiceListItem, + type VoiceQueryOutput, + type VoiceCreateOutput, + type VoiceEnvelope, + type VoiceRequest, + type CreateCloneVoiceInput, + type CreateDesignVoiceInput, + type CreateVoiceParameters, +} from "./voice.ts"; diff --git a/packages/core/src/speech/voice.ts b/packages/core/src/speech/voice.ts new file mode 100644 index 000000000..992d65c0f --- /dev/null +++ b/packages/core/src/speech/voice.ts @@ -0,0 +1,191 @@ +/** + * Voice enrollment HTTP API wrappers (clone / design / manage). + * + * Thin functions over `requestJson`. Bodies and responses stay snake_case so + * callers can surface fields without remapping. + */ +import { speechVoiceCustomizationPath } from "../client/endpoints.ts"; +import type { Client } from "../client/client.ts"; + +/** Fixed model id for the voice customization endpoint. */ +export const VOICE_ENROLLMENT_MODEL = "voice-enrollment"; + +export type VoiceAction = + | "create_voice" + | "list_voice" + | "query_voice" + | "update_voice" + | "delete_voice"; + +export interface VoicePreviewAudio { + data?: string; + sample_rate?: number; + response_format?: string; +} + +export interface VoiceListItem { + voice_id?: string; + gmt_create?: string; + gmt_modified?: string; + /** DEPLOYING | OK | UNDEPLOYED */ + status?: string; + target_model?: string; + voice_prompt?: string; + preview_text?: string; +} + +export interface VoiceQueryOutput { + voice_id?: string; + gmt_create?: string; + gmt_modified?: string; + status?: string; + target_model?: string; + /** Reference audio URL for cloned voices; not guaranteed for design voices. */ + resource_link?: string; + voice_prompt?: string; + preview_text?: string; + preview_audio?: VoicePreviewAudio; +} + +export interface VoiceCreateOutput { + voice_id?: string; + target_model?: string; + preview_audio?: VoicePreviewAudio; + request_id?: string; +} + +export interface VoiceEnvelope { + request_id?: string; + output?: T; + usage?: { count?: number }; + code?: string; + message?: string; +} + +export interface VoiceRequest { + model: string; + input: Record; + parameters?: Record; +} + +/** Clone create input (reference audio). */ +export interface CreateCloneVoiceInput { + target_model: string; + prefix: string; + audio: string; + language_hints?: string[]; + enable_volume_normalization?: string; + max_prompt_audio_length?: number; + enable_preprocess?: boolean; +} + +/** Design create input (text prompt). Mutually exclusive with audio. */ +export interface CreateDesignVoiceInput { + target_model: string; + prefix: string; + voice_prompt: string; + preview_text: string; + language_hints?: string[]; +} + +export interface CreateVoiceParameters { + sample_rate?: number; + response_format?: string; +} + +/** Shared request body builder for dry-run and live calls. */ +export function buildVoiceRequest( + action: VoiceAction, + input: Record, + parameters?: Record, +): VoiceRequest { + const request: VoiceRequest = { + model: VOICE_ENROLLMENT_MODEL, + input: { action, ...input }, + }; + if (parameters !== undefined && Object.keys(parameters).length > 0) { + request.parameters = parameters; + } + return request; +} + +async function callVoiceApi( + client: Client, + action: VoiceAction, + input: Record, + options?: { parameters?: Record; signal?: AbortSignal }, +): Promise> { + return client.requestJson>({ + path: speechVoiceCustomizationPath(), + method: "POST", + body: buildVoiceRequest(action, input, options?.parameters), + signal: options?.signal, + }); +} + +function omitUndefined(record: Record): Record { + const result: Record = {}; + for (const [key, value] of Object.entries(record)) { + if (value !== undefined) result[key] = value; + } + return result; +} + +export function createCloneVoice( + client: Client, + input: CreateCloneVoiceInput, + signal?: AbortSignal, +): Promise> { + return callVoiceApi(client, "create_voice", omitUndefined({ ...input }), { signal }); +} + +export function createDesignVoice( + client: Client, + input: CreateDesignVoiceInput, + parameters?: CreateVoiceParameters, + signal?: AbortSignal, +): Promise> { + const params = parameters ? omitUndefined({ ...parameters }) : undefined; + return callVoiceApi(client, "create_voice", omitUndefined({ ...input }), { + parameters: params && Object.keys(params).length > 0 ? params : undefined, + signal, + }); +} + +export function listVoices( + client: Client, + params: { prefix?: string; pageIndex?: number; pageSize?: number } = {}, + signal?: AbortSignal, +): Promise> { + const input = omitUndefined({ + prefix: params.prefix, + page_index: params.pageIndex, + page_size: params.pageSize, + }); + return callVoiceApi(client, "list_voice", input, { signal }); +} + +export function queryVoice( + client: Client, + voiceId: string, + signal?: AbortSignal, +): Promise> { + return callVoiceApi(client, "query_voice", { voice_id: voiceId }, { signal }); +} + +export function updateVoice( + client: Client, + voiceId: string, + audio: string, + signal?: AbortSignal, +): Promise>> { + return callVoiceApi(client, "update_voice", { voice_id: voiceId, audio }, { signal }); +} + +export function deleteVoice( + client: Client, + voiceId: string, + signal?: AbortSignal, +): Promise>> { + return callVoiceApi(client, "delete_voice", { voice_id: voiceId }, { signal }); +} diff --git a/packages/core/src/types/api.ts b/packages/core/src/types/api.ts index 2c7e94c15..8ca1de651 100644 --- a/packages/core/src/types/api.ts +++ b/packages/core/src/types/api.ts @@ -800,6 +800,47 @@ export interface DashScopeTTSStreamChunk { request_id?: string; } +// ---- Audio content generation (qwen-audio-*-tts-next) ---- + +export interface AudioGenerationReference { + audio_url?: string; + audio_data?: string; +} + +export interface AudioGenerationRequest { + model: string; + input: { + text_prompt: string; + references?: AudioGenerationReference[]; + format?: "wav" | "mp3" | "pcm"; + sample_rate?: number; + channels?: number; + volume?: number; + rate?: number; + seed?: number; + enable_aigc_tag?: boolean; + enable_cbr?: boolean; + bit_rate?: number; + quality?: number; + }; +} + +export interface AudioGenerationResponse { + output: { + audio: { + url?: string; + data?: string; + id?: string; + expires_at?: string; + duration?: number; + }; + }; + usage?: Record; + request_id?: string; + code?: string; + message?: string; +} + // ---- Speech Recognition / ASR (DashScope) ---- /** Context-enhancement message for async ASR `input.context` / sync Flash `input.messages`. */ diff --git a/packages/core/src/types/index.ts b/packages/core/src/types/index.ts index e3f273dc6..316b7d9ca 100644 --- a/packages/core/src/types/index.ts +++ b/packages/core/src/types/index.ts @@ -62,6 +62,9 @@ export type { DashScopeTTSRequest, DashScopeTTSResponse, DashScopeTTSStreamChunk, + AudioGenerationReference, + AudioGenerationRequest, + AudioGenerationResponse, DashScopeVideoEditRequest, DashScopeVideoRefRequest, DashScopeVideoRequest, diff --git a/packages/core/tests/image-input.test.ts b/packages/core/tests/image-input.test.ts index 903b27d91..d4b630f38 100644 --- a/packages/core/tests/image-input.test.ts +++ b/packages/core/tests/image-input.test.ts @@ -58,6 +58,18 @@ describe("Token Plan image input compatibility", () => { expect(redactDataUri(dataUri)).toBe("data:image/png;base64,"); }); + test("redacts Data URIs that include codecs or charset parameters", () => { + expect(redactDataUri("data:audio/mp4;codecs=mp4a.40.2;base64,SECRET")).toBe( + "data:audio/mp4;base64,", + ); + expect(redactDataUri("data:audio/wav;charset=utf-8;base64,SECRET")).toBe( + "data:audio/wav;base64,", + ); + expect(redactDataUri("data:image/png;foo=1;bar=2;base64,SECRET")).toBe( + "data:image/png;base64,", + ); + }); + test("rejects files whose image MIME type cannot be inferred", () => { const imagePath = makeImage(".unknown"); expect(() => imageFileToDataUri(imagePath)).toThrow(/Unsupported image format/); diff --git a/packages/core/tests/speech-voice.test.ts b/packages/core/tests/speech-voice.test.ts new file mode 100644 index 000000000..a6d61484b --- /dev/null +++ b/packages/core/tests/speech-voice.test.ts @@ -0,0 +1,117 @@ +import { describe, expect, test } from "vite-plus/test"; +import { + VOICE_ENROLLMENT_MODEL, + buildVoiceRequest, + type CreateCloneVoiceInput, + type CreateDesignVoiceInput, +} from "../src/index.ts"; + +describe("buildVoiceRequest", () => { + test("create_voice clone body keeps audio and string volume normalize", () => { + const input: CreateCloneVoiceInput = { + target_model: "cosyvoice-v3-flash", + prefix: "Demo01", + audio: "https://example.com/ref.wav", + enable_volume_normalization: "false", + enable_preprocess: false, + language_hints: ["zh"], + max_prompt_audio_length: 10, + }; + const request = buildVoiceRequest("create_voice", { ...input }); + expect(request.model).toBe(VOICE_ENROLLMENT_MODEL); + expect(request.input).toEqual({ + action: "create_voice", + target_model: "cosyvoice-v3-flash", + prefix: "Demo01", + audio: "https://example.com/ref.wav", + enable_volume_normalization: "false", + enable_preprocess: false, + language_hints: ["zh"], + max_prompt_audio_length: 10, + }); + expect(request.parameters).toBeUndefined(); + }); + + test("create_voice design body has voice_prompt/preview_text without audio", () => { + const input: CreateDesignVoiceInput = { + target_model: "cosyvoice-v3.5-plus", + prefix: "demo", + voice_prompt: "沉稳温暖的中文播音员", + preview_text: "各位听众朋友们大家好,欢迎收听今天的节目。", + language_hints: ["zh"], + }; + const request = buildVoiceRequest( + "create_voice", + { ...input }, + { sample_rate: 24000, response_format: "wav" }, + ); + expect(request.input.action).toBe("create_voice"); + expect(request.input).not.toHaveProperty("audio"); + expect(request.input.voice_prompt).toBe("沉稳温暖的中文播音员"); + expect(request.input.preview_text).toBe("各位听众朋友们大家好,欢迎收听今天的节目。"); + expect(request.parameters).toEqual({ + sample_rate: 24000, + response_format: "wav", + }); + }); + + test("list_voice maps page_index / page_size", () => { + const request = buildVoiceRequest("list_voice", { + prefix: "demo", + page_index: 1, + page_size: 20, + }); + expect(request.input).toEqual({ + action: "list_voice", + prefix: "demo", + page_index: 1, + page_size: 20, + }); + }); + + test("query_voice / update_voice / delete_voice use voice_id", () => { + expect(buildVoiceRequest("query_voice", { voice_id: "v1" }).input).toEqual({ + action: "query_voice", + voice_id: "v1", + }); + expect( + buildVoiceRequest("update_voice", { + voice_id: "v1", + audio: "https://example.com/a.wav", + }).input, + ).toEqual({ + action: "update_voice", + voice_id: "v1", + audio: "https://example.com/a.wav", + }); + expect(buildVoiceRequest("delete_voice", { voice_id: "v1" }).input).toEqual({ + action: "delete_voice", + voice_id: "v1", + }); + }); + + test("optional false values are preserved when provided", () => { + const withFalse = buildVoiceRequest("create_voice", { + target_model: "cosyvoice-v3-flash", + prefix: "demo", + audio: "https://example.com/a.wav", + enable_preprocess: false, + enable_volume_normalization: "false", + }); + expect(withFalse.input.enable_preprocess).toBe(false); + expect(withFalse.input.enable_volume_normalization).toBe("false"); + + const withoutOptional = buildVoiceRequest("create_voice", { + target_model: "cosyvoice-v3-flash", + prefix: "demo", + audio: "https://example.com/a.wav", + }); + expect(withoutOptional.input).not.toHaveProperty("enable_preprocess"); + expect(withoutOptional.input).not.toHaveProperty("enable_volume_normalization"); + }); + + test("empty parameters object is omitted", () => { + const request = buildVoiceRequest("create_voice", { prefix: "demo" }, {}); + expect(request.parameters).toBeUndefined(); + }); +}); diff --git a/packages/e2e/src/gating.ts b/packages/e2e/src/gating.ts index 03da48b9a..d5cfccaa3 100644 --- a/packages/e2e/src/gating.ts +++ b/packages/e2e/src/gating.ts @@ -37,6 +37,18 @@ export function isBailianE2EMediaEnabled(): boolean { return isBailianE2EEnabled(); } +/** + * `speech generate` live: media gate + API key + workspace (default MaaS host). + * Missing workspace must skip the suite, not early-return a green pass. + */ +export function isSpeechGenerateE2EReady(): boolean { + return ( + isBailianE2EMediaEnabled() && + isDashScopeE2EReady() && + !!process.env.BAILIAN_WORKSPACE_ID?.trim() + ); +} + /** 文生视频 / 图生视频等(耗时长,默认关闭) */ export function isBailianE2EVideoEnabled(): boolean { return isBailianE2EEnabled() && process.env.BAILIAN_E2E_VIDEO === "1"; diff --git a/skills/bailian-gen/SKILL.md b/skills/bailian-gen/SKILL.md index 7ae20cae8..23bf9e51a 100644 --- a/skills/bailian-gen/SKILL.md +++ b/skills/bailian-gen/SKILL.md @@ -35,22 +35,31 @@ BAILIAN_CALLER_TYPE=agent BAILIAN_CALLER_NAME=codex BAILIAN_SKILL_NAME=bailian-g ## When to use which command -| User intent | Command | Default model | -| ---------------------------------------- | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | -| Text-to-image | `bl image generate` | `qwen-image-3.0` | -| Image edit / multi-image merge | `bl image edit` (repeat `--image`) | `qwen-image-3.0` | -| Text-to-video / image-to-video | `bl video generate` | `wan3.0-video` | -| Video edit / style transfer | `bl video edit` | `happyhorse-1.0-video-edit` | -| Reference-to-video + voice | `bl video ref` | `wan3.0-video` | -| Speech synthesis (TTS / voiceover) | `bl speech synthesize` | `cosyvoice-v3-flash` | -| Speech recognition (ASR / transcription) | `bl speech recognize` | `fun-asr` | -| Image describe | `bl vision describe` | `qwen3-vl-plus`;宿主能做且未点名 → host-first | -| Video / A-V understand | `bl vision describe --video` 或 `bl omni` | 视频理解默认走百炼;`omni` 默认 `qwen3.8-omni-flash`,省略 `--model` 且无 `--text-only` 时自动切换 `qwen3.5-omni-plus` | +| User intent | Command | Default model | +| ------------------------------------------- | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------- | +| Text-to-image | `bl image generate` | `qwen-image-3.0` | +| Image edit / multi-image merge | `bl image edit` (repeat `--image`) | `qwen-image-3.0` | +| Text-to-video / image-to-video | `bl video generate` | `wan3.0-video` | +| Video edit / style transfer | `bl video edit` | `happyhorse-1.0-video-edit` | +| Reference-to-video + voice | `bl video ref` | `wan3.0-video` | +| Speech synthesis (TTS / voiceover) | `bl speech synthesize` | `cosyvoice-v3-flash` | +| Voice clone / manage | `bl speech voice clone/list/get/update/delete/preview` | `voice-enrollment` → then synthesize with the same `--target-model` | +| Voice design from text | `bl speech voice design` | `voice-enrollment` → preview saved locally; formal listen via `speech synthesize` | +| Audio content generation (podcast/dialogue) | `bl speech generate` | `qwen-audio-3.1-tts-next` (text_prompt/references; not voice IDs) | +| Speech recognition (ASR / transcription) | `bl speech recognize` | `fun-asr` | +| Image describe | `bl vision describe` | `qwen3-vl-plus`;宿主能做且未点名 → host-first | +| Video / A-V understand | `bl vision describe --video` 或 `bl omni` | 视频理解默认走百炼;`omni` 默认 `qwen3.8-omni-flash`,省略 `--model` 且无 `--text-only` 时自动切换 `qwen3.5-omni-plus` | Unless the user explicitly specifies a model, omit `--model` and let the CLI use the active Profile’s default. For ASR model selection, keep `fun-asr` (or other `*-filetrans`) for long recordings, repeated files, speaker diarization, or asynchronous task IDs. For one local or remote audio file up to about five minutes when the user asks for low-latency Flash models, use `--model fun-asr-flash-2026-06-15`, `--model qwen-audio-3.0-asr-flash`, or `--model qwen3-asr-flash`. Flash recognition is synchronous and accepts exactly one file per call. +Voice clone vs design vs generate: + +- Clone (`bl speech voice clone`) needs reference audio; manage with list/get/update/delete; `preview` returns enrollment `resource_link` (not a synthesized sample). +- Design (`bl speech voice design`) creates a voice from a text description and saves the one-shot `preview_audio` locally; formal listen still uses `bl speech synthesize --voice --model `. +- Generate (`bl speech generate`) creates podcast/dialogue content from `text_prompt` + optional `@voice1..3` references; it does **not** take system/cloned voice IDs. + To improve ASR accuracy with domain terms: - Prefer instant `--vocabulary` / `--context` on `bl speech recognize` when the model is Qwen-Audio-3.0-ASR-Flash series (and Fun-ASR-Flash for `--context` only) — no pre-built vocabulary needed. Start weights at 4 (do not default everything to 5). `--context` must list the target words themselves; a topic description alone has little effect. diff --git a/skills/bailian-gen/reference/index.md b/skills/bailian-gen/reference/index.md index 62cab27f0..841255a6e 100644 --- a/skills/bailian-gen/reference/index.md +++ b/skills/bailian-gen/reference/index.md @@ -14,6 +14,7 @@ Use this index for the skill-scoped quick index and global flags. | `bl image edit` | API Key | Edit an existing image with text instructions (Qwen-Image / Wan 2.7) | [image.md](image.md) | | `bl image generate` | API Key | Generate images (Qwen-Image / wan2.x) | [image.md](image.md) | | `bl omni` | API Key | Multimodal chat with text + audio output (Qwen-Omni) | [omni.md](omni.md) | +| `bl speech generate` | API Key | Generate podcast/dialogue audio content from a text prompt | [speech.md](speech.md) | | `bl speech recognize` | API Key | Recognize speech from audio files (FunAudio-ASR / Qwen-ASR Flash) | [speech.md](speech.md) | | `bl speech synthesize` | API Key | Synthesize speech from text | [speech.md](speech.md) | | `bl speech vocabulary create` | API Key | Create a precompiled hot-word vocabulary for ASR | [speech.md](speech.md) | @@ -21,6 +22,13 @@ Use this index for the skill-scoped quick index and global flags. | `bl speech vocabulary get` | API Key | Get details of a precompiled hot-word vocabulary | [speech.md](speech.md) | | `bl speech vocabulary list` | API Key | List precompiled hot-word vocabularies | [speech.md](speech.md) | | `bl speech vocabulary update` | API Key | Replace the contents of a precompiled hot-word vocabulary | [speech.md](speech.md) | +| `bl speech voice clone` | API Key | Clone a custom voice from reference audio | [speech.md](speech.md) | +| `bl speech voice delete` | API Key | Delete a custom voice | [speech.md](speech.md) | +| `bl speech voice design` | API Key | Design a custom voice from a text description | [speech.md](speech.md) | +| `bl speech voice get` | API Key | Get details of a custom voice | [speech.md](speech.md) | +| `bl speech voice list` | API Key | List custom voices | [speech.md](speech.md) | +| `bl speech voice preview` | API Key | Fetch the reference audio URL for a cloned voice | [speech.md](speech.md) | +| `bl speech voice update` | API Key | Update the reference audio of a cloned voice | [speech.md](speech.md) | | `bl video download` | API Key | Download a completed video by task ID | [video.md](video.md) | | `bl video edit` | API Key | Edit a video with happyhorse-1.0-video-edit (style transfer, object replacement, etc.) | [video.md](video.md) | | `bl video generate` | API Key | Generate a video from text or image (wan3.0-video / wan2.6-t2v / happyhorse-1.1-i2v) | [video.md](video.md) | @@ -30,13 +38,13 @@ Use this index for the skill-scoped quick index and global flags. ## By group -| Group | Commands | Reference | -| -------- | ----------------------------------------------------------------------------------------------------------------------------- | ---------------------- | -| `image` | `edit`, `generate` | [image.md](image.md) | -| `omni` | `(root)` | [omni.md](omni.md) | -| `speech` | `recognize`, `synthesize`, `vocabulary create`, `vocabulary delete`, `vocabulary get`, `vocabulary list`, `vocabulary update` | [speech.md](speech.md) | -| `video` | `download`, `edit`, `generate`, `ref`, `task get` | [video.md](video.md) | -| `vision` | `describe` | [vision.md](vision.md) | +| Group | Commands | Reference | +| -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | +| `image` | `edit`, `generate` | [image.md](image.md) | +| `omni` | `(root)` | [omni.md](omni.md) | +| `speech` | `generate`, `recognize`, `synthesize`, `vocabulary create`, `vocabulary delete`, `vocabulary get`, `vocabulary list`, `vocabulary update`, `voice clone`, `voice delete`, `voice design`, `voice get`, `voice list`, `voice preview`, `voice update` | [speech.md](speech.md) | +| `video` | `download`, `edit`, `generate`, `ref`, `task get` | [video.md](video.md) | +| `vision` | `describe` | [vision.md](vision.md) | ## Global flags diff --git a/skills/bailian-gen/reference/speech.md b/skills/bailian-gen/reference/speech.md index ce095288d..0da1c7484 100644 --- a/skills/bailian-gen/reference/speech.md +++ b/skills/bailian-gen/reference/speech.md @@ -9,6 +9,7 @@ Index: [index.md](index.md) | Command | Authentication | Description | | ----------------------------- | -------------- | ----------------------------------------------------------------- | +| `bl speech generate` | API Key | Generate podcast/dialogue audio content from a text prompt | | `bl speech recognize` | API Key | Recognize speech from audio files (FunAudio-ASR / Qwen-ASR Flash) | | `bl speech synthesize` | API Key | Synthesize speech from text | | `bl speech vocabulary create` | API Key | Create a precompiled hot-word vocabulary for ASR | @@ -16,9 +17,63 @@ Index: [index.md](index.md) | `bl speech vocabulary get` | API Key | Get details of a precompiled hot-word vocabulary | | `bl speech vocabulary list` | API Key | List precompiled hot-word vocabularies | | `bl speech vocabulary update` | API Key | Replace the contents of a precompiled hot-word vocabulary | +| `bl speech voice clone` | API Key | Clone a custom voice from reference audio | +| `bl speech voice delete` | API Key | Delete a custom voice | +| `bl speech voice design` | API Key | Design a custom voice from a text description | +| `bl speech voice get` | API Key | Get details of a custom voice | +| `bl speech voice list` | API Key | List custom voices | +| `bl speech voice preview` | API Key | Fetch the reference audio URL for a cloned voice | +| `bl speech voice update` | API Key | Update the reference audio of a cloned voice | ## Command details +### `bl speech generate` + +| Field | Value | +| ------------------ | ------------------------------------------------------------------------- | +| **Name** | `speech generate` | +| **Description** | Generate podcast/dialogue audio content from a text prompt | +| **Authentication** | API Key | +| **Usage** | `bl speech generate --text-prompt [--audio ] [flags]` | + +#### Flags + +| Flag | Type | Required | Description | +| ----------------------- | ------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------- | +| `--text-prompt ` | string | yes | Audio generation prompt (up to 3000 characters) | +| `--audio ` | array | no | Reference audio URL, local file, or Data URI (repeatable, up to 3; WAV/MP3/OGG Opus only). Referenced in order as @voice1/@voice2/@voice3 | +| `--model ` | string | no | Model id (default: qwen-audio-3.1-tts-next) | +| `--workspace-id ` | string | no | Workspace ID for the default MaaS endpoint (or set BAILIAN_WORKSPACE_ID) | +| `--format ` | string | no | Output format: wav / mp3 / pcm (default: wav) | +| `--sample-rate ` | number | no | Sample rate: 8000 / 16000 / 24000 / 44100 / 48000 | +| `--channels ` | number | no | Channels: 1 or 2 | +| `--volume ` | number | no | Volume 0-100 (integer) | +| `--rate ` | number | no | Speech rate 0.5-2 | +| `--seed ` | number | no | Random seed (integer) | +| `--aigc-tag ` | boolean | no | Enable AIGC tag (enable_aigc_tag) | +| `--enable-cbr ` | boolean | no | Enable MP3 CBR encoding (MP3 only) | +| `--bit-rate ` | number | no | MP3 bit rate when CBR is enabled | +| `--quality <0-9>` | number | no | MP3 VBR quality 0-9 (when CBR is off) | +| `--out-dir ` | string | no | Output directory for the generated audio | +| `--out-prefix ` | string | no | Filename prefix (basename only; cannot escape the output directory) | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- Unlike `speech synthesize`, this command uses text_prompt/references and does not accept system or cloned voice IDs. +- Default endpoint needs a workspace ID (Beijing/Singapore). Explicit --base-url / DASHSCOPE_BASE_URL / Profile base_url overrides without requiring workspace. + +#### Examples + +```bash +bl speech generate --workspace-id --text-prompt "Generate a short podcast about city nightscapes." --dry-run --output json +``` + +```bash +bl speech generate --workspace-id --text-prompt "Introduce today's weather with @voice1." --audio ./reference.wav --out-dir ./audio +``` + ### `bl speech recognize` | Field | Value | @@ -370,3 +425,255 @@ bl speech vocabulary update --id vocab-demo-xxx --words '{"Fendouzhe":4}' --dry- # Only after explicit user confirmation: bl speech vocabulary update --id vocab-demo-xxx --words '{"Fendouzhe":4,"Jingluo":4}' --yes ``` + +### `bl speech voice clone` + +| Field | Value | +| ------------------ | -------------------------------------------------------------------------------------------- | +| **Name** | `speech voice clone` | +| **Description** | Clone a custom voice from reference audio | +| **Authentication** | API Key | +| **Usage** | `bl speech voice clone --audio --prefix --target-model [flags]` | + +#### Flags + +| Flag | Type | Required | Description | +| ------------------------------------- | ------- | -------- | ---------------------------------------------------------------------------------------- | +| `--audio ` | string | yes | Reference audio URL or local file | +| `--prefix ` | string | yes | Custom voice prefix. Digits and letters only, 1-10 characters | +| `--target-model ` | string | yes | Target TTS model for this voice (required). Must match later `speech synthesize --model` | +| `--language-hint ` | array | no | Language hint for the reference audio (at most one; mapped to language_hints) | +| `--volume-normalize ` | boolean | no | Enable volume normalization (sent as string "true"/"false") | +| `--max-prompt-audio-length ` | number | no | Max prompt audio length in seconds (3-30) | +| `--preprocess ` | boolean | no | Enable audio preprocess (enable_preprocess) | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- You must have the right to use the reference audio or described voice. Do not clone or imitate a real person without authorization. +- A voice ID is bound to its target model. Later `speech synthesize` calls must use the same --model. +- Creation returns voice_id immediately. Review status is DEPLOYING/OK/UNDEPLOYED — use `speech voice get` to check; preview reference audio with `speech voice preview`. + +#### Examples + +```bash +bl speech voice clone --audio ./reference.wav --prefix demo --target-model cosyvoice-v3-flash --dry-run --output json +``` + +```bash +bl speech voice clone --audio https://example.com/ref.wav --prefix Demo01 --target-model cosyvoice-v3-flash --language-hint zh +``` + +### `bl speech voice delete` + +| Field | Value | +| ------------------ | -------------------------------------------------------------------------------------------------------------------- | +| **Name** | `speech voice delete` | +| **Description** | Delete a custom voice | +| **Authentication** | API Key | +| **Usage** | `bl speech voice delete --id ` | +| **Risk** | `high` | +| **Risk message** | This permanently deletes the voice ID. Synthesis that still references it will fail and the action cannot be undone. | + +> **Agent safety:** Never add `--yes` automatically. On `type="requires_confirmation"`, stop and ask for explicit user confirmation of the same action and scope. + +#### Flags + +| Flag | Type | Required | Description | +| ------------------ | ------ | -------- | -------------------------------- | +| `--id ` | string | yes | Voice ID (required) | +| `--yes` | switch | no | Confirm this high-risk operation | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- Deletion is irreversible. Prefer --dry-run first to confirm the request body. + +#### Examples + +```bash +bl speech voice delete --id --dry-run --output json +``` + +```bash +# Only after explicit user confirmation: +bl speech voice delete --id --yes +``` + +### `bl speech voice design` + +| Field | Value | +| ------------------ | ------------------------------------------------------------------------------------------------------------- | +| **Name** | `speech voice design` | +| **Description** | Design a custom voice from a text description | +| **Authentication** | API Key | +| **Usage** | `bl speech voice design --prompt --preview-text --prefix --target-model [flags]` | + +#### Flags + +| Flag | Type | Required | Description | +| ------------------------ | ------ | -------- | ---------------------------------------------------------------------------------------- | +| `--prompt ` | string | yes | Voice description (up to 500 characters) | +| `--preview-text ` | string | yes | Preview text spoken in the design sample (15-200 characters) | +| `--prefix ` | string | yes | Custom voice prefix. Digits and letters only, 1-10 characters | +| `--target-model ` | string | yes | Target TTS model for this voice (required). Must match later `speech synthesize --model` | +| `--language-hint ` | array | no | Language hint (zh/en; at most one; server default: zh) | +| `--sample-rate ` | number | no | Preview sample rate: 16000 / 24000 / 48000 (server default: 24000) | +| `--format ` | string | no | Preview format: pcm / wav / mp3 (server default: wav) | +| `--out ` | string | no | Local path for the preview audio (optional; defaults under the audio output dir) | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- You must have the right to use the reference audio or described voice. Do not clone or imitate a real person without authorization. +- A voice ID is bound to its target model. Later `speech synthesize` calls must use the same --model. +- The response includes a one-shot preview_audio (Base64). Querying the voice later may not return preview_audio again. Formal listen tests use `speech synthesize`. + +#### Examples + +```bash +bl speech voice design --prompt "Calm warm Chinese announcer" --preview-text "Welcome to today's program." --prefix demo --target-model cosyvoice-v3.5-plus --dry-run --output json +``` + +### `bl speech voice get` + +| Field | Value | +| ------------------ | ------------------------------------- | +| **Name** | `speech voice get` | +| **Description** | Get details of a custom voice | +| **Authentication** | API Key | +| **Usage** | `bl speech voice get --id ` | + +#### Flags + +| Flag | Type | Required | Description | +| ------------------ | ------ | -------- | ------------------- | +| `--id ` | string | yes | Voice ID (required) | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- Status DEPLOYING/OK/UNDEPLOYED is returned as data, not as a CLI failure. Use `speech voice preview` for the reference audio URL when present. + +#### Examples + +```bash +bl speech voice get --id --output json +``` + +```bash +bl speech voice get --id --quiet +``` + +### `bl speech voice list` + +| Field | Value | +| ------------------ | ----------------------------------------------------------------------- | +| **Name** | `speech voice list` | +| **Description** | List custom voices | +| **Authentication** | API Key | +| **Usage** | `bl speech voice list [--prefix ] [--page ] [--page-size ]` | + +#### Flags + +| Flag | Type | Required | Description | +| ------------------ | ------ | -------- | ----------------------------------------------------------------------- | +| `--prefix ` | string | no | Custom voice prefix. Digits and letters only, 1-10 characters | +| `--page ` | number | no | Page number, 1-based (default: 1). Mapped to API page_index as page - 1 | +| `--page-size ` | number | no | Results per page (default: 10) | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- Lists one page only (CLI defaults: page=1, page-size=10). Status values: DEPLOYING / OK / UNDEPLOYED. + +#### Examples + +```bash +bl speech voice list +``` + +```bash +bl speech voice list --prefix demo +``` + +```bash +bl speech voice list --page 2 --page-size 20 +``` + +### `bl speech voice preview` + +| Field | Value | +| ------------------ | -------------------------------------------------------- | +| **Name** | `speech voice preview` | +| **Description** | Fetch the reference audio URL for a cloned voice | +| **Authentication** | API Key | +| **Usage** | `bl speech voice preview --id [--out ]` | + +#### Flags + +| Flag | Type | Required | Description | +| ------------------ | ------ | -------- | ----------------------------------------- | +| `--id ` | string | yes | Voice ID (required) | +| `--out ` | string | no | Download the reference audio to this path | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- Returns resource_link from query_voice. This is the enrollment reference audio, not a synthesized preview. Use `speech synthesize` to hear the voice. Design voices may not include resource_link. + +#### Examples + +```bash +bl speech voice preview --id --output json +``` + +```bash +bl speech voice preview --id --out reference.wav +``` + +### `bl speech voice update` + +| Field | Value | +| ------------------ | ------------------------------------------------------------------------------------------------------- | +| **Name** | `speech voice update` | +| **Description** | Update the reference audio of a cloned voice | +| **Authentication** | API Key | +| **Usage** | `bl speech voice update --id --audio ` | +| **Risk** | `high` | +| **Risk message** | This updates the reference audio for the voice ID. All synthesis that uses this voice will be affected. | + +> **Agent safety:** Never add `--yes` automatically. On `type="requires_confirmation"`, stop and ask for explicit user confirmation of the same action and scope. + +#### Flags + +| Flag | Type | Required | Description | +| ----------------------- | ------ | -------- | ------------------------------------- | +| `--id ` | string | yes | Voice ID (required) | +| `--audio ` | string | yes | New reference audio URL or local file | +| `--yes` | switch | no | Confirm this high-risk operation | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- You must have the right to use the reference audio or described voice. Do not clone or imitate a real person without authorization. +- A voice ID is bound to its target model. Later `speech synthesize` calls must use the same --model. +- Only reference audio can be updated. Design voices do not support voice_prompt updates via this command. + +#### Examples + +```bash +bl speech voice update --id --audio ./new-reference.wav --dry-run --output json +``` + +```bash +# Only after explicit user confirmation: +bl speech voice update --id --audio ./new-reference.wav --yes +``` From a67236bd4095c6f22f7d5aea7935904180e8bcac Mon Sep 17 00:00:00 2001 From: clh02467605 Date: Fri, 20 Nov 2026 18:13:10 +0800 Subject: [PATCH 2/3] feat(translate): add text/image/omni Qwen-MT commands and shared chat streaming - Add `text translate`, `image translate`, and `omni translate` with dry-run, async wait, and skill/reference updates - Extract Chat Completions into `run-chat` and support snapshot vs delta SSE for qwen-mt-plus - Extend task-get projections (`image_url`, Success/Data) and reject empty MT messages locally - Cover empty input, flash/lite incremental stream, plus text-mode suffixes, and image CANCELED/UNKNOWN offline --- packages/cli/src/commands.ts | 6 + .../commands/src/commands/image/translate.ts | 439 ++++++++++++++ packages/commands/src/commands/omni/chat.ts | 7 + .../commands/src/commands/omni/translate.ts | 499 ++++++++++++++++ .../commands/shared/chat-completion-stream.ts | 41 +- packages/commands/src/commands/text/chat.ts | 186 ++---- .../commands/src/commands/text/run-chat.ts | 165 ++++++ .../commands/src/commands/text/translate.ts | 292 +++++++++ .../commands/src/commands/video/task-get.ts | 8 +- packages/commands/src/index.ts | 3 + .../tests/e2e/image-translate.e2e.test.ts | 498 ++++++++++++++++ .../tests/e2e/omni-translate.e2e.test.ts | 558 ++++++++++++++++++ .../tests/e2e/text-translation.e2e.test.ts | 518 ++++++++++++++++ packages/commands/tests/e2e/topic-routes.ts | 18 + .../shared/chat-completion-stream.test.ts | 48 ++ packages/core/src/client/endpoints.ts | 5 + packages/core/src/client/index.ts | 1 + packages/core/src/types/api.ts | 58 +- skills/bailian-cli/SKILL.md | 11 +- skills/bailian-cli/reference/index.md | 3 +- skills/bailian-cli/reference/text.md | 49 +- skills/bailian-gen/SKILL.md | 9 +- skills/bailian-gen/reference/image.md | 53 +- skills/bailian-gen/reference/index.md | 6 +- skills/bailian-gen/reference/omni.md | 49 +- 25 files changed, 3370 insertions(+), 160 deletions(-) create mode 100644 packages/commands/src/commands/image/translate.ts create mode 100644 packages/commands/src/commands/omni/translate.ts create mode 100644 packages/commands/src/commands/text/run-chat.ts create mode 100644 packages/commands/src/commands/text/translate.ts create mode 100644 packages/commands/tests/e2e/image-translate.e2e.test.ts create mode 100644 packages/commands/tests/e2e/omni-translate.e2e.test.ts create mode 100644 packages/commands/tests/e2e/text-translation.e2e.test.ts create mode 100644 packages/commands/tests/shared/chat-completion-stream.test.ts diff --git a/packages/cli/src/commands.ts b/packages/cli/src/commands.ts index 46ca22e11..0c9d78b5f 100644 --- a/packages/cli/src/commands.ts +++ b/packages/cli/src/commands.ts @@ -6,6 +6,7 @@ import { authLogout, authGenerateAccessToken, textChat, + textTranslate, batchConvert, batchCreate, batchList, @@ -13,8 +14,10 @@ import { batchCancel, batchDownload, textOmni, + omniTranslate, imageGenerate, imageEdit, + imageTranslate, videoGenerate, videoEdit, videoRef, @@ -369,6 +372,7 @@ export const commands: Record = { "auth logout": authLogout, "auth generate-access-token": authGenerateAccessToken, "text chat": textChat, + "text translate": textTranslate, "batch convert": batchConvert, "batch create": batchCreate, "batch list": batchList, @@ -376,8 +380,10 @@ export const commands: Record = { "batch cancel": batchCancel, "batch download": batchDownload, omni: textOmni, + "omni translate": omniTranslate, "image generate": imageGenerate, "image edit": imageEdit, + "image translate": imageTranslate, "video generate": videoGenerate, "video edit": videoEdit, "video ref": videoRef, diff --git a/packages/commands/src/commands/image/translate.ts b/packages/commands/src/commands/image/translate.ts new file mode 100644 index 000000000..403a7ac1c --- /dev/null +++ b/packages/commands/src/commands/image/translate.ts @@ -0,0 +1,439 @@ +import { + defineCommand, + taskPath, + image2ImagePath, + ASYNC_FLAG, + BailianError, + ExitCode, + resolveOutputDir, + stripUndefined, + type DashScopeAsyncResponse, + type DashScopeTaskResponse, + type FlagsDef, + type ImageTranslationRequest, + type LocalizedText, +} from "bailian-cli-core"; +import { downloadFile, poll } from "bailian-cli-runtime"; +import { existsSync, statSync } from "fs"; +import { join } from "path"; +import { randomBytes } from "crypto"; + +const DEFAULT_MODEL = "qwen-mt-image"; +const SUPPORTED_MODELS = new Set(["qwen-mt-image", "qwen-mt-image-2.0"]); + +const TRANSLATE_FLAGS = { + image: { + type: "string", + valueHint: "", + description: { + "en-US": "Image URL or local file", + "zh-CN": "图片 URL 或本地文件", + }, + required: true, + }, + sourceLang: { + type: "string", + valueHint: "", + description: { + "en-US": "Source language name or code (default: auto)", + "zh-CN": "源语言名称或编码(默认:auto)", + }, + }, + targetLang: { + type: "string", + valueHint: "", + description: { + "en-US": "Target language name or code", + "zh-CN": "目标语言名称或编码", + }, + required: true, + }, + model: { + type: "string", + valueHint: "", + description: { + "en-US": "Model ID (default: qwen-mt-image; optional: qwen-mt-image-2.0)", + "zh-CN": "模型 ID(默认:qwen-mt-image;可选:qwen-mt-image-2.0)", + }, + }, + domainHint: { + type: "string", + valueHint: "", + description: { + "en-US": "English domain hint (maps to ext.domainHint)", + "zh-CN": "英文领域提示(映射为 ext.domainHint)", + }, + }, + term: { + type: "array", + valueHint: "", + description: { + "en-US": "Terminology pair source=target (repeatable; maps to terminologies)", + "zh-CN": "术语对 source=target(可重复;映射为 terminologies)", + }, + }, + imageSegment: { + type: "boolean", + valueHint: "", + description: { + "en-US": "Skip translating text on image subjects (ext.config.imageSegment)", + "zh-CN": "跳过图片主体上的文字翻译(ext.config.imageSegment)", + }, + }, + outDir: { + type: "string", + valueHint: "", + description: { "en-US": "Download directory", "zh-CN": "下载目录" }, + }, + ...ASYNC_FLAG, + pollInterval: { + type: "number", + valueHint: "", + description: { + "en-US": "Polling interval when waiting (default: 5)", + "zh-CN": "等待任务时的轮询间隔(默认:5 秒)", + }, + }, +} satisfies FlagsDef; + +function isHttpUrl(value: string): boolean { + return /^https?:\/\//i.test(value); +} + +function hasNonAscii(value: string): boolean { + for (const char of value) { + if ((char.codePointAt(0) ?? 0) > 0x7f) return true; + } + return false; +} + +function isChineseOrEnglish(lang: string): boolean { + const normalized = lang.trim().toLowerCase(); + return ( + normalized === "zh" || + normalized === "zh-cn" || + normalized === "zh-tw" || + normalized === "chinese" || + normalized === "en" || + normalized === "en-us" || + normalized === "en-gb" || + normalized === "english" + ); +} + +function parseImageTerms( + terms: string[] | undefined, + localize: (text: LocalizedText) => string, +): Array<{ src: string; tgt: string }> | undefined { + if (terms === undefined) return undefined; + const parsed: Array<{ src: string; tgt: string }> = []; + for (const termValue of terms) { + const equalsIndex = termValue.indexOf("="); + if (equalsIndex <= 0 || equalsIndex === termValue.length - 1) { + throw new BailianError( + localize({ + "en-US": `Invalid --term "${termValue}". Use source=target with non-empty sides.`, + "zh-CN": `无效的 --term "${termValue}"。请使用 source=target,且两侧均非空。`, + }), + ExitCode.USAGE, + ); + } + parsed.push({ + src: termValue.slice(0, equalsIndex), + tgt: termValue.slice(equalsIndex + 1), + }); + } + return parsed; +} + +function assertImageInput(image: string, localize: (text: LocalizedText) => string): void { + if (!image) { + throw new BailianError( + localize({ + "en-US": "--image must not be empty.", + "zh-CN": "--image 不能为空。", + }), + ExitCode.USAGE, + ); + } + if (isHttpUrl(image)) { + if (hasNonAscii(image)) { + throw new BailianError( + localize({ + "en-US": "Image URL must not contain non-ASCII characters.", + "zh-CN": "图片 URL 不能包含非 ASCII 字符。", + }), + ExitCode.USAGE, + ); + } + return; + } + if (!existsSync(image)) { + throw new BailianError( + localize({ + "en-US": `Image file not found: ${image}`, + "zh-CN": `图片文件不存在:${image}`, + }), + ExitCode.USAGE, + ); + } + if (!statSync(image).isFile()) { + throw new BailianError( + localize({ + "en-US": `Not a file: ${image}`, + "zh-CN": `不是文件:${image}`, + }), + ExitCode.USAGE, + ); + } +} + +function assertLanguagePair( + model: string, + sourceLang: string, + targetLang: string, + localize: (text: LocalizedText) => string, +): void { + if (!targetLang) { + throw new BailianError( + localize({ + "en-US": "--target-lang must not be empty.", + "zh-CN": "--target-lang 不能为空。", + }), + ExitCode.USAGE, + ); + } + if (targetLang.toLowerCase() === "auto") { + throw new BailianError( + localize({ + "en-US": "--target-lang cannot be auto.", + "zh-CN": "--target-lang 不能为 auto。", + }), + ExitCode.USAGE, + ); + } + if (sourceLang.toLowerCase() === targetLang.toLowerCase()) { + throw new BailianError( + localize({ + "en-US": "--source-lang and --target-lang must differ.", + "zh-CN": "--source-lang 与 --target-lang 不能相同。", + }), + ExitCode.USAGE, + ); + } + // Legacy model requires Chinese or English on at least one side; auto is allowed. + if (model === "qwen-mt-image" && sourceLang.toLowerCase() !== "auto") { + if (!isChineseOrEnglish(sourceLang) && !isChineseOrEnglish(targetLang)) { + throw new BailianError( + localize({ + "en-US": + "qwen-mt-image requires Chinese or English on at least one side of the language pair.", + "zh-CN": "qwen-mt-image 要求源/目标语言至少一侧为中文或英文。", + }), + ExitCode.USAGE, + ); + } + } +} + +export default defineCommand({ + description: { + "en-US": "Translate text in an image (Qwen-MT-Image)", + "zh-CN": "翻译图片中的文字(Qwen-MT-Image)", + }, + auth: "apiKey", + usageArgs: "--image --target-lang [flags]", + flags: TRANSLATE_FLAGS, + exampleArgs: [ + { + "en-US": "--image ./poster.png --target-lang en --dry-run --output json", + "zh-CN": "--image ./poster.png --target-lang en --dry-run --output json", + }, + { + "en-US": + "--image https://example.com/poster.png --source-lang zh --target-lang en --async --output json", + "zh-CN": + "--image https://example.com/poster.png --source-lang zh --target-lang en --async --output json", + }, + { + "en-US": + '--image https://example.com/poster.png --target-lang en --term "百炼=Bailian" --domain-hint "ecommerce"', + "zh-CN": + '--image https://example.com/poster.png --target-lang en --term "百炼=Bailian" --domain-hint "ecommerce"', + }, + ], + notes: [ + { + "en-US": + "Always submits asynchronously. Use --async to return the task ID immediately, or wait and download the translated JPG. Query with `video task get --task-id`. Local files upload to temporary OSS (requires OssResourceResolve).", + "zh-CN": + "始终异步提交。使用 --async 立即返回任务 ID,或等待完成后下载译后 JPG。可用 `video task get --task-id` 查询。本地文件会上传临时 OSS(需 OssResourceResolve)。", + }, + ], + validate: (flags) => { + if ( + flags.pollInterval !== undefined && + (!(flags.pollInterval > 0) || !Number.isFinite(flags.pollInterval)) + ) { + return { + "en-US": "--poll-interval must be a positive number.", + "zh-CN": "--poll-interval 必须为正数。", + }; + } + return undefined; + }, + async run(ctx) { + const { settings, flags } = ctx; + const localize = ctx.localize; + const image = flags.image; + const model = flags.model || DEFAULT_MODEL; + const sourceLang = + flags.sourceLang === undefined || flags.sourceLang === "" ? "auto" : flags.sourceLang; + const targetLang = flags.targetLang; + + if (!SUPPORTED_MODELS.has(model)) { + throw new BailianError( + localize({ + "en-US": `Unsupported model "${model}". Use qwen-mt-image or qwen-mt-image-2.0.`, + "zh-CN": `不支持的模型 "${model}"。请使用 qwen-mt-image 或 qwen-mt-image-2.0。`, + }), + ExitCode.USAGE, + ); + } + + assertImageInput(image, localize); + assertLanguagePair(model, sourceLang, targetLang, localize); + + const localInput = !isHttpUrl(image); + const previewImageUrl = localInput ? `` : image; + + const ext: NonNullable = {}; + if (flags.domainHint !== undefined) ext.domainHint = flags.domainHint; + const terminologies = parseImageTerms(flags.term, localize); + if (terminologies !== undefined) ext.terminologies = terminologies; + if (flags.imageSegment !== undefined) { + ext.config = { imageSegment: flags.imageSegment }; + } + + const request: ImageTranslationRequest = { + model, + input: stripUndefined({ + image_url: previewImageUrl, + source_lang: sourceLang, + target_lang: targetLang, + ext: Object.keys(ext).length > 0 ? ext : undefined, + }) as ImageTranslationRequest["input"], + }; + + const endpoint = image2ImagePath(); + + if (settings.dryRun) { + ctx.output.result({ endpoint, request }); + return; + } + + const imageUrl = localInput ? await ctx.client.uploadFile(image, model) : image; + const liveRequest: ImageTranslationRequest = { + ...request, + input: { ...request.input, image_url: imageUrl }, + }; + + const submitted = await ctx.client.requestJson({ + path: endpoint, + method: "POST", + body: liveRequest, + async: true, + }); + + const taskId = submitted.output.task_id; + if (flags.async) { + ctx.output.result({ task_id: taskId, request_id: submitted.request_id }); + return; + } + + const pollInterval = flags.pollInterval ?? 5; + const taskResponse = await poll(ctx.client, { + output: ctx.output, + url: ctx.client.url(taskPath(taskId)), + intervalSec: pollInterval, + timeoutSec: settings.timeout, + load: async () => { + const data = await ctx.client.requestJson({ + path: taskPath(taskId), + }); + const status = data.output.task_status; + if (status === "FAILED") { + const message = data.output.message || data.output.code || "Task failed."; + throw new BailianError(message, ExitCode.GENERAL, undefined, { + api: { + apiCode: data.output.code, + requestId: data.request_id, + }, + }); + } + if (status === "CANCELED" || status === "UNKNOWN") { + throw new BailianError( + localize({ + "en-US": `Task ended with status ${status}.`, + "zh-CN": `任务以状态 ${status} 结束。`, + }), + ExitCode.GENERAL, + undefined, + { + api: { + apiCode: data.output.code, + requestId: data.request_id, + }, + }, + ); + } + return data; + }, + isComplete: (data) => (data as DashScopeTaskResponse).output.task_status === "SUCCEEDED", + isFailed: () => false, + getStatus: (data) => (data as DashScopeTaskResponse).output.task_status, + }); + + const imageResultUrl = taskResponse.output.image_url; + if (!imageResultUrl) { + throw new BailianError( + localize({ + "en-US": "Task succeeded but output.image_url is missing.", + "zh-CN": "任务成功但缺少 output.image_url。", + }), + ExitCode.GENERAL, + ); + } + + const outDir = resolveOutputDir(settings, { + flagDir: flags.outDir, + subDir: flags.outDir ? undefined : "images", + }); + const destPath = join(outDir, `image-translate-${randomBytes(6).toString("hex")}.jpg`); + await downloadFile(imageResultUrl, destPath, { + quiet: settings.quiet || settings.output === "json", + }); + + const serverMessage = taskResponse.output.message; + const result = { + ...taskResponse, + task_id: taskId, + image_url: imageResultUrl, + saved: destPath, + }; + + let text: string | undefined; + if (settings.output === "text") { + text = [ + `image_url: ${imageResultUrl}`, + `saved: ${destPath}`, + serverMessage ? `message: ${serverMessage}` : undefined, + taskResponse.request_id ? `request_id: ${taskResponse.request_id}` : undefined, + ] + .filter((line): line is string => line !== undefined) + .join("\n"); + } + + ctx.output.result(result, text !== undefined ? { text } : undefined); + }, +}); diff --git a/packages/commands/src/commands/omni/chat.ts b/packages/commands/src/commands/omni/chat.ts index e311829d7..6a5d48cfb 100644 --- a/packages/commands/src/commands/omni/chat.ts +++ b/packages/commands/src/commands/omni/chat.ts @@ -228,6 +228,13 @@ export default defineCommand({ }, }, }, + notes: [ + { + "en-US": + "For file translation (documents / images / audio), use `omni translate` instead of this multimodal chat entry.", + "zh-CN": "如需翻译文档/图片/音频文件,请使用 `omni translate`,而不是本多模态对话入口。", + }, + ], exampleArgs: [ "--list-voices", { diff --git a/packages/commands/src/commands/omni/translate.ts b/packages/commands/src/commands/omni/translate.ts new file mode 100644 index 000000000..e7b408c81 --- /dev/null +++ b/packages/commands/src/commands/omni/translate.ts @@ -0,0 +1,499 @@ +import { + defineCommand, + multimodalGenerationPath, + taskPath, + ASYNC_FLAG, + BailianError, + ExitCode, + resolveOutputDir, + stripUndefined, + type DashScopeAsyncResponse, + type DashScopeTaskResponse, + type FlagsDef, + type LocalizedText, + type QwenMtUniRequest, +} from "bailian-cli-core"; +import { downloadFile, poll } from "bailian-cli-runtime"; +import { existsSync, statSync } from "fs"; +import { extname, join } from "path"; +import { randomBytes } from "crypto"; + +const DEFAULT_MODEL = "qwen-mt-uni"; +const MAX_FILE_BYTES = 100 * 1024 * 1024; + +const SUPPORTED_EXTENSIONS = new Set([ + "pdf", + "docx", + "pptx", + "xlsx", + "txt", + "html", + "htm", + "md", + "markdown", + "mdown", + "mkd", + "png", + "jpg", + "jpeg", + "mp3", + "wav", +]); + +const REJECTED_EXTENSIONS = new Set(["doc", "ppt", "mp4", "mov", "avi", "mkv", "webm"]); + +const OUTPUT_EXTENSION_BY_INPUT: Record = { + pdf: "pdf", + docx: "docx", + pptx: "pptx", + xlsx: "xlsx", + txt: "txt", + html: "html", + htm: "html", + md: "md", + markdown: "md", + mdown: "md", + mkd: "md", + png: "jpg", + jpg: "jpg", + jpeg: "jpg", + mp3: "mp3", + wav: "mp3", +}; + +const OUTPUT_EXTENSION_BY_FILE_TYPE: Record = { + pdf: "pdf", + document: "docx", + docx: "docx", + pptx: "pptx", + xlsx: "xlsx", + txt: "txt", + html: "html", + md: "md", + markdown: "md", + image: "jpg", + jpg: "jpg", + jpeg: "jpg", + png: "jpg", + audio: "mp3", + mp3: "mp3", + wav: "mp3", +}; + +const TRANSLATE_FLAGS = { + file: { + type: "string", + valueHint: "", + description: { + "en-US": "Document, image or audio URL/local file", + "zh-CN": "文档、图片或音频 URL/本地文件", + }, + required: true, + }, + targetLang: { + type: "string", + valueHint: "", + description: { + "en-US": "Target language code or name", + "zh-CN": "目标语言代码或名称", + }, + required: true, + }, + sourceLang: { + type: "string", + valueHint: "", + description: { + "en-US": "Source language (omit to auto-detect; do not send auto)", + "zh-CN": "源语言(省略则自动检测;不发送 auto)", + }, + }, + formatHint: { + type: "string", + valueHint: "", + description: { + "en-US": "Format hint when URL has no extension (e.g. pdf, image)", + "zh-CN": "URL 无后缀时的格式提示(如 pdf、image)", + }, + }, + domainHint: { + type: "string", + valueHint: "", + description: { + "en-US": "English domain hint ≤200 words (ext.domainHint)", + "zh-CN": "英文领域提示,不超过 200 词(ext.domainHint)", + }, + }, + outDir: { + type: "string", + valueHint: "", + description: { "en-US": "Download directory", "zh-CN": "下载目录" }, + }, + ...ASYNC_FLAG, + pollInterval: { + type: "number", + valueHint: "", + description: { + "en-US": "Polling interval when waiting (default: 5)", + "zh-CN": "等待任务时的轮询间隔(默认:5 秒)", + }, + }, +} satisfies FlagsDef; + +function isHttpUrl(value: string): boolean { + return /^https?:\/\//i.test(value); +} + +function hasNonAscii(value: string): boolean { + for (const char of value) { + if ((char.codePointAt(0) ?? 0) > 0x7f) return true; + } + return false; +} + +function pathExtension(source: string): string { + const pathPart = source.split("?")[0]?.split("#")[0] ?? source; + return extname(pathPart).slice(1).toLowerCase(); +} + +function assertFileInput( + file: string, + formatHint: string | undefined, + localize: (text: LocalizedText) => string, +): { local: boolean; extension: string | undefined } { + if (!file) { + throw new BailianError( + localize({ + "en-US": "--file must not be empty.", + "zh-CN": "--file 不能为空。", + }), + ExitCode.USAGE, + ); + } + + if (isHttpUrl(file)) { + if (hasNonAscii(file)) { + throw new BailianError( + localize({ + "en-US": "File URL must not contain non-ASCII characters.", + "zh-CN": "文件 URL 不能包含非 ASCII 字符。", + }), + ExitCode.USAGE, + ); + } + const extension = pathExtension(file); + if (REJECTED_EXTENSIONS.has(extension)) { + throw new BailianError( + localize({ + "en-US": `Unsupported file type ".${extension}". Convert Office legacy formats to docx/pptx; video is not supported.`, + "zh-CN": `不支持的文件类型 ".${extension}"。请将旧版 Office 转为 docx/pptx;不支持视频。`, + }), + ExitCode.USAGE, + ); + } + if (!extension && !formatHint) { + throw new BailianError( + localize({ + "en-US": "URL has no recognizable extension. Pass --format-hint (e.g. pdf, image, mp3).", + "zh-CN": "URL 无可识别后缀。请传 --format-hint(如 pdf、image、mp3)。", + }), + ExitCode.USAGE, + ); + } + if (extension && !SUPPORTED_EXTENSIONS.has(extension) && !formatHint) { + throw new BailianError( + localize({ + "en-US": `Unsupported file type ".${extension}".`, + "zh-CN": `不支持的文件类型 ".${extension}"。`, + }), + ExitCode.USAGE, + ); + } + return { local: false, extension: extension || undefined }; + } + + if (!existsSync(file)) { + throw new BailianError( + localize({ + "en-US": `File not found: ${file}`, + "zh-CN": `文件不存在:${file}`, + }), + ExitCode.USAGE, + ); + } + const stats = statSync(file); + if (!stats.isFile()) { + throw new BailianError( + localize({ + "en-US": `Not a file: ${file}`, + "zh-CN": `不是文件:${file}`, + }), + ExitCode.USAGE, + ); + } + if (stats.size > MAX_FILE_BYTES) { + throw new BailianError( + localize({ + "en-US": `File exceeds 100 MB: ${file}`, + "zh-CN": `文件超过 100 MB:${file}`, + }), + ExitCode.USAGE, + ); + } + + const extension = pathExtension(file); + if (REJECTED_EXTENSIONS.has(extension)) { + throw new BailianError( + localize({ + "en-US": `Unsupported file type ".${extension}". Convert Office legacy formats to docx/pptx; video is not supported.`, + "zh-CN": `不支持的文件类型 ".${extension}"。请将旧版 Office 转为 docx/pptx;不支持视频。`, + }), + ExitCode.USAGE, + ); + } + if (!extension && !formatHint) { + throw new BailianError( + localize({ + "en-US": "Local file has no extension. Pass --format-hint.", + "zh-CN": "本地文件无后缀。请传 --format-hint。", + }), + ExitCode.USAGE, + ); + } + if (extension && !SUPPORTED_EXTENSIONS.has(extension) && !formatHint) { + throw new BailianError( + localize({ + "en-US": `Unsupported file type ".${extension}".`, + "zh-CN": `不支持的文件类型 ".${extension}"。`, + }), + ExitCode.USAGE, + ); + } + return { local: true, extension: extension || undefined }; +} + +function resolveOutputExtension( + fileType: string | undefined, + inputExtension: string | undefined, + formatHint: string | undefined, +): string { + if (fileType) { + const mapped = OUTPUT_EXTENSION_BY_FILE_TYPE[fileType.toLowerCase()]; + if (mapped) return mapped; + } + if (inputExtension) { + const mapped = OUTPUT_EXTENSION_BY_INPUT[inputExtension]; + if (mapped) return mapped; + } + if (formatHint) { + const mapped = + OUTPUT_EXTENSION_BY_INPUT[formatHint.toLowerCase()] || + OUTPUT_EXTENSION_BY_FILE_TYPE[formatHint.toLowerCase()]; + if (mapped) return mapped; + } + return "bin"; +} + +export default defineCommand({ + description: { + "en-US": "Translate a document, image or audio file (Qwen-MT-Uni)", + "zh-CN": "翻译文档、图片或音频文件(Qwen-MT-Uni)", + }, + auth: "apiKey", + usageArgs: "--file --target-lang [flags]", + flags: TRANSLATE_FLAGS, + exampleArgs: [ + { + "en-US": "--file ./brochure.pdf --target-lang en --dry-run --output json", + "zh-CN": "--file ./brochure.pdf --target-lang en --dry-run --output json", + }, + { + "en-US": + "--file https://example.com/brochure.pdf --target-lang en --format-hint pdf --async --output json", + "zh-CN": + "--file https://example.com/brochure.pdf --target-lang en --format-hint pdf --async --output json", + }, + ], + notes: [ + { + "en-US": + "Always submits asynchronously. Use --async to return the task ID immediately. Task SUCCEEDED still requires output.Success=true before download. Query with `video task get --task-id`. Supported: pdf/docx/pptx/xlsx/txt/html/md/png/jpg/mp3/wav. Video and legacy doc/ppt are rejected.", + "zh-CN": + "始终异步提交。使用 --async 立即返回任务 ID。任务 SUCCEEDED 仍需 output.Success=true 才会下载。可用 `video task get --task-id` 查询。支持:pdf/docx/pptx/xlsx/txt/html/md/png/jpg/mp3/wav。拒绝视频与旧版 doc/ppt。", + }, + ], + validate: (flags) => { + if ( + flags.pollInterval !== undefined && + (!(flags.pollInterval > 0) || !Number.isFinite(flags.pollInterval)) + ) { + return { + "en-US": "--poll-interval must be a positive number.", + "zh-CN": "--poll-interval 必须为正数。", + }; + } + return undefined; + }, + async run(ctx) { + const { settings, flags } = ctx; + const localize = ctx.localize; + const file = flags.file; + const targetLang = flags.targetLang; + const model = DEFAULT_MODEL; + + if (!targetLang) { + throw new BailianError( + localize({ + "en-US": "--target-lang must not be empty.", + "zh-CN": "--target-lang 不能为空。", + }), + ExitCode.USAGE, + ); + } + + const inputInfo = assertFileInput(file, flags.formatHint, localize); + + const previewFileUrl = inputInfo.local ? `` : file; + const ext: NonNullable = {}; + if (flags.formatHint !== undefined) ext.format_hint = flags.formatHint; + if (flags.domainHint !== undefined) ext.domainHint = flags.domainHint; + + const requestInput: QwenMtUniRequest["input"] = stripUndefined({ + fileUrl: previewFileUrl, + target_lang: targetLang, + source_lang: + flags.sourceLang === undefined || flags.sourceLang === "" ? undefined : flags.sourceLang, + ext: Object.keys(ext).length > 0 ? ext : undefined, + }) as QwenMtUniRequest["input"]; + + const request: QwenMtUniRequest = { model, input: requestInput }; + const endpoint = multimodalGenerationPath(); + + if (settings.dryRun) { + ctx.output.result({ endpoint, request }); + return; + } + + const fileUrl = inputInfo.local ? await ctx.client.uploadFile(file, model) : file; + const liveRequest: QwenMtUniRequest = { + ...request, + input: { ...request.input, fileUrl }, + }; + + const submitted = await ctx.client.requestJson({ + path: endpoint, + method: "POST", + body: liveRequest, + async: true, + }); + + const taskId = submitted.output.task_id; + if (flags.async) { + ctx.output.result({ task_id: taskId, request_id: submitted.request_id }); + return; + } + + const pollInterval = flags.pollInterval ?? 5; + const taskResponse = await poll(ctx.client, { + output: ctx.output, + url: ctx.client.url(taskPath(taskId)), + intervalSec: pollInterval, + timeoutSec: settings.timeout, + load: async () => { + const data = await ctx.client.requestJson({ + path: taskPath(taskId), + }); + const status = data.output.task_status; + if (status === "FAILED") { + const message = + data.output.Message || data.output.message || data.output.code || "Task failed."; + throw new BailianError(String(message), ExitCode.GENERAL, undefined, { + api: { + apiCode: data.output.Code !== undefined ? String(data.output.Code) : data.output.code, + requestId: data.output.RequestId || data.request_id, + }, + }); + } + if (status === "CANCELED" || status === "UNKNOWN") { + throw new BailianError( + localize({ + "en-US": `Task ended with status ${status}.`, + "zh-CN": `任务以状态 ${status} 结束。`, + }), + ExitCode.GENERAL, + undefined, + { + api: { + apiCode: + data.output.Code !== undefined ? String(data.output.Code) : data.output.code, + requestId: data.output.RequestId || data.request_id, + }, + }, + ); + } + // SUCCEEDED is terminal: missing / non-true Success is a business failure, not "still running". + if (status === "SUCCEEDED" && data.output.Success !== true) { + const message = data.output.Message || "Translation business call failed."; + throw new BailianError(String(message), ExitCode.GENERAL, undefined, { + api: { + apiCode: data.output.Code !== undefined ? String(data.output.Code) : undefined, + requestId: data.output.RequestId || data.request_id, + }, + }); + } + return data; + }, + isComplete: (data) => (data as DashScopeTaskResponse).output.task_status === "SUCCEEDED", + isFailed: () => false, + getStatus: (data) => (data as DashScopeTaskResponse).output.task_status, + }); + + const translatedUrl = taskResponse.output.Data?.TranslatedFileUrl; + if (!translatedUrl) { + throw new BailianError( + localize({ + "en-US": "Task succeeded but output.Data.TranslatedFileUrl is missing.", + "zh-CN": "任务成功但缺少 output.Data.TranslatedFileUrl。", + }), + ExitCode.GENERAL, + ); + } + + const outDir = resolveOutputDir(settings, { + flagDir: flags.outDir, + subDir: flags.outDir ? undefined : "translations", + }); + const extension = resolveOutputExtension( + taskResponse.output.Data?.FileType, + inputInfo.extension, + flags.formatHint, + ); + // Safe local basename only — never trust remote URL path segments. + const destPath = join(outDir, `omni-translate-${randomBytes(6).toString("hex")}.${extension}`); + + await downloadFile(translatedUrl, destPath, { + quiet: settings.quiet || settings.output === "json", + }); + + const result = { + ...taskResponse, + task_id: taskId, + translated_file_url: translatedUrl, + saved: destPath, + }; + + let text: string | undefined; + if (settings.output === "text") { + const fileType = taskResponse.output.Data?.FileType; + text = [ + `translated_file_url: ${translatedUrl}`, + `saved: ${destPath}`, + fileType ? `file_type: ${fileType}` : undefined, + taskResponse.request_id ? `request_id: ${taskResponse.request_id}` : undefined, + ] + .filter((line): line is string => line !== undefined) + .join("\n"); + } + + ctx.output.result(result, text !== undefined ? { text } : undefined); + }, +}); diff --git a/packages/commands/src/commands/shared/chat-completion-stream.ts b/packages/commands/src/commands/shared/chat-completion-stream.ts index 16178bab0..aca1243fd 100644 --- a/packages/commands/src/commands/shared/chat-completion-stream.ts +++ b/packages/commands/src/commands/shared/chat-completion-stream.ts @@ -18,6 +18,13 @@ export interface ChatStreamResult { finish_reason: string | null; } +export type ChatStreamContentMode = "delta" | "snapshot"; + +export interface ChatCompletionStreamOptions { + /** How content fragments accumulate. Default: delta (append). Snapshot is for qwen-mt-plus. */ + contentMode?: ChatStreamContentMode; +} + interface PendingToolCall { id: string; name: string; @@ -31,11 +38,15 @@ function asRecord(value: unknown): Record | undefined { } /** Aggregate Chat Completions SSE deltas with completion and tool-call checks. */ -export function createChatCompletionStreamAccumulator(localize: (text: LocalizedText) => string): { +export function createChatCompletionStreamAccumulator( + localize: (text: LocalizedText) => string, + options?: ChatCompletionStreamOptions, +): { readonly completed: boolean; accept(event: { event?: string; data: string }): ChatStreamUpdate; finish(): ChatStreamResult; } { + const contentMode = options?.contentMode ?? "delta"; let completed = false; let content = ""; let finishReason: string | null = null; @@ -59,6 +70,16 @@ export function createChatCompletionStreamAccumulator(localize: (text: Localized ExitCode.GENERAL, ); + const snapshotProtocolError = () => + new BailianError( + localize({ + "en-US": + "Stream protocol error: snapshot content is not an extension of previously emitted text.", + "zh-CN": "流协议错误:累计内容不是此前已输出文本的扩展。", + }), + ExitCode.GENERAL, + ); + const acceptToolCallDelta = (fragment: unknown): void => { const delta = asRecord(fragment); if (!delta) throw missingIndexError(); @@ -81,6 +102,21 @@ export function createChatCompletionStreamAccumulator(localize: (text: Localized toolCalls.set(index, entry); }; + const applyContentFragment = (fragment: string): string => { + if (!fragment) return ""; + if (contentMode === "delta") { + content += fragment; + return fragment; + } + + // Snapshot: each fragment is the full text so far; emit only the new suffix. + if (fragment === content) return ""; + if (!fragment.startsWith(content)) throw snapshotProtocolError(); + const suffix = fragment.slice(content.length); + content = fragment; + return suffix; + }; + return { get completed() { return completed; @@ -109,8 +145,7 @@ export function createChatCompletionStreamAccumulator(localize: (text: Localized reasoningDelta += delta.reasoning_content; } if (typeof delta.content === "string" && delta.content) { - contentDelta += delta.content; - content += delta.content; + contentDelta += applyContentFragment(delta.content); } if (Array.isArray(delta.tool_calls)) { for (const fragment of delta.tool_calls) acceptToolCallDelta(fragment); diff --git a/packages/commands/src/commands/text/chat.ts b/packages/commands/src/commands/text/chat.ts index fc52ac205..cda5cb32f 100644 --- a/packages/commands/src/commands/text/chat.ts +++ b/packages/commands/src/commands/text/chat.ts @@ -1,4 +1,4 @@ -import { createChatCompletionStreamAccumulator } from "../shared/chat-completion-stream.ts"; +import { buildChatMessages, parseTextMessages, runChatCompletions } from "./run-chat.ts"; import { defineCommand, resolveBatchChatEndpoint, @@ -6,17 +6,12 @@ import { chatPath, responsesPath, parseSSE, - readTextFromPathOrStdin, - type ChatMessage, type ChatRequest, - type ChatResponse, type ResponsesRequest, type ResponsesResponse, type ResponsesStreamEvent, type FlagsDef, - type ParsedFlags, } from "bailian-cli-core"; -import { ansi } from "bailian-cli-runtime"; import { readFileSync } from "fs"; import { assertResponsesStreamCompleted, @@ -133,55 +128,6 @@ const CHAT_FLAGS = { }, ...PRIME_FLAGS, } satisfies FlagsDef; -type ChatFlags = ParsedFlags; - -interface ParsedMessages { - system?: string; - messages: ChatMessage[]; -} - -function parseMessages(flags: ChatFlags): ParsedMessages { - const messages: ChatMessage[] = []; - let system: string | undefined; - - if (flags.system) { - system = flags.system; - } - - if (flags.messagesFile) { - const raw = readTextFromPathOrStdin(flags.messagesFile); - const parsed = JSON.parse(raw) as Array<{ role: string; content: string }>; - for (const parsedMessage of parsed) { - if (parsedMessage.role === "system") { - system = typeof parsedMessage.content === "string" ? parsedMessage.content : ""; - } else { - messages.push(parsedMessage as ChatMessage); - } - } - } - - if (flags.message) { - const validRoles = new Set(["system", "user", "assistant"]); - const messageValues = flags.message; - for (const messageValue of messageValues) { - const colonIndex = messageValue.indexOf(":"); - const maybeRole = colonIndex !== -1 ? messageValue.slice(0, colonIndex) : ""; - - if (validRoles.has(maybeRole)) { - const content = messageValue.slice(colonIndex + 1); - if (maybeRole === "system") { - system = content; - } else { - messages.push({ role: maybeRole as "user" | "assistant", content }); - } - } else { - messages.push({ role: "user", content: messageValue }); - } - } - } - - return { system, messages }; -} export default defineCommand({ description: { @@ -258,6 +204,11 @@ export default defineCommand({ "zh-CN": '--enable-search 映射为 Chat Completions 的 enable_search 参数,需要模型支持内置联网搜索;白名单模型(qwen3.8-max)默认开启,可传 --enable-search false 显式关闭;Responses API 请改用 --tool \'{"type":"web_search"}\'。', }, + { + "en-US": + "For dedicated Qwen-MT text translation, use `text translate` instead of this chat entry.", + "zh-CN": "如需 Qwen-MT 专用文本翻译,请使用 `text translate`,而不是本对话入口。", + }, ], validate: (flags) => { if (!flags.message && !flags.messagesFile) return "Provide --message or --messages-file."; @@ -287,19 +238,13 @@ export default defineCommand({ "Batch Chat --timeout must be 60–3600 seconds.", "Batch Chat --timeout 必须为 60–3600 秒。", ); - const { system, messages } = parseMessages(flags); + + const allMessages = buildChatMessages(parseTextMessages(flags)); const api = flags.api ?? "chat"; const model = flags.model || settings.defaultTextModel || "qwen3.8-max"; const shouldStream = flags.batch ? false : flags.stream || process.stdout.isTTY; const format = settings.output; - // Build messages array with system prompt - const allMessages: ChatMessage[] = []; - if (system) { - allMessages.push({ role: "system", content: system }); - } - allMessages.push(...messages); - let body: ChatRequest | ResponsesRequest; if (api === "responses") { body = { @@ -336,7 +281,6 @@ export default defineCommand({ if (flags.enableSearch !== undefined) { body.enable_search = flags.enableSearch; } else if (SEARCH_BY_DEFAULT_MODELS.has(model)) { - // Whitelisted models get built-in web search unless explicitly opted out. body.enable_search = true; } } @@ -361,6 +305,26 @@ export default defineCommand({ body.tools = tools; } + if (api === "chat" && "messages" in body) { + const dryRunResult = flags.batch + ? { endpoint: requestPath, request: body, timeout: settings.timeout } + : flags.prime + ? { endpoint: requestPath, request: body } + : { request: body }; + + await runChatCompletions({ + client: ctx.client, + output: ctx.output, + localize: ctx.localize, + settings, + path: requestPath, + body, + shouldStream, + dryRunResult, + }); + return; + } + if (settings.dryRun) { ctx.output.result( flags.batch @@ -381,88 +345,40 @@ export default defineCommand({ }); let textContent = ""; - let inThinking = false; let responsesCompleted = false; - const accumulator = createChatCompletionStreamAccumulator(ctx.localize); const writesStreamingStdout = format === "text"; - const isTTY = process.stdout.isTTY; - const statusOut = - format === "json" ? process.stderr : isTTY ? process.stdout : process.stderr; const resultOut = process.stdout; - const statusColor = ansi(statusOut); for await (const event of parseSSE(responseStream)) { - if (api === "responses") { - if (event.data === "[DONE]") break; - let parsedEvent: ResponsesStreamEvent; - try { - parsedEvent = JSON.parse(event.data) as ResponsesStreamEvent; - } catch { - continue; - } - - const update = inspectResponsesStreamEvent(parsedEvent); - if (update.delta) { - textContent += update.delta; - if (writesStreamingStdout) resultOut.write(update.delta); - } - if (update.completed) { - responsesCompleted = true; - break; - } + if (event.data === "[DONE]") break; + let parsedEvent: ResponsesStreamEvent; + try { + parsedEvent = JSON.parse(event.data) as ResponsesStreamEvent; + } catch { continue; } - const update = accumulator.accept(event); - - // Write thinking/reasoning content to the status stream. - if (update.reasoning) { - if (writesStreamingStdout && !inThinking) { - inThinking = true; - statusOut.write(statusColor.dim("Thinking:\n")); - } - if (writesStreamingStdout) statusOut.write(update.reasoning); + const update = inspectResponsesStreamEvent(parsedEvent); + if (update.delta) { + textContent += update.delta; + if (writesStreamingStdout) resultOut.write(update.delta); } - - // Write regular content to stdout; tool_calls stay off the text stream. - if (update.content) { - if (writesStreamingStdout && inThinking) { - statusOut.write(`${statusColor.reset}\n\nResponse:\n`); - inThinking = false; - } - if (writesStreamingStdout) resultOut.write(update.content); + if (update.completed) { + responsesCompleted = true; + break; } - - if (accumulator.completed) break; - } - if (api === "responses") assertResponsesStreamCompleted(responsesCompleted); - if (inThinking) statusOut.write(statusColor.reset); - - if (api === "responses") { - ctx.output.result({ content: textContent }, format === "text" ? { text: "" } : undefined); - } else { - ctx.output.result(accumulator.finish(), format === "text" ? { text: "" } : undefined); } - } else if (api === "responses") { - const response = await ctx.client.requestJson({ - path: requestPath, - method: "POST", - body, - }); - - const text = extractResponsesText(response); - - ctx.output.result(response, format === "text" ? { text } : undefined); - } else { - const response = await ctx.client.requestJson({ - path: requestPath, - method: "POST", - body, - }); - - const text = response.choices?.[0]?.message?.content ?? ""; - - ctx.output.result(response, format === "text" ? { text } : undefined); + assertResponsesStreamCompleted(responsesCompleted); + ctx.output.result({ content: textContent }, format === "text" ? { text: "" } : undefined); + return; } + + const response = await ctx.client.requestJson({ + path: requestPath, + method: "POST", + body, + }); + const text = extractResponsesText(response); + ctx.output.result(response, format === "text" ? { text } : undefined); }, }); diff --git a/packages/commands/src/commands/text/run-chat.ts b/packages/commands/src/commands/text/run-chat.ts new file mode 100644 index 000000000..3309168f7 --- /dev/null +++ b/packages/commands/src/commands/text/run-chat.ts @@ -0,0 +1,165 @@ +import { + createChatCompletionStreamAccumulator, + type ChatStreamContentMode, +} from "../shared/chat-completion-stream.ts"; +import { + parseSSE, + readTextFromPathOrStdin, + type ChatMessage, + type ChatRequest, + type ChatResponse, + type Client, + type CommandOutput, + type LocalizedText, + type Settings, +} from "bailian-cli-core"; +import { ansi } from "bailian-cli-runtime"; + +export interface TextMessageFlags { + message?: string[]; + messagesFile?: string; + system?: string; +} + +export interface ParsedTextMessages { + system?: string; + messages: ChatMessage[]; +} + +/** Parse --message / --messages-file / --system into chat messages. */ +export function parseTextMessages(flags: TextMessageFlags): ParsedTextMessages { + const messages: ChatMessage[] = []; + let system: string | undefined; + + if (flags.system) { + system = flags.system; + } + + if (flags.messagesFile) { + const raw = readTextFromPathOrStdin(flags.messagesFile); + const parsed = JSON.parse(raw) as Array<{ role: string; content: string }>; + for (const parsedMessage of parsed) { + if (parsedMessage.role === "system") { + system = typeof parsedMessage.content === "string" ? parsedMessage.content : ""; + } else { + messages.push(parsedMessage as ChatMessage); + } + } + } + + if (flags.message) { + const validRoles = new Set(["system", "user", "assistant"]); + for (const messageValue of flags.message) { + const colonIndex = messageValue.indexOf(":"); + const maybeRole = colonIndex !== -1 ? messageValue.slice(0, colonIndex) : ""; + + if (validRoles.has(maybeRole)) { + const content = messageValue.slice(colonIndex + 1); + if (maybeRole === "system") { + system = content; + } else { + messages.push({ role: maybeRole as "user" | "assistant", content }); + } + } else { + messages.push({ role: "user", content: messageValue }); + } + } + } + + return { system, messages }; +} + +/** Build the final messages array including an optional system prompt. */ +export function buildChatMessages(parsed: ParsedTextMessages): ChatMessage[] { + const allMessages: ChatMessage[] = []; + if (parsed.system) { + allMessages.push({ role: "system", content: parsed.system }); + } + allMessages.push(...parsed.messages); + return allMessages; +} + +export interface RunChatCompletionsParams { + client: Client; + output: CommandOutput; + localize: (text: LocalizedText) => string; + settings: Settings; + path: string; + body: ChatRequest; + shouldStream: boolean; + contentMode?: ChatStreamContentMode; + /** Dry-run payload; defaults to `{ request: body }`. */ + dryRunResult?: unknown; +} + +/** Execute a Chat Completions request (dry-run, stream, or JSON). */ +export async function runChatCompletions(params: RunChatCompletionsParams): Promise { + const { + client, + output, + localize, + settings, + path, + body, + shouldStream, + contentMode = "delta", + dryRunResult, + } = params; + const format = settings.output; + + if (settings.dryRun) { + output.result(dryRunResult ?? { request: body }); + return; + } + + if (shouldStream) { + const responseStream = await client.request({ + path, + method: "POST", + body, + stream: true, + }); + + let inThinking = false; + const accumulator = createChatCompletionStreamAccumulator(localize, { contentMode }); + const writesStreamingStdout = format === "text"; + const isTTY = process.stdout.isTTY; + const statusOut = format === "json" ? process.stderr : isTTY ? process.stdout : process.stderr; + const resultOut = process.stdout; + const statusColor = ansi(statusOut); + + for await (const event of parseSSE(responseStream)) { + const update = accumulator.accept(event); + + if (update.reasoning) { + if (writesStreamingStdout && !inThinking) { + inThinking = true; + statusOut.write(statusColor.dim("Thinking:\n")); + } + if (writesStreamingStdout) statusOut.write(update.reasoning); + } + + if (update.content) { + if (writesStreamingStdout && inThinking) { + statusOut.write(`${statusColor.reset}\n\nResponse:\n`); + inThinking = false; + } + if (writesStreamingStdout) resultOut.write(update.content); + } + + if (accumulator.completed) break; + } + if (inThinking) statusOut.write(statusColor.reset); + + output.result(accumulator.finish(), format === "text" ? { text: "" } : undefined); + return; + } + + const response = await client.requestJson({ + path, + method: "POST", + body, + }); + const text = response.choices?.[0]?.message?.content ?? ""; + output.result(response, format === "text" ? { text } : undefined); +} diff --git a/packages/commands/src/commands/text/translate.ts b/packages/commands/src/commands/text/translate.ts new file mode 100644 index 000000000..2d71017b4 --- /dev/null +++ b/packages/commands/src/commands/text/translate.ts @@ -0,0 +1,292 @@ +import { buildChatMessages, parseTextMessages, runChatCompletions } from "./run-chat.ts"; +import { + defineCommand, + chatPath, + BailianError, + ExitCode, + type ChatMessage, + type ChatRequest, + type FlagsDef, + type LocalizedText, + type ParsedFlags, + type TranslationOptions, +} from "bailian-cli-core"; + +/** Text Qwen-MT family: plus/flash/lite, including optional dated suffixes. */ +const QWEN_MT_TEXT_MODEL = /^qwen-mt-(plus|flash|lite)(-|$)/; + +/** qwen-mt-plus family streams cumulative snapshots; flash/lite stream incremental deltas. */ +const QWEN_MT_SNAPSHOT_MODEL = /^qwen-mt-plus(-|$)/; + +const DEFAULT_MODEL = "qwen-mt-flash"; + +function isQwenMtTextModel(model: string): boolean { + return QWEN_MT_TEXT_MODEL.test(model); +} + +function usesSnapshotStream(model: string): boolean { + return QWEN_MT_SNAPSHOT_MODEL.test(model); +} + +const TRANSLATE_FLAGS = { + model: { + type: "string", + valueHint: "", + description: { + "en-US": + "Qwen-MT model ID (default: qwen-mt-flash; qwen-mt-plus|flash|lite, optional dated suffix)", + "zh-CN": "Qwen-MT 模型 ID(默认:qwen-mt-flash;qwen-mt-plus|flash|lite,可带日期后缀)", + }, + }, + message: { + type: "array", + valueHint: "", + description: { + "en-US": "Text to translate (repeatable user messages; or use --messages-file)", + "zh-CN": "待翻译文本(可重复的 user 消息;也可使用 --messages-file)", + }, + }, + messagesFile: { + type: "string", + valueHint: "", + description: { + "en-US": "Messages array for one request (use - for stdin; user role only)", + "zh-CN": "同一请求的 messages 数组(使用 - 从 stdin 读取;仅 user 角色)", + }, + }, + sourceLang: { + type: "string", + valueHint: "", + description: { + "en-US": "Translation source language (English name; default: auto)", + "zh-CN": "翻译源语言(英文全称;默认:auto)", + }, + }, + targetLang: { + type: "string", + valueHint: "", + description: { + "en-US": "Translation target language (English name)", + "zh-CN": "翻译目标语言(英文全称)", + }, + required: true, + }, + term: { + type: "array", + valueHint: "", + description: { + "en-US": "Terminology pair source=target (repeatable)", + "zh-CN": "术语对 source=target(可重复)", + }, + }, + domainHint: { + type: "string", + valueHint: "", + description: { + "en-US": "Translation domain hint in English (maps to domains)", + "zh-CN": "翻译领域提示(英文;映射为 domains)", + }, + }, + maxTokens: { + type: "number", + valueHint: "", + description: { + "en-US": "Maximum tokens to generate (default: 4096)", + "zh-CN": "最大生成 Token 数(默认:4096)", + }, + }, + temperature: { + type: "number", + valueHint: "", + description: { "en-US": "Sampling temperature (0.0, 2.0]", "zh-CN": "采样温度 (0.0, 2.0]" }, + }, + topP: { + type: "number", + valueHint: "", + description: { "en-US": "Nucleus sampling threshold", "zh-CN": "核采样阈值" }, + }, + stream: { + type: "switch", + description: { + "en-US": "Stream response tokens (default: on in TTY)", + "zh-CN": "流式输出响应 Token(TTY 中默认开启)", + }, + }, +} satisfies FlagsDef; +type TranslateFlags = ParsedFlags; + +function parseTranslationTerms( + terms: string[] | undefined, + localize: (text: LocalizedText) => string, +): Array<{ source: string; target: string }> | undefined { + if (terms === undefined) return undefined; + const parsed: Array<{ source: string; target: string }> = []; + for (const termValue of terms) { + const equalsIndex = termValue.indexOf("="); + if (equalsIndex <= 0 || equalsIndex === termValue.length - 1) { + throw new BailianError( + localize({ + "en-US": `Invalid --term "${termValue}". Use source=target with non-empty sides.`, + "zh-CN": `无效的 --term "${termValue}"。请使用 source=target,且两侧均非空。`, + }), + ExitCode.USAGE, + ); + } + parsed.push({ + source: termValue.slice(0, equalsIndex), + target: termValue.slice(equalsIndex + 1), + }); + } + return parsed; +} + +function assertUserOnlyTextMessages( + messages: ChatMessage[], + localize: (text: LocalizedText) => string, +): void { + if (messages.length === 0) { + throw new BailianError( + localize({ + "en-US": + "Qwen-MT translation requires at least one non-empty user message (--message or --messages-file).", + "zh-CN": "Qwen-MT 翻译至少需要一条非空 user 消息(--message 或 --messages-file)。", + }), + ExitCode.USAGE, + ); + } + for (const message of messages) { + if (message.role !== "user") { + throw new BailianError( + localize({ + "en-US": + "Qwen-MT translation accepts only user messages (no system/assistant/tool roles).", + "zh-CN": "Qwen-MT 翻译仅接受 user 消息(不能包含 system/assistant/tool 角色)。", + }), + ExitCode.USAGE, + ); + } + if (typeof message.content !== "string" || !message.content) { + throw new BailianError( + localize({ + "en-US": "Qwen-MT translation requires non-empty text content on every user message.", + "zh-CN": "Qwen-MT 翻译要求每条 user 消息的 content 为非空文本。", + }), + ExitCode.USAGE, + ); + } + } +} + +function buildTranslationOptions( + flags: TranslateFlags, + localize: (text: LocalizedText) => string, +): TranslationOptions { + const targetLang = flags.targetLang; + if (targetLang === undefined || targetLang === "") { + throw new BailianError( + localize({ + "en-US": + "--target-lang is required and must be a non-empty English language name (not auto).", + "zh-CN": "--target-lang 必填,且必须为非空英文全称(不能为 auto)。", + }), + ExitCode.USAGE, + ); + } + if (targetLang.toLowerCase() === "auto") { + throw new BailianError( + localize({ + "en-US": "--target-lang cannot be auto.", + "zh-CN": "--target-lang 不能为 auto。", + }), + ExitCode.USAGE, + ); + } + + const options: TranslationOptions = { + source_lang: + flags.sourceLang === undefined || flags.sourceLang === "" ? "auto" : flags.sourceLang, + target_lang: targetLang, + }; + + const terms = parseTranslationTerms(flags.term, localize); + if (terms !== undefined) options.terms = terms; + if (flags.domainHint !== undefined) options.domains = flags.domainHint; + return options; +} + +export default defineCommand({ + description: { + "en-US": "Translate text with Qwen-MT (Chat Completions translation_options)", + "zh-CN": "使用 Qwen-MT 翻译文本(Chat Completions translation_options)", + }, + auth: "apiKey", + usageArgs: "(--message | --messages-file ) --target-lang [flags]", + flags: TRANSLATE_FLAGS, + exampleArgs: [ + { + "en-US": + '--message "欢迎使用百炼。" --source-lang Chinese --target-lang English --term "百炼=Bailian" --dry-run --output json', + "zh-CN": + '--message "欢迎使用百炼。" --source-lang Chinese --target-lang English --term "百炼=Bailian" --dry-run --output json', + }, + { + "en-US": + '--model qwen-mt-plus --message "欢迎使用百炼。" --target-lang English --stream --output json', + "zh-CN": + '--model qwen-mt-plus --message "欢迎使用百炼。" --target-lang English --stream --output json', + }, + ], + notes: [ + { + "en-US": + "Language names use English full names (Chinese/English), not zh/en codes. Only user messages are accepted. Does not support --api responses, --batch, --prime, --tool, --enable-search, or thinking flags. For general chat use `text chat`.", + "zh-CN": + "语言名用英文全称(Chinese/English),不用 zh/en 代码。仅接受 user 消息。不支持 --api responses、--batch、--prime、--tool、--enable-search 及 thinking 参数。普通对话请用 `text chat`。", + }, + ], + validate: (flags) => { + if (!flags.message && !flags.messagesFile) return "Provide --message or --messages-file."; + return undefined; + }, + async run(ctx) { + const { settings, flags } = ctx; + const localize = ctx.localize; + const model = flags.model || DEFAULT_MODEL; + + if (!isQwenMtTextModel(model)) { + throw new BailianError( + localize({ + "en-US": `Unsupported model "${model}". Use qwen-mt-flash, qwen-mt-plus, or qwen-mt-lite (optional dated suffix).`, + "zh-CN": `不支持的模型 "${model}"。请使用 qwen-mt-flash、qwen-mt-plus 或 qwen-mt-lite(可带日期后缀)。`, + }), + ExitCode.USAGE, + ); + } + + const allMessages = buildChatMessages(parseTextMessages(flags)); + assertUserOnlyTextMessages(allMessages, localize); + + const shouldStream = flags.stream || process.stdout.isTTY; + const body: ChatRequest = { + model, + messages: allMessages, + max_tokens: flags.maxTokens ?? 4096, + stream: shouldStream, + translation_options: buildTranslationOptions(flags, localize), + }; + if (flags.temperature !== undefined) body.temperature = flags.temperature; + if (flags.topP !== undefined) body.top_p = flags.topP; + + const contentMode = usesSnapshotStream(model) ? "snapshot" : "delta"; + await runChatCompletions({ + client: ctx.client, + output: ctx.output, + localize, + settings, + path: chatPath(), + body, + shouldStream, + contentMode, + }); + }, +}); diff --git a/packages/commands/src/commands/video/task-get.ts b/packages/commands/src/commands/video/task-get.ts index f367ce53f..513a795f9 100644 --- a/packages/commands/src/commands/video/task-get.ts +++ b/packages/commands/src/commands/video/task-get.ts @@ -34,13 +34,19 @@ export default defineCommand({ path: taskPath(taskId), }); - // 透传服务端失败字段:FAILED 时 output.code / output.message 是排查依据;request_id 便于工单溯源。 + // Pass through server failure fields and translation outputs for image/omni tasks. ctx.output.result( stripUndefined({ task_id: response.output.task_id, task_status: response.output.task_status, video_url: response.output.video_url, + image_url: response.output.image_url, results: response.output.results, + Success: response.output.Success, + Code: response.output.Code, + Message: response.output.Message, + RequestId: response.output.RequestId, + Data: response.output.Data, submit_time: response.output.submit_time, scheduled_time: response.output.scheduled_time, end_time: response.output.end_time, diff --git a/packages/commands/src/index.ts b/packages/commands/src/index.ts index ea63db17c..51a2c9885 100644 --- a/packages/commands/src/index.ts +++ b/packages/commands/src/index.ts @@ -8,6 +8,7 @@ export { default as authStatus } from "./commands/auth/status.ts"; export { default as authLogout } from "./commands/auth/logout.ts"; export { default as authGenerateAccessToken } from "./commands/auth/generate-access-token.ts"; export { default as textChat } from "./commands/text/chat.ts"; +export { default as textTranslate } from "./commands/text/translate.ts"; export { default as batchConvert } from "./commands/batch/convert.ts"; export { default as batchCreate } from "./commands/batch/create.ts"; export { default as batchList } from "./commands/batch/list.ts"; @@ -15,8 +16,10 @@ export { default as batchGet } from "./commands/batch/get.ts"; export { default as batchCancel } from "./commands/batch/cancel.ts"; export { default as batchDownload } from "./commands/batch/download.ts"; export { default as textOmni } from "./commands/omni/chat.ts"; +export { default as omniTranslate } from "./commands/omni/translate.ts"; export { default as imageGenerate } from "./commands/image/generate.ts"; export { default as imageEdit } from "./commands/image/edit.ts"; +export { default as imageTranslate } from "./commands/image/translate.ts"; export { default as videoGenerate } from "./commands/video/generate.ts"; export { default as videoEdit } from "./commands/video/edit.ts"; export { default as videoRef } from "./commands/video/ref.ts"; diff --git a/packages/commands/tests/e2e/image-translate.e2e.test.ts b/packages/commands/tests/e2e/image-translate.e2e.test.ts new file mode 100644 index 000000000..ca90c18dd --- /dev/null +++ b/packages/commands/tests/e2e/image-translate.e2e.test.ts @@ -0,0 +1,498 @@ +import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http"; +import { readFileSync, rmSync } from "node:fs"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { + isBailianE2EMediaEnabled, + isDashScopeE2EReady, + makeE2eOutputDir, + parseStdoutJson, + runCommandHelp, + runCommandE2e, + runCommandInProcess, +} from "./helpers.ts"; +import { IMAGE_TRANSLATE_ROUTES } from "./topic-routes.ts"; + +const servers: Server[] = []; +const tempDirs: string[] = []; + +afterEach(async () => { + await Promise.all( + servers.splice(0).map( + (server) => + new Promise((resolve, reject) => { + server.close((error) => (error ? reject(error) : resolve())); + }), + ), + ); + for (const tempDir of tempDirs.splice(0)) { + rmSync(tempDir, { recursive: true, force: true }); + } +}); + +async function startServer( + handler: (request: IncomingMessage, response: ServerResponse, body: string) => void, +): Promise<{ + origin: string; + requests: Array<{ + method?: string; + path: string; + headers: IncomingMessage["headers"]; + body: string; + }>; +}> { + const requests: Array<{ + method?: string; + path: string; + headers: IncomingMessage["headers"]; + body: string; + }> = []; + const server = createServer((request, response) => { + const chunks: Buffer[] = []; + request.on("data", (chunk) => chunks.push(chunk)); + request.on("end", () => { + const body = Buffer.concat(chunks).toString("utf8"); + const url = new URL(request.url ?? "/", "http://127.0.0.1"); + requests.push({ + method: request.method, + path: url.pathname, + headers: request.headers, + body, + }); + handler(request, response, body); + }); + }); + await new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", resolve); + }); + servers.push(server); + const address = server.address(); + if (!address || typeof address === "string") throw new Error("Expected TCP address"); + return { origin: `http://127.0.0.1:${address.port}`, requests }; +} + +describe("e2e: image translate", () => { + test("help shows bilingual flags and default model", async () => { + const { stderr, exitCode } = await runCommandHelp(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--help", + ]); + expect(exitCode, stderr).toBe(0); + expect(stderr).toMatch(/--image/); + expect(stderr).toMatch(/--target-lang/); + expect(stderr).toMatch(/Target language|目标语言/); + expect(stderr).toMatch(/qwen-mt-image/); + expect(stderr).toMatch(/--async/); + }); + + test("missing image or target-lang → 2", async () => { + const missingImage = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--target-lang", + "en", + "--quiet", + ]); + expect(missingImage.exitCode).toBe(2); + + const missingTarget = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--image", + "https://example.com/a.png", + "--quiet", + ]); + expect(missingTarget.exitCode).toBe(2); + }); + + test("rejects auto target, same langs, missing local file", async () => { + const autoTarget = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--image", + "https://example.com/a.png", + "--target-lang", + "auto", + "--dry-run", + "--quiet", + ]); + expect(autoTarget.exitCode).toBe(2); + + const same = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--image", + "https://example.com/a.png", + "--source-lang", + "en", + "--target-lang", + "en", + "--dry-run", + "--quiet", + ]); + expect(same.exitCode).toBe(2); + + const missingLocal = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--image", + "/tmp/bl-image-translate-missing.png", + "--target-lang", + "en", + "--dry-run", + "--quiet", + ]); + expect(missingLocal.exitCode).toBe(2); + expect(missingLocal.stderr).toMatch(/not found|不存在/i); + }); + + test("dry-run builds async image2image body with defaults", async () => { + const { stdout, stderr, exitCode } = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--image", + "https://example.com/poster.png", + "--target-lang", + "en", + "--term", + "百炼=Bailian", + "--domain-hint", + "ecommerce", + "--image-segment", + "false", + "--dry-run", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + endpoint?: string; + request?: { + model?: string; + input?: { + image_url?: string; + source_lang?: string; + target_lang?: string; + ext?: { + domainHint?: string; + terminologies?: Array<{ src: string; tgt: string }>; + config?: { imageSegment?: boolean }; + }; + }; + }; + }>(stdout); + expect(body.endpoint).toBe("/api/v1/services/aigc/image2image/image-synthesis"); + expect(body.request?.model).toBe("qwen-mt-image"); + expect(body.request?.input?.image_url).toBe("https://example.com/poster.png"); + expect(body.request?.input?.source_lang).toBe("auto"); + expect(body.request?.input?.target_lang).toBe("en"); + expect(body.request?.input?.ext?.domainHint).toBe("ecommerce"); + expect(body.request?.input?.ext?.terminologies).toEqual([{ src: "百炼", tgt: "Bailian" }]); + expect(body.request?.input?.ext?.config?.imageSegment).toBe(false); + }); + + test("async loopback returns task id with zero GET", async () => { + const { origin, requests } = await startServer((_request, response) => { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { task_id: "task-image-mt-1", task_status: "PENDING" }, + request_id: "req-1", + }), + ); + }); + + const { stdout, stderr, exitCode } = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--image", + "https://example.com/poster.png", + "--target-lang", + "en", + "--async", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ task_id?: string; request_id?: string }>(stdout); + expect(body.task_id).toBe("task-image-mt-1"); + expect(body.request_id).toBe("req-1"); + expect(requests).toHaveLength(1); + expect(requests[0]?.method).toBe("POST"); + expect(requests[0]?.path).toBe("/api/v1/services/aigc/image2image/image-synthesis"); + expect(requests[0]?.headers["x-dashscope-async"]).toBe("enable"); + }); + + test("wait mode polls until image_url and downloads jpg", async () => { + let pollCount = 0; + const jpgBytes = Buffer.from([0xff, 0xd8, 0xff, 0xd9]); + const { origin } = await startServer((request, response) => { + const url = new URL(request.url ?? "/", "http://127.0.0.1"); + if (request.method === "POST") { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { task_id: "task-image-mt-2", task_status: "PENDING" }, + request_id: "req-2", + }), + ); + return; + } + if (url.pathname === "/api/v1/tasks/task-image-mt-2") { + pollCount += 1; + if (pollCount < 2) { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { task_id: "task-image-mt-2", task_status: "RUNNING" }, + request_id: "req-2", + }), + ); + return; + } + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { + task_id: "task-image-mt-2", + task_status: "SUCCEEDED", + image_url: `${origin}/translated.jpg`, + message: "No text detected for translation", + }, + request_id: "req-2", + }), + ); + return; + } + if (url.pathname === "/translated.jpg") { + response.writeHead(200, { "content-type": "image/jpeg" }); + response.end(jpgBytes); + return; + } + response.writeHead(404); + response.end(); + }); + + const outDir = makeE2eOutputDir("image-translate-wait"); + tempDirs.push(outDir); + + const { stdout, stderr, exitCode } = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--image", + "https://example.com/poster.png", + "--target-lang", + "en", + "--poll-interval", + "0.05", + "--timeout", + "10", + "--out-dir", + outDir, + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + task_id?: string; + image_url?: string; + saved?: string; + output?: { message?: string }; + }>(stdout); + expect(body.task_id).toBe("task-image-mt-2"); + expect(body.image_url).toMatch(/translated\.jpg$/); + expect(body.output?.message).toBe("No text detected for translation"); + expect(body.saved).toBeTruthy(); + expect(readFileSync(body.saved!).equals(jpgBytes)).toBe(true); + }); + + test("FAILED preserves server message via load hook", async () => { + const { origin } = await startServer((request, response) => { + const url = new URL(request.url ?? "/", "http://127.0.0.1"); + if (request.method === "POST") { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { task_id: "task-fail", task_status: "PENDING" }, + request_id: "req-fail", + }), + ); + return; + } + if (url.pathname === "/api/v1/tasks/task-fail") { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { + task_id: "task-fail", + task_status: "FAILED", + code: "InvalidParameter", + message: "image too large", + }, + request_id: "req-fail", + }), + ); + return; + } + response.writeHead(404); + response.end(); + }); + + const { stderr, exitCode } = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--image", + "https://example.com/poster.png", + "--target-lang", + "en", + "--poll-interval", + "0.05", + "--timeout", + "5", + "--output", + "json", + ]); + expect(exitCode).toBe(1); + expect(stderr).toMatch(/image too large/); + expect(stderr).not.toMatch(/Task failed:/); + }); + + test("CANCELED and UNKNOWN fail without waiting for timeout", async () => { + for (const status of ["CANCELED", "UNKNOWN"] as const) { + let getCount = 0; + const taskId = `task-${status.toLowerCase()}`; + const { origin } = await startServer((request, response) => { + const url = new URL(request.url ?? "/", "http://127.0.0.1"); + if (request.method === "POST") { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { task_id: taskId, task_status: "PENDING" }, + request_id: `req-${status.toLowerCase()}`, + }), + ); + return; + } + if (url.pathname === `/api/v1/tasks/${taskId}`) { + getCount += 1; + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { + task_id: taskId, + task_status: status, + }, + request_id: `req-${status.toLowerCase()}`, + }), + ); + return; + } + response.writeHead(404); + response.end(); + }); + + const { stderr, exitCode } = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--image", + "https://example.com/poster.png", + "--target-lang", + "en", + "--poll-interval", + "0.05", + "--timeout", + "5", + "--output", + "json", + ]); + expect(exitCode, status).toBe(1); + expect(stderr, status).toMatch(new RegExp(status)); + expect(getCount, status).toBe(1); + } + }); + + test("task get projects image_url", async () => { + const { origin } = await startServer((_request, response) => { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { + task_id: "task-get-img", + task_status: "SUCCEEDED", + image_url: "https://example.com/out.jpg", + video_url: "https://example.com/out.mp4", + }, + request_id: "req-get", + }), + ); + }); + + const { stdout, stderr, exitCode } = await runCommandInProcess(IMAGE_TRANSLATE_ROUTES, [ + "video", + "task", + "get", + "--base-url", + origin, + "--api-key", + "sk-test", + "--task-id", + "task-get-img", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + task_id?: string; + image_url?: string; + video_url?: string; + }>(stdout); + expect(body.task_id).toBe("task-get-img"); + expect(body.image_url).toBe("https://example.com/out.jpg"); + expect(body.video_url).toBe("https://example.com/out.mp4"); + }); +}); + +describe.skipIf(!isBailianE2EMediaEnabled() || !isDashScopeE2EReady())( + "e2e: image translate live", + () => { + test("translates a public image URL", async () => { + const outDir = makeE2eOutputDir("image-translate-live"); + tempDirs.push(outDir); + const { stdout, stderr, exitCode } = await runCommandE2e(IMAGE_TRANSLATE_ROUTES, [ + "image", + "translate", + "--image", + "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250916/ordhsk/1.webp", + "--source-lang", + "zh", + "--target-lang", + "en", + "--out-dir", + outDir, + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ image_url?: string; saved?: string }>(stdout); + expect(body.image_url).toBeTruthy(); + expect(body.saved && readFileSync(body.saved).length).toBeGreaterThan(0); + }, 300_000); + }, +); diff --git a/packages/commands/tests/e2e/omni-translate.e2e.test.ts b/packages/commands/tests/e2e/omni-translate.e2e.test.ts new file mode 100644 index 000000000..4d3ae6826 --- /dev/null +++ b/packages/commands/tests/e2e/omni-translate.e2e.test.ts @@ -0,0 +1,558 @@ +import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { + isBailianE2EMediaEnabled, + isDashScopeE2EReady, + makeE2eOutputDir, + parseStdoutJson, + runCommandHelp, + runCommandE2e, + runCommandInProcess, +} from "./helpers.ts"; +import { OMNI_TRANSLATE_ROUTES } from "./topic-routes.ts"; + +const servers: Server[] = []; +const tempDirs: string[] = []; + +afterEach(async () => { + await Promise.all( + servers.splice(0).map( + (server) => + new Promise((resolve, reject) => { + server.close((error) => (error ? reject(error) : resolve())); + }), + ), + ); + for (const tempDir of tempDirs.splice(0)) { + rmSync(tempDir, { recursive: true, force: true }); + } +}); + +async function startServer( + handler: (request: IncomingMessage, response: ServerResponse, body: string) => void, +): Promise<{ + origin: string; + requests: Array<{ method?: string; path: string; body: string }>; +}> { + const requests: Array<{ method?: string; path: string; body: string }> = []; + const server = createServer((request, response) => { + const chunks: Buffer[] = []; + request.on("data", (chunk) => chunks.push(chunk)); + request.on("end", () => { + const body = Buffer.concat(chunks).toString("utf8"); + const url = new URL(request.url ?? "/", "http://127.0.0.1"); + requests.push({ method: request.method, path: url.pathname, body }); + handler(request, response, body); + }); + }); + await new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", resolve); + }); + servers.push(server); + const address = server.address(); + if (!address || typeof address === "string") throw new Error("Expected TCP address"); + return { origin: `http://127.0.0.1:${address.port}`, requests }; +} + +describe("e2e: omni translate routing / validation", () => { + test("omni help keeps chat usage and mentions translate", async () => { + const { stderr, exitCode } = await runCommandHelp(OMNI_TRANSLATE_ROUTES, ["omni", "--help"]); + expect(exitCode, stderr).toBe(0); + expect(stderr).toMatch(/--message/); + expect(stderr).toMatch(/omni translate|文件翻译|file translation/i); + }); + + test("omni translate help is bilingual", async () => { + const { stderr, exitCode } = await runCommandHelp(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--help", + ]); + expect(exitCode, stderr).toBe(0); + expect(stderr).toMatch(/--file/); + expect(stderr).toMatch(/--target-lang/); + expect(stderr).toMatch(/Document, image or audio|文档、图片或音频/); + expect(stderr).toMatch(/qwen-mt-uni|async/i); + }); + + test("old omni dry-run still works", async () => { + const { stdout, stderr, exitCode } = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "--message", + "请介绍你自己。", + "--text-only", + "--dry-run", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ request?: { model?: string; messages?: unknown } }>(stdout); + expect(body.request?.model).toBeTruthy(); + expect(body.request?.messages).toBeTruthy(); + }); + + test("missing file/target and rejected formats → 2", async () => { + const missingFile = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--target-lang", + "en", + "--quiet", + ]); + expect(missingFile.exitCode).toBe(2); + + const missingTarget = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--file", + "https://example.com/a.pdf", + "--quiet", + ]); + expect(missingTarget.exitCode).toBe(2); + + const video = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--file", + "https://example.com/a.mp4", + "--target-lang", + "en", + "--dry-run", + "--quiet", + ]); + expect(video.exitCode).toBe(2); + expect(video.stderr).toMatch(/mp4|video|视频/i); + + const legacyDoc = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--file", + "https://example.com/a.doc", + "--target-lang", + "en", + "--dry-run", + "--quiet", + ]); + expect(legacyDoc.exitCode).toBe(2); + }); + + test("dry-run uses fileUrl and omits source_lang when unset", async () => { + const { stdout, stderr, exitCode } = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--file", + "https://example.com/brochure.pdf", + "--target-lang", + "en", + "--format-hint", + "pdf", + "--domain-hint", + "legal", + "--dry-run", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + endpoint?: string; + request?: { + model?: string; + input?: { + fileUrl?: string; + target_lang?: string; + source_lang?: string; + ext?: { format_hint?: string; domainHint?: string }; + }; + }; + }>(stdout); + expect(body.endpoint).toBe("/api/v1/services/aigc/multimodal-generation/generation"); + expect(body.request?.model).toBe("qwen-mt-uni"); + expect(body.request?.input?.fileUrl).toBe("https://example.com/brochure.pdf"); + expect(body.request?.input?.target_lang).toBe("en"); + expect(body.request?.input?.source_lang).toBeUndefined(); + expect(body.request?.input?.ext?.format_hint).toBe("pdf"); + expect(body.request?.input?.ext?.domainHint).toBe("legal"); + }); + + test("local dry-run marks pending upload", async () => { + const tempDir = mkdtempSync(join(tmpdir(), "bl-omni-mt-")); + tempDirs.push(tempDir); + const pdfPath = join(tempDir, "brochure.pdf"); + writeFileSync(pdfPath, "%PDF-1.4"); + + const { stdout, stderr, exitCode } = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--file", + pdfPath, + "--target-lang", + "en", + "--dry-run", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ request?: { input?: { fileUrl?: string } } }>(stdout); + expect(body.request?.input?.fileUrl).toMatch(/pending-upload/); + }); +}); + +describe("e2e: omni translate loopback", () => { + test("async returns task id with zero GET", async () => { + const { origin, requests } = await startServer((_request, response) => { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { task_id: "task-omni-1", task_status: "PENDING" }, + request_id: "req-omni-1", + }), + ); + }); + + const { stdout, stderr, exitCode } = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--file", + "https://example.com/a.pdf", + "--target-lang", + "en", + "--async", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ task_id?: string }>(stdout); + expect(body.task_id).toBe("task-omni-1"); + expect(requests).toHaveLength(1); + expect(requests[0]?.path).toBe("/api/v1/services/aigc/multimodal-generation/generation"); + }); + + test("SUCCEEDED with Success=false fails with original Message", async () => { + const { origin } = await startServer((request, response) => { + const url = new URL(request.url ?? "/", "http://127.0.0.1"); + if (request.method === "POST") { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { task_id: "task-biz-fail", task_status: "PENDING" }, + request_id: "req-biz", + }), + ); + return; + } + if (url.pathname === "/api/v1/tasks/task-biz-fail") { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { + task_id: "task-biz-fail", + task_status: "SUCCEEDED", + Success: false, + Code: 500, + Message: "biz translation failed", + RequestId: "biz-req", + }, + request_id: "req-biz", + }), + ); + return; + } + response.writeHead(404); + response.end(); + }); + + const { stderr, exitCode } = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--file", + "https://example.com/a.pdf", + "--target-lang", + "en", + "--poll-interval", + "0.05", + "--timeout", + "5", + "--output", + "json", + ]); + expect(exitCode).toBe(1); + expect(stderr).toMatch(/biz translation failed/); + }); + + test("SUCCEEDED without Success fails without waiting for timeout", async () => { + let getCount = 0; + const { origin } = await startServer((request, response) => { + const url = new URL(request.url ?? "/", "http://127.0.0.1"); + if (request.method === "POST") { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { task_id: "task-no-success", task_status: "PENDING" }, + request_id: "req-no-success", + }), + ); + return; + } + if (url.pathname === "/api/v1/tasks/task-no-success") { + getCount += 1; + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { + task_id: "task-no-success", + task_status: "SUCCEEDED", + Message: "missing Success field", + RequestId: "biz-no-success", + }, + request_id: "req-no-success", + }), + ); + return; + } + response.writeHead(404); + response.end(); + }); + + const { stderr, exitCode } = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--file", + "https://example.com/a.pdf", + "--target-lang", + "en", + "--poll-interval", + "0.05", + "--timeout", + "5", + "--output", + "json", + ]); + expect(exitCode).toBe(1); + expect(stderr).toMatch(/missing Success field|Translation business call failed/i); + expect(getCount).toBe(1); + }); + + test("CANCELED fails without waiting for timeout", async () => { + let getCount = 0; + const { origin } = await startServer((request, response) => { + const url = new URL(request.url ?? "/", "http://127.0.0.1"); + if (request.method === "POST") { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { task_id: "task-canceled", task_status: "PENDING" }, + request_id: "req-canceled", + }), + ); + return; + } + if (url.pathname === "/api/v1/tasks/task-canceled") { + getCount += 1; + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { + task_id: "task-canceled", + task_status: "CANCELED", + }, + request_id: "req-canceled", + }), + ); + return; + } + response.writeHead(404); + response.end(); + }); + + const { stderr, exitCode } = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--file", + "https://example.com/a.pdf", + "--target-lang", + "en", + "--poll-interval", + "0.05", + "--timeout", + "5", + "--output", + "json", + ]); + expect(exitCode).toBe(1); + expect(stderr).toMatch(/CANCELED/); + expect(getCount).toBe(1); + }); + + test("Success=true downloads translated file", async () => { + const fileBytes = Buffer.from("%PDF-translated"); + const { origin } = await startServer((request, response) => { + const url = new URL(request.url ?? "/", "http://127.0.0.1"); + if (request.method === "POST") { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { task_id: "task-ok", task_status: "PENDING" }, + request_id: "req-ok", + }), + ); + return; + } + if (url.pathname === "/api/v1/tasks/task-ok") { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { + task_id: "task-ok", + task_status: "SUCCEEDED", + Success: true, + Code: 200, + Message: "Success", + RequestId: "biz-ok", + Data: { + FileType: "pdf", + TranslatedFileUrl: `${origin}/out.pdf`, + }, + }, + request_id: "req-ok", + }), + ); + return; + } + if (url.pathname === "/out.pdf") { + response.writeHead(200, { "content-type": "application/pdf" }); + response.end(fileBytes); + return; + } + response.writeHead(404); + response.end(); + }); + + const outDir = makeE2eOutputDir("omni-translate-wait"); + tempDirs.push(outDir); + + const { stdout, stderr, exitCode } = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--file", + "https://example.com/a.pdf", + "--target-lang", + "en", + "--poll-interval", + "0.05", + "--timeout", + "10", + "--out-dir", + outDir, + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + task_id?: string; + translated_file_url?: string; + saved?: string; + output?: { Success?: boolean; Data?: { FileType?: string } }; + }>(stdout); + expect(body.task_id).toBe("task-ok"); + expect(body.output?.Success).toBe(true); + expect(body.output?.Data?.FileType).toBe("pdf"); + expect(body.saved).toMatch(/\.pdf$/); + expect(readFileSync(body.saved!).equals(fileBytes)).toBe(true); + }); + + test("task get projects Success/Data fields", async () => { + const { origin } = await startServer((_request, response) => { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + output: { + task_id: "task-get-omni", + task_status: "SUCCEEDED", + Success: true, + Code: 200, + Message: "Success", + RequestId: "biz-get", + Data: { FileType: "image", TranslatedFileUrl: "https://example.com/t.jpg" }, + video_url: "https://example.com/v.mp4", + }, + request_id: "req-get", + }), + ); + }); + + const { stdout, stderr, exitCode } = await runCommandInProcess(OMNI_TRANSLATE_ROUTES, [ + "video", + "task", + "get", + "--base-url", + origin, + "--api-key", + "sk-test", + "--task-id", + "task-get-omni", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + Success?: boolean; + Data?: { FileType?: string }; + video_url?: string; + }>(stdout); + expect(body.Success).toBe(true); + expect(body.Data?.FileType).toBe("image"); + expect(body.video_url).toBe("https://example.com/v.mp4"); + }); +}); + +describe.skipIf(!isBailianE2EMediaEnabled() || !isDashScopeE2EReady())( + "e2e: omni translate live", + () => { + test("translates a public image file URL", async () => { + const outDir = makeE2eOutputDir("omni-translate-live"); + tempDirs.push(outDir); + const { stdout, stderr, exitCode } = await runCommandE2e(OMNI_TRANSLATE_ROUTES, [ + "omni", + "translate", + "--file", + "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250916/ordhsk/1.webp", + "--target-lang", + "en", + "--format-hint", + "image", + "--out-dir", + outDir, + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ translated_file_url?: string; saved?: string }>(stdout); + expect(body.translated_file_url).toBeTruthy(); + expect(body.saved && readFileSync(body.saved).length).toBeGreaterThan(0); + }, 300_000); + }, +); diff --git a/packages/commands/tests/e2e/text-translation.e2e.test.ts b/packages/commands/tests/e2e/text-translation.e2e.test.ts new file mode 100644 index 000000000..41f8e2eab --- /dev/null +++ b/packages/commands/tests/e2e/text-translation.e2e.test.ts @@ -0,0 +1,518 @@ +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, test } from "vite-plus/test"; +import { + isDashScopeE2EReady, + parseStdoutJson, + runCommandHelp, + runCommandE2e, + runCommandInProcess, +} from "./helpers.ts"; +import { TEXT_CHAT_ROUTES, TEXT_TRANSLATE_ROUTES } from "./topic-routes.ts"; + +let isolatedDirectory: string; +const servers: Server[] = []; + +beforeEach(() => { + isolatedDirectory = mkdtempSync(join(tmpdir(), "bl-text-mt-e2e-")); + writeFileSync(join(isolatedDirectory, "config.json"), "{}"); +}); + +afterEach(async () => { + await Promise.all( + servers.splice(0).map( + (server) => + new Promise((resolve, reject) => { + server.close((error) => (error ? reject(error) : resolve())); + }), + ), + ); + rmSync(isolatedDirectory, { recursive: true, force: true }); +}); + +async function startChatServer( + handler: (request: IncomingMessage, response: ServerResponse) => void, +): Promise<{ origin: string }> { + const server = createServer(handler); + await new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", resolve); + }); + servers.push(server); + const address = server.address(); + if (!address || typeof address === "string") throw new Error("Expected a local TCP server."); + return { origin: `http://127.0.0.1:${address.port}` }; +} + +describe("e2e: text translate (Qwen-MT)", () => { + test("help lists translation flags bilingually", async () => { + const { stderr, exitCode } = await runCommandHelp(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--help", + ]); + expect(exitCode, stderr).toBe(0); + expect(stderr).toMatch(/--source-lang/); + expect(stderr).toMatch(/--target-lang/); + expect(stderr).toMatch(/Translation target language|翻译目标语言/); + expect(stderr).toMatch(/--term/); + expect(stderr).toMatch(/--domain-hint/); + expect(stderr).toMatch(/qwen-mt-flash/); + }); + + test("text chat help points to text translate and has no MT flags", async () => { + const { stderr, exitCode } = await runCommandHelp(TEXT_CHAT_ROUTES, ["text", "chat", "--help"]); + expect(exitCode, stderr).toBe(0); + expect(stderr).toMatch(/text translate/); + expect(stderr).not.toMatch(/--source-lang/); + expect(stderr).not.toMatch(/--target-lang/); + }); + + test("unsupported models → 2 (generic, image/uni, turbo)", async () => { + for (const model of ["qwen3.8-max", "qwen-mt-image", "qwen-mt-uni", "qwen-mt-turbo"]) { + const { stderr, exitCode } = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--model", + model, + "--message", + "hello", + "--target-lang", + "English", + "--dry-run", + "--quiet", + ]); + expect(exitCode, `${model}: ${stderr}`).toBe(2); + expect(stderr).toMatch(/Unsupported model|不支持的模型/i); + } + }); + + test("dated flash/plus suffixes are accepted in dry-run", async () => { + const flash = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--model", + "qwen-mt-flash-2025-01-01", + "--message", + "hello", + "--target-lang", + "English", + "--dry-run", + "--output", + "json", + ]); + expect(flash.exitCode, flash.stderr).toBe(0); + expect(parseStdoutJson<{ request?: { model?: string } }>(flash.stdout).request?.model).toBe( + "qwen-mt-flash-2025-01-01", + ); + + const plus = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--model", + "qwen-mt-plus-2025-01-01", + "--message", + "hello", + "--target-lang", + "English", + "--dry-run", + "--output", + "json", + ]); + expect(plus.exitCode, plus.stderr).toBe(0); + expect(parseStdoutJson<{ request?: { model?: string } }>(plus.stdout).request?.model).toBe( + "qwen-mt-plus-2025-01-01", + ); + }); + + test("missing target-lang → 2", async () => { + const { stderr, exitCode } = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--message", + "hello", + "--dry-run", + "--quiet", + ]); + expect(exitCode).toBe(2); + expect(stderr).toMatch(/target-lang/i); + }); + + test("rejects blank target, auto target, bad term", async () => { + const blankTarget = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--message", + "hello", + "--target-lang", + "", + "--dry-run", + "--quiet", + ]); + expect(blankTarget.exitCode).toBe(2); + + const autoTarget = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--message", + "hello", + "--target-lang", + "auto", + "--dry-run", + "--quiet", + ]); + expect(autoTarget.exitCode).toBe(2); + expect(autoTarget.stderr).toMatch(/auto/i); + + const badTerm = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--message", + "hello", + "--target-lang", + "English", + "--term", + "=Bailian", + "--dry-run", + "--quiet", + ]); + expect(badTerm.exitCode).toBe(2); + expect(badTerm.stderr).toMatch(/--term/i); + }); + + test("rejects system/assistant roles", async () => { + const withSystem = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--message", + "system:You are a translator", + "--message", + "hello", + "--target-lang", + "English", + "--dry-run", + "--quiet", + ]); + expect(withSystem.exitCode).toBe(2); + expect(withSystem.stderr).toMatch(/user|system/i); + }); + + test("rejects empty messages-file and empty message content → 2", async () => { + const emptyFile = join(isolatedDirectory, "empty-messages.json"); + writeFileSync(emptyFile, "[]"); + + const emptyArray = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--messages-file", + emptyFile, + "--target-lang", + "English", + "--dry-run", + "--quiet", + ]); + expect(emptyArray.exitCode).toBe(2); + expect(emptyArray.stderr).toMatch(/at least one|至少需要一条/i); + + const emptyContent = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--message", + "", + "--target-lang", + "English", + "--dry-run", + "--quiet", + ]); + expect(emptyContent.exitCode).toBe(2); + expect(emptyContent.stderr).toMatch(/non-empty|非空/i); + }); + + test("dry-run injects top-level translation_options without extra_body", async () => { + const { stdout, stderr, exitCode } = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--model", + "qwen-mt-flash", + "--message", + "欢迎使用百炼。", + "--source-lang", + "Chinese", + "--target-lang", + "English", + "--term", + "百炼=Bailian", + "--term", + "模型=model", + "--domain-hint", + "product documentation", + "--dry-run", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + request?: { + model?: string; + translation_options?: { + source_lang?: string; + target_lang?: string; + terms?: Array<{ source: string; target: string }>; + domains?: string; + }; + extra_body?: unknown; + enable_search?: boolean; + }; + }>(stdout); + expect(body.request?.model).toBe("qwen-mt-flash"); + expect(body.request?.translation_options).toEqual({ + source_lang: "Chinese", + target_lang: "English", + terms: [ + { source: "百炼", target: "Bailian" }, + { source: "模型", target: "model" }, + ], + domains: "product documentation", + }); + expect(body.request?.extra_body).toBeUndefined(); + expect(body.request?.enable_search).toBeUndefined(); + }); + + test("default model is qwen-mt-flash and source-lang defaults to auto", async () => { + const { stdout, stderr, exitCode } = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--message", + "hello", + "--target-lang", + "Chinese", + "--dry-run", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + request?: { model?: string; translation_options?: { source_lang?: string } }; + }>(stdout); + expect(body.request?.model).toBe("qwen-mt-flash"); + expect(body.request?.translation_options?.source_lang).toBe("auto"); + }); + + test("ordinary chat without translation flags stays unchanged", async () => { + const { stdout, stderr, exitCode } = await runCommandInProcess( + TEXT_CHAT_ROUTES, + ["text", "chat", "--message", "hello", "--dry-run", "--output", "json"], + { DASHSCOPE_API_KEY: "" }, + ); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ request?: Record }>(stdout); + expect(body.request?.translation_options).toBeUndefined(); + }); + + test("loopback non-stream returns translation text", async () => { + const { origin } = await startChatServer((_request, response) => { + response.writeHead(200, { "content-type": "application/json" }); + response.end( + JSON.stringify({ + id: "chatcmpl-mt", + object: "chat.completion", + created: 1, + model: "qwen-mt-flash", + choices: [ + { + index: 0, + message: { role: "assistant", content: "Welcome to Bailian." }, + finish_reason: "stop", + }, + ], + usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, + }), + ); + }); + + const { stdout, stderr, exitCode } = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--message", + "欢迎使用百炼。", + "--target-lang", + "English", + "--output", + "json", + ]); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + choices?: Array<{ message?: { content?: string } }>; + }>(stdout); + expect(body.choices?.[0]?.message?.content).toBe("Welcome to Bailian."); + }); + + test("loopback SSE snapshot mode for qwen-mt-plus family does not duplicate", async () => { + for (const model of ["qwen-mt-plus", "qwen-mt-plus-2025-01-01"]) { + const { origin } = await startChatServer((_request, response) => { + response.writeHead(200, { "content-type": "text/event-stream" }); + const frames = ["H", "Hello", "Hello world"]; + for (const content of frames) { + response.write( + `data: ${JSON.stringify({ + choices: [{ delta: { content }, finish_reason: null }], + })}\n\n`, + ); + } + response.write( + `data: ${JSON.stringify({ + choices: [{ delta: {}, finish_reason: "stop" }], + })}\n\n`, + ); + response.write("data: [DONE]\n\n"); + response.end(); + }); + + const { stdout, stderr, exitCode } = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--model", + model, + "--message", + "欢迎使用百炼。", + "--target-lang", + "English", + "--stream", + "--output", + "json", + ]); + expect(exitCode, `${model}: ${stderr}`).toBe(0); + const body = parseStdoutJson<{ content?: string }>(stdout); + expect(body.content, model).toBe("Hello world"); + } + }); + + test("loopback SSE delta mode for flash/lite appends fragments once", async () => { + for (const model of ["qwen-mt-flash", "qwen-mt-lite", "qwen-mt-flash-2025-01-01"]) { + const { origin } = await startChatServer((_request, response) => { + response.writeHead(200, { "content-type": "text/event-stream" }); + const frames = ["H", "ello", " world"]; + for (const content of frames) { + response.write( + `data: ${JSON.stringify({ + choices: [{ delta: { content }, finish_reason: null }], + })}\n\n`, + ); + } + response.write( + `data: ${JSON.stringify({ + choices: [{ delta: {}, finish_reason: "stop" }], + })}\n\n`, + ); + response.write("data: [DONE]\n\n"); + response.end(); + }); + + const { stdout, stderr, exitCode } = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--model", + model, + "--message", + "欢迎使用百炼。", + "--target-lang", + "English", + "--stream", + "--output", + "json", + ]); + expect(exitCode, `${model}: ${stderr}`).toBe(0); + const body = parseStdoutJson<{ content?: string }>(stdout); + expect(body.content, model).toBe("Hello world"); + } + }); + + test("loopback SSE snapshot in text mode writes each suffix once", async () => { + const { origin } = await startChatServer((_request, response) => { + response.writeHead(200, { "content-type": "text/event-stream" }); + const frames = ["H", "Hello", "Hello world"]; + for (const content of frames) { + response.write( + `data: ${JSON.stringify({ + choices: [{ delta: { content }, finish_reason: null }], + })}\n\n`, + ); + } + response.write( + `data: ${JSON.stringify({ + choices: [{ delta: {}, finish_reason: "stop" }], + })}\n\n`, + ); + response.write("data: [DONE]\n\n"); + response.end(); + }); + + const { stdout, stderr, exitCode } = await runCommandInProcess(TEXT_TRANSLATE_ROUTES, [ + "text", + "translate", + "--base-url", + origin, + "--api-key", + "sk-test", + "--model", + "qwen-mt-plus", + "--message", + "欢迎使用百炼。", + "--target-lang", + "English", + "--stream", + "--output", + "text", + ]); + expect(exitCode, stderr).toBe(0); + // Streaming writes suffixes; final result uses empty text so only a trailing newline is added. + expect(stdout).toBe("Hello world\n"); + }); +}); + +describe.skipIf(!isDashScopeE2EReady())("e2e: text translate live", () => { + test("qwen-mt-flash with term returns non-empty translation", async () => { + const { stdout, stderr, exitCode } = await runCommandE2e( + TEXT_TRANSLATE_ROUTES, + [ + "text", + "translate", + "--message", + "欢迎使用百炼。", + "--source-lang", + "Chinese", + "--target-lang", + "English", + "--term", + "百炼=Bailian", + "--output", + "json", + ], + { + BAILIAN_CONFIG_DIR: isolatedDirectory, + HOME: isolatedDirectory, + USERPROFILE: isolatedDirectory, + }, + ); + expect(exitCode, stderr).toBe(0); + const body = parseStdoutJson<{ + choices?: Array<{ message?: { content?: string | null } }>; + }>(stdout); + const content = body.choices?.[0]?.message?.content ?? ""; + expect(content.length).toBeGreaterThan(0); + }, 120_000); +}); diff --git a/packages/commands/tests/e2e/topic-routes.ts b/packages/commands/tests/e2e/topic-routes.ts index 89b2bf421..180c7eede 100644 --- a/packages/commands/tests/e2e/topic-routes.ts +++ b/packages/commands/tests/e2e/topic-routes.ts @@ -16,6 +16,11 @@ export const UPDATE_ROUTES: E2eRouteExports = { export const TEXT_CHAT_ROUTES: E2eRouteExports = { "text chat": "textChat" }; +export const TEXT_TRANSLATE_ROUTES: E2eRouteExports = { + "text translate": "textTranslate", + "text chat": "textChat", +}; + export const BATCH_ROUTES: E2eRouteExports = { "batch convert": "batchConvert", "batch create": "batchCreate", @@ -117,6 +122,12 @@ export const KNOWLEDGE_CHAT_ROUTES: E2eRouteExports = { export const IMAGE_ROUTES: E2eRouteExports = { "image generate": "imageGenerate", "image edit": "imageEdit", + "image translate": "imageTranslate", +}; + +export const IMAGE_TRANSLATE_ROUTES: E2eRouteExports = { + "image translate": "imageTranslate", + "video task get": "videoTaskGet", }; export const VIDEO_ROUTES: E2eRouteExports = { @@ -188,9 +199,16 @@ export const PIPELINE_ROUTES: E2eRouteExports = { export const OMNI_ROUTES: E2eRouteExports = { omni: "textOmni", + "omni translate": "omniTranslate", "speech synthesize": "speechSynthesize", }; +export const OMNI_TRANSLATE_ROUTES: E2eRouteExports = { + omni: "textOmni", + "omni translate": "omniTranslate", + "video task get": "videoTaskGet", +}; + export const FILE_UPLOAD_ROUTES: E2eRouteExports = { "file upload": "fileUpload", }; diff --git a/packages/commands/tests/shared/chat-completion-stream.test.ts b/packages/commands/tests/shared/chat-completion-stream.test.ts new file mode 100644 index 000000000..92c82bcf0 --- /dev/null +++ b/packages/commands/tests/shared/chat-completion-stream.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, test } from "vite-plus/test"; +import type { LocalizedText } from "bailian-cli-core"; +import { createChatCompletionStreamAccumulator } from "../../src/commands/shared/chat-completion-stream.ts"; + +const localize = (text: LocalizedText) => (typeof text === "string" ? text : text["en-US"]); + +function contentEvent(content: string, finishReason?: string) { + return { + data: JSON.stringify({ + choices: [ + { + delta: { content }, + ...(finishReason ? { finish_reason: finishReason } : {}), + }, + ], + }), + }; +} + +describe("chat-completion-stream contentMode", () => { + test("delta mode appends fragments", () => { + const accumulator = createChatCompletionStreamAccumulator(localize, { contentMode: "delta" }); + expect(accumulator.accept(contentEvent("H")).content).toBe("H"); + expect(accumulator.accept(contentEvent("ello")).content).toBe("ello"); + expect(accumulator.accept(contentEvent(" world", "stop")).content).toBe(" world"); + expect(accumulator.finish().content).toBe("Hello world"); + }); + + test("snapshot mode emits only new suffixes", () => { + const accumulator = createChatCompletionStreamAccumulator(localize, { + contentMode: "snapshot", + }); + expect(accumulator.accept(contentEvent("H")).content).toBe("H"); + expect(accumulator.accept(contentEvent("Hello")).content).toBe("ello"); + expect(accumulator.accept(contentEvent("Hello world")).content).toBe(" world"); + expect(accumulator.accept(contentEvent("Hello world")).content).toBe(""); + accumulator.accept({ data: "[DONE]" }); + expect(accumulator.finish().content).toBe("Hello world"); + }); + + test("snapshot mode rejects non-prefix snapshots", () => { + const accumulator = createChatCompletionStreamAccumulator(localize, { + contentMode: "snapshot", + }); + accumulator.accept(contentEvent("Hello")); + expect(() => accumulator.accept(contentEvent("Hi"))).toThrow(/protocol|snapshot/i); + }); +}); diff --git a/packages/core/src/client/endpoints.ts b/packages/core/src/client/endpoints.ts index cb014ee3d..e5b5686b2 100644 --- a/packages/core/src/client/endpoints.ts +++ b/packages/core/src/client/endpoints.ts @@ -101,6 +101,11 @@ export function imageSyncPath(): string { return "/api/v1/services/aigc/multimodal-generation/generation"; } +/** Native multimodal generation path (OCR, qwen-mt-uni, image sync). Same as imageSyncPath. */ +export function multimodalGenerationPath(): string { + return imageSyncPath(); +} + /** Legacy async text-to-image API (wan2.5/2.2/2.1-t2i, wanx-*-t2i). */ export function imageText2ImagePath(): string { return "/api/v1/services/aigc/text2image/image-synthesis"; diff --git a/packages/core/src/client/index.ts b/packages/core/src/client/index.ts index eef4d20c6..7ce385be6 100644 --- a/packages/core/src/client/index.ts +++ b/packages/core/src/client/index.ts @@ -5,6 +5,7 @@ export { chatPath, imagePath, imageSyncPath, + multimodalGenerationPath, imageText2ImagePath, image2ImagePath, knowledgeChatEndpoint, diff --git a/packages/core/src/types/api.ts b/packages/core/src/types/api.ts index 8ca1de651..95c6f91f8 100644 --- a/packages/core/src/types/api.ts +++ b/packages/core/src/types/api.ts @@ -31,6 +31,14 @@ export interface ChatResponseFormat { }; } +/** Qwen-MT translation_options for Chat Completions. */ +export interface TranslationOptions { + source_lang: string; + target_lang: string; + terms?: Array<{ source: string; target: string }>; + domains?: string; +} + export interface ChatRequest { model: string; messages: ChatMessage[]; @@ -47,6 +55,7 @@ export interface ChatRequest { audio?: { voice: string; format?: string }; stream_options?: { include_usage?: boolean }; response_format?: ChatResponseFormat; + translation_options?: TranslationOptions; } export interface ChatChoice { @@ -911,10 +920,49 @@ export interface DashScopeAsyncResponse { request_id: string; } +/** Image translation (qwen-mt-image) request body. */ +export interface ImageTranslationRequest { + model: string; + input: { + image_url: string; + source_lang: string; + target_lang: string; + ext?: { + domainHint?: string; + terminologies?: Array<{ src: string; tgt: string }>; + config?: { imageSegment?: boolean }; + }; + }; +} + +/** File multimodal translation (qwen-mt-uni) request body. */ +export interface QwenMtUniRequest { + model: string; + input: { + fileUrl: string; + target_lang: string; + source_lang?: string; + ext?: { + format_hint?: string; + domainHint?: string; + glossary?: Array<{ src: string; tgt: string }>; + sensitives?: string[]; + config?: { imageSegment?: boolean }; + }; + }; +} + +/** Business result envelope returned by qwen-mt-uni tasks. */ +export interface QwenMtUniOutputData { + FileType?: string; + TranslatedFileUrl?: string; + TranslatedTexts?: string | string[]; +} + export interface DashScopeTaskResponse { output: { task_id: string; - task_status: "PENDING" | "RUNNING" | "SUCCEEDED" | "FAILED" | "UNKNOWN"; + task_status: "PENDING" | "RUNNING" | "SUCCEEDED" | "FAILED" | "UNKNOWN" | "CANCELED"; finished?: boolean; task_metrics?: { TOTAL?: number; @@ -933,6 +981,14 @@ export interface DashScopeTaskResponse { results?: Array<{ url: string }>; // Video generation returns video_url video_url?: string; + // Image translation (qwen-mt-image) returns image_url + image_url?: string; + // qwen-mt-uni business envelope (exact casing) + Success?: boolean; + Code?: number | string; + Message?: string; + RequestId?: string; + Data?: QwenMtUniOutputData; submit_time?: string; scheduled_time?: string; end_time?: string; diff --git a/skills/bailian-cli/SKILL.md b/skills/bailian-cli/SKILL.md index 40e6bc4fb..0757bbcf3 100644 --- a/skills/bailian-cli/SKILL.md +++ b/skills/bailian-cli/SKILL.md @@ -11,11 +11,13 @@ description: >- Agent skill 安装/列表/更新/卸载(bl skill add|list|update|remove,百炼 skill registry)。 用户点名百炼 / DashScope / `bl`,或继续既有 `bl` 工作流时直接使用。 共享协议(consent / 版本预检 / 鉴权 / 错误上报)在 bailian-protocol;官方安装 `bl skill init`。 - 家族路由:生图/生视频/配音/语音合成/转写 → bailian-gen;精调/微调/训练/训练数据集 → bailian-finetune; + 家族路由:生图/生视频/配音/语音合成/转写/图片翻译/文档翻译/PDF 翻译 → bailian-gen;精调/微调/训练/训练数据集 → bailian-finetune; agents.yaml 托管 Agent → bailian-managed-agent;Sandbox 实例与模版 → bailian-sandbox; 个人长期记忆与应用记忆资源 → bailian-memory;个人历史资料辅助任务、资料接入与知识库管理 → bailian-knowledge; 联网搜索路由(默认模型搜索 vs 显式 `--mode mcp` + 兜底)→ bailian-web-search。 - 不要用于普通问答、编程、写作、翻译、摘要、泛搜索,或图片理解等宿主自己能做的任务(普通问答、编程、写作、翻译、摘要、泛搜索不触发)。 + 点名百炼 / Qwen-MT / `bl text translate` 的专用文本翻译走本技能。 + 不要用于普通问答、编程、写作、宿主能直接完成的纯文本翻译、摘要、泛搜索,或图片理解等宿主自己能做的任务 + (普通问答、编程、写作、宿主可完成的纯文本翻译、摘要、泛搜索不触发)。 未命名用量/额度问题:先问用户使用哪个产品,再运行 `bl usage` / `bl quota` 查询。 --- @@ -31,9 +33,9 @@ BAILIAN_CALLER_TYPE=agent BAILIAN_CALLER_NAME=codex BAILIAN_SKILL_NAME=bailian-c > **Family hub** — This skill owns Bailian resource commands and the hub `reference/` (apps, usage, auth, config, …). > Shared protocol → [`../bailian-protocol/SKILL.md`](../bailian-protocol/SKILL.md) (install the full family with `bl skill init`). -> Soft hand-offs by skill name (Read if installed; else `bl … --help` / prompt `bl skill init`): `bailian-gen` (media) · `bailian-finetune` (training) · `bailian-managed-agent` (agents.yaml IaC) · `bailian-sandbox` (Sandbox lifecycle) · `bailian-web-search` (web search routing) · `bailian-knowledge` (personal documents in everyday tasks and knowledge workflows). +> Soft hand-offs by skill name (Read if installed; else `bl … --help` / prompt `bl skill init`): `bailian-gen` (media / image·doc·PDF translation) · `bailian-finetune` (training) · `bailian-managed-agent` (agents.yaml IaC) · `bailian-sandbox` (Sandbox lifecycle) · `bailian-web-search` (web search routing) · `bailian-knowledge` (personal documents in everyday tasks and knowledge workflows). > Tasks that need the user’s own historical documents belong to `bailian-knowledge`; ordinary writing does not itself select a cloud provider. -> Do not invoke it for ordinary reasoning, coding, writing, translation, summarization, generic research, or image understanding the host agent can complete directly. +> Do not invoke it for ordinary reasoning, coding, writing, host-capable plain-text translation, summarization, generic research, or image understanding the host agent can complete directly. Named Bailian / Qwen-MT / `bl text translate` text translation stays here; image / document / PDF translation → `bailian-gen`. > > **Install (supported):** `bl skill init` @@ -69,6 +71,7 @@ Use this table only after the decision table in [`bailian-protocol`](../bailian- | User intent | Command | Notes | | ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | | Explicit Bailian model chat / text execution | `bl text chat` | Default `qwen3.8-max` | +| Dedicated text translation (Qwen-MT) | `bl text translate --target-lang ` | Default `qwen-mt-flash`; language names are English full names (`Chinese`/`English`), not `zh`/`en`; only user messages; see `reference/text.md` | | 百炼异步文件批量推理 / CSV 转换 | `bl batch convert` / `create` / `list` / `get` / `cancel` / `download` | CSV → JSONL → 异步任务 → 原始结果,可额外导出 CSV;格式样例见 `reference/batch.md` | | 百炼单次同步 Batch Chat | `bl text chat --batch` | 等待单次响应,不创建任务 ID;messages-file 是同一请求的对话上下文,边界见 `reference/text.md` | | Typed decision model inference | `bl decision` | Workspace endpoint; returns `choice` / `noul` / `score` values and probabilities, not generated text | diff --git a/skills/bailian-cli/reference/index.md b/skills/bailian-cli/reference/index.md index 7fbb0b575..7426ae811 100644 --- a/skills/bailian-cli/reference/index.md +++ b/skills/bailian-cli/reference/index.md @@ -121,6 +121,7 @@ Use this index for the skill-scoped quick index and global flags. | `bl skill remove` | No Auth | Remove locally installed skills (registry is untouched) | [skill.md](skill.md) | | `bl skill update` | No Auth | Update installed skills to the latest registry versions | [skill.md](skill.md) | | `bl text chat` | API Key | Send a text model request (OpenAI compatible, DashScope) | [text.md](text.md) | +| `bl text translate` | API Key | Translate text with Qwen-MT (Chat Completions translation_options) | [text.md](text.md) | | `bl token-plan add-member` | AK/SK | Add a member to a Token Plan organization | [token-plan.md](token-plan.md) | | `bl token-plan assign-seats` | AK/SK | Batch assign Token Plan seats to members | [token-plan.md](token-plan.md) | | `bl token-plan create-key` | AK/SK | Create a Token Plan API key for a seat | [token-plan.md](token-plan.md) | @@ -163,7 +164,7 @@ Use this index for the skill-scoped quick index and global flags. | `quota` | `check`, `delete`, `history`, `list`, `update` | [quota.md](quota.md) | | `search` | `web` | [search.md](search.md) | | `skill` | `add`, `init`, `list`, `remove`, `update` | [skill.md](skill.md) | -| `text` | `chat` | [text.md](text.md) | +| `text` | `chat`, `translate` | [text.md](text.md) | | `token-plan` | `add-member`, `assign-seats`, `create-key`, `harness-quota`, `list-seats` | [token-plan.md](token-plan.md) | | `update` | `(root)` | [update.md](update.md) | | `usage` | `coding-plan`, `free`, `freetier`, `stats`, `summary`, `token-plan` | [usage.md](usage.md) | diff --git a/skills/bailian-cli/reference/text.md b/skills/bailian-cli/reference/text.md index 24c651e72..43dee8f85 100644 --- a/skills/bailian-cli/reference/text.md +++ b/skills/bailian-cli/reference/text.md @@ -7,9 +7,10 @@ Index: [index.md](index.md) ## Commands in this group -| Command | Authentication | Description | -| -------------- | -------------- | -------------------------------------------------------- | -| `bl text chat` | API Key | Send a text model request (OpenAI compatible, DashScope) | +| Command | Authentication | Description | +| ------------------- | -------------- | ------------------------------------------------------------------ | +| `bl text chat` | API Key | Send a text model request (OpenAI compatible, DashScope) | +| `bl text translate` | API Key | Translate text with Qwen-MT (Chat Completions translation_options) | ## Command details @@ -52,6 +53,7 @@ Index: [index.md](index.md) - Prime mode requires an explicit model and currently supports only the Chat Completions API. - Prime endpoint: configured --base-url, DASHSCOPE_BASE_URL, or profile base_url takes precedence; otherwise workspace is resolved from --workspace-id, BAILIAN_WORKSPACE_ID, then config workspace_id. - --enable-search maps to the Chat Completions enable_search parameter and requires a model with built-in web search support; whitelisted models (qwen3.8-max) enable it by default unless --enable-search false is passed; for the Responses API use --tool '{"type":"web_search"}' instead. +- For dedicated Qwen-MT text translation, use `text translate` instead of this chat entry. #### Examples @@ -102,3 +104,44 @@ bl text chat --enable-search true --message "Latest news about Alibaba Cloud" ```bash bl text chat --prime --workspace-id --model glm-5.3-prime --message "Explain this code" ``` + +### `bl text translate` + +| Field | Value | +| ------------------ | --------------------------------------------------------------------------------------------- | +| **Name** | `text translate` | +| **Description** | Translate text with Qwen-MT (Chat Completions translation_options) | +| **Authentication** | API Key | +| **Usage** | `bl text translate (--message \| --messages-file ) --target-lang [flags]` | + +#### Flags + +| Flag | Type | Required | Description | +| ------------------------ | ------ | -------- | ------------------------------------------------------------------------------------------- | +| `--model ` | string | no | Qwen-MT model ID (default: qwen-mt-flash; qwen-mt-plus\|flash\|lite, optional dated suffix) | +| `--message ` | array | no | Text to translate (repeatable user messages; or use --messages-file) | +| `--messages-file ` | string | no | Messages array for one request (use - for stdin; user role only) | +| `--source-lang ` | string | no | Translation source language (English name; default: auto) | +| `--target-lang ` | string | yes | Translation target language (English name) | +| `--term ` | array | no | Terminology pair source=target (repeatable) | +| `--domain-hint ` | string | no | Translation domain hint in English (maps to domains) | +| `--max-tokens ` | number | no | Maximum tokens to generate (default: 4096) | +| `--temperature ` | number | no | Sampling temperature (0.0, 2.0] | +| `--top-p ` | number | no | Nucleus sampling threshold | +| `--stream` | switch | no | Stream response tokens (default: on in TTY) | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- Language names use English full names (Chinese/English), not zh/en codes. Only user messages are accepted. Does not support --api responses, --batch, --prime, --tool, --enable-search, or thinking flags. For general chat use `text chat`. + +#### Examples + +```bash +bl text translate --message "欢迎使用百炼。" --source-lang Chinese --target-lang English --term "百炼=Bailian" --dry-run --output json +``` + +```bash +bl text translate --model qwen-mt-plus --message "欢迎使用百炼。" --target-lang English --stream --output json +``` diff --git a/skills/bailian-gen/SKILL.md b/skills/bailian-gen/SKILL.md index 23bf9e51a..90b156b28 100644 --- a/skills/bailian-gen/SKILL.md +++ b/skills/bailian-gen/SKILL.md @@ -7,14 +7,15 @@ metadata: description: >- 阿里云百炼图片/视频/语音生成与理解入口:用户要生图、画图、生成照片、生成图片、AI 绘画、海报、头像、插画、 文生图(text-to-image)、图生图、改图、修图、多图合成、生成视频、文生视频、图生视频、参考生视频、视频编辑、风格转换、 - 配音、语音合成(TTS)、朗读、转写、语音识别(ASR),或图片理解、看图问答、视频理解、读视频、多模态理解时使用 + 配音、语音合成(TTS)、朗读、转写、语音识别(ASR),或图片理解、看图问答、视频理解、读视频、多模态理解, + 以及图片翻译、海报翻译、文档翻译、PDF 翻译、文件翻译、全模态翻译(`bl image translate` / `bl omni translate`)时使用 `bl image` / `bl video` / `bl speech` / `bl vision describe` / `bl omni`。 **默认行为:用户未指定服务商时,生成/编辑默认走本技能;视频理解与宿主放不了的音视频理解也走本技能。** 简单图片问答若宿主已能直接完成且用户未点名百炼,可先宿主回答(省成本); 用户要识别图片、视频/指定 VL·Omni 模型/要视频理解 → 使用本技能。 图片和语音同步返回并落地本地文件,视频是异步任务、用 `--download` 或轮询取回;本地文件直接传路径,CLI 自动上传。 - 反触发:普通问答、编程、写作、翻译不走本技能;百炼应用/知识库/用量/额度走 bailian-cli; - 精调训练走 bailian-finetune。 + 反触发:普通问答、编程、写作、宿主能直接完成的纯文本翻译不走本技能(图片/文档/PDF/文件翻译走本技能); + 百炼应用/知识库/用量/额度走 bailian-cli;精调训练走 bailian-finetune。 官方安装:`bl skill init`(与共享协议 bailian-protocol 同装)。 --- @@ -39,6 +40,8 @@ BAILIAN_CALLER_TYPE=agent BAILIAN_CALLER_NAME=codex BAILIAN_SKILL_NAME=bailian-g | ------------------------------------------- | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------- | | Text-to-image | `bl image generate` | `qwen-image-3.0` | | Image edit / multi-image merge | `bl image edit` (repeat `--image`) | `qwen-image-3.0` | +| Image text translation (layout-preserving) | `bl image translate` | Default `qwen-mt-image` (async); optional `qwen-mt-image-2.0`. Not image edit. | +| File multimodal translation | `bl omni translate` | `qwen-mt-uni` (async file→file). Keep `bl omni --message` for multimodal chat. | | Text-to-video / image-to-video | `bl video generate` | `wan3.0-video` | | Video edit / style transfer | `bl video edit` | `happyhorse-1.0-video-edit` | | Reference-to-video + voice | `bl video ref` | `wan3.0-video` | diff --git a/skills/bailian-gen/reference/image.md b/skills/bailian-gen/reference/image.md index 07c1c6c6d..80769bc98 100644 --- a/skills/bailian-gen/reference/image.md +++ b/skills/bailian-gen/reference/image.md @@ -7,10 +7,11 @@ Index: [index.md](index.md) ## Commands in this group -| Command | Authentication | Description | -| ------------------- | -------------- | -------------------------------------------------------------------- | -| `bl image edit` | API Key | Edit an existing image with text instructions (Qwen-Image / Wan 2.7) | -| `bl image generate` | API Key | Generate images (Qwen-Image / wan2.x) | +| Command | Authentication | Description | +| -------------------- | -------------- | -------------------------------------------------------------------- | +| `bl image edit` | API Key | Edit an existing image with text instructions (Qwen-Image / Wan 2.7) | +| `bl image generate` | API Key | Generate images (Qwen-Image / wan2.x) | +| `bl image translate` | API Key | Translate text in an image (Qwen-MT-Image) | ## Command details @@ -161,3 +162,47 @@ bl image generate --prompt "Pro quality" --model qwen-image-2.0-pro ```bash bl image generate --prompt "Product shots" --n 2 --concurrent 3 # 6 images in parallel ``` + +### `bl image translate` + +| Field | Value | +| ------------------ | ----------------------------------------------------------------------- | +| **Name** | `image translate` | +| **Description** | Translate text in an image (Qwen-MT-Image) | +| **Authentication** | API Key | +| **Usage** | `bl image translate --image --target-lang [flags]` | + +#### Flags + +| Flag | Type | Required | Description | +| --------------------------- | ------- | -------- | ------------------------------------------------------------------ | +| `--image ` | string | yes | Image URL or local file | +| `--source-lang ` | string | no | Source language name or code (default: auto) | +| `--target-lang ` | string | yes | Target language name or code | +| `--model ` | string | no | Model ID (default: qwen-mt-image; optional: qwen-mt-image-2.0) | +| `--domain-hint ` | string | no | English domain hint (maps to ext.domainHint) | +| `--term ` | array | no | Terminology pair source=target (repeatable; maps to terminologies) | +| `--image-segment ` | boolean | no | Skip translating text on image subjects (ext.config.imageSegment) | +| `--out-dir ` | string | no | Download directory | +| `--async` | switch | no | Return async task id without waiting | +| `--poll-interval ` | number | no | Polling interval when waiting (default: 5) | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- Always submits asynchronously. Use --async to return the task ID immediately, or wait and download the translated JPG. Query with `video task get --task-id`. Local files upload to temporary OSS (requires OssResourceResolve). + +#### Examples + +```bash +bl image translate --image ./poster.png --target-lang en --dry-run --output json +``` + +```bash +bl image translate --image https://example.com/poster.png --source-lang zh --target-lang en --async --output json +``` + +```bash +bl image translate --image https://example.com/poster.png --target-lang en --term "百炼=Bailian" --domain-hint "ecommerce" +``` diff --git a/skills/bailian-gen/reference/index.md b/skills/bailian-gen/reference/index.md index 841255a6e..a5a4b1671 100644 --- a/skills/bailian-gen/reference/index.md +++ b/skills/bailian-gen/reference/index.md @@ -13,7 +13,9 @@ Use this index for the skill-scoped quick index and global flags. | ----------------------------- | -------------- | -------------------------------------------------------------------------------------------------------------------- | ---------------------- | | `bl image edit` | API Key | Edit an existing image with text instructions (Qwen-Image / Wan 2.7) | [image.md](image.md) | | `bl image generate` | API Key | Generate images (Qwen-Image / wan2.x) | [image.md](image.md) | +| `bl image translate` | API Key | Translate text in an image (Qwen-MT-Image) | [image.md](image.md) | | `bl omni` | API Key | Multimodal chat with text + audio output (Qwen-Omni) | [omni.md](omni.md) | +| `bl omni translate` | API Key | Translate a document, image or audio file (Qwen-MT-Uni) | [omni.md](omni.md) | | `bl speech generate` | API Key | Generate podcast/dialogue audio content from a text prompt | [speech.md](speech.md) | | `bl speech recognize` | API Key | Recognize speech from audio files (FunAudio-ASR / Qwen-ASR Flash) | [speech.md](speech.md) | | `bl speech synthesize` | API Key | Synthesize speech from text | [speech.md](speech.md) | @@ -40,8 +42,8 @@ Use this index for the skill-scoped quick index and global flags. | Group | Commands | Reference | | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | -| `image` | `edit`, `generate` | [image.md](image.md) | -| `omni` | `(root)` | [omni.md](omni.md) | +| `image` | `edit`, `generate`, `translate` | [image.md](image.md) | +| `omni` | `(root)`, `translate` | [omni.md](omni.md) | | `speech` | `generate`, `recognize`, `synthesize`, `vocabulary create`, `vocabulary delete`, `vocabulary get`, `vocabulary list`, `vocabulary update`, `voice clone`, `voice delete`, `voice design`, `voice get`, `voice list`, `voice preview`, `voice update` | [speech.md](speech.md) | | `video` | `download`, `edit`, `generate`, `ref`, `task get` | [video.md](video.md) | | `vision` | `describe` | [vision.md](vision.md) | diff --git a/skills/bailian-gen/reference/omni.md b/skills/bailian-gen/reference/omni.md index 7dc733a12..37dee0ae4 100644 --- a/skills/bailian-gen/reference/omni.md +++ b/skills/bailian-gen/reference/omni.md @@ -7,9 +7,10 @@ Index: [index.md](index.md) ## Commands in this group -| Command | Authentication | Description | -| --------- | -------------- | ---------------------------------------------------- | -| `bl omni` | API Key | Multimodal chat with text + audio output (Qwen-Omni) | +| Command | Authentication | Description | +| ------------------- | -------------- | ------------------------------------------------------- | +| `bl omni` | API Key | Multimodal chat with text + audio output (Qwen-Omni) | +| `bl omni translate` | API Key | Translate a document, image or audio file (Qwen-MT-Uni) | ## Command details @@ -42,6 +43,10 @@ Index: [index.md](index.md) | `--api-key ` | string | no | API key | | `--base-url ` | string | no | API base URL | +#### Notes + +- For file translation (documents / images / audio), use `omni translate` instead of this multimodal chat entry. + #### Examples ```bash @@ -79,3 +84,41 @@ bl omni --message "Hello" --text-only --output json ```bash bl omni --message "Read this passage aloud" --audio-out greeting.wav ``` + +### `bl omni translate` + +| Field | Value | +| ------------------ | --------------------------------------------------------------------- | +| **Name** | `omni translate` | +| **Description** | Translate a document, image or audio file (Qwen-MT-Uni) | +| **Authentication** | API Key | +| **Usage** | `bl omni translate --file --target-lang [flags]` | + +#### Flags + +| Flag | Type | Required | Description | +| --------------------------- | ------ | -------- | ------------------------------------------------------- | +| `--file ` | string | yes | Document, image or audio URL/local file | +| `--target-lang ` | string | yes | Target language code or name | +| `--source-lang ` | string | no | Source language (omit to auto-detect; do not send auto) | +| `--format-hint ` | string | no | Format hint when URL has no extension (e.g. pdf, image) | +| `--domain-hint ` | string | no | English domain hint ≤200 words (ext.domainHint) | +| `--out-dir ` | string | no | Download directory | +| `--async` | switch | no | Return async task id without waiting | +| `--poll-interval ` | number | no | Polling interval when waiting (default: 5) | +| `--api-key ` | string | no | API key | +| `--base-url ` | string | no | API base URL | + +#### Notes + +- Always submits asynchronously. Use --async to return the task ID immediately. Task SUCCEEDED still requires output.Success=true before download. Query with `video task get --task-id`. Supported: pdf/docx/pptx/xlsx/txt/html/md/png/jpg/mp3/wav. Video and legacy doc/ppt are rejected. + +#### Examples + +```bash +bl omni translate --file ./brochure.pdf --target-lang en --dry-run --output json +``` + +```bash +bl omni translate --file https://example.com/brochure.pdf --target-lang en --format-hint pdf --async --output json +``` From bbe7edb9f17c2a221895459e274391a0b75f6809 Mon Sep 17 00:00:00 2001 From: clh02467605 Date: Fri, 20 Nov 2026 18:41:49 +0800 Subject: [PATCH 3/3] fix(e2e): skip hybrid leaf prefixes in deriveGroupPaths --- packages/e2e/src/registry-smoke.ts | 8 ++++++-- packages/e2e/tests/registry-smoke.test.ts | 14 +++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/e2e/src/registry-smoke.ts b/packages/e2e/src/registry-smoke.ts index 6b91aa79f..c9e25084a 100644 --- a/packages/e2e/src/registry-smoke.ts +++ b/packages/e2e/src/registry-smoke.ts @@ -1,13 +1,17 @@ /** - * 从产品 commands map 的 path key 推导有子命令的分组前缀。 - * 供 cli / kscli registry smoke 共用。 + * Derive command-group prefixes from product command-map keys. + * Shared by cli / kscli registry smoke. Skip prefixes that are themselves + * registered leaves (hybrid nodes such as `omni` + `omni translate`): registry + * help at that path renders the leaf, not `Usage: … [flags]`. */ export function deriveGroupPaths(commandPaths: string[]): string[] { + const registeredLeaves = new Set(commandPaths); const groups = new Set(); for (const path of commandPaths) { const parts = path.split(" "); for (let partIndex = 1; partIndex < parts.length; partIndex++) { const prefix = parts.slice(0, partIndex).join(" "); + if (registeredLeaves.has(prefix)) continue; const hasChildren = commandPaths.some( (candidate) => candidate.startsWith(`${prefix} `) && candidate !== prefix, ); diff --git a/packages/e2e/tests/registry-smoke.test.ts b/packages/e2e/tests/registry-smoke.test.ts index 4209ac250..657a7e3e1 100644 --- a/packages/e2e/tests/registry-smoke.test.ts +++ b/packages/e2e/tests/registry-smoke.test.ts @@ -1,5 +1,5 @@ import { expect, test } from "vite-plus/test"; -import { captureRegistryHelp } from "../src/registry-smoke.ts"; +import { captureRegistryHelp, deriveGroupPaths } from "../src/registry-smoke.ts"; test("captureRegistryHelp 捕获指定命令路径的非 TTY 输出", () => { const requestedPaths: string[][] = []; @@ -15,3 +15,15 @@ test("captureRegistryHelp 捕获指定命令路径的非 TTY 输出", () => { expect(output).toBe("Usage: bl text chat"); expect(requestedPaths).toEqual([["text", "chat"]]); }); + +test("deriveGroupPaths skips hybrid leaf prefixes that also have children", () => { + expect( + deriveGroupPaths([ + "omni", + "omni translate", + "text chat", + "text translate", + "speech voice list", + ]), + ).toEqual(["speech", "speech voice", "text"]); +});