gadk-engine 0.5.25

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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
// 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.

//! Port of Go workflow/agent_node_output_schema_test.go.
//! External package `workflow_test`, mapped to an internal target because it reads
//! `llminternal.Reveal` (Go: be529e7b, #2194 — a typed node passes its output schema to the model).

use std::any::Any;
use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;

use futures::StreamExt;
use futures::stream::BoxStream;
use gadk_agent::{
    Agent, BeforeModelCallback, Context, ContextDelta, Mode, RunConfig, Tool, ToolError,
};
use gadk_genai::{Content, FunctionCall, GenerateContentConfig, Part, Schema, Type};
use gadk_model::{Backend, Llm, LlmRequest, LlmResponse, ModelError};
use gadk_session::{CreateRequest, InMemorySessionService, SessionService};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};

use crate::llm::mode::with_bound_mode;
use crate::llm::output_schema_binding::with_bound_output_schema;
use crate::llm::state::reveal;
use crate::llm::{LlmAgentConfig, agent as llm_agent};
use crate::tool::utils::infer_schema;
use crate::tool::{FunctionTool, FunctionToolConfig};
use crate::workflow::{AgentNode, Node, NodeConfig};

#[derive(Serialize, Deserialize, schemars::JsonSchema)]
struct WeatherOut {
    city: String,
    temp: i64,
}

fn prop(t: Type) -> Schema {
    Schema {
        r#type: Some(t),
        ..Schema::default()
    }
}

/// Go `weatherSchema`.
fn weather_schema() -> Schema {
    Schema {
        r#type: Some(Type::Object),
        properties: BTreeMap::from([
            ("city".to_string(), prop(Type::String)),
            ("temp".to_string(), prop(Type::Integer)),
        ]),
        required: vec!["city".into(), "temp".into()],
        ..Schema::default()
    }
}

/// Port of Go `requestRecorder`: records every request; on a Gemini API model offered
/// set_model_response, the first call answers with that tool.
#[derive(Default)]
struct RequestRecorder {
    name: String,
    backend: Option<Backend>,
    reqs: Mutex<Vec<LlmRequest>>,
    /// Text answer; defaults to a WeatherOut value.
    reply: String,
    set_reply: Option<Map<String, Value>>,
    /// Makes the set_model_response call carry no args at all.
    set_no_args: bool,
}

impl RequestRecorder {
    fn named(name: &str) -> Arc<Self> {
        Arc::new(Self {
            name: name.into(),
            ..Self::default()
        })
    }
    /// Go `&requestRecorder{name: "recorder"}`.
    fn plain() -> Self {
        Self {
            name: "recorder".into(),
            ..Self::default()
        }
    }
    fn gemini() -> Self {
        Self {
            name: "gemini-2.5-flash".into(),
            backend: Some(Backend::GeminiApi),
            ..Self::default()
        }
    }
    fn reqs(&self) -> Vec<LlmRequest> {
        self.reqs.lock().clone()
    }
    /// Go `firstRequest`.
    fn first_request(&self) -> LlmRequest {
        self.reqs
            .lock()
            .first()
            .cloned()
            .expect("the model was never called")
    }
}

