diff --git a/src/ai/rag_study.rs b/src/ai/rag_study.rs index 883aedc..cb2c29e 100644 --- a/src/ai/rag_study.rs +++ b/src/ai/rag_study.rs @@ -64,6 +64,9 @@ pub struct RagScope { pub struct RagSummarizePayload { /// The corpus and range to summarize. pub scope: RagScope, + /// Backend-computed caller grant, in the same `(sinif, ders)` pair shape as `rag.chat`. + #[serde(default)] + pub scope_pairs: Vec<(Option, String)>, /// The id of the person who asked. The service reads that person's own /// data back through the bridge's api reads (`on_behalf_of`). Read live /// from the authenticated session, never taken from a request body. @@ -73,7 +76,6 @@ pub struct RagSummarizePayload { /// student's role may not have. pub asker_role: String, } - /// What the backend asks a RAG service for (`rag.questions`). #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct RagQuestionsPayload { @@ -81,6 +83,9 @@ pub struct RagQuestionsPayload { /// on their answers: a question whose answer is not in this range must not /// be generated. pub scope: RagScope, + /// Backend-computed caller grant, in the same `(sinif, ders)` pair shape as `rag.chat`. + #[serde(default)] + pub scope_pairs: Vec<(Option, String)>, /// The id of the person who asked, exactly as [`RagSummarizePayload`]'s. pub asker: String, /// The asker's live school role. @@ -317,6 +322,7 @@ mod tests { span_ids: vec![], scope_label: "DNA".into(), }, + scope_pairs: vec![(Some("10".into()), "Biyoloji".into())], asker: "01ASKER".into(), asker_role: "teacher".into(), }) @@ -331,6 +337,7 @@ mod tests { "span_ids": [], "scope_label": "DNA", }, + "scope_pairs": [["10", "Biyoloji"]], "asker": "01ASKER", "asker_role": "teacher", }) @@ -346,6 +353,7 @@ mod tests { span_ids: vec!["s-7".into()], scope_label: String::new(), }, + scope_pairs: vec![(None, "Satranç Kulübü".into())], asker: "01ASKER".into(), asker_role: "student".into(), n: 5, @@ -363,6 +371,7 @@ mod tests { "span_ids": ["s-7"], "scope_label": "", }, + "scope_pairs": [[null, "Satranç Kulübü"]], "asker": "01ASKER", "asker_role": "student", "n": 5, diff --git a/src/web/rag.rs b/src/web/rag.rs index 8281154..d40145d 100644 --- a/src/web/rag.rs +++ b/src/web/rag.rs @@ -1146,11 +1146,14 @@ async fn summarize( let pair = resolve_study_pair(&pairs, &req.ders, req.sinif.as_deref())?; let payload = RagSummarizePayload { scope: req.into_scope(pair), + scope_pairs: pairs + .iter() + .map(|pair| (pair.sinif.clone(), pair.ders.clone())) + .collect(), // What the session already proves, never what the body claims. asker: user.get_id().key(), asker_role: user.get_role().as_str().to_string(), }; - match rag_study::summarize(&bridge, &school, payload).await { Ok(reply) => Ok(Json(RagSummarizeResponse::from(reply)).into_response()), Err(code) => Ok(failure(&code)), @@ -1209,6 +1212,10 @@ async fn questions( } let payload = RagQuestionsPayload { scope: req.scope.into_scope(pair), + scope_pairs: pairs + .iter() + .map(|pair| (pair.sinif.clone(), pair.ders.clone())) + .collect(), asker: user.get_id().key(), asker_role: user.get_role().as_str().to_string(), n: req.n, diff --git a/tests/ai_bridge.rs b/tests/ai_bridge.rs index bc95918..413b109 100644 --- a/tests/ai_bridge.rs +++ b/tests/ai_bridge.rs @@ -3497,6 +3497,12 @@ async fn the_summarize_request_carries_the_askers_scope_and_role() { }), "a club is school-wide, so its scope names no grade" ); + assert_eq!( + payload["scope_pairs"], + json!([[null, "Satranç Kulübü"]]), + "the frame carries the backend-computed grant, not only the selected scope" + ); + } /// 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() { "scope_label": "", }) ); + assert_eq!( + payload["scope_pairs"], + json!([[null, "Satranç Kulübü"]]), + "the frame carries the backend-computed grant, not only the selected scope" + ); + // The knobs' defaults are the documented ones, and a body that names none // of them still dispatches a well-formed request. @@ -3625,6 +3637,45 @@ async fn the_questions_request_carries_its_knobs_and_maps_its_rows() { assert_eq!(seen[1].payload["seed_question"], Value::Null); } +/// Manager and admin callers receive the whole school-derived scope, not just +/// the one pair selected by the study request body. +#[tokio::test] +async fn a_manager_study_request_carries_the_school_wide_scope_pairs() { + let bridge = bridge().await; + let service = connect_service( + &bridge, + hello("rag", &[AI_RAG_SUMMARIZE_CAPABILITY]), + Behaviour::Answer(json!({ "text": "ok" })), + ) + .await; + await_workers(&bridge, 1).await; + let (app, db) = chat_app(&bridge).await; + let manager = common::login_as(&app, &db, "mudur", "manager").await; + + common::taught(&app, &manager, "Matematik").await; + common::taught(&app, &manager, "Fizik").await; + + let res = common::send( + &app, + "POST", + "/rag/summarize", + Some(&manager), + Some(json!({ "ders": "Matematik", "pages": [1] })), + ) + .await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + + let seen = service.seen(); + assert_eq!(seen.len(), 1, "one dispatch per call, never a re-send"); + assert_eq!(seen[0].payload["asker_role"], "manager"); + let pairs = seen[0].payload["scope_pairs"] + .as_array() + .expect("scope_pairs is an array"); + assert_eq!(pairs.len(), 2, "manager receives every school pair"); + assert!(pairs.contains(&json!([null, "Matematik"]))); + assert!(pairs.contains(&json!([null, "Fizik"]))); +} + /// A connected worker that does not carry the capability is not a worker for /// these doors: both answer the shared `503`, and nothing is dispatched. #[tokio::test]