Arslan1997 commited on
Commit
8e6bdfb
·
1 Parent(s): d191365

added latest models

Browse files
app/routes/forms.py CHANGED
@@ -14,21 +14,47 @@ from datetime import datetime
14
 
15
  from ..core.db import get_db
16
  from ..core.security import get_current_user
17
- from ..models import User, Form, FormQuestion, ConditionalRule, PublicForm, QuestionType, ConditionType, FormResponse as FormResponseModel, ResponseAnswer, ChatMessage
18
  from ..schemas.form import (
19
  FormCreate, FormUpdate, FormResponse, FormGenerationResponse,
20
  QuestionCreate, QuestionUpdate, QuestionResponse,
21
  ConditionalRuleCreate, ConditionalRuleResponse,
22
  PublicFormCreate, PublicFormResponse,
23
- ChatMessage, ChatResponse
24
  )
25
  from ..services.form_creator import generate_form_spec, edit_form_spec, validate_question_type, validate_condition_type
26
  from ..services.credit_service import credit_service
27
 
28
  import logging
 
29
 
30
  logger = logging.getLogger(__name__)
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  router = APIRouter(prefix="/api/forms", tags=["forms"])
33
 
34
 
@@ -241,7 +267,7 @@ async def delete_form(
241
 
242
  # Delete all related records in order to respect foreign key constraints
243
  # 1. Delete chat messages
244
- db.query(ChatMessage).filter(ChatMessage.form_id == form_id).delete(synchronize_session=False)
245
 
246
  # 2. Delete response answers (linked to responses)
247
  response_ids = [r.id for r in db.query(FormResponseModel.id).filter(FormResponseModel.form_id == form_id).all()]
@@ -593,6 +619,8 @@ async def publish_form(
593
 
594
  # Generate OG image for social sharing
595
  question_count = len(form.questions) if form.questions else 0
 
 
596
  og_image_url = og_image_service.generate_and_upload(
597
  form_id=form.id,
598
  token=existing.share_token if existing else "temp",
@@ -604,6 +632,8 @@ async def publish_form(
604
  question_count=question_count
605
  )
606
 
 
 
607
  if existing:
608
  # Update the OG image URL (in case form title/styling changed)
609
  existing.og_image_url = og_image_url
@@ -682,7 +712,7 @@ async def get_publish_info(
682
  @router.post("/{form_id}/chat/stream")
683
  async def chat_stream(
684
  form_id: int,
685
- chat_data: ChatMessage,
686
  current_user: User = Depends(get_current_user),
687
  db: Session = Depends(get_db)
688
  ):
@@ -694,7 +724,6 @@ async def chat_stream(
694
  from sqlalchemy import func
695
  import json
696
  import asyncio
697
- from ..models import FormResponse as FormResponseModel, ChatMessage as ChatMessageModel
698
  from ..services.response_chat_service import response_chat_service
699
  from ..services.agents import FormChatFunction
700
 
@@ -904,7 +933,12 @@ async def chat_stream(
904
  response = result.get('response')
905
  if hasattr(response, 'component_spec'):
906
  try:
907
- component_spec = json.loads(response.component_spec) if isinstance(response.component_spec, str) else response.component_spec
 
 
 
 
 
908
 
909
  # Create question in DB
910
  max_order = db.query(func.max(FormQuestion.question_order)).filter(
@@ -938,7 +972,12 @@ async def chat_stream(
938
  response = result.get('response')
939
  if hasattr(response, 'updated_form'):
940
  try:
941
- updated_form = json.loads(response.updated_form) if isinstance(response.updated_form, str) else response.updated_form
 
 
 
 
 
942
  changes_desc = getattr(response, 'changes_made', 'Form updated')
943
 
944
  if updated_form and "components" in updated_form:
@@ -1070,7 +1109,7 @@ async def chat_stream(
1070
  @router.post("/{form_id}/chat", response_model=ChatResponse)
1071
  async def chat_edit_form(
1072
  form_id: int,
1073
- chat_data: ChatMessage,
1074
  current_user: User = Depends(get_current_user),
1075
  db: Session = Depends(get_db)
1076
  ):
@@ -1231,8 +1270,14 @@ async def chat_edit_form(
1231
  component_spec = None
1232
  if hasattr(response, 'component_spec'):
1233
  try:
1234
- component_spec = json.loads(response.component_spec) if isinstance(response.component_spec, str) else response.component_spec
1235
- except:
 
 
 
 
 
 
1236
  component_spec = None
1237
 
1238
  if component_spec:
@@ -1270,8 +1315,14 @@ async def chat_edit_form(
1270
  updated_form = None
1271
  if hasattr(response, 'updated_form'):
1272
  try:
1273
- updated_form = json.loads(response.updated_form) if isinstance(response.updated_form, str) else response.updated_form
1274
- except:
 
 
 
 
 
 
1275
  updated_form = None
1276
 
1277
  if updated_form and "components" in updated_form:
 
14
 
15
  from ..core.db import get_db
16
  from ..core.security import get_current_user
17
+ from ..models import User, Form, FormQuestion, ConditionalRule, PublicForm, QuestionType, ConditionType, FormResponse as FormResponseModel, ResponseAnswer, ChatMessage as ChatMessageModel
18
  from ..schemas.form import (
19
  FormCreate, FormUpdate, FormResponse, FormGenerationResponse,
20
  QuestionCreate, QuestionUpdate, QuestionResponse,
21
  ConditionalRuleCreate, ConditionalRuleResponse,
22
  PublicFormCreate, PublicFormResponse,
23
+ ChatMessage as ChatMessageSchema, ChatResponse
24
  )
25
  from ..services.form_creator import generate_form_spec, edit_form_spec, validate_question_type, validate_condition_type
26
  from ..services.credit_service import credit_service
27
 
28
  import logging
29
+ import re
30
 
31
  logger = logging.getLogger(__name__)
32
 
33
+
34
+ def repair_json(text: str) -> str:
35
+ """
36
+ Repair common JSON issues from LLM output.
37
+ - Remove // comments
38
+ - Remove /* */ comments
39
+ - Fix trailing commas before } or ]
40
+ """
41
+ if not text:
42
+ return text
43
+
44
+ # Remove single-line comments (// ...)
45
+ text = re.sub(r'//[^\n]*', '', text)
46
+
47
+ # Remove multi-line comments (/* ... */)
48
+ text = re.sub(r'/\*.*?\*/', '', text, flags=re.DOTALL)
49
+
50
+ # Remove trailing commas before } or ]
51
+ text = re.sub(r',(\s*[}\]])', r'\1', text)
52
+
53
+ # Remove any BOM or weird unicode
54
+ text = text.strip().lstrip('\ufeff')
55
+
56
+ return text
57
+
58
  router = APIRouter(prefix="/api/forms", tags=["forms"])
59
 
60
 
 
267
 
268
  # Delete all related records in order to respect foreign key constraints
269
  # 1. Delete chat messages
270
+ db.query(ChatMessageModel).filter(ChatMessageModel.form_id == form_id).delete(synchronize_session=False)
271
 
272
  # 2. Delete response answers (linked to responses)
273
  response_ids = [r.id for r in db.query(FormResponseModel.id).filter(FormResponseModel.form_id == form_id).all()]
 
619
 
620
  # Generate OG image for social sharing
621
  question_count = len(form.questions) if form.questions else 0
622
+ logger.info(f"Generating OG image for form {form_id}, title: {form.title}, questions: {question_count}")
623
+
624
  og_image_url = og_image_service.generate_and_upload(
625
  form_id=form.id,
626
  token=existing.share_token if existing else "temp",
 
632
  question_count=question_count
633
  )
634
 
635
+ logger.info(f"OG image result for form {form_id}: {og_image_url}")
636
+
637
  if existing:
638
  # Update the OG image URL (in case form title/styling changed)
639
  existing.og_image_url = og_image_url
 
712
  @router.post("/{form_id}/chat/stream")
713
  async def chat_stream(
714
  form_id: int,
715
+ chat_data: ChatMessageSchema,
716
  current_user: User = Depends(get_current_user),
717
  db: Session = Depends(get_db)
718
  ):
 
724
  from sqlalchemy import func
725
  import json
726
  import asyncio
 
727
  from ..services.response_chat_service import response_chat_service
728
  from ..services.agents import FormChatFunction
729
 
 
933
  response = result.get('response')
934
  if hasattr(response, 'component_spec'):
935
  try:
936
+ raw_spec = response.component_spec
937
+ if isinstance(raw_spec, str):
938
+ raw_spec = repair_json(raw_spec)
939
+ component_spec = json.loads(raw_spec)
940
+ else:
941
+ component_spec = raw_spec
942
 
943
  # Create question in DB
944
  max_order = db.query(func.max(FormQuestion.question_order)).filter(
 
972
  response = result.get('response')
973
  if hasattr(response, 'updated_form'):
974
  try:
975
+ raw_form = response.updated_form
976
+ if isinstance(raw_form, str):
977
+ raw_form = repair_json(raw_form)
978
+ updated_form = json.loads(raw_form)
979
+ else:
980
+ updated_form = raw_form
981
  changes_desc = getattr(response, 'changes_made', 'Form updated')
982
 
983
  if updated_form and "components" in updated_form:
 
1109
  @router.post("/{form_id}/chat", response_model=ChatResponse)
1110
  async def chat_edit_form(
1111
  form_id: int,
1112
+ chat_data: ChatMessageSchema,
1113
  current_user: User = Depends(get_current_user),
1114
  db: Session = Depends(get_db)
1115
  ):
 
1270
  component_spec = None
1271
  if hasattr(response, 'component_spec'):
1272
  try:
1273
+ raw_spec = response.component_spec
1274
+ if isinstance(raw_spec, str):
1275
+ raw_spec = repair_json(raw_spec)
1276
+ component_spec = json.loads(raw_spec)
1277
+ else:
1278
+ component_spec = raw_spec
1279
+ except Exception as e:
1280
+ logger.error(f"Failed to parse component_spec: {e}")
1281
  component_spec = None
1282
 
1283
  if component_spec:
 
1315
  updated_form = None
1316
  if hasattr(response, 'updated_form'):
1317
  try:
1318
+ raw_form = response.updated_form
1319
+ if isinstance(raw_form, str):
1320
+ raw_form = repair_json(raw_form)
1321
+ updated_form = json.loads(raw_form)
1322
+ else:
1323
+ updated_form = raw_form
1324
+ except Exception as e:
1325
+ logger.error(f"Failed to parse updated_form: {e}")
1326
  updated_form = None
1327
 
1328
  if updated_form and "components" in updated_form:
app/routes/responses.py CHANGED
@@ -6,7 +6,7 @@ Routes for form submission and response management.
6
  """
7
 
8
  from fastapi import APIRouter, Depends, HTTPException, status, Request
9
- from fastapi.responses import HTMLResponse
10
  from sqlalchemy.orm import Session
11
  from sqlalchemy import desc, func
12
  from typing import List
 
6
  """
7
 
8
  from fastapi import APIRouter, Depends, HTTPException, status, Request
9
+ from fastapi.responses import HTMLResponse, Response
10
  from sqlalchemy.orm import Session
11
  from sqlalchemy import desc, func
12
  from typing import List
app/services/agents.py CHANGED
@@ -203,6 +203,8 @@ class ComponentSignatureGenerator(dspy.Signature):
203
  """
204
  Generate detailed component specifications that the frontend can render.
205
 
 
 
206
  FRONTEND CAPABILITIES:
207
  - Headless UI for modals and interactive components
208
  - Markdown support in question text and descriptions
@@ -216,35 +218,28 @@ class ComponentSignatureGenerator(dspy.Signature):
216
  - component_id: Unique identifier
217
  - form_context: Overall form context
218
 
219
- OUTPUT FORMAT (JSON):
220
  {
221
  "component_id": "comp_1",
222
- "question_type": "type from list",
223
- "question_text": "Clear question text (markdown supported)",
224
- "description": "Optional helper text (markdown supported)",
225
- "required": true/false,
226
  "settings": {
227
- // Type-specific settings
228
- "choices": ["Option 1", "Option 2"], // For multiple_choice, checkboxes, dropdown
229
- "min_value": 1, // For number, linear_scale
230
- "max_value": 10,
231
- "placeholder": "Enter text here", // For text inputs
232
- "scale_min_label": "Not at all", // For linear_scale
233
- "scale_max_label": "Extremely",
234
- "rows": ["Row 1", "Row 2"], // For matrix (MUST be array of strings)
235
- "columns": ["Col 1", "Col 2"], // For matrix (MUST be array of strings)
236
- "file_types": [".pdf", ".doc"], // For file_upload
237
- "max_file_size": 5242880, // bytes
238
- "ranking_items": ["Item 1", "Item 2"] // For ranking (MUST be array of strings)
239
- },
240
- "validation_rules": {
241
- "min_length": 5, // For text inputs
242
- "max_length": 100,
243
- "pattern": "regex_pattern", // For custom validation
244
- "error_message": "Custom error message"
245
  }
246
  }
247
 
 
 
 
 
 
 
 
 
 
248
  RULES:
249
  1. **USE USER'S EXACT TERMINOLOGY** - If the brief mentions specific terms (e.g., "Valima", "Nikkah", "Barat"), use those EXACT terms
250
  2. **DO NOT SUBSTITUTE** cultural, local, or specific terms with generic alternatives
 
203
  """
204
  Generate detailed component specifications that the frontend can render.
205
 
206
+ CRITICAL: Output ONLY valid JSON. NO comments (no // or /* */). NO trailing commas.
207
+
208
  FRONTEND CAPABILITIES:
209
  - Headless UI for modals and interactive components
210
  - Markdown support in question text and descriptions
 
218
  - component_id: Unique identifier
219
  - form_context: Overall form context
220
 
221
+ OUTPUT FORMAT (strict JSON, no comments):
222
  {
223
  "component_id": "comp_1",
224
+ "question_type": "file_upload",
225
+ "question_text": "Upload your resume/CV",
226
+ "description": "Accepted formats: PDF, DOC, DOCX (max 10MB)",
227
+ "required": true,
228
  "settings": {
229
+ "file_types": [".pdf", ".doc", ".docx"],
230
+ "max_file_size": 10485760
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  }
232
  }
233
 
234
+ SETTINGS BY TYPE (include ONLY relevant settings):
235
+ - multiple_choice/checkboxes/dropdown: "choices": ["Option A", "Option B"]
236
+ - number/linear_scale: "min_value": 1, "max_value": 10
237
+ - text inputs: "placeholder": "Enter text here"
238
+ - linear_scale: "scale_min_label": "Low", "scale_max_label": "High"
239
+ - matrix: "rows": ["Row 1"], "columns": ["Col 1"]
240
+ - file_upload: "file_types": [".pdf", ".doc"], "max_file_size": 5242880
241
+ - ranking: "ranking_items": ["Item 1", "Item 2"]
242
+
243
  RULES:
244
  1. **USE USER'S EXACT TERMINOLOGY** - If the brief mentions specific terms (e.g., "Valima", "Nikkah", "Barat"), use those EXACT terms
245
  2. **DO NOT SUBSTITUTE** cultural, local, or specific terms with generic alternatives
app/services/og_image_service.py CHANGED
@@ -259,6 +259,8 @@ class OGImageService:
259
  Returns the public URL of the uploaded image, or None if upload fails.
260
  """
261
  try:
 
 
262
  # Generate image
263
  image_bytes = self.generate_og_image(
264
  title=title,
@@ -268,9 +270,9 @@ class OGImageService:
268
  text_color=text_color,
269
  question_count=question_count
270
  )
 
271
 
272
  # Generate a deterministic filename based on form styling
273
- # This allows caching - same styling = same filename
274
  style_hash = hashlib.md5(
275
  f"{title}{description}{background_color}{accent_color}{text_color}{question_count}".encode()
276
  ).hexdigest()[:12]
@@ -280,21 +282,35 @@ class OGImageService:
280
  # Upload to S3
281
  s3 = self._get_s3_service()
282
  if not s3.bucket_name:
283
- logger.warning("S3 not configured, cannot upload OG image")
284
  return None
285
 
286
- url = s3.put_object(
287
- key=s3_key,
288
- body=image_bytes,
289
- content_type="image/png",
290
- public=True
291
- )
 
 
 
 
 
 
 
 
 
 
 
 
292
 
293
- logger.info(f"Generated and uploaded OG image for form {form_id}: {url}")
294
  return url
295
 
296
  except Exception as e:
297
- logger.error(f"Failed to generate/upload OG image for form {form_id}: {e}")
 
 
298
  return None
299
 
300
  def get_or_generate_url(
 
259
  Returns the public URL of the uploaded image, or None if upload fails.
260
  """
261
  try:
262
+ logger.warning(f"[OG] Starting generation for form {form_id}, title: {title}")
263
+
264
  # Generate image
265
  image_bytes = self.generate_og_image(
266
  title=title,
 
270
  text_color=text_color,
271
  question_count=question_count
272
  )
273
+ logger.warning(f"[OG] Image generated, size: {len(image_bytes)} bytes")
274
 
275
  # Generate a deterministic filename based on form styling
 
276
  style_hash = hashlib.md5(
277
  f"{title}{description}{background_color}{accent_color}{text_color}{question_count}".encode()
278
  ).hexdigest()[:12]
 
282
  # Upload to S3
283
  s3 = self._get_s3_service()
284
  if not s3.bucket_name:
285
+ logger.warning("[OG] S3 not configured (no bucket name)")
286
  return None
287
 
288
+ logger.warning(f"[OG] Uploading to bucket: {s3.bucket_name}, key: {s3_key}")
289
+
290
+ # Try upload - first with ACL, then without if that fails
291
+ try:
292
+ url = s3.put_object(
293
+ key=s3_key,
294
+ body=image_bytes,
295
+ content_type="image/png",
296
+ public=True
297
+ )
298
+ except Exception as acl_error:
299
+ logger.warning(f"[OG] ACL upload failed ({acl_error}), trying without ACL")
300
+ url = s3.put_object(
301
+ key=s3_key,
302
+ body=image_bytes,
303
+ content_type="image/png",
304
+ public=False
305
+ )
306
 
307
+ logger.warning(f"[OG] SUCCESS! Uploaded to: {url}")
308
  return url
309
 
310
  except Exception as e:
311
+ import traceback
312
+ logger.error(f"[OG] FAILED for form {form_id}: {e}")
313
+ logger.error(f"[OG] Traceback: {traceback.format_exc()}")
314
  return None
315
 
316
  def get_or_generate_url(