Spaces:
Running
Running
File size: 57,757 Bytes
0b10b26 ad2bd73 d2d8063 5977914 efc826f 5977914 ad2bd73 0b10b26 84d7e6e b643904 5977914 ad2bd73 5977914 ac1b7f1 4c67bdd 0b10b26 b643904 0b10b26 d2d8063 1ce5fa1 a97fbfc 4c67bdd ac1b7f1 4c67bdd d2d8063 a97fbfc d2d8063 4c67bdd 1ce5fa1 4c67bdd d2d8063 4c67bdd d2d8063 4c67bdd a97fbfc d2d8063 a97fbfc 4c67bdd 1ce5fa1 efc826f 5977914 f1ac8ee 383eb71 f1ac8ee 383eb71 f1ac8ee 383eb71 f1ac8ee 383eb71 f1ac8ee 383eb71 f1ac8ee 383eb71 f1ac8ee 383eb71 f1ac8ee 383eb71 f1ac8ee 383eb71 f1ac8ee 40dca72 d2d8063 1ce5fa1 0b10b26 b643904 ac1b7f1 cb3ff8c c50af6f cb3ff8c d2d8063 ac1b7f1 b643904 ac1b7f1 ad2bd73 b643904 d2d8063 ac1b7f1 1ce5fa1 ac1b7f1 0b10b26 d2d8063 b643904 d2d8063 b643904 ac1b7f1 d2d8063 ac1b7f1 0b10b26 ac1b7f1 d2d8063 b643904 ac1b7f1 0b10b26 40dca72 47405f2 efc826f 5977914 f1ac8ee 383eb71 f1ac8ee 383eb71 f1ac8ee 1ce5fa1 3922911 0b10b26 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 | from flask import Flask, request, jsonify
from flask_cors import CORS
import json
from datetime import datetime
from typing import Optional, Dict, Any, List
import re
import os
import io
from pypdf import PdfReader
from docx import Document
from firestore_client import get_firestore_client
from gemini_client import ask_gpt
from prompt_instructions import build_system_message
from role_access import get_allowed_collections
from data_fetcher import fetch_data_from_firestore
from data_planner import determine_data_requirements
from resolver import resolve_user_context
from schema_utils import has_field, resolve_field
app = Flask(__name__)
CORS(app)
db = get_firestore_client()
# -- helpers --------------------------------------------------------------
def to_jsonable(obj):
"""Recursively convert Firestore types (e.g., DatetimeWithNanoseconds) to JSON-safe values."""
# Firestore timestamp types come through as DatetimeWithNanoseconds (duck-type datetime)
if isinstance(obj, datetime):
return obj.isoformat()
# Some Firestore SDKs expose Timestamp-like objects without subclassing datetime.
# Fallback: detect presence of isoformat()
if hasattr(obj, "isoformat") and callable(getattr(obj, "isoformat")):
try:
return obj.isoformat()
except Exception:
pass
if isinstance(obj, dict):
return {k: to_jsonable(v) for k, v in obj.items()}
if isinstance(obj, list):
return [to_jsonable(v) for v in obj]
return obj
# 🔧 Normalize Gemini plan into proper Firestore fetch format
def normalize_plan(plan: dict, token_map: Optional[Dict[str, Any]] = None) -> dict:
token_map = token_map or {}
filters = plan.get("filters", {}) or {}
planned_cols = plan.get("collections", []) or []
def canonical_value(key, val):
# replace tokens like {{participantId}}
if isinstance(val, str) and val in token_map:
val = token_map[val]
# special-case status normalization
if key == "status" and val == "running":
return "active"
return val
collections_out = []
for col in planned_cols:
# Allow both strings ("participants") and objects ({"name":"participants","fields":[...]}).
name = col["name"] if isinstance(col, dict) else col
col_filters = []
for k, v in filters.items():
canon_key = resolve_field(name, k)
# only add filters that exist for this collection (schema-aware)
if has_field(name, canon_key):
col_filters.append({
"field": canon_key,
"op": "==",
"value": canonical_value(k, v),
})
# else: skip invalid field for this collection
collections_out.append({"name": name, "filters": col_filters, "limit": 50})
return {"collections": collections_out}
def _norm(v):
return str(v or "").strip().lower()
def _safe_number(v, default=0):
try:
n = float(v)
return n
except Exception:
return default
def _clamp_pct(n):
try:
return max(0, min(100, round(float(n))))
except Exception:
return 0
def _normalize_metric(metric: Optional[str]) -> Optional[str]:
m = _norm(metric)
if not m:
return None
if "hour" in m:
return "hours"
if "session" in m:
return "sessions"
if "document" in m:
return "documents"
if "deliverable" in m:
return "deliverables"
if "report" in m:
return "reports"
if "submission" in m:
return "submissions"
return None
def _proof_rules_for_metric(metric_key: Optional[str]) -> List[Dict[str, Any]]:
if metric_key == "sessions":
return [
{
"type": "attendance",
"label": "Attendance register / session proof",
"reason": "Sessions usually require attendance or meeting evidence.",
"required": True,
},
{
"type": "minutes",
"label": "Session notes / minutes",
"reason": "Session outcomes should be supported by notes or minutes.",
"required": False,
},
]
if metric_key in ["reports", "documents", "deliverables", "submissions"]:
return [
{
"type": "document",
"label": "Output file / submitted document",
"reason": "This metric usually requires the actual file or submission proof.",
"required": True,
}
]
if metric_key == "hours":
return [
{
"type": "supporting_evidence",
"label": "Supporting evidence",
"reason": "Hours-only updates should still have some supporting evidence where possible.",
"required": False,
}
]
return []
def _build_intervention_analysis_prompt(payload: Dict[str, Any]) -> str:
return f"""
You are analysing a consultant intervention progress update.
Return STRICT JSON only with this shape:
{{
"summary": "short summary",
"polishedNotes": "professional progress notes",
"activities": ["..."],
"outputs": ["..."],
"blockers": ["..."],
"nextSteps": ["..."],
"suggestedHours": 0,
"suggestedUnitsCompleted": 0,
"confidence": 0,
"completionSignal": "none|partial|strong",
"warnings": ["..."],
"proofHints": [
{{
"type": "attendance|minutes|document|photo|supporting_evidence|submission_receipt|other",
"label": "short label",
"reason": "why this is needed"
}}
]
}}
Rules:
- Use the user's notes/transcript/message as the primary source.
- Do not invent facts not grounded in the input.
- suggestedHours should be conservative.
- suggestedUnitsCompleted should only be given if the described work clearly maps to a measurable unit.
- completionSignal means:
- "none" = no sign of completion
- "partial" = meaningful progress but not clearly complete
- "strong" = strong signal that deliverable/work may be complete
- warnings should mention ambiguity, weak evidence, or missing detail.
- polishedNotes should be concise and professional.
Intervention context:
{json.dumps(payload, ensure_ascii=False)}
""".strip()
def _extract_json_block(text: str) -> Dict[str, Any]:
text = (text or "").strip()
try:
return json.loads(text)
except Exception:
pass
match = re.search(r"\{.*\}", text, flags=re.DOTALL)
if match:
try:
return json.loads(match.group(0))
except Exception:
pass
return {
"summary": "",
"polishedNotes": text,
"activities": [],
"outputs": [],
"blockers": [],
"nextSteps": [],
"suggestedHours": None,
"suggestedUnitsCompleted": None,
"confidence": 40,
"completionSignal": "none",
"warnings": ["AI response was not clean JSON. Parsed fallback response."],
"proofHints": [],
}
def _merge_proof_hints(metric_rules: List[Dict[str, Any]], ai_hints: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
merged = []
seen = set()
for item in metric_rules + (ai_hints or []):
t = _norm(item.get("type"))
label = str(item.get("label") or "").strip()
key = f"{t}|{label.lower()}"
if not t and not label:
continue
if key in seen:
continue
seen.add(key)
merged.append({
"type": t or "other",
"label": label or "Supporting evidence",
"reason": str(item.get("reason") or "").strip(),
"required": bool(item.get("required", False)),
})
return merged
def _calculate_progress_suggestion(intervention: Dict[str, Any], ai_result: Dict[str, Any]) -> Dict[str, Any]:
target_type = _norm(intervention.get("targetType"))
target_value = _safe_number(intervention.get("targetValue"), 0)
current_progress = _clamp_pct(_safe_number(intervention.get("progress"), 0))
current_actual = _safe_number(intervention.get("targetActual"), 0)
metric_key = _normalize_metric(intervention.get("targetMetric"))
suggested_hours = ai_result.get("suggestedHours")
try:
suggested_hours = float(suggested_hours) if suggested_hours is not None else None
except Exception:
suggested_hours = None
suggested_units = ai_result.get("suggestedUnitsCompleted")
try:
suggested_units = float(suggested_units) if suggested_units is not None else None
except Exception:
suggested_units = None
if target_type == "percentage":
# For percentage interventions, AI can only suggest a conservative bump.
base_bump = 0
signal = _norm(ai_result.get("completionSignal"))
if signal == "strong":
base_bump = 20
elif signal == "partial":
base_bump = 10
elif suggested_hours and suggested_hours > 0:
base_bump = 5
suggested_progress = _clamp_pct(current_progress + base_bump)
return {
"targetMode": "percentage",
"metricKey": metric_key,
"suggestedHours": suggested_hours,
"suggestedUnitsCompleted": None,
"currentActual": None,
"suggestedActualAfter": None,
"suggestedProgressPct": suggested_progress,
"overTargetBy": 0,
}
if target_type == "number" and target_value > 0:
units = suggested_units
if metric_key == "hours" and (units is None or units <= 0):
units = suggested_hours
if units is None or units <= 0:
return {
"targetMode": "number",
"metricKey": metric_key,
"suggestedHours": suggested_hours,
"suggestedUnitsCompleted": None,
"currentActual": current_actual,
"suggestedActualAfter": current_actual,
"suggestedProgressPct": current_progress,
"overTargetBy": 0,
}
actual_after = current_actual + units
progress_after = _clamp_pct((actual_after / target_value) * 100)
over_target_by = max(0, actual_after - target_value)
return {
"targetMode": "number",
"metricKey": metric_key,
"suggestedHours": suggested_hours,
"suggestedUnitsCompleted": units,
"currentActual": current_actual,
"suggestedActualAfter": actual_after,
"suggestedProgressPct": progress_after,
"overTargetBy": over_target_by,
}
return {
"targetMode": "none",
"metricKey": metric_key,
"suggestedHours": suggested_hours,
"suggestedUnitsCompleted": suggested_units,
"currentActual": None,
"suggestedActualAfter": None,
"suggestedProgressPct": current_progress,
"overTargetBy": 0,
}
ALLOWED_COURSE_SOURCE_EXTENSIONS = {"pdf", "docx"}
MAX_SOURCE_TEXT_CHARS = 60000
def _allowed_course_source(filename: str) -> bool:
if not filename or "." not in filename:
return False
return filename.rsplit(".", 1)[1].lower() in ALLOWED_COURSE_SOURCE_EXTENSIONS
def _clean_extracted_text(text: str) -> str:
if not text:
return ""
text = text.replace("\x00", " ")
text = re.sub(r"[ \t]+", " ", text)
text = re.sub(r"\n{3,}", "\n\n", text)
return text.strip()
def _extract_text_from_pdf_bytes(file_bytes: bytes) -> str:
reader = PdfReader(io.BytesIO(file_bytes))
pages = []
for page in reader.pages:
try:
pages.append(page.extract_text() or "")
except Exception:
pages.append("")
return _clean_extracted_text("\n\n".join(pages))
def _extract_text_from_docx_bytes(file_bytes: bytes) -> str:
doc = Document(io.BytesIO(file_bytes))
lines: List[str] = []
for p in doc.paragraphs:
txt = (p.text or "").strip()
if txt:
lines.append(txt)
for table in doc.tables:
for row in table.rows:
row_text = " | ".join((cell.text or "").strip() for cell in row.cells if (cell.text or "").strip())
if row_text:
lines.append(row_text)
return _clean_extracted_text("\n".join(lines))
def _extract_course_source_text(filename: str, file_bytes: bytes) -> str:
ext = filename.rsplit(".", 1)[1].lower()
if ext == "pdf":
return _extract_text_from_pdf_bytes(file_bytes)
if ext == "docx":
return _extract_text_from_docx_bytes(file_bytes)
raise ValueError("Unsupported file type")
def _truncate_source_text(text: str, limit: int = MAX_SOURCE_TEXT_CHARS) -> str:
if len(text) <= limit:
return text
return text[:limit]
def _build_course_outline_prompt(source_text: str, filename: str) -> str:
return f"""
You are designing a practical learning course outline from source material.
Return STRICT JSON only with this exact shape:
{{
"courseTitle": "string",
"courseDescription": "string",
"difficulty": "beginner|intermediate|advanced",
"category": "string",
"courseType": "string",
"estimatedTotalDuration": "string",
"learningObjectives": ["string"],
"modules": [
{{
"type": "lesson|quiz|assignment|review",
"title": "string",
"description": "string",
"duration": "e.g. 20m or 1h",
"content": "only for lesson when useful",
"assignmentPrompt": "only for assignment when useful",
"answerKey": "only for assignment when useful",
"questions": [
{{
"question": "string",
"options": ["string", "string", "string", "string"],
"correctAnswer": 0
}}
]
}}
],
"warnings": ["string"]
}}
Rules:
- Build a course outline grounded in the uploaded document.
- Prefer 4 to 12 modules unless the source strongly suggests otherwise.
- Most modules should be lessons.
- Include quizzes only where knowledge checks make sense.
- Include assignments only when there is something practical to apply.
- Include review modules only when useful for recap.
- Every lesson must have a realistic duration estimate.
- estimatedTotalDuration must reflect the sum of lesson durations approximately.
- Keep titles practical and clean.
- Do not invent niche facts that are not supported by the source.
- If the document is too thin, still produce a usable outline and add a warning.
- If the content looks like a scanned PDF with poor extraction, say so in warnings.
Filename: {filename}
Source document text:
{source_text}
""".strip()
def _normalize_outline_json(ai_result: Dict[str, Any]) -> Dict[str, Any]:
raw_modules = ai_result.get("modules") or []
out_modules = []
for idx, mod in enumerate(raw_modules):
mtype = str(mod.get("type") or "lesson").strip().lower()
if mtype not in ["lesson", "quiz", "assignment", "review"]:
mtype = "lesson"
base = {
"id": f"module-{idx + 1}",
"type": mtype,
"title": str(mod.get("title") or f"Module {idx + 1}").strip(),
"description": str(mod.get("description") or "").strip(),
}
if mtype == "lesson":
base["duration"] = str(mod.get("duration") or "20m").strip()
base["content"] = str(mod.get("content") or "").strip()
base["videoUrls"] = []
base["imageUrls"] = []
elif mtype == "quiz":
questions = []
for qidx, q in enumerate(mod.get("questions") or []):
options = q.get("options") or []
while len(options) < 4:
options.append("")
questions.append({
"id": f"q-{idx + 1}-{qidx + 1}",
"question": str(q.get("question") or "").strip(),
"options": [str(x or "").strip() for x in options[:4]],
"correctAnswer": int(q.get("correctAnswer") or 0),
})
base["questions"] = questions
elif mtype == "assignment":
base["assignmentPrompt"] = str(mod.get("assignmentPrompt") or "").strip()
base["answerKey"] = str(mod.get("answerKey") or "").strip()
out_modules.append(base)
return {
"courseTitle": str(ai_result.get("courseTitle") or "").strip(),
"courseDescription": str(ai_result.get("courseDescription") or "").strip(),
"difficulty": str(ai_result.get("difficulty") or "beginner").strip().lower(),
"category": str(ai_result.get("category") or "General").strip(),
"courseType": str(ai_result.get("courseType") or "Foundational").strip(),
"estimatedTotalDuration": str(ai_result.get("estimatedTotalDuration") or "").strip(),
"learningObjectives": [
str(x).strip() for x in (ai_result.get("learningObjectives") or []) if str(x).strip()
],
"modules": out_modules,
"warnings": [
str(x).strip() for x in (ai_result.get("warnings") or []) if str(x).strip()
],
}
# --- SME Intake helpers -------------------------------------------------
ALLOWED_SME_DOC_EXTENSIONS = {"pdf", "docx", "txt", "png", "jpg", "jpeg"}
MAX_SME_DOC_TEXT_CHARS = 45000
def _allowed_sme_doc(filename: str) -> bool:
if not filename or "." not in filename:
return False
return filename.rsplit(".", 1)[1].lower() in ALLOWED_SME_DOC_EXTENSIONS
def _extract_text_from_txt_bytes(file_bytes: bytes) -> str:
try:
return _clean_extracted_text(file_bytes.decode("utf-8", errors="ignore"))
except Exception:
return ""
def _extract_sme_doc_text(filename: str, file_bytes: bytes) -> str:
ext = filename.rsplit(".", 1)[1].lower()
if ext == "pdf":
return _extract_text_from_pdf_bytes(file_bytes)
if ext == "docx":
return _extract_text_from_docx_bytes(file_bytes)
if ext == "txt":
return _extract_text_from_txt_bytes(file_bytes)
# For images, we are not doing OCR here.
# Send metadata to AI, but text extraction stays blank.
if ext in ["png", "jpg", "jpeg"]:
return ""
return ""
def _fetch_active_programs(company_code: Optional[str] = None) -> List[Dict[str, Any]]:
try:
ref = db.collection("programs")
if company_code:
docs = ref.where("companyCode", "==", company_code).stream()
else:
docs = ref.stream()
programs = []
for doc_snap in docs:
data = doc_snap.to_dict() or {}
status = _norm(data.get("status") or data.get("programStatus"))
if status and status not in ["active", "open", "running"]:
continue
eligibility = data.get("eligibilityCriteria") or {}
programs.append({
"id": doc_snap.id,
"companyCode": data.get("companyCode") or "",
"name": data.get("name") or "",
"description": data.get("description") or "",
"type": data.get("type") or "",
"cohortYear": data.get("cohortYear") or "",
"startDate": to_jsonable(data.get("startDate")),
"endDate": to_jsonable(data.get("endDate")),
"maxCapacity": data.get("maxCapacity"),
"registrationLink": data.get("registrationLink") or "",
"eligibilityCriteria": eligibility,
"eligibilitySummary": {
"minimumAge": eligibility.get("minimumAge"),
"maximumAge": eligibility.get("maximumAge"),
"gender": eligibility.get("gender"),
"sector": eligibility.get("sector"),
"province": eligibility.get("province"),
"minYearsOfTrading": eligibility.get("minYearsOfTrading"),
"bbbeeLevel": eligibility.get("bbbeeLevel"),
"minYouthOwnershipPercent": eligibility.get("minYouthOwnershipPercent"),
"minBlackOwnershipPercent": eligibility.get("minBlackOwnershipPercent"),
"minFemaleOwnershipPercent": eligibility.get("minFemaleOwnershipPercent"),
},
})
return programs[:50]
except Exception as e:
print("fetch_active_programs_failed:", e)
return []
def _fetch_intervention_catalog(company_code: str) -> List[Dict[str, Any]]:
try:
docs = (
db.collection("interventions")
.where("companyCode", "==", company_code)
.stream()
)
interventions = []
for doc_snap in docs:
data = doc_snap.to_dict() or {}
status = _norm(data.get("status"))
if status and status not in ["active", "enabled", "approved"]:
continue
interventions.append({
"id": doc_snap.id,
"title": data.get("title") or data.get("interventionTitle") or data.get("name") or "",
"department": data.get("departmentName") or data.get("areaOfSupport") or data.get("department") or "",
"description": data.get("description") or data.get("objective") or "",
"tags": data.get("tags") or [],
"stage": data.get("stage") or data.get("businessStage") or "",
})
return interventions[:80]
except Exception as e:
print("fetch_intervention_catalog_failed:", e)
return []
def _build_sme_intake_prompt(payload: Dict[str, Any]) -> str:
return f"""
You are analysing an unregistered SME intake for an incubation/support platform.
Return STRICT JSON only with this exact shape:
{{
"detectedLanguages": ["English"],
"confidence": 0,
"extractedProfile": {{
"businessName": "",
"beneficiaryName": "",
"contactPerson": "",
"email": "",
"phone": "",
"registrationNumber": "",
"taxNumber": "",
"sector": "",
"province": "",
"city": "",
"yearsOperating": "",
"employeeCount": "",
"monthlyRevenue": "",
"mainChallenges": []
}},
"extractedApplication": {{
"motivation": "",
"challenges": "",
"businessDescription": "",
"supportNeeded": [],
"documentsMentioned": []
}},
"documentFindings": [
{{
"documentType": "id_document|cipc|tax_pin|other",
"filename": "",
"extractedFields": {{}},
"confidence": 0,
"warnings": []
}}
],
"missingFields": [
{{
"field": "",
"label": "",
"question": "",
"reason": ""
}}
],
"missingDocuments": [
{{
"type": "id_document|cipc|tax_pin|other",
"reason": ""
}}
],
"nextQuestion": "",
"decision": {{
"stage": "idea|startup|early_growth|struggling|scaling|market_ready",
"urgencyLevel": "low|medium|high|urgent",
"urgencyScore": 0,
"riskLevel": "low|medium|high",
"recommendedProgramId": "",
"recommendedProgramName": "",
"recommendedInterventions": [
{{
"title": "",
"department": "",
"urgency": "low|medium|high|urgent",
"reason": ""
}}
],
"summary": "",
"classificationReasons": [],
"redFlags": [],
"growthSignals": [],
"benefitsOfProgram": [],
"benefitsOfAgents": []
}},
"warnings": []
}}
Rules:
- Use the SME story and uploaded document text only.
- Do not invent registration numbers, tax numbers, IDs, turnover, employees, or compliance status.
- If a field is missing, add it to missingFields using a friendly conversational question.
- Required basic documents are: Director ID, CIPC document, SARS Tax PIN.
- If a required document is not uploaded or cannot be identified, add it to missingDocuments.
- Classify the SME stage:
- idea: concept only, no operating history
- startup: newly operating or still proving model
- early_growth: some traction, some customers/revenue
- struggling: stagnant revenue, compliance gaps, cashflow pressure, no growth, operational distress
- scaling: growing and needing structured support
- market_ready: compliant and ready for procurement/market linkage
- Urgency must reflect intervention need, not emotional tone.
- urgent/high urgency should be used where compliance, tax, funding, payroll, legal, safety, or survival risks are clear.
- Recommend interventions only from the intervention catalog where possible.
- Recommend one program from availablePrograms where possible.
- Match programmes using eligibilityCriteria first, especially province, sector, gender, age, years of trading, B-BBEE level, youth ownership, black ownership, and female ownership.
- If the SME does not clearly meet a programme's eligibility criteria, do not force that programme.
- If required eligibility data is missing from the SME story, add friendly questions to missingFields.
- If no programme fits, leave recommendedProgramId blank and explain why in summary.
- benefitsOfProgram must explain why structured incubation helps.
- benefitsOfAgents must explain why direct agent support helps without programme onboarding.
- Keep text professional and concise.
- Return JSON only.
Payload:
{json.dumps(payload, ensure_ascii=False)}
""".strip()
def _normalize_sme_intake_result(raw: Dict[str, Any]) -> Dict[str, Any]:
decision = raw.get("decision") or {}
allowed_stages = ["idea", "startup", "early_growth", "struggling", "scaling", "market_ready"]
allowed_urgency = ["low", "medium", "high", "urgent"]
allowed_risk = ["low", "medium", "high"]
stage = _norm(decision.get("stage"))
urgency = _norm(decision.get("urgencyLevel"))
risk = _norm(decision.get("riskLevel"))
if stage not in allowed_stages:
stage = "startup"
if urgency not in allowed_urgency:
urgency = "medium"
if risk not in allowed_risk:
risk = "medium"
interventions = []
for item in decision.get("recommendedInterventions") or []:
item_urgency = _norm(item.get("urgency"))
if item_urgency not in allowed_urgency:
item_urgency = urgency
title = str(item.get("title") or "").strip()
if not title:
continue
interventions.append({
"title": title,
"department": str(item.get("department") or "").strip(),
"urgency": item_urgency,
"reason": str(item.get("reason") or "").strip(),
})
missing_fields = []
for item in raw.get("missingFields") or []:
field = str(item.get("field") or "").strip()
question = str(item.get("question") or "").strip()
if not field or not question:
continue
missing_fields.append({
"field": field,
"label": str(item.get("label") or field).strip(),
"question": question,
"reason": str(item.get("reason") or "").strip(),
})
missing_documents = []
for item in raw.get("missingDocuments") or []:
doc_type = str(item.get("type") or "").strip()
if not doc_type:
continue
missing_documents.append({
"type": doc_type,
"reason": str(item.get("reason") or "").strip(),
})
document_findings = []
for item in raw.get("documentFindings") or []:
document_findings.append({
"documentType": str(item.get("documentType") or "other").strip(),
"filename": str(item.get("filename") or "").strip(),
"extractedFields": item.get("extractedFields") or {},
"confidence": _clamp_pct(item.get("confidence")),
"warnings": [
str(x).strip()
for x in item.get("warnings", [])
if str(x).strip()
],
})
return {
"detectedLanguages": [
str(x).strip()
for x in raw.get("detectedLanguages", [])
if str(x).strip()
],
"confidence": _clamp_pct(raw.get("confidence")),
"extractedProfile": raw.get("extractedProfile") or {},
"extractedApplication": raw.get("extractedApplication") or {},
"documentFindings": document_findings,
"missingFields": missing_fields[:12],
"missingDocuments": missing_documents[:10],
"nextQuestion": str(raw.get("nextQuestion") or "").strip(),
"decision": {
"stage": stage,
"urgencyLevel": urgency,
"urgencyScore": _clamp_pct(decision.get("urgencyScore")),
"riskLevel": risk,
"recommendedProgramId": str(decision.get("recommendedProgramId") or "").strip(),
"recommendedProgramName": str(decision.get("recommendedProgramName") or "").strip(),
"recommendedInterventions": interventions[:10],
"summary": str(decision.get("summary") or "").strip(),
"classificationReasons": [
str(x).strip()
for x in decision.get("classificationReasons", [])
if str(x).strip()
][:10],
"redFlags": [
str(x).strip()
for x in decision.get("redFlags", [])
if str(x).strip()
][:10],
"growthSignals": [
str(x).strip()
for x in decision.get("growthSignals", [])
if str(x).strip()
][:10],
"benefitsOfProgram": [
str(x).strip()
for x in decision.get("benefitsOfProgram", [])
if str(x).strip()
][:6],
"benefitsOfAgents": [
str(x).strip()
for x in decision.get("benefitsOfAgents", [])
if str(x).strip()
][:6],
},
"warnings": [
str(x).strip()
for x in raw.get("warnings", [])
if str(x).strip()
],
}
def build_assignments_page_context(company_code, user_id, program_id, role, user_context):
"""
Server-side mirror of the assignments page.
Must return:
- current user's department
- eligible SMEs
- each SME's required interventions from diagnosticPlans
- confirmation status per department
- existing assignedInterventions
- available consultants for department
- recurring / once-off assignment metadata
"""
# Fetch:
# users / operationsStaff / consultants
# departments
# diagnosticPlans
# applications
# participants
# interventions
# assignedInterventions
return {
"page": "assignments",
"companyCode": company_code,
"programId": program_id or "all",
"department": {},
"rules": {
"smmeMustHaveAcceptedApplication": True,
"diagnosticPlanMustBeConfirmedByDepartment": True,
"diagnosticPlanMustBeConfirmedBySmme": True,
"groupedAssignmentRequiresSharedIntervention": True,
"recurringCannotSkipOpenPreviousCycle": True,
"onceOffCannotRepeatAfterCompletion": True,
},
"smes": [],
"interventions": [],
"assignments": [],
"consultants": []
}
# -- route ---------------------------------------------------------------
@app.route('/chat', methods=['POST'])
def chat():
data = request.json
role = data.get('role')
user_input = data.get('message')
company_code = data.get('companyCode')
user_id = data.get('userId')
if not role or not user_input or not company_code or not user_id:
return jsonify({"error": "Missing role, message, companyCode, or userId"}), 400
# 🔎 Resolve current user's email + participantId (participants has no companyCode; resolve via applications)
ctx = resolve_user_context(user_id, company_code)
token_map = {
"{{participantId}}": ctx.get("participantId"),
"{{userEmail}}": ctx.get("email"),
"{{userId}}": ctx.get("uid"),
}
# 🧠 Plan
planning_result = determine_data_requirements(
user_input, company_code, user_id,
participant_email=ctx.get("email"),
participant_id=ctx.get("participantId"),
)
if "error" in planning_result:
return jsonify({"reply": f"⚠️ Planning error: {planning_result['error']}"})
# 🛠️ Normalize & replace tokens (schema-aware)
normalized_plan = normalize_plan(planning_result, token_map)
# 📥 Fetch
firestore_data = fetch_data_from_firestore(normalized_plan)
# 🧩 Build messages — convert Firestore payload to JSON-safe types
system_msg = build_system_message(company_code)
safe_ctx = to_jsonable(ctx)
safe_data = to_jsonable(firestore_data)
data_msg = {
"role": "system",
"content": (
f"CurrentUserContext: {json.dumps(safe_ctx)}\n"
f"Here is the data from Firestore:\n{json.dumps(safe_data)}"
)
}
user_msg = {"role": "user", "content": user_input}
final_response = ask_gpt([system_msg, data_msg, user_msg])
return jsonify({"reply": final_response})
@app.route("/page-assist", methods=["POST"])
def page_assist():
data = request.json or {}
page = data.get("page")
message = data.get("message")
role = data.get("role")
company_code = data.get("companyCode")
user_id = data.get("userId")
program_id = data.get("programId") # optional, "all" allowed
if not page or not message or not role or not company_code or not user_id:
return jsonify({"error": "Missing page, message, role, companyCode, or userId"}), 400
if page != "assignments":
return jsonify({"error": "Unsupported page"}), 400
ctx = resolve_user_context(user_id, company_code)
assignment_context = build_assignments_page_context(
company_code=company_code,
user_id=user_id,
program_id=None if program_id in [None, "", "all"] else program_id,
role=role,
user_context=ctx,
)
system_msg = {
"role": "system",
"content": """
You are a page-aware assistant for the Interventions Assignments page.
Answer using ONLY the supplied page context.
Explain blockers clearly.
Do not invent SMEs, interventions, departments, or assignment rules.
If asked why an intervention cannot be assigned, identify the exact reason:
- not part of that SME's Diagnostic Plan
- Diagnostic Plan not confirmed by both department and SME
- not shared across selected SMEs
- previous recurring cycle still open
- once-off intervention already completed
- missing facilitator, due date, target mode, or sub-intervention
Return concise JSON:
{
"reply": "",
"reasonCode": "",
"affectedSmes": [],
"eligibleSmes": [],
"blockedSmes": [],
"suggestedNextAction": ""
}
"""
}
user_msg = {
"role": "user",
"content": json.dumps({
"question": message,
"pageContext": assignment_context,
}, ensure_ascii=False)
}
ai_raw = ask_gpt([system_msg, user_msg])
result = _extract_json_block(ai_raw)
return jsonify(to_jsonable(result))
@app.route("/assignments-agent", methods=["POST"])
def assignments_agent():
data = request.json or {}
company_code = data.get("companyCode")
user_id = data.get("userId")
role = data.get("role")
message = data.get("message") or data.get("transcript")
program_id = data.get("programId")
if not company_code or not user_id or not role or not message:
return jsonify({"error": "Missing companyCode, userId, role, or message"}), 400
ctx = resolve_user_context(user_id, company_code)
assignment_context = build_assignments_page_context(
company_code=company_code,
user_id=user_id,
program_id=None if program_id in [None, "", "all"] else program_id,
role=role,
user_context=ctx,
)
system_msg = {
"role": "system",
"content": """
You are an assignment action planner.
Extract the user's intent, but DO NOT save anything directly.
Return strict JSON:
{
"intent": "assign_intervention|explain|unknown",
"interventionName": "",
"smeNames": [],
"assignmentType": "singular|grouped|unknown",
"assignTo": "consultant|operations|unknown",
"consultantName": "",
"dueDate": "",
"targetMode": "hours|sessions|documents|milestones|percentage|unknown",
"missingFields": [],
"canExecute": false,
"validationErrors": [],
"confirmationMessage": ""
}
Required fields before execution:
- interventionName
- at least one SME
- assignTo
- dueDate
- targetMode
- consultantName if assignTo is consultant
Validate against pageContext.
"""
}
user_msg = {
"role": "user",
"content": json.dumps({
"message": message,
"pageContext": assignment_context,
}, ensure_ascii=False)
}
ai_raw = ask_gpt([system_msg, user_msg])
plan = _extract_json_block(ai_raw)
if not plan.get("canExecute"):
return jsonify({
"mode": "needs_more_info",
"plan": plan,
"reply": plan.get("confirmationMessage") or "I need more information before creating the assignment."
})
validation = validate_assignment_plan(plan, assignment_context)
if not validation["ok"]:
return jsonify({
"mode": "blocked",
"reply": validation["message"],
"validationErrors": validation["errors"],
"plan": plan
})
# Do not write immediately unless frontend sends confirmed=true
if not data.get("confirmed"):
return jsonify({
"mode": "confirmation_required",
"reply": validation["confirmationMessage"],
"plan": plan
})
saved = create_assignments_from_plan(
plan=plan,
company_code=company_code,
user_id=user_id,
user_context=ctx,
assignment_context=assignment_context,
)
return jsonify({
"mode": "executed",
"reply": f"Created {saved['count']} assignment(s) successfully.",
"createdIds": saved["ids"]
})
# --- NEW: lightweight Help/Assistant intent router -------------------------
@app.route('/assist', methods=['POST'])
def assist():
"""
Classifies a short help question and, if appropriate, returns
a suggested navigation target + tour key for the frontend to action.
Request JSON:
{
"role": "incubatee" | ...,
"message": "Where do I apply?",
"companyCode": "ACME",
"userId": "abc123",
"path": "/current/location" # optional, helps decide replace vs push if you want
}
Response JSON:
{
"reply": "bot text to show",
"navigate": {
"to": "/incubatee/sme",
"tour": "apply", # optional
"ts": 1712345678901, # force re-run effects
"delayMs": 900 # UI should wait before navigating (to show the bot msg)
},
"handoff": {
"open": false, # whether to auto-open your drawer after nav
"botText": "You're on Programs. Tap an 'Apply' button to begin.",
"followups": ["track_application","profile_setup","inquiries"]
}
}
"""
data = request.json or {}
role = data.get('role')
user_input = (data.get('message') or '').strip().lower()
company_code = data.get('companyCode')
user_id = data.get('userId')
if not role or not user_input or not company_code or not user_id:
return jsonify({"error": "Missing role, message, companyCode, or userId"}), 400
# Simple keyword intent router (deterministic; fast)
def nav_payload(to: str, tour: Optional[str], reply: str, handoff: Optional[Dict[str, Any]] = None):
return jsonify({
"reply": reply,
"navigate": {
"to": to,
"tour": tour,
"ts": int(datetime.now().timestamp() * 1000),
"delayMs": 900
},
"handoff": handoff or {}
})
tokens = set(user_input.replace('?', '').split())
contains = lambda *words: any(w in tokens or w in user_input for w in words)
# 1) Where do I apply?
if contains('apply', 'application', 'register', 'program', 'submit'):
return nav_payload(
to="/incubatee/sme",
tour="apply",
reply="Taking you to Programs and starting the tour…",
handoff={
"open": False,
"botText": "You're on Programs. Tap an **Apply** button to begin.",
"followups": ["track_application", "profile_setup", "inquiries"]
}
)
# 2) Track my application
if contains('track', 'status', 'progress'):
return nav_payload(
to="/incubatee/tracker",
tour="track",
reply="Opening your application tracker…",
handoff={
"open": False,
"botText": "This is your tracker. You can view statuses and details here.",
"followups": ["where_apply", "inquiries"]
}
)
# 3) Inquiries / helpdesk
if contains('inquiry', 'inquiries', 'question', 'helpdesk', 'support', 'ticket'):
return nav_payload(
to="/incubatee/sme/inquiries",
tour="inquiries",
reply="Taking you to Inquiries…",
handoff={
"open": True,
"botText": "Here you can log questions or check replies.",
"followups": ["where_apply", "track_application"]
}
)
# 4) Profile/setup questions
if contains('profile', 'setup', 'account', 'redirect'):
return jsonify({
"reply": (
"You need to complete your profile before applying. "
"If something’s missing (e.g., name/company), we’ll redirect you to finish it first."
),
"handoff": {
"open": True,
"botText": "Would you like to go to **My Profile** now?",
"followups": ["where_apply"]
}
})
# 5) Contact support
if contains('contact', 'email', 'help', 'assist'):
return jsonify({
"reply": "You can email support at **support@smartincubation.example** or log an inquiry from the Inquiries page.",
"handoff": {
"open": True,
"botText": "Try the **Inquiries** option if you want to log a ticket.",
"followups": ["inquiries"]
}
})
# 6) Fallback → optionally leverage GPT to draft a helpful answer (no redirect)
# Keep this light; we're not doing Firestore fetches here.
try:
sys = {
"role": "system",
"content": (
"You are Q-Bot, a concise, friendly assistant. "
"Clarify the request in one or two sentences and suggest a next step. "
"Do NOT mention databases or internal logic."
)
}
user = {"role": "user", "content": data.get('message', '')}
answer = ask_gpt([sys, user]) or "I’m here to help! Could you tell me a bit more about what you need?"
return jsonify({
"reply": answer,
"handoff": {
"open": True,
"followups": ["where_apply", "track_application", "inquiries"]
}
})
except Exception:
return jsonify({
"reply": "I’m here to help! Could you tell me a bit more about what you need?",
"handoff": {
"open": True,
"followups": ["where_apply", "track_application", "inquiries"]
}
})
@app.route('/analyze-intervention-update', methods=['POST'])
def analyze_intervention_update():
"""
AI-assisted intervention update analysis.
Request JSON:
{
"role": "consultant",
"companyCode": "ABC",
"userId": "uid123",
"captureMode": "message|notes|audio",
"rawInput": "what the user typed or transcript",
"intervention": {
"id": "...",
"interventionTitle": "...",
"subtitle": "...",
"beneficiaryName": "...",
"status": "assigned|in-progress|completed",
"targetType": "percentage|number",
"targetMetric": "sessions|hours|reports|...",
"targetValue": 10,
"targetActual": 2,
"progress": 20,
"dueDate": "...",
"notes": "..."
},
"history": [
{
"notes": "...",
"progressAfter": 20,
"targetActualAfter": 2,
"createdAt": "..."
}
]
}
"""
data = request.json or {}
role = data.get('role')
company_code = data.get('companyCode')
user_id = data.get('userId')
capture_mode = data.get('captureMode')
raw_input = (data.get('rawInput') or '').strip()
intervention = data.get('intervention') or {}
history = data.get('history') or []
if not role or not company_code or not user_id or not capture_mode or not raw_input:
return jsonify({
"error": "Missing role, companyCode, userId, captureMode, or rawInput"
}), 400
if capture_mode not in ['message', 'notes', 'audio']:
return jsonify({"error": "Invalid captureMode"}), 400
metric_key = _normalize_metric(intervention.get("targetMetric"))
metric_rules = _proof_rules_for_metric(metric_key)
prompt_payload = {
"captureMode": capture_mode,
"rawInput": raw_input,
"role": role,
"intervention": {
"id": intervention.get("id"),
"interventionTitle": intervention.get("interventionTitle"),
"subtitle": intervention.get("subtitle"),
"beneficiaryName": intervention.get("beneficiaryName"),
"status": intervention.get("status"),
"targetType": intervention.get("targetType"),
"targetMetric": intervention.get("targetMetric"),
"targetValue": intervention.get("targetValue"),
"targetActual": intervention.get("targetActual"),
"progress": intervention.get("progress"),
"dueDate": to_jsonable(intervention.get("dueDate")),
"notes": intervention.get("notes"),
},
"history": history[:10],
"rules": {
"metricKey": metric_key,
"proofRules": metric_rules,
"instruction": (
"Infer what work was likely done from the raw input, "
"but do not freely calculate final progress. "
"Only suggest hours and measurable units if grounded in the text."
)
}
}
try:
system_msg = {
"role": "system",
"content": (
"You are an assistant that analyses intervention update notes for a business workflow. "
"You must return strict JSON only."
)
}
user_msg = {
"role": "user",
"content": _build_intervention_analysis_prompt(prompt_payload)
}
ai_raw = ask_gpt([system_msg, user_msg])
ai_result = _extract_json_block(ai_raw)
progress_calc = _calculate_progress_suggestion(intervention, ai_result)
merged_proof = _merge_proof_hints(metric_rules, ai_result.get("proofHints", []))
completion_signal = _norm(ai_result.get("completionSignal"))
readiness = "not_ready"
if completion_signal == "strong":
readiness = "close"
elif progress_calc.get("suggestedProgressPct", 0) >= 100:
readiness = "ready"
response = {
"summary": ai_result.get("summary", ""),
"polishedNotes": ai_result.get("polishedNotes", ""),
"activities": ai_result.get("activities", []),
"outputs": ai_result.get("outputs", []),
"blockers": ai_result.get("blockers", []),
"nextSteps": ai_result.get("nextSteps", []),
"warnings": ai_result.get("warnings", []),
"confidence": ai_result.get("confidence", 0),
"completionSignal": completion_signal or "none",
"completionReadiness": readiness,
"proofSuggestions": merged_proof,
"calculation": progress_calc,
"rawInput": raw_input,
}
return jsonify(to_jsonable(response))
except Exception as e:
print("analyze_intervention_update_failed:", e)
return jsonify({
"error": "Failed to analyse intervention update"
}), 500
@app.route('/generate-course-outline', methods=['POST'])
def generate_course_outline():
"""
multipart/form-data:
- file: pdf or docx
- role: operations | consultant | admin | ...
- companyCode: ...
- userId: ...
Response:
{
"reply": "Course outline generated successfully",
"outline": {
"courseTitle": "...",
"courseDescription": "...",
"difficulty": "...",
"category": "...",
"courseType": "...",
"estimatedTotalDuration": "...",
"learningObjectives": [],
"modules": [],
"warnings": []
},
"meta": {
"filename": "...",
"contentType": "...",
"extractedChars": 12345,
"truncated": false
}
}
"""
try:
role = request.form.get('role')
company_code = request.form.get('companyCode')
user_id = request.form.get('userId')
uploaded = request.files.get('file')
if not role or not company_code or not user_id:
return jsonify({
"error": "Missing role, companyCode, or userId"
}), 400
if uploaded is None:
return jsonify({
"error": "Missing file"
}), 400
filename = uploaded.filename or ""
if not _allowed_course_source(filename):
return jsonify({
"error": "Only PDF and DOCX files are supported"
}), 400
file_bytes = uploaded.read()
if not file_bytes:
return jsonify({
"error": "Uploaded file is empty"
}), 400
extracted_text = _extract_course_source_text(filename, file_bytes)
if not extracted_text:
return jsonify({
"error": "Could not extract readable text from the uploaded file"
}), 400
truncated_text = _truncate_source_text(extracted_text)
was_truncated = len(truncated_text) < len(extracted_text)
system_msg = {
"role": "system",
"content": (
"You generate practical LMS course outlines from uploaded documents. "
"Return strict JSON only."
)
}
user_msg = {
"role": "user",
"content": _build_course_outline_prompt(truncated_text, filename)
}
ai_raw = ask_gpt([system_msg, user_msg])
ai_result = _extract_json_block(ai_raw)
outline = _normalize_outline_json(ai_result)
if was_truncated:
outline["warnings"] = outline.get("warnings", [])
outline["warnings"].append(
"The source document was long, so only the first portion was used to generate this outline."
)
return jsonify({
"reply": "Course outline generated successfully",
"outline": outline,
"meta": {
"filename": filename,
"contentType": uploaded.content_type,
"extractedChars": len(extracted_text),
"truncated": was_truncated,
}
})
except Exception as e:
print("generate_course_outline_failed:", e)
return jsonify({
"error": "Failed to generate course outline from file"
}), 500
@app.route('/analyze-sme-application-intake', methods=['POST'])
def analyze_sme_application_intake():
"""
Multipart form-data endpoint for unregistered SME intake.
Expected form fields:
- mode: initial_review | final_decision
- companyCode
- userId optional
- contactName
- contactEmail
- contactPhone
- rawStory
- missingAnswersJson optional
- requiredDocumentsJson optional
- files[] optional
Response:
{
detectedLanguages,
confidence,
extractedProfile,
extractedApplication,
documentFindings,
missingFields,
missingDocuments,
nextQuestion,
decision,
warnings
}
"""
try:
mode = request.form.get("mode") or "initial_review"
company_code = (request.form.get("companyCode") or "").strip()
user_id = request.form.get("userId") or ""
contact_name = request.form.get("contactName") or ""
contact_email = request.form.get("email") or request.form.get("contactEmail") or ""
contact_phone = request.form.get("contactPhone") or ""
raw_story = (request.form.get("rawStory") or "").strip()
# companyCode is optional for public/unregistered SME intake
if not raw_story:
return jsonify({"error": "Missing rawStory"}), 400
try:
missing_answers = json.loads(request.form.get("missingAnswersJson") or "{}")
except Exception:
missing_answers = {}
try:
required_documents = json.loads(request.form.get("requiredDocumentsJson") or "[]")
except Exception:
required_documents = []
uploaded_files = request.files.getlist("files")
document_payloads = []
for uploaded in uploaded_files:
filename = uploaded.filename or ""
if not filename:
continue
if not _allowed_sme_doc(filename):
document_payloads.append({
"filename": filename,
"contentType": uploaded.content_type,
"extractedText": "",
"warnings": ["Unsupported document type."]
})
continue
file_bytes = uploaded.read()
if not file_bytes:
document_payloads.append({
"filename": filename,
"contentType": uploaded.content_type,
"extractedText": "",
"warnings": ["Uploaded file was empty."]
})
continue
extracted_text = _extract_sme_doc_text(filename, file_bytes)
truncated_text = _truncate_source_text(extracted_text, MAX_SME_DOC_TEXT_CHARS)
warnings = []
ext = filename.rsplit(".", 1)[1].lower()
if ext in ["png", "jpg", "jpeg"]:
warnings.append("Image OCR is not enabled on this endpoint yet.")
if extracted_text and len(truncated_text) < len(extracted_text):
warnings.append("Document text was truncated before AI analysis.")
if not extracted_text and ext not in ["png", "jpg", "jpeg"]:
warnings.append("No readable text could be extracted from this document.")
document_payloads.append({
"filename": filename,
"contentType": uploaded.content_type,
"extractedText": truncated_text,
"warnings": warnings
})
available_programs = _fetch_active_programs(company_code)
intervention_catalog = _fetch_intervention_catalog(company_code)
payload = {
"mode": mode,
"companyCode": company_code,
"userId": user_id,
"contact": {
"name": contact_name,
"email": contact_email,
"phone": contact_phone,
},
"rawStory": raw_story,
"missingAnswers": missing_answers,
"requiredDocuments": required_documents,
"uploadedDocuments": document_payloads,
"availablePrograms": available_programs,
"interventionCatalog": intervention_catalog,
"analysisDate": datetime.utcnow().isoformat(),
}
system_msg = {
"role": "system",
"content": (
"You analyse SME intake information for an incubation platform. "
"You return strict JSON only. "
"You must not invent official facts, registration numbers, tax numbers, "
"document statuses, revenue, employees, or compliance claims."
)
}
user_msg = {
"role": "user",
"content": _build_sme_intake_prompt(payload)
}
ai_raw = ask_gpt([system_msg, user_msg])
ai_result = _extract_json_block(ai_raw)
normalized = _normalize_sme_intake_result(ai_result)
return jsonify(to_jsonable(normalized))
except Exception as e:
print("analyze_sme_application_intake_failed:", e)
return jsonify({
"error": "Failed to analyse SME application intake"
}), 500
# Assessment extraction (uses this deployment's Firebase project and Gemini client)
from flask_assessment_extraction import create_assessment_blueprint
app.register_blueprint(create_assessment_blueprint(ask_gpt, db))
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)
|