Skip to main content

gadk_agent/
mode.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//! Agent modes, hoisted so `ContextDelta` can name them, and their per-invocation binding, plus
17//! the bound output schema, which rides the same per-invocation map.
18
19use std::collections::HashMap;
20use std::sync::Arc;
21
22use gadk_genai::Schema;
23
24/// The mode an agent runs in.
25#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
26pub enum Mode {
27    /// No mode set (`""`).
28    #[default]
29    Unset,
30    /// `"chat"`.
31    Chat,
32    /// `"task"`.
33    Task,
34    /// `"single_turn"`.
35    SingleTurn,
36}
37
38impl Mode {
39    /// The mode's string value.
40    pub fn as_str(&self) -> &'static str {
41        match self {
42            Mode::Unset => "",
43            Mode::Chat => "chat",
44            Mode::Task => "task",
45            Mode::SingleTurn => "single_turn",
46        }
47    }
48}
49
50/// Immutable (copy-on-write) map (agent name, `AgentState::key()`) → [`Mode`]. A second map under
51/// the same key holds the output schema a typed workflow node binds for the agent.
52#[derive(Debug, Clone, Default)]
53pub struct BoundModes {
54    map: Arc<HashMap<(String, usize), Mode>>,
55    output_schemas: Arc<HashMap<(String, usize), Option<Schema>>>,
56}
57
58impl BoundModes {
59    /// A copy with `mode` bound to the agent; [`Mode::Unset`] leaves the map
60    /// unchanged.
61    pub fn with(&self, agent_name: &str, state_key: usize, mode: Mode) -> Self {
62        if mode == Mode::Unset {
63            return self.clone();
64        }
65        let mut map = (*self.map).clone();
66        map.insert((agent_name.to_string(), state_key), mode);
67        Self {
68            map: Arc::new(map),
69            output_schemas: Arc::clone(&self.output_schemas),
70        }
71    }
72
73    /// The mode bound to the agent, if any.
74    pub fn get(&self, agent_name: &str, state_key: usize) -> Option<Mode> {
75        self.map.get(&(agent_name.to_string(), state_key)).copied()
76    }
77
78    /// A copy in which the agent is asked to answer with
79    /// `schema`. `None` is bound too, so an inner placement shadows an outer one.
80    pub fn with_output_schema(
81        &self,
82        agent_name: &str,
83        state_key: usize,
84        schema: Option<Schema>,
85    ) -> Self {
86        let mut output_schemas = (*self.output_schemas).clone();
87        output_schemas.insert((agent_name.to_string(), state_key), schema);
88        Self {
89            map: Arc::clone(&self.map),
90            output_schemas: Arc::new(output_schemas),
91        }
92    }
93
94    /// The output schema bound to the agent: `None` when nothing is bound, `Some(None)` when a
95    /// placement bound no schema (shadowing any outer binding).
96    pub fn output_schema(&self, agent_name: &str, state_key: usize) -> Option<Option<&Schema>> {
97        self.output_schemas
98            .get(&(agent_name.to_string(), state_key))
99            .map(Option::as_ref)
100    }
101}
102
103#[cfg(test)]
104#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
105mod tests {
106    use super::*;
107
108    fn object(prop: &str) -> Schema {
109        serde_json::from_value(serde_json::json!({
110            "type": "OBJECT",
111            "properties": {prop: {"type": "STRING"}}
112        }))
113        .unwrap()
114    }
115
116    #[test]
117    fn output_schema_binding_is_keyed_shadowable_and_kept_by_mode_bindings() {
118        let outer = BoundModes::default().with_output_schema("a", 1, Some(object("x")));
119        assert_eq!(outer.output_schema("a", 1), Some(Some(&object("x"))));
120        assert_eq!(outer.output_schema("a", 2), None, "another state");
121        assert_eq!(outer.output_schema("b", 1), None, "another name");
122        // A mode binding keeps the schema binding and vice versa.
123        let both = outer.with("a", 1, Mode::SingleTurn);
124        assert_eq!(both.output_schema("a", 1), Some(Some(&object("x"))));
125        assert_eq!(both.get("a", 1), Some(Mode::SingleTurn));
126        // A None binding shadows the outer one; the outer copy is unchanged.
127        let inner = both.with_output_schema("a", 1, None);
128        assert_eq!(inner.output_schema("a", 1), Some(None));
129        assert_eq!(inner.get("a", 1), Some(Mode::SingleTurn));
130        assert_eq!(both.output_schema("a", 1), Some(Some(&object("x"))));
131    }
132}