impl Llm for RequestRecorder {
    fn name(&self) -> &str {
        &self.name
    }
    fn generate_content(
        &self,
        req: LlmRequest,
        _stream: bool,
    ) -> BoxStream<'static, Result<LlmResponse, ModelError>> {
        let offers = offers_tool(&req, "set_model_response");
        let n = {
            let mut reqs = self.reqs.lock();
            reqs.push(req);
            reqs.len()
        };
        let text = if self.reply.is_empty() {
            r#"{"city":"Paris","temp":20}"#.to_string()
        } else {
            self.reply.clone()
        };
        let mut part = Part::from_text(text);
        if (self.set_reply.is_some() || self.set_no_args) && n == 1 && offers {
            part = Part {
                function_call: Some(FunctionCall {
                    name: "set_model_response".into(),
                    args: self.set_reply.clone().unwrap_or_default(),
                    ..FunctionCall::default()
                }),
                ..Part::default()
            };
        }
        let resp = LlmResponse {
            content: Some(Content {
                role: "model".into(),
                parts: vec![part],
            }),
            ..Default::default()
        };
        futures::stream::iter([Ok(resp)]).boxed()
    }
    fn google_variant(&self) -> Option<Backend> {
        self.backend
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Go `offersTool`.
fn offers_tool(req: &LlmRequest, name: &str) -> bool {
    tool_decl(req, name).is_some()
}

/// Go `toolDecl`: the function declaration `name` in the request config, as JSON.
fn tool_decl(req: &LlmRequest, name: &str) -> Option<gadk_genai::FunctionDeclaration> {
    req.config
        .as_ref()?
        .tools
        .iter()
        .flat_map(|t| t.function_declarations.iter())
        .find(|d| d.name == name)
        .cloned()
}

#[derive(Deserialize, schemars::JsonSchema)]
struct LookupArgs {
    q: String,
}

/// Go `lookupTool`.
fn lookup_tool() -> Arc<dyn Tool> {
    FunctionTool::new(
        FunctionToolConfig::new("lookup", "looks a city up"),
        |_c: Context, input: LookupArgs| async move {
            Ok::<_, ToolError>(HashMap::from([("q".to_string(), input.q)]))
        },
    )
    .expect("functiontool.New")
}

fn cfg_of(req: &LlmRequest) -> GenerateContentConfig {
    req.config.clone().unwrap_or_default()
}

/// Port of Go `newRunnableNodeContext`: in-memory session, agent, invocation "inv-test",
/// StreamingMode none.
async fn new_runnable_node_context(a: &Arc<dyn Agent>) -> Context {
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    let session = svc
        .create(CreateRequest {
            app_name: "app".into(),
            user_id: "u".into(),
            ..Default::default()
        })
        .await
        .expect("create session");
    Context::builder()
        .session_service(svc)
        .session(session)
        .agent(Arc::clone(a))
        .invocation_id("inv-test")
        .run_config(RunConfig::default())
        .build()
}

/// Go `runNode`: runs `node` once and returns the last output it emitted.
async fn run_node(node: &Arc<AgentNode>, a: &Arc<dyn Agent>) -> Option<Value> {
    let mut s = node.run(
        new_runnable_node_context(a).await,
        json!("weather in Paris?"),
    );
    let mut out = None;
    while let Some(item) = s.next().await {
        let ev = item.unwrap_or_else(|e| panic!("Run: {e}"));
        if ev.output.is_some() {
            out = ev.output;
        }
    }
    out
}

fn new_agent(cfg: LlmAgentConfig) -> Arc<dyn Agent> {
    llm_agent::new(cfg).expect("llmagent.New")
}

/// Go `NewAgentNodeTyped[string, T]`.
fn typed_node<T: schemars::JsonSchema>(a: &Arc<dyn Agent>) -> Arc<AgentNode> {
    AgentNode::with_schemas(
        Arc::clone(a),
        Some(infer_schema::<String>()),
        Some(infer_schema::<T>()),
        NodeConfig::default(),
    )
    .expect("NewAgentNodeTyped")
}

type BuildNode = Box<dyn Fn(&Arc<dyn Agent>) -> Arc<AgentNode>>;

/// Go `untypedAndTypedRequests`: one fresh agent built from `cfg` under an untyped node, another
/// under `build`; the first request each sent.
async fn untyped_and_typed_requests(
    cfg: impl Fn() -> LlmAgentConfig,
    new_llm: impl Fn() -> Arc<RequestRecorder>,
    build: &BuildNode,
) -> (LlmRequest, LlmRequest) {
    let mut out = Vec::new();
    for typed in [false, true] {
        let llm = new_llm();
        let a = new_agent(LlmAgentConfig {
            model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
            ..cfg()
        });
        let node = if typed {
            build(&a)
        } else {
            AgentNode::new(Arc::clone(&a), NodeConfig::default()).expect("NewAgentNode")
        };
        run_node(&node, &a).await;
        out.push(llm.first_request());
    }
    let typed = out.pop().expect("typed");
    (out.pop().expect("untyped"), typed)
}

// parity: adapted: Go's generic NewAgentNodeTyped[string, weatherOut] → AgentNode::with_schemas(inferred).
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_typed_object_output_sets_response_schema() {
    let llm = RequestRecorder::named("recorder");
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        ..Default::default()
    });
    let node = typed_node::<WeatherOut>(&a);
    run_node(&node, &a).await;
    let cfg = cfg_of(&llm.first_request());
    assert_eq!(
        cfg.response_schema,
        Some(weather_schema()),
        "ResponseSchema mismatch"
    );
    assert_eq!(cfg.response_mime_type, "application/json");
}

