| |
| const BACK_URL = "https://shantanupathak94-ai-code-editor.hf.space"; |
|
|
| export const api = { |
| |
| |
| generateCode: async (prompt, existingFiles, provider) => { |
| |
| const API_URL = "https://shantanupathak94-ai-code-editor.hf.space"; |
| |
| try { |
| |
| const response = await fetch(`${API_URL}/api/agent/generate`, { |
| method: 'POST', |
| headers: { 'Content-Type': 'application/json' }, |
| body: JSON.stringify({ |
| prompt: prompt, |
| existing_files: existingFiles, |
| model_preference: provider || "gemini" |
| }) |
| }); |
| |
| if (!response.ok) { |
| const err = await response.json(); |
| throw new Error(err.detail || "AI Gateway Error"); |
| } |
| |
| const data = await response.json(); |
| return data.files; |
| } catch (error) { |
| throw error; |
| } |
| }, |
|
|
| |
| saveWorkspace: async (name, filesObject) => { |
| |
| const filesArray = Object.values(filesObject).map(f => ({ |
| filename: f.name, language: f.language, code: f.value |
| })); |
| const res = await fetch(`${BACK_URL}/workspace/save`, { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ user_id: "shantanu_founder", name, files: filesArray }) |
| }); |
| return await res.json(); |
| }, |
|
|
| loadWorkspaces: async () => { |
| const res = await fetch(`${BACK_URL}/workspace/user/shantanu_founder`); |
| return await res.json(); |
| }, |
|
|
| loadSingleWorkspace: async (workspaceId) => { |
| const res = await fetch(`${BACK_URL}/workspace/${workspaceId}`); |
| return await res.json(); |
| }, |
|
|
| |
| githubClone: async (repoUrl) => { |
| const res = await fetch(`${BACK_URL}/github/clone`, { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ repo_url: repoUrl, branch: "main" }) |
| }); |
| if (!res.ok) throw new Error("Failed to clone repo"); |
| return await res.json(); |
| }, |
|
|
| githubPush: async (repoFullName, commitMessage, filesObject, token) => { |
| const filesArray = Object.values(filesObject).map(f => ({ |
| filename: f.name, language: f.language, code: f.value |
| })); |
| const res = await fetch(`${BACK_URL}/github/commit-push`, { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ repo_full_name: repoFullName, commit_message: commitMessage, files: filesArray, token }) |
| }); |
| if (!res.ok) throw new Error("GitHub Push Failed"); |
| return await res.json(); |
| } |
| }; |