Send the caller's scope pairs on study frames
Browse files at this commit987866d507848bf72da4f73f3d5d835874036bd4Parent: 1e8567d- fix
- send the caller's scope pairs on study frames
3 changed files +69 -2
- src/ai/rag_study.rs +10 -1
- src/web/rag.rs +8 -1
- tests/ai_bridge.rs +51 -0
src/ai/rag_study.rsmodified+10 -1
| @@ -64,6 +64,9 @@ pub struct RagScope { | |||
| 64 | pub struct RagSummarizePayload { | 64 | pub struct RagSummarizePayload { |
| 65 | /// The corpus and range to summarize. | 65 | /// The corpus and range to summarize. |
| 66 | pub scope: RagScope, | 66 | pub scope: RagScope, |
| 67 | + /// Backend-computed caller grant, in the same `(sinif, ders)` pair shape as `rag.chat`. | ||
| 68 | + #[serde(default)] | ||
| 69 | + pub scope_pairs: Vec<(Option<String>, String)>, | ||
| 67 | /// The id of the person who asked. The service reads that person's own | 70 | /// The id of the person who asked. The service reads that person's own |
| 68 | /// data back through the bridge's api reads (`on_behalf_of`). Read live | 71 | /// data back through the bridge's api reads (`on_behalf_of`). Read live |
| 69 | /// from the authenticated session, never taken from a request body. | 72 | /// from the authenticated session, never taken from a request body. |
| @@ -73,7 +76,6 @@ pub struct RagSummarizePayload { | |||
| 73 | /// student's role may not have. | 76 | /// student's role may not have. |
| 74 | pub asker_role: String, | 77 | pub asker_role: String, |
| 75 | } | 78 | } |
| 76 | - | ||
| 77 | /// What the backend asks a RAG service for (`rag.questions`). | 79 | /// What the backend asks a RAG service for (`rag.questions`). |
| 78 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | 80 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 79 | pub struct RagQuestionsPayload { | 81 | pub struct RagQuestionsPayload { |
| @@ -81,6 +83,9 @@ pub struct RagQuestionsPayload { | |||
| 81 | /// on their answers: a question whose answer is not in this range must not | 83 | /// on their answers: a question whose answer is not in this range must not |
| 82 | /// be generated. | 84 | /// be generated. |
| 83 | pub scope: RagScope, | 85 | pub scope: RagScope, |
| 86 | + /// Backend-computed caller grant, in the same `(sinif, ders)` pair shape as `rag.chat`. | ||
| 87 | + #[serde(default)] | ||
| 88 | + pub scope_pairs: Vec<(Option<String>, String)>, | ||
| 84 | /// The id of the person who asked, exactly as [`RagSummarizePayload`]'s. | 89 | /// The id of the person who asked, exactly as [`RagSummarizePayload`]'s. |
| 85 | pub asker: String, | 90 | pub asker: String, |
| 86 | /// The asker's live school role. | 91 | /// The asker's live school role. |
| @@ -317,6 +322,7 @@ mod tests { | |||
| 317 | span_ids: vec![], | 322 | span_ids: vec![], |
| 318 | scope_label: "DNA".into(), | 323 | scope_label: "DNA".into(), |
| 319 | }, | 324 | }, |
| 325 | + scope_pairs: vec![(Some("10".into()), "Biyoloji".into())], | ||
| 320 | asker: "01ASKER".into(), | 326 | asker: "01ASKER".into(), |
| 321 | asker_role: "teacher".into(), | 327 | asker_role: "teacher".into(), |
| 322 | }) | 328 | }) |
| @@ -331,6 +337,7 @@ mod tests { | |||
| 331 | "span_ids": [], | 337 | "span_ids": [], |
| 332 | "scope_label": "DNA", | 338 | "scope_label": "DNA", |
| 333 | }, | 339 | }, |
| 340 | + "scope_pairs": [["10", "Biyoloji"]], | ||
| 334 | "asker": "01ASKER", | 341 | "asker": "01ASKER", |
| 335 | "asker_role": "teacher", | 342 | "asker_role": "teacher", |
| 336 | }) | 343 | }) |
| @@ -346,6 +353,7 @@ mod tests { | |||
| 346 | span_ids: vec!["s-7".into()], | 353 | span_ids: vec!["s-7".into()], |
| 347 | scope_label: String::new(), | 354 | scope_label: String::new(), |
| 348 | }, | 355 | }, |
| 356 | + scope_pairs: vec![(None, "Satranç Kulübü".into())], | ||
| 349 | asker: "01ASKER".into(), | 357 | asker: "01ASKER".into(), |
| 350 | asker_role: "student".into(), | 358 | asker_role: "student".into(), |
| 351 | n: 5, | 359 | n: 5, |
| @@ -363,6 +371,7 @@ mod tests { | |||
| 363 | "span_ids": ["s-7"], | 371 | "span_ids": ["s-7"], |
| 364 | "scope_label": "", | 372 | "scope_label": "", |
| 365 | }, | 373 | }, |
| 374 | + "scope_pairs": [[null, "Satranç Kulübü"]], | ||
| 366 | "asker": "01ASKER", | 375 | "asker": "01ASKER", |
| 367 | "asker_role": "student", | 376 | "asker_role": "student", |
| 368 | "n": 5, | 377 | "n": 5, |
src/web/rag.rsmodified+8 -1
| @@ -1146,11 +1146,14 @@ async fn summarize( | |||
| 1146 | let pair = resolve_study_pair(&pairs, &req.ders, req.sinif.as_deref())?; | 1146 | let pair = resolve_study_pair(&pairs, &req.ders, req.sinif.as_deref())?; |
| 1147 | let payload = RagSummarizePayload { | 1147 | let payload = RagSummarizePayload { |
| 1148 | scope: req.into_scope(pair), | 1148 | scope: req.into_scope(pair), |
| 1149 | + scope_pairs: pairs | ||
| 1150 | + .iter() | ||
| 1151 | + .map(|pair| (pair.sinif.clone(), pair.ders.clone())) | ||
| 1152 | + .collect(), | ||
| 1149 | // What the session already proves, never what the body claims. | 1153 | // What the session already proves, never what the body claims. |
| 1150 | asker: user.get_id().key(), | 1154 | asker: user.get_id().key(), |
| 1151 | asker_role: user.get_role().as_str().to_string(), | 1155 | asker_role: user.get_role().as_str().to_string(), |
| 1152 | }; | 1156 | }; |
| 1153 | - | ||
| 1154 | match rag_study::summarize(&bridge, &school, payload).await { | 1157 | match rag_study::summarize(&bridge, &school, payload).await { |
| 1155 | Ok(reply) => Ok(Json(RagSummarizeResponse::from(reply)).into_response()), | 1158 | Ok(reply) => Ok(Json(RagSummarizeResponse::from(reply)).into_response()), |
| 1156 | Err(code) => Ok(failure(&code)), | 1159 | Err(code) => Ok(failure(&code)), |
| @@ -1209,6 +1212,10 @@ async fn questions( | |||
| 1209 | } | 1212 | } |
| 1210 | let payload = RagQuestionsPayload { | 1213 | let payload = RagQuestionsPayload { |
| 1211 | scope: req.scope.into_scope(pair), | 1214 | scope: req.scope.into_scope(pair), |
| 1215 | + scope_pairs: pairs | ||
| 1216 | + .iter() | ||
| 1217 | + .map(|pair| (pair.sinif.clone(), pair.ders.clone())) | ||
| 1218 | + .collect(), | ||
| 1212 | asker: user.get_id().key(), | 1219 | asker: user.get_id().key(), |
| 1213 | asker_role: user.get_role().as_str().to_string(), | 1220 | asker_role: user.get_role().as_str().to_string(), |
| 1214 | n: req.n, | 1221 | n: req.n, |
tests/ai_bridge.rsmodified+51 -0
| @@ -3497,6 +3497,12 @@ async fn the_summarize_request_carries_the_askers_scope_and_role() { | |||
| 3497 | }), | 3497 | }), |
| 3498 | "a club is school-wide, so its scope names no grade" | 3498 | "a club is school-wide, so its scope names no grade" |
| 3499 | ); | 3499 | ); |
| 3500 | + assert_eq!( | ||
| 3501 | + payload["scope_pairs"], | ||
| 3502 | + json!([[null, "Satranç Kulübü"]]), | ||
| 3503 | + "the frame carries the backend-computed grant, not only the selected scope" | ||
| 3504 | + ); | ||
| 3505 | + | ||
| 3500 | } | 3506 | } |
| 3501 | 3507 | ||
| 3502 | /// A study request launched off an answer's citation names the course by the | 3508 | /// A study request launched off an answer's citation names the course by the |
| @@ -3606,6 +3612,12 @@ async fn the_questions_request_carries_its_knobs_and_maps_its_rows() { | |||
| 3606 | "scope_label": "", | 3612 | "scope_label": "", |
| 3607 | }) | 3613 | }) |
| 3608 | ); | 3614 | ); |
| 3615 | + assert_eq!( | ||
| 3616 | + payload["scope_pairs"], | ||
| 3617 | + json!([[null, "Satranç Kulübü"]]), | ||
| 3618 | + "the frame carries the backend-computed grant, not only the selected scope" | ||
| 3619 | + ); | ||
| 3620 | + | ||
| 3609 | 3621 | ||
| 3610 | // The knobs' defaults are the documented ones, and a body that names none | 3622 | // The knobs' defaults are the documented ones, and a body that names none |
| 3611 | // of them still dispatches a well-formed request. | 3623 | // of them still dispatches a well-formed request. |
| @@ -3625,6 +3637,45 @@ async fn the_questions_request_carries_its_knobs_and_maps_its_rows() { | |||
| 3625 | assert_eq!(seen[1].payload["seed_question"], Value::Null); | 3637 | assert_eq!(seen[1].payload["seed_question"], Value::Null); |
| 3626 | } | 3638 | } |
| 3627 | 3639 | ||
| 3640 | +/// Manager and admin callers receive the whole school-derived scope, not just | ||
| 3641 | +/// the one pair selected by the study request body. | ||
| 3642 | +#[tokio::test] | ||
| 3643 | +async fn a_manager_study_request_carries_the_school_wide_scope_pairs() { | ||
| 3644 | + let bridge = bridge().await; | ||
| 3645 | + let service = connect_service( | ||
| 3646 | + &bridge, | ||
| 3647 | + hello("rag", &[AI_RAG_SUMMARIZE_CAPABILITY]), | ||
| 3648 | + Behaviour::Answer(json!({ "text": "ok" })), | ||
| 3649 | + ) | ||
| 3650 | + .await; | ||
| 3651 | + await_workers(&bridge, 1).await; | ||
| 3652 | + let (app, db) = chat_app(&bridge).await; | ||
| 3653 | + let manager = common::login_as(&app, &db, "mudur", "manager").await; | ||
| 3654 | + | ||
| 3655 | + common::taught(&app, &manager, "Matematik").await; | ||
| 3656 | + common::taught(&app, &manager, "Fizik").await; | ||
| 3657 | + | ||
| 3658 | + let res = common::send( | ||
| 3659 | + &app, | ||
| 3660 | + "POST", | ||
| 3661 | + "/rag/summarize", | ||
| 3662 | + Some(&manager), | ||
| 3663 | + Some(json!({ "ders": "Matematik", "pages": [1] })), | ||
| 3664 | + ) | ||
| 3665 | + .await; | ||
| 3666 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 3667 | + | ||
| 3668 | + let seen = service.seen(); | ||
| 3669 | + assert_eq!(seen.len(), 1, "one dispatch per call, never a re-send"); | ||
| 3670 | + assert_eq!(seen[0].payload["asker_role"], "manager"); | ||
| 3671 | + let pairs = seen[0].payload["scope_pairs"] | ||
| 3672 | + .as_array() | ||
| 3673 | + .expect("scope_pairs is an array"); | ||
| 3674 | + assert_eq!(pairs.len(), 2, "manager receives every school pair"); | ||
| 3675 | + assert!(pairs.contains(&json!([null, "Matematik"]))); | ||
| 3676 | + assert!(pairs.contains(&json!([null, "Fizik"]))); | ||
| 3677 | +} | ||
| 3678 | + | ||
| 3628 | /// A connected worker that does not carry the capability is not a worker for | 3679 | /// A connected worker that does not carry the capability is not a worker for |
| 3629 | /// these doors: both answer the shared `503`, and nothing is dispatched. | 3680 | /// these doors: both answer the shared `503`, and nothing is dispatched. |
| 3630 | #[tokio::test] | 3681 | #[tokio::test] |