// A Gemini API model cannot combine tools with a response schema, so the schema goes out as
// set_model_response and the node returns its arguments.
// parity: adapted: the reply passes through Go's JSON text ({"temp":31}), which serde_json reads back as the integer 31 where Go reads float64(31); the expected output uses 31
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_typed_object_output_with_tools_uses_set_model_response() {
    let llm = Arc::new(RequestRecorder {
        // Differs from the default text reply, so the output can only come from the
        // set_model_response call.
        set_reply: Some(
            json!({"city": "Paris", "temp": 31.0})
                .as_object()
                .cloned()
                .expect("object"),
        ),
        ..RequestRecorder::gemini()
    });
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        tools: vec![lookup_tool()],
        ..Default::default()
    });
    let node = typed_node::<WeatherOut>(&a);
    let out = run_node(&node, &a).await;

    let first = llm.first_request();
    let decl = tool_decl(&first, "set_model_response")
        .expect("set_model_response was not offered to the model");
    assert_eq!(
        decl.parameters_json_schema,
        Some(serde_json::to_value(weather_schema()).expect("schema")),
        "set_model_response parameters mismatch"
    );
    assert_eq!(
        cfg_of(&first).response_schema,
        None,
        "ResponseSchema alongside tools, want nil"
    );
    let got = node
        .validate_output(out.clone().unwrap_or(Value::Null))
        .unwrap_or_else(|e| panic!("ValidateOutput({out:?}): {e}"));
    assert_eq!(
        got,
        json!({"city": "Paris", "temp": 31}),
        "node output mismatch"
    );
    // The set_model_response result ends the turn: no second model call.
    assert_eq!(llm.reqs().len(), 1, "model calls, want 1");
}

// An OutputSchema the agent declares is what the model sees, whatever the node's Output type.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_declared_output_schema_wins() {
    let declared = Schema {
        r#type: Some(Type::Object),
        properties: BTreeMap::from([("answer".to_string(), prop(Type::String))]),
        ..Schema::default()
    };
    let llm = Arc::new(RequestRecorder {
        reply: r#"{"answer":"sunny"}"#.into(),
        ..RequestRecorder::plain()
    });
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        output_schema: Some(declared.clone()),
        ..Default::default()
    });
    let node = typed_node::<WeatherOut>(&a);
    run_node(&node, &a).await;
    assert_eq!(
        cfg_of(&llm.first_request()).response_schema,
        Some(declared),
        "ResponseSchema mismatch"
    );
}

#[derive(Serialize, Deserialize, schemars::JsonSchema)]
struct CityOnly {
    city: String,
}

// One agent can back several nodes. Each node's schema reaches only its own runs, and the agent's
// declared OutputSchema is never written.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_one_agent_two_typed_nodes() {
    let llm = RequestRecorder::named("recorder");
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        ..Default::default()
    });
    let weather_node = typed_node::<WeatherOut>(&a);
    let city_node = typed_node::<CityOnly>(&a);
    let city_schema = Schema {
        r#type: Some(Type::Object),
        properties: BTreeMap::from([("city".to_string(), prop(Type::String))]),
        required: vec!["city".into()],
        ..Schema::default()
    };
    run_node(&weather_node, &a).await;
    run_node(&city_node, &a).await;
    run_node(&weather_node, &a).await;
    let reqs = llm.reqs();
    assert_eq!(reqs.len(), 3, "model requests, want 3");
    for (i, want) in [weather_schema(), city_schema, weather_schema()]
        .into_iter()
        .enumerate()
    {
        assert_eq!(
            cfg_of(&reqs[i]).response_schema,
            Some(want),
            "run {i} ResponseSchema mismatch"
        );
    }
    assert_eq!(
        reveal(a.as_ref()).expect("llm agent").cfg.output_schema,
        None,
        "agent's declared OutputSchema after the runs, want nil"
    );
}

