use gadk_agent::{AgentError, InvocationContext, ReadonlyContext};
pub async fn inject_session_state(
ctx: &ReadonlyContext,
template: &str,
) -> Result<String, AgentError> {
let ictx = InvocationContext::from_readonly(ctx);
crate::llm::processors::instructions::inject_session_state(&ictx, template).await
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use std::sync::Arc;
use gadk_agent::{Artifacts, InvocationContext};
use gadk_artifact::InMemoryArtifactService;
use gadk_genai::Part;
use gadk_session::{CreateRequest, InMemorySessionService, SessionService};
use serde_json::json;
use super::inject_session_state;
use crate::artifacts::ScopedArtifacts;
async fn ctx() -> InvocationContext {
let svc = InMemorySessionService::new();
let sess = svc
.create(CreateRequest {
app_name: "app".into(),
user_id: "u".into(),
session_id: Some("s".into()),
state: json!({"a": 1}).as_object().cloned(),
})
.await
.unwrap();
let artifacts: Arc<dyn Artifacts> = Arc::new(ScopedArtifacts::new(
Arc::new(InMemoryArtifactService::new()),
"app",
"u",
"s",
));
artifacts
.save("f", Part::from_text("file body"))
.await
.unwrap();
InvocationContext::builder()
.session(sess)
.artifacts(artifacts)
.build_invocation()
}
#[tokio::test]
async fn inject_session_state_public_renders_state_and_optional() {
let ctx = ctx().await;
let got = inject_session_state(ctx.as_readonly(), "x {a} {b?}")
.await
.unwrap();
assert_eq!(got, "x 1 ");
}
#[tokio::test]
async fn inject_session_state_public_missing_is_error() {
let ctx = ctx().await;
let err = inject_session_state(ctx.as_readonly(), "{missing}")
.await
.unwrap_err();
assert!(
matches!(
err,
gadk_agent::AgentError::Session(gadk_session::SessionError::StateKeyNotExist)
),
"{err:?}"
);
}
#[tokio::test]
async fn inject_session_state_public_renders_artifact() {
let ctx = ctx().await;
let got = inject_session_state(ctx.as_readonly(), "<{artifact.f}>")
.await
.unwrap();
assert_eq!(got, "<file body>");
}
}