@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
|
@@ -26,9 +26,11 @@ var llm_agent_exports = {};
|
|
|
26
26
|
__export(llm_agent_exports, {
|
|
27
27
|
LlmAgent: () => LlmAgent,
|
|
28
28
|
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR: () => REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
29
|
+
isLlmAgent: () => isLlmAgent,
|
|
29
30
|
responseProcessor: () => responseProcessor
|
|
30
31
|
});
|
|
31
32
|
module.exports = __toCommonJS(llm_agent_exports);
|
|
33
|
+
var import_api = require("@opentelemetry/api");
|
|
32
34
|
var import_lodash_es = require("lodash-es");
|
|
33
35
|
var import_zod = require("zod");
|
|
34
36
|
var import_base_code_executor = require("../code_executors/base_code_executor.js");
|
|
@@ -47,6 +49,7 @@ var import_tool_confirmation = require("../tools/tool_confirmation.js");
|
|
|
47
49
|
var import_tool_context = require("../tools/tool_context.js");
|
|
48
50
|
var import_env_aware_utils = require("../utils/env_aware_utils.js");
|
|
49
51
|
var import_logger = require("../utils/logger.js");
|
|
52
|
+
var import_tracing = require("../telemetry/tracing.js");
|
|
50
53
|
var import_base_agent = require("./base_agent.js");
|
|
51
54
|
var import_base_llm_processor = require("./base_llm_processor.js");
|
|
52
55
|
var import_callback_context = require("./callback_context.js");
|
|
@@ -60,22 +63,24 @@ var import_run_config = require("./run_config.js");
|
|
|
60
63
|
* Copyright 2025 Google LLC
|
|
61
64
|
* SPDX-License-Identifier: Apache-2.0
|
|
62
65
|
*/
|
|
66
|
+
var _a, _b;
|
|
63
67
|
const ADK_AGENT_NAME_LABEL_KEY = "adk_agent_name";
|
|
64
|
-
async function convertToolUnionToTools(toolUnion,
|
|
68
|
+
async function convertToolUnionToTools(toolUnion, context2) {
|
|
65
69
|
if (toolUnion instanceof import_base_tool.BaseTool) {
|
|
66
70
|
return [toolUnion];
|
|
67
71
|
}
|
|
68
|
-
return await toolUnion.getTools(
|
|
72
|
+
return await toolUnion.getTools(context2);
|
|
69
73
|
}
|
|
70
74
|
class BasicLlmRequestProcessor extends import_base_llm_processor.BaseLlmRequestProcessor {
|
|
75
|
+
// eslint-disable-next-line require-yield
|
|
71
76
|
async *runAsync(invocationContext, llmRequest) {
|
|
72
|
-
var
|
|
77
|
+
var _a2;
|
|
73
78
|
const agent = invocationContext.agent;
|
|
74
|
-
if (!(agent
|
|
79
|
+
if (!isLlmAgent(agent)) {
|
|
75
80
|
return;
|
|
76
81
|
}
|
|
77
82
|
llmRequest.model = agent.canonicalModel.model;
|
|
78
|
-
llmRequest.config = { ...(
|
|
83
|
+
llmRequest.config = { ...(_a2 = agent.generateContentConfig) != null ? _a2 : {} };
|
|
79
84
|
if (agent.outputSchema) {
|
|
80
85
|
(0, import_llm_request.setOutputSchema)(llmRequest, agent.outputSchema);
|
|
81
86
|
}
|
|
@@ -92,6 +97,7 @@ class BasicLlmRequestProcessor extends import_base_llm_processor.BaseLlmRequestP
|
|
|
92
97
|
}
|
|
93
98
|
const BASIC_LLM_REQUEST_PROCESSOR = new BasicLlmRequestProcessor();
|
|
94
99
|
class IdentityLlmRequestProcessor extends import_base_llm_processor.BaseLlmRequestProcessor {
|
|
100
|
+
// eslint-disable-next-line require-yield
|
|
95
101
|
async *runAsync(invocationContext, llmRequest) {
|
|
96
102
|
const agent = invocationContext.agent;
|
|
97
103
|
const si = [`You are an agent. Your internal name is "${agent.name}".`];
|
|
@@ -106,13 +112,14 @@ class InstructionsLlmRequestProcessor extends import_base_llm_processor.BaseLlmR
|
|
|
106
112
|
/**
|
|
107
113
|
* Handles instructions and global instructions for LLM flow.
|
|
108
114
|
*/
|
|
115
|
+
// eslint-disable-next-line require-yield
|
|
109
116
|
async *runAsync(invocationContext, llmRequest) {
|
|
110
117
|
const agent = invocationContext.agent;
|
|
111
118
|
if (!(agent instanceof LlmAgent) || !(agent.rootAgent instanceof LlmAgent)) {
|
|
112
119
|
return;
|
|
113
120
|
}
|
|
114
121
|
const rootAgent = agent.rootAgent;
|
|
115
|
-
if (rootAgent
|
|
122
|
+
if (isLlmAgent(rootAgent) && rootAgent.globalInstruction) {
|
|
116
123
|
const { instruction, requireStateInjection } = await rootAgent.canonicalGlobalInstruction(
|
|
117
124
|
new import_readonly_context.ReadonlyContext(invocationContext)
|
|
118
125
|
);
|
|
@@ -142,9 +149,10 @@ class InstructionsLlmRequestProcessor extends import_base_llm_processor.BaseLlmR
|
|
|
142
149
|
}
|
|
143
150
|
const INSTRUCTIONS_LLM_REQUEST_PROCESSOR = new InstructionsLlmRequestProcessor();
|
|
144
151
|
class ContentRequestProcessor {
|
|
152
|
+
// eslint-disable-next-line require-yield
|
|
145
153
|
async *runAsync(invocationContext, llmRequest) {
|
|
146
154
|
const agent = invocationContext.agent;
|
|
147
|
-
if (!agent || !(agent
|
|
155
|
+
if (!agent || !isLlmAgent(agent)) {
|
|
148
156
|
return;
|
|
149
157
|
}
|
|
150
158
|
if (agent.includeContents === "default") {
|
|
@@ -183,6 +191,7 @@ class AgentTransferLlmRequestProcessor extends import_base_llm_processor.BaseLlm
|
|
|
183
191
|
}
|
|
184
192
|
});
|
|
185
193
|
}
|
|
194
|
+
// eslint-disable-next-line require-yield
|
|
186
195
|
async *runAsync(invocationContext, llmRequest) {
|
|
187
196
|
if (!(invocationContext.agent instanceof LlmAgent)) {
|
|
188
197
|
return;
|
|
@@ -232,7 +241,7 @@ to your parent agent.
|
|
|
232
241
|
getTransferTargets(agent) {
|
|
233
242
|
const targets = [];
|
|
234
243
|
targets.push(...agent.subAgents);
|
|
235
|
-
if (!agent.parentAgent || !(agent.parentAgent
|
|
244
|
+
if (!agent.parentAgent || !isLlmAgent(agent.parentAgent)) {
|
|
236
245
|
return targets;
|
|
237
246
|
}
|
|
238
247
|
if (!agent.disallowTransferToParent) {
|
|
@@ -251,9 +260,9 @@ to your parent agent.
|
|
|
251
260
|
const AGENT_TRANSFER_LLM_REQUEST_PROCESSOR = new AgentTransferLlmRequestProcessor();
|
|
252
261
|
class RequestConfirmationLlmRequestProcessor extends import_base_llm_processor.BaseLlmRequestProcessor {
|
|
253
262
|
/** Handles tool confirmation information to build the LLM request. */
|
|
254
|
-
async *runAsync(invocationContext
|
|
263
|
+
async *runAsync(invocationContext) {
|
|
255
264
|
const agent = invocationContext.agent;
|
|
256
|
-
if (!(agent
|
|
265
|
+
if (!isLlmAgent(agent)) {
|
|
257
266
|
return;
|
|
258
267
|
}
|
|
259
268
|
const events = invocationContext.session.events;
|
|
@@ -279,7 +288,9 @@ class RequestConfirmationLlmRequestProcessor extends import_base_llm_processor.B
|
|
|
279
288
|
foundConfirmation = true;
|
|
280
289
|
let toolConfirmation = null;
|
|
281
290
|
if (functionResponse.response && Object.keys(functionResponse.response).length === 1 && "response" in functionResponse.response) {
|
|
282
|
-
toolConfirmation = JSON.parse(
|
|
291
|
+
toolConfirmation = JSON.parse(
|
|
292
|
+
functionResponse.response["response"]
|
|
293
|
+
);
|
|
283
294
|
} else if (functionResponse.response) {
|
|
284
295
|
toolConfirmation = new import_tool_confirmation.ToolConfirmation({
|
|
285
296
|
hint: functionResponse.response["hint"],
|
|
@@ -343,8 +354,12 @@ class RequestConfirmationLlmRequestProcessor extends import_base_llm_processor.B
|
|
|
343
354
|
if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
|
|
344
355
|
continue;
|
|
345
356
|
}
|
|
346
|
-
const toolsList = await agent.canonicalTools(
|
|
347
|
-
|
|
357
|
+
const toolsList = await agent.canonicalTools(
|
|
358
|
+
new import_readonly_context.ReadonlyContext(invocationContext)
|
|
359
|
+
);
|
|
360
|
+
const toolsDict = Object.fromEntries(
|
|
361
|
+
toolsList.map((tool) => [tool.name, tool])
|
|
362
|
+
);
|
|
348
363
|
const functionResponseEvent = await (0, import_functions.handleFunctionCallList)({
|
|
349
364
|
invocationContext,
|
|
350
365
|
functionCalls: Object.values(toolsToResumeWithArgs),
|
|
@@ -373,12 +388,12 @@ class CodeExecutionRequestProcessor extends import_base_llm_processor.BaseLlmReq
|
|
|
373
388
|
for await (const event of runPreProcessor(invocationContext, llmRequest)) {
|
|
374
389
|
yield event;
|
|
375
390
|
}
|
|
376
|
-
if (!(invocationContext.agent.codeExecutor
|
|
391
|
+
if (!(0, import_base_code_executor.isBaseCodeExecutor)(invocationContext.agent.codeExecutor)) {
|
|
377
392
|
return;
|
|
378
393
|
}
|
|
379
394
|
for (const content of llmRequest.contents) {
|
|
380
395
|
const delimeters = invocationContext.agent.codeExecutor.codeBlockDelimiters.length ? invocationContext.agent.codeExecutor.codeBlockDelimiters[0] : ["", ""];
|
|
381
|
-
|
|
396
|
+
(0, import_code_execution_utils.convertCodeExecutionParts)(
|
|
382
397
|
content,
|
|
383
398
|
delimeters,
|
|
384
399
|
invocationContext.agent.codeExecutor.executionResultDelimiters
|
|
@@ -442,7 +457,10 @@ class CodeExecutionResponseProcessor {
|
|
|
442
457
|
if (llmResponse.partial) {
|
|
443
458
|
return;
|
|
444
459
|
}
|
|
445
|
-
for await (const event of runPostProcessor(
|
|
460
|
+
for await (const event of runPostProcessor(
|
|
461
|
+
invocationContext,
|
|
462
|
+
llmResponse
|
|
463
|
+
)) {
|
|
446
464
|
yield event;
|
|
447
465
|
}
|
|
448
466
|
}
|
|
@@ -450,27 +468,36 @@ class CodeExecutionResponseProcessor {
|
|
|
450
468
|
const responseProcessor = new CodeExecutionResponseProcessor();
|
|
451
469
|
async function* runPreProcessor(invocationContext, llmRequest) {
|
|
452
470
|
const agent = invocationContext.agent;
|
|
453
|
-
if (!(agent
|
|
471
|
+
if (!isLlmAgent(agent)) {
|
|
454
472
|
return;
|
|
455
473
|
}
|
|
456
474
|
const codeExecutor = agent.codeExecutor;
|
|
457
|
-
if (!codeExecutor || !(
|
|
475
|
+
if (!codeExecutor || !(0, import_base_code_executor.isBaseCodeExecutor)(codeExecutor)) {
|
|
458
476
|
return;
|
|
459
477
|
}
|
|
460
|
-
if (
|
|
478
|
+
if ((0, import_built_in_code_executor.isBuiltInCodeExecutor)(codeExecutor)) {
|
|
461
479
|
codeExecutor.processLlmRequest(llmRequest);
|
|
462
480
|
return;
|
|
463
481
|
}
|
|
464
482
|
if (!codeExecutor.optimizeDataFile) {
|
|
465
483
|
return;
|
|
466
484
|
}
|
|
467
|
-
const codeExecutorContext = new import_code_executor_context.CodeExecutorContext(
|
|
485
|
+
const codeExecutorContext = new import_code_executor_context.CodeExecutorContext(
|
|
486
|
+
new import_state.State(invocationContext.session.state)
|
|
487
|
+
);
|
|
468
488
|
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
469
489
|
return;
|
|
470
490
|
}
|
|
471
|
-
const allInputFiles = extractAndReplaceInlineFiles(
|
|
472
|
-
|
|
473
|
-
|
|
491
|
+
const allInputFiles = extractAndReplaceInlineFiles(
|
|
492
|
+
codeExecutorContext,
|
|
493
|
+
llmRequest
|
|
494
|
+
);
|
|
495
|
+
const processedFileNames = new Set(
|
|
496
|
+
codeExecutorContext.getProcessedFileNames()
|
|
497
|
+
);
|
|
498
|
+
const filesToProcess = allInputFiles.filter(
|
|
499
|
+
(f) => !processedFileNames.has(f.name)
|
|
500
|
+
);
|
|
474
501
|
for (const file of filesToProcess) {
|
|
475
502
|
const codeStr = getDataFilePreprocessingCode(file);
|
|
476
503
|
if (!codeStr) {
|
|
@@ -490,7 +517,10 @@ async function* runPreProcessor(invocationContext, llmRequest) {
|
|
|
490
517
|
branch: invocationContext.branch,
|
|
491
518
|
content: codeContent
|
|
492
519
|
});
|
|
493
|
-
const executionId = getOrSetExecutionId(
|
|
520
|
+
const executionId = getOrSetExecutionId(
|
|
521
|
+
invocationContext,
|
|
522
|
+
codeExecutorContext
|
|
523
|
+
);
|
|
494
524
|
const codeExecutionResult = await codeExecutor.executeCode({
|
|
495
525
|
invocationContext,
|
|
496
526
|
codeExecutionInput: {
|
|
@@ -517,20 +547,22 @@ async function* runPreProcessor(invocationContext, llmRequest) {
|
|
|
517
547
|
}
|
|
518
548
|
async function* runPostProcessor(invocationContext, llmResponse) {
|
|
519
549
|
const agent = invocationContext.agent;
|
|
520
|
-
if (!(agent
|
|
550
|
+
if (!isLlmAgent(agent)) {
|
|
521
551
|
return;
|
|
522
552
|
}
|
|
523
553
|
const codeExecutor = agent.codeExecutor;
|
|
524
|
-
if (!codeExecutor || !(
|
|
554
|
+
if (!codeExecutor || !(0, import_base_code_executor.isBaseCodeExecutor)(codeExecutor)) {
|
|
525
555
|
return;
|
|
526
556
|
}
|
|
527
557
|
if (!llmResponse || !llmResponse.content) {
|
|
528
558
|
return;
|
|
529
559
|
}
|
|
530
|
-
if (
|
|
560
|
+
if ((0, import_built_in_code_executor.isBuiltInCodeExecutor)(codeExecutor)) {
|
|
531
561
|
return;
|
|
532
562
|
}
|
|
533
|
-
const codeExecutorContext = new import_code_executor_context.CodeExecutorContext(
|
|
563
|
+
const codeExecutorContext = new import_code_executor_context.CodeExecutorContext(
|
|
564
|
+
new import_state.State(invocationContext.session.state)
|
|
565
|
+
);
|
|
534
566
|
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
535
567
|
return;
|
|
536
568
|
}
|
|
@@ -548,7 +580,10 @@ async function* runPostProcessor(invocationContext, llmResponse) {
|
|
|
548
580
|
branch: invocationContext.branch,
|
|
549
581
|
content: responseContent
|
|
550
582
|
});
|
|
551
|
-
const executionId = getOrSetExecutionId(
|
|
583
|
+
const executionId = getOrSetExecutionId(
|
|
584
|
+
invocationContext,
|
|
585
|
+
codeExecutorContext
|
|
586
|
+
);
|
|
552
587
|
const codeExecutionResult = await codeExecutor.executeCode({
|
|
553
588
|
invocationContext,
|
|
554
589
|
codeExecutionInput: {
|
|
@@ -568,10 +603,10 @@ async function* runPostProcessor(invocationContext, llmResponse) {
|
|
|
568
603
|
codeExecutorContext,
|
|
569
604
|
codeExecutionResult
|
|
570
605
|
);
|
|
571
|
-
llmResponse.content =
|
|
606
|
+
llmResponse.content = void 0;
|
|
572
607
|
}
|
|
573
608
|
function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
574
|
-
var
|
|
609
|
+
var _a2;
|
|
575
610
|
const allInputFiles = codeExecutorContext.getInputFiles();
|
|
576
611
|
const savedFileNames = new Set(allInputFiles.map((f) => f.name));
|
|
577
612
|
for (let i = 0; i < llmRequest.contents.length; i++) {
|
|
@@ -581,7 +616,7 @@ function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
|
581
616
|
}
|
|
582
617
|
for (let j = 0; j < content.parts.length; j++) {
|
|
583
618
|
const part = content.parts[j];
|
|
584
|
-
const mimeType = (
|
|
619
|
+
const mimeType = (_a2 = part.inlineData) == null ? void 0 : _a2.mimeType;
|
|
585
620
|
if (!mimeType || !part.inlineData || !DATA_FILE_UTIL_MAP[mimeType]) {
|
|
586
621
|
continue;
|
|
587
622
|
}
|
|
@@ -603,9 +638,9 @@ Available file: \`${fileName}\`
|
|
|
603
638
|
return allInputFiles;
|
|
604
639
|
}
|
|
605
640
|
function getOrSetExecutionId(invocationContext, codeExecutorContext) {
|
|
606
|
-
var
|
|
641
|
+
var _a2;
|
|
607
642
|
const agent = invocationContext.agent;
|
|
608
|
-
if (!(agent
|
|
643
|
+
if (!isLlmAgent(agent) || !((_a2 = agent.codeExecutor) == null ? void 0 : _a2.stateful)) {
|
|
609
644
|
return void 0;
|
|
610
645
|
}
|
|
611
646
|
let executionId = codeExecutorContext.getExecutionId();
|
|
@@ -623,7 +658,9 @@ async function postProcessCodeExecutionResult(invocationContext, codeExecutorCon
|
|
|
623
658
|
role: "model",
|
|
624
659
|
parts: [(0, import_code_execution_utils.buildCodeExecutionResultPart)(codeExecutionResult)]
|
|
625
660
|
};
|
|
626
|
-
const eventActions = (0, import_event_actions.createEventActions)({
|
|
661
|
+
const eventActions = (0, import_event_actions.createEventActions)({
|
|
662
|
+
stateDelta: codeExecutorContext.getStateDelta()
|
|
663
|
+
});
|
|
627
664
|
if (codeExecutionResult.stderr) {
|
|
628
665
|
codeExecutorContext.incrementErrorCount(invocationContext.invocationId);
|
|
629
666
|
} else {
|
|
@@ -662,10 +699,7 @@ function getDataFilePreprocessingCode(file) {
|
|
|
662
699
|
return void 0;
|
|
663
700
|
}
|
|
664
701
|
const varName = getNormalizedFileName(file.name);
|
|
665
|
-
const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace(
|
|
666
|
-
"{filename}",
|
|
667
|
-
file.name
|
|
668
|
-
);
|
|
702
|
+
const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace("{filename}", file.name);
|
|
669
703
|
return `
|
|
670
704
|
${DATA_FILE_HELPER_LIB}
|
|
671
705
|
|
|
@@ -677,13 +711,19 @@ explore_df(${varName})
|
|
|
677
711
|
`;
|
|
678
712
|
}
|
|
679
713
|
const CODE_EXECUTION_REQUEST_PROCESSOR = new CodeExecutionRequestProcessor();
|
|
680
|
-
|
|
714
|
+
const LLM_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.llmAgent");
|
|
715
|
+
function isLlmAgent(obj) {
|
|
716
|
+
return typeof obj === "object" && obj !== null && LLM_AGENT_SIGNATURE_SYMBOL in obj && obj[LLM_AGENT_SIGNATURE_SYMBOL] === true;
|
|
717
|
+
}
|
|
718
|
+
class LlmAgent extends (_b = import_base_agent.BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
681
719
|
constructor(config) {
|
|
682
|
-
var
|
|
720
|
+
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
|
|
683
721
|
super(config);
|
|
722
|
+
/** A unique symbol to identify ADK LLM agent class. */
|
|
723
|
+
this[_a] = true;
|
|
684
724
|
this.model = config.model;
|
|
685
|
-
this.instruction = (
|
|
686
|
-
this.globalInstruction = (
|
|
725
|
+
this.instruction = (_a2 = config.instruction) != null ? _a2 : "";
|
|
726
|
+
this.globalInstruction = (_b2 = config.globalInstruction) != null ? _b2 : "";
|
|
687
727
|
this.tools = (_c = config.tools) != null ? _c : [];
|
|
688
728
|
this.generateContentConfig = config.generateContentConfig;
|
|
689
729
|
this.disallowTransferToParent = (_d = config.disallowTransferToParent) != null ? _d : false;
|
|
@@ -761,7 +801,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
761
801
|
}
|
|
762
802
|
let ancestorAgent = this.parentAgent;
|
|
763
803
|
while (ancestorAgent) {
|
|
764
|
-
if (ancestorAgent
|
|
804
|
+
if (isLlmAgent(ancestorAgent)) {
|
|
765
805
|
return ancestorAgent.canonicalModel;
|
|
766
806
|
}
|
|
767
807
|
ancestorAgent = ancestorAgent.parentAgent;
|
|
@@ -769,47 +809,50 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
769
809
|
throw new Error(`No model found for ${this.name}.`);
|
|
770
810
|
}
|
|
771
811
|
/**
|
|
772
|
-
* The resolved
|
|
812
|
+
* The resolved instruction field to construct instruction for this
|
|
773
813
|
* agent.
|
|
774
814
|
*
|
|
775
815
|
* This method is only for use by Agent Development Kit.
|
|
776
816
|
* @param context The context to retrieve the session state.
|
|
777
|
-
* @returns The resolved
|
|
817
|
+
* @returns The resolved instruction field.
|
|
778
818
|
*/
|
|
779
|
-
async canonicalInstruction(
|
|
819
|
+
async canonicalInstruction(context2) {
|
|
780
820
|
if (typeof this.instruction === "string") {
|
|
781
821
|
return { instruction: this.instruction, requireStateInjection: true };
|
|
782
822
|
}
|
|
783
823
|
return {
|
|
784
|
-
instruction: await this.instruction(
|
|
824
|
+
instruction: await this.instruction(context2),
|
|
785
825
|
requireStateInjection: false
|
|
786
826
|
};
|
|
787
827
|
}
|
|
788
828
|
/**
|
|
789
|
-
* The resolved
|
|
829
|
+
* The resolved globalInstruction field to construct global instruction.
|
|
790
830
|
*
|
|
791
831
|
* This method is only for use by Agent Development Kit.
|
|
792
832
|
* @param context The context to retrieve the session state.
|
|
793
|
-
* @returns The resolved
|
|
833
|
+
* @returns The resolved globalInstruction field.
|
|
794
834
|
*/
|
|
795
|
-
async canonicalGlobalInstruction(
|
|
835
|
+
async canonicalGlobalInstruction(context2) {
|
|
796
836
|
if (typeof this.globalInstruction === "string") {
|
|
797
|
-
return {
|
|
837
|
+
return {
|
|
838
|
+
instruction: this.globalInstruction,
|
|
839
|
+
requireStateInjection: true
|
|
840
|
+
};
|
|
798
841
|
}
|
|
799
842
|
return {
|
|
800
|
-
instruction: await this.globalInstruction(
|
|
843
|
+
instruction: await this.globalInstruction(context2),
|
|
801
844
|
requireStateInjection: false
|
|
802
845
|
};
|
|
803
846
|
}
|
|
804
847
|
/**
|
|
805
|
-
* The resolved
|
|
848
|
+
* The resolved tools field as a list of BaseTool based on the context.
|
|
806
849
|
*
|
|
807
850
|
* This method is only for use by Agent Development Kit.
|
|
808
851
|
*/
|
|
809
|
-
async canonicalTools(
|
|
852
|
+
async canonicalTools(context2) {
|
|
810
853
|
const resolvedTools = [];
|
|
811
854
|
for (const toolUnion of this.tools) {
|
|
812
|
-
const tools = await convertToolUnionToTools(toolUnion,
|
|
855
|
+
const tools = await convertToolUnionToTools(toolUnion, context2);
|
|
813
856
|
resolvedTools.push(...tools);
|
|
814
857
|
}
|
|
815
858
|
return resolvedTools;
|
|
@@ -830,7 +873,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
830
873
|
return [callback];
|
|
831
874
|
}
|
|
832
875
|
/**
|
|
833
|
-
* The resolved
|
|
876
|
+
* The resolved beforeModelCallback field as a list of
|
|
834
877
|
* SingleBeforeModelCallback.
|
|
835
878
|
*
|
|
836
879
|
* This method is only for use by Agent Development Kit.
|
|
@@ -839,7 +882,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
839
882
|
return LlmAgent.normalizeCallbackArray(this.beforeModelCallback);
|
|
840
883
|
}
|
|
841
884
|
/**
|
|
842
|
-
* The resolved
|
|
885
|
+
* The resolved afterModelCallback field as a list of
|
|
843
886
|
* SingleAfterModelCallback.
|
|
844
887
|
*
|
|
845
888
|
* This method is only for use by Agent Development Kit.
|
|
@@ -848,7 +891,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
848
891
|
return LlmAgent.normalizeCallbackArray(this.afterModelCallback);
|
|
849
892
|
}
|
|
850
893
|
/**
|
|
851
|
-
* The resolved
|
|
894
|
+
* The resolved beforeToolCallback field as a list of
|
|
852
895
|
* BeforeToolCallback.
|
|
853
896
|
*
|
|
854
897
|
* This method is only for use by Agent Development Kit.
|
|
@@ -857,7 +900,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
857
900
|
return LlmAgent.normalizeCallbackArray(this.beforeToolCallback);
|
|
858
901
|
}
|
|
859
902
|
/**
|
|
860
|
-
* The resolved
|
|
903
|
+
* The resolved afterToolCallback field as a list of AfterToolCallback.
|
|
861
904
|
*
|
|
862
905
|
* This method is only for use by Agent Development Kit.
|
|
863
906
|
*/
|
|
@@ -874,7 +917,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
874
917
|
* @param event The event to process.
|
|
875
918
|
*/
|
|
876
919
|
maybeSaveOutputToState(event) {
|
|
877
|
-
var
|
|
920
|
+
var _a2, _b2;
|
|
878
921
|
if (event.author !== this.name) {
|
|
879
922
|
import_logger.logger.debug(
|
|
880
923
|
`Skipping output save for agent ${this.name}: event authored by ${event.author}`
|
|
@@ -893,7 +936,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
893
936
|
);
|
|
894
937
|
return;
|
|
895
938
|
}
|
|
896
|
-
if (!((
|
|
939
|
+
if (!((_b2 = (_a2 = event.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2.length)) {
|
|
897
940
|
import_logger.logger.debug(
|
|
898
941
|
`Skipping output save for agent ${this.name}: event content is empty`
|
|
899
942
|
);
|
|
@@ -913,10 +956,10 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
913
956
|
}
|
|
914
957
|
event.actions.stateDelta[this.outputKey] = result;
|
|
915
958
|
}
|
|
916
|
-
async *runAsyncImpl(
|
|
959
|
+
async *runAsyncImpl(context2) {
|
|
917
960
|
while (true) {
|
|
918
961
|
let lastEvent = void 0;
|
|
919
|
-
for await (const event of this.runOneStepAsync(
|
|
962
|
+
for await (const event of this.runOneStepAsync(context2)) {
|
|
920
963
|
lastEvent = event;
|
|
921
964
|
this.maybeSaveOutputToState(event);
|
|
922
965
|
yield event;
|
|
@@ -930,19 +973,20 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
930
973
|
}
|
|
931
974
|
}
|
|
932
975
|
}
|
|
933
|
-
async *runLiveImpl(
|
|
934
|
-
for await (const event of this.runLiveFlow(
|
|
976
|
+
async *runLiveImpl(context2) {
|
|
977
|
+
for await (const event of this.runLiveFlow(context2)) {
|
|
935
978
|
this.maybeSaveOutputToState(event);
|
|
936
979
|
yield event;
|
|
937
980
|
}
|
|
938
|
-
if (
|
|
981
|
+
if (context2.endInvocation) {
|
|
939
982
|
return;
|
|
940
983
|
}
|
|
941
984
|
}
|
|
942
985
|
// --------------------------------------------------------------------------
|
|
943
986
|
// #START LlmFlow Logic
|
|
944
987
|
// --------------------------------------------------------------------------
|
|
945
|
-
|
|
988
|
+
// eslint-disable-next-line require-yield
|
|
989
|
+
async *runLiveFlow(_invocationContext) {
|
|
946
990
|
await Promise.resolve();
|
|
947
991
|
throw new Error("LlmAgent.runLiveFlow not implemented");
|
|
948
992
|
}
|
|
@@ -953,7 +997,10 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
953
997
|
liveConnectConfig: {}
|
|
954
998
|
};
|
|
955
999
|
for (const processor of this.requestProcessors) {
|
|
956
|
-
for await (const event of processor.runAsync(
|
|
1000
|
+
for await (const event of processor.runAsync(
|
|
1001
|
+
invocationContext,
|
|
1002
|
+
llmRequest
|
|
1003
|
+
)) {
|
|
957
1004
|
yield event;
|
|
958
1005
|
}
|
|
959
1006
|
}
|
|
@@ -975,27 +1022,39 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
975
1022
|
author: this.name,
|
|
976
1023
|
branch: invocationContext.branch
|
|
977
1024
|
});
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
1025
|
+
const span = import_tracing.tracer.startSpan("call_llm");
|
|
1026
|
+
const ctx = import_api.trace.setSpan(import_api.context.active(), span);
|
|
1027
|
+
yield* (0, import_tracing.runAsyncGeneratorWithOtelContext)(
|
|
1028
|
+
ctx,
|
|
1029
|
+
this,
|
|
1030
|
+
async function* () {
|
|
1031
|
+
for await (const llmResponse of this.callLlmAsync(
|
|
1032
|
+
invocationContext,
|
|
1033
|
+
llmRequest,
|
|
1034
|
+
modelResponseEvent
|
|
1035
|
+
)) {
|
|
1036
|
+
for await (const event of this.postprocess(
|
|
1037
|
+
invocationContext,
|
|
1038
|
+
llmRequest,
|
|
1039
|
+
llmResponse,
|
|
1040
|
+
modelResponseEvent
|
|
1041
|
+
)) {
|
|
1042
|
+
modelResponseEvent.id = (0, import_event.createNewEventId)();
|
|
1043
|
+
modelResponseEvent.timestamp = (/* @__PURE__ */ new Date()).getTime();
|
|
1044
|
+
yield event;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
992
1047
|
}
|
|
993
|
-
|
|
1048
|
+
);
|
|
1049
|
+
span.end();
|
|
994
1050
|
}
|
|
995
1051
|
async *postprocess(invocationContext, llmRequest, llmResponse, modelResponseEvent) {
|
|
996
|
-
var
|
|
1052
|
+
var _a2;
|
|
997
1053
|
for (const processor of this.responseProcessors) {
|
|
998
|
-
for await (const event of processor.runAsync(
|
|
1054
|
+
for await (const event of processor.runAsync(
|
|
1055
|
+
invocationContext,
|
|
1056
|
+
llmResponse
|
|
1057
|
+
)) {
|
|
999
1058
|
yield event;
|
|
1000
1059
|
}
|
|
1001
1060
|
}
|
|
@@ -1016,7 +1075,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
1016
1075
|
}
|
|
1017
1076
|
}
|
|
1018
1077
|
yield mergedEvent;
|
|
1019
|
-
if (!((
|
|
1078
|
+
if (!((_a2 = (0, import_event.getFunctionCalls)(mergedEvent)) == null ? void 0 : _a2.length)) {
|
|
1020
1079
|
return;
|
|
1021
1080
|
}
|
|
1022
1081
|
const functionResponseEvent = await (0, import_functions.handleFunctionCallsAsync)({
|
|
@@ -1029,7 +1088,10 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
1029
1088
|
if (!functionResponseEvent) {
|
|
1030
1089
|
return;
|
|
1031
1090
|
}
|
|
1032
|
-
const authEvent = (0, import_functions.generateAuthEvent)(
|
|
1091
|
+
const authEvent = (0, import_functions.generateAuthEvent)(
|
|
1092
|
+
invocationContext,
|
|
1093
|
+
functionResponseEvent
|
|
1094
|
+
);
|
|
1033
1095
|
if (authEvent) {
|
|
1034
1096
|
yield authEvent;
|
|
1035
1097
|
}
|
|
@@ -1071,7 +1133,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
1071
1133
|
return agentToRun;
|
|
1072
1134
|
}
|
|
1073
1135
|
async *callLlmAsync(invocationContext, llmRequest, modelResponseEvent) {
|
|
1074
|
-
var
|
|
1136
|
+
var _a2, _b2, _c, _d, _e;
|
|
1075
1137
|
const beforeModelResponse = await this.handleBeforeModelCallback(
|
|
1076
1138
|
invocationContext,
|
|
1077
1139
|
llmRequest,
|
|
@@ -1081,8 +1143,8 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
1081
1143
|
yield beforeModelResponse;
|
|
1082
1144
|
return;
|
|
1083
1145
|
}
|
|
1084
|
-
(
|
|
1085
|
-
(_c = (
|
|
1146
|
+
(_a2 = llmRequest.config) != null ? _a2 : llmRequest.config = {};
|
|
1147
|
+
(_c = (_b2 = llmRequest.config).labels) != null ? _c : _b2.labels = {};
|
|
1086
1148
|
if (!llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY]) {
|
|
1087
1149
|
llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY] = this.name;
|
|
1088
1150
|
}
|
|
@@ -1102,6 +1164,12 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
1102
1164
|
llmRequest,
|
|
1103
1165
|
modelResponseEvent
|
|
1104
1166
|
)) {
|
|
1167
|
+
(0, import_tracing.traceCallLlm)({
|
|
1168
|
+
invocationContext,
|
|
1169
|
+
eventId: modelResponseEvent.id,
|
|
1170
|
+
llmRequest,
|
|
1171
|
+
llmResponse
|
|
1172
|
+
});
|
|
1105
1173
|
const alteredLlmResponse = await this.handleAfterModelCallback(
|
|
1106
1174
|
invocationContext,
|
|
1107
1175
|
llmResponse,
|
|
@@ -1112,17 +1180,22 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
1112
1180
|
}
|
|
1113
1181
|
}
|
|
1114
1182
|
async handleBeforeModelCallback(invocationContext, llmRequest, modelResponseEvent) {
|
|
1115
|
-
const callbackContext = new import_callback_context.CallbackContext(
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1183
|
+
const callbackContext = new import_callback_context.CallbackContext({
|
|
1184
|
+
invocationContext,
|
|
1185
|
+
eventActions: modelResponseEvent.actions
|
|
1186
|
+
});
|
|
1187
|
+
const beforeModelCallbackResponse = await invocationContext.pluginManager.runBeforeModelCallback({
|
|
1188
|
+
callbackContext,
|
|
1189
|
+
llmRequest
|
|
1190
|
+
});
|
|
1121
1191
|
if (beforeModelCallbackResponse) {
|
|
1122
1192
|
return beforeModelCallbackResponse;
|
|
1123
1193
|
}
|
|
1124
1194
|
for (const callback of this.canonicalBeforeModelCallbacks) {
|
|
1125
|
-
const callbackResponse = await callback({
|
|
1195
|
+
const callbackResponse = await callback({
|
|
1196
|
+
context: callbackContext,
|
|
1197
|
+
request: llmRequest
|
|
1198
|
+
});
|
|
1126
1199
|
if (callbackResponse) {
|
|
1127
1200
|
return callbackResponse;
|
|
1128
1201
|
}
|
|
@@ -1130,17 +1203,22 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
1130
1203
|
return void 0;
|
|
1131
1204
|
}
|
|
1132
1205
|
async handleAfterModelCallback(invocationContext, llmResponse, modelResponseEvent) {
|
|
1133
|
-
const callbackContext = new import_callback_context.CallbackContext(
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1206
|
+
const callbackContext = new import_callback_context.CallbackContext({
|
|
1207
|
+
invocationContext,
|
|
1208
|
+
eventActions: modelResponseEvent.actions
|
|
1209
|
+
});
|
|
1210
|
+
const afterModelCallbackResponse = await invocationContext.pluginManager.runAfterModelCallback({
|
|
1211
|
+
callbackContext,
|
|
1212
|
+
llmResponse
|
|
1213
|
+
});
|
|
1139
1214
|
if (afterModelCallbackResponse) {
|
|
1140
1215
|
return afterModelCallbackResponse;
|
|
1141
1216
|
}
|
|
1142
1217
|
for (const callback of this.canonicalAfterModelCallbacks) {
|
|
1143
|
-
const callbackResponse = await callback({
|
|
1218
|
+
const callbackResponse = await callback({
|
|
1219
|
+
context: callbackContext,
|
|
1220
|
+
response: llmResponse
|
|
1221
|
+
});
|
|
1144
1222
|
if (callbackResponse) {
|
|
1145
1223
|
return callbackResponse;
|
|
1146
1224
|
}
|
|
@@ -1153,9 +1231,10 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
1153
1231
|
yield response;
|
|
1154
1232
|
}
|
|
1155
1233
|
} catch (modelError) {
|
|
1156
|
-
const callbackContext = new import_callback_context.CallbackContext(
|
|
1157
|
-
|
|
1158
|
-
|
|
1234
|
+
const callbackContext = new import_callback_context.CallbackContext({
|
|
1235
|
+
invocationContext,
|
|
1236
|
+
eventActions: modelResponseEvent.actions
|
|
1237
|
+
});
|
|
1159
1238
|
if (modelError instanceof Error) {
|
|
1160
1239
|
const onModelErrorCallbackResponse = await invocationContext.pluginManager.runOnModelErrorCallback({
|
|
1161
1240
|
callbackContext,
|
|
@@ -1188,5 +1267,6 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
1188
1267
|
0 && (module.exports = {
|
|
1189
1268
|
LlmAgent,
|
|
1190
1269
|
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
1270
|
+
isLlmAgent,
|
|
1191
1271
|
responseProcessor
|
|
1192
1272
|
});
|