File size: 4,213 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { NextResponse } from "next/server";
import {
  getTaskRoutingConfig,
  setTaskRoutingConfig,
  resetTaskRoutingStats,
  getDefaultTaskModelMap,
} from "@omniroute/open-sse/services/taskAwareRouter.ts";
import { updateSettings } from "@/lib/db/settings";
import { taskRoutingActionSchema, updateTaskRoutingSchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";

/**
 * GET /api/settings/task-routing
 * Returns the current task-aware routing configuration.
 */
export async function GET(request: Request) {
  const authError = await requireManagementAuth(request);
  if (authError) return authError;
  try {
    return NextResponse.json({
      ...getTaskRoutingConfig(),
      defaultTaskModelMap: getDefaultTaskModelMap(),
    });
  } catch (error) {
    console.error("[API ERROR] /api/settings/task-routing GET:", error);
    return NextResponse.json({ error: "Failed to get config" }, { status: 500 });
  }
}

/**
 * PUT /api/settings/task-routing
 * Update the task-aware routing configuration.
 * Body: { enabled?: boolean, taskModelMap?: { coding?: "...", ... }, detectionEnabled?: boolean }
 */
export async function PUT(request: Request) {
  const authError = await requireManagementAuth(request);
  if (authError) return authError;
  let rawBody: unknown;
  try {
    rawBody = await request.json();
  } catch {
    return NextResponse.json(
      {
        error: {
          message: "Invalid request",
          details: [{ field: "body", message: "Invalid JSON body" }],
        },
      },
      { status: 400 }
    );
  }

  try {
    const validation = validateBody(updateTaskRoutingSchema, rawBody);
    if (isValidationFailure(validation)) {
      return NextResponse.json({ error: validation.error }, { status: 400 });
    }
    const config =
      validation.data as import("@omniroute/open-sse/services/taskAwareRouter.ts").TaskRoutingConfig;

    setTaskRoutingConfig(config);

    // Persist to database (excluding stats)
    const { stats, ...persistable } = getTaskRoutingConfig();
    await updateSettings({ taskRouting: JSON.stringify(persistable) });

    return NextResponse.json({ success: true, ...getTaskRoutingConfig() });
  } catch (error) {
    console.error("[API ERROR] /api/settings/task-routing PUT:", error);
    return NextResponse.json({ error: "Failed to update config" }, { status: 500 });
  }
}

/**
 * POST /api/settings/task-routing
 * Actions: { action: "reset-stats" | "detect" }
 * For "detect": pass { action: "detect", body: <request-body> } to test detection
 */
export async function POST(request: Request) {
  const authError = await requireManagementAuth(request);
  if (authError) return authError;
  let rawBody: unknown;
  try {
    rawBody = await request.json();
  } catch {
    return NextResponse.json(
      {
        error: {
          message: "Invalid request",
          details: [{ field: "body", message: "Invalid JSON body" }],
        },
      },
      { status: 400 }
    );
  }

  try {
    const validation = validateBody(taskRoutingActionSchema, rawBody);
    if (isValidationFailure(validation)) {
      return NextResponse.json({ error: validation.error }, { status: 400 });
    }
    const actionRequest = validation.data;

    if (actionRequest.action === "reset-stats") {
      resetTaskRoutingStats();
      return NextResponse.json({
        success: true,
        stats: getTaskRoutingConfig().stats,
      });
    }

    if (actionRequest.action === "detect") {
      const { detectTaskType } = await import("@omniroute/open-sse/services/taskAwareRouter.ts");
      const taskType = detectTaskType(actionRequest.body || {});
      const config = getTaskRoutingConfig();
      return NextResponse.json({
        taskType,
        preferredModel: config.taskModelMap[taskType] || "(no override)",
      });
    }

    return NextResponse.json({ error: "Unknown action" }, { status: 400 });
  } catch (error) {
    console.error("[API ERROR] /api/settings/task-routing POST:", error);
    return NextResponse.json({ error: "Failed to execute action" }, { status: 500 });
  }
}