Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app/api/submitjobv1/route.ts +117 -0
app/api/submitjobv1/route.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
+
import { generateText } from "ai";
|
| 3 |
+
import { getProviderForModel } from "@/lib/ai/provider-manager";
|
| 4 |
+
import { extractFilesFromAI, sanitizeBranchName } from "@/lib/ai-utils";
|
| 5 |
+
import { pushToGitHub } from "@/lib/github";
|
| 6 |
+
import { appConfig } from "@/config/app.config";
|
| 7 |
+
|
| 8 |
+
export async function GET() {
|
| 9 |
+
return NextResponse.json({ message: "Submit job endpoint (v1) is active. Use POST to submit a paper." });
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
export async function POST(request: NextRequest) {
|
| 13 |
+
try {
|
| 14 |
+
const {
|
| 15 |
+
arxivUrl,
|
| 16 |
+
paperContent,
|
| 17 |
+
repo,
|
| 18 |
+
paperName,
|
| 19 |
+
model = appConfig.ai.defaultModel
|
| 20 |
+
} = await request.json();
|
| 21 |
+
|
| 22 |
+
if (!repo) {
|
| 23 |
+
return NextResponse.json({ error: "Target GitHub repository is required (e.g. 'owner/repo')" }, { status: 400 });
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
if (!paperName) {
|
| 27 |
+
return NextResponse.json({ error: "Paper name is required for branch naming" }, { status: 400 });
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
const githubToken = process.env.PERSONAL_ACCESS_TOKEN;
|
| 31 |
+
if (!githubToken) {
|
| 32 |
+
return NextResponse.json({ error: "GitHub PERSONAL_ACCESS_TOKEN not configured in environment" }, { status: 500 });
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
let finalPaperContent = paperContent || "";
|
| 36 |
+
|
| 37 |
+
// 1. Get paper content from Arxiv if URL is provided
|
| 38 |
+
if (arxivUrl && !paperContent) {
|
| 39 |
+
console.log(`[submit-job] Scraping Arxiv: ${arxivUrl}`);
|
| 40 |
+
try {
|
| 41 |
+
const scrapeResponse = await fetch(`${process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'}/api/scrape-website`, {
|
| 42 |
+
method: 'POST',
|
| 43 |
+
headers: { 'Content-Type': 'application/json' },
|
| 44 |
+
body: JSON.stringify({ url: arxivUrl })
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
if (scrapeResponse.ok) {
|
| 48 |
+
const scrapeData = await scrapeResponse.json();
|
| 49 |
+
if (scrapeData.success) {
|
| 50 |
+
finalPaperContent = scrapeData.data.content;
|
| 51 |
+
} else {
|
| 52 |
+
console.warn(`[submit-job] Scrape failed: ${scrapeData.error}`);
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
} catch (error) {
|
| 56 |
+
console.error(`[submit-job] Error calling scrape API:`, error);
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
if (!finalPaperContent) {
|
| 61 |
+
return NextResponse.json({ error: "No paper content provided or found at URL" }, { status: 400 });
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// 2. Generate Code from Paper
|
| 65 |
+
console.log(`[submit-job] Generating code for: ${paperName}`);
|
| 66 |
+
const { client, actualModel } = getProviderForModel(model);
|
| 67 |
+
|
| 68 |
+
const systemPrompt = `You are an expert React developer. Your task is to transform the provided research paper content into a functional React application using Vite and Tailwind CSS.
|
| 69 |
+
|
| 70 |
+
The application should:
|
| 71 |
+
1. Visualize the core concepts or findings of the paper.
|
| 72 |
+
2. Provide interactive components if applicable (e.g., simulators, calculators, data explorers).
|
| 73 |
+
3. Have a professional, clean UI.
|
| 74 |
+
4. Include a detailed "About" section summarizing the paper.
|
| 75 |
+
|
| 76 |
+
Output format:
|
| 77 |
+
Generate multiple files using the <file path="..."> format.
|
| 78 |
+
Always start with src/index.css, then src/App.jsx, then components.
|
| 79 |
+
Do NOT include tailwind.config.js or vite.config.js.
|
| 80 |
+
|
| 81 |
+
${finalPaperContent.substring(0, 10000)} // Truncated paper content for prompt`;
|
| 82 |
+
|
| 83 |
+
const { text: generatedContent } = await generateText({
|
| 84 |
+
model: client(actualModel),
|
| 85 |
+
system: "You transform research papers into interactive React code.",
|
| 86 |
+
prompt: systemPrompt,
|
| 87 |
+
});
|
| 88 |
+
|
| 89 |
+
// 3. Extract Files
|
| 90 |
+
const files = extractFilesFromAI(generatedContent);
|
| 91 |
+
if (files.length === 0) {
|
| 92 |
+
return NextResponse.json({
|
| 93 |
+
error: "AI failed to generate code in the expected format",
|
| 94 |
+
rawResponse: generatedContent.substring(0, 500)
|
| 95 |
+
}, { status: 500 });
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
// 4. Push to GitHub
|
| 99 |
+
const branchName = sanitizeBranchName(paperName);
|
| 100 |
+
const githubResult = await pushToGitHub(repo, branchName, files, githubToken);
|
| 101 |
+
|
| 102 |
+
return NextResponse.json({
|
| 103 |
+
success: true,
|
| 104 |
+
message: `Successfully transformed paper to code and pushed to GitHub`,
|
| 105 |
+
githubUrl: githubResult.url,
|
| 106 |
+
branch: branchName,
|
| 107 |
+
filesCount: files.length
|
| 108 |
+
});
|
| 109 |
+
|
| 110 |
+
} catch (error: any) {
|
| 111 |
+
console.error("[submit-job] Error:", error);
|
| 112 |
+
return NextResponse.json({
|
| 113 |
+
success: false,
|
| 114 |
+
error: error.message || "An unexpected error occurred"
|
| 115 |
+
}, { status: 500 });
|
| 116 |
+
}
|
| 117 |
+
}
|