"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listProjects = listProjects;
const api_client_1 = require("../lib/api-client");
const device_flow_1 = require("../lib/device-flow");
/**
* List all FastMode projects the authenticated user has access to
*/
async function listProjects() {
// Check if we need to authenticate
if (await (0, api_client_1.needsAuthentication)()) {
const authResult = await (0, device_flow_1.ensureAuthenticated)();
if (!authResult.authenticated) {
return authResult.message;
}
}
// Fetch user's tenants/projects
const response = await (0, api_client_1.apiRequest)('/api/tenants');
if ((0, api_client_1.isApiError)(response)) {
// If auth error, try to re-authenticate
if ((0, api_client_1.needsAuthError)(response)) {
const authResult = await (0, device_flow_1.ensureAuthenticated)();
if (!authResult.authenticated) {
return authResult.message;
}
// Retry the request
const retryResponse = await (0, api_client_1.apiRequest)('/api/tenants');
if ((0, api_client_1.isApiError)(retryResponse)) {
return `# API Error
Failed to fetch projects: ${retryResponse.error}
**Status:** ${retryResponse.statusCode}
Please try authenticating again.
`;
}
return formatProjectList(retryResponse.data);
}
return `# API Error
Failed to fetch projects: ${response.error}
**Status:** ${response.statusCode}
Please check:
1. Your authentication is valid
2. The API URL is correct
3. You have network connectivity
`;
}
return formatProjectList(response.data);
}
/**
* Format the project list for display
*/
function formatProjectList(projects) {
if (!projects || projects.length === 0) {
return `# No Projects Found
You don't have access to any FastMode projects.
**To create a project:**
1. Go to app.fastmode.ai
2. Click "Create New Project"
3. Run \`list_projects\` again to see it here
`;
}
// Format the project list
let output = `# Your FastMode Projects
Found ${projects.length} project${projects.length > 1 ? 's' : ''}:
`;
projects.forEach((project, index) => {
const siteStatus = project.site?.status || 'pending';
output += `## ${index + 1}. ${project.name}
- **Project ID:** \`${project.id}\`
- **Subdomain:** ${project.subdomain}.fastmode.ai
${project.customDomain ? `- **Custom Domain:** ${project.customDomain}\n` : ''}- **Your Role:** ${project.role}
- **Site Status:** ${siteStatus}
`;
});
output += `---
## How to Use
To get the schema for a project, use:
\`\`\`
get_tenant_schema(projectId: "${projects[0]?.id || 'project-id-here'}")
\`\`\`
Or use the project name:
\`\`\`
get_tenant_schema(projectId: "${projects[0]?.name || 'Project Name'}")
\`\`\`
`;
return output;
}