feat(transcript): add Chinese script choices

This commit is contained in:
yiling
2026-12-05 11:43:32 +08:00
parent 6c74413375
commit 70e4c63323
19 changed files with 242 additions and 29 deletions
@@ -2,9 +2,23 @@ import {
embeddingServerService,
LOCAL_EMBEDDING_PROVIDER,
} from '@shumai/core/src/search/embedding-server'
import { transcriptLanguageCodeSchema, transcriptLanguagesSchema } from '@shumai/dtos'
import {
chineseTranscriptLanguageSchema,
transcriptionProviderLanguageCodeSchema,
} from '@shumai/dtos'
import { z } from 'zod'
const transcriptionProviderLanguagesSchema = z.object({
model: z.string().min(1),
languages: z.array(
z.object({ code: transcriptionProviderLanguageCodeSchema, name: z.string().min(1) }),
),
})
export function getTranscriptionProviderLanguage(language: string | null) {
return chineseTranscriptLanguageSchema.options.some((code) => code === language) ? 'zh' : language
}
export async function isLocalTranscriptionEnabled(teamId?: string): Promise<boolean> {
const { provider } = await embeddingServerService.resolveRuntimeConfig(teamId)
return provider === LOCAL_EMBEDDING_PROVIDER
@@ -16,12 +30,12 @@ export async function listLocalTranscriptionLanguages(teamId?: string) {
signal: AbortSignal.timeout(5_000),
})
if (!response.ok) throw new Error(`Transcription languages endpoint returned ${response.status}`)
return transcriptLanguagesSchema.parse(await response.json())
return transcriptionProviderLanguagesSchema.parse(await response.json())
}
const transcriptResponseSchema = z
.object({
language: transcriptLanguageCodeSchema.nullable(),
language: transcriptionProviderLanguageCodeSchema.nullable(),
segments: z.array(
z
.object({ start: z.number().min(0), end: z.number().min(0), text: z.string().min(1) })
@@ -144,7 +144,14 @@ describe('TranscriptService', () => {
})
it('validates provider capability, language and asset eligibility before queuing', async () => {
await expect(transcriptService.listLanguages()).resolves.toMatchObject({ model })
await expect(transcriptService.listLanguages()).resolves.toEqual({
model,
languages: [
{ code: 'en', name: 'English' },
{ code: 'zh-Hans', name: 'Chinese (Simplified)' },
{ code: 'zh-Hant', name: 'Chinese (Traditional)' },
],
})
await expect(transcriptService.generate(assetId, 'xx')).rejects.toMatchObject({ status: 400 })
await prisma.asset.update({ where: { id: assetId }, data: { mediaType: 'image/png' } })
await expect(transcriptService.generate(assetId, 'en')).rejects.toMatchObject({ status: 400 })
@@ -155,6 +162,20 @@ describe('TranscriptService', () => {
expect(await prisma.workflowTask.count({ where: { assetId } })).toBe(0)
})
it('only offers Chinese scripts when the provider supports Chinese', async () => {
const list = { model, languages: [{ code: 'en', name: 'English' }] }
vi.mocked(fetch).mockImplementation(async () => Response.json(list))
await expect(transcriptService.listLanguages()).resolves.toEqual(list)
await expect(transcriptService.generate(assetId, 'zh-Hans')).rejects.toMatchObject({
status: 400,
})
await expect(transcriptService.generate(assetId, 'zh-Hant')).rejects.toMatchObject({
status: 400,
})
expect(await prisma.workflowTask.count({ where: { assetId } })).toBe(0)
})
it('requires an enabled Transcript Agent before contacting the provider or queuing', async () => {
vi.stubEnv('ENABLE_TRANSCRIPTION_AGENT', 'false')
await expect(transcriptService.isGenerationEnabled()).resolves.toBe(false)
+25 -3
View File
@@ -8,6 +8,7 @@ import {
type WorkflowTask,
} from '@shumai/db'
import {
chineseTranscriptLanguageSchema,
selectTranscriptVideoProxyKey,
transcriptionErrors,
transcriptVideoStatuses,
@@ -18,13 +19,22 @@ import {
type TranscriptSegment,
} from '@shumai/dtos'
import { HTTPException } from 'hono/http-exception'
import { isLocalTranscriptionEnabled, listLocalTranscriptionLanguages } from './local-transcription'
import {
getTranscriptionProviderLanguage,
isLocalTranscriptionEnabled,
listLocalTranscriptionLanguages,
} from './local-transcription'
const type = WorkflowTaskType.ai_transcription
const transcriptSelection = { id: true, language: true } as const
const segmentSelection = { id: true, startMs: true, endMs: true, text: true } as const
const chineseTranscriptLanguageNames = {
'zh-Hans': 'Chinese (Simplified)',
'zh-Hant': 'Chinese (Traditional)',
}
type Segment = { startMs: number; endMs: number; text: string }
export class TranscriptService {
@@ -46,7 +56,18 @@ export class TranscriptService {
async listLanguages() {
await this.requireEnabled()
return this.loadLanguages()
const { model, languages } = await this.loadLanguages()
return {
model,
languages: languages.flatMap((language) =>
language.code === 'zh'
? chineseTranscriptLanguageSchema.options.map((code) => ({
code,
name: chineseTranscriptLanguageNames[code],
}))
: [language],
),
}
}
private async loadLanguages(teamId?: string) {
@@ -111,7 +132,8 @@ export class TranscriptService {
if (saved) return saved
const { model, languages } = await this.loadLanguages(teamId)
if (language && !languages.some((entry) => entry.code === language)) {
const providerLanguage = getTranscriptionProviderLanguage(language)
if (providerLanguage && !languages.some((entry) => entry.code === providerLanguage)) {
throw new HTTPException(400, { message: 'Unsupported transcription language' })
}