adk-server 3.0.0

HTTP server for ADK-Rust agents: REST and SSE, A2A v1.0, background runs and cron, approvals, and the AWP agent edge
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
use crate::a2a::{AgentCapabilities, AgentCard, AgentSkill};
use adk_core::{Agent, AgentKind};
use adk_skill::SkillIndex;
use std::sync::Arc;

/// The skills an agent card lists for `agent`, as adk-go's `BuildAgentSkills` builds them:
/// the agent's own skills, then each sub-agent's, named after the sub-agent.
///
/// - An `LlmAgent` has a `model` skill whose description is its description, then its
///   fixed instruction and global instruction told in the first person ("you are" becomes
///   "I am"), tagged `llm`; and one skill per tool, `<agent>-<tool>`, tagged `llm` and `tools`.
/// - A workflow agent describes what it runs (`First, … Then, … Finally, …` for a sequence,
///   `… simultaneously.` in parallel, `… in a loop (max N iterations).`), tagged with its
///   kind (`sequential_workflow`, `parallel_workflow`, `loop_workflow`), and lists its
///   sub-agents in a `sub-agents` skill tagged `orchestration`. Any other agent is a
///   `custom_agent`.
/// - A sub-agent's skills are `<sub>_<id>`, named `<sub>: <name>`, tagged `sub_agent:<sub>`.
pub fn build_agent_skills(agent: &dyn Agent) -> Vec<AgentSkill> {
    let mut skills = build_primary_skills(agent);
    for sub in agent.sub_agents() {
        for skill in build_primary_skills(sub.as_ref()) {
            let mut tags = vec![format!("sub_agent:{}", sub.name())];
            tags.extend(skill.tags);
            skills.push(AgentSkill::new(
                format!("{}_{}", sub.name(), skill.id),
                format!("{}: {}", sub.name(), skill.name),
                skill.description,
                tags,
            ));
        }
    }
    skills
}

fn build_primary_skills(agent: &dyn Agent) -> Vec<AgentSkill> {
    let profile = agent.profile();
    if profile.kind == AgentKind::Llm {
        let mut parts: Vec<String> = Vec::new();
        if !agent.description().is_empty() {
            parts.push(agent.description().to_string());
        }
        parts.extend(profile.instruction.as_deref().map(first_person));
        parts.extend(profile.global_instruction.as_deref().map(first_person));
        let description =
            if parts.is_empty() { default_description(profile.kind) } else { parts.join(" ") };
        let mut skills = vec![AgentSkill::new(
            agent.name().to_string(),
            "model".to_string(),
            description,
            vec!["llm".to_string()],
        )];
        for tool in &profile.tools {
            let description = if tool.description.is_empty() {
                format!("Tool: {}", tool.name)
            } else {
                tool.description.clone()
            };
            skills.push(AgentSkill::new(
                format!("{}-{}", agent.name(), tool.name),
                tool.name.clone(),
                description,
                vec!["llm".to_string(), "tools".to_string()],
            ));
        }
        return skills;
    }

    let tag = kind_tag(profile.kind);
    let mut parts: Vec<String> = Vec::new();
    if !agent.description().is_empty() {
        parts.push(agent.description().to_string());
    }
    if !agent.sub_agents().is_empty()
        && let Some(flow) = workflow_description(agent, profile.kind)
    {
        parts.push(flow);
    }
    let description =
        if parts.is_empty() { default_description(profile.kind) } else { parts.join(" ") };
    let mut skills = vec![AgentSkill::new(
        agent.name().to_string(),
        skill_name(profile.kind).to_string(),
        description,
        vec![tag.to_string()],
    )];
    let sub_agents = agent.sub_agents();
    if !sub_agents.is_empty() {
        let descriptions: Vec<String> = sub_agents
            .iter()
            .map(|sub| {
                if sub.description().is_empty() {
                    "No description".to_string()
                } else {
                    sub.description().to_string()
                }
            })
            .collect();
        skills.push(AgentSkill::new(
            format!("{}-sub-agents", agent.name()),
            "sub-agents".to_string(),
            format!("Orchestrates: {}", descriptions.join("; ")),
            vec![tag.to_string(), "orchestration".to_string()],
        ));
    }
    skills
}

