gadk-engine 0.4.2

The gadk runtime: LLM agent and flow, workflow agents and graphs, and the runner
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright 2026 Chris Olson
// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! `AgentTool`, `AgentToolConfig`: run an agent as a tool.

use std::any::Any;
use std::collections::BTreeSet;
use std::sync::Arc;

use async_trait::async_trait;
use futures::StreamExt;
use gadk_agent::{
    Agent, AgentError, Context, RunConfig, RunnableTool, StreamingMode, Tool, ToolError,
    ToolOriginKind,
};
use gadk_genai::{Content, FunctionDeclaration, ROLE_USER};
use gadk_session::{CreateRequest, InMemorySessionService, SessionService};
use serde_json::{Map, Value};

use crate::artifacts::new_forwarding_service;
use crate::llm::processors::{as_go_floats, go_json_marshal, go_sprint};
use crate::llm::state::reveal;
use crate::runner::{PluginConfig, RunOptions, Runner, RunnerConfig};
use crate::tool::single_turn::{input_schema_of, make_function_declaration};
use crate::tool::utils::{json_kind_article, validate_map_on_schema, validate_output_schema};
use crate::workflow::route::go_quote;

/// Configuration of an [`AgentTool`].
#[derive(Clone, Default)]
pub struct AgentToolConfig {
    /// If true, the calling agent skips summarization after the sub-agent finishes.
    pub skip_summarization: bool,
}

/// A tool that runs an agent in a child runner over a fresh
/// in-memory session and returns its final answer.
pub struct AgentTool {
    agent: Arc<dyn Agent>,
    skip_summarization: bool,
}

impl AgentTool {
    /// Wraps `agent` as a tool; `None` means `skip_summarization = false`.
    #[allow(clippy::new_ret_no_self)] // returns the tool as `Arc<dyn Tool>`
    pub fn new(agent: Arc<dyn Agent>, cfg: Option<AgentToolConfig>) -> Arc<dyn Tool> {
        Arc::new(AgentTool {
            agent,
            skip_summarization: cfg.is_some_and(|c| c.skip_summarization),
        })
    }
}

/// The error of [`AgentTool`]'s run: "<message>: <source>", so a caller can
/// find the wrapped error (e.g. `AgentError::LlmCallsLimitExceeded`) in the source chain.
#[derive(Debug)]
struct AgentToolError {
    message: String,
    source: Option<Box<dyn std::error::Error + Send + Sync>>,
}

impl std::fmt::Display for AgentToolError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.source {
            Some(s) => write!(f, "{}: {}", self.message, s),
            None => f.write_str(&self.message),
        }
    }
}

impl std::error::Error for AgentToolError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source
            .as_deref()
            .map(|s| s as &(dyn std::error::Error + 'static))
    }
}

fn exec(message: String) -> ToolError {
    ToolError::Execution(Box::new(AgentToolError {
        message,
        source: None,
    }))
}

