Incremental engine for long horizon agents π Star if you like it!
Rust agentic-data-frameworkaiai-agentschange-data-capturecodebase-intelligencecontext-engineeringdata-engineeringdata-indexingdata-processingetlhelp-wantedindexingknowledge-graphllmlong-horizon-agentpythonragreal-timerustsemantic-search
feat(rust-sdk): ApiChatClient is a MemoInput, so it can sit in a detect_change key (#2723)
LJ π₯₯π΄38bca8785045
diff --git a/rust/sdk/cocoindex/src/ops/api.rs b/rust/sdk/cocoindex/src/ops/api.rsindex a40495b3..acf6646b 100644--- a/rust/sdk/cocoindex/src/ops/api.rs+++ b/rust/sdk/cocoindex/src/ops/api.rs@@ -678,12 +678,18 @@ impl ApiChatClient {}}-/// The memo identity of a client's answers: the model and the endpoint.-fn client_identity(client: &ApiChatClient) -> (&str, &str) {- (&client.model, &client.endpoint.base_url)+/// A client's memo identity is the model and the endpoint: what can change an+/// answer. The key, timeout and in-flight cap cannot, and are absent. So a+/// `detect_change` context key holding a client invalidates memoized work+/// when the model or endpoint changes.+#[async_trait]+impl MemoInput for ApiChatClient {+ fn write_memo_key(&self, writer: &mut crate::memo::MemoKeyWriter<'_>) -> Result<()> {+ writer.write(&(&self.model, &self.endpoint.base_url))+ }}-#[crate::function(memo, memo_key(client = client_identity))]+#[crate::function(memo)]async fn chat_json_memoized(ctx: &Ctx,client: &ApiChatClient,diff --git a/rust/sdk/cocoindex/tests/ops_api.rs b/rust/sdk/cocoindex/tests/ops_api.rsindex 061e282d..a2bc33f9 100644--- a/rust/sdk/cocoindex/tests/ops_api.rs+++ b/rust/sdk/cocoindex/tests/ops_api.rs@@ -690,6 +690,58 @@ async fn chat_client_extract_deserializes_and_memoizes() {assert_eq!(body["response_format"]["type"], "json_object");}+cocoindex::context_key!(static CHAT: ApiChatClient, detect_change);++static CHAT_BODY_CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);++/// Reads the chat client from a `detect_change` key and calls nothing, so+/// only the key's memo identity decides whether the body runs again.+#[cocoindex::function(memo)]+async fn uses_chat_client(ctx: &Ctx, text: &str) -> cocoindex::Result<String> {+ let client = ctx.get_key(&CHAT)?;+ CHAT_BODY_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);+ Ok(format!("{}:{text}", client.model()))+}++/// `ApiChatClient` is a `MemoInput`, so it can sit in a `detect_change` key:+/// changing the model reruns memoized work, an unchanged client does not.+#[tokio::test]+async fn chat_client_in_a_detect_change_key_invalidates_on_model_change() {+ use std::sync::atomic::Ordering;+ let tempdir = tempfile::tempdir().unwrap();+ let run = |model: &'static str| {+ let db = tempdir.path().join("db");+ async move {+ let app = cocoindex::Environment::builder()+ .db_path(db)+ .provide_key(&CHAT, ApiChatClient::new(model))+ .build()+ .await+ .unwrap()+ .app("chat_detect_change")+ .await+ .unwrap();+ app.update(|ctx| async move { uses_chat_client(&ctx, "hi").await })+ .await+ .unwrap()+ }+ };++ assert_eq!(run("model-a").await, "model-a:hi");+ assert_eq!(run("model-a").await, "model-a:hi");+ assert_eq!(+ CHAT_BODY_CALLS.load(Ordering::SeqCst),+ 1,+ "unchanged client: memo hit"+ );+ assert_eq!(run("model-b").await, "model-b:hi");+ assert_eq!(+ CHAT_BODY_CALLS.load(Ordering::SeqCst),+ 2,+ "a new model reruns the body"+ );+}+#[tokio::test]async fn chat_client_extract_rejects_an_answer_of_another_shape() {let Some(server) = mock_server().await else {