@google/adk 0.2.4 → 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 +52 -24
- 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/invocation_context.js +3 -1
- package/dist/cjs/agents/llm_agent.js +188 -108
- package/dist/cjs/agents/loop_agent.js +18 -6
- package/dist/cjs/agents/parallel_agent.js +20 -7
- package/dist/cjs/agents/readonly_context.js +4 -1
- package/dist/cjs/agents/sequential_agent.js +34 -12
- 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 +14 -2
- package/dist/cjs/code_executors/built_in_code_executor.js +21 -5
- package/dist/cjs/code_executors/code_executor_context.js +5 -5
- package/dist/cjs/common.js +45 -0
- package/dist/cjs/events/event.js +1 -3
- package/dist/cjs/examples/base_example_provider.js +18 -2
- package/dist/cjs/examples/example_util.js +1 -1
- 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 +71 -49
- 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 +5 -2
- package/dist/cjs/runner/runner.js +126 -101
- 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 +26 -13
- package/dist/cjs/tools/base_tool.js +19 -7
- package/dist/cjs/tools/forwarding_artifact_service.js +1 -1
- package/dist/cjs/tools/function_tool.js +15 -7
- package/dist/cjs/tools/google_search_tool.js +8 -4
- 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/tools/tool_context.js +4 -9
- package/dist/cjs/utils/env_aware_utils.js +1 -1
- package/dist/cjs/utils/gemini_schema_util.js +10 -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 +57 -25
- 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/invocation_context.js +3 -1
- package/dist/esm/agents/llm_agent.js +228 -116
- package/dist/esm/agents/loop_agent.js +16 -5
- package/dist/esm/agents/parallel_agent.js +18 -6
- package/dist/esm/agents/readonly_context.js +4 -1
- package/dist/esm/agents/sequential_agent.js +33 -12
- 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 +12 -1
- package/dist/esm/code_executors/built_in_code_executor.js +19 -4
- package/dist/esm/code_executors/code_executor_context.js +5 -5
- package/dist/esm/common.js +56 -11
- package/dist/esm/events/event.js +1 -3
- package/dist/esm/examples/base_example_provider.js +16 -1
- package/dist/esm/examples/example_util.js +4 -2
- 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 +71 -49
- 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 +5 -2
- package/dist/esm/runner/runner.js +139 -105
- 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 +25 -13
- package/dist/esm/tools/base_tool.js +17 -6
- package/dist/esm/tools/forwarding_artifact_service.js +1 -1
- package/dist/esm/tools/function_tool.js +13 -8
- package/dist/esm/tools/google_search_tool.js +6 -3
- 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/tools/tool_context.js +4 -9
- package/dist/esm/utils/env_aware_utils.js +1 -1
- package/dist/esm/utils/gemini_schema_util.js +10 -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 +16 -4
- package/dist/types/agents/callback_context.d.ts +1 -1
- package/dist/types/agents/invocation_context.d.ts +4 -2
- package/dist/types/agents/llm_agent.d.ts +43 -31
- package/dist/types/agents/loop_agent.d.ts +17 -1
- package/dist/types/agents/parallel_agent.d.ts +17 -1
- package/dist/types/agents/sequential_agent.d.ts +17 -1
- package/dist/types/artifacts/in_memory_artifact_service.d.ts +3 -3
- 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 +20 -1
- package/dist/types/code_executors/code_executor_context.d.ts +2 -4
- package/dist/types/common.d.ts +40 -15
- package/dist/types/examples/base_example_provider.d.ts +16 -0
- 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/base_plugin.d.ts +50 -40
- 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 +18 -10
- 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 +15 -1
- package/dist/types/tools/base_tool.d.ts +15 -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 +18 -4
- package/dist/types/tools/google_search_tool.d.ts +5 -6
- 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/tools/tool_context.d.ts +1 -1
- 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 +103 -36
- 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/invocation_context.js +3 -1
- package/dist/web/agents/llm_agent.js +282 -137
- package/dist/web/agents/loop_agent.js +16 -5
- package/dist/web/agents/parallel_agent.js +18 -6
- package/dist/web/agents/readonly_context.js +4 -1
- package/dist/web/agents/sequential_agent.js +33 -12
- 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 +12 -1
- package/dist/web/code_executors/built_in_code_executor.js +19 -4
- package/dist/web/code_executors/code_executor_context.js +5 -5
- package/dist/web/common.js +56 -11
- package/dist/web/events/event.js +1 -3
- 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/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 +71 -49
- 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 +5 -2
- package/dist/web/runner/runner.js +186 -119
- 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 +25 -13
- package/dist/web/tools/base_tool.js +17 -6
- package/dist/web/tools/forwarding_artifact_service.js +1 -1
- package/dist/web/tools/function_tool.js +13 -8
- package/dist/web/tools/google_search_tool.js +6 -3
- 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/tools/tool_context.js +4 -9
- package/dist/web/utils/env_aware_utils.js +1 -1
- package/dist/web/utils/gemini_schema_util.js +10 -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 +5 -3
|
@@ -3,16 +3,34 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
+
var _a, _b;
|
|
7
|
+
import { context, trace } from "@opentelemetry/api";
|
|
6
8
|
import { cloneDeep } from "lodash-es";
|
|
7
9
|
import { z } from "zod";
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
import {
|
|
11
|
+
isBaseCodeExecutor
|
|
12
|
+
} from "../code_executors/base_code_executor.js";
|
|
13
|
+
import { isBuiltInCodeExecutor } from "../code_executors/built_in_code_executor.js";
|
|
14
|
+
import {
|
|
15
|
+
buildCodeExecutionResultPart,
|
|
16
|
+
buildExecutableCodePart,
|
|
17
|
+
convertCodeExecutionParts,
|
|
18
|
+
extractCodeAndTruncateContent
|
|
19
|
+
} from "../code_executors/code_execution_utils.js";
|
|
11
20
|
import { CodeExecutorContext } from "../code_executors/code_executor_context.js";
|
|
12
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
createEvent,
|
|
23
|
+
createNewEventId,
|
|
24
|
+
getFunctionCalls,
|
|
25
|
+
getFunctionResponses,
|
|
26
|
+
isFinalResponse
|
|
27
|
+
} from "../events/event.js";
|
|
13
28
|
import { createEventActions } from "../events/event_actions.js";
|
|
14
29
|
import { isBaseLlm } from "../models/base_llm.js";
|
|
15
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
appendInstructions,
|
|
32
|
+
setOutputSchema
|
|
33
|
+
} from "../models/llm_request.js";
|
|
16
34
|
import { LLMRegistry } from "../models/registry.js";
|
|
17
35
|
import { State } from "../sessions/state.js";
|
|
18
36
|
import { BaseTool } from "../tools/base_tool.js";
|
|
@@ -21,30 +39,49 @@ import { ToolConfirmation } from "../tools/tool_confirmation.js";
|
|
|
21
39
|
import { ToolContext } from "../tools/tool_context.js";
|
|
22
40
|
import { base64Decode } from "../utils/env_aware_utils.js";
|
|
23
41
|
import { logger } from "../utils/logger.js";
|
|
42
|
+
import {
|
|
43
|
+
runAsyncGeneratorWithOtelContext,
|
|
44
|
+
traceCallLlm,
|
|
45
|
+
tracer
|
|
46
|
+
} from "../telemetry/tracing.js";
|
|
24
47
|
import { BaseAgent } from "./base_agent.js";
|
|
25
|
-
import {
|
|
48
|
+
import {
|
|
49
|
+
BaseLlmRequestProcessor
|
|
50
|
+
} from "./base_llm_processor.js";
|
|
26
51
|
import { CallbackContext } from "./callback_context.js";
|
|
27
|
-
import {
|
|
28
|
-
|
|
52
|
+
import {
|
|
53
|
+
getContents,
|
|
54
|
+
getCurrentTurnContents
|
|
55
|
+
} from "./content_processor_utils.js";
|
|
56
|
+
import {
|
|
57
|
+
generateAuthEvent,
|
|
58
|
+
generateRequestConfirmationEvent,
|
|
59
|
+
getLongRunningFunctionCalls,
|
|
60
|
+
handleFunctionCallList,
|
|
61
|
+
handleFunctionCallsAsync,
|
|
62
|
+
populateClientFunctionCallId,
|
|
63
|
+
REQUEST_CONFIRMATION_FUNCTION_CALL_NAME
|
|
64
|
+
} from "./functions.js";
|
|
29
65
|
import { injectSessionState } from "./instructions.js";
|
|
30
66
|
import { ReadonlyContext } from "./readonly_context.js";
|
|
31
67
|
import { StreamingMode } from "./run_config.js";
|
|
32
68
|
const ADK_AGENT_NAME_LABEL_KEY = "adk_agent_name";
|
|
33
|
-
async function convertToolUnionToTools(toolUnion,
|
|
69
|
+
async function convertToolUnionToTools(toolUnion, context2) {
|
|
34
70
|
if (toolUnion instanceof BaseTool) {
|
|
35
71
|
return [toolUnion];
|
|
36
72
|
}
|
|
37
|
-
return await toolUnion.getTools(
|
|
73
|
+
return await toolUnion.getTools(context2);
|
|
38
74
|
}
|
|
39
75
|
class BasicLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
76
|
+
// eslint-disable-next-line require-yield
|
|
40
77
|
async *runAsync(invocationContext, llmRequest) {
|
|
41
|
-
var
|
|
78
|
+
var _a2;
|
|
42
79
|
const agent = invocationContext.agent;
|
|
43
|
-
if (!(agent
|
|
80
|
+
if (!isLlmAgent(agent)) {
|
|
44
81
|
return;
|
|
45
82
|
}
|
|
46
83
|
llmRequest.model = agent.canonicalModel.model;
|
|
47
|
-
llmRequest.config = { ...(
|
|
84
|
+
llmRequest.config = { ...(_a2 = agent.generateContentConfig) != null ? _a2 : {} };
|
|
48
85
|
if (agent.outputSchema) {
|
|
49
86
|
setOutputSchema(llmRequest, agent.outputSchema);
|
|
50
87
|
}
|
|
@@ -61,6 +98,7 @@ class BasicLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
61
98
|
}
|
|
62
99
|
const BASIC_LLM_REQUEST_PROCESSOR = new BasicLlmRequestProcessor();
|
|
63
100
|
class IdentityLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
101
|
+
// eslint-disable-next-line require-yield
|
|
64
102
|
async *runAsync(invocationContext, llmRequest) {
|
|
65
103
|
const agent = invocationContext.agent;
|
|
66
104
|
const si = [`You are an agent. Your internal name is "${agent.name}".`];
|
|
@@ -75,13 +113,14 @@ class InstructionsLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
75
113
|
/**
|
|
76
114
|
* Handles instructions and global instructions for LLM flow.
|
|
77
115
|
*/
|
|
116
|
+
// eslint-disable-next-line require-yield
|
|
78
117
|
async *runAsync(invocationContext, llmRequest) {
|
|
79
118
|
const agent = invocationContext.agent;
|
|
80
119
|
if (!(agent instanceof LlmAgent) || !(agent.rootAgent instanceof LlmAgent)) {
|
|
81
120
|
return;
|
|
82
121
|
}
|
|
83
122
|
const rootAgent = agent.rootAgent;
|
|
84
|
-
if (rootAgent
|
|
123
|
+
if (isLlmAgent(rootAgent) && rootAgent.globalInstruction) {
|
|
85
124
|
const { instruction, requireStateInjection } = await rootAgent.canonicalGlobalInstruction(
|
|
86
125
|
new ReadonlyContext(invocationContext)
|
|
87
126
|
);
|
|
@@ -111,9 +150,10 @@ class InstructionsLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
111
150
|
}
|
|
112
151
|
const INSTRUCTIONS_LLM_REQUEST_PROCESSOR = new InstructionsLlmRequestProcessor();
|
|
113
152
|
class ContentRequestProcessor {
|
|
153
|
+
// eslint-disable-next-line require-yield
|
|
114
154
|
async *runAsync(invocationContext, llmRequest) {
|
|
115
155
|
const agent = invocationContext.agent;
|
|
116
|
-
if (!agent || !(agent
|
|
156
|
+
if (!agent || !isLlmAgent(agent)) {
|
|
117
157
|
return;
|
|
118
158
|
}
|
|
119
159
|
if (agent.includeContents === "default") {
|
|
@@ -152,6 +192,7 @@ class AgentTransferLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
152
192
|
}
|
|
153
193
|
});
|
|
154
194
|
}
|
|
195
|
+
// eslint-disable-next-line require-yield
|
|
155
196
|
async *runAsync(invocationContext, llmRequest) {
|
|
156
197
|
if (!(invocationContext.agent instanceof LlmAgent)) {
|
|
157
198
|
return;
|
|
@@ -201,7 +242,7 @@ to your parent agent.
|
|
|
201
242
|
getTransferTargets(agent) {
|
|
202
243
|
const targets = [];
|
|
203
244
|
targets.push(...agent.subAgents);
|
|
204
|
-
if (!agent.parentAgent || !(agent.parentAgent
|
|
245
|
+
if (!agent.parentAgent || !isLlmAgent(agent.parentAgent)) {
|
|
205
246
|
return targets;
|
|
206
247
|
}
|
|
207
248
|
if (!agent.disallowTransferToParent) {
|
|
@@ -220,9 +261,9 @@ to your parent agent.
|
|
|
220
261
|
const AGENT_TRANSFER_LLM_REQUEST_PROCESSOR = new AgentTransferLlmRequestProcessor();
|
|
221
262
|
class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
222
263
|
/** Handles tool confirmation information to build the LLM request. */
|
|
223
|
-
async *runAsync(invocationContext
|
|
264
|
+
async *runAsync(invocationContext) {
|
|
224
265
|
const agent = invocationContext.agent;
|
|
225
|
-
if (!(agent
|
|
266
|
+
if (!isLlmAgent(agent)) {
|
|
226
267
|
return;
|
|
227
268
|
}
|
|
228
269
|
const events = invocationContext.session.events;
|
|
@@ -248,7 +289,9 @@ class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
248
289
|
foundConfirmation = true;
|
|
249
290
|
let toolConfirmation = null;
|
|
250
291
|
if (functionResponse.response && Object.keys(functionResponse.response).length === 1 && "response" in functionResponse.response) {
|
|
251
|
-
toolConfirmation = JSON.parse(
|
|
292
|
+
toolConfirmation = JSON.parse(
|
|
293
|
+
functionResponse.response["response"]
|
|
294
|
+
);
|
|
252
295
|
} else if (functionResponse.response) {
|
|
253
296
|
toolConfirmation = new ToolConfirmation({
|
|
254
297
|
hint: functionResponse.response["hint"],
|
|
@@ -312,8 +355,12 @@ class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
312
355
|
if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
|
|
313
356
|
continue;
|
|
314
357
|
}
|
|
315
|
-
const toolsList = await agent.canonicalTools(
|
|
316
|
-
|
|
358
|
+
const toolsList = await agent.canonicalTools(
|
|
359
|
+
new ReadonlyContext(invocationContext)
|
|
360
|
+
);
|
|
361
|
+
const toolsDict = Object.fromEntries(
|
|
362
|
+
toolsList.map((tool) => [tool.name, tool])
|
|
363
|
+
);
|
|
317
364
|
const functionResponseEvent = await handleFunctionCallList({
|
|
318
365
|
invocationContext,
|
|
319
366
|
functionCalls: Object.values(toolsToResumeWithArgs),
|
|
@@ -342,12 +389,12 @@ class CodeExecutionRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
342
389
|
for await (const event of runPreProcessor(invocationContext, llmRequest)) {
|
|
343
390
|
yield event;
|
|
344
391
|
}
|
|
345
|
-
if (!(invocationContext.agent.codeExecutor
|
|
392
|
+
if (!isBaseCodeExecutor(invocationContext.agent.codeExecutor)) {
|
|
346
393
|
return;
|
|
347
394
|
}
|
|
348
395
|
for (const content of llmRequest.contents) {
|
|
349
396
|
const delimeters = invocationContext.agent.codeExecutor.codeBlockDelimiters.length ? invocationContext.agent.codeExecutor.codeBlockDelimiters[0] : ["", ""];
|
|
350
|
-
|
|
397
|
+
convertCodeExecutionParts(
|
|
351
398
|
content,
|
|
352
399
|
delimeters,
|
|
353
400
|
invocationContext.agent.codeExecutor.executionResultDelimiters
|
|
@@ -411,7 +458,10 @@ class CodeExecutionResponseProcessor {
|
|
|
411
458
|
if (llmResponse.partial) {
|
|
412
459
|
return;
|
|
413
460
|
}
|
|
414
|
-
for await (const event of runPostProcessor(
|
|
461
|
+
for await (const event of runPostProcessor(
|
|
462
|
+
invocationContext,
|
|
463
|
+
llmResponse
|
|
464
|
+
)) {
|
|
415
465
|
yield event;
|
|
416
466
|
}
|
|
417
467
|
}
|
|
@@ -419,27 +469,36 @@ class CodeExecutionResponseProcessor {
|
|
|
419
469
|
const responseProcessor = new CodeExecutionResponseProcessor();
|
|
420
470
|
async function* runPreProcessor(invocationContext, llmRequest) {
|
|
421
471
|
const agent = invocationContext.agent;
|
|
422
|
-
if (!(agent
|
|
472
|
+
if (!isLlmAgent(agent)) {
|
|
423
473
|
return;
|
|
424
474
|
}
|
|
425
475
|
const codeExecutor = agent.codeExecutor;
|
|
426
|
-
if (!codeExecutor || !(codeExecutor
|
|
476
|
+
if (!codeExecutor || !isBaseCodeExecutor(codeExecutor)) {
|
|
427
477
|
return;
|
|
428
478
|
}
|
|
429
|
-
if (codeExecutor
|
|
479
|
+
if (isBuiltInCodeExecutor(codeExecutor)) {
|
|
430
480
|
codeExecutor.processLlmRequest(llmRequest);
|
|
431
481
|
return;
|
|
432
482
|
}
|
|
433
483
|
if (!codeExecutor.optimizeDataFile) {
|
|
434
484
|
return;
|
|
435
485
|
}
|
|
436
|
-
const codeExecutorContext = new CodeExecutorContext(
|
|
486
|
+
const codeExecutorContext = new CodeExecutorContext(
|
|
487
|
+
new State(invocationContext.session.state)
|
|
488
|
+
);
|
|
437
489
|
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
438
490
|
return;
|
|
439
491
|
}
|
|
440
|
-
const allInputFiles = extractAndReplaceInlineFiles(
|
|
441
|
-
|
|
442
|
-
|
|
492
|
+
const allInputFiles = extractAndReplaceInlineFiles(
|
|
493
|
+
codeExecutorContext,
|
|
494
|
+
llmRequest
|
|
495
|
+
);
|
|
496
|
+
const processedFileNames = new Set(
|
|
497
|
+
codeExecutorContext.getProcessedFileNames()
|
|
498
|
+
);
|
|
499
|
+
const filesToProcess = allInputFiles.filter(
|
|
500
|
+
(f) => !processedFileNames.has(f.name)
|
|
501
|
+
);
|
|
443
502
|
for (const file of filesToProcess) {
|
|
444
503
|
const codeStr = getDataFilePreprocessingCode(file);
|
|
445
504
|
if (!codeStr) {
|
|
@@ -459,7 +518,10 @@ async function* runPreProcessor(invocationContext, llmRequest) {
|
|
|
459
518
|
branch: invocationContext.branch,
|
|
460
519
|
content: codeContent
|
|
461
520
|
});
|
|
462
|
-
const executionId = getOrSetExecutionId(
|
|
521
|
+
const executionId = getOrSetExecutionId(
|
|
522
|
+
invocationContext,
|
|
523
|
+
codeExecutorContext
|
|
524
|
+
);
|
|
463
525
|
const codeExecutionResult = await codeExecutor.executeCode({
|
|
464
526
|
invocationContext,
|
|
465
527
|
codeExecutionInput: {
|
|
@@ -486,20 +548,22 @@ async function* runPreProcessor(invocationContext, llmRequest) {
|
|
|
486
548
|
}
|
|
487
549
|
async function* runPostProcessor(invocationContext, llmResponse) {
|
|
488
550
|
const agent = invocationContext.agent;
|
|
489
|
-
if (!(agent
|
|
551
|
+
if (!isLlmAgent(agent)) {
|
|
490
552
|
return;
|
|
491
553
|
}
|
|
492
554
|
const codeExecutor = agent.codeExecutor;
|
|
493
|
-
if (!codeExecutor || !(codeExecutor
|
|
555
|
+
if (!codeExecutor || !isBaseCodeExecutor(codeExecutor)) {
|
|
494
556
|
return;
|
|
495
557
|
}
|
|
496
558
|
if (!llmResponse || !llmResponse.content) {
|
|
497
559
|
return;
|
|
498
560
|
}
|
|
499
|
-
if (codeExecutor
|
|
561
|
+
if (isBuiltInCodeExecutor(codeExecutor)) {
|
|
500
562
|
return;
|
|
501
563
|
}
|
|
502
|
-
const codeExecutorContext = new CodeExecutorContext(
|
|
564
|
+
const codeExecutorContext = new CodeExecutorContext(
|
|
565
|
+
new State(invocationContext.session.state)
|
|
566
|
+
);
|
|
503
567
|
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
504
568
|
return;
|
|
505
569
|
}
|
|
@@ -517,7 +581,10 @@ async function* runPostProcessor(invocationContext, llmResponse) {
|
|
|
517
581
|
branch: invocationContext.branch,
|
|
518
582
|
content: responseContent
|
|
519
583
|
});
|
|
520
|
-
const executionId = getOrSetExecutionId(
|
|
584
|
+
const executionId = getOrSetExecutionId(
|
|
585
|
+
invocationContext,
|
|
586
|
+
codeExecutorContext
|
|
587
|
+
);
|
|
521
588
|
const codeExecutionResult = await codeExecutor.executeCode({
|
|
522
589
|
invocationContext,
|
|
523
590
|
codeExecutionInput: {
|
|
@@ -537,10 +604,10 @@ async function* runPostProcessor(invocationContext, llmResponse) {
|
|
|
537
604
|
codeExecutorContext,
|
|
538
605
|
codeExecutionResult
|
|
539
606
|
);
|
|
540
|
-
llmResponse.content =
|
|
607
|
+
llmResponse.content = void 0;
|
|
541
608
|
}
|
|
542
609
|
function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
543
|
-
var
|
|
610
|
+
var _a2;
|
|
544
611
|
const allInputFiles = codeExecutorContext.getInputFiles();
|
|
545
612
|
const savedFileNames = new Set(allInputFiles.map((f) => f.name));
|
|
546
613
|
for (let i = 0; i < llmRequest.contents.length; i++) {
|
|
@@ -550,7 +617,7 @@ function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
|
550
617
|
}
|
|
551
618
|
for (let j = 0; j < content.parts.length; j++) {
|
|
552
619
|
const part = content.parts[j];
|
|
553
|
-
const mimeType = (
|
|
620
|
+
const mimeType = (_a2 = part.inlineData) == null ? void 0 : _a2.mimeType;
|
|
554
621
|
if (!mimeType || !part.inlineData || !DATA_FILE_UTIL_MAP[mimeType]) {
|
|
555
622
|
continue;
|
|
556
623
|
}
|
|
@@ -572,9 +639,9 @@ Available file: \`${fileName}\`
|
|
|
572
639
|
return allInputFiles;
|
|
573
640
|
}
|
|
574
641
|
function getOrSetExecutionId(invocationContext, codeExecutorContext) {
|
|
575
|
-
var
|
|
642
|
+
var _a2;
|
|
576
643
|
const agent = invocationContext.agent;
|
|
577
|
-
if (!(agent
|
|
644
|
+
if (!isLlmAgent(agent) || !((_a2 = agent.codeExecutor) == null ? void 0 : _a2.stateful)) {
|
|
578
645
|
return void 0;
|
|
579
646
|
}
|
|
580
647
|
let executionId = codeExecutorContext.getExecutionId();
|
|
@@ -592,7 +659,9 @@ async function postProcessCodeExecutionResult(invocationContext, codeExecutorCon
|
|
|
592
659
|
role: "model",
|
|
593
660
|
parts: [buildCodeExecutionResultPart(codeExecutionResult)]
|
|
594
661
|
};
|
|
595
|
-
const eventActions = createEventActions({
|
|
662
|
+
const eventActions = createEventActions({
|
|
663
|
+
stateDelta: codeExecutorContext.getStateDelta()
|
|
664
|
+
});
|
|
596
665
|
if (codeExecutionResult.stderr) {
|
|
597
666
|
codeExecutorContext.incrementErrorCount(invocationContext.invocationId);
|
|
598
667
|
} else {
|
|
@@ -631,10 +700,7 @@ function getDataFilePreprocessingCode(file) {
|
|
|
631
700
|
return void 0;
|
|
632
701
|
}
|
|
633
702
|
const varName = getNormalizedFileName(file.name);
|
|
634
|
-
const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace(
|
|
635
|
-
"{filename}",
|
|
636
|
-
file.name
|
|
637
|
-
);
|
|
703
|
+
const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace("{filename}", file.name);
|
|
638
704
|
return `
|
|
639
705
|
${DATA_FILE_HELPER_LIB}
|
|
640
706
|
|
|
@@ -646,13 +712,19 @@ explore_df(${varName})
|
|
|
646
712
|
`;
|
|
647
713
|
}
|
|
648
714
|
const CODE_EXECUTION_REQUEST_PROCESSOR = new CodeExecutionRequestProcessor();
|
|
649
|
-
|
|
715
|
+
const LLM_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.llmAgent");
|
|
716
|
+
function isLlmAgent(obj) {
|
|
717
|
+
return typeof obj === "object" && obj !== null && LLM_AGENT_SIGNATURE_SYMBOL in obj && obj[LLM_AGENT_SIGNATURE_SYMBOL] === true;
|
|
718
|
+
}
|
|
719
|
+
class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
650
720
|
constructor(config) {
|
|
651
|
-
var
|
|
721
|
+
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
|
|
652
722
|
super(config);
|
|
723
|
+
/** A unique symbol to identify ADK LLM agent class. */
|
|
724
|
+
this[_a] = true;
|
|
653
725
|
this.model = config.model;
|
|
654
|
-
this.instruction = (
|
|
655
|
-
this.globalInstruction = (
|
|
726
|
+
this.instruction = (_a2 = config.instruction) != null ? _a2 : "";
|
|
727
|
+
this.globalInstruction = (_b2 = config.globalInstruction) != null ? _b2 : "";
|
|
656
728
|
this.tools = (_c = config.tools) != null ? _c : [];
|
|
657
729
|
this.generateContentConfig = config.generateContentConfig;
|
|
658
730
|
this.disallowTransferToParent = (_d = config.disallowTransferToParent) != null ? _d : false;
|
|
@@ -730,7 +802,7 @@ class LlmAgent extends BaseAgent {
|
|
|
730
802
|
}
|
|
731
803
|
let ancestorAgent = this.parentAgent;
|
|
732
804
|
while (ancestorAgent) {
|
|
733
|
-
if (ancestorAgent
|
|
805
|
+
if (isLlmAgent(ancestorAgent)) {
|
|
734
806
|
return ancestorAgent.canonicalModel;
|
|
735
807
|
}
|
|
736
808
|
ancestorAgent = ancestorAgent.parentAgent;
|
|
@@ -738,47 +810,50 @@ class LlmAgent extends BaseAgent {
|
|
|
738
810
|
throw new Error(`No model found for ${this.name}.`);
|
|
739
811
|
}
|
|
740
812
|
/**
|
|
741
|
-
* The resolved
|
|
813
|
+
* The resolved instruction field to construct instruction for this
|
|
742
814
|
* agent.
|
|
743
815
|
*
|
|
744
816
|
* This method is only for use by Agent Development Kit.
|
|
745
817
|
* @param context The context to retrieve the session state.
|
|
746
|
-
* @returns The resolved
|
|
818
|
+
* @returns The resolved instruction field.
|
|
747
819
|
*/
|
|
748
|
-
async canonicalInstruction(
|
|
820
|
+
async canonicalInstruction(context2) {
|
|
749
821
|
if (typeof this.instruction === "string") {
|
|
750
822
|
return { instruction: this.instruction, requireStateInjection: true };
|
|
751
823
|
}
|
|
752
824
|
return {
|
|
753
|
-
instruction: await this.instruction(
|
|
825
|
+
instruction: await this.instruction(context2),
|
|
754
826
|
requireStateInjection: false
|
|
755
827
|
};
|
|
756
828
|
}
|
|
757
829
|
/**
|
|
758
|
-
* The resolved
|
|
830
|
+
* The resolved globalInstruction field to construct global instruction.
|
|
759
831
|
*
|
|
760
832
|
* This method is only for use by Agent Development Kit.
|
|
761
833
|
* @param context The context to retrieve the session state.
|
|
762
|
-
* @returns The resolved
|
|
834
|
+
* @returns The resolved globalInstruction field.
|
|
763
835
|
*/
|
|
764
|
-
async canonicalGlobalInstruction(
|
|
836
|
+
async canonicalGlobalInstruction(context2) {
|
|
765
837
|
if (typeof this.globalInstruction === "string") {
|
|
766
|
-
return {
|
|
838
|
+
return {
|
|
839
|
+
instruction: this.globalInstruction,
|
|
840
|
+
requireStateInjection: true
|
|
841
|
+
};
|
|
767
842
|
}
|
|
768
843
|
return {
|
|
769
|
-
instruction: await this.globalInstruction(
|
|
844
|
+
instruction: await this.globalInstruction(context2),
|
|
770
845
|
requireStateInjection: false
|
|
771
846
|
};
|
|
772
847
|
}
|
|
773
848
|
/**
|
|
774
|
-
* The resolved
|
|
849
|
+
* The resolved tools field as a list of BaseTool based on the context.
|
|
775
850
|
*
|
|
776
851
|
* This method is only for use by Agent Development Kit.
|
|
777
852
|
*/
|
|
778
|
-
async canonicalTools(
|
|
853
|
+
async canonicalTools(context2) {
|
|
779
854
|
const resolvedTools = [];
|
|
780
855
|
for (const toolUnion of this.tools) {
|
|
781
|
-
const tools = await convertToolUnionToTools(toolUnion,
|
|
856
|
+
const tools = await convertToolUnionToTools(toolUnion, context2);
|
|
782
857
|
resolvedTools.push(...tools);
|
|
783
858
|
}
|
|
784
859
|
return resolvedTools;
|
|
@@ -799,7 +874,7 @@ class LlmAgent extends BaseAgent {
|
|
|
799
874
|
return [callback];
|
|
800
875
|
}
|
|
801
876
|
/**
|
|
802
|
-
* The resolved
|
|
877
|
+
* The resolved beforeModelCallback field as a list of
|
|
803
878
|
* SingleBeforeModelCallback.
|
|
804
879
|
*
|
|
805
880
|
* This method is only for use by Agent Development Kit.
|
|
@@ -808,7 +883,7 @@ class LlmAgent extends BaseAgent {
|
|
|
808
883
|
return LlmAgent.normalizeCallbackArray(this.beforeModelCallback);
|
|
809
884
|
}
|
|
810
885
|
/**
|
|
811
|
-
* The resolved
|
|
886
|
+
* The resolved afterModelCallback field as a list of
|
|
812
887
|
* SingleAfterModelCallback.
|
|
813
888
|
*
|
|
814
889
|
* This method is only for use by Agent Development Kit.
|
|
@@ -817,7 +892,7 @@ class LlmAgent extends BaseAgent {
|
|
|
817
892
|
return LlmAgent.normalizeCallbackArray(this.afterModelCallback);
|
|
818
893
|
}
|
|
819
894
|
/**
|
|
820
|
-
* The resolved
|
|
895
|
+
* The resolved beforeToolCallback field as a list of
|
|
821
896
|
* BeforeToolCallback.
|
|
822
897
|
*
|
|
823
898
|
* This method is only for use by Agent Development Kit.
|
|
@@ -826,7 +901,7 @@ class LlmAgent extends BaseAgent {
|
|
|
826
901
|
return LlmAgent.normalizeCallbackArray(this.beforeToolCallback);
|
|
827
902
|
}
|
|
828
903
|
/**
|
|
829
|
-
* The resolved
|
|
904
|
+
* The resolved afterToolCallback field as a list of AfterToolCallback.
|
|
830
905
|
*
|
|
831
906
|
* This method is only for use by Agent Development Kit.
|
|
832
907
|
*/
|
|
@@ -843,7 +918,7 @@ class LlmAgent extends BaseAgent {
|
|
|
843
918
|
* @param event The event to process.
|
|
844
919
|
*/
|
|
845
920
|
maybeSaveOutputToState(event) {
|
|
846
|
-
var
|
|
921
|
+
var _a2, _b2;
|
|
847
922
|
if (event.author !== this.name) {
|
|
848
923
|
logger.debug(
|
|
849
924
|
`Skipping output save for agent ${this.name}: event authored by ${event.author}`
|
|
@@ -862,7 +937,7 @@ class LlmAgent extends BaseAgent {
|
|
|
862
937
|
);
|
|
863
938
|
return;
|
|
864
939
|
}
|
|
865
|
-
if (!((
|
|
940
|
+
if (!((_b2 = (_a2 = event.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2.length)) {
|
|
866
941
|
logger.debug(
|
|
867
942
|
`Skipping output save for agent ${this.name}: event content is empty`
|
|
868
943
|
);
|
|
@@ -882,10 +957,10 @@ class LlmAgent extends BaseAgent {
|
|
|
882
957
|
}
|
|
883
958
|
event.actions.stateDelta[this.outputKey] = result;
|
|
884
959
|
}
|
|
885
|
-
async *runAsyncImpl(
|
|
960
|
+
async *runAsyncImpl(context2) {
|
|
886
961
|
while (true) {
|
|
887
962
|
let lastEvent = void 0;
|
|
888
|
-
for await (const event of this.runOneStepAsync(
|
|
963
|
+
for await (const event of this.runOneStepAsync(context2)) {
|
|
889
964
|
lastEvent = event;
|
|
890
965
|
this.maybeSaveOutputToState(event);
|
|
891
966
|
yield event;
|
|
@@ -899,19 +974,20 @@ class LlmAgent extends BaseAgent {
|
|
|
899
974
|
}
|
|
900
975
|
}
|
|
901
976
|
}
|
|
902
|
-
async *runLiveImpl(
|
|
903
|
-
for await (const event of this.runLiveFlow(
|
|
977
|
+
async *runLiveImpl(context2) {
|
|
978
|
+
for await (const event of this.runLiveFlow(context2)) {
|
|
904
979
|
this.maybeSaveOutputToState(event);
|
|
905
980
|
yield event;
|
|
906
981
|
}
|
|
907
|
-
if (
|
|
982
|
+
if (context2.endInvocation) {
|
|
908
983
|
return;
|
|
909
984
|
}
|
|
910
985
|
}
|
|
911
986
|
// --------------------------------------------------------------------------
|
|
912
987
|
// #START LlmFlow Logic
|
|
913
988
|
// --------------------------------------------------------------------------
|
|
914
|
-
|
|
989
|
+
// eslint-disable-next-line require-yield
|
|
990
|
+
async *runLiveFlow(_invocationContext) {
|
|
915
991
|
await Promise.resolve();
|
|
916
992
|
throw new Error("LlmAgent.runLiveFlow not implemented");
|
|
917
993
|
}
|
|
@@ -922,7 +998,10 @@ class LlmAgent extends BaseAgent {
|
|
|
922
998
|
liveConnectConfig: {}
|
|
923
999
|
};
|
|
924
1000
|
for (const processor of this.requestProcessors) {
|
|
925
|
-
for await (const event of processor.runAsync(
|
|
1001
|
+
for await (const event of processor.runAsync(
|
|
1002
|
+
invocationContext,
|
|
1003
|
+
llmRequest
|
|
1004
|
+
)) {
|
|
926
1005
|
yield event;
|
|
927
1006
|
}
|
|
928
1007
|
}
|
|
@@ -944,27 +1023,39 @@ class LlmAgent extends BaseAgent {
|
|
|
944
1023
|
author: this.name,
|
|
945
1024
|
branch: invocationContext.branch
|
|
946
1025
|
});
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
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
|
+
}
|
|
961
1048
|
}
|
|
962
|
-
|
|
1049
|
+
);
|
|
1050
|
+
span.end();
|
|
963
1051
|
}
|
|
964
1052
|
async *postprocess(invocationContext, llmRequest, llmResponse, modelResponseEvent) {
|
|
965
|
-
var
|
|
1053
|
+
var _a2;
|
|
966
1054
|
for (const processor of this.responseProcessors) {
|
|
967
|
-
for await (const event of processor.runAsync(
|
|
1055
|
+
for await (const event of processor.runAsync(
|
|
1056
|
+
invocationContext,
|
|
1057
|
+
llmResponse
|
|
1058
|
+
)) {
|
|
968
1059
|
yield event;
|
|
969
1060
|
}
|
|
970
1061
|
}
|
|
@@ -985,7 +1076,7 @@ class LlmAgent extends BaseAgent {
|
|
|
985
1076
|
}
|
|
986
1077
|
}
|
|
987
1078
|
yield mergedEvent;
|
|
988
|
-
if (!((
|
|
1079
|
+
if (!((_a2 = getFunctionCalls(mergedEvent)) == null ? void 0 : _a2.length)) {
|
|
989
1080
|
return;
|
|
990
1081
|
}
|
|
991
1082
|
const functionResponseEvent = await handleFunctionCallsAsync({
|
|
@@ -998,7 +1089,10 @@ class LlmAgent extends BaseAgent {
|
|
|
998
1089
|
if (!functionResponseEvent) {
|
|
999
1090
|
return;
|
|
1000
1091
|
}
|
|
1001
|
-
const authEvent = generateAuthEvent(
|
|
1092
|
+
const authEvent = generateAuthEvent(
|
|
1093
|
+
invocationContext,
|
|
1094
|
+
functionResponseEvent
|
|
1095
|
+
);
|
|
1002
1096
|
if (authEvent) {
|
|
1003
1097
|
yield authEvent;
|
|
1004
1098
|
}
|
|
@@ -1040,7 +1134,7 @@ class LlmAgent extends BaseAgent {
|
|
|
1040
1134
|
return agentToRun;
|
|
1041
1135
|
}
|
|
1042
1136
|
async *callLlmAsync(invocationContext, llmRequest, modelResponseEvent) {
|
|
1043
|
-
var
|
|
1137
|
+
var _a2, _b2, _c, _d, _e;
|
|
1044
1138
|
const beforeModelResponse = await this.handleBeforeModelCallback(
|
|
1045
1139
|
invocationContext,
|
|
1046
1140
|
llmRequest,
|
|
@@ -1050,8 +1144,8 @@ class LlmAgent extends BaseAgent {
|
|
|
1050
1144
|
yield beforeModelResponse;
|
|
1051
1145
|
return;
|
|
1052
1146
|
}
|
|
1053
|
-
(
|
|
1054
|
-
(_c = (
|
|
1147
|
+
(_a2 = llmRequest.config) != null ? _a2 : llmRequest.config = {};
|
|
1148
|
+
(_c = (_b2 = llmRequest.config).labels) != null ? _c : _b2.labels = {};
|
|
1055
1149
|
if (!llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY]) {
|
|
1056
1150
|
llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY] = this.name;
|
|
1057
1151
|
}
|
|
@@ -1071,6 +1165,12 @@ class LlmAgent extends BaseAgent {
|
|
|
1071
1165
|
llmRequest,
|
|
1072
1166
|
modelResponseEvent
|
|
1073
1167
|
)) {
|
|
1168
|
+
traceCallLlm({
|
|
1169
|
+
invocationContext,
|
|
1170
|
+
eventId: modelResponseEvent.id,
|
|
1171
|
+
llmRequest,
|
|
1172
|
+
llmResponse
|
|
1173
|
+
});
|
|
1074
1174
|
const alteredLlmResponse = await this.handleAfterModelCallback(
|
|
1075
1175
|
invocationContext,
|
|
1076
1176
|
llmResponse,
|
|
@@ -1081,17 +1181,22 @@ class LlmAgent extends BaseAgent {
|
|
|
1081
1181
|
}
|
|
1082
1182
|
}
|
|
1083
1183
|
async handleBeforeModelCallback(invocationContext, llmRequest, modelResponseEvent) {
|
|
1084
|
-
const callbackContext = new CallbackContext(
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1184
|
+
const callbackContext = new CallbackContext({
|
|
1185
|
+
invocationContext,
|
|
1186
|
+
eventActions: modelResponseEvent.actions
|
|
1187
|
+
});
|
|
1188
|
+
const beforeModelCallbackResponse = await invocationContext.pluginManager.runBeforeModelCallback({
|
|
1189
|
+
callbackContext,
|
|
1190
|
+
llmRequest
|
|
1191
|
+
});
|
|
1090
1192
|
if (beforeModelCallbackResponse) {
|
|
1091
1193
|
return beforeModelCallbackResponse;
|
|
1092
1194
|
}
|
|
1093
1195
|
for (const callback of this.canonicalBeforeModelCallbacks) {
|
|
1094
|
-
const callbackResponse = await callback({
|
|
1196
|
+
const callbackResponse = await callback({
|
|
1197
|
+
context: callbackContext,
|
|
1198
|
+
request: llmRequest
|
|
1199
|
+
});
|
|
1095
1200
|
if (callbackResponse) {
|
|
1096
1201
|
return callbackResponse;
|
|
1097
1202
|
}
|
|
@@ -1099,17 +1204,22 @@ class LlmAgent extends BaseAgent {
|
|
|
1099
1204
|
return void 0;
|
|
1100
1205
|
}
|
|
1101
1206
|
async handleAfterModelCallback(invocationContext, llmResponse, modelResponseEvent) {
|
|
1102
|
-
const callbackContext = new CallbackContext(
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1207
|
+
const callbackContext = new CallbackContext({
|
|
1208
|
+
invocationContext,
|
|
1209
|
+
eventActions: modelResponseEvent.actions
|
|
1210
|
+
});
|
|
1211
|
+
const afterModelCallbackResponse = await invocationContext.pluginManager.runAfterModelCallback({
|
|
1212
|
+
callbackContext,
|
|
1213
|
+
llmResponse
|
|
1214
|
+
});
|
|
1108
1215
|
if (afterModelCallbackResponse) {
|
|
1109
1216
|
return afterModelCallbackResponse;
|
|
1110
1217
|
}
|
|
1111
1218
|
for (const callback of this.canonicalAfterModelCallbacks) {
|
|
1112
|
-
const callbackResponse = await callback({
|
|
1219
|
+
const callbackResponse = await callback({
|
|
1220
|
+
context: callbackContext,
|
|
1221
|
+
response: llmResponse
|
|
1222
|
+
});
|
|
1113
1223
|
if (callbackResponse) {
|
|
1114
1224
|
return callbackResponse;
|
|
1115
1225
|
}
|
|
@@ -1122,9 +1232,10 @@ class LlmAgent extends BaseAgent {
|
|
|
1122
1232
|
yield response;
|
|
1123
1233
|
}
|
|
1124
1234
|
} catch (modelError) {
|
|
1125
|
-
const callbackContext = new CallbackContext(
|
|
1126
|
-
|
|
1127
|
-
|
|
1235
|
+
const callbackContext = new CallbackContext({
|
|
1236
|
+
invocationContext,
|
|
1237
|
+
eventActions: modelResponseEvent.actions
|
|
1238
|
+
});
|
|
1128
1239
|
if (modelError instanceof Error) {
|
|
1129
1240
|
const onModelErrorCallbackResponse = await invocationContext.pluginManager.runOnModelErrorCallback({
|
|
1130
1241
|
callbackContext,
|
|
@@ -1156,5 +1267,6 @@ class LlmAgent extends BaseAgent {
|
|
|
1156
1267
|
export {
|
|
1157
1268
|
LlmAgent,
|
|
1158
1269
|
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
1270
|
+
isLlmAgent,
|
|
1159
1271
|
responseProcessor
|
|
1160
1272
|
};
|