Add an AIGen-only generate API for external tools.

Cursor and similar clients can enqueue Studio 2 stills with the same steps and CFG the bench uses, through session or AIGEN_API_KEY.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-10-26 19:05:28 -05:00
co-authored by Cursor
parent 68469d9c71
commit 2b603b1d58
12 changed files with 399 additions and 9 deletions
+38
View File
@@ -0,0 +1,38 @@
import type { H3Event } from 'h3'
import { assertLibraryOwner } from '~/server/utils/library'
import {
aigenBearerAuthorized,
aigenInstanceOwnerKey,
assertAigenFeature
} from '~/server/utils/aigenApiAuth'
export {
aigenApiKeyConfigured,
aigenApiPathAllowed,
aigenBearerAuthorized,
aigenInstanceOwnerKey,
assertAigenFeature
} from '~/server/utils/aigenApiAuth'
export { defaultLibraryFolderId as defaultAigenFolderId } from '~/shared/studio2/engine-defaults.mjs'
/**
* Session (assertLibraryOwner) or Authorization: Bearer <AIGEN_API_KEY>.
* Bearer runs as the instance library owner. xAIGen is 404.
*/
export function assertAigenApiAccess(event: H3Event) {
assertAigenFeature()
if (aigenBearerAuthorized(event)) {
// libraryOwnerKey already maps this bearer to aigenInstanceOwnerKey()
const { owner, catalog } = assertLibraryOwner(event)
return { owner, catalog, auth: 'bearer' as const, instanceOwner: aigenInstanceOwnerKey() }
}
try {
const { owner, catalog } = assertLibraryOwner(event)
return { owner, catalog, auth: 'session' as const }
} catch (e: any) {
if (Number(e?.statusCode) === 401) {
throw createError({ statusCode: 401, statusMessage: 'Authentication required' })
}
throw e
}
}
+52
View File
@@ -0,0 +1,52 @@
import { createHash, timingSafeEqual } from 'node:crypto'
import type { H3Event } from 'h3'
import { isXaigenStudio } from '~/utils/videoModels'
import { passwordAuthEnabled, privateInstanceId } from '~/server/utils/localAuth'
function configuredApiKey() {
const config = useRuntimeConfig()
return String((config as { aigenApiKey?: string }).aigenApiKey || process.env.AIGEN_API_KEY || '').trim()
}
function safeEqual(left: string, right: string) {
const a = Buffer.from(left)
const b = Buffer.from(right)
if (a.length !== b.length) return false
try {
return timingSafeEqual(a, b)
} catch {
return false
}
}
/** Owner key for bearer calls — same catalog as a no-session / password instance. */
export function aigenInstanceOwnerKey() {
if (passwordAuthEnabled()) return privateInstanceId()
return createHash('sha256').update('local').digest('hex').slice(0, 24)
}
export function aigenApiKeyConfigured() {
return Boolean(configuredApiKey())
}
export function aigenBearerAuthorized(event: H3Event) {
if (isXaigenStudio()) return false
const expected = configuredApiKey()
if (!expected) return false
const header = String(getHeader(event, 'authorization') || '')
const match = header.match(/^Bearer\s+(.+)$/i)
if (!match) return false
return safeEqual(match[1].trim(), expected)
}
export function aigenApiPathAllowed(path: string, method: string) {
if (path === '/api/aigen' || path.startsWith('/api/aigen/')) return true
if (method === 'GET' && (path === '/api/library/stills' || path.startsWith('/api/library/stills/'))) return true
return false
}
export function assertAigenFeature() {
if (isXaigenStudio()) {
throw createError({ statusCode: 404, statusMessage: 'Not found' })
}
}
+3
View File
@@ -21,6 +21,7 @@ import {
} from '~/utils/keepPrompts'
import { familyBaseName, nextClipPartName, clipPartLabel } from '~/utils/libraryBrowse'
import { musicExtension } from '~/utils/music'
import { aigenBearerAuthorized, aigenInstanceOwnerKey } from '~/server/utils/aigenApiAuth'
export { familyBaseName, nextClipPartName, clipPartLabel }
@@ -197,6 +198,8 @@ export function libraryRoot() {
export function libraryOwnerKey(event: H3Event) {
if (passwordAuthEnabled()) return privateInstanceId()
// AIGen API key: same catalog as the unauthenticated local owner. Never invent a second tree.
if (aigenBearerAuthorized(event)) return aigenInstanceOwnerKey()
const user = getSessionUser(event)
const raw = user?.sub || user?.email || (!authEnabled() ? 'local' : '')
if (!raw) {