2026-12-04 18:54:29 +08:00
|
|
|
import {
|
|
|
|
|
embeddingServerService,
|
|
|
|
|
LOCAL_EMBEDDING_PROVIDER,
|
|
|
|
|
} from '@shumai/core/src/search/embedding-server'
|
2026-12-04 13:49:25 +08:00
|
|
|
import { transcriptLanguageCodeSchema, transcriptLanguagesSchema } from '@shumai/dtos'
|
|
|
|
|
import { z } from 'zod'
|
|
|
|
|
|
2026-12-04 18:54:29 +08:00
|
|
|
export async function isLocalTranscriptionEnabled(teamId?: string): Promise<boolean> {
|
|
|
|
|
const { provider } = await embeddingServerService.resolveRuntimeConfig(teamId)
|
|
|
|
|
return provider === LOCAL_EMBEDDING_PROVIDER
|
2026-12-04 13:49:25 +08:00
|
|
|
}
|
|
|
|
|
|
2026-12-04 18:54:29 +08:00
|
|
|
export async function listLocalTranscriptionLanguages(teamId?: string) {
|
|
|
|
|
const { transcriptionEndpoint } = await embeddingServerService.resolveRuntimeConfig(teamId)
|
|
|
|
|
const response = await fetch(`${transcriptionEndpoint}/languages`, {
|
2026-12-04 13:49:25 +08:00
|
|
|
signal: AbortSignal.timeout(5_000),
|
|
|
|
|
})
|
|
|
|
|
if (!response.ok) throw new Error(`Transcription languages endpoint returned ${response.status}`)
|
|
|
|
|
return transcriptLanguagesSchema.parse(await response.json())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const transcriptResponseSchema = z
|
|
|
|
|
.object({
|
|
|
|
|
language: transcriptLanguageCodeSchema.nullable(),
|
|
|
|
|
segments: z.array(
|
|
|
|
|
z
|
|
|
|
|
.object({ start: z.number().min(0), end: z.number().min(0), text: z.string().min(1) })
|
|
|
|
|
.refine((segment) => segment.end >= segment.start && segment.end < 2_147_483),
|
|
|
|
|
),
|
|
|
|
|
})
|
|
|
|
|
.refine((result) => result.language !== null || result.segments.length === 0)
|
|
|
|
|
|
|
|
|
|
export class LocalTranscriptionError extends Error {
|
|
|
|
|
constructor(readonly status: number) {
|
|
|
|
|
super(`Transcription server returned HTTP ${status}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function transcribeLocalAudio({
|
2026-12-04 18:54:29 +08:00
|
|
|
teamId,
|
2026-12-04 13:49:25 +08:00
|
|
|
audio,
|
|
|
|
|
language,
|
|
|
|
|
model,
|
|
|
|
|
signal,
|
|
|
|
|
}: {
|
2026-12-04 18:54:29 +08:00
|
|
|
teamId?: string
|
2026-12-04 13:49:25 +08:00
|
|
|
audio: Blob
|
|
|
|
|
language: string | null
|
|
|
|
|
model: string
|
|
|
|
|
signal?: AbortSignal
|
|
|
|
|
}) {
|
|
|
|
|
const form = new FormData()
|
|
|
|
|
form.set('file', audio, 'audio.wav')
|
|
|
|
|
form.set('model', model)
|
|
|
|
|
form.set('response_format', 'verbose_json')
|
|
|
|
|
if (language) form.set('language', language)
|
|
|
|
|
|
|
|
|
|
const timeout = AbortSignal.timeout(90 * 60 * 1000)
|
2026-12-04 18:54:29 +08:00
|
|
|
const { transcriptionEndpoint } = await embeddingServerService.resolveRuntimeConfig(teamId)
|
|
|
|
|
const response = await fetch(transcriptionEndpoint, {
|
2026-12-04 13:49:25 +08:00
|
|
|
method: 'POST',
|
|
|
|
|
body: form,
|
|
|
|
|
redirect: 'error',
|
|
|
|
|
timeout: false,
|
|
|
|
|
signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
|
|
|
|
|
})
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
await response.body?.cancel()
|
|
|
|
|
throw new LocalTranscriptionError(response.status)
|
|
|
|
|
}
|
|
|
|
|
return transcriptResponseSchema.parse(await response.json())
|
|
|
|
|
}
|