"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSite = createSite;
const api_client_1 = require("../lib/api-client");
const device_flow_1 = require("../lib/device-flow");
/**
* Check for similar project names
*/
function findSimilarProjects(name, projects) {
const normalizedName = name.toLowerCase().trim();
return projects.filter(project => {
const projectName = project.name.toLowerCase();
// Exact match
if (projectName === normalizedName)
return true;
// One contains the other
if (projectName.includes(normalizedName) || normalizedName.includes(projectName))
return true;
// Similar words (check if main words overlap)
const nameWords = normalizedName.split(/\s+/).filter(w => w.length > 2);
const projectWords = projectName.split(/\s+/).filter(w => w.length > 2);
const overlap = nameWords.filter(w => projectWords.some(pw => pw.includes(w) || w.includes(pw)));
if (overlap.length > 0 && overlap.length >= nameWords.length * 0.5)
return true;
return false;
});
}
/**
* List existing projects for the authenticated user
*/
async function listExistingProjects() {
const response = await (0, api_client_1.apiRequest)('/api/tenants');
if ((0, api_client_1.isApiError)(response)) {
return [];
}
return response.data;
}
/**
* Create a new Fast Mode site/project
*
* @param name - The name of the project
* @param subdomain - Optional: Custom subdomain (auto-generated from name if not provided)
* @param confirmCreate - Optional: Skip similar-name check (set to true after user confirms)
*/
async function createSite(name, subdomain, confirmCreate) {
// Check authentication
if (await (0, api_client_1.needsAuthentication)()) {
const authResult = await (0, device_flow_1.ensureAuthenticated)();
if (!authResult.authenticated) {
return authResult.message;
}
}
// Validate name
if (!name || name.trim().length === 0) {
return `# Invalid Name
Please provide a name for your project.
Example:
\`\`\`
create_site(name: "My Awesome Website")
\`\`\`
`;
}
// Check for similar projects (unless confirmCreate is true)
if (!confirmCreate) {
const existingProjects = await listExistingProjects();
const similarProjects = findSimilarProjects(name, existingProjects);
if (similarProjects.length > 0) {
let output = `# Similar Project Found
Before creating "${name}", please note that you have similar existing project(s):
`;
similarProjects.forEach((project, index) => {
const url = project.customDomain || `${project.subdomain}.fastmode.ai`;
output += `${index + 1}. **${project.name}**
- ID: \`${project.id}\`
- URL: ${url}
`;
});
output += `---
## ACTION REQUIRED
**Ask the user:** "I found a similar project called '${similarProjects[0].name}'. Would you like to:
1. Deploy to the existing project '${similarProjects[0].name}'?
2. Create a new project called '${name}'?"
### If deploying to existing project:
\`\`\`
deploy_package(
packagePath: "./your-site.zip",
projectId: "${similarProjects[0].id}"
)
\`\`\`
### If creating new project anyway:
\`\`\`
create_site(name: "${name}", confirmCreate: true)
\`\`\`
`;
return output;
}
}
// Generate subdomain from name if not provided
const finalSubdomain = subdomain || name
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '')
.slice(0, 30);
// Create the site
const response = await (0, api_client_1.apiRequest)('/api/tenants', {
method: 'POST',
body: {
name: name.trim(),
subdomain: finalSubdomain,
},
});
if ((0, api_client_1.isApiError)(response)) {
// Check for common errors
if (response.error.includes('subdomain') && response.error.includes('taken')) {
return `# Subdomain Already Taken
The subdomain "${finalSubdomain}" is already in use.
Try creating with a different subdomain:
\`\`\`
create_site(name: "${name}", subdomain: "${finalSubdomain}-2")
\`\`\`
`;
}
if (response.statusCode === 401) {
// Try to re-authenticate
const authResult = await (0, device_flow_1.ensureAuthenticated)();
if (!authResult.authenticated) {
return authResult.message;
}
// Retry
const retryResponse = await (0, api_client_1.apiRequest)('/api/tenants', {
method: 'POST',
body: {
name: name.trim(),
subdomain: finalSubdomain,
},
});
if ((0, api_client_1.isApiError)(retryResponse)) {
return `# Failed to Create Site
${retryResponse.error}
Please try again or create manually at app.fastmode.ai
`;
}
return formatSuccess(retryResponse.data);
}
return `# Failed to Create Site
${response.error}
Please try again or create manually at app.fastmode.ai
`;
}
return formatSuccess(response.data);
}
/**
* Format success message
*/
function formatSuccess(site) {
return `# Site Created Successfully! 🎉
## Your New Site
- **Name:** ${site.name}
- **URL:** https://${site.subdomain}.fastmode.ai
- **Project ID:** \`${site.id}\`
## Next Steps
**Option 1: Deploy via MCP**
\`\`\`
deploy_package(
packagePath: "./your-site.zip",
projectId: "${site.id}"
)
\`\`\`
**Option 2: Connect GitHub**
1. Go to app.fastmode.ai
2. Open Settings → Connected Git Repository
3. Connect your GitHub account and select a repo
**Option 3: Manual Upload**
1. Go to app.fastmode.ai
2. Upload your website package in the Editor
---
Your site is ready at: https://${site.subdomain}.fastmode.ai
`;
}