Adds automatic image tagging that runs as a post-scan phase, sending thumbnails to an OpenAI-compatible vision API and applying matching tags from the user-defined tag vocabulary. - New ai-tagger module with batch processing, failure tolerance, and tag validation against existing vocabulary - Admin settings page (Manage > AI Tagging) for endpoint, model, and enable toggle with connection testing - DB migration for ai_tagged_at tracking column and AI config seeds - Re-tag All support to queue items for reprocessing Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { requireAdmin } from '@/lib/auth'
|
|
import { getAiConfig } from '@/lib/app-settings'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const auth = await requireAdmin(request)
|
|
if (auth instanceof NextResponse) return auth
|
|
|
|
const { endpoint, model } = getAiConfig()
|
|
|
|
if (!endpoint) {
|
|
return NextResponse.json({ error: 'No endpoint configured' }, { status: 400 })
|
|
}
|
|
|
|
const url = endpoint.replace(/\/+$/, '') + '/chat/completions'
|
|
|
|
try {
|
|
const controller = new AbortController()
|
|
const timeout = setTimeout(() => controller.abort(), 10_000)
|
|
|
|
const res = await fetch(url, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
signal: controller.signal,
|
|
body: JSON.stringify({
|
|
model: model || 'test',
|
|
messages: [{ role: 'user', content: 'Hi' }],
|
|
max_tokens: 1,
|
|
}),
|
|
})
|
|
|
|
clearTimeout(timeout)
|
|
|
|
if (!res.ok) {
|
|
const text = await res.text().catch(() => '')
|
|
return NextResponse.json(
|
|
{ error: `LLM returned ${res.status}: ${text.slice(0, 200)}` },
|
|
{ status: 502 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({ ok: true })
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : 'Unknown error'
|
|
return NextResponse.json({ error: `Connection failed: ${message}` }, { status: 502 })
|
|
}
|
|
}
|