-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathroute.ts
More file actions
54 lines (47 loc) · 1.49 KB
/
route.ts
File metadata and controls
54 lines (47 loc) · 1.49 KB
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
import { tasks } from "@trigger.dev/sdk";
import type { analyzeRepo } from "@/trigger/analyze-repo";
import { NextRequest, NextResponse } from "next/server";
export async function POST(request: NextRequest) {
try {
const { repoUrl, question } = await request.json();
// Validate inputs
if (!repoUrl || typeof repoUrl !== "string") {
return NextResponse.json(
{ error: "Repository URL is required" },
{ status: 400 },
);
}
if (!question || typeof question !== "string") {
return NextResponse.json(
{ error: "Question is required" },
{ status: 400 },
);
}
// Basic GitHub URL validation
const githubUrlPattern = /^https?:\/\/(www\.)?github\.com\/[\w-]+\/[\w.-]+/;
if (!githubUrlPattern.test(repoUrl)) {
return NextResponse.json(
{ error: "Invalid GitHub URL format" },
{ status: 400 },
);
}
// Trigger the analyze task
const handle = await tasks.trigger<typeof analyzeRepo>(
"analyze-repo",
{ repoUrl, question },
);
// Get public access token from handle (auto-generated, expires in 15 min)
const accessToken = handle.publicAccessToken;
// Return run details
return NextResponse.json({
runId: handle.id,
accessToken,
});
} catch (error: any) {
console.error("Failed to trigger analyze-repo task:", error);
return NextResponse.json(
{ error: error.message || "Failed to start analysis" },
{ status: 500 },
);
}
}