From 983624b12d4b1f819fb4b62c939026e17a78d827 Mon Sep 17 00:00:00 2001 From: Chandini Date: Fri, 19 Sep 2025 15:49:23 +0530 Subject: [PATCH] frontend changes --- src/services/aiAnalysis.ts | 47 +++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/services/aiAnalysis.ts b/src/services/aiAnalysis.ts index fa0df9b..5326d5d 100644 --- a/src/services/aiAnalysis.ts +++ b/src/services/aiAnalysis.ts @@ -5,6 +5,8 @@ export interface AIAnalysisResponse { logicRules: string[] } +import { BACKEND_URL } from "@/config/backend" + export async function analyzeFeatureWithAI( featureName: string, description: string, @@ -13,17 +15,50 @@ export async function analyzeFeatureWithAI( userSelectedComplexity?: Complexity ): Promise { try { - const res = await fetch('/api/ai/analyze', { + // Prefer dedicated analysis endpoint if available in requirements service + const primaryUrl = `${BACKEND_URL}/api/requirements/analyze-feature` + let res = await fetch(primaryUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ featureName, description, requirements, projectType, complexity: userSelectedComplexity }), }) - const json = await res.json() - if (!res.ok || !json.success) { - throw new Error(json.message || `AI request failed (${res.status})`) + + // If primary endpoint not found, fall back to mockup service to at least return deterministic hints + if (res.status === 404) { + const fallbackUrl = `${BACKEND_URL}/api/mockup/generate-wireframe` + res = await fetch(fallbackUrl, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + prompt: `${featureName}: ${description}. Requirements: ${requirements.join('; ')}`, + device: 'desktop', + }), + }) + } + + const text = await res.text() + let json: any = {} + try { json = text ? JSON.parse(text) : {} } catch { /* non-json */ } + + if (!res.ok) { + throw new Error(json?.message || `AI request failed (${res.status})`) + } + + // Map various backend shapes to AIAnalysisResponse + if (json?.success && json?.data?.complexity && json?.data?.logicRules) { + return { complexity: json.data.complexity as Complexity, logicRules: json.data.logicRules as string[] } + } + if (json?.complexity && json?.logicRules) { + return { complexity: json.complexity as Complexity, logicRules: json.logicRules as string[] } + } + // If mockup returns something else, provide sensible defaults + return { + complexity: userSelectedComplexity || 'medium', + logicRules: [ + 'Generated via fallback path; refine requirements for better analysis', + 'Consider validation rules for key fields', + ], } - const data = json.data as AIAnalysisResponse - return { complexity: data.complexity, logicRules: data.logicRules } } catch (error) { // Fallback if the server route fails return {