// Chat and task agents talk to the user between replies, so the node's shape is not forced on
// them: neither a response schema nor set_model_response.
#[rstest::rstest]
#[case::task_no_tools(Mode::Task, false)]
#[case::task_gemini_api_with_tools(Mode::Task, true)]
#[case::chat_no_tools(Mode::Chat, false)]
#[case::chat_gemini_api_with_tools(Mode::Chat, true)]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_chat_and_task_agents_get_no_output_schema(
    #[case] mode: Mode,
    #[case] gemini_with_tools: bool,
) {
    let llm = Arc::new(if gemini_with_tools {
        RequestRecorder::gemini()
    } else {
        RequestRecorder::plain()
    });
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        mode,
        tools: if gemini_with_tools {
            vec![lookup_tool()]
        } else {
            Vec::new()
        },
        ..Default::default()
    });
    let node = typed_node::<WeatherOut>(&a);
    run_node(&node, &a).await;
    let req = llm.first_request();
    let cfg = cfg_of(&req);
    assert!(
        cfg.response_schema.is_none() && cfg.response_mime_type.is_empty(),
        "{mode:?} agent request has ResponseSchema {:?}, ResponseMIMEType {:?}; want neither",
        cfg.response_schema,
        cfg.response_mime_type
    );
    assert!(
        !offers_tool(&req, "set_model_response"),
        "{mode:?} agent was offered set_model_response"
    );
}

#[derive(Serialize, Deserialize, schemars::JsonSchema)]
struct WithAnyField {
    summary: String,
    details: Value,
}

#[derive(Serialize, Deserialize, schemars::JsonSchema)]
struct WithMapField {
    counts: HashMap<String, i64>,
}

#[derive(Serialize, Deserialize, schemars::JsonSchema)]
struct Empty {}

fn schema_node(a: &Arc<dyn Agent>, output: Value) -> Arc<AgentNode> {
    AgentNode::with_schemas(Arc::clone(a), None, Some(output), NodeConfig::default())
        .expect("NewAgentNodeWithSchemas")
}

/// The node constructors of Go's TestAgentNode_OutputSchemaLeavesRequestUnchanged.
fn leaves_unchanged_node(name: &str) -> BuildNode {
    match name {
        "string" => Box::new(typed_node::<String>),
        "int" => Box::new(typed_node::<i64>),
        "slice_of_objects" => Box::new(typed_node::<Vec<WeatherOut>>),
        "map" => Box::new(typed_node::<HashMap<String, i64>>),
        "empty_struct" => Box::new(typed_node::<Empty>),
        "struct_with_an_any_field" => Box::new(typed_node::<WithAnyField>),
        "struct_with_a_map_field" => Box::new(typed_node::<WithMapField>),
        "explicit_schema_with_an_integer_enum" => Box::new(|a| {
            schema_node(
                a,
                json!({
                    "type": "object",
                    "properties": {"level": {"type": "integer", "enum": [1, 2, 3]}},
                    "additionalProperties": false,
                }),
            )
        }),
        // No additionalProperties: the node accepts extra keys, which set_model_response would
        // reject.
        "explicit_open_object_schema" => Box::new(|a| {
            schema_node(
                a,
                json!({"type": "object", "properties": {"city": {"type": "string"}}}),
            )
        }),
        "pointer_to_struct" => Box::new(typed_node::<Option<WeatherOut>>),
        other => panic!("unknown node {other}"),
    }
}

