frontend changes
This commit is contained in:
parent
2cc7cdbf00
commit
983624b12d
@ -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<AIAnalysisResponse> {
|
||||
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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user