File size: 2,911 Bytes
cfbfd8d
 
 
 
 
b3d9f70
 
0e118b0
6268fd4
0e118b0
b3d9f70
0e118b0
b3d9f70
 
 
 
 
0e118b0
b3d9f70
 
 
0e118b0
b3d9f70
 
 
 
0e118b0
b3d9f70
0e118b0
b3d9f70
 
 
0e118b0
cfbfd8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// ✨ TUMHARA CLOUD BACKEND URL ✨
const BACK_URL = "https://shantanupathak94-ai-code-editor.hf.space";

export const api = {
  // 1. AI Generation (Secure Gateway)
  // apiService.js ke andar bas yeh function update karna hai
  generateCode: async (prompt, existingFiles, provider) => {
    // Yahan tumne jo apna Hugging Face space url dala hai, wahi rehne dena
    const API_URL = "https://shantanupathak94-ai-code-editor.hf.space"; 
    
    try {
      // DHYAAN DO: Naya endpoint aur naya body format
      const response = await fetch(`${API_URL}/api/agent/generate`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          prompt: prompt,
          existing_files: existingFiles, // Ab AI ko tumhari files dikhengi!
          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; // Naya return format
    } catch (error) {
      throw error;
    }
  },

  // 2. MongoDB Workspace Sync
  saveWorkspace: async (name, filesObject) => {
    // React files state ko backend format mein convert karna
    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();
  },

  // 3. GitHub Logic
  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();
  }
};