/// How a workflow agent runs its sub-agents, in words.
fn workflow_description(agent: &dyn Agent, kind: AgentKind) -> Option<String> {
    let subs = agent.sub_agents();
    let what = |sub: &Arc<dyn Agent>| {
        if sub.description().is_empty() {
            format!("execute the {} agent", sub.name())
        } else {
            sub.description().to_string()
        }
    };
    let last = subs.len() - 1;
    let listed = || {
        subs.iter()
            .enumerate()
            .map(|(i, sub)| match i {
                0 => format!("This agent will {}", what(sub)),
                i if i == last => format!("and {}", what(sub)),
                _ => format!(", {}", what(sub)),
            })
            .collect::<Vec<_>>()
            .join(" ")
    };
    match kind {
        AgentKind::Sequential => Some(
            subs.iter()
                .enumerate()
                .map(|(i, sub)| match i {
                    0 => format!("First, this agent will {}.", what(sub)),
                    i if i == last => format!("Finally, this agent will {}.", what(sub)),
                    _ => format!("Then, this agent will {}.", what(sub)),
                })
                .collect::<Vec<_>>()
                .join(" "),
        ),
        AgentKind::Parallel => Some(format!("{} simultaneously.", listed())),
        AgentKind::Loop { max_iterations } => {
            let max = max_iterations.map_or_else(|| "unlimited".to_string(), |max| max.to_string());
            Some(format!("{} in a loop (max {max} iterations).", listed()))
        }
        _ => None,
    }
}

fn default_description(kind: AgentKind) -> String {
    match kind {
        AgentKind::Loop { .. } => "A loop workflow agent",
        AgentKind::Sequential => "A sequential workflow agent",
        AgentKind::Parallel => "A parallel workflow agent",
        AgentKind::Llm => "An LLM-based agent",
        _ => "A custom agent",
    }
    .to_string()
}

fn kind_tag(kind: AgentKind) -> &'static str {
    match kind {
        AgentKind::Loop { .. } => "loop_workflow",
        AgentKind::Sequential => "sequential_workflow",
        AgentKind::Parallel => "parallel_workflow",
        AgentKind::Llm => "llm_agent",
        _ => "custom_agent",
    }
}

fn skill_name(kind: AgentKind) -> &'static str {
    match kind {
        AgentKind::Loop { .. } | AgentKind::Sequential | AgentKind::Parallel => "workflow",
        _ => "custom",
    }
}

/// An instruction told by the agent itself: "you are" becomes "I am", "your" becomes "my",
/// and so on, matching whole words in any case, as adk-go's `replacePronouns` does.
fn first_person(instruction: &str) -> String {
    const SUBSTITUTIONS: [(&str, &str); 7] = [
        ("you were", "I was"),
        ("you are", "I am"),
        ("you're", "I am"),
        ("you've", "I have"),
        ("yours", "mine"),
        ("your", "my"),
        ("you", "I"),
    ];
    let mut text = instruction.to_string();
    for (original, target) in SUBSTITUTIONS {
        text = replace_word(&text, original, target);
    }
    text
}

/// `text` with each whole-word, case-insensitive match of `word` replaced by `with`.
fn replace_word(text: &str, word: &str, with: &str) -> String {
    let lower = text.to_ascii_lowercase();
    let is_word = |c: char| c.is_alphanumeric() || c == '_';
    let mut out = String::with_capacity(text.len());
    let mut i = 0;
    while let Some(found) = lower[i..].find(word) {
        let start = i + found;
        let end = start + word.len();
        let before = text[..start].chars().next_back().is_none_or(|c| !is_word(c));
        let after = text[end..].chars().next().is_none_or(|c| !is_word(c));
        if before && after {
            out.push_str(&text[i..start]);
            out.push_str(with);
        } else {
            out.push_str(&text[i..end]);
        }
        i = end;
    }
    out.push_str(&text[i..]);
    out
}

