返回 CodeWhale
route.ts
根目录 / web / app / api / cron / route.ts
1 import { NextResponse } from "next/server";
2 import { getEnv } from "@/lib/kv";
3 import type { AgentEnv } from "@/lib/community-agent-tasks";
4 import { runFactsDrift } from "@/lib/facts-drift";
5 import { runLinkCheck, runSemanticDrift } from "@/lib/content-watch";
6 import { safeEqual } from "@/lib/community-agent";
7 import { CRON_SECRET_UNAVAILABLE, secretStrongEnough } from "@/lib/maintainer-token";
8
9 export const dynamic = "force-dynamic";
10
11 const TASKS = ["facts-drift", "linkcheck", "semantic-drift"] as const;
12 type Task = (typeof TASKS)[number];
13
14 /**
15 * Manual trigger surface for community-agent tasks.
16 *
17 * Usage:
18 * GET /api/cron?task=facts-drift
19 * Header: x-cron-secret: <CRON_SECRET>
20 *
21 * Real cron scheduling is handled by worker.ts's scheduled() handler.
22 */
23 export async function GET(req: Request) {
24 const env = await getEnv();
25
26 // Always require auth
27 if (!env.CRON_SECRET) {
28 return NextResponse.json(
29 { error: "manual trigger disabled in production" },
30 { status: 503 }
31 );
32 }
33
34 if (!secretStrongEnough(env.CRON_SECRET)) {
35 console.warn(CRON_SECRET_UNAVAILABLE);
36 return NextResponse.json(
37 { error: CRON_SECRET_UNAVAILABLE },
38 { status: 503, headers: { "Cache-Control": "no-store" } }
39 );
40 }
41
42 try {
43 if (!env.ADMIN_LOGIN_LIMITER) throw new Error("Missing cron limiter");
44 const { success } = await env.ADMIN_LOGIN_LIMITER.limit({ key: "codewhale-web:cron-secret" });
45 if (!success) {
46 return NextResponse.json(
47 { error: "too many attempts" },
48 { status: 429, headers: { "Cache-Control": "no-store", "Retry-After": "60" } }
49 );
50 }
51 } catch {
52 return NextResponse.json(
53 { error: "manual trigger temporarily unavailable" },
54 { status: 503, headers: { "Cache-Control": "no-store", "Retry-After": "60" } }
55 );
56 }
57
58 const auth = req.headers.get("x-cron-secret") ?? "";
59 if (!(await safeEqual(auth, env.CRON_SECRET))) {
60 return NextResponse.json({ error: "unauthorized" }, { status: 401, headers: { "Cache-Control": "no-store" } });
61 }
62
63 const { searchParams } = new URL(req.url);
64 const task = searchParams.get("task");
65
66 if (!task || !TASKS.includes(task as Task)) {
67 return NextResponse.json(
68 { error: `missing or invalid task. Allowed: ${TASKS.join(", ")}` },
69 { status: 400 }
70 );
71 }
72
73 // Build AgentEnv from the same shape expected by the task functions
74 const agentEnv: AgentEnv = {
75 CURATED_KV: env.CURATED_KV,
76 DEEPSEEK_API_KEY: env.DEEPSEEK_API_KEY,
77 DEEPSEEK_BASE_URL: env.DEEPSEEK_BASE_URL ?? process.env.DEEPSEEK_BASE_URL,
78 DEEPSEEK_MODEL: env.DEEPSEEK_MODEL ?? process.env.DEEPSEEK_MODEL,
79 GITHUB_TOKEN: env.GITHUB_TOKEN,
80 CRON_SECRET: env.CRON_SECRET,
81 GITHUB_REPO: env.GITHUB_REPO,
82 MAINTAINER_TOKEN: undefined,
83 MAINTAINER_GITHUB_PAT: undefined,
84 };
85
86 const watchEnv = { ...agentEnv, DRAFT_CLAIM_LOCK: env.DRAFT_CLAIM_LOCK };
87
88 try {
89 let result: Record<string, unknown>;
90 switch (task) {
91 case "facts-drift":
92 result = await runFactsDrift(watchEnv) as unknown as Record<string, unknown>;
93 break;
94 case "linkcheck":
95 result = await runLinkCheck(watchEnv) as unknown as Record<string, unknown>;
96 break;
97 case "semantic-drift":
98 result = await runSemanticDrift(watchEnv) as unknown as Record<string, unknown>;
99 break;
100 default:
101 // unreachable — guarded by TASKS check above
102 result = { error: "unknown task" };
103 }
104 return NextResponse.json({ ok: true, task, result });
105 } catch (e) {
106 return NextResponse.json({ ok: false, error: String(e) }, { status: 200 });
107 }
108 }
109
109 lines TYPESCRIPT