feat: analysis providers, settings UI, song search, WAV duration fix

- Multi-provider AI analysis (Anthropic, OpenAI, Ollama, Algorithmic)
- server-only guards on all provider files; client bundle fix
- /settings page with provider status, Ollama model picker, preferences
- Song search box on /analyze replacing raw MBID input (debounced, keyboard nav)
- Auto-register song via MusicBrainz on POST /api/tracks (no more 404)
- Fix WAV duration bug: last section songEnd was double-counting elapsed time
- Registry sync comment updated for self-hosted HTTPS git servers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
AJ Avezzano
2026-04-03 18:46:17 -04:00
parent 51f67f0aeb
commit 8b9d72bc9d
22 changed files with 1803 additions and 293 deletions

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { generateCTPWithAI } from "@/lib/analysis/ai-ctp";
import { getProvider, getAvailableProviders } from "@/lib/analysis/providers/registry";
import { validateCTP } from "@/lib/ctp/validate";
// ─── Request schema ───────────────────────────────────────────────────────────
@@ -12,16 +12,18 @@ const AnalyzeRequestSchema = z.object({
artist: z.string().min(1).max(256).optional(),
mbid: z.string().uuid().optional().nullable(),
contributed_by: z.string().min(1).max(64).optional(),
provider: z.string().optional(),
ollamaModel: z.string().optional(),
});
/**
* POST /api/analyze
*
* Accepts BPM detection results from the browser and uses Claude to generate
* a draft CTP document for human review.
* Accepts BPM detection results from the browser and uses the selected provider
* to generate a draft CTP document for human review.
*
* Body (JSON):
* { bpm, duration, title?, artist?, mbid?, contributed_by? }
* { bpm, duration, title?, artist?, mbid?, contributed_by?, provider?, ollamaModel? }
*
* Returns:
* { ctp: CTPDocument, warnings: string[] }
@@ -42,40 +44,67 @@ export async function POST(req: NextRequest) {
);
}
const { bpm, duration, title, artist, mbid, contributed_by } = parsed.data;
const { bpm, duration, title, artist, mbid, contributed_by, provider: providerId, ollamaModel } =
parsed.data;
if (!process.env.ANTHROPIC_API_KEY) {
// Validate Ollama-specific requirement
if (providerId === "ollama" && (!ollamaModel || ollamaModel.trim() === "")) {
return NextResponse.json(
{ error: "ANTHROPIC_API_KEY is not configured on this server" },
{ status: 503 }
{ error: "ollamaModel is required when using the Ollama provider" },
{ status: 400 }
);
}
// Resolve provider
let provider;
if (providerId) {
try {
provider = await getProvider(providerId);
} catch (err) {
return NextResponse.json(
{ error: err instanceof Error ? err.message : String(err) },
{ status: 400 }
);
}
} else {
const available = await getAvailableProviders();
provider = available[0];
if (!provider) {
return NextResponse.json(
{ error: "No analysis providers are currently available" },
{ status: 503 }
);
}
}
const input = {
bpm,
duration,
title,
artist,
mbid: mbid ?? null,
contributed_by: contributed_by ?? "anonymous",
ollamaModel,
};
let ctpDoc;
try {
ctpDoc = await generateCTPWithAI({
bpm,
duration,
title,
artist,
mbid: mbid ?? null,
contributedBy: contributed_by ?? "anonymous",
});
ctpDoc = await provider.generateCTP(input);
} catch (err) {
console.error("[analyze] AI generation failed:", err);
console.error(`[analyze] Provider '${provider.label}' failed:`, err);
return NextResponse.json(
{ error: "Failed to generate CTP document", detail: String(err) },
{ status: 500 }
{
error: `Provider '${provider.label}' failed: ${err instanceof Error ? err.message : String(err)}`,
},
{ status: 502 }
);
}
// Validate the AI output against the CTP schema
// Validate the output against the CTP schema
const validation = validateCTP(ctpDoc);
const warnings: string[] = [];
if (!validation.success) {
// Rather than 500-ing, return the draft with validation warnings so the user
// can still see and manually correct it.
warnings.push(...validation.errors.issues.map((i) => `${i.path.join(".")}: ${i.message}`));
}