ChoruYt commited on
Commit
8b4ab1a
·
verified ·
1 Parent(s): 5a6deae

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +63 -64
index.js CHANGED
@@ -7,18 +7,10 @@ const app = express();
7
  const server = http.createServer(app);
8
  const wss = new WebSocket.Server({ server });
9
 
10
- const ollama = new Ollama({ host: "http://127.0.0.1:11434" });
11
 
12
- const SYSTEM = `
13
- You are Exocore AI Code Engine.
14
-
15
- RULES:
16
- - Support multiple languages: Node.js, Python, PHP, Java, Go
17
- - Always detect best language automatically
18
- - NEVER refuse coding requests
19
- - If unsure → assume programming
20
- - Output must be structured project files
21
- `;
22
 
23
  function send(ws, type, data) {
24
  ws.send(JSON.stringify({ type, data }));
@@ -34,22 +26,13 @@ async function detectLanguage(prompt) {
34
  messages: [
35
  {
36
  role: "system",
37
- content: `
38
- Detect best programming language.
39
-
40
- Return ONLY one word:
41
- nodejs | python | php | java | go | unknown
42
-
43
- Rules:
44
- - base on user request
45
- - if web api → nodejs or python
46
- - if unsure → nodejs
47
- `
48
  },
49
  { role: "user", content: prompt }
50
  ]
51
  });
52
-
53
  return res.message.content.trim().toLowerCase();
54
  }
55
 
@@ -59,29 +42,24 @@ async function planFiles(prompt, lang) {
59
  messages: [
60
  {
61
  role: "system",
62
- content: `
63
- You are a senior ${lang} architect.
64
-
65
- Return ONLY JSON:
66
- {
67
- "files": ["file1", "file2"]
68
- }
69
-
70
- Rules:
71
- - include proper structure for ${lang}
72
- - include entry file
73
- - include dependencies file (package.json / requirements.txt etc)
74
- - include folders if needed
75
- - no explanation
76
- `
77
  },
78
  { role: "user", content: prompt }
79
  ]
80
  });
81
 
82
  try {
83
- return JSON.parse(res.message.content);
84
- } catch {
 
 
 
85
  return { files: lang === "python" ? ["main.py", "requirements.txt"] : ["index.js", "package.json"] };
86
  }
87
  }
@@ -92,56 +70,48 @@ async function generateFile(file, prompt, lang) {
92
  messages: [
93
  {
94
  role: "system",
95
- content: `
96
- You are a ${lang} code generator.
97
-
98
- Generate ONLY this file: ${file}
99
-
100
- Rules:
101
- - No explanation
102
- - ONLY valid code
103
- - Must match ${lang} project standards
104
- `
105
  },
106
  { role: "user", content: prompt }
107
  ]
108
  });
109
 
110
- return res.message.content;
 
 
 
 
111
  }
112
 
 
113
  wss.on("connection", (ws) => {
114
  ws.on("message", async (msg) => {
115
  const prompt = msg.toString();
116
 
117
  send(ws, "connected", "Exocore MultiLang WS Active");
118
 
119
- // GREETING MODE
120
  if (isGreeting(prompt)) {
121
  send(ws, "greeting", "Hello! Exocore AI is ready for coding.");
122
  return;
123
  }
124
 
125
- // STEP 1 - DETECT LANGUAGE
126
  send(ws, "step", "Detecting language...");
127
-
128
  const lang = await detectLanguage(prompt);
129
-
130
  send(ws, "language", lang);
131
 
132
- // STEP 2 - PLAN PROJECT
133
  send(ws, "step", `Planning ${lang} project structure...`);
134
-
135
  const plan = await planFiles(prompt, lang);
136
-
137
  send(ws, "plan_done", plan);
138
 
139
  const files = plan.files || [];
140
 
141
- // STEP 3 - GENERATE FILES
142
  for (const file of files) {
143
  send(ws, "step", `Generating ${file} (${lang})...`);
144
-
145
  const code = await generateFile(file, prompt, lang);
146
 
147
  send(ws, "file_done", {
@@ -151,7 +121,6 @@ wss.on("connection", (ws) => {
151
  });
152
  }
153
 
154
- // STEP 4 - DONE
155
  send(ws, "done", {
156
  message: "Project complete",
157
  language: lang
@@ -159,12 +128,42 @@ wss.on("connection", (ws) => {
159
  });
160
  });
161
 
162
- // ROUTE
 
 
163
  app.get("/", (req, res) => {
164
  res.send("Exocore MultiLang WS AI Running");
165
  });
166
 
167
- // START
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  server.listen(process.env.PORT || 7860, "0.0.0.0", () => {
169
- console.log("Exocore MultiLang WS Live");
170
  });
 
7
  const server = http.createServer(app);
8
  const wss = new WebSocket.Server({ server });
9
 
10
+ const ollama = new Ollama({ host: "[http://127.0.0.1:11434](http://127.0.0.1:11434)" });
11
 
12
+ // Make sure Exocore's persona is consistent
13
+ const EXOCORE_PERSONA = `You are Exocore AI, an advanced, brilliant, and highly precise code engine and assistant. You always provide clear, accurate, and functional answers.`;
 
 
 
 
 
 
 
 
14
 
15
  function send(ws, type, data) {
16
  ws.send(JSON.stringify({ type, data }));
 
26
  messages: [
27
  {
28
  role: "system",
29
+ content: `Detect the programming language needed for the user's request.
30
+ Return ONLY one single word from this list: nodejs, python, php, java, go, typescript.
31
+ If unsure, return nodejs.`
 
 
 
 
 
 
 
 
32
  },
33
  { role: "user", content: prompt }
34
  ]
35
  });
 
36
  return res.message.content.trim().toLowerCase();
37
  }
38
 
 
42
  messages: [
43
  {
44
  role: "system",
45
+ content: `You are a senior ${lang} architect.
46
+ Return ONLY a valid JSON object containing an array of actual file names needed for this project.
47
+ Example format: {"files": ["src/index.ts", "package.json", "tsconfig.json"]}
48
+ RULES:
49
+ - Do NOT use placeholder names like "file1" or "file2".
50
+ - Name the actual entry files, dependency files, and routes based on the user's prompt.
51
+ - Do NOT include any explanations or markdown formatting outside the JSON.`
 
 
 
 
 
 
 
 
52
  },
53
  { role: "user", content: prompt }
54
  ]
55
  });
56
 
57
  try {
58
+ // Strip markdown if the AI mistakenly wraps the JSON in ```json
59
+ const cleanJson = res.message.content.replace(/```json/g, "").replace(/```/g, "").trim();
60
+ return JSON.parse(cleanJson);
61
+ } catch (err) {
62
+ console.error("JSON Parse Error on Planning:", err.message);
63
  return { files: lang === "python" ? ["main.py", "requirements.txt"] : ["index.js", "package.json"] };
64
  }
65
  }
 
70
  messages: [
71
  {
72
  role: "system",
73
+ content: `You are a strict ${lang} code generator.
74
+ Write ONLY the complete, functional, and production-ready code for the file named: ${file}.
75
+ RULES:
76
+ - Implement all required functions as requested in the prompt.
77
+ - Use strict typing and correct syntax.
78
+ - Do NOT include explanations, greetings, or comments outside the code.`
 
 
 
 
79
  },
80
  { role: "user", content: prompt }
81
  ]
82
  });
