-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoc_web.py
242 lines (197 loc) · 8.29 KB
/
poc_web.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import sys
import argparse
import os
import json
from typing import Dict, List, Tuple
from dataclasses import dataclass
import subprocess
import pytest
from llm.openrouter import OpenRouterLLM
from command.cli import parse_arguments, read_requirements
@dataclass
class WebComponent:
html: str
css: str
javascript: str
tests: str
class WebProject:
def __init__(self, workspace_dir: str, api_key: str):
self.workspace_dir = workspace_dir
self.llm = OpenRouterLLM(api_key)
def get_llm_response(self, role: str, prompt: str) -> str:
"""Get response from LLM based on role and prompt."""
system_messages = {
"web_developer": "You are a skilled frontend web developer. Write clean, efficient, and well-documented HTML, CSS, and JavaScript code.",
"ui_designer": "You are a UI designer. Create modern, responsive, and accessible web designs.",
"test_engineer": "You are a frontend test engineer. Write comprehensive Jest tests for JavaScript code and UI components.",
"architect": "You are a frontend architect. Suggest appropriate file structure and component organization."
}
messages = [
{"role": "system", "content": system_messages[role]},
{"role": "user", "content": prompt}
]
return self.llm.generate_response(messages)
def extract_code_blocks(self, response: str) -> Dict[str, str]:
"""Extract different types of code blocks from LLM response."""
code_blocks = {}
current_block = None
current_content = []
for line in response.split('\n'):
if line.startswith('```'):
if current_block:
code_blocks[current_block] = '\n'.join(current_content)
current_block = None
current_content = []
else:
block_type = line.replace('```', '').strip().lower()
if block_type in ['html', 'css', 'javascript', 'js', 'jest']:
current_block = 'javascript' if block_type in ['js', 'jest'] else block_type
elif current_block:
current_content.append(line)
if current_block:
code_blocks[current_block] = '\n'.join(current_content)
return code_blocks
def setup_project_structure(self) -> Tuple[str, str, str, str]:
"""Create project directory structure and return file paths."""
os.makedirs(self.workspace_dir, exist_ok=True)
# Create directories
src_dir = os.path.join(self.workspace_dir, 'src')
test_dir = os.path.join(self.workspace_dir, 'tests')
os.makedirs(src_dir, exist_ok=True)
os.makedirs(test_dir, exist_ok=True)
# Define file paths
html_file = os.path.join(src_dir, 'index.html')
css_file = os.path.join(src_dir, 'styles.css')
js_file = os.path.join(src_dir, 'app.js')
test_file = os.path.join(test_dir, 'app.test.js')
return html_file, css_file, js_file, test_file
def generate_web_component(self, requirements: str) -> WebComponent:
"""Generate HTML, CSS, JavaScript and tests based on requirements."""
# Generate HTML
html_prompt = f"""
Create a modern, responsive HTML structure for these requirements:
{requirements}
Include proper meta tags, semantic HTML5 elements, and accessibility attributes.
Respond with only the HTML code block.
"""
html_response = self.get_llm_response("web_developer", html_prompt)
html_code = self.extract_code_blocks(html_response).get('html', '')
# Generate CSS
css_prompt = f"""
Create responsive CSS styles for this HTML:
```html
{html_code}
```
Based on these requirements:
{requirements}
Use modern CSS features, flexbox/grid, and mobile-first approach.
Include media queries for responsiveness.
"""
css_response = self.get_llm_response("ui_designer", css_prompt)
css_code = self.extract_code_blocks(css_response).get('css', '')
# Generate JavaScript
js_prompt = f"""
Create JavaScript code to implement the interactive functionality for:
{requirements}
Work with this HTML:
```html
{html_code}
```
Use modern JavaScript (ES6+) features and best practices.
Include error handling and input validation.
"""
js_response = self.get_llm_response("web_developer", js_prompt)
js_code = self.extract_code_blocks(js_response).get('javascript', '')
# Generate Tests
test_prompt = f"""
Write Jest tests for this JavaScript code:
```javascript
{js_code}
```
Include:
- Unit tests for functions
- DOM manipulation tests
- Event handling tests
- Error cases
- Mock API calls if needed
"""
test_response = self.get_llm_response("test_engineer", test_prompt)
test_code = self.extract_code_blocks(test_response).get('javascript', '')
return WebComponent(html_code, css_code, js_code, test_code)
def write_files(self, component: WebComponent, file_paths: Tuple[str, str, str, str]):
"""Write generated code to files."""
html_file, css_file, js_file, test_file = file_paths
# Write HTML
with open(html_file, 'w') as f:
f.write(component.html)
# Write CSS
with open(css_file, 'w') as f:
f.write(component.css)
# Write JavaScript
with open(js_file, 'w') as f:
f.write(component.javascript)
# Write Tests
with open(test_file, 'w') as f:
f.write(component.tests)
def setup_testing_environment(self):
"""Set up Jest and necessary dependencies for testing."""
package_json = {
"name": "web-project",
"version": "1.0.0",
"scripts": {
"test": "jest"
},
"devDependencies": {
"jest": "^29.0.0",
"jest-environment-jsdom": "^29.0.0",
"@babel/preset-env": "^7.20.0"
},
"jest": {
"testEnvironment": "jsdom"
}
}
# Write package.json
with open(os.path.join(self.workspace_dir, 'package.json'), 'w') as f:
json.dump(package_json, f, indent=2)
# Install dependencies
subprocess.run(['npm', 'install'], cwd=self.workspace_dir)
def run_tests(self):
"""Run Jest tests."""
try:
subprocess.run(['npm', 'test'], cwd=self.workspace_dir, check=True)
except subprocess.CalledProcessError as e:
print(f"Error running tests: {e}")
class WebProjectManager:
def __init__(self, workspace_dir: str, api_key: str):
self.project = WebProject(workspace_dir, api_key)
def create_project(self, requirements: str):
"""Create a new web project with the given requirements."""
try:
print("Setting up project structure...")
file_paths = self.project.setup_project_structure()
print("Generating web components...")
component = self.project.generate_web_component(requirements)
print("Writing files...")
self.project.write_files(component, file_paths)
print("Setting up testing environment...")
self.project.setup_testing_environment()
print("Running tests...")
self.project.run_tests()
print("\nProject generation completed!")
print(f"Project location: {self.project.workspace_dir}")
except Exception as e:
print(f"Error creating project: {e}")
def main():
args = parse_arguments()
# Get API key from command line or environment
api_key = args.api_key or os.getenv('OPENROUTER_API_KEY')
if not api_key:
print("Error: OpenRouter API key not provided. Use --api-key or set OPENROUTER_API_KEY environment variable.")
sys.exit(1)
# Read requirements
requirements = read_requirements(args.requirements)
# Create and run project
manager = WebProjectManager(args.workspace, api_key)
manager.create_project(requirements)
if __name__ == "__main__":
main()