Spaces:
Paused
Paused
File size: 24,342 Bytes
a51d841 5c91b05 a51d841 5c91b05 a51d841 5c91b05 a51d841 5c91b05 a51d841 5c91b05 a51d841 | 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 | # API Contract
**Version:** 1.0
**Base URL:** `/api/v1`
**Format:** JSON
**Authentication:** Session-based (session_id in headers or cookies)
---
## Table of Contents
1. [Session Management](#session-management)
2. [Lesson Initialization](#lesson-initialization)
3. [Vocabulary Endpoints](#vocabulary-endpoints)
4. [Grammar Endpoints](#grammar-endpoints)
5. [Exercise Endpoints](#exercise-endpoints)
6. [Test Endpoints](#test-endpoints)
7. [Error Responses](#error-responses)
8. [State Queries](#state-queries)
---
## Session Management
### Start New Session
**Endpoint:** `POST /session/start`
**Request:**
```json
{
"user_id": "user_123",
"lesson_number": 3
}
```
**Response:**
```json
{
"session_id": "sess_abc123",
"user_id": "user_123",
"lesson_number": 3,
"created_at": "2026-04-04T10:30:00Z",
"state": {
"vocab_mode": "not_started",
"grammar_mode": "not_started",
"vocab_batches_completed": [],
"grammar_topics_completed": []
}
}
```
### Get Session State
**Endpoint:** `GET /session/{session_id}`
**Response:**
```json
{
"session_id": "sess_abc123",
"user_id": "user_123",
"lesson_number": 3,
"state": {
"vocab_mode": "batch_quiz",
"current_vocab_batch": 2,
"vocab_batches_completed": [1],
"vocab_batch_scores": {"1": "2/3"},
"vocab_words_missed": ["ู
ูุฏูุฑูุณูุฉ"],
"grammar_mode": "not_started",
"grammar_topics_completed": [],
"current_grammar_topic": null
}
}
```
---
## Lesson Initialization
### Initialize Lesson
**Endpoint:** `POST /lesson/initialize`
**Description:** Loads all content into memory (Agent 3 caches vocabulary + grammar, Agent 2 loads with grading flexibility rules)
**Request:**
```json
{
"session_id": "sess_abc123",
"lesson_number": 3
}
```
**Response:**
```json
{
"success": true,
"lesson_number": 3,
"lesson_structure": {
"vocabulary": {
"total_words": 10,
"batch_structure": [3, 3, 3, 1],
"batches_count": 4
},
"grammar": {
"topics": ["feminine_nouns", "definite_article"],
"topics_count": 2,
"quiz_questions_per_topic": 5
}
},
"message": "Lesson 3 ready! Start with vocabulary or grammar?",
"next_actions": [
{"action": "start_vocab", "label": "Start Vocabulary (10 words)"},
{"action": "start_grammar", "label": "Start Grammar (2 topics)"},
{"action": "vocab_final_test", "label": "Skip to Vocabulary Test"}
]
}
```
**Orchestrator Mapping:**
```python
orchestrator.initialize_lesson(lesson_number=3)
```
---
## Vocabulary Endpoints
### Start Vocabulary Section
**Endpoint:** `POST /vocab/start`
**Request:**
```json
{
"session_id": "sess_abc123"
}
```
**Response:**
```json
{
"success": true,
"message": "Let's learn 10 new words in 4 batches! Ready to start?",
"total_words": 10,
"batches_count": 4,
"next_action": {
"action": "get_batch",
"batch_number": 1
}
}
```
---
### Get Vocabulary Batch
**Endpoint:** `GET /vocab/batch/{batch_number}`
**Request:** (batch_number in URL)
**Response:**
```json
{
"success": true,
"lesson_number": 3,
"batch_number": 1,
"total_batches": 4,
"words": [
{
"word_id": "w1",
"arabic": "ููุชูุงุจ",
"transliteration": "kitaab",
"english": "book"
},
{
"word_id": "w2",
"arabic": "ู
ูุฏูุฑูุณูุฉ",
"transliteration": "madrasa",
"english": "school"
},
{
"word_id": "w3",
"arabic": "ููููู
",
"transliteration": "qalam",
"english": "pen"
}
],
"message": "Let's learn Batch 1! Here are your first 3 words:\n\n๐ ููุชูุงุจ (kitaab) - book\n๐ ู
ูุฏูุฑูุณูุฉ (madrasa) - school\n๐ ููููู
(qalam) - pen\n\nTake your time reviewing these. When you're ready, let's test your knowledge!",
"next_action": {
"action": "start_batch_quiz",
"batch_number": 1
}
}
```
**Orchestrator Mapping:**
```python
orchestrator.get_vocab_batch(batch_number=1)
```
---
### Start Batch Quiz
**Endpoint:** `POST /vocab/batch/{batch_number}/quiz/start`
**Request:**
```json
{
"session_id": "sess_abc123"
}
```
**Response:**
```json
{
"success": true,
"batch_number": 1,
"total_questions": 3,
"question_number": 1,
"question": {
"question_id": "q1",
"word_id": "w1",
"type": "arabic_to_english",
"prompt": "What does ููุชูุงุจ mean?",
"arabic": "ููุชูุงุจ",
"transliteration": "kitaab"
}
}
```
---
### Submit Vocabulary Answer
**Endpoint:** `POST /vocab/batch/{batch_number}/quiz/answer`
**Request:**
```json
{
"session_id": "sess_abc123",
"question_id": "q1",
"word_id": "w1",
"student_answer": "book"
}
```
**Response (Correct):**
```json
{
"success": true,
"correct": true,
"feedback": "Correct! โ ููุชูุงุจ = book. Great job!",
"score": {
"current": "1/1",
"progress": "Question 1 of 3 complete"
},
"next_action": {
"action": "next_question",
"question_number": 2
}
}
```
**Response (Incorrect):**
```json
{
"success": true,
"correct": false,
"student_answer": "school",
"correct_answer": "book",
"feedback": "Not quite! The word ููุชูุงุจ (kitaab) means 'book', not 'school'. Try to remember: kitaab = book.",
"score": {
"current": "0/1",
"progress": "Question 1 of 3 complete"
},
"next_action": {
"action": "next_question",
"question_number": 2
}
}
```
**Orchestrator Mapping:**
```python
orchestrator.handle_vocab_quiz_answer(
batch_number=1,
question_id="q1",
word_id="w1",
student_answer="book"
)
```
---
### Get Batch Quiz Summary
**Endpoint:** `GET /vocab/batch/{batch_number}/quiz/summary`
**Response:**
```json
{
"success": true,
"batch_number": 1,
"score": "2/3",
"words_correct": ["ููุชูุงุจ", "ููููู
"],
"words_incorrect": [
{
"arabic": "ู
ูุฏูุฑูุณูุฉ",
"transliteration": "madrasa",
"correct_answer": "school",
"student_answer": "house"
}
],
"message": "Good effort! You got 2 out of 3 correct.\n\nYou missed:\nโข ู
ูุฏูุฑูุณูุฉ (madrasa) = school",
"next_action": {
"action": "get_batch",
"batch_number": 2,
"label": "Continue to Batch 2"
}
}
```
---
### Start Vocabulary Final Test
**Endpoint:** `POST /vocab/final-test/start`
**Request:**
```json
{
"session_id": "sess_abc123"
}
```
**Response:**
```json
{
"success": true,
"message": "Final vocabulary test! All 10 words, randomized order.",
"total_questions": 10,
"test_type": "vocabulary_final",
"next_action": {
"action": "get_question",
"question_number": 1
}
}
```
---
### Submit Final Test Answer
**Endpoint:** `POST /vocab/final-test/answer`
**Request:**
```json
{
"session_id": "sess_abc123",
"question_number": 3,
"word_id": "w5",
"student_answer": "house"
}
```
**Response:**
```json
{
"success": true,
"correct": true,
"feedback": "Correct! โ",
"score": {
"current": "3/3",
"total": 10,
"progress": "Question 3 of 10 complete"
},
"next_action": {
"action": "next_question",
"question_number": 4
}
}
```
---
### Get Final Test Results
**Endpoint:** `GET /vocab/final-test/results`
**Response:**
```json
{
"success": true,
"score": "8/10",
"percentage": 80,
"words_correct": 8,
"words_incorrect": [
{
"arabic": "ู
ูุฏูุฑูุณูุฉ",
"correct_answer": "school",
"student_answer": "house"
},
{
"arabic": "ุจูููุช",
"correct_answer": "house",
"student_answer": "door"
}
],
"message": "Great job! You scored 8 out of 10! ๐\n\nWords to review:\nโข ู
ูุฏูุฑูุณูุฉ (madrasa) = school\nโข ุจูููุช (bayt) = house",
"next_action": {
"action": "start_grammar",
"label": "Start Grammar Section"
}
}
```
---
## Grammar Endpoints
### Start Grammar Section
**Endpoint:** `POST /grammar/start`
**Request:**
```json
{
"session_id": "sess_abc123"
}
```
**Response:**
```json
{
"success": true,
"lesson_number": 3,
"topics": [
{
"topic_id": "feminine_nouns",
"topic_name": "Feminine Nouns",
"order": 1
},
{
"topic_id": "definite_article",
"topic_name": "Definite Article",
"order": 2
}
],
"message": "I'll teach you 2 grammar topics:\n1. Feminine Nouns\n2. Definite Article\n\nEach topic has a 5-question quiz. Ready to start?",
"next_action": {
"action": "get_topic",
"topic_id": "feminine_nouns"
}
}
```
---
### Get Grammar Topic Explanation
**Endpoint:** `GET /grammar/topic/{topic_id}`
**Response:**
```json
{
"success": true,
"lesson_number": 3,
"topic_id": "feminine_nouns",
"topic_name": "Feminine Nouns",
"explanation": "Let's learn about Feminine Nouns! ๐\n\n**The Rule:**\nIn Arabic, most feminine nouns end with ุฉ (taa marbuuta).\n\n**Examples:**\n- ู
ูุฏูุฑูุณูุฉ (madrasa) - school โ (ends in ุฉ)\n- ููุชูุงุจ (kitaab) - book (masculine, no ุฉ)\n\n**Quick Check:** Look at the word ุจูููุช (bint - girl). Does it end in ุฉ?",
"grammar_rule": "Feminine nouns usually end in ุฉ (taa marbuuta)",
"examples": [
{
"arabic": "ู
ูุฏูุฑูุณูุฉ",
"transliteration": "madrasa",
"english": "school",
"is_feminine": true,
"explanation": "ends in ุฉ"
},
{
"arabic": "ููุชูุงุจ",
"transliteration": "kitaab",
"english": "book",
"is_feminine": false,
"explanation": "no ุฉ - masculine"
}
],
"topics_remaining": 1,
"next_action": {
"action": "start_topic_quiz",
"topic_id": "feminine_nouns"
}
}
```
**Orchestrator Mapping:**
```python
orchestrator.get_grammar_topic_explanation(topic_id="feminine_nouns")
```
---
### Start Topic Quiz
**Endpoint:** `POST /grammar/topic/{topic_id}/quiz/start`
**Request:**
```json
{
"session_id": "sess_abc123"
}
```
**Response:**
```json
{
"success": true,
"topic_id": "feminine_nouns",
"topic_name": "Feminine Nouns",
"total_questions": 5,
"message": "Now let's test your understanding with 5 questions!",
"next_action": {
"action": "get_question",
"question_number": 1
}
}
```
---
### Get Topic Quiz Question
**Endpoint:** `GET /grammar/topic/{topic_id}/quiz/question/{question_number}`
**Response:**
```json
{
"success": true,
"topic_id": "feminine_nouns",
"question_number": 1,
"total_questions": 5,
"question": {
"question_id": "gq1",
"type": "identification",
"prompt": "Is 'ู
ูุฏูุฑูุณูุฉ' masculine or feminine?",
"arabic": "ู
ูุฏูุฑูุณูุฉ",
"transliteration": "madrasa",
"options": ["masculine", "feminine"]
}
}
```
---
### Submit Topic Quiz Answer
**Endpoint:** `POST /grammar/topic/{topic_id}/quiz/answer`
**Request:**
```json
{
"session_id": "sess_abc123",
"question_id": "gq1",
"question_number": 1,
"student_answer": "feminine"
}
```
**Response (Correct):**
```json
{
"success": true,
"correct": true,
"feedback": "Correct! โ ู
ูุฏูุฑูุณูุฉ is feminine because it ends in ุฉ (taa marbuuta). You're doing great! (1/1 so far)",
"explanation": "The ุฉ ending is the key indicator of feminine nouns.",
"score": {
"current_topic": "1/1",
"questions_remaining": 4
},
"next_action": {
"action": "next_question",
"question_number": 2
}
}
```
**Response (Incorrect):**
```json
{
"success": true,
"correct": false,
"student_answer": "masculine",
"correct_answer": "feminine",
"feedback": "Not quite. ู
ูุฏูุฑูุณูุฉ is feminine because it ends in ุฉ (taa marbuuta). The ุฉ is the key indicator! (0/1 so far)",
"errors": [
{
"error_type": "gender_identification",
"details": "ู
ูุฏูุฑูุณูุฉ ends in ุฉ, which indicates feminine",
"correction": "Look for the ุฉ (taa marbuuta) ending"
}
],
"score": {
"current_topic": "0/1",
"questions_remaining": 4
},
"next_action": {
"action": "next_question",
"question_number": 2
}
}
```
**Orchestrator Mapping:**
```python
orchestrator.handle_grammar_quiz_answer(
topic_id="feminine_nouns",
question_id="gq1",
question_number=1,
student_answer="feminine"
)
```
---
### Get Topic Quiz Results
**Endpoint:** `GET /grammar/topic/{topic_id}/quiz/results`
**Response (Score >= 4/5):**
```json
{
"success": true,
"topic_id": "feminine_nouns",
"topic_name": "Feminine Nouns",
"score": "4/5",
"percentage": 80,
"passed": true,
"message": "Great job! You got 4 out of 5 correct! โ\n\nYou've mastered feminine nouns. Ready for the next topic?",
"questions_correct": 4,
"questions_incorrect": 1,
"next_action": {
"action": "get_topic",
"topic_id": "definite_article",
"label": "Continue to Next Topic"
}
}
```
**Response (Score < 4/5 - Suggest Review):**
```json
{
"success": true,
"topic_id": "feminine_nouns",
"topic_name": "Feminine Nouns",
"score": "3/5",
"percentage": 60,
"passed": false,
"message": "You got 3 out of 5. You might want to review feminine nouns.\n\nWeak areas:\n- Identifying ุฉ endings\n- Exceptions to the rule\n\nReview and re-test, or move on?",
"questions_correct": 3,
"questions_incorrect": 2,
"weak_areas": [
"Identifying ุฉ endings",
"Exceptions to the rule"
],
"next_actions": [
{
"action": "review_topic",
"topic_id": "feminine_nouns",
"label": "Review and Re-test"
},
{
"action": "get_topic",
"topic_id": "definite_article",
"label": "Move to Next Topic"
}
]
}
```
**Orchestrator Mapping:**
```python
orchestrator.get_topic_quiz_results(topic_id="feminine_nouns")
```
---
## Exercise Endpoints
### Generate Exercises
**Endpoint:** `POST /exercises/generate`
**Request:**
```json
{
"session_id": "sess_abc123",
"lesson_number": 3,
"exercise_type": "fill_in_blank",
"grammar_point": "gender_agreement",
"count": 5
}
```
**Response:**
```json
{
"success": true,
"exercise_set_id": "exset_123",
"lesson_number": 3,
"exercise_type": "fill_in_blank",
"count": 5,
"exercises": [
{
"exercise_id": "ex1",
"question": "Complete: ููุชูุงุจ ___",
"options": ["ููุจููุฑ", "ููุจููุฑูุฉ"],
"hint": "ูุชุงุจ is masculine"
},
{
"exercise_id": "ex2",
"question": "Complete: ู
ูุฏูุฑูุณูุฉ ___",
"options": ["ุตูุบููุฑ", "ุตูุบููุฑูุฉ"],
"hint": "ู
ุฏุฑุณุฉ is feminine"
},
{
"exercise_id": "ex3",
"question": "Complete: ููููู
___",
"options": ["ุฌูุฏููุฏ", "ุฌูุฏููุฏูุฉ"],
"hint": "ููู
is masculine"
},
{
"exercise_id": "ex4",
"question": "Complete: ุณููููุงุฑูุฉ ___",
"options": ["ููุจููุฑ", "ููุจููุฑูุฉ"],
"hint": "ุณูุงุฑุฉ is feminine"
},
{
"exercise_id": "ex5",
"question": "Complete: ุจูููุช ___",
"options": ["ุฌูู
ููู", "ุฌูู
ููููุฉ"],
"hint": "ุจูุช is masculine"
}
],
"message": "Here are 5 fill-in-blank exercises to practice gender agreement!"
}
```
**Orchestrator Mapping:**
```python
orchestrator.generate_exercises(
lesson_number=3,
exercise_type="fill_in_blank",
grammar_point="gender_agreement",
count=5
)
```
---
### Submit Exercise Answers
**Endpoint:** `POST /exercises/{exercise_set_id}/submit`
**Request:**
```json
{
"session_id": "sess_abc123",
"answers": [
{"exercise_id": "ex1", "answer": "ููุจููุฑ"},
{"exercise_id": "ex2", "answer": "ุตูุบููุฑูุฉ"},
{"exercise_id": "ex3", "answer": "ุฌูุฏููุฏ"},
{"exercise_id": "ex4", "answer": "ููุจููุฑูุฉ"},
{"exercise_id": "ex5", "answer": "ุฌูู
ููููุฉ"}
]
}
```
**Response:**
```json
{
"success": true,
"exercise_set_id": "exset_123",
"total_score": "4/5",
"percentage": 80,
"results": [
{
"exercise_id": "ex1",
"correct": true,
"student_answer": "ููุจููุฑ"
},
{
"exercise_id": "ex2",
"correct": true,
"student_answer": "ุตูุบููุฑูุฉ"
},
{
"exercise_id": "ex3",
"correct": true,
"student_answer": "ุฌูุฏููุฏ"
},
{
"exercise_id": "ex4",
"correct": true,
"student_answer": "ููุจููุฑูุฉ"
},
{
"exercise_id": "ex5",
"correct": false,
"student_answer": "ุฌูู
ููููุฉ",
"correct_answer": "ุฌูู
ููู",
"explanation": "ุจูููุช is masculine, so use ุฌูู
ููู (masculine)"
}
],
"message": "Great work! You got 4 out of 5 correct! ๐\n\nReview:\nโข Exercise 5: ุจูููุช is masculine, use ุฌูู
ููู"
}
```
**Orchestrator Mapping:**
```python
orchestrator.grade_exercises(
exercise_set_id="exset_123",
answers=[...]
)
# Internally calls Agent 2 (GradingAgent) for each answer
# Returns {"correct": true/false} for each, with flexible grading:
# - Accepts synonyms (e.g., "instructor" โ "teacher")
# - Accepts minor typos (e.g., "scool" โ "school")
# - Accepts capitalization variations (e.g., "Book" โ "book")
# - For Arabic text: Internal harakaat optional, case endings required
```
---
## Test Endpoints
### Generate Lesson Test
**Endpoint:** `POST /test/generate`
**Request:**
```json
{
"session_id": "sess_abc123",
"lesson_number": 3,
"question_count": 10
}
```
**Response:**
```json
{
"success": true,
"test_id": "test_456",
"lesson_number": 3,
"question_count": 10,
"grammar_points": ["feminine_nouns", "definite_article"],
"message": "Lesson 3 Test - 10 questions covering all topics",
"questions": [
{
"question_id": "t1",
"type": "fill_in_blank",
"prompt": "Complete: ุงููุชุงุจ ___",
"options": ["ุงููุจูุฑ", "ูุจูุฑ"]
},
{
"question_id": "t2",
"type": "identification",
"prompt": "Is 'ุจูููุช' masculine or feminine?",
"options": ["masculine", "feminine"]
}
// ... 8 more questions
]
}
```
---
### Submit Test Answers
**Endpoint:** `POST /test/{test_id}/submit`
**Request:**
```json
{
"session_id": "sess_abc123",
"answers": [
{"question_id": "t1", "answer": "ุงููุจูุฑ"},
{"question_id": "t2", "answer": "feminine"}
// ... all 10 answers
]
}
```
**Response:**
```json
{
"success": true,
"test_id": "test_456",
"total_score": "8/10",
"percentage": 80,
"passed": true,
"results_by_topic": {
"feminine_nouns": {
"score": "5/5",
"status": "excellent"
},
"definite_article": {
"score": "3/5",
"status": "needs_review"
}
},
"message": "Well done! You scored 8 out of 10! ๐\n\nStrengths:\n- Excellent at feminine nouns (5/5 correct)\n\nAreas to review:\n- Definite article agreement (3/5 correct)\n\nSuggestion: Review when to use ุงู with adjectives.\n\nReady to move to Lesson 4?",
"next_actions": [
{
"action": "review_topic",
"topic_id": "definite_article",
"label": "Review Definite Article"
},
{
"action": "next_lesson",
"lesson_number": 4,
"label": "Start Lesson 4"
}
]
}
```
---
## Error Responses
### Standard Error Format
All errors follow this structure:
```json
{
"success": false,
"error": {
"code": "ERROR_CODE",
"message": "Human-readable error message",
"details": {}
}
}
```
### Error Codes
**Authentication/Session:**
- `INVALID_SESSION` (401) - Session ID not found or expired
- `SESSION_EXPIRED` (401) - Session timed out
- `UNAUTHORIZED` (401) - User not authorized for this resource
**Validation:**
- `INVALID_REQUEST` (400) - Malformed JSON or missing required fields
- `INVALID_LESSON` (400) - Lesson number doesn't exist
- `INVALID_BATCH` (400) - Batch number out of range
- `INVALID_TOPIC` (400) - Topic ID not found in lesson
**State:**
- `LESSON_NOT_INITIALIZED` (400) - Must call `/lesson/initialize` first
- `WRONG_MODE` (400) - Action not allowed in current mode (e.g., trying to quiz before teaching)
- `QUIZ_NOT_STARTED` (400) - Must start quiz before submitting answers
**RAG/Content:**
- `CONTENT_NOT_FOUND` (404) - Requested content not in RAG database
- `RAG_TIMEOUT` (504) - RAG query timed out
**Agent:**
- `AGENT_ERROR` (500) - Agent returned invalid response
- `GENERATION_TIMEOUT` (504) - Exercise/question generation timed out
- `GRADING_ERROR` (500) - Agent 2 (GradingAgent) failed to grade or returned invalid JSON
### Error Examples
**Invalid Session:**
```json
{
"success": false,
"error": {
"code": "INVALID_SESSION",
"message": "Session not found or expired",
"details": {
"session_id": "sess_invalid"
}
}
}
```
**Lesson Not Initialized:**
```json
{
"success": false,
"error": {
"code": "LESSON_NOT_INITIALIZED",
"message": "Lesson must be initialized before starting vocabulary",
"details": {
"required_action": "POST /lesson/initialize"
}
}
}
```
**Content Not Found:**
```json
{
"success": false,
"error": {
"code": "CONTENT_NOT_FOUND",
"message": "Lesson 99 not found in database",
"details": {
"lesson_number": 99,
"available_lessons": [1, 2, 3, 4, 5, 6, 7, 8]
}
}
}
```
---
## State Queries
### Get Current Progress
**Endpoint:** `GET /progress/{session_id}`
**Response:**
```json
{
"success": true,
"lesson_number": 3,
"overall_progress": {
"vocabulary": {
"status": "in_progress",
"batches_completed": 2,
"batches_total": 4,
"scores": {"1": "2/3", "2": "3/3"},
"words_mastered": 5,
"words_total": 10
},
"grammar": {
"status": "not_started",
"topics_completed": 0,
"topics_total": 2,
"scores": {}
}
}
}
```
---
## Implementation Notes
### Session Management
**Storage:** Redis or in-memory cache (for demo)
**Session Object:**
```python
{
"session_id": "sess_abc123",
"user_id": "user_123",
"lesson_number": 3,
"created_at": "2026-04-04T10:30:00Z",
"last_activity": "2026-04-04T11:00:00Z",
"expires_at": "2026-04-04T18:30:00Z", # 8 hours
# Full conversation state (from ARCHITECTURE.md)
"state": {
"vocab_mode": "batch_quiz",
"vocab_batches_completed": [1],
"current_vocab_batch": 2,
# ... full state schema
}
}
```
### Rate Limiting
- 100 requests per minute per session
- 10 concurrent sessions per user
### Response Times
Target latencies:
- Vocabulary/Grammar content: < 200ms (from cache)
- Single answer grading: < 500ms (Agent 2 GradingAgent with 7B model, max 50 tokens)
- Exercise generation: < 2s (Agent 3 LLM generation)
- Lesson initialization: < 3s (RAG query + caching)
### Agent 2 (Grading Agent) Details
**Model:** Fine-tuned Qwen2.5-7B (baseline evaluated 2026-04-13)
**Grading Modes:**
1. **grading_vocab** - Vocabulary translation grading
- Input: word (Arabic), student_answer, correct_answer
- Output: `{"correct": true/false}`
- Flexibility: Accepts synonyms, typos, capitalization variations
2. **grading_grammar** (single question) - Grammar quiz question grading
- Input: question, student_answer, correct_answer
- Output: `{"correct": true/false}`
- Flexibility: Accepts abbreviations (m/f for masculine/feminine), synonyms, typos
- Arabic text: Internal harakaat optional, case endings (final harakaat) required
3. **grading_grammar** (multiple questions) - Test grading
- Input: lesson_number, answers (list of question/answer pairs)
- Output: `{"total_score": "X/Y", "results": [{"question_id": "...", "correct": true/false}, ...]}`
**Edge Case Handling:**
- Synonyms: โ "instructor" = "teacher"
- Minor typos: โ "scool" = "school"
- Capitalization: โ "Book" = "book"
- Articles: โ "a pen" = "pen"
- Arabic harakaat: Internal marks optional (ุงููุชุงุจู = ุงูููุชูุงุจู), case endings required (ุงููุชุงุจ โ ุงูููุชูุงุจู)
**Baseline Results (7B model, 2026-04-13):**
- JSON compliance: 0-6% (adds explanations after JSON)
- Reasoning accuracy: 83% (5/6 correct decisions when ignoring format issues)
- Fine-tuning planned to address format compliance and harakaat flexibility
---
**Next Steps for Implementation:**
1. Build orchestrator with these exact return formats
2. Wrap orchestrator in FastAPI with these endpoints
3. Frontend consumes these contracts exactly
**Contract is versioned:** If we need changes, create `/api/v2`
|