hezarfen_backend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The RAG nest: browser ⇄ backend ⇄ AI service (over the QUIC bridge), |
| 2 | //! scoped to a school's course-note corpus. |
| 3 | //! |
| 4 | //! The relay contract is [`super::chatbot`]'s, shape for shape: the backend |
| 5 | //! owns everything except the answer — auth, the per-user rate limit, the |
| 6 | //! thread, the payload format, and the last look at the reply before it is |
| 7 | //! stored. There are no intent or rule tables, and every thread is private to |
| 8 | //! its owner. What differs is the **scope**: every question carries the |
| 9 | //! `(class, course)` pairs the corpus is routed by, derived server-side |
| 10 | //! ([`crate::service::rag_scope`]: a manager or an admin gets every class-course |
| 11 | //! pair in the school, everyone else their own memberships) and never from the |
| 12 | //! request body, and every citation the service returns is resolved here to |
| 13 | //! the course-note file that owns the cited document — the `doc_id` only the |
| 14 | //! backend can turn into something a reader can open. |
| 15 | //! |
| 16 | //! The school's existing AI caps govern both nests: a RAG thread is reserved |
| 17 | //! against the same `max_chatbot_threads` seat count, a prompt and its answer |
| 18 | //! are bounded by the same `max_chatbot_message_len`, and the history replayed |
| 19 | //! to the service is the same `chatbot_history_turns` window. Only the |
| 20 | //! per-minute tier is RAG's own (`DEFAULT_RAG_RATE_LIMIT`), because every |
| 21 | //! message here spends a retrieval over the whole corpus before it generates |
| 22 | //! anything. |
| 23 | //! |
| 24 | //! Sending is asynchronous by design. `POST .../messages` writes the user's |
| 25 | //! turn plus an empty `pending` assistant row and answers `202` immediately; |
| 26 | //! a retrieval-then-answer round trip can outlast the request-timeout layer |
| 27 | //! that guards every HTTP handler, so nothing is awaited on the request path. |
| 28 | //! The client then either polls `GET .../messages/{mid}` or opens its SSE |
| 29 | //! stream — both read the same row, so they can never disagree. |
| 30 | //! |
| 31 | //! The bridge round trip runs in a `tokio::spawn` the POST leaves behind. A |
| 32 | //! restart therefore orphans a turn in flight: its row stays `pending` until a |
| 33 | //! reader projects it failed, and the boot sweep stamps that verdict durably |
| 34 | //! ([`crate::constant::RAG_PENDING_STALE_SECS`]). |
| 35 | //! |
| 36 | //! The two **study** doors (`POST /rag/summarize`, `POST /rag/questions`) are |
| 37 | //! the exception to that shape, and deliberately: a summary or a question set |
| 38 | //! is one artifact about one range of one corpus, so there is no thread to |
| 39 | //! open, no row to poll and nothing stored — the request waits for the |
| 40 | //! artifact, under a dispatch deadline kept inside the middleware's own |
| 41 | //! envelope. |
| 42 | |
| 43 | use Duration; |
| 44 | |
| 45 | use crateResolvedTenant; |
| 46 | use crate; |
| 47 | use Json; |
| 48 | use ; |
| 49 | use StatusCode; |
| 50 | use ; |
| 51 | use ; |
| 52 | use ; |
| 53 | use json; |
| 54 | use Json as SqlJson; |
| 55 | use mpsc; |
| 56 | use Stream; |
| 57 | use ReceiverStream; |
| 58 | use ToSchema; |
| 59 | use OpenApiRouter; |
| 60 | use routes; |
| 61 | |
| 62 | use crateChatRole; |
| 63 | use crateRagScopePair; |
| 64 | use crate |
| 65 | |
| 66 | |
| 67 | ; |
| 68 | use crateAiBridge; |
| 69 | use crate |
| 70 | |
| 71 | |
| 72 | ; |
| 73 | use crateDatabase; |
| 74 | use crate |
| 75 | |
| 76 | ; |
| 77 | use crate; |
| 78 | use crateRole; |
| 79 | use crateSettings; |
| 80 | use crateUserId; |
| 81 | use crate; |
| 82 | use crateservice; |
| 83 | use crate; |
| 84 | |
| 85 | use ; |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | // ---- threads ---------------------------------------------------------- |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | /// One RAG thread. `updated_at` moves on every turn, and the list is sorted by |
| 111 | /// it, so the thread just written to is always first. |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | /// Start a new RAG thread, optionally named. Every authenticated role may ask |
| 136 | /// — parents included. A user may keep up to the school's |
| 137 | /// `max_chatbot_threads` threads *across both AI nests*; at the cap the |
| 138 | /// request is refused (409) until an old thread is deleted — the cap is |
| 139 | /// storage protection, not a usage quota (that is the per-minute message |
| 140 | /// limit). |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | async |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | /// The caller's own threads, most recently active first. Paged via |
| 178 | /// `?limit=&offset=`. Nobody — no teacher, no admin — reads anyone else's. |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | async |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | /// Rename a thread, or clear its name (`title: null`). Owner only; someone |
| 212 | /// else's thread is a `404`, never a `403`. The edit counts as activity, so the |
| 213 | /// thread moves to the top of the list. |
| 214 | /// |
| 215 | /// Nothing names a thread automatically: the first message is not turned into |
| 216 | /// a title, and the AI service is never asked for one. |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | async |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | /// Delete a thread and every turn in it, permanently. Owner only; someone |
| 248 | /// else's thread is a `404`, never a `403` (its existence is not leaked). |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | async |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | /// Read a thread the caller owns, or `404`. A foreign id is indistinguishable |
| 272 | /// from a missing one. |
| 273 | async |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | // ---- turns ------------------------------------------------------------------ |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | /// The receipt for an accepted turn. The answer is *not* here: it is being |
| 294 | /// retrieved and generated. Poll `GET /rag/threads/{id}/messages/{mid}` or |
| 295 | /// open its `/stream`. |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | /// One citation behind an answer, already resolved to something openable. |
| 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 | /// One turn. `content` is empty while `status` is `pending`; `error_code` is |
| 345 | /// set only when `status` is `failed`. |
| 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 | /// The whole thread, oldest first. Paged via `?limit=&offset=`. Owner only. |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | async |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | /// Ask the corpus a question: `{content}`, at most the school's |
| 433 | /// `max_chatbot_message_len`. Answers `202 {message_id, status: "pending"}` the |
| 434 | /// moment both rows are written — the answer itself lands later, in the |
| 435 | /// reserved assistant row. `503` when no AI service offers `rag.chat`, and |
| 436 | /// nothing is written; `429` + `Retry-After` over the per-user send limit. |
| 437 | /// |
| 438 | /// Order matters and is deliberate: ownership is settled first (a foreign |
| 439 | /// thread is a `404` and is never charged), then the per-user rate limit (a |
| 440 | /// refused turn leaves no trace), then availability (so an unavailable service |
| 441 | /// produces a `503` and no dead pending row), then the scope the question is |
| 442 | /// asked under — derived by [`crate::service::rag_scope`], never from the |
| 443 | /// body. Once the rows are written the turn always settles: the answering task |
| 444 | /// stamps `complete`/`failed`, a reader projects a long-stale `pending` as |
| 445 | /// failed, and the boot sweep repairs whatever a process death left behind. |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | async |
| 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 | /// The school's per-message character cap, never above the domain's hard |
| 566 | /// ceiling. Applied to what the user sends *and* to what the service answers — |
| 567 | /// the same knob the chatbot nest reads. |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | /// Fetch one turn's answer and settle its row. Runs detached from the request. |
| 573 | /// |
| 574 | /// Every path settles: a row left `pending` would show as a spinner forever. |
| 575 | // the whole turn, handed to the detached task as one |
| 576 | async |
| 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 | /// The tail of the thread replayed to the service: oldest first, only settled |
| 674 | /// turns with text, and never the two rows this turn just wrote (the new |
| 675 | /// prompt rides in `message`, the answer does not exist yet). |
| 676 | async |
| 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 | /// Poll one turn. The non-SSE fallback for `/stream`, reading the same row — |
| 707 | /// including the projection that presents a long-stale `pending` as `failed`, |
| 708 | /// so the two can never disagree about a turn's state. |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | async |
| 725 | |
| 726 | |
| 727 | : , |
| 728 | ) |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | /// Read one turn only if the caller owns it *and* it belongs to the named |
| 734 | /// thread — a mismatched pair is a `404`, like a foreign one. |
| 735 | /// |
| 736 | /// The thread is re-read rather than inferred from the message's `thread_id`, |
| 737 | /// so a turn whose thread was deleted reads as gone through these two routes |
| 738 | /// too — the same defence in depth [`super::chatbot`]'s twin carries. |
| 739 | async |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | /// Watch one turn as Server-Sent Events: `delta` chunks of the answer, then a |
| 753 | /// single `done` carrying the finished message, or one `error`. The stream |
| 754 | /// closes after `done`/`error` — one stream per turn, not per thread. |
| 755 | /// |
| 756 | /// Works whatever state the turn is in when the stream opens: an answer that |
| 757 | /// already landed replays as `delta`s and a `done` straight away, so a client |
| 758 | /// that reconnects late is never left hanging. Consume with `EventSource` |
| 759 | /// (cookies ride along on same-site / credentialed requests). The `done` |
| 760 | /// payload carries the whole settled turn — text, abstention, citations — so a |
| 761 | /// late reader gets the citations too, not only the deltas. |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | async |
| 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 | // ---- the one-shot study doors ---------------------------------------------- |
| 852 | // |
| 853 | // Two capabilities of the same RAG service, each exactly one dispatch: a |
| 854 | // summary of one page/span range, and a set of practice questions over one. |
| 855 | // They are this nest's only doors that neither thread nor store — the artifact |
| 856 | // *is* the answer — so they wait for it, and no per-user rate limiter charges |
| 857 | // them: one-shot requests cannot be retried into a loop the way a thread can, |
| 858 | // and the RAG counts its own per-call rate and budget caps. |
| 859 | // |
| 860 | // Authorization is the nest's own scope derivation, applied twice: the asker's |
| 861 | // `(sinif, ders)` pairs come from [`service::rag_scope::for_user`] — never from |
| 862 | // the body — and the body only *names* a target inside them |
| 863 | // ([`resolve_study_pair`]). The pair that travels to the service is the derived |
| 864 | // one, so the corpus it retrieves from is a corpus the backend already decided |
| 865 | // the asker may study. The service's own scope guard is defence in depth: when |
| 866 | // it answers `role_required`/`scope_mismatch` the **backend** built a bad |
| 867 | // request, and that is reported as one (a `502`, logged by |
| 868 | // [`crate::ai::rag_study`]). |
| 869 | |
| 870 | /// The scope half of a study request: which corpus, and which range inside it. |
| 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 | /// A `/rag/questions` body: the same scope, plus the shape of the set to |
| 912 | /// generate. |
| 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 | /// One citation behind a summary, as the bridge carries it. Unlike `rag.chat`'s |
| 941 | /// citations it names no corpus document, so nothing here resolves to a |
| 942 | /// course-note file: a summary is addressed to a range the caller selected. |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | /// A summary of one range of one corpus. `abstained` with a `reason` is a |
| 961 | /// complete answer, exactly as it is on a chat turn. |
| 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 | /// One generated practice question. The bridge's `soru`/`cevap`/`zorluk` become |
| 989 | /// this API's `question`/`answer`/`difficulty` here: the wire keeps the |
| 990 | /// service's vocabulary, the HTTP surface keeps the backend's. |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | /// A generated set of practice questions over one range of one corpus. |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | /// The one `(sınıf, ders)` pair a study request may address, resolved against |
| 1033 | /// the asker's own derived pairs — the body only *names* a target inside them. |
| 1034 | /// |
| 1035 | /// A named ders matches a pair **exactly** first — a picker's own spelling is |
| 1036 | /// never re-rolled — and only then through [`crate::domain::text_fold::search_fold`], |
| 1037 | /// the fold the RAG slug vocabulary rides: a citation names its course by slug |
| 1038 | /// (`biyoloji`), the pair carries the course title (`Biyoloji`), and the two |
| 1039 | /// spellings must resolve to one pair. A named grade must still be a pair the |
| 1040 | /// asker holds; anything else is a `403`, because a corpus the asker cannot |
| 1041 | /// study must not be summarized on their behalf. An absent or empty grade |
| 1042 | /// resolves the subject alone: exactly one pair for that `ders` is the target, |
| 1043 | /// none is a `403`, and more than one is a `400` — the caller must name the |
| 1044 | /// şube, since picking one for them would answer from a corpus they did not |
| 1045 | /// name. Two *distinct* courses folding to one key refuse the same way rather |
| 1046 | /// than routing on a guess. |
| 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 | /// Summarize one range of one corpus the caller may study. |
| 1093 | /// |
| 1094 | /// Synchronous by design: the summary is what the caller asked for, so there is |
| 1095 | /// no thread to open, no row to poll and nothing stored. The scope is derived |
| 1096 | /// by [`crate::service::rag_scope`] — a manager or an admin gets every |
| 1097 | /// class-course pair in the school, everyone else their own memberships — |
| 1098 | /// never from the body, which only names a target inside them — and the |
| 1099 | /// `(sinif, ders)` pair that reaches the service is the derived one. `503` |
| 1100 | /// when no AI service offers `rag.summarize`. |
| 1101 | /// `abstained: true` with a `reason` is a complete answer riding a `200`. |
| 1102 | /// |
| 1103 | /// The dispatch deadline (25 s) is deliberately under the middleware's own |
| 1104 | /// 30 s envelope ([`crate::constant::REQUEST_TIMEOUT_SECS`]): this door waits, |
| 1105 | /// and being dropped mid-flight would answer with prose written for a request |
| 1106 | /// that may have written something. |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | async |
| 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 | /// Generate practice questions over one range of one corpus the caller may |
| 1164 | /// study, with the answers bounded to that range. |
| 1165 | /// |
| 1166 | /// One-shot and synchronous like [`summarize`], and gated the same way: the |
| 1167 | /// same derived scope, the same `403`/`400` resolution, the same `503` when no |
| 1168 | /// service offers the capability, and the same rule that an abstention is a |
| 1169 | /// complete answer on a `200`. What it adds is the shape of the set: `n` |
| 1170 | /// (default 5, at most [`MAX_RAG_QUESTIONS`]), `difficulty` (default `orta`) |
| 1171 | /// and an optional `seed_question` to generate variations of one exercise. |
| 1172 | |
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | async |
| 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 | /// A relayed failure as the response a client branches on: the service's own |
| 1233 | /// code decides the status where it is one this backend knows, and anything |
| 1234 | /// else is a `502` — the backend cannot vouch for an answer it did not |
| 1235 | /// understand, and a gateway error is exactly that claim. |
| 1236 | /// |
| 1237 | /// `role_required` and `scope_mismatch` land in that fallback deliberately: |
| 1238 | /// both mean the **backend** composed a request the service may not answer, so |
| 1239 | /// [`crate::ai::rag_study`] logs them as bugs where they are detected and the |
| 1240 | /// caller is told the gateway failed rather than being handed the service's own |
| 1241 | /// wording for this side's defect. |
| 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 |