|
| 1 | +/** |
| 2 | + * AI API Layer |
| 3 | + * |
| 4 | + * Maps to the actual backend endpoints: |
| 5 | + * - POST /forms/ai-generate → AI form generation (creates form + fields server-side) |
| 6 | + * - POST /forms/:id/analytics → AI analytics report for a form |
| 7 | + */ |
| 8 | + |
| 9 | +const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000' |
| 10 | + |
| 11 | +// ---- Helpers ---- |
| 12 | + |
| 13 | +async function handleResponse<T>(response: Response): Promise<T> { |
| 14 | + if (!response.ok) { |
| 15 | + const errorData = (await response.json().catch(() => ({}))) as { |
| 16 | + message?: string |
| 17 | + } |
| 18 | + throw new Error( |
| 19 | + errorData.message || `Request failed: ${response.statusText}`, |
| 20 | + ) |
| 21 | + } |
| 22 | + const result = (await response.json()) as { |
| 23 | + success: boolean |
| 24 | + data: T |
| 25 | + message?: string |
| 26 | + } |
| 27 | + if (!result.success) { |
| 28 | + throw new Error(result.message || 'Request failed') |
| 29 | + } |
| 30 | + return result.data |
| 31 | +} |
| 32 | + |
| 33 | +// ---- Exported API ---- |
| 34 | + |
| 35 | +export interface AIGeneratedField { |
| 36 | + id: string |
| 37 | + fieldName: string |
| 38 | + label: string |
| 39 | + fieldType: string |
| 40 | + fieldValueType: string |
| 41 | + validation?: Record<string, unknown> |
| 42 | + options?: Array<string> |
| 43 | +} |
| 44 | + |
| 45 | +export interface AIGeneratedForm { |
| 46 | + id: string |
| 47 | + title: string |
| 48 | + description: string |
| 49 | + isPublished: boolean |
| 50 | + createdAt: string |
| 51 | + fields: Array<AIGeneratedField> |
| 52 | +} |
| 53 | + |
| 54 | +export interface AnalyticsInsight { |
| 55 | + question: string |
| 56 | + metric: string |
| 57 | + value: string | number |
| 58 | +} |
| 59 | + |
| 60 | +export interface AnalyticsTheme { |
| 61 | + theme: string |
| 62 | + description: string |
| 63 | + frequency: string |
| 64 | +} |
| 65 | + |
| 66 | +export interface AnalyticsReport { |
| 67 | + totalResponsesAnalyzed: number |
| 68 | + executiveSummary: string |
| 69 | + quantitativeInsights: Array<AnalyticsInsight> |
| 70 | + qualitativeThemes: Array<AnalyticsTheme> |
| 71 | +} |
| 72 | + |
| 73 | +export const aiApi = { |
| 74 | + /** |
| 75 | + * Generates a complete form (title + fields) from a text prompt. |
| 76 | + * The backend creates the form and all fields in a single transaction. |
| 77 | + * POST /forms/ai-generate |
| 78 | + * Body: { prompt: string } |
| 79 | + * Returns: { id, title, description, fields, ... } |
| 80 | + */ |
| 81 | + generateForm: async (prompt: string): Promise<AIGeneratedForm> => { |
| 82 | + const response = await fetch(`${API_URL}/forms/ai-generate`, { |
| 83 | + method: 'POST', |
| 84 | + headers: { 'Content-Type': 'application/json' }, |
| 85 | + body: JSON.stringify({ prompt }), |
| 86 | + credentials: 'include', |
| 87 | + }) |
| 88 | + return handleResponse<AIGeneratedForm>(response) |
| 89 | + }, |
| 90 | + |
| 91 | + /** |
| 92 | + * Generates an AI analytics report for a specific form's responses. |
| 93 | + * POST /forms/:formId/analytics |
| 94 | + * Body: {} (formId is in the URL) |
| 95 | + * Query: ?format=json (default) |
| 96 | + * Returns: AnalyticsReport |
| 97 | + */ |
| 98 | + generateSummary: async (formId: string): Promise<AnalyticsReport> => { |
| 99 | + const response = await fetch(`${API_URL}/forms/${formId}/analytics`, { |
| 100 | + method: 'POST', |
| 101 | + headers: { 'Content-Type': 'application/json' }, |
| 102 | + body: JSON.stringify({}), |
| 103 | + credentials: 'include', |
| 104 | + }) |
| 105 | + return handleResponse<AnalyticsReport>(response) |
| 106 | + }, |
| 107 | + |
| 108 | + /** |
| 109 | + * Placeholder — no backend endpoint exists yet. |
| 110 | + * Returns an empty array so AISuggestionPanel renders gracefully. |
| 111 | + */ |
| 112 | + suggestFields: ( |
| 113 | + _fields: Array<string>, |
| 114 | + ): Promise<{ suggestions: Array<{ label: string; type: string }> }> => { |
| 115 | + return Promise.resolve({ suggestions: [] }) |
| 116 | + }, |
| 117 | +} |
0 commit comments