1- import { runs , tasks } from "@trigger.dev/sdk" ;
2- import { cloneRepo } from "@/trigger/clone-repo" ;
3- import { repoChatSession } from "@/trigger/repo-chat-session" ;
1+ import { tasks } from "@trigger.dev/sdk" ;
2+ import { analyzeRepo } from "@/trigger/analyze-repo" ;
43import { NextRequest , NextResponse } from "next/server" ;
54
65export async function POST ( request : NextRequest ) {
76 try {
8- const { githubUrl } = await request . json ( ) ;
7+ const { repoUrl , question } = await request . json ( ) ;
98
109 // Validate inputs
11- if ( ! githubUrl || typeof githubUrl !== "string" ) {
10+ if ( ! repoUrl || typeof repoUrl !== "string" ) {
1211 return NextResponse . json (
13- { error : "GitHub URL is required" } ,
12+ { error : "Repository URL is required" } ,
1413 { status : 400 } ,
1514 ) ;
1615 }
1716
18- // Basic GitHub URL validation
19- const githubUrlPattern = / ^ h t t p s ? : \/ \/ ( w w w \. ) ? g i t h u b \. c o m \/ [ \w - ] + \/ [ \w . - ] + / ;
20- if ( ! githubUrlPattern . test ( githubUrl ) ) {
17+ if ( ! question || typeof question !== "string" ) {
2118 return NextResponse . json (
22- { error : "Invalid GitHub URL format " } ,
19+ { error : "Question is required " } ,
2320 { status : 400 } ,
2421 ) ;
2522 }
2623
27- // Trigger the clone task
28- const cloneHandle = await tasks . trigger < typeof cloneRepo > (
29- "clone-repo" ,
30- { githubUrl } ,
31- ) ;
32-
33- // Wait for clone to complete using pollForCompletion
34- let cloneResult ;
35- let attempts = 0 ;
36- const maxAttempts = 60 ; // 60 seconds timeout
37-
38- while ( attempts < maxAttempts ) {
39- const run = await runs . retrieve < typeof cloneRepo > ( cloneHandle . id ) ;
40-
41- if ( run . status === "COMPLETED" && run . output ) {
42- cloneResult = { ok : true , output : run . output } ;
43- break ;
44- } else if ( run . status === "FAILED" ) {
45- return NextResponse . json (
46- { error : "Failed to clone repository" } ,
47- { status : 500 } ,
48- ) ;
49- }
50-
51- // Wait 1 second before next check
52- await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
53- attempts ++ ;
54- }
55-
56- if ( ! cloneResult ) {
24+ // Basic GitHub URL validation
25+ const githubUrlPattern = / ^ h t t p s ? : \/ \/ ( w w w \. ) ? g i t h u b \. c o m \/ [ \w - ] + \/ [ \w . - ] + / ;
26+ if ( ! githubUrlPattern . test ( repoUrl ) ) {
5727 return NextResponse . json (
58- { error : "Clone operation timed out " } ,
59- { status : 500 } ,
28+ { error : "Invalid GitHub URL format " } ,
29+ { status : 400 } ,
6030 ) ;
6131 }
6232
63- // Immediately trigger chat session with the cloned repo
64- const chatHandle = await tasks . trigger < typeof repoChatSession > (
65- "repo-chat-session" ,
66- {
67- tempDir : cloneResult . output . tempDir ,
68- sessionId : cloneResult . output . sessionId ,
69- repoName : cloneResult . output . repoName ,
70- } ,
33+ // Trigger the analyze task
34+ const handle = await tasks . trigger < typeof analyzeRepo > (
35+ "analyze-repo" ,
36+ { repoUrl, question } ,
7137 ) ;
7238
7339 // Get public access token from handle (auto-generated, expires in 15 min)
74- const accessToken = chatHandle . publicAccessToken ;
40+ const accessToken = handle . publicAccessToken ;
7541
76- // Return session details
42+ // Return run details
7743 return NextResponse . json ( {
78- sessionId : cloneResult . output . sessionId ,
79- chatRunId : chatHandle . id ,
44+ runId : handle . id ,
8045 accessToken,
81- repoName : cloneResult . output . repoName ,
82- cloneRunId : cloneHandle . id , // Keep for backward compatibility
8346 } ) ;
8447 } catch ( error : any ) {
85- console . error ( "Failed to trigger clone -repo task:" , error ) ;
48+ console . error ( "Failed to trigger analyze -repo task:" , error ) ;
8649 return NextResponse . json (
87- { error : error . message || "Failed to start cloning " } ,
50+ { error : error . message || "Failed to start analysis " } ,
8851 { status : 500 } ,
8952 ) ;
9053 }
91- }
54+ }
0 commit comments