-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path21-agent-tool.ts
More file actions
152 lines (139 loc) · 5.07 KB
/
Copy path21-agent-tool.ts
File metadata and controls
152 lines (139 loc) · 5.07 KB
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
/**
* Google ADK AgentTool -- agent-as-tool invocation.
*
* Demonstrates:
* - Using AgentTool to wrap an agent as a callable tool
* - The parent agent's LLM invokes the child agent like a function
* - The child agent runs its own tools and returns the result
* - Unlike subAgents (handoff), AgentTool runs inline and returns
*
* Architecture:
* manager (parent agent)
* tools:
* - AgentTool(researcher) <- child agent with its own tools
* - AgentTool(calculator) <- another child agent
*
* Requirements:
* - npm install @google/adk zod
* - AGENTSPAN_SERVER_URL for agentspan path
*/
import { LlmAgent, FunctionTool, AgentTool } from '@google/adk';
import { z } from 'zod';
import { AgentRuntime } from '@io-orkes/conductor-javascript/agents';
const model = process.env.AGENTSPAN_LLM_MODEL ?? 'gemini-2.5-flash';
// ── Child agents (each has their own tools) ──────────────────────
const searchKnowledgeBase = new FunctionTool({
name: 'search_knowledge_base',
description: 'Search an internal knowledge base for information.',
parameters: z.object({
query: z.string().describe('The search query'),
}),
execute: async (args: { query: string }) => {
const data: Record<string, {
summary: string;
popularity: string;
key_use_cases: string[];
}> = {
python: {
summary: 'Python is a high-level programming language created by Guido van Rossum in 1991.',
popularity: 'Most popular language on TIOBE index (2024)',
key_use_cases: ['web development', 'data science', 'AI/ML', 'automation'],
},
rust: {
summary: 'Rust is a systems programming language focused on safety and performance.',
popularity: 'Most admired language on Stack Overflow survey (2024)',
key_use_cases: ['systems programming', 'WebAssembly', 'CLI tools', 'embedded'],
},
};
for (const [key, val] of Object.entries(data)) {
if (args.query.toLowerCase().includes(key)) {
return { query: args.query, found: true, ...val };
}
}
return { query: args.query, found: false, summary: 'No results found.' };
},
});
export const researcher = new LlmAgent({
name: 'researcher',
model,
instruction:
'You are a research assistant. Use the knowledge base tool to find ' +
'information and provide concise, factual answers.',
tools: [searchKnowledgeBase],
});
const compute = new FunctionTool({
name: 'compute',
description: 'Evaluate a mathematical expression.',
parameters: z.object({
expression: z.string().describe("A math expression like '2 + 3 * 4'"),
}),
execute: async (args: { expression: string }) => {
// Safe subset of math operations
const safeMath: Record<string, unknown> = {
abs: Math.abs,
round: Math.round,
min: Math.min,
max: Math.max,
sqrt: Math.sqrt,
pow: Math.pow,
PI: Math.PI,
E: Math.E,
};
try {
// Simple expression evaluation (for demo purposes)
const expr = args.expression
.replace(/pi/gi, String(Math.PI))
.replace(/e(?![a-z])/gi, String(Math.E));
// Use Function constructor for basic math evaluation
const result = new Function(`"use strict"; return (${expr})`)();
return { expression: args.expression, result };
} catch (e: unknown) {
return { expression: args.expression, error: String(e) };
}
},
});
export const calculator = new LlmAgent({
name: 'calculator',
model,
instruction: 'You are a math assistant. Use the compute tool for calculations.',
tools: [compute],
});
// ── Parent agent with AgentTool wrappers ─────────────────────────
export const manager = new LlmAgent({
name: 'manager',
model,
instruction:
'You are a manager agent. You have two specialist agents available as tools:\n' +
'- researcher: for looking up information\n' +
'- calculator: for math computations\n\n' +
'Use the appropriate agent tool to answer the user\'s question. ' +
'You can call multiple agent tools if needed.',
tools: [
new AgentTool({ agent: researcher }),
new AgentTool({ agent: calculator }),
],
});
// ── Run on agentspan ───────────────────────────────────────────────
async function main() {
const runtime = new AgentRuntime();
try {
const result = await runtime.run(
manager,
'Look up information about Python and Rust, then calculate ' +
"what percentage of Python's 4 key use cases overlap with Rust's 4 use cases.",
);
console.log('Status:', result.status);
result.printResult();
// Production pattern:
// 1. Deploy once during CI/CD:
// await runtime.deploy(manager);
// CLI alternative:
// agentspan deploy --package sdk/typescript/examples/adk --agents manager
//
// 2. In a separate long-lived worker process:
// await runtime.serve(manager);
} finally {
await runtime.shutdown();
}
}
main().catch(console.error);