mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 22:14:50 +00:00
feat(video): generate transcripts with the local embedding server
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,69 @@
|
||||
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),
|
||||
})
|
||||
|
||||
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('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,38 @@
|
||||
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 } from '@shumai/dtos'
|
||||
import { Hono } from 'hono'
|
||||
|
||||
const route = new Hono<{ Variables: { user: User } }>()
|
||||
.get('/transcription/languages', async (c) => c.json(await transcriptService.listLanguages()))
|
||||
.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