gadk_engine/llm/config.rs
1// Copyright 2026 Chris Olson
2// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! `LlmAgentConfig`, `InstructionProvider` and `IncludeContents`, plus the re-exported `Mode`.
17
18use std::sync::Arc;
19
20use futures::future::BoxFuture;
21use gadk_agent::{
22 AfterAgentCallback, AfterModelCallback, AfterToolCallback, Agent, AgentError,
23 BeforeAgentCallback, BeforeModelCallback, BeforeToolCallback, OnModelErrorCallback,
24 OnToolErrorCallback, ReadonlyContext, Tool, Toolset,
25};
26use gadk_genai::{GenerateContentConfig, Schema};
27use gadk_model::Llm;
28
29pub use gadk_agent::Mode;
30
31/// Builds an instruction dynamically from the agent context.
32pub type InstructionProvider =
33 Arc<dyn Fn(ReadonlyContext) -> BoxFuture<'static, Result<String, AgentError>> + Send + Sync>;
34
35/// What parts of prior conversation history the agent receives (`""` / `"default"` / `"none"`
36/// as strings; see [`as_str`](Self::as_str)).
37///
38/// `Unset` acts as `Default` except under a single_turn placement, where history is hidden
39/// (the contents processor hides history whenever the value is not `Default`). An explicit
40/// `Default` keeps history.
41#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
42pub enum IncludeContents {
43 /// Not set (the empty string).
44 #[default]
45 Unset,
46 /// The agent receives the relevant conversation history, even when placed as a single_turn
47 /// workflow node.
48 Default,
49 /// The agent operates solely on its current turn: latest user input plus any following agent
50 /// events.
51 None,
52}
53
54impl IncludeContents {
55 /// The string form: `""`, `"default"` or `"none"`.
56 pub fn as_str(&self) -> &'static str {
57 match self {
58 Self::Unset => "",
59 Self::Default => "default",
60 Self::None => "none",
61 }
62 }
63}
64
65/// The configuration of an LLM agent.
66#[derive(Clone, Default)]
67pub struct LlmAgentConfig {
68 /// Name must be a non-empty string, unique within the agent tree; it cannot be "user", since
69 /// that is reserved for end-user's input.
70 pub name: String,
71 /// Description of the agent's capability; the LLM uses it to determine whether to delegate
72 /// control to the agent.
73 pub description: String,
74 /// The child agents that this agent can delegate tasks to.
75 pub sub_agents: Vec<Arc<dyn Agent>>,
76 /// Called sequentially before the agent starts its run.
77 pub before_agent_callbacks: Vec<BeforeAgentCallback>,
78 /// Called sequentially after the agent completes its run.
79 pub after_agent_callbacks: Vec<AfterAgentCallback>,
80 /// Additional content generation configuration (tools must be configured via `tools`).
81 pub generate_content_config: Option<GenerateContentConfig>,
82 /// Called in order until one returns a response or error, which then replaces the actual LLM
83 /// call.
84 pub before_model_callbacks: Vec<BeforeModelCallback>,
85 /// Model that is used by the agent.
86 pub model: Option<Arc<dyn Llm>>,
87 /// Called in order until one returns a response or error, which then replaces the actual LLM
88 /// response.
89 pub after_model_callbacks: Vec<AfterModelCallback>,
90 /// Called when the model returns an error.
91 pub on_model_error_callbacks: Vec<OnModelErrorCallback>,
92 /// Instruction for the LLM model guiding the agent's behavior; a template with `{key_name}`
93 /// placeholders resolved from session state and artifacts.
94 pub instruction: String,
95 /// Creates instructions dynamically based on the agent context; takes over `instruction` if both
96 /// are set.
97 pub instruction_provider: Option<InstructionProvider>,
98 /// Instruction for all agents in the entire agent tree; only the root agent's takes effect.
99 pub global_instruction: String,
100 /// Creates global instructions dynamically based on the agent context; takes over
101 /// `global_instruction` if both are set.
102 pub global_instruction_provider: Option<InstructionProvider>,
103 /// Prevents transferring to the parent agent if the LLM decides to.
104 pub disallow_transfer_to_parent: bool,
105 /// Prevents transferring to peer agents.
106 pub disallow_transfer_to_peers: bool,
107 /// Whether to include contents (conversation history) in the model request.
108 pub include_contents: IncludeContents,
109 /// The input schema when the agent is used as a tool.
110 pub input_schema: Option<Schema>,
111 /// The output schema when the agent replies.
112 pub output_schema: Option<Schema>,
113 /// Executed in order; if one returns a result or error, the remaining callbacks and the actual
114 /// tool call are skipped.
115 pub before_tool_callbacks: Vec<BeforeToolCallback>,
116 /// Tools available to the agent.
117 pub tools: Vec<Arc<dyn Tool>>,
118 /// Executed in order; if one returns a result or error, the list stops and that is returned
119 /// instead.
120 pub after_tool_callbacks: Vec<AfterToolCallback>,
121 /// Toolsets the agent extracts tools from to pass to the underlying LLM.
122 pub toolsets: Vec<Arc<dyn Toolset>>,
123 /// Called when a tool returns an error.
124 pub on_tool_error_callbacks: Vec<OnToolErrorCallback>,
125 /// Optional key in session state for the agent output.
126 pub output_key: String,
127 /// The delegation mode for this agent; defaults to chat as a sub-agent and single_turn as a
128 /// workflow node.
129 pub mode: Mode,
130}