-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
180 lines (121 loc) · 3.75 KB
/
Copy pathindex.js
File metadata and controls
180 lines (121 loc) · 3.75 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { GoogleGenAI } from "@google/genai";
import readlineSync from 'readline-sync';
import { exec } from "child_process";
import { promisify } from "util";
import os from 'os'
import dotenv from 'dotenv';
dotenv.config();
const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY;
console.log("Google API Key:", GOOGLE_API_KEY);
const platform = os.platform();
const asyncExecute = promisify(exec);
const History = [];
const ai = new GoogleGenAI({ apiKey: GOOGLE_API_KEY });
// Tool create karte hai, jo kisi bhi terminal/ shell command ko execute kar sakta hai
async function executeCommand({command}) {
try{
const {stdout, stderr} = await asyncExecute(command);
if(stderr){
return `Error: ${stderr}`
}
return `Success: ${stdout} || Task executed completely`
}
catch(error){
return `Error: ${error}`
}
}
const executeCommandDeclaration = {
name: "executeCommand",
description:"Execute a single terminal/shell command. A command can be to create a folder, file, write on a file, edit the file or delete the file",
parameters:{
type:'OBJECT',
properties:{
command:{
type:'STRING',
description: 'It will be a single terminal command. Ex: "mkdir calculator"'
},
},
required: ['command']
}
}
const availableTools = {
executeCommand
}
async function runAgent(userProblem) {
History.push({
role:'user',
parts:[{text:userProblem}]
});
while(true){
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: History,
config: {
systemInstruction: `You are an Website builder expert. You have to create the frontend of the website by analysing the user Input.
You have access of tool, which can run or execute any shell or terminal command.
Current user operation system is: ${platform}
Give command to the user according to its operating system support.
<-- What is your job -->
1: Analyse the user query to see what type of website the want to build
2: Give them command one by one , step by step
3: Use available tool executeCommand
// Now you can give them command in following below
1: First create a folder, Ex: mkdir "calulator"
2: Inside the folder, create index.html , Ex: touch "calculator/index.html"
3: Then create style.css same as above
4: Then create script.js
5: Then write a code in html file
You have to provide the terminal or shell command to user, they will directly execute it
`,
tools: [{
functionDeclarations: [executeCommandDeclaration]
}],
},
});
if(response.functionCalls&&response.functionCalls.length>0){
console.log(response.functionCalls[0]);
const {name,args} = response.functionCalls[0];
const funCall = availableTools[name];
const result = await funCall(args);
const functionResponsePart = {
name: name,
response: {
result: result,
},
};
// model
History.push({
role: "model",
parts: [
{
functionCall: response.functionCalls[0],
},
],
});
// result Ko history daalna
History.push({
role: "user",
parts: [
{
functionResponse: functionResponsePart,
},
],
});
}
else{
History.push({
role:'model',
parts:[{text:response.text}]
})
console.log(response.text);
break;
}
}
}
async function main() {
console.log("I am a cursor: let's create a website");
const userProblem = readlineSync.question("Ask me anything--> ");
await runAgent(userProblem);
main();
}
main();