File size: 2,639 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextRequest, NextResponse } from "next/server";
import { isAuthenticated } from "@/shared/utils/apiAuth";
import {
  clearReasoningCacheAll,
  deleteReasoningCacheEntry,
  getReasoningCacheServiceEntries,
  getReasoningCacheServiceStats,
} from "@omniroute/open-sse/services/reasoningCache.ts";
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";

function errorMessage(error: unknown): string {
  return sanitizeErrorMessage(error);
}

/**

 * GET /api/cache/reasoning

 *

 * Returns reasoning replay cache stats + paginated entries.

 * Query params: ?provider=deepseek&model=deepseek-reasoner&limit=50&offset=0

 */
export async function GET(req: NextRequest) {
  if (!(await isAuthenticated(req))) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  try {
    const { searchParams } = new URL(req.url);
    const provider = searchParams.get("provider") || undefined;
    const model = searchParams.get("model") || undefined;
    const limit = parseInt(searchParams.get("limit") || "50", 10);
    const offset = parseInt(searchParams.get("offset") || "0", 10);

    const stats = getReasoningCacheServiceStats();
    const entries = getReasoningCacheServiceEntries({
      limit: Math.min(Math.max(limit, 1), 200),
      offset: Math.max(offset, 0),
      provider,
      model,
    });

    return NextResponse.json({ stats, entries });
  } catch (error) {
    return NextResponse.json({ error: errorMessage(error) }, { status: 500 });
  }
}

/**

 * DELETE /api/cache/reasoning

 *

 * Clears reasoning cache entries.

 * Query params: ?toolCallId=call_abc (single entry), ?provider=deepseek, or no params.

 */
export async function DELETE(req: NextRequest) {
  if (!(await isAuthenticated(req))) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  try {
    const { searchParams } = new URL(req.url);
    const toolCallId = searchParams.get("toolCallId") || undefined;
    const provider = searchParams.get("provider") || undefined;

    if (toolCallId) {
      const cleared = deleteReasoningCacheEntry(toolCallId);
      return NextResponse.json({
        ok: true,
        cleared,
        scope: "toolCallId",
        toolCallId,
      });
    }

    const cleared = clearReasoningCacheAll(provider);

    return NextResponse.json({
      ok: true,
      cleared,
      scope: provider ? "provider" : "all",
      ...(provider ? { provider } : {}),
    });
  } catch (error) {
    return NextResponse.json({ error: errorMessage(error) }, { status: 500 });
  }
}