mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 06:54:28 +00:00
feat(video): add video transcripts (#559)
* feat(video): generate transcripts with the local embedding server * feat(video): show saved transcripts in the player * fix(video): preserve transcript seek timestamps * fix(video): refine transcript viewing and generation * feat(video): resize transcripts and follow playback earlier * fix(video): animate transcript playback scrolling * fix(video): transcribe from the smallest full proxy * fix(video): refine the sync to playback button * feat(video): edit transcript segment text inline * fix(video): keep transcript segment highlighted over edit button * docs(video): note embedding server version for transcripts
This commit is contained in:
@@ -13,9 +13,11 @@ describe('authn api', () => {
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllEnvs()
|
||||
})
|
||||
|
||||
it('GET /signup-info', async () => {
|
||||
vi.stubEnv('EMBEDDING_PROVIDER', 'gemini-embedding-2')
|
||||
mockGetSignupInfo.mockResolvedValue({
|
||||
initialized: true,
|
||||
demoMode: false,
|
||||
@@ -29,6 +31,16 @@ describe('authn api', () => {
|
||||
initialized: true,
|
||||
demoMode: false,
|
||||
archiveStorageEnabled: false,
|
||||
transcriptionEnabled: false,
|
||||
})
|
||||
})
|
||||
|
||||
it('advertises transcription with the local embedding provider', async () => {
|
||||
vi.stubEnv('EMBEDDING_PROVIDER', 'gemma-embedding-2-openai')
|
||||
mockGetSignupInfo.mockResolvedValue({ initialized: true, demoMode: false })
|
||||
|
||||
const res = await new Hono().route('/', authnRoute).request('/signup-info')
|
||||
|
||||
expect(await res.json()).toMatchObject({ transcriptionEnabled: true })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -4,6 +4,7 @@ import { userService } from '@shumai/core/src/user/user'
|
||||
import { auth } from '@shumai/core/src/auth/auth'
|
||||
import { createGuestToken } from '@shumai/core/src/auth/guest-token'
|
||||
import { isArchiveStorageEnabled } from '@shumai/core/src/s3/archive-storage'
|
||||
import { isLocalTranscriptionEnabled } from '@shumai/core/src/transcript/local-transcription'
|
||||
import { z } from 'zod'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { ulid } from 'ulid'
|
||||
@@ -24,6 +25,7 @@ const route = app
|
||||
initialized: info.initialized,
|
||||
demoMode: info.demoMode,
|
||||
archiveStorageEnabled: isArchiveStorageEnabled(),
|
||||
transcriptionEnabled: isLocalTranscriptionEnabled(),
|
||||
})
|
||||
})
|
||||
.get('/me', async (c) => {
|
||||
|
||||
@@ -31,6 +31,7 @@ import versionStackRoute from './versionStack'
|
||||
import kanbanRoute from './kanban'
|
||||
import mediaGenerationRoute from './media-generation'
|
||||
import runnerWsRoute, { runnerWebsocket } from './runner-ws'
|
||||
import transcriptRoute from './transcript'
|
||||
|
||||
export { runnerWebsocket }
|
||||
|
||||
@@ -72,6 +73,7 @@ const apiRoute = new Hono<{ Variables: { user: User } }>()
|
||||
.route('/', teamRoute)
|
||||
.route('/', versionStackRoute)
|
||||
.route('/', fileRoute)
|
||||
.route('/', transcriptRoute)
|
||||
.route('/', folderRoute)
|
||||
.route('/', agentRoute)
|
||||
.route('/', chatRoute)
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { transcriptService } from '@shumai/core/src/transcript/transcript'
|
||||
import { Hono } from 'hono'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import transcriptRoute from './transcript'
|
||||
|
||||
describe('Transcript API', () => {
|
||||
const app = new Hono().route('/', transcriptRoute)
|
||||
const submit = (body: { language?: string }) =>
|
||||
app.request('/files/file-id/transcripts', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
const edit = (body: object, language = 'en') =>
|
||||
app.request(`/files/file-id/transcripts/${language}/segments/segment-id`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
afterEach(() => vi.restoreAllMocks())
|
||||
|
||||
it('proxies the server language list', async () => {
|
||||
const list = { model: 'whisper-tiny', languages: [{ code: 'en', name: 'English' }] }
|
||||
vi.spyOn(transcriptService, 'listLanguages').mockResolvedValue(list)
|
||||
const response = await app.request('/transcription/languages')
|
||||
expect(await response.json()).toEqual(list)
|
||||
})
|
||||
|
||||
it('returns saved languages and segments with asset read access', async () => {
|
||||
const permission = vi.spyOn(authzService, 'hasPermission').mockResolvedValue(undefined)
|
||||
const list = vi
|
||||
.spyOn(transcriptService, 'listSavedLanguages')
|
||||
.mockResolvedValue({ languages: ['en'] })
|
||||
const saved = {
|
||||
id: 'transcript-id',
|
||||
language: 'en',
|
||||
segments: [{ id: 'segment-id', startMs: 0, endMs: 1400, text: 'Hello' }],
|
||||
}
|
||||
const get = vi.spyOn(transcriptService, 'getSavedTranscript').mockResolvedValue(saved)
|
||||
|
||||
const languages = await app.request('/files/file-id/transcripts')
|
||||
expect(languages.status).toBe(200)
|
||||
expect(await languages.json()).toEqual({ languages: ['en'] })
|
||||
expect(list).toHaveBeenCalledWith('file-id')
|
||||
const transcript = await app.request('/files/file-id/transcripts/en')
|
||||
expect(transcript.status).toBe(200)
|
||||
expect(await transcript.json()).toEqual(saved)
|
||||
expect(get).toHaveBeenCalledWith('file-id', 'en')
|
||||
expect(permission).toHaveBeenCalledTimes(2)
|
||||
for (const [request] of permission.mock.calls) {
|
||||
expect(request).toMatchObject({
|
||||
type: ResourceType.Asset,
|
||||
permission: Permission.Read,
|
||||
id: 'file-id',
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
it('validates the saved language path and authorizes before reading saved data', async () => {
|
||||
const permission = vi
|
||||
.spyOn(authzService, 'hasPermission')
|
||||
.mockRejectedValue(new HTTPException(403))
|
||||
const list = vi.spyOn(transcriptService, 'listSavedLanguages')
|
||||
const get = vi.spyOn(transcriptService, 'getSavedTranscript')
|
||||
|
||||
expect((await app.request('/files/file-id/transcripts/auto')).status).toBe(400)
|
||||
expect((await app.request('/files/file-id/transcripts')).status).toBe(403)
|
||||
expect((await app.request('/files/file-id/transcripts/en')).status).toBe(403)
|
||||
expect(list).not.toHaveBeenCalled()
|
||||
expect(get).not.toHaveBeenCalled()
|
||||
|
||||
permission.mockResolvedValue(undefined)
|
||||
get.mockRejectedValue(new HTTPException(404))
|
||||
expect((await app.request('/files/file-id/transcripts/zh')).status).toBe(404)
|
||||
})
|
||||
|
||||
it('requires asset edit permission and accepts an empty segment', async () => {
|
||||
const permission = vi.spyOn(authzService, 'hasPermission').mockResolvedValue(undefined)
|
||||
const segment = { id: 'segment-id', startMs: 0, endMs: 1400, text: '' }
|
||||
const update = vi.spyOn(transcriptService, 'updateSegmentText').mockResolvedValue(segment)
|
||||
|
||||
const response = await edit({ text: '' })
|
||||
expect(response.status).toBe(200)
|
||||
expect(await response.json()).toEqual(segment)
|
||||
expect(update).toHaveBeenCalledWith('file-id', 'en', 'segment-id', '')
|
||||
expect(permission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: ResourceType.Asset,
|
||||
permission: Permission.Edit,
|
||||
id: 'file-id',
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('rejects invalid text and timing fields before handling segment edits', async () => {
|
||||
const permission = vi.spyOn(authzService, 'hasPermission')
|
||||
const update = vi.spyOn(transcriptService, 'updateSegmentText')
|
||||
for (const body of [
|
||||
{},
|
||||
{ text: null },
|
||||
{ text: 'invalid\0text' },
|
||||
{ text: 'Hello', startMs: 1 },
|
||||
{ text: 'Hello', endMs: 1 },
|
||||
]) {
|
||||
expect((await edit(body)).status).toBe(400)
|
||||
}
|
||||
expect((await edit({ text: 'Hello' }, 'auto')).status).toBe(400)
|
||||
expect(permission).not.toHaveBeenCalled()
|
||||
expect(update).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not edit for a read-only user and forwards missing segment errors', async () => {
|
||||
const permission = vi
|
||||
.spyOn(authzService, 'hasPermission')
|
||||
.mockRejectedValue(new HTTPException(403))
|
||||
const update = vi
|
||||
.spyOn(transcriptService, 'updateSegmentText')
|
||||
.mockRejectedValue(new HTTPException(404))
|
||||
expect((await edit({ text: 'Hello' })).status).toBe(403)
|
||||
expect(update).not.toHaveBeenCalled()
|
||||
|
||||
permission.mockResolvedValue(undefined)
|
||||
expect((await edit({ text: 'Hello' })).status).toBe(404)
|
||||
})
|
||||
|
||||
it('requires asset edit permission and returns a queued Auto job', async () => {
|
||||
const permission = vi.spyOn(authzService, 'hasPermission').mockResolvedValue(undefined)
|
||||
const generate = vi
|
||||
.spyOn(transcriptService, 'generate')
|
||||
.mockResolvedValue({ kind: 'queued', taskId: 'task-id' })
|
||||
|
||||
const response = await submit({})
|
||||
|
||||
expect(response.status).toBe(202)
|
||||
expect(await response.json()).toEqual({ kind: 'queued', taskId: 'task-id' })
|
||||
expect(generate).toHaveBeenCalledWith('file-id', null)
|
||||
expect(permission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: ResourceType.Asset,
|
||||
permission: Permission.Edit,
|
||||
id: 'file-id',
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('returns a saved transcript for an explicit language', async () => {
|
||||
vi.spyOn(authzService, 'hasPermission').mockResolvedValue(undefined)
|
||||
const saved = { kind: 'existing', transcriptId: 'transcript-id', language: 'en' } as const
|
||||
vi.spyOn(transcriptService, 'generate').mockResolvedValue(saved)
|
||||
|
||||
const response = await submit({ language: 'en' })
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(await response.json()).toEqual(saved)
|
||||
expect(transcriptService.generate).toHaveBeenCalledWith('file-id', 'en')
|
||||
})
|
||||
|
||||
it('rejects invalid language codes and denies reads and writes without edit permission', async () => {
|
||||
vi.spyOn(authzService, 'hasPermission').mockRejectedValue(new HTTPException(403))
|
||||
const generate = vi.spyOn(transcriptService, 'generate')
|
||||
const status = vi.spyOn(transcriptService, 'getStatus')
|
||||
|
||||
expect((await submit({ language: 'auto' })).status).toBe(400)
|
||||
expect((await submit({ language: 'en' })).status).toBe(403)
|
||||
expect((await app.request('/files/file-id/transcription-status')).status).toBe(403)
|
||||
expect(generate).not.toHaveBeenCalled()
|
||||
expect(status).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,90 @@
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { transcriptService } from '@shumai/core/src/transcript/transcript'
|
||||
import type { User } from '@shumai/db'
|
||||
import {
|
||||
generateTranscriptRequestSchema,
|
||||
transcriptLanguageCodeSchema,
|
||||
updateTranscriptSegmentRequestSchema,
|
||||
} from '@shumai/dtos'
|
||||
import { Hono } from 'hono'
|
||||
import { z } from 'zod'
|
||||
|
||||
const route = new Hono<{ Variables: { user: User } }>()
|
||||
.get('/transcription/languages', async (c) => c.json(await transcriptService.listLanguages()))
|
||||
.get('/files/:fileId/transcripts', async (c) => {
|
||||
const assetId = c.req.param('fileId')
|
||||
await authzService.hasPermission({
|
||||
user: c.get('user'),
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: assetId,
|
||||
})
|
||||
return c.json(await transcriptService.listSavedLanguages(assetId))
|
||||
})
|
||||
.get(
|
||||
'/files/:fileId/transcripts/:language',
|
||||
zValidator('param', z.object({ fileId: z.string(), language: transcriptLanguageCodeSchema })),
|
||||
async (c) => {
|
||||
const { fileId, language } = c.req.valid('param')
|
||||
await authzService.hasPermission({
|
||||
user: c.get('user'),
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: fileId,
|
||||
})
|
||||
return c.json(await transcriptService.getSavedTranscript(fileId, language))
|
||||
},
|
||||
)
|
||||
.patch(
|
||||
'/files/:fileId/transcripts/:language/segments/:segmentId',
|
||||
zValidator(
|
||||
'param',
|
||||
z.object({
|
||||
fileId: z.string(),
|
||||
language: transcriptLanguageCodeSchema,
|
||||
segmentId: z.string(),
|
||||
}),
|
||||
),
|
||||
zValidator('json', updateTranscriptSegmentRequestSchema),
|
||||
async (c) => {
|
||||
const { fileId, language, segmentId } = c.req.valid('param')
|
||||
await authzService.hasPermission({
|
||||
user: c.get('user'),
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id: fileId,
|
||||
})
|
||||
const { text } = c.req.valid('json')
|
||||
return c.json(await transcriptService.updateSegmentText(fileId, language, segmentId, text))
|
||||
},
|
||||
)
|
||||
.post(
|
||||
'/files/:fileId/transcripts',
|
||||
zValidator('json', generateTranscriptRequestSchema),
|
||||
async (c) => {
|
||||
const assetId = c.req.param('fileId')
|
||||
await authzService.hasPermission({
|
||||
user: c.get('user'),
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id: assetId,
|
||||
})
|
||||
|
||||
const { language } = c.req.valid('json')
|
||||
const result = await transcriptService.generate(assetId, language ?? null)
|
||||
return c.json(result, result.kind === 'queued' || result.kind === 'busy' ? 202 : 200)
|
||||
},
|
||||
)
|
||||
.get('/files/:fileId/transcription-status', async (c) => {
|
||||
const assetId = c.req.param('fileId')
|
||||
await authzService.hasPermission({
|
||||
user: c.get('user'),
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id: assetId,
|
||||
})
|
||||
return c.json(await transcriptService.getStatus(assetId))
|
||||
})
|
||||
|
||||
export default route
|
||||
Reference in New Issue
Block a user