hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! Payload contract for the `insight.*` capabilities, in both directions. |
| 2 | //! |
| 3 | //! ZEKA (`hezarfen_zeka`) computes student and class insights from a school's |
| 4 | //! own data. The family has two halves and they travel opposite ways: |
| 5 | //! |
| 6 | //! * **Outbound** (the backend dispatches to the service): |
| 7 | //! [`AI_INSIGHT_STUDENT_CAPABILITY`] — one student, on demand; |
| 8 | //! [`AI_INSIGHT_REFRESH_CAPABILITY`] — a school-wide sweep, on demand; |
| 9 | //! [`AI_INSIGHT_REPORT_CAPABILITY`] — a school-level report document, on |
| 10 | //! demand (its request carries the rows the backend already holds). The |
| 11 | //! dispatches below carry them ([`compute_student`], [`refresh`], |
| 12 | //! [`report`]). |
| 13 | //! * **Inbound** (the service calls the backend): the nine operations of |
| 14 | //! [`AI_INSIGHT_SUMMARY_UPSERT_CAPABILITY`]'s family — the storage surface |
| 15 | //! ZEKA's `zeka_*` rows are written and read through, because **no AI |
| 16 | //! service may touch a school database**. [`serve`] runs them; the SQL |
| 17 | //! lives in [`crate::db::insight`], and [`crate::web::insights`] mounts the |
| 18 | //! same functions as HTTP doors. This is the shape the whole family exists |
| 19 | //! for: an operation named on the wire, scoped to the frame's school, with |
| 20 | //! no path, no table and no query any caller can name. |
| 21 | //! |
| 22 | //! [`AI_INSIGHT_CLASS_CAPABILITY`] is declared (the service announces it) but |
| 23 | //! deliberately not dispatched: its request names a course the service would |
| 24 | //! have to enumerate a roster for, and the bridge's read allowlist carries no |
| 25 | //! member listing yet — the backend half of that contract is still open |
| 26 | //! (`hezarfen_zeka/service/docs/BACKEND-GEREKSINIMLERI.md`, item 3). Routing |
| 27 | //! it early would only hand the service a request it cannot answer. |
| 28 | //! |
| 29 | //! Every payload member is optional on the response side on purpose: the |
| 30 | //! service's TypedDicts are `total=False`, so a partial answer is a legal |
| 31 | //! answer, and a strict struct here would turn a service update into a |
| 32 | //! backend error. The inbound rows are the opposite: they are a *write* |
| 33 | //! contract, so a missing field is refused rather than defaulted — except |
| 34 | //! where this module says an omission is meaningful. |
| 35 | |
| 36 | use Duration; |
| 37 | |
| 38 | use DeserializeOwned; |
| 39 | use ; |
| 40 | use Value; |
| 41 | |
| 42 | use crateAiBridge; |
| 43 | use crateAiError; |
| 44 | use crateFrameError; |
| 45 | use crate |
| 46 | |
| 47 | |
| 48 | ; |
| 49 | use crateSchoolId; |
| 50 | |
| 51 | pub use crate |
| 52 | |
| 53 | |
| 54 | ; |
| 55 | |
| 56 | /// What the backend asks a service to compute for one student. |
| 57 | /// |
| 58 | /// `school` is **not** a payload member: it rides the frame's own |
| 59 | /// [`Request.school`](crate::ai::protocol::Request::school), and a second |
| 60 | /// copy here would be one more place the two could disagree. |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | /// What the service computed for one student. |
| 82 | /// |
| 83 | /// `signals` and `recommendations` stay JSON values: their shape belongs to |
| 84 | /// the service's rule catalogue, which is versioned far faster than this |
| 85 | /// crate, and the stored form of the same data is JSONB for exactly that |
| 86 | /// reason. A reader that needs a card's fields reads them by name. |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | /// What the backend would ask a service to compute for one class (a course |
| 106 | /// as one section teaches it). Defined for the contract; nothing dispatches it |
| 107 | /// yet — see the module docs. |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | /// What the service computed for one class. Same leniency as |
| 123 | /// [`StudentResponse`]: the loose members are the service's own shapes. |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | /// `roster_source` when the caller named the students themselves: the list is |
| 141 | /// passed through exactly as given. |
| 142 | pub const ROSTER_SOURCE_EXPLICIT: &str = "explicit"; |
| 143 | |
| 144 | /// `roster_source` when the door filled `user_ids` from the school's own |
| 145 | /// student roster (an empty request body). |
| 146 | pub const ROSTER_SOURCE_SCHOOL: &str = "school"; |
| 147 | |
| 148 | /// What the backend asks a service to recompute for a whole school. |
| 149 | /// |
| 150 | /// A batch job, and the door always fills `user_ids`: a caller-named list |
| 151 | /// passes through exactly as given ([`ROSTER_SOURCE_EXPLICIT`]), while an |
| 152 | /// empty one is filled from the school's own student roster |
| 153 | /// ([`ROSTER_SOURCE_SCHOOL`]) — the backend enumerates one now because the |
| 154 | /// service's own roster discovery reads homework `assigned` lists, which a |
| 155 | /// live school rarely fills, so an empty body used to sweep nobody. |
| 156 | /// `roster_source` records which of the two the list was, so the run's counts |
| 157 | /// read honestly: a `school` sweep that computed few students is a bounded |
| 158 | /// run, not a caller who named few. |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | /// The receipt for a refresh: what the sweep did, once it did it. The run's |
| 175 | /// own ledger is `zeka_run` in the school database, which is what a reader |
| 176 | /// polls while this answer is still on its way. |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | /// `kind` on a school-level report request — the one document kind this |
| 193 | /// dispatch asks for today. Spelled as the service's own vocabulary spells it |
| 194 | /// (`okul`), because it selects a builder there. |
| 195 | pub const REPORT_KIND_SCHOOL: &str = "okul"; |
| 196 | |
| 197 | /// What the backend asks a service to render for a whole school, for one run |
| 198 | /// day. |
| 199 | /// |
| 200 | /// Unlike the other two outbound requests, this one **carries the data**: the |
| 201 | /// service computes nothing here, and it may not read a school's database, so |
| 202 | /// the backend sends the rows it already holds from its own `zeka_*` tables — |
| 203 | /// the same serde rows the storage surface writes ([`SummaryRow`], |
| 204 | /// [`RecommendationRow`], [`ProfileRow`], [`RunRow`]) — and the service |
| 205 | /// renders them into one self-contained document. |
| 206 | /// |
| 207 | /// A run day with no `zeka_run` row, or one whose tables are empty, is a |
| 208 | /// legal request: the lists are then empty (or missing the absent day) and |
| 209 | /// the document's own `notes` say so. Nothing here refuses a day the ledger |
| 210 | /// does not know. |
| 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 | /// What the service rendered. `html` is the one required member: a response |
| 241 | /// without a document is not a partial answer, it is a dispatch that produced |
| 242 | /// nothing to store, and [`crate::ai::insight::report`]'s caller would rather |
| 243 | /// refuse it than persist an empty file. The rest is the service's own |
| 244 | /// bookkeeping, optional for the same reason every response member here is. |
| 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 | /// Compute one student's insight, awaited. No capability check here: the |
| 271 | /// caller decides what "no service" means, and every caller today is an HTTP |
| 272 | /// door that has already answered `503` for it ([`crate::web::insights`]). |
| 273 | pub async |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | /// Ask for a school-wide recompute, awaited. The service answers when its |
| 289 | /// batch is over; the deadline is [`AI_INSIGHT_REFRESH_TIMEOUT_SECS`]. |
| 290 | pub async |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | /// Ask for one school-level report document, awaited. The request carries |
| 306 | /// every row the document needs, and the answer carries the rendered HTML |
| 307 | /// back; the deadline is [`AI_INSIGHT_REPORT_TIMEOUT_SECS`] — short on |
| 308 | /// purpose, so the caller's own error path wins the race against the request |
| 309 | /// envelope ([`crate::constant::REQUEST_TIMEOUT_SECS`]). The caller stores the |
| 310 | /// document — this function only moves it. |
| 311 | pub async |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | /// One request, one typed answer — the dispatches differ only in capability, |
| 327 | /// deadline and payload type. |
| 328 | /// |
| 329 | /// A reply that does not parse as the expected type is a |
| 330 | /// [`FrameError::Malformed`]: the bytes were JSON, but not this contract's |
| 331 | /// JSON, and handing back a half-understood answer would be worse than |
| 332 | /// reporting the service out of step. |
| 333 | async |
| 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 | // ---- the operations the backend serves ------------------------------------- |
| 360 | // |
| 361 | // The half of this module that runs *towards* the backend. ZEKA computes, but |
| 362 | // it may not write: no AI service holds a school database credential on this |
| 363 | // deployment, so its `zeka_*` rows are written and read here, by name, over |
| 364 | // the same bridge — one operation per call, each scoped to the school the |
| 365 | // frame named. [`crate::db::insight`] is the SQL; this is the contract: which |
| 366 | // names exist, what each payload is, and how a refusal reads. |
| 367 | |
| 368 | pub use crate |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | ; |
| 374 | |
| 375 | use crate |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | ; |
| 382 | use crateDatabase; |
| 383 | use cratedb; |
| 384 | use crateTimestamp; |
| 385 | use crateAppError; |
| 386 | use crateModule; |
| 387 | use crateResolvedTenant; |
| 388 | |
| 389 | /// A refusal headed for a [`CapabilityResponse::Err`](crate::ai::protocol::CapabilityResponse::Err): |
| 390 | /// the documented code, and a message that names the field a caller can fix. |
| 391 | pub type Refusal = ; |
| 392 | |
| 393 | /// What a served capability is scoped to. |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | /// Every capability the backend serves, and its scope. Matched exactly: a |
| 404 | /// name that is not here is `unknown_capability`, with no prefix match and no |
| 405 | /// fallback — an operation a caller cannot name is one it cannot run. |
| 406 | pub const SERVED: & = & |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | ; |
| 420 | |
| 421 | /// The scope of one served capability, or `None` if the backend serves no |
| 422 | /// operation by that name. |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | /// Run one capability call and hand back its payload. |
| 431 | /// |
| 432 | /// `school` is the tenant the bridge resolved for the frame — `None` only for |
| 433 | /// a [`Scope::Deployment`] capability, whose frame names no school. The |
| 434 | /// school's own feature set is checked here as well as at the HTTP nest: a |
| 435 | /// school that is not being served insights should not be accumulating rows |
| 436 | /// for them either. |
| 437 | pub async |
| 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 | /// Decode one payload into the operation's own type. A payload that does not |
| 519 | /// fit is `invalid_payload`, never `malformed`: the frame was a capability |
| 520 | /// call, the body was not this operation's body, and the caller can fix it. |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | /// [`AppError`] as the protocol's refusal vocabulary. The mapping is the |
| 531 | /// whole reason the db layer returns `AppError` rather than its own type: |
| 532 | /// HTTP answers these through [`crate::error`], and a capability call answers |
| 533 | /// the same verdict with the code a service branches on. |
| 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 |