/// Maps a [`SkillIndex`] to A2A agent-card `skills[]` entries.
///
/// Each indexed skill becomes one [`AgentSkill`]: the skill name maps to both
/// `id` and `name` (the stable, human-chosen identifier — the index's own
/// content-hash IDs change on every edit), the description maps to
/// `description`, and discovery tags map to `tags`. A skill version, when
/// present, is folded into `tags` as `version:{v}` because [`AgentSkill`] has
/// no version field — following the existing `sub_agent:{name}` prefixed-tag
/// convention. Agent Registry keyword/prefix search indexes these entries.
///
/// Wire via [`ServerBuilder::with_skill_index`](crate::ServerBuilder::with_skill_index)
/// or `A2aServerBuilder::skill_index` (behind the `a2a-v1` feature) to have the
/// served card include the entries.
///
/// # Example
///
/// ```
/// use adk_server::a2a::agent_skills_from_index;
/// use adk_skill::{SkillDocument, SkillIndex};
///
/// let doc = SkillDocument {
///     id: "search-expert-abc123def456".to_string(),
///     name: "search-expert".to_string(),
///     description: "Expert in semantic and keyword search.".to_string(),
///     version: Some("1.0.0".to_string()),
///     license: None,
///     compatibility: None,
///     tags: vec!["search".to_string()],
///     allowed_tools: vec![],
///     references: vec![],
///     trigger: false,
///     hint: None,
///     metadata: Default::default(),
///     body: String::new(),
///     path: "skills/search-expert.skill.md".into(),
///     hash: "abc123def456".to_string(),
///     last_modified: None,
///     triggers: vec![],
/// };
/// let index = SkillIndex::new(vec![doc]);
///
/// let skills = agent_skills_from_index(&index);
/// assert_eq!(skills[0].id, "search-expert");
/// assert_eq!(skills[0].tags, vec!["search".to_string(), "version:1.0.0".to_string()]);
/// ```
pub fn agent_skills_from_index(index: &SkillIndex) -> Vec<AgentSkill> {
    index
        .summaries()
        .into_iter()
        .map(|summary| {
            let mut tags = summary.tags;
            if let Some(version) = summary.version {
                tags.push(format!("version:{version}"));
            }
            AgentSkill::new(summary.name.clone(), summary.name, summary.description, tags)
        })
        .collect()
}

pub fn build_agent_card(agent: &dyn Agent, base_url: &str) -> AgentCard {
    AgentCard::builder()
        .name(agent.name().to_string())
        .description(agent.description().to_string())
        .url(base_url.to_string())
        .version("1.0.0".to_string())
        .capabilities(AgentCapabilities {
            streaming: true,
            push_notifications: false,
            state_transition_history: true,
            extensions: None,
        })
        .skills(build_agent_skills(agent))
        .build()
        .expect("build_agent_card: agent name, description, and url must be non-empty")
}

#[cfg(test)]
mod tests {
    use super::*;
    use adk_core::{Agent, EventStream};
    use async_trait::async_trait;
    use std::sync::Arc;

    struct TestAgent {
        name: String,
        description: String,
    }

    #[async_trait]
    impl Agent for TestAgent {
        fn name(&self) -> &str {
            &self.name
        }

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

        async fn run(
            &self,
            _ctx: Arc<dyn adk_core::InvocationContext>,
        ) -> adk_core::Result<EventStream> {
            unimplemented!()
        }

        fn sub_agents(&self) -> &[Arc<dyn Agent>] {
            &[]
        }
    }

    #[test]
    fn test_build_agent_skills() {
        let agent =
            TestAgent { name: "test_agent".to_string(), description: "A test agent".to_string() };

        let skills = build_agent_skills(&agent);
        assert_eq!(skills.len(), 1);
        assert_eq!(skills[0].id, "test_agent");
        assert_eq!(skills[0].name, "custom", "an agent of its own kind is a custom agent");
        assert_eq!(skills[0].tags, ["custom_agent"]);
    }

    #[test]
    fn an_llm_agent_lists_its_model_and_each_tool_as_adk_go_does() {
        struct AddTool;

        #[async_trait]
        impl adk_core::Tool for AddTool {
            fn name(&self) -> &str {
                "add"
            }

            fn description(&self) -> &str {
                "Adds two integers."
            }

            async fn execute(
                &self,
                _ctx: Arc<dyn adk_core::ToolContext>,
                _args: serde_json::Value,
            ) -> adk_core::Result<serde_json::Value> {
                Ok(serde_json::json!({}))
            }
        }
        let tool = Arc::new(AddTool);
        let agent = adk_agent::LlmAgentBuilder::new("tools_agent")
            .description("Agent with the shared tool library.")
            .instruction("You are precise. Use your tools.")
            .model(Arc::new(adk_model::MockLlm::new("m")))
            .tool(tool)
            .build()
            .unwrap();

        let skills = build_agent_skills(&agent);
        assert_eq!(skills.len(), 2);
        assert_eq!((skills[0].id.as_str(), skills[0].name.as_str()), ("tools_agent", "model"));
        assert_eq!(
            skills[0].description,
            "Agent with the shared tool library. I am precise. Use my tools."
        );
        assert_eq!(skills[0].tags, ["llm"]);
        assert_eq!((skills[1].id.as_str(), skills[1].name.as_str()), ("tools_agent-add", "add"));
        assert_eq!(skills[1].tags, ["llm", "tools"]);
    }