// Outputs that are not a closed, non-null object with properties, or that genai.Schema cannot
// describe, must construct and send exactly the request an untyped node sends, with and without
// tools.
// parity: adapted: Go `*weatherOut` (pointer to struct) → Option<WeatherOut>; Go `any` field → serde_json::Value.
#[rstest::rstest]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_output_schema_leaves_request_unchanged(
    #[values(false, true)] gemini_with_tools: bool,
    #[values(
        "string",
        "int",
        "slice_of_objects",
        "map",
        "empty_struct",
        "struct_with_an_any_field",
        "struct_with_a_map_field",
        "explicit_schema_with_an_integer_enum",
        "explicit_open_object_schema",
        "pointer_to_struct"
    )]
    node_name: &str,
) {
    let build = leaves_unchanged_node(node_name);
    let (untyped, typed) = untyped_and_typed_requests(
        || LlmAgentConfig {
            name: "weather".into(),
            tools: if gemini_with_tools {
                vec![lookup_tool()]
            } else {
                Vec::new()
            },
            ..Default::default()
        },
        || {
            Arc::new(if gemini_with_tools {
                RequestRecorder::gemini()
            } else {
                RequestRecorder::plain()
            })
        },
        &build,
    )
    .await;
    assert_eq!(
        untyped.config, typed.config,
        "{node_name}: request config differs from an untyped node's"
    );
}

/// The agent setups of Go's TestAgentNode_AgentSetupKeepsItsRequest.
fn setup_case(name: &str, sub: &Arc<dyn Agent>) -> (LlmAgentConfig, RequestRecorder) {
    let declared = Schema {
        r#type: Some(Type::Object),
        properties: BTreeMap::from([("answer".to_string(), prop(Type::String))]),
        ..Schema::default()
    };
    let recorder = |reply: &str| RequestRecorder {
        name: "recorder".into(),
        reply: reply.into(),
        ..RequestRecorder::default()
    };
    match name {
        "response_schema" => (
            LlmAgentConfig {
                generate_content_config: Some(GenerateContentConfig {
                    response_schema: Some(declared),
                    ..GenerateContentConfig::default()
                }),
                ..Default::default()
            },
            recorder(r#"{"answer":"sunny"}"#),
        ),
        "response_json_schema" => (
            LlmAgentConfig {
                generate_content_config: Some(GenerateContentConfig {
                    response_json_schema: Some(
                        json!({"type": "object", "properties": {"answer": {"type": "string"}}}),
                    ),
                    ..GenerateContentConfig::default()
                }),
                ..Default::default()
            },
            recorder(r#"{"answer":"sunny"}"#),
        ),
        "response_mime_type" => (
            LlmAgentConfig {
                generate_content_config: Some(GenerateContentConfig {
                    response_mime_type: "text/plain".into(),
                    ..GenerateContentConfig::default()
                }),
                ..Default::default()
            },
            recorder("sunny"),
        ),
        "sub_agent_on_a_gemini_api_model" => (
            LlmAgentConfig {
                sub_agents: vec![Arc::clone(sub)],
                ..Default::default()
            },
            RequestRecorder::gemini(),
        ),
        other => panic!("unknown case {other}"),
    }
}

// An agent whose own setup makes a response schema unsafe to add sends the same request under a
// typed node as under an untyped one.
#[rstest::rstest]
#[case::generate_content_config_response_schema("response_schema")]
#[case::generate_content_config_response_json_schema("response_json_schema")]
#[case::generate_content_config_response_mime_type("response_mime_type")]
#[case::sub_agent_on_a_gemini_api_model("sub_agent_on_a_gemini_api_model")]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_agent_setup_keeps_its_request(#[case] name: &str) {
    let sub = new_agent(LlmAgentConfig {
        name: "helper".into(),
        model: Some(RequestRecorder::named("recorder") as Arc<dyn Llm>),
        ..Default::default()
    });
    let build: BuildNode = Box::new(typed_node::<WeatherOut>);
    let (untyped, typed) = untyped_and_typed_requests(
        || LlmAgentConfig {
            name: "weather".into(),
            ..setup_case(name, &sub).0
        },
        || Arc::new(setup_case(name, &sub).1),
        &build,
    )
    .await;
    assert_eq!(
        untyped.config, typed.config,
        "{name}: request config differs from an untyped node's"
    );
}

// A model callback that edits the request's schema changes that request only; the node's next run
// is sent the node's schema unchanged.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_callback_edit_does_not_reach_next_run() {
    let llm = RequestRecorder::named("recorder");
    let edited = Arc::new(Mutex::new(false));
    let cb: BeforeModelCallback = {
        let edited = Arc::clone(&edited);
        Arc::new(move |_ctx: Context, mut req: LlmRequest| {
            let mut done = edited.lock();
            if !*done && let Some(s) = req.config.as_mut().and_then(|c| c.response_schema.as_mut())
            {
                *done = true;
                s.properties.insert("injected".into(), prop(Type::String));
            }
            Box::pin(async move { (req, Ok(None)) })
        })
    };
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        before_model_callbacks: vec![cb],
        ..Default::default()
    });
    let node = typed_node::<WeatherOut>(&a);
    run_node(&node, &a).await;
    run_node(&node, &a).await;
    let reqs = llm.reqs();
    assert_eq!(reqs.len(), 2, "model calls, want 2");
    assert_eq!(
        cfg_of(&reqs[1]).response_schema,
        Some(weather_schema()),
        "second run's ResponseSchema mismatch"
    );
}

