File size: 1,677 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { NextResponse } from "next/server";
import {
  suggestFilter,
  commandToId,
  listRtkCommandSamples,
} from "@omniroute/open-sse/services/compression/engines/rtk";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";

/** Parse a positive `limit` query param, clamped to [1, 2000]; default 500. */
function parseLimit(value: string | null): number {
  const n = Number(value);
  if (!Number.isFinite(n) || n <= 0) return 500;
  return Math.min(2000, Math.floor(n));
}

/**
 * GET /api/context/rtk/learn?command=<cmd> — suggest an RTK filter draft for a
 * command, learned from the captured outputs of THAT command in the opt-in raw-output
 * sample store. Read-only; returns a draft for the operator to review and save (the
 * existing filter trust path persists it). Other commands' samples are excluded so the
 * learned drop/preserve patterns stay specific.
 */
export async function GET(request: Request) {
  const authError = await requireManagementAuth(request);
  if (authError) return authError;

  const url = new URL(request.url);
  const command = (url.searchParams.get("command") || "").trim();
  if (!command) {
    return NextResponse.json(
      { error: { message: "The 'command' query parameter is required.", type: "invalid_request" } },
      { status: 400 }
    );
  }

  const limit = parseLimit(url.searchParams.get("limit"));
  const targetId = commandToId(command);
  const matching = listRtkCommandSamples({ limit }).filter(
    (sample) => commandToId(sample.command) === targetId
  );
  const filter = suggestFilter(command, matching);

  return NextResponse.json({ command, sampleCount: matching.length, filter });
}