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
// 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.
//! Parity port of Go internal/llminternal/foreign_function_response_test.go.
use gadk_genai::{Content, FunctionCall, FunctionResponse, Part, ROLE_MODEL, ROLE_USER};
use gadk_model::LlmResponse;
use gadk_session::Event;
use pretty_assertions::assert_eq;
use serde_json::json;
use crate::llm::processors::contents::{
build_contents_current_turn_context_only, build_contents_default,
build_contents_default_with_call_source,
};
fn event(author: &str, content: Content) -> Event {
Event {
author: author.into(),
response: LlmResponse {
content: Some(content),
..Default::default()
},
..Default::default()
}
}
fn on_branch(branch: &str, mut ev: Event) -> Event {
ev.branch = branch.into();
ev
}
/// The role is kept as given (`Content::from_parts` would turn an empty role into "user").
fn call_with_role(role: &str, id: &str) -> Content {
Content {
parts: vec![Part {
function_call: Some(FunctionCall {
id: id.into(),
name: "adk_request_input".into(),
args: json!({"message": "approve"}).as_object().cloned().unwrap(),
..Default::default()
}),
..Default::default()
}],
role: role.into(),
}
}
fn call(id: &str) -> Content {
call_with_role(ROLE_MODEL, id)
}
fn response_part(id: &str) -> Part {
Part {
function_response: Some(FunctionResponse {
id: id.into(),
name: "adk_request_input".into(),
response: json!({"result": "ok"}).as_object().cloned().unwrap(),
..Default::default()
}),
..Default::default()
}
}
fn response(id: &str) -> Content {
Content::from_parts(vec![response_part(id)], ROLE_USER)
}
/// A "For context:" turn with the given lines.
fn context(lines: &[&str]) -> Content {
let mut parts = vec![Part::from_text("For context:")];
parts.extend(lines.iter().map(|l| Part::from_text(*l)));
Content::from_parts(parts, ROLE_USER)
}
const CALLED_B: &str =
r#"[b] called tool `adk_request_input` with parameters: {"message":"approve"}"#;
const ANSWERED: &str = r#"[user] `adk_request_input` tool returned result: {"result":"ok"}"#;
fn start() -> Content {
Content::from_text("start", ROLE_USER)
}
/// A user's function response to a call another agent made reaches the current agent as
/// context, like the call itself, as in adk-python's `_get_contents`. Left as a raw response,
/// its call is missing from the history: it is dropped mid-history and fails the request when
/// it is the last event.
#[test]
fn test_build_contents_user_response_to_other_agent_call_is_context() {
let done = context(&["[d] said: done"]);
// (name, branch, events, want)
let cases: Vec<(&str, &str, Vec<Event>, Vec<Content>)> = vec![
(
"response is the last event",
"",
vec![
event("user", start()),
event("b", call("b1")),
event("user", response("b1")),
],
vec![start(), context(&[CALLED_B]), context(&[ANSWERED])],
),
(
"response mid-history",
"",
vec![
event("user", start()),
event("b", call("b1")),
event("user", response("b1")),
event("d", Content::from_text("done", ROLE_MODEL)),
],
vec![
start(),
context(&[CALLED_B]),
context(&[ANSWERED]),
done.clone(),
],
),
(
"response to the agent's own call stays a response",
"",
vec![
event("user", start()),
event("after", call("a1")),
event("user", response("a1")),
],
vec![start(), call("a1"), response("a1")],
),
(
// Some providers reuse call ids. A response pairs with the latest call before
// it, so a later call with the same id does not turn the agent's own exchange
// into context.
"call id reused by another agent later",
"",
vec![
event("user", start()),
event("after", call("c0")),
event("after", response("c0")),
event("b", call("c0")),
event("user", response("c0")),
],
vec![
start(),
call("c0"),
response("c0"),
context(&[CALLED_B]),
context(&[ANSWERED]),
],
),
(
"response to a call the user made stays a response",
"",
vec![
event("user", start()),
event("user", call("u1")),
event("user", response("u1")),
],
vec![start(), call("u1"), response("u1")],
),
(
// The call reaches the agent as text, so nothing is left for the response to
// pair with and history rearrangement drops it.
"response without an id is not matched",
"",
vec![
event("user", start()),
event("b", call("")),
event("user", response("")),
event("d", Content::from_text("done", ROLE_MODEL)),
],
vec![start(), context(&[CALLED_B]), done.clone()],
),
(
// The call is on a branch this agent does not see, so the response stays a raw
// response, and history rearrangement drops it because no call before it
// matches. adk-python drops it too, as an orphan.
"response to a call outside the agent's branch is not shown",
"root.after",
vec![
event("user", start()),
on_branch("root.wf", event("b", call("b1"))),
event("user", response("b1")),
event("d", Content::from_text("done", ROLE_MODEL)),
],
vec![start(), done.clone()],
),
];
for (name, branch, events, want) in cases {
let got = build_contents_default("after", branch, "", &events, false, None)
.unwrap_or_else(|e| panic!("{name}: {e}"));
assert_eq!(want, got, "{name}: contents");
}
}
/// Text sent in the same message as the answer to another agent's call arrives as context with
/// it, whether the agent gets its history or only the current turn.
#[test]
fn test_build_contents_user_text_with_response_to_other_agent_call() {
let answer = Content::from_parts(
vec![Part::from_text("note"), response_part("b1")],
ROLE_USER,
);
let events = vec![
event("user", start()),
event("b", call("b1")),
event("user", answer),
];
let called_b = context(&[CALLED_B]);
let answered = context(&["[user] said: note", ANSWERED]);
let history = build_contents_default("after", "", "", &events, false, None)
.unwrap_or_else(|e| panic!("history: {e}"));
assert_eq!(
vec![start(), called_b.clone(), answered.clone()],
history,
"history: contents"
);
let current = build_contents_current_turn_context_only("after", "", "", &events, false, None)
.unwrap_or_else(|e| panic!("current turn only: {e}"));
assert_eq!(
vec![called_b, answered],
current,
"current turn only: contents"
);
}
/// When only a suffix of the session is built, as the compaction token estimate does, a
/// response in the suffix pairs with the call before it the same way as when the whole session
/// is built.
#[test]
fn test_build_contents_response_in_suffix_to_other_agent_call_before_it() {
let ev =
|author: &str, branch: &str, content: Content| on_branch(branch, event(author, content));
let start_ev = ev("user", "", start());
let d = ev("d", "", Content::from_text("done", ROLE_MODEL));
let done = context(&["[d] said: done"]);
// (name, branch, events, from, want)
type SuffixCase = (&'static str, &'static str, Vec<Event>, usize, Vec<Content>);
let cases: Vec<SuffixCase> = vec![
(
"call visible to the agent",
"",
vec![
start_ev.clone(),
ev("b", "", call("b1")),
ev("user", "", response("b1")),
d.clone(),
],
2,
vec![context(&[ANSWERED]), done.clone()],
),
(
// As when the whole session is built, a call the agent does not see leaves its
// response unmatched, and it is dropped.
"call outside the agent's branch",
"root.after",
vec![
start_ev.clone(),
ev("b", "root.wf", call("b1")),
ev("user", "", response("b1")),
d.clone(),
],
2,
vec![done.clone()],
),
(
// The whole build skips an event whose content has no role, so its call does not
// pair with the response there either.
"call event without a role",
"",
vec![
start_ev.clone(),
ev("b", "", call_with_role("", "b1")),
ev("user", "", response("b1")),
d.clone(),
],
2,
vec![done.clone()],
),
(
// Only calls before the suffix are recorded: b's later call with the same id must
// not turn the agent's own response into context.
"id reused by a call after the suffix starts",
"",
vec![
start_ev.clone(),
ev("after", "", call("c0")),
ev("after", "", response("c0")),
ev("b", "", call("c0")),
],
2,
vec![context(&[CALLED_B])],
),
];
for (name, branch, events, from, want) in cases {
let got = build_contents_default_with_call_source(
"after",
branch,
"",
&events[from..],
&events,
false,
None,
)
.unwrap_or_else(|e| panic!("{name}: {e}"));
assert_eq!(want, got, "{name}: contents");
}
}