/// Port of Go `statelessLLM`: the same text on every call, nothing recorded.
struct StatelessLlm;

impl Llm for StatelessLlm {
    fn name(&self) -> &str {
        "stateless"
    }
    fn generate_content(
        &self,
        _req: LlmRequest,
        _stream: bool,
    ) -> BoxStream<'static, Result<LlmResponse, ModelError>> {
        futures::stream::iter([Ok(LlmResponse {
            content: Some(Content::from_text(r#"{"city":"Paris","temp":20}"#, "model")),
            ..Default::default()
        })])
        .boxed()
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

// Concurrent runs of one node, as under a fan-out, each get their own schema.
// parity: adapted: a Rust callback owns its LlmRequest, so two runs cannot share one *genai.Schema; the port checks each run's callback saw the node's schema (Go's pointer-identity check has no counterpart).
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_concurrent_runs_do_not_share_schema() {
    let seen: Arc<Mutex<Vec<Option<Schema>>>> = Arc::default();
    let cb: BeforeModelCallback = {
        let seen = Arc::clone(&seen);
        Arc::new(move |_ctx: Context, mut req: LlmRequest| {
            let schema = req.config.as_ref().and_then(|c| c.response_schema.clone());
            seen.lock().push(schema);
            if let Some(s) = req.config.as_mut().and_then(|c| c.response_schema.as_mut()) {
                s.description = "set by callback".into();
            }
            Box::pin(async move { (req, Ok(None)) })
        })
    };
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::new(StatelessLlm) as Arc<dyn Llm>),
        before_model_callbacks: vec![cb],
        ..Default::default()
    });
    let node = typed_node::<WeatherOut>(&a);
    let mut runs = Vec::new();
    for _ in 0..2 {
        let ctx = new_runnable_node_context(&a).await;
        let node = Arc::clone(&node);
        runs.push(tokio::spawn(async move {
            let mut s = node.run(ctx, json!("weather in Paris?"));
            while let Some(item) = s.next().await {
                if let Err(e) = item {
                    panic!("Run: {e}");
                }
            }
        }));
    }
    for r in runs {
        r.await.expect("run task");
    }
    let seen = seen.lock().clone();
    assert!(
        seen.len() == 2 && seen.iter().all(|s| s.as_ref() == Some(&weather_schema())),
        "callback saw schemas {seen:?}, want two equal to the node's"
    );
}

// An agent that declares single_turn itself is treated like one that runs single_turn by default
// at a node.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_explicit_single_turn_agent_gets_output_schema() {
    let llm = RequestRecorder::named("recorder");
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        mode: Mode::SingleTurn,
        ..Default::default()
    });
    let node = typed_node::<WeatherOut>(&a);
    run_node(&node, &a).await;
    assert_eq!(
        cfg_of(&llm.first_request()).response_schema,
        Some(weather_schema()),
        "ResponseSchema mismatch"
    );
}

