@google/adk 0.2.5 → 0.3.0
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 +43 -21
- package/dist/cjs/agents/callback_context.js +4 -1
- package/dist/cjs/agents/content_processor_utils.js +15 -7
- package/dist/cjs/agents/functions.js +79 -29
- package/dist/cjs/agents/llm_agent.js +59 -33
- package/dist/cjs/agents/loop_agent.js +2 -1
- package/dist/cjs/agents/parallel_agent.js +3 -4
- package/dist/cjs/artifacts/gcs_artifact_service.js +28 -20
- package/dist/cjs/artifacts/in_memory_artifact_service.js +18 -4
- package/dist/cjs/auth/auth_handler.js +3 -1
- package/dist/cjs/code_executors/base_code_executor.js +3 -1
- package/dist/cjs/code_executors/built_in_code_executor.js +7 -3
- package/dist/cjs/code_executors/code_executor_context.js +5 -5
- package/dist/cjs/common.js +4 -0
- package/dist/cjs/events/event.js +1 -3
- package/dist/cjs/index.js +19 -19
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/memory/in_memory_memory_service.js +3 -1
- package/dist/cjs/models/base_llm.js +8 -4
- package/dist/cjs/models/gemini_llm_connection.js +1 -0
- package/dist/cjs/models/google_llm.js +3 -3
- package/dist/cjs/plugins/base_plugin.js +12 -0
- package/dist/cjs/plugins/logging_plugin.js +50 -13
- package/dist/cjs/plugins/plugin_manager.js +56 -24
- package/dist/cjs/plugins/security_plugin.js +1 -1
- package/dist/cjs/runner/runner.js +110 -95
- package/dist/cjs/sessions/in_memory_session_service.js +38 -14
- package/dist/cjs/telemetry/google_cloud.js +7 -9
- package/dist/cjs/telemetry/setup.js +15 -7
- package/dist/cjs/telemetry/tracing.js +37 -15
- package/dist/cjs/tools/agent_tool.js +8 -4
- package/dist/cjs/tools/base_tool.js +4 -2
- package/dist/cjs/tools/forwarding_artifact_service.js +1 -1
- package/dist/cjs/tools/function_tool.js +1 -2
- package/dist/cjs/tools/google_search_tool.js +1 -2
- package/dist/cjs/tools/mcp/mcp_session_manager.js +16 -10
- package/dist/cjs/tools/mcp/mcp_tool.js +1 -3
- package/dist/cjs/tools/mcp/mcp_toolset.js +1 -1
- package/dist/cjs/utils/env_aware_utils.js +1 -1
- package/dist/cjs/utils/gemini_schema_util.js +9 -4
- package/dist/cjs/utils/logger.js +47 -3
- package/dist/cjs/utils/simple_zod_to_json.js +100 -141
- package/dist/cjs/utils/variant_utils.js +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/agents/base_agent.js +48 -22
- package/dist/esm/agents/callback_context.js +4 -1
- package/dist/esm/agents/content_processor_utils.js +25 -9
- package/dist/esm/agents/functions.js +83 -29
- package/dist/esm/agents/llm_agent.js +63 -33
- package/dist/esm/agents/loop_agent.js +2 -1
- package/dist/esm/agents/parallel_agent.js +3 -4
- package/dist/esm/artifacts/gcs_artifact_service.js +28 -20
- package/dist/esm/artifacts/in_memory_artifact_service.js +18 -4
- package/dist/esm/auth/auth_handler.js +3 -1
- package/dist/esm/code_executors/base_code_executor.js +3 -1
- package/dist/esm/code_executors/built_in_code_executor.js +7 -3
- package/dist/esm/code_executors/code_executor_context.js +5 -5
- package/dist/esm/common.js +3 -1
- package/dist/esm/events/event.js +1 -3
- package/dist/esm/index.js +19 -19
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/memory/in_memory_memory_service.js +3 -1
- package/dist/esm/models/base_llm.js +8 -4
- package/dist/esm/models/gemini_llm_connection.js +1 -0
- package/dist/esm/models/google_llm.js +8 -4
- package/dist/esm/plugins/base_plugin.js +12 -0
- package/dist/esm/plugins/logging_plugin.js +55 -14
- package/dist/esm/plugins/plugin_manager.js +56 -24
- package/dist/esm/plugins/security_plugin.js +1 -1
- package/dist/esm/runner/runner.js +114 -96
- package/dist/esm/sessions/in_memory_session_service.js +41 -15
- package/dist/esm/telemetry/google_cloud.js +7 -9
- package/dist/esm/telemetry/setup.js +23 -9
- package/dist/esm/telemetry/tracing.js +37 -15
- package/dist/esm/tools/agent_tool.js +8 -4
- package/dist/esm/tools/base_tool.js +4 -2
- package/dist/esm/tools/forwarding_artifact_service.js +1 -1
- package/dist/esm/tools/function_tool.js +1 -2
- package/dist/esm/tools/google_search_tool.js +2 -5
- package/dist/esm/tools/long_running_tool.js +3 -1
- package/dist/esm/tools/mcp/mcp_session_manager.js +22 -12
- package/dist/esm/tools/mcp/mcp_tool.js +1 -3
- package/dist/esm/tools/mcp/mcp_toolset.js +1 -1
- package/dist/esm/utils/env_aware_utils.js +1 -1
- package/dist/esm/utils/gemini_schema_util.js +9 -4
- package/dist/esm/utils/logger.js +43 -2
- package/dist/esm/utils/simple_zod_to_json.js +102 -141
- package/dist/esm/utils/variant_utils.js +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/agents/base_agent.d.ts +2 -1
- package/dist/types/agents/callback_context.d.ts +1 -1
- package/dist/types/agents/llm_agent.d.ts +1 -1
- package/dist/types/agents/loop_agent.d.ts +1 -1
- package/dist/types/agents/parallel_agent.d.ts +1 -1
- package/dist/types/artifacts/in_memory_artifact_service.d.ts +3 -3
- package/dist/types/code_executors/built_in_code_executor.d.ts +1 -1
- package/dist/types/code_executors/code_executor_context.d.ts +2 -4
- package/dist/types/common.d.ts +2 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/models/base_llm_connection.d.ts +1 -1
- package/dist/types/models/llm_response.d.ts +1 -1
- package/dist/types/plugins/logging_plugin.d.ts +12 -12
- package/dist/types/plugins/plugin_manager.d.ts +12 -12
- package/dist/types/plugins/security_plugin.d.ts +1 -1
- package/dist/types/runner/runner.d.ts +1 -1
- package/dist/types/sessions/in_memory_session_service.d.ts +5 -5
- package/dist/types/telemetry/setup.d.ts +1 -1
- package/dist/types/telemetry/tracing.d.ts +7 -6
- package/dist/types/tools/agent_tool.d.ts +1 -1
- package/dist/types/tools/base_tool.d.ts +1 -1
- package/dist/types/tools/base_toolset.d.ts +2 -1
- package/dist/types/tools/forwarding_artifact_service.d.ts +2 -2
- package/dist/types/tools/function_tool.d.ts +4 -3
- package/dist/types/tools/google_search_tool.d.ts +3 -3
- package/dist/types/tools/mcp/mcp_session_manager.d.ts +10 -3
- package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -2
- package/dist/types/utils/gemini_schema_util.d.ts +4 -12
- package/dist/types/utils/logger.d.ts +11 -10
- package/dist/types/utils/simple_zod_to_json.d.ts +5 -4
- package/dist/types/version.d.ts +1 -1
- package/dist/web/agents/base_agent.js +94 -33
- package/dist/web/agents/callback_context.js +4 -1
- package/dist/web/agents/content_processor_utils.js +25 -9
- package/dist/web/agents/functions.js +83 -29
- package/dist/web/agents/llm_agent.js +117 -54
- package/dist/web/agents/loop_agent.js +2 -1
- package/dist/web/agents/parallel_agent.js +3 -4
- package/dist/web/artifacts/gcs_artifact_service.js +25 -17
- package/dist/web/artifacts/in_memory_artifact_service.js +18 -4
- package/dist/web/auth/auth_handler.js +3 -1
- package/dist/web/code_executors/base_code_executor.js +3 -1
- package/dist/web/code_executors/built_in_code_executor.js +7 -3
- package/dist/web/code_executors/code_executor_context.js +5 -5
- package/dist/web/common.js +3 -1
- package/dist/web/events/event.js +1 -3
- package/dist/web/index.js +1 -1
- package/dist/web/index.js.map +4 -4
- package/dist/web/memory/in_memory_memory_service.js +3 -1
- package/dist/web/models/base_llm.js +8 -4
- package/dist/web/models/gemini_llm_connection.js +1 -0
- package/dist/web/models/google_llm.js +8 -4
- package/dist/web/plugins/base_plugin.js +12 -0
- package/dist/web/plugins/logging_plugin.js +55 -14
- package/dist/web/plugins/plugin_manager.js +56 -24
- package/dist/web/plugins/security_plugin.js +1 -1
- package/dist/web/runner/runner.js +159 -108
- package/dist/web/sessions/in_memory_session_service.js +41 -15
- package/dist/web/telemetry/google_cloud.js +7 -9
- package/dist/web/telemetry/setup.js +23 -9
- package/dist/web/telemetry/tracing.js +37 -15
- package/dist/web/tools/agent_tool.js +8 -4
- package/dist/web/tools/base_tool.js +4 -2
- package/dist/web/tools/forwarding_artifact_service.js +1 -1
- package/dist/web/tools/function_tool.js +1 -2
- package/dist/web/tools/google_search_tool.js +2 -5
- package/dist/web/tools/long_running_tool.js +3 -1
- package/dist/web/tools/mcp/mcp_session_manager.js +22 -12
- package/dist/web/tools/mcp/mcp_tool.js +1 -3
- package/dist/web/tools/mcp/mcp_toolset.js +1 -1
- package/dist/web/utils/env_aware_utils.js +1 -1
- package/dist/web/utils/gemini_schema_util.js +9 -4
- package/dist/web/utils/logger.js +43 -2
- package/dist/web/utils/simple_zod_to_json.js +102 -155
- package/dist/web/utils/variant_utils.js +1 -1
- package/dist/web/version.js +1 -1
- package/package.json +4 -2
|
@@ -10,6 +10,11 @@ import { mergeEventActions } from "../events/event_actions.js";
|
|
|
10
10
|
import { ToolContext } from "../tools/tool_context.js";
|
|
11
11
|
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
12
12
|
import { logger } from "../utils/logger.js";
|
|
13
|
+
import {
|
|
14
|
+
traceMergedToolCalls,
|
|
15
|
+
tracer,
|
|
16
|
+
traceToolCall
|
|
17
|
+
} from "../telemetry/tracing.js";
|
|
13
18
|
const AF_FUNCTION_CALL_ID_PREFIX = "adk-";
|
|
14
19
|
const REQUEST_EUC_FUNCTION_CALL_NAME = "adk_request_credential";
|
|
15
20
|
const REQUEST_CONFIRMATION_FUNCTION_CALL_NAME = "adk_request_confirmation";
|
|
@@ -127,8 +132,51 @@ function generateRequestConfirmationEvent({
|
|
|
127
132
|
});
|
|
128
133
|
}
|
|
129
134
|
async function callToolAsync(tool, args, toolContext) {
|
|
130
|
-
|
|
131
|
-
|
|
135
|
+
return tracer.startActiveSpan(`execute_tool ${tool.name}`, async (span) => {
|
|
136
|
+
try {
|
|
137
|
+
logger.debug(`callToolAsync ${tool.name}`);
|
|
138
|
+
const result = await tool.runAsync({ args, toolContext });
|
|
139
|
+
traceToolCall({
|
|
140
|
+
tool,
|
|
141
|
+
args,
|
|
142
|
+
functionResponseEvent: buildResponseEvent(
|
|
143
|
+
tool,
|
|
144
|
+
result,
|
|
145
|
+
toolContext,
|
|
146
|
+
toolContext.invocationContext
|
|
147
|
+
)
|
|
148
|
+
});
|
|
149
|
+
return result;
|
|
150
|
+
} finally {
|
|
151
|
+
span.end();
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function buildResponseEvent(tool, functionResult, toolContext, invocationContext) {
|
|
156
|
+
let responseResult;
|
|
157
|
+
if (typeof functionResult !== "object" || functionResult == null) {
|
|
158
|
+
responseResult = { result: functionResult };
|
|
159
|
+
} else {
|
|
160
|
+
responseResult = functionResult;
|
|
161
|
+
}
|
|
162
|
+
const partFunctionResponse = {
|
|
163
|
+
functionResponse: {
|
|
164
|
+
name: tool.name,
|
|
165
|
+
response: responseResult,
|
|
166
|
+
id: toolContext.functionCallId
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
const content = {
|
|
170
|
+
role: "user",
|
|
171
|
+
parts: [partFunctionResponse]
|
|
172
|
+
};
|
|
173
|
+
return createEvent({
|
|
174
|
+
invocationId: invocationContext.invocationId,
|
|
175
|
+
author: invocationContext.agent.name,
|
|
176
|
+
content,
|
|
177
|
+
actions: toolContext.actions,
|
|
178
|
+
branch: invocationContext.branch
|
|
179
|
+
});
|
|
132
180
|
}
|
|
133
181
|
async function handleFunctionCallsAsync({
|
|
134
182
|
invocationContext,
|
|
@@ -169,14 +217,12 @@ async function handleFunctionCallList({
|
|
|
169
217
|
if (toolConfirmationDict && functionCall.id) {
|
|
170
218
|
toolConfirmation = toolConfirmationDict[functionCall.id];
|
|
171
219
|
}
|
|
172
|
-
const { tool, toolContext } = getToolAndContext(
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
);
|
|
220
|
+
const { tool, toolContext } = getToolAndContext({
|
|
221
|
+
invocationContext,
|
|
222
|
+
functionCall,
|
|
223
|
+
toolsDict,
|
|
224
|
+
toolConfirmation
|
|
225
|
+
});
|
|
180
226
|
logger.debug(`execute_tool ${tool.name}`);
|
|
181
227
|
const functionArgs = (_a = functionCall.args) != null ? _a : {};
|
|
182
228
|
let functionResponse = null;
|
|
@@ -200,21 +246,15 @@ async function handleFunctionCallList({
|
|
|
200
246
|
}
|
|
201
247
|
if (functionResponse == null) {
|
|
202
248
|
try {
|
|
203
|
-
functionResponse = await callToolAsync(
|
|
204
|
-
tool,
|
|
205
|
-
functionArgs,
|
|
206
|
-
toolContext
|
|
207
|
-
);
|
|
249
|
+
functionResponse = await callToolAsync(tool, functionArgs, toolContext);
|
|
208
250
|
} catch (e) {
|
|
209
251
|
if (e instanceof Error) {
|
|
210
|
-
const onToolErrorResponse = await invocationContext.pluginManager.runOnToolErrorCallback(
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
);
|
|
252
|
+
const onToolErrorResponse = await invocationContext.pluginManager.runOnToolErrorCallback({
|
|
253
|
+
tool,
|
|
254
|
+
toolArgs: functionArgs,
|
|
255
|
+
toolContext,
|
|
256
|
+
error: e
|
|
257
|
+
});
|
|
218
258
|
if (onToolErrorResponse) {
|
|
219
259
|
functionResponse = onToolErrorResponse;
|
|
220
260
|
} else {
|
|
@@ -278,12 +318,24 @@ async function handleFunctionCallList({
|
|
|
278
318
|
if (!functionResponseEvents.length) {
|
|
279
319
|
return null;
|
|
280
320
|
}
|
|
281
|
-
const mergedEvent = mergeParallelFunctionResponseEvents(
|
|
321
|
+
const mergedEvent = mergeParallelFunctionResponseEvents(
|
|
322
|
+
functionResponseEvents
|
|
323
|
+
);
|
|
282
324
|
if (functionResponseEvents.length > 1) {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
325
|
+
tracer.startActiveSpan("execute_tool (merged)", (span) => {
|
|
326
|
+
try {
|
|
327
|
+
logger.debug("execute_tool (merged)");
|
|
328
|
+
logger.debug("traceMergedToolCalls", {
|
|
329
|
+
responseEventId: mergedEvent.id,
|
|
330
|
+
functionResponseEvent: mergedEvent.id
|
|
331
|
+
});
|
|
332
|
+
traceMergedToolCalls({
|
|
333
|
+
responseEventId: mergedEvent.id,
|
|
334
|
+
functionResponseEvent: mergedEvent
|
|
335
|
+
});
|
|
336
|
+
} finally {
|
|
337
|
+
span.end();
|
|
338
|
+
}
|
|
287
339
|
});
|
|
288
340
|
}
|
|
289
341
|
return mergedEvent;
|
|
@@ -321,7 +373,9 @@ function mergeParallelFunctionResponseEvents(functionResponseEvents) {
|
|
|
321
373
|
}
|
|
322
374
|
}
|
|
323
375
|
const baseEvent = functionResponseEvents[0];
|
|
324
|
-
const actionsList = functionResponseEvents.map(
|
|
376
|
+
const actionsList = functionResponseEvents.map(
|
|
377
|
+
(event) => event.actions || {}
|
|
378
|
+
);
|
|
325
379
|
const mergedActions = mergeEventActions(actionsList);
|
|
326
380
|
return createEvent({
|
|
327
381
|
author: baseEvent.author,
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
var _a, _b;
|
|
7
|
+
import { context, trace } from "@opentelemetry/api";
|
|
7
8
|
import { cloneDeep } from "lodash-es";
|
|
8
9
|
import { z } from "zod";
|
|
9
10
|
import {
|
|
@@ -38,6 +39,11 @@ import { ToolConfirmation } from "../tools/tool_confirmation.js";
|
|
|
38
39
|
import { ToolContext } from "../tools/tool_context.js";
|
|
39
40
|
import { base64Decode } from "../utils/env_aware_utils.js";
|
|
40
41
|
import { logger } from "../utils/logger.js";
|
|
42
|
+
import {
|
|
43
|
+
runAsyncGeneratorWithOtelContext,
|
|
44
|
+
traceCallLlm,
|
|
45
|
+
tracer
|
|
46
|
+
} from "../telemetry/tracing.js";
|
|
41
47
|
import { BaseAgent } from "./base_agent.js";
|
|
42
48
|
import {
|
|
43
49
|
BaseLlmRequestProcessor
|
|
@@ -60,13 +66,14 @@ import { injectSessionState } from "./instructions.js";
|
|
|
60
66
|
import { ReadonlyContext } from "./readonly_context.js";
|
|
61
67
|
import { StreamingMode } from "./run_config.js";
|
|
62
68
|
const ADK_AGENT_NAME_LABEL_KEY = "adk_agent_name";
|
|
63
|
-
async function convertToolUnionToTools(toolUnion,
|
|
69
|
+
async function convertToolUnionToTools(toolUnion, context2) {
|
|
64
70
|
if (toolUnion instanceof BaseTool) {
|
|
65
71
|
return [toolUnion];
|
|
66
72
|
}
|
|
67
|
-
return await toolUnion.getTools(
|
|
73
|
+
return await toolUnion.getTools(context2);
|
|
68
74
|
}
|
|
69
75
|
class BasicLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
76
|
+
// eslint-disable-next-line require-yield
|
|
70
77
|
async *runAsync(invocationContext, llmRequest) {
|
|
71
78
|
var _a2;
|
|
72
79
|
const agent = invocationContext.agent;
|
|
@@ -91,6 +98,7 @@ class BasicLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
91
98
|
}
|
|
92
99
|
const BASIC_LLM_REQUEST_PROCESSOR = new BasicLlmRequestProcessor();
|
|
93
100
|
class IdentityLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
101
|
+
// eslint-disable-next-line require-yield
|
|
94
102
|
async *runAsync(invocationContext, llmRequest) {
|
|
95
103
|
const agent = invocationContext.agent;
|
|
96
104
|
const si = [`You are an agent. Your internal name is "${agent.name}".`];
|
|
@@ -105,6 +113,7 @@ class InstructionsLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
105
113
|
/**
|
|
106
114
|
* Handles instructions and global instructions for LLM flow.
|
|
107
115
|
*/
|
|
116
|
+
// eslint-disable-next-line require-yield
|
|
108
117
|
async *runAsync(invocationContext, llmRequest) {
|
|
109
118
|
const agent = invocationContext.agent;
|
|
110
119
|
if (!(agent instanceof LlmAgent) || !(agent.rootAgent instanceof LlmAgent)) {
|
|
@@ -141,6 +150,7 @@ class InstructionsLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
141
150
|
}
|
|
142
151
|
const INSTRUCTIONS_LLM_REQUEST_PROCESSOR = new InstructionsLlmRequestProcessor();
|
|
143
152
|
class ContentRequestProcessor {
|
|
153
|
+
// eslint-disable-next-line require-yield
|
|
144
154
|
async *runAsync(invocationContext, llmRequest) {
|
|
145
155
|
const agent = invocationContext.agent;
|
|
146
156
|
if (!agent || !isLlmAgent(agent)) {
|
|
@@ -182,6 +192,7 @@ class AgentTransferLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
182
192
|
}
|
|
183
193
|
});
|
|
184
194
|
}
|
|
195
|
+
// eslint-disable-next-line require-yield
|
|
185
196
|
async *runAsync(invocationContext, llmRequest) {
|
|
186
197
|
if (!(invocationContext.agent instanceof LlmAgent)) {
|
|
187
198
|
return;
|
|
@@ -250,7 +261,7 @@ to your parent agent.
|
|
|
250
261
|
const AGENT_TRANSFER_LLM_REQUEST_PROCESSOR = new AgentTransferLlmRequestProcessor();
|
|
251
262
|
class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
252
263
|
/** Handles tool confirmation information to build the LLM request. */
|
|
253
|
-
async *runAsync(invocationContext
|
|
264
|
+
async *runAsync(invocationContext) {
|
|
254
265
|
const agent = invocationContext.agent;
|
|
255
266
|
if (!isLlmAgent(agent)) {
|
|
256
267
|
return;
|
|
@@ -383,7 +394,7 @@ class CodeExecutionRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
383
394
|
}
|
|
384
395
|
for (const content of llmRequest.contents) {
|
|
385
396
|
const delimeters = invocationContext.agent.codeExecutor.codeBlockDelimiters.length ? invocationContext.agent.codeExecutor.codeBlockDelimiters[0] : ["", ""];
|
|
386
|
-
|
|
397
|
+
convertCodeExecutionParts(
|
|
387
398
|
content,
|
|
388
399
|
delimeters,
|
|
389
400
|
invocationContext.agent.codeExecutor.executionResultDelimiters
|
|
@@ -593,7 +604,7 @@ async function* runPostProcessor(invocationContext, llmResponse) {
|
|
|
593
604
|
codeExecutorContext,
|
|
594
605
|
codeExecutionResult
|
|
595
606
|
);
|
|
596
|
-
llmResponse.content =
|
|
607
|
+
llmResponse.content = void 0;
|
|
597
608
|
}
|
|
598
609
|
function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
599
610
|
var _a2;
|
|
@@ -806,12 +817,12 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
806
817
|
* @param context The context to retrieve the session state.
|
|
807
818
|
* @returns The resolved instruction field.
|
|
808
819
|
*/
|
|
809
|
-
async canonicalInstruction(
|
|
820
|
+
async canonicalInstruction(context2) {
|
|
810
821
|
if (typeof this.instruction === "string") {
|
|
811
822
|
return { instruction: this.instruction, requireStateInjection: true };
|
|
812
823
|
}
|
|
813
824
|
return {
|
|
814
|
-
instruction: await this.instruction(
|
|
825
|
+
instruction: await this.instruction(context2),
|
|
815
826
|
requireStateInjection: false
|
|
816
827
|
};
|
|
817
828
|
}
|
|
@@ -822,12 +833,15 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
822
833
|
* @param context The context to retrieve the session state.
|
|
823
834
|
* @returns The resolved globalInstruction field.
|
|
824
835
|
*/
|
|
825
|
-
async canonicalGlobalInstruction(
|
|
836
|
+
async canonicalGlobalInstruction(context2) {
|
|
826
837
|
if (typeof this.globalInstruction === "string") {
|
|
827
|
-
return {
|
|
838
|
+
return {
|
|
839
|
+
instruction: this.globalInstruction,
|
|
840
|
+
requireStateInjection: true
|
|
841
|
+
};
|
|
828
842
|
}
|
|
829
843
|
return {
|
|
830
|
-
instruction: await this.globalInstruction(
|
|
844
|
+
instruction: await this.globalInstruction(context2),
|
|
831
845
|
requireStateInjection: false
|
|
832
846
|
};
|
|
833
847
|
}
|
|
@@ -836,10 +850,10 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
836
850
|
*
|
|
837
851
|
* This method is only for use by Agent Development Kit.
|
|
838
852
|
*/
|
|
839
|
-
async canonicalTools(
|
|
853
|
+
async canonicalTools(context2) {
|
|
840
854
|
const resolvedTools = [];
|
|
841
855
|
for (const toolUnion of this.tools) {
|
|
842
|
-
const tools = await convertToolUnionToTools(toolUnion,
|
|
856
|
+
const tools = await convertToolUnionToTools(toolUnion, context2);
|
|
843
857
|
resolvedTools.push(...tools);
|
|
844
858
|
}
|
|
845
859
|
return resolvedTools;
|
|
@@ -943,10 +957,10 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
943
957
|
}
|
|
944
958
|
event.actions.stateDelta[this.outputKey] = result;
|
|
945
959
|
}
|
|
946
|
-
async *runAsyncImpl(
|
|
960
|
+
async *runAsyncImpl(context2) {
|
|
947
961
|
while (true) {
|
|
948
962
|
let lastEvent = void 0;
|
|
949
|
-
for await (const event of this.runOneStepAsync(
|
|
963
|
+
for await (const event of this.runOneStepAsync(context2)) {
|
|
950
964
|
lastEvent = event;
|
|
951
965
|
this.maybeSaveOutputToState(event);
|
|
952
966
|
yield event;
|
|
@@ -960,19 +974,20 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
960
974
|
}
|
|
961
975
|
}
|
|
962
976
|
}
|
|
963
|
-
async *runLiveImpl(
|
|
964
|
-
for await (const event of this.runLiveFlow(
|
|
977
|
+
async *runLiveImpl(context2) {
|
|
978
|
+
for await (const event of this.runLiveFlow(context2)) {
|
|
965
979
|
this.maybeSaveOutputToState(event);
|
|
966
980
|
yield event;
|
|
967
981
|
}
|
|
968
|
-
if (
|
|
982
|
+
if (context2.endInvocation) {
|
|
969
983
|
return;
|
|
970
984
|
}
|
|
971
985
|
}
|
|
972
986
|
// --------------------------------------------------------------------------
|
|
973
987
|
// #START LlmFlow Logic
|
|
974
988
|
// --------------------------------------------------------------------------
|
|
975
|
-
|
|
989
|
+
// eslint-disable-next-line require-yield
|
|
990
|
+
async *runLiveFlow(_invocationContext) {
|
|
976
991
|
await Promise.resolve();
|
|
977
992
|
throw new Error("LlmAgent.runLiveFlow not implemented");
|
|
978
993
|
}
|
|
@@ -1008,22 +1023,31 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1008
1023
|
author: this.name,
|
|
1009
1024
|
branch: invocationContext.branch
|
|
1010
1025
|
});
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1026
|
+
const span = tracer.startSpan("call_llm");
|
|
1027
|
+
const ctx = trace.setSpan(context.active(), span);
|
|
1028
|
+
yield* runAsyncGeneratorWithOtelContext(
|
|
1029
|
+
ctx,
|
|
1030
|
+
this,
|
|
1031
|
+
async function* () {
|
|
1032
|
+
for await (const llmResponse of this.callLlmAsync(
|
|
1033
|
+
invocationContext,
|
|
1034
|
+
llmRequest,
|
|
1035
|
+
modelResponseEvent
|
|
1036
|
+
)) {
|
|
1037
|
+
for await (const event of this.postprocess(
|
|
1038
|
+
invocationContext,
|
|
1039
|
+
llmRequest,
|
|
1040
|
+
llmResponse,
|
|
1041
|
+
modelResponseEvent
|
|
1042
|
+
)) {
|
|
1043
|
+
modelResponseEvent.id = createNewEventId();
|
|
1044
|
+
modelResponseEvent.timestamp = (/* @__PURE__ */ new Date()).getTime();
|
|
1045
|
+
yield event;
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1025
1048
|
}
|
|
1026
|
-
|
|
1049
|
+
);
|
|
1050
|
+
span.end();
|
|
1027
1051
|
}
|
|
1028
1052
|
async *postprocess(invocationContext, llmRequest, llmResponse, modelResponseEvent) {
|
|
1029
1053
|
var _a2;
|
|
@@ -1141,6 +1165,12 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1141
1165
|
llmRequest,
|
|
1142
1166
|
modelResponseEvent
|
|
1143
1167
|
)) {
|
|
1168
|
+
traceCallLlm({
|
|
1169
|
+
invocationContext,
|
|
1170
|
+
eventId: modelResponseEvent.id,
|
|
1171
|
+
llmRequest,
|
|
1172
|
+
llmResponse
|
|
1173
|
+
});
|
|
1144
1174
|
const alteredLlmResponse = await this.handleAfterModelCallback(
|
|
1145
1175
|
invocationContext,
|
|
1146
1176
|
llmResponse,
|
|
@@ -38,7 +38,8 @@ class LoopAgent extends (_b = BaseAgent, _a = LOOP_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
38
38
|
}
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
|
-
|
|
41
|
+
// eslint-disable-next-line require-yield
|
|
42
|
+
async *runLiveImpl(_context) {
|
|
42
43
|
throw new Error("This is not supported yet for LoopAgent.");
|
|
43
44
|
}
|
|
44
45
|
}
|
|
@@ -20,15 +20,14 @@ class ParallelAgent extends (_b = BaseAgent, _a = PARALLEL_AGENT_SIGNATURE_SYMBO
|
|
|
20
20
|
}
|
|
21
21
|
async *runAsyncImpl(context) {
|
|
22
22
|
const agentRuns = this.subAgents.map(
|
|
23
|
-
(subAgent) => subAgent.runAsync(
|
|
24
|
-
createBranchCtxForSubAgent(this, subAgent, context)
|
|
25
|
-
)
|
|
23
|
+
(subAgent) => subAgent.runAsync(createBranchCtxForSubAgent(this, subAgent, context))
|
|
26
24
|
);
|
|
27
25
|
for await (const event of mergeAgentRuns(agentRuns)) {
|
|
28
26
|
yield event;
|
|
29
27
|
}
|
|
30
28
|
}
|
|
31
|
-
|
|
29
|
+
// eslint-disable-next-line require-yield
|
|
30
|
+
async *runLiveImpl(_context) {
|
|
32
31
|
throw new Error("This is not supported yet for ParallelAgent.");
|
|
33
32
|
}
|
|
34
33
|
}
|
|
@@ -12,10 +12,12 @@ class GcsArtifactService {
|
|
|
12
12
|
async saveArtifact(request) {
|
|
13
13
|
const versions = await this.listVersions(request);
|
|
14
14
|
const version = versions.length > 0 ? Math.max(...versions) + 1 : 0;
|
|
15
|
-
const file = this.bucket.file(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
const file = this.bucket.file(
|
|
16
|
+
getFileName({
|
|
17
|
+
...request,
|
|
18
|
+
version
|
|
19
|
+
})
|
|
20
|
+
);
|
|
19
21
|
if (request.artifact.inlineData) {
|
|
20
22
|
await file.save(JSON.stringify(request.artifact.inlineData.data), {
|
|
21
23
|
contentType: request.artifact.inlineData.mimeType
|
|
@@ -39,11 +41,16 @@ class GcsArtifactService {
|
|
|
39
41
|
}
|
|
40
42
|
version = Math.max(...versions);
|
|
41
43
|
}
|
|
42
|
-
const file = this.bucket.file(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
const file = this.bucket.file(
|
|
45
|
+
getFileName({
|
|
46
|
+
...request,
|
|
47
|
+
version
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
const [[metadata], [rawDataBuffer]] = await Promise.all([
|
|
51
|
+
file.getMetadata(),
|
|
52
|
+
file.download()
|
|
53
|
+
]);
|
|
47
54
|
if (metadata.contentType === "text/plain") {
|
|
48
55
|
return createPartFromText(rawDataBuffer.toString("utf-8"));
|
|
49
56
|
}
|
|
@@ -56,10 +63,7 @@ class GcsArtifactService {
|
|
|
56
63
|
const fileNames = [];
|
|
57
64
|
const sessionPrefix = `${request.appName}/${request.userId}/${request.sessionId}/`;
|
|
58
65
|
const usernamePrefix = `${request.appName}/${request.userId}/user/`;
|
|
59
|
-
const [
|
|
60
|
-
[sessionFiles],
|
|
61
|
-
[userSessionFiles]
|
|
62
|
-
] = await Promise.all([
|
|
66
|
+
const [[sessionFiles], [userSessionFiles]] = await Promise.all([
|
|
63
67
|
this.bucket.getFiles({ prefix: sessionPrefix }),
|
|
64
68
|
this.bucket.getFiles({ prefix: usernamePrefix })
|
|
65
69
|
]);
|
|
@@ -73,13 +77,17 @@ class GcsArtifactService {
|
|
|
73
77
|
}
|
|
74
78
|
async deleteArtifact(request) {
|
|
75
79
|
const versions = await this.listVersions(request);
|
|
76
|
-
await Promise.all(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
await Promise.all(
|
|
81
|
+
versions.map((version) => {
|
|
82
|
+
const file = this.bucket.file(
|
|
83
|
+
getFileName({
|
|
84
|
+
...request,
|
|
85
|
+
version
|
|
86
|
+
})
|
|
87
|
+
);
|
|
88
|
+
return file.delete();
|
|
89
|
+
})
|
|
90
|
+
);
|
|
83
91
|
return;
|
|
84
92
|
}
|
|
85
93
|
async listVersions(request) {
|
|
@@ -39,7 +39,11 @@ class InMemoryArtifactService {
|
|
|
39
39
|
}
|
|
40
40
|
return Promise.resolve(versions[version]);
|
|
41
41
|
}
|
|
42
|
-
listArtifactKeys({
|
|
42
|
+
listArtifactKeys({
|
|
43
|
+
appName,
|
|
44
|
+
userId,
|
|
45
|
+
sessionId
|
|
46
|
+
}) {
|
|
43
47
|
const sessionPrefix = `${appName}/${userId}/${sessionId}/`;
|
|
44
48
|
const usernamespacePrefix = `${appName}/${userId}/user/`;
|
|
45
49
|
const filenames = [];
|
|
@@ -54,7 +58,12 @@ class InMemoryArtifactService {
|
|
|
54
58
|
}
|
|
55
59
|
return Promise.resolve(filenames.sort());
|
|
56
60
|
}
|
|
57
|
-
deleteArtifact({
|
|
61
|
+
deleteArtifact({
|
|
62
|
+
appName,
|
|
63
|
+
userId,
|
|
64
|
+
sessionId,
|
|
65
|
+
filename
|
|
66
|
+
}) {
|
|
58
67
|
const path = artifactPath(appName, userId, sessionId, filename);
|
|
59
68
|
if (!this.artifacts[path]) {
|
|
60
69
|
return Promise.resolve();
|
|
@@ -62,13 +71,18 @@ class InMemoryArtifactService {
|
|
|
62
71
|
delete this.artifacts[path];
|
|
63
72
|
return Promise.resolve();
|
|
64
73
|
}
|
|
65
|
-
listVersions({
|
|
74
|
+
listVersions({
|
|
75
|
+
appName,
|
|
76
|
+
userId,
|
|
77
|
+
sessionId,
|
|
78
|
+
filename
|
|
79
|
+
}) {
|
|
66
80
|
const path = artifactPath(appName, userId, sessionId, filename);
|
|
67
81
|
const artifacts = this.artifacts[path];
|
|
68
82
|
if (!artifacts) {
|
|
69
83
|
return Promise.resolve([]);
|
|
70
84
|
}
|
|
71
|
-
|
|
85
|
+
const versions = [];
|
|
72
86
|
for (let i = 0; i < artifacts.length; i++) {
|
|
73
87
|
versions.push(i);
|
|
74
88
|
}
|
|
@@ -37,7 +37,9 @@ class AuthHandler {
|
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
if (!this.authConfig.rawAuthCredential.oauth2.clientId || !this.authConfig.rawAuthCredential.oauth2.clientSecret) {
|
|
40
|
-
throw new Error(
|
|
40
|
+
throw new Error(
|
|
41
|
+
`Auth Scheme ${authSchemeType} requires both clientId and clientSecret in authCredential.oauth2.`
|
|
42
|
+
);
|
|
41
43
|
}
|
|
42
44
|
return {
|
|
43
45
|
credentialKey: this.authConfig.credentialKey,
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
var _a;
|
|
7
|
-
const BASE_CODE_EXECUTOR_SIGNATURE_SYMBOL = Symbol.for(
|
|
7
|
+
const BASE_CODE_EXECUTOR_SIGNATURE_SYMBOL = Symbol.for(
|
|
8
|
+
"google.adk.baseCodeExecutor"
|
|
9
|
+
);
|
|
8
10
|
function isBaseCodeExecutor(obj) {
|
|
9
11
|
return typeof obj === "object" && obj !== null && BASE_CODE_EXECUTOR_SIGNATURE_SYMBOL in obj && obj[BASE_CODE_EXECUTOR_SIGNATURE_SYMBOL] === true;
|
|
10
12
|
}
|
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
var _a, _b;
|
|
7
7
|
import { isGemini2OrAbove } from "../utils/model_name.js";
|
|
8
8
|
import { BaseCodeExecutor } from "./base_code_executor.js";
|
|
9
|
-
const BUILT_IN_CODE_EXECUTOR_SIGNATURE_SYMBOL = Symbol.for(
|
|
9
|
+
const BUILT_IN_CODE_EXECUTOR_SIGNATURE_SYMBOL = Symbol.for(
|
|
10
|
+
"google.adk.builtInCodeExecutor"
|
|
11
|
+
);
|
|
10
12
|
function isBuiltInCodeExecutor(obj) {
|
|
11
13
|
return typeof obj === "object" && obj !== null && BUILT_IN_CODE_EXECUTOR_SIGNATURE_SYMBOL in obj && obj[BUILT_IN_CODE_EXECUTOR_SIGNATURE_SYMBOL] === true;
|
|
12
14
|
}
|
|
@@ -16,7 +18,7 @@ class BuiltInCodeExecutor extends (_b = BaseCodeExecutor, _a = BUILT_IN_CODE_EXE
|
|
|
16
18
|
/** A unique symbol to identify BuiltInCodeExecutor class. */
|
|
17
19
|
this[_a] = true;
|
|
18
20
|
}
|
|
19
|
-
executeCode(
|
|
21
|
+
executeCode(_params) {
|
|
20
22
|
return Promise.resolve({
|
|
21
23
|
stdout: "",
|
|
22
24
|
stderr: "",
|
|
@@ -30,7 +32,9 @@ class BuiltInCodeExecutor extends (_b = BaseCodeExecutor, _a = BUILT_IN_CODE_EXE
|
|
|
30
32
|
llmRequest.config.tools.push({ codeExecution: {} });
|
|
31
33
|
return;
|
|
32
34
|
}
|
|
33
|
-
throw new Error(
|
|
35
|
+
throw new Error(
|
|
36
|
+
`Gemini code execution tool is not supported for model ${llmRequest.model}`
|
|
37
|
+
);
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
40
|
export {
|
|
@@ -142,7 +142,9 @@ class CodeExecutorContext {
|
|
|
142
142
|
if (!(CODE_EXECUTION_RESULTS_KEY in this.sessionState)) {
|
|
143
143
|
this.sessionState.set(CODE_EXECUTION_RESULTS_KEY, {});
|
|
144
144
|
}
|
|
145
|
-
const codeExecutionResults = this.sessionState.get(
|
|
145
|
+
const codeExecutionResults = this.sessionState.get(
|
|
146
|
+
CODE_EXECUTION_RESULTS_KEY
|
|
147
|
+
);
|
|
146
148
|
if (!(invocationId in codeExecutionResults)) {
|
|
147
149
|
codeExecutionResults[invocationId] = [];
|
|
148
150
|
}
|
|
@@ -154,12 +156,10 @@ class CodeExecutorContext {
|
|
|
154
156
|
});
|
|
155
157
|
}
|
|
156
158
|
/**
|
|
157
|
-
* Gets the code
|
|
158
|
-
* @param invocationId The session state to get the code executor context
|
|
159
|
-
* from.
|
|
159
|
+
* Gets the code execution context from the session state.
|
|
160
160
|
* @return The code execution context for the given invocation ID.
|
|
161
161
|
*/
|
|
162
|
-
getCodeExecutionContext(
|
|
162
|
+
getCodeExecutionContext() {
|
|
163
163
|
return this.sessionState.get(CONTEXT_KEY) || {};
|
|
164
164
|
}
|
|
165
165
|
}
|
package/dist/esm/common.js
CHANGED
|
@@ -63,7 +63,7 @@ import { GOOGLE_SEARCH, GoogleSearchTool } from "./tools/google_search_tool.js";
|
|
|
63
63
|
import { LongRunningFunctionTool } from "./tools/long_running_tool.js";
|
|
64
64
|
import { ToolConfirmation } from "./tools/tool_confirmation.js";
|
|
65
65
|
import { ToolContext } from "./tools/tool_context.js";
|
|
66
|
-
import { LogLevel, setLogLevel } from "./utils/logger.js";
|
|
66
|
+
import { LogLevel, getLogger, setLogLevel, setLogger } from "./utils/logger.js";
|
|
67
67
|
import { isGemini2OrAbove } from "./utils/model_name.js";
|
|
68
68
|
import { zodObjectToSchema } from "./utils/simple_zod_to_json.js";
|
|
69
69
|
import { GoogleLLMVariant } from "./utils/variant_utils.js";
|
|
@@ -124,6 +124,7 @@ export {
|
|
|
124
124
|
getAskUserConfirmationFunctionCalls,
|
|
125
125
|
getFunctionCalls,
|
|
126
126
|
getFunctionResponses,
|
|
127
|
+
getLogger,
|
|
127
128
|
hasTrailingCodeExecutionResult,
|
|
128
129
|
isAgentTool,
|
|
129
130
|
isBaseAgent,
|
|
@@ -138,6 +139,7 @@ export {
|
|
|
138
139
|
isParallelAgent,
|
|
139
140
|
isSequentialAgent,
|
|
140
141
|
setLogLevel,
|
|
142
|
+
setLogger,
|
|
141
143
|
stringifyContent,
|
|
142
144
|
version,
|
|
143
145
|
zodObjectToSchema
|
package/dist/esm/events/event.js
CHANGED
|
@@ -66,9 +66,7 @@ const ASCII_LETTERS_AND_NUMBERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS
|
|
|
66
66
|
function createNewEventId() {
|
|
67
67
|
let id = "";
|
|
68
68
|
for (let i = 0; i < 8; i++) {
|
|
69
|
-
id += ASCII_LETTERS_AND_NUMBERS[Math.floor(
|
|
70
|
-
Math.random() * ASCII_LETTERS_AND_NUMBERS.length
|
|
71
|
-
)];
|
|
69
|
+
id += ASCII_LETTERS_AND_NUMBERS[Math.floor(Math.random() * ASCII_LETTERS_AND_NUMBERS.length)];
|
|
72
70
|
}
|
|
73
71
|
return id;
|
|
74
72
|
}
|