fix deploy
Browse files162 unmodified lines16316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021111 unmodified lines2232242252262272282292302312322332345 unmodified lines240241242243244245246162 unmodified linesreturn;}// Determine if we're updating an existing space or creating a new onelet existingRepoId = currentRepoId;// If we have a current repo, check if user owns itif (currentRepoId && username) {const ownsSpace = currentRepoId.startsWith(`${username}/`);if (ownsSpace) {// Update existing space without askingconsole.log('[Deploy] Updating existing space:', currentRepoId);} else {// User doesn't own the imported space, create a new oneexistingRepoId = null;console.log('[Deploy] Creating new space (user does not own imported space)');}} else {console.log('[Deploy] Creating new space (no current repo)');}// Auto-generate space name (never prompt user)let spaceName = undefined; // undefined = backend will auto-generatetry {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] ========================================');const deployRequest = {code: generatedCode,space_name: spaceName,language: selectedLanguage,existing_repo_id: existingRepoId || undefined,commit_message: existingRepoId ? '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));const response = await apiClient.deploy(deployRequest);11 unmodified lines}}// Add deployment message to chatconst deployMessage: Message = {role: 'assistant',content: existingRepoId? `β Updated space: ${response.space_url}`: `β Published to: ${response.space_url}`,timestamp: new Date().toISOString(),};setMessages((prev) => [...prev, deployMessage]);5 unmodified linesconst isDev = response.dev_mode;const message = isDev? 'π Opening HuggingFace Spaces creation page...\nPlease complete the space setup in the new tab.': existingRepoId? `β Updated successfully!\n\nOpening: ${response.space_url}`: `β Published successfully!\n\nOpening: ${response.space_url}`;alert(message);162 unmodified lines16316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124211 unmodified lines2542552562572582592602612622632642655 unmodified lines271272273274275276277162 unmodified linesreturn;}// SAME LOGIC AS GRADIO VERSION: Parse message history to find existing spacelet existingSpace: string | null = null;// Look for previous deployment or imported space in historyif (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 messagesif (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 messagesif (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)}}}}}// Auto-generate space name (never prompt user)let spaceName = undefined; // undefined = backend will auto-generatetry {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: existingSpace || undefined,commit_message: existingSpace ? 'Update via AnyCoder' : undefined,};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 (matches Gradio format)const deployMessage: Message = {role: 'assistant',content: existingSpace? `β Updated! View at: ${response.space_url}`: `β Deployed! View at: ${response.space_url}`,timestamp: new Date().toISOString(),};setMessages((prev) => [...prev, deployMessage]);5 unmodified linesconst isDev = response.dev_mode;const message = isDev? 'π Opening HuggingFace Spaces creation page...\nPlease complete the space setup in the new tab.': existingSpace? `β Updated successfully!\n\nOpening: ${response.space_url}`: `β Published successfully!\n\nOpening: ${response.space_url}`;alert(message);
frontend/src/app/page.tsx+64-33