use std::collections::BTreeMap;
use std::sync::Arc;
use futures::future::{BoxFuture, ready};
use gadk_agent::{
AfterModelCallback, AgentError, BeforeModelCallback, Context, Plugin, PluginConfig,
};
use gadk_genai::{Schema, Type};
use gadk_model::{LlmRequest, LlmResponse, ModelError};
use crate::llm::flow::model_error_into_agent;
use crate::llm::processors::wrap_err;
const NAME: &str = "FunctionCallModifierPlugin";
type RequestResult = BoxFuture<'static, (LlmRequest, Result<Option<LlmResponse>, AgentError>)>;
type ResponseResult = BoxFuture<'static, (LlmResponse, Result<Option<LlmResponse>, AgentError>)>;
#[derive(Clone)]
pub struct FunctionCallModifierConfig {
pub predicate: Arc<dyn Fn(&str) -> bool + Send + Sync>,
pub args: BTreeMap<String, Schema>,
#[allow(clippy::type_complexity)] pub override_description: Option<Arc<dyn Fn(&str) -> String + Send + Sync>>,
}
pub fn new_function_call_modifier_plugin(cfg: FunctionCallModifierConfig) -> Plugin {
let cfg = Arc::new(cfg);
let before_model: BeforeModelCallback = {
let cfg = cfg.clone();
Arc::new(move |_ctx: Context, mut req: LlmRequest| -> RequestResult {
add_args(&cfg, &mut req);
Box::pin(ready((req, Ok(None))))
})
};
let after_model: AfterModelCallback = Arc::new(
move |ctx: Context, resp: LlmResponse, err: Option<Arc<ModelError>>| -> ResponseResult {
Box::pin(ready(strip_args(&cfg, &ctx, resp, err)))
},
);
Plugin::new(PluginConfig {
name: NAME.into(),
before_model_callback: Some(before_model),
after_model_callback: Some(after_model),
..Default::default()
})
}
fn add_args(cfg: &FunctionCallModifierConfig, req: &mut LlmRequest) {
let LlmRequest { config, tools, .. } = req;
let Some(config) = config.as_mut() else {
return;
};
for tool in &mut config.tools {
for decl in &mut tool.function_declarations {
if !tools.contains_key(decl.name.as_str()) || !(cfg.predicate)(decl.name.as_str()) {
continue;
}
let params = decl.parameters.get_or_insert_with(|| Schema {
r#type: Some(Type::Object),
..Default::default()
});
params
.properties
.extend(cfg.args.iter().map(|(k, v)| (k.clone(), v.clone())));
if let Some(override_description) = &cfg.override_description {
decl.description = override_description(decl.description.as_str());
}
}
}
}
fn strip_args(
cfg: &FunctionCallModifierConfig,
ctx: &Context,
mut resp: LlmResponse,
err: Option<Arc<ModelError>>,
) -> (LlmResponse, Result<Option<LlmResponse>, AgentError>) {
if let Some(err) = err {
return (resp, Err(model_error_into_agent(err)));
}
if resp.content.as_ref().is_none_or(|c| c.parts.is_empty()) {
return (resp, Ok(None));
}
let state = ctx.state();
let mut failure = None;
if let Some(content) = resp.content.as_mut() {
'parts: for part in &mut content.parts {
let Some(fc) = part.function_call.as_mut() else {
continue;
};
if !(cfg.predicate)(fc.name.as_str()) {
continue;
}
for name in cfg.args.keys() {
let Some(arg) = fc.args.remove(name) else {
continue;
};
let state_key = format!("{}/{}", fc.id, name);
if let Err(e) = state.set(&state_key, arg) {
failure = Some(wrap_err("failed to set state", e));
break 'parts;
}
}
}
}
match failure {
Some(e) => (resp, Err(e)),
None => (resp, Ok(None)),
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use gadk_genai::{Content, FunctionCall, Part};
use serde_json::{Map, json};
use super::*;
fn cfg() -> FunctionCallModifierConfig {
FunctionCallModifierConfig {
predicate: Arc::new(|_: &str| true),
args: BTreeMap::from([("skill_id".to_string(), Schema::default())]),
override_description: None,
}
}
#[test]
fn plugin_is_named_like_go() {
let p = new_function_call_modifier_plugin(cfg());
assert_eq!(p.name(), "FunctionCallModifierPlugin");
assert!(p.before_model_callback().is_some());
assert!(p.after_model_callback().is_some());
}
#[tokio::test]
async fn after_model_passes_a_model_error_through() {
let cb = new_function_call_modifier_plugin(cfg())
.after_model_callback()
.unwrap();
let (_, res) = cb(
Context::builder().build(),
LlmResponse::default(),
Some(Arc::new(ModelError::EmptyResponse)),
)
.await;
let err = res.unwrap_err();
assert!(
matches!(err, AgentError::Model(ModelError::EmptyResponse)),
"{err:?}"
);
assert_eq!(err.to_string(), "empty response");
}
#[tokio::test]
async fn after_model_leaves_a_partless_response_to_the_chain() {
let cb = new_function_call_modifier_plugin(cfg())
.after_model_callback()
.unwrap();
let (resp, res) = cb(Context::builder().build(), LlmResponse::default(), None).await;
assert_eq!(resp, LlmResponse::default());
assert_eq!(res.unwrap(), None);
}
#[tokio::test]
async fn after_model_hands_back_the_stripped_response_with_ok_none() {
let cb = new_function_call_modifier_plugin(cfg())
.after_model_callback()
.unwrap();
let resp = LlmResponse {
content: Some(Content {
parts: vec![Part {
function_call: Some(FunctionCall {
id: "id1".into(),
name: "t".into(),
args: json!({"skill_id": "s", "x": 1})
.as_object()
.cloned()
.unwrap(),
..Default::default()
}),
..Default::default()
}],
..Default::default()
}),
..Default::default()
};
let ctx = Context::builder().build();
let (got, res) = cb(ctx.clone(), resp, None).await;
assert!(matches!(res, Ok(None)), "{res:?}");
let fc = got.content.unwrap().parts[0].function_call.clone().unwrap();
let want: Map<String, serde_json::Value> = json!({"x": 1}).as_object().cloned().unwrap();
assert_eq!(fc.args, want);
assert_eq!(ctx.state().get("id1/skill_id").unwrap(), json!("s"));
}
#[test]
fn before_model_adds_object_parameters_only_to_packed_selected_declarations() {
use gadk_genai::{FunctionDeclaration, GenerateContentConfig, Tool as GenaiTool};
let mut req = LlmRequest {
config: Some(GenerateContentConfig {
tools: vec![GenaiTool {
function_declarations: vec![
FunctionDeclaration {
name: "packed".into(),
..Default::default()
},
FunctionDeclaration {
name: "unpacked".into(),
..Default::default()
},
],
..Default::default()
}],
..Default::default()
}),
..Default::default()
};
req.tools.insert(
"packed".into(),
Arc::new(()) as Arc<dyn std::any::Any + Send + Sync>,
);
add_args(&cfg(), &mut req);
let decls = &req.config.as_ref().unwrap().tools[0].function_declarations;
let p = decls[0].parameters.as_ref().unwrap();
assert_eq!(p.r#type, Some(Type::Object));
assert!(p.properties.contains_key("skill_id"));
assert!(p.required.is_empty());
assert!(decls[1].parameters.is_none());
}
}