Spaces:
Running
Running
add the chat option with ai so i can tell the ai to code and create projects for me
Browse files- components/ai-agent.js +120 -33
components/ai-agent.js
CHANGED
|
@@ -7,12 +7,16 @@ class AIAgent extends HTMLElement {
|
|
| 7 |
this.conversation = [
|
| 8 |
{
|
| 9 |
role: 'assistant',
|
| 10 |
-
content: 'Hello! I\'m your AI coding assistant
|
| 11 |
}
|
| 12 |
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
}
|
| 14 |
-
|
| 15 |
-
connectedCallback() {
|
| 16 |
this.render();
|
| 17 |
this.setupEventListeners();
|
| 18 |
}
|
|
@@ -221,8 +225,8 @@ class AIAgent extends HTMLElement {
|
|
| 221 |
</div>
|
| 222 |
</div>
|
| 223 |
<div class="chat-input">
|
| 224 |
-
|
| 225 |
-
|
| 226 |
<i data-feather="send"></i>
|
| 227 |
</button>
|
| 228 |
</div>
|
|
@@ -356,40 +360,123 @@ class AIAgent extends HTMLElement {
|
|
| 356 |
|
| 357 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 358 |
}
|
| 359 |
-
|
| 360 |
async callGrokAPI(message) {
|
| 361 |
-
//
|
| 362 |
-
const API_KEY = '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';
|
| 363 |
-
|
| 364 |
try {
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
{ role: 'system', content: 'You are a helpful AI coding assistant. Provide concise, accurate coding advice and explanations.' },
|
| 375 |
-
...this.conversation.filter(msg => msg.role !== 'assistant' || msg.content !== 'Sorry, I encountered an error. Please try again.'),
|
| 376 |
-
{ role: 'user', content: message }
|
| 377 |
-
],
|
| 378 |
-
temperature: 0.7,
|
| 379 |
-
max_tokens: 500
|
| 380 |
-
})
|
| 381 |
-
});
|
| 382 |
-
|
| 383 |
-
if (!response.ok) {
|
| 384 |
-
throw new Error(`API request failed with status ${response.status}`);
|
| 385 |
}
|
| 386 |
|
| 387 |
-
|
| 388 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
} catch (error) {
|
| 390 |
-
console.error('Error
|
| 391 |
-
return 'Sorry, I encountered an error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
}
|
| 394 |
}
|
| 395 |
|
|
|
|
| 7 |
this.conversation = [
|
| 8 |
{
|
| 9 |
role: 'assistant',
|
| 10 |
+
content: 'Hello! I\'m your AI coding assistant. I can help you:\n\n1. Write code\n2. Debug issues\n3. Create new projects\n4. Explain concepts\n\nWhat would you like to work on today?'
|
| 11 |
}
|
| 12 |
];
|
| 13 |
+
this.projectContext = {
|
| 14 |
+
currentFile: null,
|
| 15 |
+
projectType: null,
|
| 16 |
+
language: null
|
| 17 |
+
};
|
| 18 |
}
|
| 19 |
+
connectedCallback() {
|
|
|
|
| 20 |
this.render();
|
| 21 |
this.setupEventListeners();
|
| 22 |
}
|
|
|
|
| 225 |
</div>
|
| 226 |
</div>
|
| 227 |
<div class="chat-input">
|
| 228 |
+
<input type="text" id="messageInput" placeholder="Ask me to create projects, write code, or explain concepts..." />
|
| 229 |
+
<button class="send-btn" id="sendBtn">
|
| 230 |
<i data-feather="send"></i>
|
| 231 |
</button>
|
| 232 |
</div>
|
|
|
|
| 360 |
|
| 361 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 362 |
}
|
|
|
|
| 363 |
async callGrokAPI(message) {
|
| 364 |
+
// Simulated API call - replace with actual API in production
|
|
|
|
|
|
|
| 365 |
try {
|
| 366 |
+
// Check for special commands
|
| 367 |
+
if (message.toLowerCase().includes('create project')) {
|
| 368 |
+
return this.handleProjectCreation(message);
|
| 369 |
+
} else if (message.toLowerCase().includes('write code')) {
|
| 370 |
+
return this.handleCodeRequest(message);
|
| 371 |
+
} else if (message.toLowerCase().includes('explain')) {
|
| 372 |
+
return this.handleExplanationRequest(message);
|
| 373 |
+
} else if (message.toLowerCase().includes('debug')) {
|
| 374 |
+
return this.handleDebugRequest(message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
}
|
| 376 |
|
| 377 |
+
// Default response
|
| 378 |
+
return `I can help you with coding tasks. Try asking me to:
|
| 379 |
+
- "Create a new React project"
|
| 380 |
+
- "Write a Python function to calculate factorial"
|
| 381 |
+
- "Explain how promises work in JavaScript"
|
| 382 |
+
- "Debug this code: [paste your code]"`;
|
| 383 |
} catch (error) {
|
| 384 |
+
console.error('Error:', error);
|
| 385 |
+
return 'Sorry, I encountered an error. Please try again.';
|
| 386 |
+
}
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
handleProjectCreation(message) {
|
| 390 |
+
// Extract project type from message
|
| 391 |
+
const projectType = message.match(/create (.*?) project/i)?.[1] || 'web';
|
| 392 |
+
this.projectContext.projectType = projectType;
|
| 393 |
+
|
| 394 |
+
let response = `I'll help you create a ${projectType} project. `;
|
| 395 |
+
|
| 396 |
+
switch(projectType.toLowerCase()) {
|
| 397 |
+
case 'react':
|
| 398 |
+
response += `Here's how to set up a basic React project:\n\n1. Create these files:\n- index.html\n- app.js\n- styles.css\n\n2. Add this to index.html:\n\`\`\`html\n<!DOCTYPE html>\n<html>\n<head>\n <title>React App</title>\n <link rel="stylesheet" href="styles.css">\n</head>\n<body>\n <div id="root"></div>\n <script src="app.js"></script>\n</body>\n</html>\n\`\`\`\n\n3. Add this to app.js:\n\`\`\`javascript\nimport React from 'react';\nimport ReactDOM from 'react-dom';\n\nfunction App() {\n return (\n <div>\n <h1>Hello React!</h1>\n </div>\n );\n}\n\nReactDOM.render(<App />, document.getElementById('root'));\n\`\`\``;
|
| 399 |
+
this.projectContext.language = 'javascript';
|
| 400 |
+
break;
|
| 401 |
+
case 'python':
|
| 402 |
+
response += `Here's a basic Python project structure:\n\n1. Create main.py:\n\`\`\`python\ndef main():\n print("Hello Python!")\n\nif __name__ == "__main__":\n main()\n\`\`\`\n\n2. Create requirements.txt for dependencies`;
|
| 403 |
+
this.projectContext.language = 'python';
|
| 404 |
+
break;
|
| 405 |
+
default:
|
| 406 |
+
response += `Here's a basic web project structure:\n\n1. Create index.html:\n\`\`\`html\n<!DOCTYPE html>\n<html>\n<head>\n <title>My Project</title>\n <link rel="stylesheet" href="styles.css">\n</head>\n<body>\n <h1>Hello World!</h1>\n <script src="script.js"></script>\n</body>\n</html>\n\`\`\`\n\n2. Create styles.css for styling\n3. Create script.js for JavaScript`;
|
| 407 |
+
this.projectContext.language = 'javascript';
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
return response;
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
handleCodeRequest(message) {
|
| 414 |
+
if (!this.projectContext.language) {
|
| 415 |
+
return "First tell me what kind of project you're working on (e.g., 'Create a React project') so I can provide language-specific help.";
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
const codeType = message.match(/write (.*?) code/i)?.[1] || 'function';
|
| 419 |
+
let codeExample = '';
|
| 420 |
+
|
| 421 |
+
switch(this.projectContext.language) {
|
| 422 |
+
case 'javascript':
|
| 423 |
+
if (codeType.includes('function')) {
|
| 424 |
+
codeExample = `\`\`\`javascript\n// Function example\nfunction ${this.getFunctionName(message)}(${this.getFunctionParams(message)}) {\n // ${this.getFunctionDescription(message)}\n return result;\n}\n\`\`\``;
|
| 425 |
+
} else if (codeType.includes('component')) {
|
| 426 |
+
codeExample = `\`\`\`javascript\n// React component example\nfunction ${this.getComponentName(message)}() {\n return (\n <div>\n <h1>${this.getComponentTitle(message)}</h1>\n </div>\n );\n}\n\`\`\``;
|
| 427 |
+
}
|
| 428 |
+
break;
|
| 429 |
+
case 'python':
|
| 430 |
+
codeExample = `\`\`\`python\ndef ${this.getFunctionName(message)}(${this.getFunctionParams(message)}):\n """\n ${this.getFunctionDescription(message)}\n """\n return result\n\`\`\``;
|
| 431 |
+
break;
|
| 432 |
+
default:
|
| 433 |
+
codeExample = `Here's a generic code example for your request:\n\`\`\`\n// Implement your ${codeType} here\n\`\`\``;
|
| 434 |
}
|
| 435 |
+
|
| 436 |
+
return `Here's ${this.projectContext.language} code for ${codeType}:\n\n${codeExample}\n\nWould you like me to explain any part of this?`;
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
handleExplanationRequest(message) {
|
| 440 |
+
const concept = message.match(/explain (.*)/i)?.[1] || 'this concept';
|
| 441 |
+
return `Here's an explanation of ${concept}:\n\n${this.getConceptExplanation(concept)}\n\nLet me know if you'd like more details or examples.`;
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
handleDebugRequest(message) {
|
| 445 |
+
const codeBlock = message.match(/```[\s\S]*?```/)?.[0] || 'your code';
|
| 446 |
+
return `Looking at ${codeBlock.includes('```') ? 'the code' : 'your code'}, here are potential issues to check:\n\n1. Syntax errors\n2. Variable scope issues\n3. Type mismatches\n4. Async/await problems\n\nFor more specific help, please share the exact error message you're getting.`;
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
// Helper methods
|
| 450 |
+
getFunctionName(message) {
|
| 451 |
+
return message.match(/function named (.*?)[\s,.]/i)?.[1] || 'exampleFunction';
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
getFunctionParams(message) {
|
| 455 |
+
return message.match(/parameters? (.*?)[\s,.]/i)?.[1] || 'param1, param2';
|
| 456 |
+
}
|
| 457 |
+
|
| 458 |
+
getFunctionDescription(message) {
|
| 459 |
+
return message.match(/that (.*?)[\s,.]/i)?.[1] || 'Does something useful';
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
getComponentName(message) {
|
| 463 |
+
return message.match(/component named (.*?)[\s,.]/i)?.[1] || 'ExampleComponent';
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
getComponentTitle(message) {
|
| 467 |
+
return message.match(/title (.*?)[\s,.]/i)?.[1] || 'My Component';
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
getConceptExplanation(concept) {
|
| 471 |
+
const explanations = {
|
| 472 |
+
'react': 'React is a JavaScript library for building user interfaces. It lets you create reusable UI components and efficiently update the DOM when data changes.',
|
| 473 |
+
'python': 'Python is a high-level, interpreted programming language known for its readability and versatility. It\'s great for web development, data analysis, AI, and more.',
|
| 474 |
+
'javascript': 'JavaScript is the programming language of the web. It runs in browsers and can manipulate HTML/CSS, handle user interactions, and make network requests.',
|
| 475 |
+
'promises': 'Promises in JavaScript represent the eventual completion (or failure) of an asynchronous operation and its resulting value. They help avoid callback hell.'
|
| 476 |
+
};
|
| 477 |
+
|
| 478 |
+
return explanations[concept.toLowerCase()] ||
|
| 479 |
+
`I'll explain ${concept} in simple terms...`;
|
| 480 |
}
|
| 481 |
}
|
| 482 |
|