fix deploy
Browse files162 unmodified lines16316416516616716616716816917017117217317417517617717816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221318018121421521621721821918818919019119219319419522022122222322422522622722822920120223023123223320520620720823423523623723823924024124211 unmodified lines2542552562262572582592292302312602612622632642655 unmodified lines271272273243274275276277162 unmodified lines return; }
// Determine if we're updating an existing space or creating a new one let existingRepoId = currentRepoId; // SAME LOGIC AS GRADIO VERSION: Parse message history to find existing space let existingSpace: string | null = null; // If we have a current repo, check if user owns it if (currentRepoId && username) { const ownsSpace = currentRepoId.startsWith(`${username}/`); if (ownsSpace) { // Update existing space without asking console.log('[Deploy] Updating existing space:', currentRepoId); } else { // User doesn't own the imported space, create a new one existingRepoId = null; console.log('[Deploy] Creating new space (user does not own imported space)'); // Look for previous deployment or imported space in history if (messages.length > 0 && username) { console.log('[Deploy] Scanning message history for existing deployments...'); for (let i = messages.length - 1; i >= 0; i--) { const msg = messages[i]; // Check for deployment messages if (msg.role === 'assistant' && msg.content.includes('β
Deployed')) { const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/); if (match) { existingSpace = match[1]; console.log('[Deploy] Found previous deployment:', existingSpace); break; } } // Check for update messages if (msg.role === 'assistant' && msg.content.includes('β
Updated')) { const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/); if (match) { existingSpace = match[1]; console.log('[Deploy] Found previous update:', existingSpace); break; } } // Check for imported space messages - THE KEY PART! if (msg.role === 'user' && msg.content.startsWith('Imported Space from')) { const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/); if (match) { const importedSpace = match[1]; // Only use imported space if user owns it (can update it) if (importedSpace.startsWith(`${username}/`)) { existingSpace = importedSpace; console.log('[Deploy] Found imported space (user owns it):', existingSpace); break; } else { console.log('[Deploy] Found imported space but user does not own it:', importedSpace); // If user doesn't own the imported space, we'll create a new one // (existingSpace remains null, triggering new deployment) } } } } } else { console.log('[Deploy] Creating new space (no current repo)'); }
// Auto-generate space name (never prompt user) let spaceName = undefined; // undefined = backend will auto-generate
try { console.log('[Deploy] ========== DEPLOY START =========='); console.log('[Deploy] Current username:', username); console.log('[Deploy] Current repo ID (state):', currentRepoId); console.log('[Deploy] Existing repo ID (var):', existingRepoId); console.log('[Deploy] Space name:', spaceName); console.log('[Deploy] Language:', selectedLanguage); console.log('[Deploy] Code length:', generatedCode.length); console.log('[Deploy] ========================================'); console.log('[Deploy] ========== DEPLOY START (Gradio-style history parsing) =========='); console.log('[Deploy] Username:', username); console.log('[Deploy] Existing space from history:', existingSpace); console.log('[Deploy] Will create new space?', !existingSpace); console.log('[Deploy] ================================================================='); const deployRequest = { code: generatedCode, space_name: spaceName, language: selectedLanguage, existing_repo_id: existingRepoId || undefined, commit_message: existingRepoId ? 'Update via AnyCoder' : undefined, existing_repo_id: existingSpace || undefined, commit_message: existingSpace ? 'Update via AnyCoder' : undefined, }; console.log('[Deploy] π Sending request to backend:', JSON.stringify({ ...deployRequest, code: `${deployRequest.code.substring(0, 100)}... (${deployRequest.code.length} chars)` }, null, 2)); console.log('[Deploy] π Sending to backend:', { existing_repo_id: deployRequest.existing_repo_id, space_name: deployRequest.space_name, language: deployRequest.language, has_code: !!deployRequest.code }); const response = await apiClient.deploy(deployRequest);
11 unmodified lines } } // Add deployment message to chat // Add deployment message to chat (matches Gradio format) const deployMessage: Message = { role: 'assistant', content: existingRepoId ? `β
Updated space: ${response.space_url}` : `β
Published to: ${response.space_url}`, content: existingSpace ? `β
Updated! View at: ${response.space_url}` : `β
Deployed! View at: ${response.space_url}`, timestamp: new Date().toISOString(), }; setMessages((prev) => [...prev, deployMessage]);5 unmodified lines const isDev = response.dev_mode; const message = isDev ? 'π Opening HuggingFace Spaces creation page...\nPlease complete the space setup in the new tab.' : existingRepoId : existingSpace ? `β
Updated successfully!\n\nOpening: ${response.space_url}` : `β
Published successfully!\n\nOpening: ${response.space_url}`; alert(message);frontend/src/app/page.tsx+64-33