fn exec_src(
    message: String,
    source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> ToolError {
    ToolError::Execution(Box::new(AgentToolError {
        message,
        source: Some(source.into()),
    }))
}

impl Tool for AgentTool {
    fn name(&self) -> &str {
        self.agent.name()
    }

    fn description(&self) -> &str {
        self.agent.description()
    }

    /// The agent's function declaration (see `make_function_declaration`).
    fn declaration(&self) -> Option<FunctionDeclaration> {
        Some(make_function_declaration(&*self.agent))
    }

    // `process_request` is not overridden: its job is `pack_tool`, which the
    // flow runs via `gadk_agent::pack_tool` before calling `Tool::process_request`.

    fn as_runnable(&self) -> Option<&dyn RunnableTool> {
        Some(self)
    }

    /// With skip summarization the sub-agent's result is the
    /// final answer, so it is still shown to the user.
    fn display_result_on_skip_summarization(&self) -> bool {
        true
    }

    /// Classified as `SubAgent` (`A2A` when its agent is remote).
    fn origin_kind(&self) -> ToolOriginKind {
        ToolOriginKind::SubAgent
    }

    /// The wrapped agent.
    fn origin_agent(&self) -> Option<Arc<dyn Agent>> {
        Some(self.agent.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// The text of one part of the sub-agent's last content: non-empty text, else a non-empty code
/// execution output without trailing newlines, else executable code, in that order.
fn part_text(p: &gadk_genai::Part) -> String {
    if !p.text.is_empty() {
        p.text.clone()
    } else if let Some(r) = p
        .code_execution_result
        .as_ref()
        .filter(|r| !r.output.is_empty())
    {
        r.output.trim_end_matches('\n').to_string()
    } else if let Some(c) = &p.executable_code {
        c.code.clone()
    } else {
        String::new()
    }
}

#[async_trait]
impl RunnableTool for AgentTool {
    /// Runs the wrapped agent in a child runner and returns its final answer under `result`.
    async fn run(&self, ctx: Context, args: Value) -> Result<Map<String, Value>, ToolError> {
        let name = self.agent.name().to_string();
        let margs = match args {
            Value::Object(m) => m,
            other => {
                return Err(ToolError::InvalidArgument(format!(
                    "agent tool expects a JSON object of arguments, found {}",
                    json_kind_article(&other)
                )));
            }
        };

        // The same lookup as the declaration (`make_function_declaration`), so a
        // workflow agent takes the arguments of its first sub-agent's input schema. The output
        // schema is an LLM agent's own only. Cloned so no borrow of the agent state is held
        // across an await.
        let input_schema = input_schema_of(&*self.agent);
        let output_schema = reveal(&*self.agent).and_then(|s| s.cfg.output_schema.clone());

        let content = match &input_schema {
            Some(schema) => {
                validate_map_on_schema(&margs, schema, true).map_err(|e| {
                    exec(format!("argument validation failed for agent {name}: {e}"))
                })?;
                // Compact JSON with sorted keys and HTML escaping ([`go_json_marshal`]); it cannot
                // fail on a JSON map, so there is no "error serializing tool arguments" branch.
                let json = go_json_marshal(&Value::Object(margs));
                Content::from_text(json, ROLE_USER)
            }
            None => {
                let input = margs.get("request").ok_or_else(|| {
                    exec(format!(
                        "missing required argument 'request' for agent {name}"
                    ))
                })?;
                // The string as is, else the display form of the JSON-decoded value.
                Content::from_text(go_sprint(&as_go_floats(input)), ROLE_USER)
            }
        };

        // Set only once the input is valid, so an argument error goes back to the parent model
        // instead of ending its turn.
        if self.skip_summarization {
            ctx.update_actions(|a| a.skip_summarization = true);
        }

        let session_service: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
        let runner = Runner::new(RunnerConfig {
            app_name: name.clone(),
            agent: self.agent.clone(),
            session_service: session_service.clone(),
            artifact_service: Some(new_forwarding_service(ctx.artifacts())),
            memory_service: Some(Arc::new(gadk_memory::InMemoryMemoryService::new())),
            plugin_config: PluginConfig::default(),
            auto_create_session: false,
            platform: ctx.platform().clone(),
            compaction: None,
        })
        .map_err(|e| exec_src(format!("failed to create runner for agent {name}"), e))?;

        // Filter out ADK-internal state.
        let state: Map<String, Value> = ctx
            .state()
            .all()
            .into_iter()
            .filter(|(k, _)| !k.starts_with("_adk"))
            .collect();
        let sub_session = session_service
            .create(CreateRequest {
                app_name: name.clone(),
                user_id: ctx.user_id().to_string(),
                session_id: None,
                state: Some(state),
            })
            .await
            .map_err(|e| exec_src(format!("failed to create session for sub-agent {name}"), e))?;

        // Forward the caller's already-resolved model-call budget;
        let max_llm_calls = ctx.llm_call_limiter().map_or(0, |l| l.max_llm_calls());
        let run_cfg = RunConfig {
            streaming_mode: StreamingMode::Sse,
            max_llm_calls,
            ..Default::default()
        };
        // Dropping the stream (on return) cancels the child invocation. The sub-runner starts
        // with a context marked to inherit this invocation's plugins
        // (`with_inherited_plugins`), which a plugin-less runner keeps.
        let mut events = runner.run_with_context(
            &ctx.as_invocation().with_inherited_plugins(),
            sub_session.user_id(),
            sub_session.id(),
            Some(content),
            run_cfg,
            RunOptions::default(),
        );
        let mut last_content: Option<Content> = None;
        // Long-running calls that are never answered before the run ends (e.g. a tool
        // confirmation) leave the sub-agent paused. Its session is discarded, so the pause can
        // never be resumed.
        let mut pending_ids: BTreeSet<String> = BTreeSet::new();
        loop {
            // The caller's context is the child's context: cancelling the caller cancels
            // the child.
            let next = tokio::select! {
                biased;
                _ = ctx.cancellation_token().cancelled() => {
                    return Err(exec_src(
                        format!("error during execution of sub-agent {name}"),
                        AgentError::Cancelled,
                    ));
                }
                n = events.next() => n,
            };
            let Some(item) = next else { break };
            let event = item
                .map_err(|e| exec_src(format!("error during execution of sub-agent {name}"), e))?;
            let (code, message) = (&event.response.error_code, &event.response.error_message);
            if !code.is_empty() || !message.is_empty() {
                return Err(exec(format!(
                    "error from sub-agent {} (code: {}, message: {})",
                    go_quote(&name),
                    go_quote(code),
                    go_quote(message)
                )));
            }
            // Partial chunks carry IDs the flow never acts on; a streamed call gets its final
            // ID only on the aggregated event.
            if !event.response.partial {
                pending_ids.extend(event.long_running_tool_ids.iter().cloned());
                if let Some(c) = &event.response.content {
                    for fr in c.parts.iter().filter_map(|p| p.function_response.as_ref()) {
                        pending_ids.remove(&fr.id);
                    }
                }
            }
            if let Some(c) = event.response.content {
                last_content = Some(c);
            }
        }

        if !pending_ids.is_empty() {
            // The parent model must see this error, so don't end its turn here, whatever set
            // skip_summarization on this call.
            ctx.update_actions(|a| a.skip_summarization = false);
            return Err(exec(format!(
                "sub-agent {} stopped waiting on unresolved long-running tool calls [{}], which AgentTool cannot resume",
                go_quote(&name),
                pending_ids.into_iter().collect::<Vec<_>>().join(" ")
            )));
        }

        // An empty reply is the result "" (also with an output schema: there is
        // nothing to validate).
        let empty_result =
            || Map::from_iter([("result".to_string(), Value::String(String::new()))]);
        let Some(last) = last_content else {
            return Ok(empty_result());
        };
        let output_text = last
            .parts
            .iter()
            .filter(|p| !p.thought)
            .map(part_text)
            .filter(|t| !t.is_empty())
            .collect::<Vec<_>>()
            .join("\n");
        if output_text.is_empty() {
            return Ok(empty_result());
        }
        if let Some(schema) = &output_schema {
            return validate_output_schema(&output_text, schema).map_err(|e| {
                exec(format!(
                    "output validation failed for sub-agent {name}: {e}"
                ))
            });
        }
        let mut out = Map::new();
        out.insert("result".to_string(), Value::String(output_text));
        Ok(out)
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;
    use crate::llm::{LlmAgentConfig, agent as llm_agent};
    use crate::testutil::MockModel;

    /// The child's user message for a non-string `request` is Go `fmt.Sprint` of the JSON-decoded
    /// (float64) value; expected texts checked against Go.
    #[tokio::test]
    async fn non_string_request_is_go_sprint_of_float64() {
        let cases = [
            (json!(1000000), "1e+06"),
            (json!({"n": 5}), "map[n:5]"),
            (json!(2.5), "2.5"),
            (json!([1, 1e21, true, null]), "[1 1e+21 true <nil>]"),
        ];
        for (request, want) in cases {
            let model = MockModel::from_contents(vec![Content::from_text("ok", "model")]);
            let agent = llm_agent::new(LlmAgentConfig {
                name: "child".into(),
                model: Some(model.clone()),
                ..Default::default()
            })
            .unwrap();
            let sess = InMemorySessionService::new()
                .create(CreateRequest {
                    app_name: "app".into(),
                    user_id: "user".into(),
                    ..Default::default()
                })
                .await
                .unwrap();
            let ctx = Context::builder().session(sess).build();
            AgentTool::new(agent, None)
                .as_runnable()
                .unwrap()
                .run(ctx, json!({ "request": request }))
                .await
                .unwrap();
            let reqs = model.requests();
            let user = reqs[0]
                .contents
                .iter()
                .find(|c| c.role == ROLE_USER)
                .expect("child user message");
            assert_eq!(user.parts[0].text, want, "request {request}");
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod origin_tests {
    use super::*;
    use crate::llm::{LlmAgentConfig, agent as llm_agent};

    #[test]
    fn agent_tool_is_a_sub_agent_tool_of_its_agent() {
        let agent = llm_agent::new(LlmAgentConfig {
            name: "child".into(),
            ..Default::default()
        })
        .unwrap();
        let t = AgentTool::new(agent.clone(), None);
        assert_eq!(t.origin_kind(), ToolOriginKind::SubAgent);
        assert!(Arc::ptr_eq(&t.origin_agent().unwrap(), &agent));
        let agents = t.origin_agents();
        assert_eq!(agents.len(), 1);
        assert!(Arc::ptr_eq(&agents[0], &agent));
        assert!(t.unwrap().is_none());
    }
}