-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickapp.py
More file actions
266 lines (213 loc) · 8.72 KB
/
quickapp.py
File metadata and controls
266 lines (213 loc) · 8.72 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
"""
QuickApp - AI-Powered Web App Generator
Main CLI application
"""
import asyncio
import os
import sys
import shutil
from dotenv import load_dotenv
from agent import CodeGenAgent
from ui import (
Spinner,
print_header,
print_context_usage,
print_separator,
print_success,
print_error,
print_info,
print_warning,
print_thought,
get_user_input,
)
class QuickAppCLI:
"""Main CLI application"""
def __init__(self, initial_app: str | None = None):
# Load environment variables
load_dotenv()
# Initialize components
self.agent: CodeGenAgent | None = None
self.running = True
self.current_app_dir = initial_app
if self.current_app_dir:
# Normalize path
if not os.path.isabs(self.current_app_dir) and not self.current_app_dir.startswith("apps/"):
# If just a name is given, assume it's in apps/
self.current_app_dir = os.path.join("apps", self.current_app_dir)
if not os.path.exists(self.current_app_dir):
print_warning(f"Note: Path {self.current_app_dir} does not exist yet. It will be created on first prompt.")
def initialize_agent(self):
"""Initialize the AI agent"""
try:
model = os.getenv("QUICKAPP_MODEL", "mistral:mistral-small-latest")
self.agent = CodeGenAgent(model_str=model)
print_success(f"Initialized agent with model: {model}")
except Exception as e:
print_error(f"Failed to initialize agent: {e}")
sys.exit(1)
# Check for uv
if shutil.which("uv") is None:
print_error("❌ 'uv' is not installed or not in PATH.")
print_info("Please install uv: https://docs.astral.sh/uv/getting-started/installation/")
sys.exit(1)
async def process_prompt(self, prompt: str):
"""Process user prompt and generate app"""
# 1. Determine app directory
if not self.current_app_dir:
# Let the LLM decide the name
candidate_name = await self.agent.get_suggested_name(prompt)
print_info(f"🆕 Starting new app: {candidate_name}")
self.current_app_dir = os.path.join("apps", candidate_name)
os.makedirs(self.current_app_dir, exist_ok=True)
else:
print_info(f"🔄 updating app in: {self.current_app_dir}")
# Remove spinner, let the agent print its actions
spinner = Spinner("Agent is thinking")
def handle_agent_log(msg: str):
"""Handle logs from the agent by pausing spinner"""
if spinner.running:
spinner.stop()
print_info(msg)
spinner.start()
else:
print_info(msg)
try:
# print_info("🧠 Agent is thinking...")
spinner.start()
# print_separator()
# Configure agent logging
self.agent.set_log_callback(handle_agent_log)
# Run the autonomous agent
result = await self.agent.run_task(prompt, self.current_app_dir)
spinner.stop()
print_success("Agent finished the task")
# Parse and display thoughts
import re
thought_match = re.search(r'<thought>(.*?)</thought>', str(result), re.DOTALL)
if thought_match:
thought = thought_match.group(1).strip()
print_thought(thought)
# Remove thought from final message
result_clean = re.sub(r'<thought>.*?</thought>', '', str(result), flags=re.DOTALL).strip()
else:
result_clean = str(result).strip()
print_info("Agent Message:")
print(f"\n{result_clean}\n")
# Show how to run it
print_separator()
print_info(f"App location: {self.current_app_dir}")
print_info("To run the app:")
print_success(f"cd {self.current_app_dir}")
print_success("uv run uvicorn main:app --reload")
print_separator()
# Show context usage
current, max_tokens = self.agent.get_context_usage()
print_context_usage(current, max_tokens)
except Exception as e:
spinner.stop()
print_error(f"Agent building failed: {e}")
import traceback
traceback.print_exc()
def handle_command(self, user_input: str) -> bool:
"""
Handle special commands
Returns:
True if command was handled, False otherwise
"""
command = user_input.lower().strip()
if command in ['exit', 'quit', 'q']:
print_info("Goodbye! 👋")
self.running = False
return True
if command == 'clear':
self.agent.clear_history()
print_success("Conversation history cleared")
return True
if command == 'new':
self.current_app_dir = None
self.agent.clear_history()
print_success("Started a new session. Next prompt will create a new app.")
return True
if command == 'help':
self.show_help()
return True
if command == 'status':
self.show_status()
return True
if command.startswith('open '):
path = user_input[5:].strip()
if not os.path.isabs(path) and not path.startswith("apps/"):
path = os.path.join("apps", path)
self.current_app_dir = path
os.makedirs(self.current_app_dir, exist_ok=True)
print_success(f"Context switched to: {self.current_app_dir}")
return True
return False
def show_help(self):
"""Show help message"""
print_separator()
print("\n📖 QuickApp Commands:\n")
print(" [prompt] - Describe the app you want to create")
print(" open [name] - Switch to an existing app in apps/")
print(" new - Start a new app (reset context)")
print(" clear - Clear conversation history")
print(" status - Show current context usage")
print(" help - Show this help message")
print(" exit/quit - Exit QuickApp")
print()
print_separator()
def show_status(self):
"""Show current status"""
current, max_tokens = self.agent.get_context_usage()
message_count = self.agent.get_message_count()
print_separator()
print(f"\n📊 Status:")
print(f" Current App: {self.current_app_dir or 'None'}")
print(f" Messages in history: {message_count}")
print_context_usage(current, max_tokens)
print_separator()
async def run(self):
"""Main application loop"""
print_header()
print_info("Initializing QuickApp...")
self.initialize_agent()
if self.current_app_dir:
print_info(f"📁 Resuming work in: {self.current_app_dir}")
print_separator()
print("\n💡 Tip: Describe the web app you want to create")
print(" Example: 'Create a to-do list app'")
print(" Type 'help' for commands\n")
print_separator()
while self.running:
try:
# Get user input
user_input = get_user_input()
if not user_input:
continue
# Handle commands
if self.handle_command(user_input):
continue
# Process as app generation prompt
await self.process_prompt(user_input)
except KeyboardInterrupt:
print("\n")
print_info("Use 'exit' to quit")
continue
except EOFError:
print("\n")
break
except Exception as e:
print_error(f"Unexpected error: {e}")
import traceback
traceback.print_exc()
def main():
"""Entry point"""
import argparse
parser = argparse.ArgumentParser(description="QuickApp CLI")
parser.add_argument("app_path", nargs="?", help="Optional path to an existing app directory")
args = parser.parse_args()
cli = QuickAppCLI(initial_app=args.app_path)
asyncio.run(cli.run())
if __name__ == "__main__":
main()