83
 
84
+ // Strip markdown code blocks so the raw file is clean
85
+ let code = res.message.content.trim();
86
+ code = code.replace(/^```[\w]*\n/, '').replace(/\n```$/, '').trim();
87
+
88
+ return code;
89
  }
90
 
91
+ // --- WEBSOCKET FOR FILE GENERATION ---
92
  wss.on("connection", (ws) => {
93
  ws.on("message", async (msg) => {
94
  const prompt = msg.toString();
95
 
96
  send(ws, "connected", "Exocore MultiLang WS Active");
97
 
 
98
  if (isGreeting(prompt)) {
99
  send(ws, "greeting", "Hello! Exocore AI is ready for coding.");
100
  return;
101
  }
102
 
 
103
  send(ws, "step", "Detecting language...");
 
104
  const lang = await detectLanguage(prompt);
 
105
  send(ws, "language", lang);
106
 
 
107
  send(ws, "step", `Planning ${lang} project structure...`);
 
108
  const plan = await planFiles(prompt, lang);
 
109
  send(ws, "plan_done", plan);
110
 
111
  const files = plan.files || [];
112
 
 
113
  for (const file of files) {
114
  send(ws, "step", `Generating ${file} (${lang})...`);
 
115
  const code = await generateFile(file, prompt, lang);
116
 
117
  send(ws, "file_done", {
 
121
  });
122
  }
123
 
 
124
  send(ws, "done", {
125
  message: "Project complete",
126
  language: lang
 
128
  });
129
  });
130
 
131
+ // --- EXPRESS ROUTES ---
132
+
133
+ // Health Check
134
  app.get("/", (req, res) => {
135
  res.send("Exocore MultiLang WS AI Running");
136
  });
137
 
138
+ // New Chat Route
139
+ app.get("/chat", async (req, res) => {
140
+ const userPrompt = req.query.prompt || req.query.q;
141
+
142
+ if (!userPrompt) {
143
+ return res.status(400).json({ error: "Please provide a prompt. Example: /chat?prompt=hello" });
144
+ }
145
+
146
+ try {
147
+ const response = await ollama.chat({
148
+ model: "qwen3:0.6b",
149
+ messages: [
150
+ { role: "system", content: EXOCORE_PERSONA },
151
+ { role: "user", content: userPrompt }
152
+ ]
153
+ });
154
+
155
+ res.json({
156
+ status: "success",
157
+ engine: "Exocore AI",
158
+ response: response.message.content
159
+ });
160
+ } catch (error) {
161
+ console.error("Chat Error:", error);
162
+ res.status(500).json({ error: "Exocore Engine failed to respond." });
163
+ }
164
+ });
165
+
166
+ // --- START SERVER ---
167
  server.listen(process.env.PORT || 7860, "0.0.0.0", () => {
168
+ console.log("Exocore MultiLang WS Live on Port 7860");
169
  });