// An untyped node inside a placement that bound a schema clears it: the inner placement binds
// no schema, which shadows the outer binding.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_inner_untyped_placement_clears_outer_schema() {
    let llm = RequestRecorder::named("recorder");
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        ..Default::default()
    });
    let inner = AgentNode::new(Arc::clone(&a), NodeConfig::default()).expect("NewAgentNode");
    // Go nodeContextFrom(outer): the node context built on the outer placement's bindings.
    let base = new_runnable_node_context(&a).await;
    let outer = with_bound_mode(
        base.as_invocation(),
        a.name(),
        a.agent_state(),
        Mode::SingleTurn,
    );
    let outer = with_bound_output_schema(&outer, a.name(), a.agent_state(), Some(weather_schema()));
    let ctx = base.with_delta(ContextDelta {
        bound_modes: Some(outer.bound_modes().clone()),
        ..Default::default()
    });

    let mut s = inner.run(ctx, json!("weather in Paris?"));
    while let Some(item) = s.next().await {
        item.unwrap_or_else(|e| panic!("Run: {e}"));
    }

    assert_eq!(
        cfg_of(&llm.first_request()).response_schema,
        None,
        "ResponseSchema under an untyped inner placement, want nil"
    );
}

// A hand-written object schema that allows extra keys is not sent: the model's answer with an
// extra key reaches the node as it does on an untyped node.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_open_object_schema_keeps_model_answer() {
    let answer = json!({"city": "Paris", "country": "FR"});
    let llm = Arc::new(RequestRecorder {
        reply: r#"{"city":"Paris","country":"FR"}"#.into(),
        // Used only if set_model_response is offered.
        set_reply: answer.as_object().cloned(),
        ..RequestRecorder::gemini()
    });
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        tools: vec![lookup_tool()],
        ..Default::default()
    });
    let node = schema_node(
        &a,
        json!({"type": "object", "properties": {"city": {"type": "string"}}}),
    );
    let out = run_node(&node, &a).await;
    let got = node
        .validate_output(out.clone().unwrap_or(Value::Null))
        .unwrap_or_else(|e| panic!("ValidateOutput({out:?}): {e}"));
    assert_eq!(got, answer, "node output mismatch");
}

#[derive(Serialize, Deserialize, schemars::JsonSchema)]
struct CityWithNote {
    city: String,
    note: Option<String>,
}

// A pointer field goes out as nullable, so set_model_response accepts a null for it, and the null
// reaches the output instead of the tool's error.
// parity: adapted: Go `*string` field → Option<String>.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_null_field_through_set_model_response() {
    let answer = json!({"city": "Paris", "note": null});
    let llm = Arc::new(RequestRecorder {
        // Differs, so a pass needs the tool call.
        reply: r#"{"city":"Lyon","note":"text"}"#.into(),
        set_reply: answer.as_object().cloned(),
        ..RequestRecorder::gemini()
    });
    let a = new_agent(LlmAgentConfig {
        name: "weather".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        tools: vec![lookup_tool()],
        ..Default::default()
    });
    let node = typed_node::<CityWithNote>(&a);
    let out = run_node(&node, &a).await;
    assert!(
        offers_tool(&llm.first_request(), "set_model_response"),
        "set_model_response was not offered to the model"
    );
    let got = node
        .validate_output(out.clone().unwrap_or(Value::Null))
        .unwrap_or_else(|e| panic!("ValidateOutput({out:?}): {e}"));
    assert_eq!(got, answer, "node output mismatch");
}

// A set_model_response call without args is the empty object, so it passes a schema whose fields
// are all optional.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_agent_node_set_model_response_without_args() {
    let llm = Arc::new(RequestRecorder {
        // Differs, so a pass needs the tool call.
        reply: r#"{"note":"text"}"#.into(),
        set_no_args: true,
        ..RequestRecorder::gemini()
    });
    let a = new_agent(LlmAgentConfig {
        name: "a".into(),
        model: Some(Arc::clone(&llm) as Arc<dyn Llm>),
        tools: vec![lookup_tool()],
        output_schema: Some(Schema {
            r#type: Some(Type::Object),
            properties: BTreeMap::from([("note".to_string(), prop(Type::String))]),
            ..Schema::default()
        }),
        ..Default::default()
    });
    let node = AgentNode::new(Arc::clone(&a), NodeConfig::default()).expect("NewAgentNode");
    let out = run_node(&node, &a).await;
    assert!(
        offers_tool(&llm.first_request(), "set_model_response"),
        "set_model_response was not offered to the model"
    );
    assert_eq!(out, Some(json!({})), "node output mismatch");
}