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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
TITLE = "AppDraft"
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def slugify(value: str) -> str:
|
| 8 |
+
value = (value or "my-app").strip().lower()
|
| 9 |
+
out = []
|
| 10 |
+
for ch in value:
|
| 11 |
+
if ch.isalnum():
|
| 12 |
+
out.append(ch)
|
| 13 |
+
elif ch in {" ", "_", ".", "/", "-"}:
|
| 14 |
+
out.append("-")
|
| 15 |
+
slug = "".join(out)
|
| 16 |
+
while "--" in slug:
|
| 17 |
+
slug = slug.replace("--", "-")
|
| 18 |
+
return slug.strip("-") or "my-app"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def build_pack(app_idea, stack, platform, style, auth, database, extras):
|
| 22 |
+
app_name = (app_idea or "My App").strip().split("\n")[0][:60] or "My App"
|
| 23 |
+
slug = slugify(app_name)
|
| 24 |
+
extras = extras or []
|
| 25 |
+
|
| 26 |
+
feature_pool = [
|
| 27 |
+
"Authentication and user onboarding" if auth != "No auth" else "Guest-friendly onboarding and local session flow",
|
| 28 |
+
"Dashboard or home view with quick actions",
|
| 29 |
+
"Create, edit, delete core records",
|
| 30 |
+
"Search, filter, and sort data",
|
| 31 |
+
"Responsive mobile-friendly UI",
|
| 32 |
+
"Error, loading, and empty states",
|
| 33 |
+
"Analytics or activity history",
|
| 34 |
+
]
|
| 35 |
+
if "Payments" in extras:
|
| 36 |
+
feature_pool.append("Subscriptions, billing, and checkout flow")
|
| 37 |
+
if "Notifications" in extras:
|
| 38 |
+
feature_pool.append("Email or in-app notification system")
|
| 39 |
+
if "Admin panel" in extras:
|
| 40 |
+
feature_pool.append("Role-based admin panel and moderation tools")
|
| 41 |
+
if "AI features" in extras:
|
| 42 |
+
feature_pool.append("AI-powered generation, summarization, or recommendation flow")
|
| 43 |
+
if "File uploads" in extras:
|
| 44 |
+
feature_pool.append("Secure file upload and storage pipeline")
|
| 45 |
+
if "Realtime" in extras:
|
| 46 |
+
feature_pool.append("Realtime collaboration or live activity updates")
|
| 47 |
+
|
| 48 |
+
pages = [
|
| 49 |
+
"Landing / marketing page",
|
| 50 |
+
"Sign in / sign up" if auth != "No auth" else "Welcome / guest entry",
|
| 51 |
+
"Main dashboard",
|
| 52 |
+
"List view",
|
| 53 |
+
"Detail view",
|
| 54 |
+
"Create / edit form",
|
| 55 |
+
"Settings page",
|
| 56 |
+
]
|
| 57 |
+
if "Admin panel" in extras:
|
| 58 |
+
pages.append("Admin dashboard")
|
| 59 |
+
if "Payments" in extras:
|
| 60 |
+
pages.append("Billing page")
|
| 61 |
+
|
| 62 |
+
components = [
|
| 63 |
+
"Navbar / app shell",
|
| 64 |
+
"Sidebar or tab navigation",
|
| 65 |
+
"Hero / empty state block",
|
| 66 |
+
"Cards and data tables",
|
| 67 |
+
"Modal / drawer for quick actions",
|
| 68 |
+
"Toast / alert system",
|
| 69 |
+
"Form inputs with validation",
|
| 70 |
+
]
|
| 71 |
+
|
| 72 |
+
tables = [
|
| 73 |
+
{"table": "users", "fields": ["id", "email", "password_hash", "role", "created_at"] if auth != "No auth" else ["id", "device_id", "display_name", "created_at"]},
|
| 74 |
+
{"table": "projects", "fields": ["id", "owner_id", "title", "description", "status", "created_at", "updated_at"]},
|
| 75 |
+
{"table": "items", "fields": ["id", "project_id", "title", "content", "type", "created_at", "updated_at"]},
|
| 76 |
+
{"table": "activity_logs", "fields": ["id", "user_id", "action", "entity_type", "entity_id", "created_at"]},
|
| 77 |
+
]
|
| 78 |
+
if "Payments" in extras:
|
| 79 |
+
tables.append({"table": "subscriptions", "fields": ["id", "user_id", "plan", "status", "provider_customer_id", "renewal_date"]})
|
| 80 |
+
if "Notifications" in extras:
|
| 81 |
+
tables.append({"table": "notifications", "fields": ["id", "user_id", "type", "title", "body", "read_at", "created_at"]})
|
| 82 |
+
if "File uploads" in extras:
|
| 83 |
+
tables.append({"table": "files", "fields": ["id", "owner_id", "project_id", "filename", "mime_type", "storage_url", "created_at"]})
|
| 84 |
+
|
| 85 |
+
endpoints = [
|
| 86 |
+
"POST /api/auth/register" if auth != "No auth" else "POST /api/session/start",
|
| 87 |
+
"POST /api/auth/login" if auth != "No auth" else "GET /api/session",
|
| 88 |
+
"GET /api/projects",
|
| 89 |
+
"POST /api/projects",
|
| 90 |
+
"GET /api/projects/:id",
|
| 91 |
+
"PUT /api/projects/:id",
|
| 92 |
+
"DELETE /api/projects/:id",
|
| 93 |
+
"GET /api/items?projectId=:id",
|
| 94 |
+
"POST /api/items",
|
| 95 |
+
]
|
| 96 |
+
if "Payments" in extras:
|
| 97 |
+
endpoints += ["POST /api/billing/create-checkout-session", "GET /api/billing/subscription"]
|
| 98 |
+
if "Notifications" in extras:
|
| 99 |
+
endpoints += ["GET /api/notifications", "POST /api/notifications/mark-read"]
|
| 100 |
+
if "File uploads" in extras:
|
| 101 |
+
endpoints += ["POST /api/files/upload", "DELETE /api/files/:id"]
|
| 102 |
+
|
| 103 |
+
folder_tree = f"""
|
| 104 |
+
{slug}/
|
| 105 |
+
βββ app/
|
| 106 |
+
β βββ components/
|
| 107 |
+
β βββ pages/
|
| 108 |
+
β βββ hooks/
|
| 109 |
+
β βββ services/
|
| 110 |
+
β βββ lib/
|
| 111 |
+
β βββ styles/
|
| 112 |
+
βββ server/
|
| 113 |
+
β βββ routes/
|
| 114 |
+
β βββ controllers/
|
| 115 |
+
β βββ middleware/
|
| 116 |
+
β βββ models/
|
| 117 |
+
βββ database/
|
| 118 |
+
β βββ schema.sql
|
| 119 |
+
β βββ seed.sql
|
| 120 |
+
βββ public/
|
| 121 |
+
βββ tests/
|
| 122 |
+
βββ .env.example
|
| 123 |
+
βββ README.md
|
| 124 |
+
βββ package.json
|
| 125 |
+
""".strip()
|
| 126 |
+
|
| 127 |
+
starter_code = f'''# {app_name}
|
| 128 |
+
|
| 129 |
+
## Suggested stack
|
| 130 |
+
- Frontend: {stack}
|
| 131 |
+
- Platform: {platform}
|
| 132 |
+
- UI style: {style}
|
| 133 |
+
- Auth: {auth}
|
| 134 |
+
- Database: {database}
|
| 135 |
+
|
| 136 |
+
## Core app shell
|
| 137 |
+
```tsx
|
| 138 |
+
export default function AppShell() {{
|
| 139 |
+
return (
|
| 140 |
+
<main>
|
| 141 |
+
<h1>{app_name}</h1>
|
| 142 |
+
<p>Build the main workflow here.</p>
|
| 143 |
+
</main>
|
| 144 |
+
);
|
| 145 |
+
}}
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
## Example API contract
|
| 149 |
+
```ts
|
| 150 |
+
export async function getProjects() {{
|
| 151 |
+
const res = await fetch('/api/projects');
|
| 152 |
+
if (!res.ok) throw new Error('Failed to load projects');
|
| 153 |
+
return res.json();
|
| 154 |
+
}}
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
## SQL starter
|
| 158 |
+
```sql
|
| 159 |
+
CREATE TABLE projects (
|
| 160 |
+
id UUID PRIMARY KEY,
|
| 161 |
+
owner_id UUID NOT NULL,
|
| 162 |
+
title TEXT NOT NULL,
|
| 163 |
+
description TEXT,
|
| 164 |
+
status TEXT DEFAULT 'draft',
|
| 165 |
+
created_at TIMESTAMP DEFAULT NOW(),
|
| 166 |
+
updated_at TIMESTAMP DEFAULT NOW()
|
| 167 |
+
);
|
| 168 |
+
```
|
| 169 |
+
'''
|
| 170 |
+
|
| 171 |
+
prd = f"""
|
| 172 |
+
# Product Brief
|
| 173 |
+
|
| 174 |
+
## App Name
|
| 175 |
+
{app_name}
|
| 176 |
+
|
| 177 |
+
## One-line idea
|
| 178 |
+
{app_idea}
|
| 179 |
+
|
| 180 |
+
## Target platform
|
| 181 |
+
{platform}
|
| 182 |
+
|
| 183 |
+
## Tech stack
|
| 184 |
+
{stack}
|
| 185 |
+
|
| 186 |
+
## Experience goal
|
| 187 |
+
Create a {style.lower()} product that feels fast, clear, and easy to expand in an IDE.
|
| 188 |
+
|
| 189 |
+
## Must-have features
|
| 190 |
+
""" + "\n".join(f"- {item}" for item in feature_pool[:8])
|
| 191 |
+
|
| 192 |
+
ui_plan = "# UI Plan\n\n## Pages\n" + "\n".join(f"- {p}" for p in pages) + "\n\n## Components\n" + "\n".join(f"- {c}" for c in components)
|
| 193 |
+
|
| 194 |
+
db_plan = "# Database Plan\n\n" + "\n\n".join(
|
| 195 |
+
f"## {t['table']}\n" + "\n".join(f"- {field}" for field in t["fields"]) for t in tables
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
api_plan = "# API Routes\n\n" + "\n".join(f"- {route}" for route in endpoints)
|
| 199 |
+
|
| 200 |
+
system_prompt = f"""
|
| 201 |
+
You are a senior product engineer and UX architect.
|
| 202 |
+
Generate implementation-ready output for this app idea: {app_idea}
|
| 203 |
+
Stack: {stack}
|
| 204 |
+
Platform: {platform}
|
| 205 |
+
Style: {style}
|
| 206 |
+
Auth: {auth}
|
| 207 |
+
Database: {database}
|
| 208 |
+
Extras: {', '.join(extras) if extras else 'None'}
|
| 209 |
+
Return:
|
| 210 |
+
1. Product requirements
|
| 211 |
+
2. User stories
|
| 212 |
+
3. Information architecture
|
| 213 |
+
4. Components
|
| 214 |
+
5. Database schema
|
| 215 |
+
6. API routes
|
| 216 |
+
7. Folder structure
|
| 217 |
+
8. Starter code
|
| 218 |
+
Keep output concise, structured, and copy-paste ready.
|
| 219 |
+
""".strip()
|
| 220 |
+
|
| 221 |
+
json_export = json.dumps({
|
| 222 |
+
"name": app_name,
|
| 223 |
+
"slug": slug,
|
| 224 |
+
"stack": stack,
|
| 225 |
+
"platform": platform,
|
| 226 |
+
"style": style,
|
| 227 |
+
"auth": auth,
|
| 228 |
+
"database": database,
|
| 229 |
+
"extras": extras,
|
| 230 |
+
"features": feature_pool,
|
| 231 |
+
"pages": pages,
|
| 232 |
+
"components": components,
|
| 233 |
+
"tables": tables,
|
| 234 |
+
"endpoints": endpoints,
|
| 235 |
+
"folder_tree": folder_tree,
|
| 236 |
+
}, indent=2)
|
| 237 |
+
|
| 238 |
+
return prd, ui_plan, db_plan, api_plan, folder_tree, starter_code, system_prompt, json_export
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
css = """
|
| 242 |
+
:root {
|
| 243 |
+
--bg: #0b1020;
|
| 244 |
+
--panel: #121937;
|
| 245 |
+
--panel-2: #1a234a;
|
| 246 |
+
--line: #2a3569;
|