Repository navigation
Implement AI design/spec generation flow and persist project specs #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bec4af5
feat: implement real-time AI chat and task status management with Sup…
xosnos e476bd2
feat: implement project specification storage, management API, and re…
xosnos b99e97d
Secure Supabase owner RPCs, grants, and AI worker auth (#12)
xosnos 9790b20
Update specs
xosnos 62942ce
Address CodeRabbit and Greptile review comments
xosnos d33c2ea
refactor: implement shared task status logic, optimize canvas dedupli…
xosnos 9185fd9
fix(ai): address remaining review concerns for design generation and …
xosnos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Local Ghost AI environment | ||
| # Copy to .env.local (Next.js) and supabase/functions/.env (Edge Functions). | ||
| # Never commit real keys. Hosted projects must use unique values, not these defaults. | ||
|
|
||
| NEXT_PUBLIC_SUPABASE_URL=http://127.0.0.1:54321 | ||
| NEXT_PUBLIC_SUPABASE_ANON_KEY= | ||
| SUPABASE_SERVICE_ROLE_KEY= | ||
|
|
||
| # Must match Vault secret `automations` (local seed sets local-dev-automation-secret). | ||
| AUTOMATION_SECRET=local-dev-automation-secret | ||
|
|
||
| # Optional named secret map used by Edge Functions (JSON object). | ||
| # SUPABASE_SECRET_KEYS={"automations":"local-dev-automation-secret"} | ||
|
|
||
| # OpenRouter key for design and spec workers. Set as an Edge Function secret in hosted projects. | ||
| OPENROUTER_API_KEY= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,91 @@ | ||
| # Ghost AI | ||
| # Ghost AI | ||
|
|
||
| Ghost AI is a collaborative system design workspace. Teams build architecture diagrams on a shared canvas, ask an AI agent to create or revise designs, and generate downloadable Markdown technical specifications. | ||
|
|
||
| ## Features | ||
|
|
||
| - Supabase Auth with protected project workspaces | ||
| - Owner and collaborator access controls | ||
| - React Flow canvas with realtime nodes, edges, cursors, selections, and presence | ||
| - Starter architecture templates and canvas autosave | ||
| - Durable AI design and spec generation with Supabase Queues, Cron, and Edge Functions | ||
| - OpenRouter powered architecture generation | ||
| - Persisted Markdown specs with preview and secure download | ||
| - Light and dark themes | ||
|
|
||
| Feature specifications 01 through 29 are implemented. See `context/progress-tracker.md` for acceptance gaps that still need follow-up. | ||
|
|
||
| ## Stack | ||
|
|
||
| - Next.js 15.4, React 19, and TypeScript | ||
| - Tailwind CSS 4 and shadcn style UI primitives | ||
| - React Flow | ||
| - Supabase Auth, Postgres, Realtime, Storage, Queues, Cron, and Edge Functions | ||
| - OpenRouter | ||
|
|
||
| ## Local setup | ||
|
|
||
| Requirements: | ||
|
|
||
| - Node.js 22 or later | ||
| - Docker | ||
| - Supabase CLI | ||
|
|
||
| Install dependencies and start Supabase: | ||
|
|
||
| ```bash | ||
| npm install | ||
| supabase start | ||
| ``` | ||
|
|
||
| Copy `.env.example` to `.env.local`. Fill the Supabase URL, anon key, and service role key from `supabase status`. Set `AUTOMATION_SECRET=local-dev-automation-secret` so it matches the Vault value from `supabase/seed.sql`. | ||
|
|
||
| Copy `.env.example` to `supabase/functions/.env`. Set `OPENROUTER_API_KEY`. For local development, keep `AUTOMATION_SECRET=local-dev-automation-secret` so it matches the value seeded into Supabase Vault. | ||
|
|
||
| Start the app: | ||
|
|
||
| ```bash | ||
| npm run dev | ||
| ``` | ||
|
|
||
| Open http://localhost:3000. Unauthenticated users are redirected to `/login`. | ||
|
|
||
| ## Environment variables | ||
|
|
||
| | Variable | Used by | Purpose | | ||
| | --- | --- | --- | | ||
| | `NEXT_PUBLIC_SUPABASE_URL` | Next.js client and server | Supabase API URL | | ||
| | `NEXT_PUBLIC_SUPABASE_ANON_KEY` | Next.js client and server | Browser safe Supabase key | | ||
| | `SUPABASE_SERVICE_ROLE_KEY` | Next.js server | Admin enrichment, task enqueueing, and worker invocation | | ||
| | `AUTOMATION_SECRET` | Next.js server and Edge Function | Authenticates worker fast path and Cron calls | | ||
| | `OPENROUTER_API_KEY` | Edge Function | AI model access | | ||
|
|
||
| Never expose the service role key, automation secret, or OpenRouter key to browser code. | ||
|
|
||
| ## Checks | ||
|
|
||
| ```bash | ||
| npm run lint | ||
| npm run build | ||
| ``` | ||
|
|
||
| ## Project layout | ||
|
|
||
| - `app/`: Next.js pages and route handlers | ||
| - `components/editor/`: canvas, workspace, AI, and spec UI | ||
| - `hooks/`: realtime, task status, autosave, and project hooks | ||
| - `lib/`: Supabase clients, access checks, and data helpers | ||
| - `supabase/functions/`: AI queue worker and generation handlers | ||
| - `supabase/migrations/`: database, RLS, Storage, Queue, and Cron changes | ||
| - `context/feature-specs/`: feature specifications 01 through 29 | ||
| - `context/`: product, architecture, UI, standards, and progress documentation | ||
|
|
||
| ## Hosted setup | ||
|
|
||
| Before enabling AI generation in a hosted Supabase project: | ||
|
|
||
| 1. Set a unique Vault secret named `automations`. | ||
| 2. Set the same value as the Edge Function secret `AUTOMATION_SECRET`. | ||
| 3. Set Vault secret `ai_worker_url` to the hosted `ai-worker` function URL. | ||
| 4. Set `OPENROUTER_API_KEY` as an Edge Function secret. | ||
| 5. Confirm the `ai-worker-recovery` Cron job receives HTTP 202 responses. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { createClient, getCurrentUser } from "@/lib/supabase/server"; | ||
| import { hasProjectAccess } from "@/lib/project-access"; | ||
| import { | ||
| enqueueTaskRun, | ||
| invokeAiWorkerFastPath, | ||
| ActiveTaskRunConflictError, | ||
| } from "@/lib/ai/task-runs"; | ||
|
|
||
| const MAX_DESIGN_PROMPT_LENGTH = 8_000; | ||
|
|
||
| export async function POST(req: NextRequest) { | ||
| let body: unknown; | ||
| try { | ||
| body = await req.json(); | ||
| } catch { | ||
| return NextResponse.json( | ||
| { error: "Invalid JSON request body" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| if (!body || typeof body !== "object") { | ||
| return NextResponse.json( | ||
| { error: "Request body must be a JSON object" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const { prompt, roomId } = body as { prompt?: unknown; roomId?: unknown }; | ||
|
|
||
| if (typeof prompt !== "string" || !prompt.trim()) { | ||
| return NextResponse.json( | ||
| { error: "A non-empty prompt is required" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| if (typeof roomId !== "string" || !roomId.trim()) { | ||
| return NextResponse.json( | ||
| { error: "A valid roomId is required" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const trimmedPrompt = prompt.trim(); | ||
| const trimmedRoomId = roomId.trim(); | ||
|
|
||
| if (trimmedPrompt.length > MAX_DESIGN_PROMPT_LENGTH) { | ||
| return NextResponse.json( | ||
| { error: `Prompt must be ${MAX_DESIGN_PROMPT_LENGTH} characters or fewer` }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // 1. Authenticate user | ||
| const supabase = await createClient(); | ||
| const user = await getCurrentUser(supabase); | ||
|
|
||
| if (!user) { | ||
| return NextResponse.json( | ||
| { error: "Unauthorized" }, | ||
| { status: 401 } | ||
| ); | ||
| } | ||
|
|
||
| // 2. Resolve project access from roomId (projectId) | ||
| const hasAccess = await hasProjectAccess(supabase, trimmedRoomId, { | ||
| userId: user.id, | ||
| email: user.email ?? "", | ||
| }); | ||
|
|
||
| if (!hasAccess) { | ||
| return NextResponse.json( | ||
| { error: "Project not found or access denied" }, | ||
| { status: 403 } | ||
| ); | ||
| } | ||
|
|
||
| // 3. Transactionally enqueue task run | ||
| let runId: string; | ||
| try { | ||
| runId = await enqueueTaskRun({ | ||
| projectId: trimmedRoomId, | ||
| userId: user.id, | ||
| kind: "design", | ||
| input: { | ||
| prompt: trimmedPrompt, | ||
| roomId: trimmedRoomId, | ||
| }, | ||
| }); | ||
| } catch (err: unknown) { | ||
| if (err instanceof ActiveTaskRunConflictError) { | ||
| return NextResponse.json( | ||
| { error: "An AI generation task is already active for this project" }, | ||
| { status: 409 } | ||
| ); | ||
| } | ||
| console.error("[POST /api/ai/design] Enqueue error:", err); | ||
| return NextResponse.json( | ||
| { error: "Failed to enqueue design generation task" }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
|
|
||
| // 4. Best-effort fast-path Edge Function invocation | ||
| // Fast-path invocation does not block or fail durability | ||
| invokeAiWorkerFastPath().catch((err) => { | ||
| console.error("[POST /api/ai/design] Fast-path invocation failed:", err); | ||
| }); | ||
|
|
||
| // 5. Return run ID with HTTP 202 Accepted | ||
| return NextResponse.json( | ||
| { runId }, | ||
| { status: 202 } | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.