Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions app/api/build-app/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextRequest } from 'next/server'
import Anthropic from '@anthropic-ai/sdk'
import { getCurrentUser } from '@/lib/auth'
import { getAnthropicModel } from '@/lib/anthropic-model'
import { getBillingState } from '@/lib/billing'
import type { AppBlueprint } from '@/lib/queries'

let __anthropicClient: Anthropic | null = null
Expand Down Expand Up @@ -140,8 +141,8 @@ async function pushFileToGitHub(
)

if (!res.ok) {
const err = (await res.json()) as { message?: string }
console.warn(`[build-app] Failed to push ${path}: ${err.message}`)
const err = (await res.json().catch(() => ({}))) as { message?: string }
throw new Error(`Failed to push ${path}: ${err.message ?? res.statusText}`)
}
}

Expand Down Expand Up @@ -202,8 +203,8 @@ async function pushFileToGitLab(
)

if (!res.ok) {
const err = (await res.json()) as { message?: string }
console.warn(`[build-app] Failed to push ${path} to GitLab: ${err.message}`)
const err = (await res.json().catch(() => ({}))) as { message?: string }
throw new Error(`Failed to push ${path} to GitLab: ${err.message ?? res.statusText}`)
}
}

Expand All @@ -223,6 +224,13 @@ export async function POST(request: NextRequest) {
return
}

const billing = await getBillingState(user)
if (!billing.canAccessPro) {
send({ step: 'error', message: 'Build This App requires an active Pro subscription.' })
controller.close()
return
}

const body = (await request.json()) as BuildAppRequest
const { platform, repoName, blueprint } = body

Expand Down Expand Up @@ -293,19 +301,31 @@ export async function POST(request: NextRequest) {

// Step 3 — push files
let pushed = 0
for (const [path, content] of fileEntries) {
if (platform === 'github') {
await pushFileToGitHub(accessToken, user.github_username, cleanRepoName, path, content)
} else if (gitlabProjectId !== null) {
await pushFileToGitLab(accessToken, gitlabProjectId, gitlabBranch, path, content)
try {
for (const [path, content] of fileEntries) {
if (platform === 'github') {
await pushFileToGitHub(accessToken, user.github_username, cleanRepoName, path, content)
} else if (gitlabProjectId !== null) {
await pushFileToGitLab(accessToken, gitlabProjectId, gitlabBranch, path, content)
}
pushed++
send({
step: 'pushing',
message: `Pushing files… (${pushed}/${fileEntries.length})`,
current: pushed,
total: fileEntries.length,
repoUrl,
})
}
pushed++
} catch (e) {
send({
step: 'pushing',
message: `Pushing files… (${pushed}/${fileEntries.length})`,
current: pushed,
total: fileEntries.length,
step: 'error',
message: e instanceof Error ? e.message : 'Failed to push generated files.',
repoUrl,
filesCreated: pushed,
})
controller.close()
return
}

send({
Expand Down
Loading
Loading