diff --git a/services/common/src/knowledge/sources/sanity_mcp.rs b/services/common/src/knowledge/sources/sanity_mcp.rs index 76f45a05a..224637041 100644 --- a/services/common/src/knowledge/sources/sanity_mcp.rs +++ b/services/common/src/knowledge/sources/sanity_mcp.rs @@ -661,10 +661,23 @@ fn dataset_names(v: &Value) -> Vec { }) { push(m["name"].as_str().unwrap_or("")); } - if let Some(a) = find_key(v, "datasets").and_then(Value::as_array) { - for n in a.iter().filter_map(Value::as_str) { - push(n); + match find_key(v, "datasets") { + Some(Value::Array(a)) => { + for n in a.iter().filter_map(Value::as_str) { + push(n); + } + } + // Sanity's server answers in XML with each dataset as an element + // named after it (`public + // ...`): the names are the keys. + Some(Value::Object(m)) => { + for (name, d) in m { + if d.get("datasetProfile").and_then(Value::as_str) != Some("comments") { + push(name); + } + } } + _ => {} } out.sort(); out @@ -829,6 +842,58 @@ mod tests { } } + /// Sanity's server, as it answered a real project: a sentence, then XML + /// with each dataset as an element named after it. + #[tokio::test] + async fn datasets_named_by_their_elements_are_listed() { + let reply = "Here are the datasets: \n\n \n public\n 2024-01-08T21:23:57.066Z\n \n \n private\n 2024-02-01T10:00:00.000Z\n \n \n private\n comments\n \n"; + let v = super::super::mcp::data_in(reply).expect("the XML is read"); + assert_eq!(dataset_names(&v), vec!["production", "staging"]); + + let mcp = server(move |tool, _| match tool { + "list_datasets" => Ok(json!({"content": [{"type": "text", "text": reply}]})), + _ => Ok(text_result(&json!([]))), + }); + let c = SanityMcp { + tools: mcp, + now: 0, + hints: Vec::new(), + }; + let nodes = c + .browse(Some("project:abc123"), "") + .await + .expect("the datasets are listed"); + let titles: Vec<&str> = nodes.iter().map(|n| n.title.as_str()).collect(); + assert_eq!(titles, vec!["production", "staging"]); + } + + /// A query answered the way Sanity's server formats it: a sentence, then + /// `` holding the JSON of the one result. + #[tokio::test] + async fn a_query_answered_in_sanitys_xml_is_read() { + let mcp = server(|tool, _| match tool { + "query_documents" => Ok(json!({"content": [{"type": "text", "text": + "Query executed successfully. Found 1 total document, returning 1 (40 tokens): \n\n {\n \"chTypes\": [\n \"post\",\n \"venue\"\n ]\n}\n\n1\n1\n"}]})), + _ => Ok(text_result(&json!([]))), + }); + let c = SanityMcp { + tools: mcp, + now: 0, + hints: Vec::new(), + }; + let v = c + .query( + "abc123", + "production", + "chTypes", + "array::unique(*[]._type)", + json!({}), + ) + .await + .expect("the answer is read"); + assert_eq!(v, json!(["post", "venue"])); + } + fn doc(id: &str, rev: &str, at: &str) -> Value { json!({"_id": id, "_type": "post", "_rev": rev, "_updatedAt": at, "title": format!("Post {id}")}) }