    #[test]
    fn a_workflow_agent_describes_what_it_runs() {
        let step = |name: &str, description: &str| -> Arc<dyn Agent> {
            Arc::new(TestAgent { name: name.to_string(), description: description.to_string() })
        };
        let sequence = adk_agent::SequentialAgent::new(
            "pipeline",
            vec![step("draft", "write a draft"), step("check", ""), step("ship", "publish it")],
        );
        let skills = build_agent_skills(&sequence);
        assert_eq!(skills[0].name, "workflow");
        assert_eq!(
            skills[0].description,
            "First, this agent will write a draft. Then, this agent will execute the check agent. \
             Finally, this agent will publish it."
        );
        assert_eq!(skills[0].tags, ["sequential_workflow"]);
        assert_eq!(skills[1].id, "pipeline-sub-agents");
        assert_eq!(skills[1].tags, ["sequential_workflow", "orchestration"]);
        assert_eq!(skills[2].id, "draft_draft");
        assert_eq!(skills[2].name, "draft: custom");
        assert_eq!(skills[2].tags, ["sub_agent:draft", "custom_agent"]);
    }

    #[test]
    fn instructions_are_told_in_the_first_person() {
        assert_eq!(
            first_person("You are helpful. Your answers are yours; you've seen youth."),
            "I am helpful. my answers are mine; I have seen youth."
        );
    }

    #[test]
    fn test_build_agent_card() {
        let agent =
            TestAgent { name: "test_agent".to_string(), description: "A test agent".to_string() };

        let card = build_agent_card(&agent, "https://example.com");
        assert_eq!(card.name, "test_agent");
        assert_eq!(card.url, "https://example.com");
        assert!(card.capabilities.streaming);
    }

    fn skill_doc(
        name: &str,
        description: &str,
        version: Option<&str>,
        tags: &[&str],
    ) -> adk_skill::SkillDocument {
        adk_skill::SkillDocument {
            id: format!("{name}-0123456789ab"),
            name: name.to_string(),
            description: description.to_string(),
            version: version.map(str::to_string),
            license: None,
            compatibility: None,
            tags: tags.iter().map(|t| t.to_string()).collect(),
            allowed_tools: vec![],
            references: vec![],
            trigger: false,
            hint: None,
            metadata: Default::default(),
            body: String::new(),
            path: format!("skills/{name}.skill.md").into(),
            hash: "0123456789ab".to_string(),
            last_modified: None,
            triggers: vec![],
        }
    }

    #[test]
    fn test_agent_skills_from_index_two_skills_in_card_json() {
        let index = adk_skill::SkillIndex::new(vec![
            skill_doc(
                "search-expert",
                "Semantic and keyword search.",
                Some("1.0.0"),
                &["search", "retrieval"],
            ),
            skill_doc("code-reviewer", "Reviews Rust code for defects.", None, &["rust"]),
        ]);

        let card = AgentCard::builder()
            .name("test_agent".to_string())
            .description("A test agent".to_string())
            .url("https://example.com".to_string())
            .skills(agent_skills_from_index(&index))
            .build()
            .expect("card fields are non-empty");

        let expected = serde_json::json!({
            "name": "test_agent",
            "description": "A test agent",
            "url": "https://example.com",
            "version": "1.0.0",
            "protocolVersion": "0.3.0",
            "capabilities": {
                "streaming": false,
                "pushNotifications": false,
                "stateTransitionHistory": false,
            },
            "skills": [
                {
                    "id": "search-expert",
                    "name": "search-expert",
                    "description": "Semantic and keyword search.",
                    "tags": ["search", "retrieval", "version:1.0.0"],
                },
                {
                    "id": "code-reviewer",
                    "name": "code-reviewer",
                    "description": "Reviews Rust code for defects.",
                    "tags": ["rust"],
                },
            ],
        });

        assert_eq!(serde_json::to_value(&card).unwrap(), expected);
    }
}