Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/app/api/combos/auto/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/

import { NextRequest, NextResponse } from "next/server";
import { createAutoComboSchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";

// ── In-memory auto-combo store (mirrors open-sse/services/autoCombo/engine.ts) ──

Expand Down Expand Up @@ -45,29 +47,45 @@ interface AutoComboConfig {
const autoCombos = new Map<string, AutoComboConfig>();

export async function POST(req: NextRequest) {
let rawBody: unknown;
try {
const body = await req.json();
const { id, name, candidatePool, weights, modePack, budgetCap, explorationRate } = body;
rawBody = await req.json();
} catch {
return NextResponse.json(
{
error: {
message: "Invalid request",
details: [{ field: "body", message: "Invalid JSON body" }],
},
},
{ status: 400 }
);
}

if (!id || !name) {
return NextResponse.json({ error: "id and name required" }, { status: 400 });
try {
const validation = validateBody(createAutoComboSchema, rawBody);
if (isValidationFailure(validation)) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const { id, name, candidatePool, weights, modePack, budgetCap, explorationRate } =
validation.data;

const config: AutoComboConfig = {
id,
name,
type: "auto",
candidatePool: candidatePool || [],
weights: weights || DEFAULT_WEIGHTS,
candidatePool,
weights: weights ?? DEFAULT_WEIGHTS,
modePack,
budgetCap,
explorationRate: explorationRate ?? 0.05,
explorationRate,
};
autoCombos.set(id, config);

return NextResponse.json(config, { status: 201 });
} catch (err) {
return NextResponse.json({ error: String(err) }, { status: 500 });
console.log("Error creating auto-combo:", err);
return NextResponse.json({ error: "Failed to create auto-combo" }, { status: 500 });
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/shared/validation/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ export const createComboSchema = z.object({
config: comboConfigSchema,
});

// ──── Auto-Combo Schemas ────

const scoringWeightsSchema = z
.object({
quota: z.number().min(0).max(1),
health: z.number().min(0).max(1),
costInv: z.number().min(0).max(1),
latencyInv: z.number().min(0).max(1),
taskFit: z.number().min(0).max(1),
stability: z.number().min(0).max(1),
})
.optional();

export const createAutoComboSchema = z.object({
id: z.string().trim().min(1, "id is required").max(100),
name: z.string().trim().min(1, "name is required").max(200),
candidatePool: z.array(z.string().min(1)).optional().default([]),
weights: scoringWeightsSchema,
modePack: z.string().max(100).optional(),
budgetCap: z.number().positive().optional(),
explorationRate: z.number().min(0).max(1).optional().default(0.05),
});

// ──── Settings Schemas ────
// FASE-01: Removed .passthrough() — only explicitly listed fields are accepted

Expand Down