SPB Git

Repository Public

route.ts 5.7 KB 98 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | ACADEMIC RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project          : UQO Éval10 * File             : src/app/api/metre/ai/route.ts11 * Module           : app12 * Repository       : https://git.spboucher.ai/uqo-eval13 * Project Website  : https://www.uqo-eval.app14 * Created          : 2026-10-2815 * Last Modified    : 2026-10-2816 *17 * =============================================================================18 * SOFTWARE MAINTAINER & AFFILIATION19 * =============================================================================20 *21 * Maintainer       : Simon-Pierre Boucher22 * Position         : Professor of Finance23 * Institution      : Université du Québec en Outaouais (UQO)24 * Department       : Département des sciences administratives25 * Website          : https://spboucher.ai26 * Git              : https://git.spboucher.ai27 *28 * Research Contact : research@spboucher.ai29 * Direct Contact   : simon@spboucher.ai30 * Security/Admin   : admin@spboucher.ai31 *32 * The affiliation is descriptive. It is not an institutional endorsement,33 * a license grant or a determination of ownership.34 *35 * =============================================================================36 * COPYRIGHT, AUTHORSHIP & DATA37 * =============================================================================38 *39 * Copyright (c) the author(s) of this work.40 * Ownership is not asserted by this notice; see the repository records.41 *42 * This header identifies the maintainer of the software. Scientific43 * authorship of any associated paper is stated in the paper itself and44 * is not changed by this notice. Datasets and third-party materials45 * remain the property of their respective owners and licensors.46 *47 * This file is governed by the applicable project license, copyright48 * notices, institutional agreements and relevant law.49 *50 * =============================================================================51 * LICENSE & MAINTENANCE52 * =============================================================================53 *54 * License          : None specified (no license file in this repository)55 * Classification   : Public56 *57 * =============================================================================58 * END OF SP BOUCHER SOURCE HEADER59 * =============================================================================60 */6162// UQO Éval — Métré : assistance IA — liste des suggestions (GET), demande de suggestion (POST). Toujours en attente de revue humaine.63import { NextRequest, NextResponse } from "next/server";64import { HttpError, handle, readJson, requireProject, requireUser } from "@/lib/metre/auth";65import { ClaudeProvider, aiUsage, classifyObservations, commentAnomalies, detectAnomalies, explainEstimate, listSuggestions, suggestAssemblyMapping, suggestMissingScope } from "@/lib/metre/ai";66import { getEstimate } from "@/lib/metre/estimates";6768export const runtime = "nodejs";69export const maxDuration = 120;7071export async function GET(req: NextRequest) {72  return handle(() => {73    const ctx = requireUser(req);74    const pid = req.nextUrl.searchParams.get("project");75    if (pid) requireProject(ctx, pid, "read");76    else if (ctx.user.role !== "admin" && ctx.user.role !== "instructor") throw new HttpError(403, "Liste globale réservée au personnel.");77    return NextResponse.json({ suggestions: listSuggestions(ctx.db, pid), usage: ctx.user.role === "student" ? null : aiUsage(ctx.db) });78  });79}8081export async function POST(req: NextRequest) {82  return handle(async () => {83    const ctx = requireUser(req, { limit: { key: "ai", max: 20, windowMs: 10 * 60_000 } });84    const b = await readJson<{ kind?: string; projectId?: string; estimateId?: string; sourceKey?: string; lang?: string }>(req);85    const provider = new ClaudeProvider();86    const needEstimate = () => { const e = b.estimateId ? getEstimate(ctx.db, b.estimateId) : null; if (!e) throw new HttpError(404, "Estimation introuvable."); requireProject(ctx, e.projectId, "write"); return e; };87    switch (b.kind) {88      case "assembly_mapping": { if (!b.projectId) throw new HttpError(400, "projectId requis."); requireProject(ctx, b.projectId, "write"); return NextResponse.json({ suggestion: await suggestAssemblyMapping(ctx, provider, b.projectId, b.estimateId ?? null) }, { status: 201 }); }89      case "missing_scope": { const e = needEstimate(); return NextResponse.json({ suggestion: await suggestMissingScope(ctx, provider, e.id) }, { status: 201 }); }90      case "explain": { const e = needEstimate(); return NextResponse.json({ suggestion: await explainEstimate(ctx, provider, e.id, b.lang === "en" ? "en" : "fr") }, { status: 201 }); }91      case "anomalies": { const e = needEstimate(); const found = detectAnomalies(ctx, e.id); if (!found.length) return NextResponse.json({ anomalies: [], suggestion: null }); return NextResponse.json({ anomalies: found, suggestion: await commentAnomalies(ctx, provider, e.id) }, { status: 201 }); }92      case "anomalies_only": { const e = needEstimate(); return NextResponse.json({ anomalies: detectAnomalies(ctx, e.id) }); }93      case "classify": { if (ctx.user.role !== "admin" && ctx.user.role !== "instructor") throw new HttpError(403, "Réservé au personnel."); return NextResponse.json({ suggestion: await classifyObservations(ctx, provider, String(b.sourceKey ?? "")) }, { status: 201 }); }94      default: throw new HttpError(400, "kind : assembly_mapping | missing_scope | explain | anomalies | anomalies_only | classify");95    }96  });97}98
Files main
Menu

Theme