@google/adk 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/agents/base_agent.js +9 -3
- package/dist/cjs/agents/invocation_context.js +3 -1
- package/dist/cjs/agents/llm_agent.js +129 -75
- package/dist/cjs/agents/loop_agent.js +16 -5
- package/dist/cjs/agents/parallel_agent.js +17 -3
- package/dist/cjs/agents/readonly_context.js +4 -1
- package/dist/cjs/agents/sequential_agent.js +34 -12
- package/dist/cjs/code_executors/base_code_executor.js +12 -2
- package/dist/cjs/code_executors/built_in_code_executor.js +15 -3
- package/dist/cjs/common.js +41 -0
- package/dist/cjs/examples/base_example_provider.js +18 -2
- package/dist/cjs/examples/example_util.js +1 -1
- package/dist/cjs/index.js +11 -11
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/plugins/base_plugin.js +59 -49
- package/dist/cjs/plugins/security_plugin.js +4 -1
- package/dist/cjs/runner/runner.js +34 -24
- package/dist/cjs/tools/agent_tool.js +18 -9
- package/dist/cjs/tools/base_tool.js +16 -6
- package/dist/cjs/tools/function_tool.js +14 -5
- package/dist/cjs/tools/google_search_tool.js +8 -3
- package/dist/cjs/tools/tool_context.js +4 -9
- package/dist/cjs/utils/gemini_schema_util.js +1 -0
- package/dist/cjs/version.js +1 -1
- package/dist/esm/agents/base_agent.js +9 -3
- package/dist/esm/agents/invocation_context.js +3 -1
- package/dist/esm/agents/llm_agent.js +165 -83
- package/dist/esm/agents/loop_agent.js +14 -4
- package/dist/esm/agents/parallel_agent.js +15 -2
- package/dist/esm/agents/readonly_context.js +4 -1
- package/dist/esm/agents/sequential_agent.js +33 -12
- package/dist/esm/code_executors/base_code_executor.js +10 -1
- package/dist/esm/code_executors/built_in_code_executor.js +13 -2
- package/dist/esm/common.js +53 -10
- package/dist/esm/examples/base_example_provider.js +16 -1
- package/dist/esm/examples/example_util.js +4 -2
- package/dist/esm/index.js +11 -11
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/plugins/base_plugin.js +59 -49
- package/dist/esm/plugins/security_plugin.js +4 -1
- package/dist/esm/runner/runner.js +43 -27
- package/dist/esm/tools/agent_tool.js +17 -9
- package/dist/esm/tools/base_tool.js +14 -5
- package/dist/esm/tools/function_tool.js +13 -7
- package/dist/esm/tools/google_search_tool.js +9 -3
- package/dist/esm/tools/tool_context.js +4 -9
- package/dist/esm/utils/gemini_schema_util.js +1 -0
- package/dist/esm/version.js +1 -1
- package/dist/types/agents/base_agent.d.ts +14 -3
- package/dist/types/agents/invocation_context.d.ts +4 -2
- package/dist/types/agents/llm_agent.d.ts +42 -30
- package/dist/types/agents/loop_agent.d.ts +16 -0
- package/dist/types/agents/parallel_agent.d.ts +16 -0
- package/dist/types/agents/sequential_agent.d.ts +17 -1
- package/dist/types/auth/auth_schemes.d.ts +5 -2
- package/dist/types/code_executors/base_code_executor.d.ts +14 -0
- package/dist/types/code_executors/built_in_code_executor.d.ts +19 -0
- package/dist/types/common.d.ts +38 -14
- package/dist/types/examples/base_example_provider.d.ts +16 -0
- package/dist/types/plugins/base_plugin.d.ts +50 -40
- package/dist/types/runner/runner.d.ts +17 -9
- package/dist/types/tools/agent_tool.d.ts +14 -0
- package/dist/types/tools/base_tool.d.ts +14 -0
- package/dist/types/tools/function_tool.d.ts +14 -1
- package/dist/types/tools/google_search_tool.d.ts +3 -4
- package/dist/types/tools/tool_context.d.ts +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/web/agents/base_agent.js +9 -3
- package/dist/web/agents/invocation_context.js +3 -1
- package/dist/web/agents/llm_agent.js +165 -83
- package/dist/web/agents/loop_agent.js +14 -4
- package/dist/web/agents/parallel_agent.js +15 -2
- package/dist/web/agents/readonly_context.js +4 -1
- package/dist/web/agents/sequential_agent.js +33 -12
- package/dist/web/code_executors/base_code_executor.js +10 -1
- package/dist/web/code_executors/built_in_code_executor.js +13 -2
- package/dist/web/common.js +53 -10
- package/dist/web/examples/base_example_provider.js +16 -1
- package/dist/web/examples/example_util.js +4 -2
- package/dist/web/index.js +1 -1
- package/dist/web/index.js.map +4 -4
- package/dist/web/plugins/base_plugin.js +59 -49
- package/dist/web/plugins/security_plugin.js +4 -1
- package/dist/web/runner/runner.js +44 -28
- package/dist/web/tools/agent_tool.js +17 -9
- package/dist/web/tools/base_tool.js +14 -5
- package/dist/web/tools/function_tool.js +13 -7
- package/dist/web/tools/google_search_tool.js +9 -3
- package/dist/web/tools/tool_context.js +4 -9
- package/dist/web/utils/gemini_schema_util.js +1 -0
- package/dist/web/version.js +1 -1
- package/package.json +2 -2
|
@@ -19,13 +19,13 @@ class BasePlugin {
|
|
|
19
19
|
* This callback helps logging and modifying the user message before the
|
|
20
20
|
* runner starts the invocation.
|
|
21
21
|
*
|
|
22
|
-
* @param invocationContext The context for the entire invocation.
|
|
23
|
-
* @param userMessage The message content input by user.
|
|
22
|
+
* @param params.invocationContext The context for the entire invocation.
|
|
23
|
+
* @param params.userMessage The message content input by user.
|
|
24
24
|
* @returns An optional `Content` to be returned to the ADK. Returning a
|
|
25
25
|
* value to replace the user message. Returning `undefined` to proceed
|
|
26
26
|
* normally.
|
|
27
27
|
*/
|
|
28
|
-
async onUserMessageCallback(
|
|
28
|
+
async onUserMessageCallback(params) {
|
|
29
29
|
return;
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
@@ -34,13 +34,13 @@ class BasePlugin {
|
|
|
34
34
|
* This is the first callback to be called in the lifecycle, ideal for global
|
|
35
35
|
* setup or initialization tasks.
|
|
36
36
|
*
|
|
37
|
-
* @param invocationContext The context for the entire invocation, containing
|
|
37
|
+
* @param params.invocationContext The context for the entire invocation, containing
|
|
38
38
|
* session information, the root agent, etc.
|
|
39
39
|
* @returns An optional `Event` to be returned to the ADK. Returning a value
|
|
40
40
|
* to halt execution of the runner and ends the runner with that event.
|
|
41
41
|
* Return `undefined` to proceed normally.
|
|
42
42
|
*/
|
|
43
|
-
async beforeRunCallback(
|
|
43
|
+
async beforeRunCallback(params) {
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
@@ -49,13 +49,13 @@ class BasePlugin {
|
|
|
49
49
|
* This is the ideal place to make modification to the event before the event
|
|
50
50
|
* is handled by the underlying agent app.
|
|
51
51
|
*
|
|
52
|
-
* @param invocationContext The context for the entire invocation.
|
|
53
|
-
* @param event The event raised by the runner.
|
|
52
|
+
* @param params.invocationContext The context for the entire invocation.
|
|
53
|
+
* @param params.event The event raised by the runner.
|
|
54
54
|
* @returns An optional value. A non-`undefined` return may be used by the
|
|
55
55
|
* framework to modify or replace the response. Returning `undefined`
|
|
56
56
|
* allows the original response to be used.
|
|
57
57
|
*/
|
|
58
|
-
async onEventCallback(
|
|
58
|
+
async onEventCallback(params) {
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
@@ -64,10 +64,10 @@ class BasePlugin {
|
|
|
64
64
|
* This is the final callback in the ADK lifecycle, suitable for cleanup,
|
|
65
65
|
* final logging, or reporting tasks.
|
|
66
66
|
*
|
|
67
|
-
* @param invocationContext The context for the entire invocation.
|
|
67
|
+
* @param params.invocationContext The context for the entire invocation.
|
|
68
68
|
* @returns undefined
|
|
69
69
|
*/
|
|
70
|
-
async afterRunCallback(
|
|
70
|
+
async afterRunCallback(params) {
|
|
71
71
|
return;
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
@@ -76,13 +76,13 @@ class BasePlugin {
|
|
|
76
76
|
* This callback can be used for logging, setup, or to short-circuit the
|
|
77
77
|
* agent's execution by returning a value.
|
|
78
78
|
*
|
|
79
|
-
* @param agent The agent that is about to run.
|
|
80
|
-
* @param callbackContext The context for the agent invocation.
|
|
79
|
+
* @param params.agent The agent that is about to run.
|
|
80
|
+
* @param params.callbackContext The context for the agent invocation.
|
|
81
81
|
* @returns An optional `Content` object. If a value is returned, it will
|
|
82
82
|
* bypass the agent's callbacks and its execution, and return this value
|
|
83
83
|
* directly. Returning `undefined` allows the agent to proceed normally.
|
|
84
84
|
*/
|
|
85
|
-
async beforeAgentCallback(
|
|
85
|
+
async beforeAgentCallback(params) {
|
|
86
86
|
return;
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
@@ -91,13 +91,13 @@ class BasePlugin {
|
|
|
91
91
|
* This callback can be used to inspect, log, or modify the agent's final
|
|
92
92
|
* result before it is returned.
|
|
93
93
|
*
|
|
94
|
-
* @param agent The agent that has just run.
|
|
95
|
-
* @param callbackContext The context for the agent invocation.
|
|
94
|
+
* @param params.agent The agent that has just run.
|
|
95
|
+
* @param params.callbackContext The context for the agent invocation.
|
|
96
96
|
* @returns An optional `Content` object. If a value is returned, it will
|
|
97
97
|
* replace the agent's original result. Returning `undefined` uses the
|
|
98
98
|
* original, unmodified result.
|
|
99
99
|
*/
|
|
100
|
-
async afterAgentCallback(
|
|
100
|
+
async afterAgentCallback(params) {
|
|
101
101
|
return;
|
|
102
102
|
}
|
|
103
103
|
/**
|
|
@@ -107,13 +107,13 @@ class BasePlugin {
|
|
|
107
107
|
* object. It can also be used to implement caching by returning a cached
|
|
108
108
|
* `LlmResponse`, which would skip the actual model call.
|
|
109
109
|
*
|
|
110
|
-
* @param callbackContext The context for the current agent call.
|
|
111
|
-
* @param llmRequest The prepared request object to be sent to the model.
|
|
110
|
+
* @param params.callbackContext The context for the current agent call.
|
|
111
|
+
* @param params.llmRequest The prepared request object to be sent to the model.
|
|
112
112
|
* @returns An optional value. The interpretation of a non-`undefined`
|
|
113
113
|
* trigger an early exit and returns the response immediately. Returning
|
|
114
114
|
* `undefined` allows the LLM request to proceed normally.
|
|
115
115
|
*/
|
|
116
|
-
async beforeModelCallback(
|
|
116
|
+
async beforeModelCallback(params) {
|
|
117
117
|
return;
|
|
118
118
|
}
|
|
119
119
|
/**
|
|
@@ -122,13 +122,13 @@ class BasePlugin {
|
|
|
122
122
|
* This is the ideal place to log model responses, collect metrics on token
|
|
123
123
|
* usage, or perform post-processing on the raw `LlmResponse`.
|
|
124
124
|
*
|
|
125
|
-
* @param callbackContext The context for the current agent call.
|
|
126
|
-
* @param llmResponse The response object received from the model.
|
|
125
|
+
* @param params.callbackContext The context for the current agent call.
|
|
126
|
+
* @param params.llmResponse The response object received from the model.
|
|
127
127
|
* @returns An optional value. A non-`undefined` return may be used by the
|
|
128
128
|
* framework to modify or replace the response. Returning `undefined`
|
|
129
129
|
* allows the original response to be used.
|
|
130
130
|
*/
|
|
131
|
-
async afterModelCallback(
|
|
131
|
+
async afterModelCallback(params) {
|
|
132
132
|
return;
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
@@ -137,15 +137,15 @@ class BasePlugin {
|
|
|
137
137
|
* This callback provides an opportunity to handle model errors gracefully,
|
|
138
138
|
* potentially providing alternative responses or recovery mechanisms.
|
|
139
139
|
*
|
|
140
|
-
* @param callbackContext The context for the current agent call.
|
|
141
|
-
* @param llmRequest The request that was sent to the model when the error
|
|
140
|
+
* @param params.callbackContext The context for the current agent call.
|
|
141
|
+
* @param params.llmRequest The request that was sent to the model when the error
|
|
142
142
|
* occurred.
|
|
143
|
-
* @param error The exception that was raised during model execution.
|
|
143
|
+
* @param params.error The exception that was raised during model execution.
|
|
144
144
|
* @returns An optional LlmResponse. If an LlmResponse is returned, it will be
|
|
145
145
|
* used instead of propagating the error. Returning `undefined` allows
|
|
146
146
|
* the original error to be raised.
|
|
147
147
|
*/
|
|
148
|
-
async onModelErrorCallback(
|
|
148
|
+
async onModelErrorCallback(params) {
|
|
149
149
|
return;
|
|
150
150
|
}
|
|
151
151
|
/**
|
|
@@ -154,15 +154,15 @@ class BasePlugin {
|
|
|
154
154
|
* This callback is useful for logging tool usage, input validation, or
|
|
155
155
|
* modifying the arguments before they are passed to the tool.
|
|
156
156
|
*
|
|
157
|
-
* @param tool The tool instance that is about to be executed.
|
|
158
|
-
* @param toolArgs The dictionary of arguments to be used for invoking the
|
|
157
|
+
* @param params.tool The tool instance that is about to be executed.
|
|
158
|
+
* @param params.toolArgs The dictionary of arguments to be used for invoking the
|
|
159
159
|
* tool.
|
|
160
|
-
* @param toolContext The context specific to the tool execution.
|
|
160
|
+
* @param params.toolContext The context specific to the tool execution.
|
|
161
161
|
* @returns An optional dictionary. If a dictionary is returned, it will stop
|
|
162
162
|
* the tool execution and return this response immediately. Returning
|
|
163
163
|
* `undefined` uses the original, unmodified arguments.
|
|
164
164
|
*/
|
|
165
|
-
async beforeToolCallback(
|
|
165
|
+
async beforeToolCallback(params) {
|
|
166
166
|
return;
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
@@ -171,33 +171,43 @@ class BasePlugin {
|
|
|
171
171
|
* This callback allows for inspecting, logging, or modifying the result
|
|
172
172
|
* returned by a tool.
|
|
173
173
|
*
|
|
174
|
-
* @param tool The tool instance that has just been executed.
|
|
175
|
-
* @param toolArgs The original arguments that were passed to the tool.
|
|
176
|
-
* @param toolContext The context specific to the tool execution.
|
|
177
|
-
* @param result The dictionary returned by the tool invocation.
|
|
174
|
+
* @param params.tool The tool instance that has just been executed.
|
|
175
|
+
* @param params.toolArgs The original arguments that were passed to the tool.
|
|
176
|
+
* @param params.toolContext The context specific to the tool execution.
|
|
177
|
+
* @param params.result The dictionary returned by the tool invocation.
|
|
178
178
|
* @returns An optional dictionary. If a dictionary is returned, it will
|
|
179
179
|
* **replace** the original result from the tool. This allows for
|
|
180
180
|
* post-processing or altering tool outputs. Returning `undefined` uses
|
|
181
181
|
* the original, unmodified result.
|
|
182
182
|
*/
|
|
183
|
-
async afterToolCallback(
|
|
183
|
+
async afterToolCallback(params) {
|
|
184
184
|
return;
|
|
185
185
|
}
|
|
186
186
|
/**
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
187
|
+
* Callback executed when a tool call encounters an error.
|
|
188
|
+
tool: BaseTool;
|
|
189
|
+
toolArgs: Record<string, unknown>;
|
|
190
|
+
toolContext: ToolContext;
|
|
191
|
+
result: Record<string, unknown>;
|
|
192
|
+
}): Promise<Record<string, unknown> | undefined> {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Callback executed when a tool call encounters an error.
|
|
198
|
+
*
|
|
199
|
+
* This callback provides an opportunity to handle tool errors gracefully,
|
|
200
|
+
* potentially providing alternative responses or recovery mechanisms.
|
|
201
|
+
*
|
|
202
|
+
* @param params.tool The tool instance that encountered an error.
|
|
203
|
+
* @param params.toolArgs The arguments that were passed to the tool.
|
|
204
|
+
* @param params.toolContext The context specific to the tool execution.
|
|
205
|
+
* @param params.error The exception that was raised during tool execution.
|
|
206
|
+
* @returns An optional dictionary. If a dictionary is returned, it will be
|
|
207
|
+
* used as the tool response instead of propagating the error. Returning
|
|
208
|
+
* `undefined` allows the original error to be raised.
|
|
209
|
+
*/
|
|
210
|
+
async onToolErrorCallback(params) {
|
|
201
211
|
return;
|
|
202
212
|
}
|
|
203
213
|
}
|
|
@@ -79,7 +79,10 @@ class SecurityPlugin extends BasePlugin {
|
|
|
79
79
|
toolArgs,
|
|
80
80
|
toolContext
|
|
81
81
|
}) {
|
|
82
|
-
const policyCheckResult = await this.policyEngine.evaluate({
|
|
82
|
+
const policyCheckResult = await this.policyEngine.evaluate({
|
|
83
|
+
tool,
|
|
84
|
+
toolArgs
|
|
85
|
+
});
|
|
83
86
|
this.setToolCallCheckState(toolContext, policyCheckResult.outcome);
|
|
84
87
|
switch (policyCheckResult.outcome) {
|
|
85
88
|
case "DENY" /* DENY */:
|
|
@@ -5,10 +5,16 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { createPartFromText } from "@google/genai";
|
|
7
7
|
import { trace } from "@opentelemetry/api";
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import {
|
|
9
|
+
InvocationContext,
|
|
10
|
+
newInvocationContextId
|
|
11
|
+
} from "../agents/invocation_context.js";
|
|
12
|
+
import { isLlmAgent } from "../agents/llm_agent.js";
|
|
10
13
|
import { createRunConfig } from "../agents/run_config.js";
|
|
11
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
BuiltInCodeExecutor,
|
|
16
|
+
isBuiltInCodeExecutor
|
|
17
|
+
} from "../code_executors/built_in_code_executor.js";
|
|
12
18
|
import { createEvent, getFunctionCalls } from "../events/event.js";
|
|
13
19
|
import { createEventActions } from "../events/event_actions.js";
|
|
14
20
|
import { PluginManager } from "../plugins/plugin_manager.js";
|
|
@@ -29,26 +35,26 @@ class Runner {
|
|
|
29
35
|
* Runs the agent with the given message, and returns an async generator of
|
|
30
36
|
* events.
|
|
31
37
|
*
|
|
32
|
-
* @param userId The user ID of the session.
|
|
33
|
-
* @param sessionId The session ID of the session.
|
|
34
|
-
* @param newMessage A new message to append to the session.
|
|
35
|
-
* @param stateDelta An optional state delta to apply to the session.
|
|
36
|
-
* @param runConfig The run config for the agent.
|
|
38
|
+
* @param params.userId The user ID of the session.
|
|
39
|
+
* @param params.sessionId The session ID of the session.
|
|
40
|
+
* @param params.newMessage A new message to append to the session.
|
|
41
|
+
* @param params.stateDelta An optional state delta to apply to the session.
|
|
42
|
+
* @param params.runConfig The run config for the agent.
|
|
37
43
|
* @yields The events generated by the agent.
|
|
38
44
|
*/
|
|
39
45
|
// TODO - b/425992518: user, sessionId, and runConfig can be internalized.
|
|
40
|
-
async *runAsync({
|
|
41
|
-
userId,
|
|
42
|
-
sessionId,
|
|
43
|
-
newMessage,
|
|
44
|
-
stateDelta,
|
|
45
|
-
runConfig
|
|
46
|
-
}) {
|
|
46
|
+
async *runAsync(params) {
|
|
47
47
|
var _a;
|
|
48
|
-
|
|
48
|
+
const { userId, sessionId, stateDelta } = params;
|
|
49
|
+
const runConfig = createRunConfig(params.runConfig);
|
|
50
|
+
let newMessage = params.newMessage;
|
|
49
51
|
const span = trace.getTracer("gcp.vertex.agent").startSpan("invocation");
|
|
50
52
|
try {
|
|
51
|
-
const session = await this.sessionService.getSession({
|
|
53
|
+
const session = await this.sessionService.getSession({
|
|
54
|
+
appName: this.appName,
|
|
55
|
+
userId,
|
|
56
|
+
sessionId
|
|
57
|
+
});
|
|
52
58
|
if (!session) {
|
|
53
59
|
if (!this.appName) {
|
|
54
60
|
throw new Error(
|
|
@@ -57,12 +63,14 @@ class Runner {
|
|
|
57
63
|
}
|
|
58
64
|
throw new Error(`Session not found: ${sessionId}`);
|
|
59
65
|
}
|
|
60
|
-
if (runConfig.supportCfc && this.agent
|
|
66
|
+
if (runConfig.supportCfc && isLlmAgent(this.agent)) {
|
|
61
67
|
const modelName = this.agent.canonicalModel.model;
|
|
62
68
|
if (!isGemini2OrAbove(modelName)) {
|
|
63
|
-
throw new Error(
|
|
69
|
+
throw new Error(
|
|
70
|
+
`CFC is not supported for model: ${modelName} in agent: ${this.agent.name}`
|
|
71
|
+
);
|
|
64
72
|
}
|
|
65
|
-
if (!(this.agent.codeExecutor
|
|
73
|
+
if (!isBuiltInCodeExecutor(this.agent.codeExecutor)) {
|
|
66
74
|
this.agent.codeExecutor = new BuiltInCodeExecutor();
|
|
67
75
|
}
|
|
68
76
|
}
|
|
@@ -107,7 +115,10 @@ class Runner {
|
|
|
107
115
|
})
|
|
108
116
|
});
|
|
109
117
|
}
|
|
110
|
-
invocationContext.agent = this.determineAgentForResumption(
|
|
118
|
+
invocationContext.agent = this.determineAgentForResumption(
|
|
119
|
+
session,
|
|
120
|
+
this.agent
|
|
121
|
+
);
|
|
111
122
|
const beforeRunCallbackResponse = await this.pluginManager.runBeforeRunCallback({ invocationContext });
|
|
112
123
|
if (beforeRunCallbackResponse) {
|
|
113
124
|
const earlyExitEvent = createEvent({
|
|
@@ -124,9 +135,10 @@ class Runner {
|
|
|
124
135
|
if (!event.partial) {
|
|
125
136
|
await this.sessionService.appendEvent({ session, event });
|
|
126
137
|
}
|
|
127
|
-
const modifiedEvent = await this.pluginManager.runOnEventCallback(
|
|
128
|
-
|
|
129
|
-
|
|
138
|
+
const modifiedEvent = await this.pluginManager.runOnEventCallback({
|
|
139
|
+
invocationContext,
|
|
140
|
+
event
|
|
141
|
+
});
|
|
130
142
|
if (modifiedEvent) {
|
|
131
143
|
yield modifiedEvent;
|
|
132
144
|
} else {
|
|
@@ -193,7 +205,9 @@ class Runner {
|
|
|
193
205
|
}
|
|
194
206
|
const agent = rootAgent.findSubAgent(event2.author);
|
|
195
207
|
if (!agent) {
|
|
196
|
-
logger.warn(
|
|
208
|
+
logger.warn(
|
|
209
|
+
`Event from an unknown agent: ${event2.author}, event id: ${event2.id}`
|
|
210
|
+
);
|
|
197
211
|
continue;
|
|
198
212
|
}
|
|
199
213
|
if (this.isRoutableLlmAgent(agent)) {
|
|
@@ -216,7 +230,7 @@ class Runner {
|
|
|
216
230
|
isRoutableLlmAgent(agentToRun) {
|
|
217
231
|
let agent = agentToRun;
|
|
218
232
|
while (agent) {
|
|
219
|
-
if (!(agent
|
|
233
|
+
if (!isLlmAgent(agent)) {
|
|
220
234
|
return false;
|
|
221
235
|
}
|
|
222
236
|
if (agent.disallowTransferToParent) {
|
|
@@ -234,7 +248,9 @@ function findEventByLastFunctionResponseId(events) {
|
|
|
234
248
|
return null;
|
|
235
249
|
}
|
|
236
250
|
const lastEvent = events[events.length - 1];
|
|
237
|
-
const functionCallId = (_d = (_c = (_b = (_a = lastEvent.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.find(
|
|
251
|
+
const functionCallId = (_d = (_c = (_b = (_a = lastEvent.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.find(
|
|
252
|
+
(part) => part.functionResponse
|
|
253
|
+
)) == null ? void 0 : _c.functionResponse) == null ? void 0 : _d.id;
|
|
238
254
|
if (!functionCallId) {
|
|
239
255
|
return null;
|
|
240
256
|
}
|
|
@@ -3,25 +3,32 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
+
var _a, _b;
|
|
6
7
|
import { Type } from "@google/genai";
|
|
7
|
-
import {
|
|
8
|
+
import { isLlmAgent } from "../agents/llm_agent.js";
|
|
8
9
|
import { InMemoryMemoryService } from "../memory/in_memory_memory_service.js";
|
|
9
10
|
import { Runner } from "../runner/runner.js";
|
|
10
11
|
import { InMemorySessionService } from "../sessions/in_memory_session_service.js";
|
|
11
12
|
import { GoogleLLMVariant } from "../utils/variant_utils.js";
|
|
12
13
|
import { BaseTool } from "./base_tool.js";
|
|
13
14
|
import { ForwardingArtifactService } from "./forwarding_artifact_service.js";
|
|
14
|
-
|
|
15
|
+
const AGENT_TOOL_SIGNATURE_SYMBOL = Symbol.for("google.adk.agentTool");
|
|
16
|
+
function isAgentTool(obj) {
|
|
17
|
+
return typeof obj === "object" && obj !== null && AGENT_TOOL_SIGNATURE_SYMBOL in obj && obj[AGENT_TOOL_SIGNATURE_SYMBOL] === true;
|
|
18
|
+
}
|
|
19
|
+
class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
15
20
|
constructor(config) {
|
|
16
21
|
super(
|
|
17
22
|
{ name: config.agent.name, description: config.agent.description || "" }
|
|
18
23
|
);
|
|
24
|
+
/** A unique symbol to identify ADK agent tool class. */
|
|
25
|
+
this[_a] = true;
|
|
19
26
|
this.agent = config.agent;
|
|
20
27
|
this.skipSummarization = config.skipSummarization || false;
|
|
21
28
|
}
|
|
22
29
|
_getDeclaration() {
|
|
23
30
|
let declaration;
|
|
24
|
-
if (this.agent
|
|
31
|
+
if (isLlmAgent(this.agent) && this.agent.inputSchema) {
|
|
25
32
|
declaration = {
|
|
26
33
|
name: this.name,
|
|
27
34
|
description: this.description,
|
|
@@ -46,17 +53,17 @@ class AgentTool extends BaseTool {
|
|
|
46
53
|
};
|
|
47
54
|
}
|
|
48
55
|
if (this.apiVariant !== GoogleLLMVariant.GEMINI_API) {
|
|
49
|
-
const hasOutputSchema = this.agent
|
|
56
|
+
const hasOutputSchema = isLlmAgent(this.agent) && this.agent.outputSchema;
|
|
50
57
|
declaration.response = hasOutputSchema ? { type: Type.OBJECT } : { type: Type.STRING };
|
|
51
58
|
}
|
|
52
59
|
return declaration;
|
|
53
60
|
}
|
|
54
61
|
async runAsync({ args, toolContext }) {
|
|
55
|
-
var
|
|
62
|
+
var _a2, _b2;
|
|
56
63
|
if (this.skipSummarization) {
|
|
57
64
|
toolContext.actions.skipSummarization = true;
|
|
58
65
|
}
|
|
59
|
-
const hasInputSchema = this.agent
|
|
66
|
+
const hasInputSchema = isLlmAgent(this.agent) && this.agent.inputSchema;
|
|
60
67
|
const content = {
|
|
61
68
|
role: "user",
|
|
62
69
|
parts: [
|
|
@@ -91,14 +98,15 @@ class AgentTool extends BaseTool {
|
|
|
91
98
|
}
|
|
92
99
|
lastEvent = event;
|
|
93
100
|
}
|
|
94
|
-
if (!((
|
|
101
|
+
if (!((_b2 = (_a2 = lastEvent == null ? void 0 : lastEvent.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2.length)) {
|
|
95
102
|
return "";
|
|
96
103
|
}
|
|
97
|
-
const hasOutputSchema = this.agent
|
|
104
|
+
const hasOutputSchema = isLlmAgent(this.agent) && this.agent.outputSchema;
|
|
98
105
|
const mergedText = lastEvent.content.parts.map((part) => part.text).filter((text) => text).join("\n");
|
|
99
106
|
return hasOutputSchema ? JSON.parse(mergedText) : mergedText;
|
|
100
107
|
}
|
|
101
108
|
}
|
|
102
109
|
export {
|
|
103
|
-
AgentTool
|
|
110
|
+
AgentTool,
|
|
111
|
+
isAgentTool
|
|
104
112
|
};
|
|
@@ -3,7 +3,13 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
+
var _a;
|
|
6
7
|
import { getGoogleLlmVariant } from "../utils/variant_utils.js";
|
|
8
|
+
const BASE_TOOL_SIGNATURE_SYMBOL = Symbol.for("google.adk.baseTool");
|
|
9
|
+
function isBaseTool(obj) {
|
|
10
|
+
return typeof obj === "object" && obj !== null && BASE_TOOL_SIGNATURE_SYMBOL in obj && obj[BASE_TOOL_SIGNATURE_SYMBOL] === true;
|
|
11
|
+
}
|
|
12
|
+
_a = BASE_TOOL_SIGNATURE_SYMBOL;
|
|
7
13
|
class BaseTool {
|
|
8
14
|
/**
|
|
9
15
|
* Base constructor for a tool.
|
|
@@ -11,10 +17,12 @@ class BaseTool {
|
|
|
11
17
|
* @param params The parameters for `BaseTool`.
|
|
12
18
|
*/
|
|
13
19
|
constructor(params) {
|
|
14
|
-
|
|
20
|
+
/** A unique symbol to identify ADK base tool class. */
|
|
21
|
+
this[_a] = true;
|
|
22
|
+
var _a2;
|
|
15
23
|
this.name = params.name;
|
|
16
24
|
this.description = params.description;
|
|
17
|
-
this.isLongRunning = (
|
|
25
|
+
this.isLongRunning = (_a2 = params.isLongRunning) != null ? _a2 : false;
|
|
18
26
|
}
|
|
19
27
|
/**
|
|
20
28
|
* Gets the OpenAPI specification of this tool in the form of a
|
|
@@ -69,9 +77,10 @@ class BaseTool {
|
|
|
69
77
|
}
|
|
70
78
|
}
|
|
71
79
|
function findToolWithFunctionDeclarations(llmRequest) {
|
|
72
|
-
var
|
|
73
|
-
return (((
|
|
80
|
+
var _a2;
|
|
81
|
+
return (((_a2 = llmRequest.config) == null ? void 0 : _a2.tools) || []).find((tool) => "functionDeclarations" in tool);
|
|
74
82
|
}
|
|
75
83
|
export {
|
|
76
|
-
BaseTool
|
|
84
|
+
BaseTool,
|
|
85
|
+
isBaseTool
|
|
77
86
|
};
|
|
@@ -3,10 +3,9 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
+
var _a, _b;
|
|
6
7
|
import { Type } from "@google/genai";
|
|
7
|
-
import {
|
|
8
|
-
ZodObject
|
|
9
|
-
} from "zod";
|
|
8
|
+
import { ZodObject } from "zod";
|
|
10
9
|
import { isZodObject, zodObjectToSchema } from "../utils/simple_zod_to_json.js";
|
|
11
10
|
import { BaseTool } from "./base_tool.js";
|
|
12
11
|
function toSchema(parameters) {
|
|
@@ -18,14 +17,18 @@ function toSchema(parameters) {
|
|
|
18
17
|
}
|
|
19
18
|
return parameters;
|
|
20
19
|
}
|
|
21
|
-
|
|
20
|
+
const FUNCTION_TOOL_SIGNATURE_SYMBOL = Symbol.for("google.adk.functionTool");
|
|
21
|
+
function isFunctionTool(obj) {
|
|
22
|
+
return typeof obj === "object" && obj !== null && FUNCTION_TOOL_SIGNATURE_SYMBOL in obj && obj[FUNCTION_TOOL_SIGNATURE_SYMBOL] === true;
|
|
23
|
+
}
|
|
24
|
+
class FunctionTool extends (_b = BaseTool, _a = FUNCTION_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
22
25
|
/**
|
|
23
26
|
* The constructor acts as the user-friendly factory.
|
|
24
27
|
* @param options The configuration for the tool.
|
|
25
28
|
*/
|
|
26
29
|
constructor(options) {
|
|
27
|
-
var
|
|
28
|
-
const name = (
|
|
30
|
+
var _a2;
|
|
31
|
+
const name = (_a2 = options.name) != null ? _a2 : options.execute.name;
|
|
29
32
|
if (!name) {
|
|
30
33
|
throw new Error(
|
|
31
34
|
"Tool name cannot be empty. Either name the `execute` function or provide a `name`."
|
|
@@ -36,6 +39,8 @@ class FunctionTool extends BaseTool {
|
|
|
36
39
|
description: options.description,
|
|
37
40
|
isLongRunning: options.isLongRunning
|
|
38
41
|
});
|
|
42
|
+
/** A unique symbol to identify ADK function tool class. */
|
|
43
|
+
this[_a] = true;
|
|
39
44
|
this.execute = options.execute;
|
|
40
45
|
this.parameters = options.parameters;
|
|
41
46
|
}
|
|
@@ -69,5 +74,6 @@ class FunctionTool extends BaseTool {
|
|
|
69
74
|
}
|
|
70
75
|
}
|
|
71
76
|
export {
|
|
72
|
-
FunctionTool
|
|
77
|
+
FunctionTool,
|
|
78
|
+
isFunctionTool
|
|
73
79
|
};
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
import { isGemini1Model, isGeminiModel } from "../utils/model_name.js";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
BaseTool
|
|
9
|
+
} from "./base_tool.js";
|
|
8
10
|
class GoogleSearchTool extends BaseTool {
|
|
9
11
|
constructor() {
|
|
10
12
|
super({ name: "google_search", description: "Google Search Tool" });
|
|
@@ -12,7 +14,10 @@ class GoogleSearchTool extends BaseTool {
|
|
|
12
14
|
runAsync(request) {
|
|
13
15
|
return Promise.resolve();
|
|
14
16
|
}
|
|
15
|
-
async processLlmRequest({
|
|
17
|
+
async processLlmRequest({
|
|
18
|
+
toolContext,
|
|
19
|
+
llmRequest
|
|
20
|
+
}) {
|
|
16
21
|
if (!llmRequest.model) {
|
|
17
22
|
return;
|
|
18
23
|
}
|
|
@@ -42,5 +47,6 @@ class GoogleSearchTool extends BaseTool {
|
|
|
42
47
|
}
|
|
43
48
|
const GOOGLE_SEARCH = new GoogleSearchTool();
|
|
44
49
|
export {
|
|
45
|
-
GOOGLE_SEARCH
|
|
50
|
+
GOOGLE_SEARCH,
|
|
51
|
+
GoogleSearchTool
|
|
46
52
|
};
|
|
@@ -18,15 +18,10 @@ class ToolContext extends CallbackContext {
|
|
|
18
18
|
* @param params.toolConfirmation The tool confirmation of the current tool
|
|
19
19
|
* call.
|
|
20
20
|
*/
|
|
21
|
-
constructor({
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
toolConfirmation
|
|
26
|
-
}) {
|
|
27
|
-
super({ invocationContext, eventActions });
|
|
28
|
-
this.functionCallId = functionCallId;
|
|
29
|
-
this.toolConfirmation = toolConfirmation;
|
|
21
|
+
constructor(params) {
|
|
22
|
+
super(params);
|
|
23
|
+
this.functionCallId = params.functionCallId;
|
|
24
|
+
this.toolConfirmation = params.toolConfirmation;
|
|
30
25
|
}
|
|
31
26
|
get actions() {
|
|
32
27
|
return this.eventActions;
|
package/dist/esm/version.js
CHANGED
|
@@ -7,9 +7,20 @@ import { Content } from '@google/genai';
|
|
|
7
7
|
import { Event } from '../events/event.js';
|
|
8
8
|
import { CallbackContext } from './callback_context.js';
|
|
9
9
|
import { InvocationContext } from './invocation_context.js';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
/**
|
|
11
|
+
* A single callback function for an agent.
|
|
12
|
+
*/
|
|
13
|
+
export type SingleAgentCallback = (context: CallbackContext) => Promise<Content | undefined> | (Content | undefined);
|
|
14
|
+
/**
|
|
15
|
+
* Type for before agent callbacks, which can be a single callback or
|
|
16
|
+
* an array of callbacks.
|
|
17
|
+
*/
|
|
18
|
+
export type BeforeAgentCallback = SingleAgentCallback | SingleAgentCallback[];
|
|
19
|
+
/**
|
|
20
|
+
* Type for after agent callbacks, which can be a single callback or
|
|
21
|
+
* an array of callbacks.
|
|
22
|
+
*/
|
|
23
|
+
export type AfterAgentCallback = SingleAgentCallback | SingleAgentCallback[];
|
|
13
24
|
/**
|
|
14
25
|
* The config of a base agent.
|
|
15
26
|
*/
|
|
@@ -15,7 +15,10 @@ import { BaseAgent } from './base_agent.js';
|
|
|
15
15
|
import { LiveRequestQueue } from './live_request_queue.js';
|
|
16
16
|
import { RunConfig } from './run_config.js';
|
|
17
17
|
import { TranscriptionEntry } from './transcription_entry.js';
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* The parameters for creating an invocation context.
|
|
20
|
+
*/
|
|
21
|
+
export interface InvocationContextParams {
|
|
19
22
|
artifactService?: BaseArtifactService;
|
|
20
23
|
sessionService?: BaseSessionService;
|
|
21
24
|
memoryService?: BaseMemoryService;
|
|
@@ -152,4 +155,3 @@ export declare class InvocationContext {
|
|
|
152
155
|
incrementLlmCallCount(): void;
|
|
153
156
|
}
|
|
154
157
|
export declare function newInvocationContextId(): string;
|
|
155
|
-
export {};
|