@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.
Files changed (191) hide show
  1. package/dist/cjs/agents/base_agent.js +52 -24
  2. package/dist/cjs/agents/callback_context.js +4 -1
  3. package/dist/cjs/agents/content_processor_utils.js +15 -7
  4. package/dist/cjs/agents/functions.js +79 -29
  5. package/dist/cjs/agents/invocation_context.js +3 -1
  6. package/dist/cjs/agents/llm_agent.js +188 -108
  7. package/dist/cjs/agents/loop_agent.js +18 -6
  8. package/dist/cjs/agents/parallel_agent.js +20 -7
  9. package/dist/cjs/agents/readonly_context.js +4 -1
  10. package/dist/cjs/agents/sequential_agent.js +34 -12
  11. package/dist/cjs/artifacts/gcs_artifact_service.js +28 -20
  12. package/dist/cjs/artifacts/in_memory_artifact_service.js +18 -4
  13. package/dist/cjs/auth/auth_handler.js +3 -1
  14. package/dist/cjs/code_executors/base_code_executor.js +14 -2
  15. package/dist/cjs/code_executors/built_in_code_executor.js +21 -5
  16. package/dist/cjs/code_executors/code_executor_context.js +5 -5
  17. package/dist/cjs/common.js +45 -0
  18. package/dist/cjs/events/event.js +1 -3
  19. package/dist/cjs/examples/base_example_provider.js +18 -2
  20. package/dist/cjs/examples/example_util.js +1 -1
  21. package/dist/cjs/index.js +19 -19
  22. package/dist/cjs/index.js.map +4 -4
  23. package/dist/cjs/memory/in_memory_memory_service.js +3 -1
  24. package/dist/cjs/models/base_llm.js +8 -4
  25. package/dist/cjs/models/gemini_llm_connection.js +1 -0
  26. package/dist/cjs/models/google_llm.js +3 -3
  27. package/dist/cjs/plugins/base_plugin.js +71 -49
  28. package/dist/cjs/plugins/logging_plugin.js +50 -13
  29. package/dist/cjs/plugins/plugin_manager.js +56 -24
  30. package/dist/cjs/plugins/security_plugin.js +5 -2
  31. package/dist/cjs/runner/runner.js +126 -101
  32. package/dist/cjs/sessions/in_memory_session_service.js +38 -14
  33. package/dist/cjs/telemetry/google_cloud.js +7 -9
  34. package/dist/cjs/telemetry/setup.js +15 -7
  35. package/dist/cjs/telemetry/tracing.js +37 -15
  36. package/dist/cjs/tools/agent_tool.js +26 -13
  37. package/dist/cjs/tools/base_tool.js +19 -7
  38. package/dist/cjs/tools/forwarding_artifact_service.js +1 -1
  39. package/dist/cjs/tools/function_tool.js +15 -7
  40. package/dist/cjs/tools/google_search_tool.js +8 -4
  41. package/dist/cjs/tools/mcp/mcp_session_manager.js +16 -10
  42. package/dist/cjs/tools/mcp/mcp_tool.js +1 -3
  43. package/dist/cjs/tools/mcp/mcp_toolset.js +1 -1
  44. package/dist/cjs/tools/tool_context.js +4 -9
  45. package/dist/cjs/utils/env_aware_utils.js +1 -1
  46. package/dist/cjs/utils/gemini_schema_util.js +10 -4
  47. package/dist/cjs/utils/logger.js +47 -3
  48. package/dist/cjs/utils/simple_zod_to_json.js +100 -141
  49. package/dist/cjs/utils/variant_utils.js +1 -1
  50. package/dist/cjs/version.js +1 -1
  51. package/dist/esm/agents/base_agent.js +57 -25
  52. package/dist/esm/agents/callback_context.js +4 -1
  53. package/dist/esm/agents/content_processor_utils.js +25 -9
  54. package/dist/esm/agents/functions.js +83 -29
  55. package/dist/esm/agents/invocation_context.js +3 -1
  56. package/dist/esm/agents/llm_agent.js +228 -116
  57. package/dist/esm/agents/loop_agent.js +16 -5
  58. package/dist/esm/agents/parallel_agent.js +18 -6
  59. package/dist/esm/agents/readonly_context.js +4 -1
  60. package/dist/esm/agents/sequential_agent.js +33 -12
  61. package/dist/esm/artifacts/gcs_artifact_service.js +28 -20
  62. package/dist/esm/artifacts/in_memory_artifact_service.js +18 -4
  63. package/dist/esm/auth/auth_handler.js +3 -1
  64. package/dist/esm/code_executors/base_code_executor.js +12 -1
  65. package/dist/esm/code_executors/built_in_code_executor.js +19 -4
  66. package/dist/esm/code_executors/code_executor_context.js +5 -5
  67. package/dist/esm/common.js +56 -11
  68. package/dist/esm/events/event.js +1 -3
  69. package/dist/esm/examples/base_example_provider.js +16 -1
  70. package/dist/esm/examples/example_util.js +4 -2
  71. package/dist/esm/index.js +19 -19
  72. package/dist/esm/index.js.map +4 -4
  73. package/dist/esm/memory/in_memory_memory_service.js +3 -1
  74. package/dist/esm/models/base_llm.js +8 -4
  75. package/dist/esm/models/gemini_llm_connection.js +1 -0
  76. package/dist/esm/models/google_llm.js +8 -4
  77. package/dist/esm/plugins/base_plugin.js +71 -49
  78. package/dist/esm/plugins/logging_plugin.js +55 -14
  79. package/dist/esm/plugins/plugin_manager.js +56 -24
  80. package/dist/esm/plugins/security_plugin.js +5 -2
  81. package/dist/esm/runner/runner.js +139 -105
  82. package/dist/esm/sessions/in_memory_session_service.js +41 -15
  83. package/dist/esm/telemetry/google_cloud.js +7 -9
  84. package/dist/esm/telemetry/setup.js +23 -9
  85. package/dist/esm/telemetry/tracing.js +37 -15
  86. package/dist/esm/tools/agent_tool.js +25 -13
  87. package/dist/esm/tools/base_tool.js +17 -6
  88. package/dist/esm/tools/forwarding_artifact_service.js +1 -1
  89. package/dist/esm/tools/function_tool.js +13 -8
  90. package/dist/esm/tools/google_search_tool.js +6 -3
  91. package/dist/esm/tools/long_running_tool.js +3 -1
  92. package/dist/esm/tools/mcp/mcp_session_manager.js +22 -12
  93. package/dist/esm/tools/mcp/mcp_tool.js +1 -3
  94. package/dist/esm/tools/mcp/mcp_toolset.js +1 -1
  95. package/dist/esm/tools/tool_context.js +4 -9
  96. package/dist/esm/utils/env_aware_utils.js +1 -1
  97. package/dist/esm/utils/gemini_schema_util.js +10 -4
  98. package/dist/esm/utils/logger.js +43 -2
  99. package/dist/esm/utils/simple_zod_to_json.js +102 -141
  100. package/dist/esm/utils/variant_utils.js +1 -1
  101. package/dist/esm/version.js +1 -1
  102. package/dist/types/agents/base_agent.d.ts +16 -4
  103. package/dist/types/agents/callback_context.d.ts +1 -1
  104. package/dist/types/agents/invocation_context.d.ts +4 -2
  105. package/dist/types/agents/llm_agent.d.ts +43 -31
  106. package/dist/types/agents/loop_agent.d.ts +17 -1
  107. package/dist/types/agents/parallel_agent.d.ts +17 -1
  108. package/dist/types/agents/sequential_agent.d.ts +17 -1
  109. package/dist/types/artifacts/in_memory_artifact_service.d.ts +3 -3
  110. package/dist/types/auth/auth_schemes.d.ts +5 -2
  111. package/dist/types/code_executors/base_code_executor.d.ts +14 -0
  112. package/dist/types/code_executors/built_in_code_executor.d.ts +20 -1
  113. package/dist/types/code_executors/code_executor_context.d.ts +2 -4
  114. package/dist/types/common.d.ts +40 -15
  115. package/dist/types/examples/base_example_provider.d.ts +16 -0
  116. package/dist/types/index.d.ts +3 -3
  117. package/dist/types/models/base_llm_connection.d.ts +1 -1
  118. package/dist/types/models/llm_response.d.ts +1 -1
  119. package/dist/types/plugins/base_plugin.d.ts +50 -40
  120. package/dist/types/plugins/logging_plugin.d.ts +12 -12
  121. package/dist/types/plugins/plugin_manager.d.ts +12 -12
  122. package/dist/types/plugins/security_plugin.d.ts +1 -1
  123. package/dist/types/runner/runner.d.ts +18 -10
  124. package/dist/types/sessions/in_memory_session_service.d.ts +5 -5
  125. package/dist/types/telemetry/setup.d.ts +1 -1
  126. package/dist/types/telemetry/tracing.d.ts +7 -6
  127. package/dist/types/tools/agent_tool.d.ts +15 -1
  128. package/dist/types/tools/base_tool.d.ts +15 -1
  129. package/dist/types/tools/base_toolset.d.ts +2 -1
  130. package/dist/types/tools/forwarding_artifact_service.d.ts +2 -2
  131. package/dist/types/tools/function_tool.d.ts +18 -4
  132. package/dist/types/tools/google_search_tool.d.ts +5 -6
  133. package/dist/types/tools/mcp/mcp_session_manager.d.ts +10 -3
  134. package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -2
  135. package/dist/types/tools/tool_context.d.ts +1 -1
  136. package/dist/types/utils/gemini_schema_util.d.ts +4 -12
  137. package/dist/types/utils/logger.d.ts +11 -10
  138. package/dist/types/utils/simple_zod_to_json.d.ts +5 -4
  139. package/dist/types/version.d.ts +1 -1
  140. package/dist/web/agents/base_agent.js +103 -36
  141. package/dist/web/agents/callback_context.js +4 -1
  142. package/dist/web/agents/content_processor_utils.js +25 -9
  143. package/dist/web/agents/functions.js +83 -29
  144. package/dist/web/agents/invocation_context.js +3 -1
  145. package/dist/web/agents/llm_agent.js +282 -137
  146. package/dist/web/agents/loop_agent.js +16 -5
  147. package/dist/web/agents/parallel_agent.js +18 -6
  148. package/dist/web/agents/readonly_context.js +4 -1
  149. package/dist/web/agents/sequential_agent.js +33 -12
  150. package/dist/web/artifacts/gcs_artifact_service.js +25 -17
  151. package/dist/web/artifacts/in_memory_artifact_service.js +18 -4
  152. package/dist/web/auth/auth_handler.js +3 -1
  153. package/dist/web/code_executors/base_code_executor.js +12 -1
  154. package/dist/web/code_executors/built_in_code_executor.js +19 -4
  155. package/dist/web/code_executors/code_executor_context.js +5 -5
  156. package/dist/web/common.js +56 -11
  157. package/dist/web/events/event.js +1 -3
  158. package/dist/web/examples/base_example_provider.js +16 -1
  159. package/dist/web/examples/example_util.js +4 -2
  160. package/dist/web/index.js +1 -1
  161. package/dist/web/index.js.map +4 -4
  162. package/dist/web/memory/in_memory_memory_service.js +3 -1
  163. package/dist/web/models/base_llm.js +8 -4
  164. package/dist/web/models/gemini_llm_connection.js +1 -0
  165. package/dist/web/models/google_llm.js +8 -4
  166. package/dist/web/plugins/base_plugin.js +71 -49
  167. package/dist/web/plugins/logging_plugin.js +55 -14
  168. package/dist/web/plugins/plugin_manager.js +56 -24
  169. package/dist/web/plugins/security_plugin.js +5 -2
  170. package/dist/web/runner/runner.js +186 -119
  171. package/dist/web/sessions/in_memory_session_service.js +41 -15
  172. package/dist/web/telemetry/google_cloud.js +7 -9
  173. package/dist/web/telemetry/setup.js +23 -9
  174. package/dist/web/telemetry/tracing.js +37 -15
  175. package/dist/web/tools/agent_tool.js +25 -13
  176. package/dist/web/tools/base_tool.js +17 -6
  177. package/dist/web/tools/forwarding_artifact_service.js +1 -1
  178. package/dist/web/tools/function_tool.js +13 -8
  179. package/dist/web/tools/google_search_tool.js +6 -3
  180. package/dist/web/tools/long_running_tool.js +3 -1
  181. package/dist/web/tools/mcp/mcp_session_manager.js +22 -12
  182. package/dist/web/tools/mcp/mcp_tool.js +1 -3
  183. package/dist/web/tools/mcp/mcp_toolset.js +1 -1
  184. package/dist/web/tools/tool_context.js +4 -9
  185. package/dist/web/utils/env_aware_utils.js +1 -1
  186. package/dist/web/utils/gemini_schema_util.js +10 -4
  187. package/dist/web/utils/logger.js +43 -2
  188. package/dist/web/utils/simple_zod_to_json.js +102 -155
  189. package/dist/web/utils/variant_utils.js +1 -1
  190. package/dist/web/version.js +1 -1
  191. package/package.json +5 -3
@@ -3,6 +3,9 @@ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
3
  var __hasOwnProp = Object.prototype.hasOwnProperty;
4
4
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
5
  var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
6
+ var __typeError = (msg) => {
7
+ throw TypeError(msg);
8
+ };
6
9
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
10
  var __spreadValues = (a, b) => {
8
11
  for (var prop in b || (b = {}))
@@ -30,22 +33,68 @@ var __asyncGenerator = (__this, __arguments, generator) => {
30
33
  }, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
31
34
  return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
32
35
  };
36
+ var __yieldStar = (value) => {
37
+ var obj = value[__knownSymbol("asyncIterator")], isAwait = false, method, it = {};
38
+ if (obj == null) {
39
+ obj = value[__knownSymbol("iterator")]();
40
+ method = (k) => it[k] = (x) => obj[k](x);
41
+ } else {
42
+ obj = obj.call(value);
43
+ method = (k) => it[k] = (v) => {
44
+ if (isAwait) {
45
+ isAwait = false;
46
+ if (k === "throw") throw v;
47
+ return v;
48
+ }
49
+ isAwait = true;
50
+ return {
51
+ done: false,
52
+ value: new __await(new Promise((resolve) => {
53
+ var x = obj[k](v);
54
+ if (!(x instanceof Object)) __typeError("Object expected");
55
+ resolve(x);
56
+ }), 1)
57
+ };
58
+ };
59
+ }
60
+ return it[__knownSymbol("iterator")] = () => it, method("next"), "throw" in obj ? method("throw") : it.throw = (x) => {
61
+ throw x;
62
+ }, "return" in obj && method("return"), it;
63
+ };
33
64
  var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
34
65
  /**
35
66
  * @license
36
67
  * Copyright 2025 Google LLC
37
68
  * SPDX-License-Identifier: Apache-2.0
38
69
  */
70
+ var _a, _b;
71
+ import { context, trace } from "@opentelemetry/api";
39
72
  import { cloneDeep } from "lodash-es";
40
73
  import { z } from "zod";
41
- import { BaseCodeExecutor } from "../code_executors/base_code_executor.js";
42
- import { BuiltInCodeExecutor } from "../code_executors/built_in_code_executor.js";
43
- import { buildCodeExecutionResultPart, buildExecutableCodePart, convertCodeExecutionParts, extractCodeAndTruncateContent } from "../code_executors/code_execution_utils.js";
74
+ import {
75
+ isBaseCodeExecutor
76
+ } from "../code_executors/base_code_executor.js";
77
+ import { isBuiltInCodeExecutor } from "../code_executors/built_in_code_executor.js";
78
+ import {
79
+ buildCodeExecutionResultPart,
80
+ buildExecutableCodePart,
81
+ convertCodeExecutionParts,
82
+ extractCodeAndTruncateContent
83
+ } from "../code_executors/code_execution_utils.js";
44
84
  import { CodeExecutorContext } from "../code_executors/code_executor_context.js";
45
- import { createEvent, createNewEventId, getFunctionCalls, getFunctionResponses, isFinalResponse } from "../events/event.js";
85
+ import {
86
+ createEvent,
87
+ createNewEventId,
88
+ getFunctionCalls,
89
+ getFunctionResponses,
90
+ isFinalResponse
91
+ } from "../events/event.js";
46
92
  import { createEventActions } from "../events/event_actions.js";
47
93
  import { isBaseLlm } from "../models/base_llm.js";
48
- import { appendInstructions, setOutputSchema } from "../models/llm_request.js";
94
+ import {
95
+ appendInstructions,
96
+ setOutputSchema
97
+ } from "../models/llm_request.js";
49
98
  import { LLMRegistry } from "../models/registry.js";
50
99
  import { State } from "../sessions/state.js";
51
100
  import { BaseTool } from "../tools/base_tool.js";
@@ -54,31 +103,50 @@ import { ToolConfirmation } from "../tools/tool_confirmation.js";
54
103
  import { ToolContext } from "../tools/tool_context.js";
55
104
  import { base64Decode } from "../utils/env_aware_utils.js";
56
105
  import { logger } from "../utils/logger.js";
106
+ import {
107
+ runAsyncGeneratorWithOtelContext,
108
+ traceCallLlm,
109
+ tracer
110
+ } from "../telemetry/tracing.js";
57
111
  import { BaseAgent } from "./base_agent.js";
58
- import { BaseLlmRequestProcessor } from "./base_llm_processor.js";
112
+ import {
113
+ BaseLlmRequestProcessor
114
+ } from "./base_llm_processor.js";
59
115
  import { CallbackContext } from "./callback_context.js";
60
- import { getContents, getCurrentTurnContents } from "./content_processor_utils.js";
61
- import { generateAuthEvent, generateRequestConfirmationEvent, getLongRunningFunctionCalls, handleFunctionCallList, handleFunctionCallsAsync, populateClientFunctionCallId, REQUEST_CONFIRMATION_FUNCTION_CALL_NAME } from "./functions.js";
116
+ import {
117
+ getContents,
118
+ getCurrentTurnContents
119
+ } from "./content_processor_utils.js";
120
+ import {
121
+ generateAuthEvent,
122
+ generateRequestConfirmationEvent,
123
+ getLongRunningFunctionCalls,
124
+ handleFunctionCallList,
125
+ handleFunctionCallsAsync,
126
+ populateClientFunctionCallId,
127
+ REQUEST_CONFIRMATION_FUNCTION_CALL_NAME
128
+ } from "./functions.js";
62
129
  import { injectSessionState } from "./instructions.js";
63
130
  import { ReadonlyContext } from "./readonly_context.js";
64
131
  import { StreamingMode } from "./run_config.js";
65
132
  const ADK_AGENT_NAME_LABEL_KEY = "adk_agent_name";
66
- async function convertToolUnionToTools(toolUnion, context) {
133
+ async function convertToolUnionToTools(toolUnion, context2) {
67
134
  if (toolUnion instanceof BaseTool) {
68
135
  return [toolUnion];
69
136
  }
70
- return await toolUnion.getTools(context);
137
+ return await toolUnion.getTools(context2);
71
138
  }
72
139
  class BasicLlmRequestProcessor extends BaseLlmRequestProcessor {
140
+ // eslint-disable-next-line require-yield
73
141
  runAsync(invocationContext, llmRequest) {
74
142
  return __asyncGenerator(this, null, function* () {
75
- var _a;
143
+ var _a2;
76
144
  const agent = invocationContext.agent;
77
- if (!(agent instanceof LlmAgent)) {
145
+ if (!isLlmAgent(agent)) {
78
146
  return;
79
147
  }
80
148
  llmRequest.model = agent.canonicalModel.model;
81
- llmRequest.config = __spreadValues({}, (_a = agent.generateContentConfig) != null ? _a : {});
149
+ llmRequest.config = __spreadValues({}, (_a2 = agent.generateContentConfig) != null ? _a2 : {});
82
150
  if (agent.outputSchema) {
83
151
  setOutputSchema(llmRequest, agent.outputSchema);
84
152
  }
@@ -96,6 +164,7 @@ class BasicLlmRequestProcessor extends BaseLlmRequestProcessor {
96
164
  }
97
165
  const BASIC_LLM_REQUEST_PROCESSOR = new BasicLlmRequestProcessor();
98
166
  class IdentityLlmRequestProcessor extends BaseLlmRequestProcessor {
167
+ // eslint-disable-next-line require-yield
99
168
  runAsync(invocationContext, llmRequest) {
100
169
  return __asyncGenerator(this, null, function* () {
101
170
  const agent = invocationContext.agent;
@@ -112,6 +181,7 @@ class InstructionsLlmRequestProcessor extends BaseLlmRequestProcessor {
112
181
  /**
113
182
  * Handles instructions and global instructions for LLM flow.
114
183
  */
184
+ // eslint-disable-next-line require-yield
115
185
  runAsync(invocationContext, llmRequest) {
116
186
  return __asyncGenerator(this, null, function* () {
117
187
  const agent = invocationContext.agent;
@@ -119,7 +189,7 @@ class InstructionsLlmRequestProcessor extends BaseLlmRequestProcessor {
119
189
  return;
120
190
  }
121
191
  const rootAgent = agent.rootAgent;
122
- if (rootAgent instanceof LlmAgent && rootAgent.globalInstruction) {
192
+ if (isLlmAgent(rootAgent) && rootAgent.globalInstruction) {
123
193
  const { instruction, requireStateInjection } = yield new __await(rootAgent.canonicalGlobalInstruction(
124
194
  new ReadonlyContext(invocationContext)
125
195
  ));
@@ -150,10 +220,11 @@ class InstructionsLlmRequestProcessor extends BaseLlmRequestProcessor {
150
220
  }
151
221
  const INSTRUCTIONS_LLM_REQUEST_PROCESSOR = new InstructionsLlmRequestProcessor();
152
222
  class ContentRequestProcessor {
223
+ // eslint-disable-next-line require-yield
153
224
  runAsync(invocationContext, llmRequest) {
154
225
  return __asyncGenerator(this, null, function* () {
155
226
  const agent = invocationContext.agent;
156
- if (!agent || !(agent instanceof LlmAgent)) {
227
+ if (!agent || !isLlmAgent(agent)) {
157
228
  return;
158
229
  }
159
230
  if (agent.includeContents === "default") {
@@ -193,6 +264,7 @@ class AgentTransferLlmRequestProcessor extends BaseLlmRequestProcessor {
193
264
  }
194
265
  });
195
266
  }
267
+ // eslint-disable-next-line require-yield
196
268
  runAsync(invocationContext, llmRequest) {
197
269
  return __asyncGenerator(this, null, function* () {
198
270
  if (!(invocationContext.agent instanceof LlmAgent)) {
@@ -225,7 +297,7 @@ class AgentTransferLlmRequestProcessor extends BaseLlmRequestProcessor {
225
297
  getTransferTargets(agent) {
226
298
  const targets = [];
227
299
  targets.push(...agent.subAgents);
228
- if (!agent.parentAgent || !(agent.parentAgent instanceof LlmAgent)) {
300
+ if (!agent.parentAgent || !isLlmAgent(agent.parentAgent)) {
229
301
  return targets;
230
302
  }
231
303
  if (!agent.disallowTransferToParent) {
@@ -244,10 +316,10 @@ class AgentTransferLlmRequestProcessor extends BaseLlmRequestProcessor {
244
316
  const AGENT_TRANSFER_LLM_REQUEST_PROCESSOR = new AgentTransferLlmRequestProcessor();
245
317
  class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
246
318
  /** Handles tool confirmation information to build the LLM request. */
247
- runAsync(invocationContext, llmRequest) {
319
+ runAsync(invocationContext) {
248
320
  return __asyncGenerator(this, null, function* () {
249
321
  const agent = invocationContext.agent;
250
- if (!(agent instanceof LlmAgent)) {
322
+ if (!isLlmAgent(agent)) {
251
323
  return;
252
324
  }
253
325
  const events = invocationContext.session.events;
@@ -273,7 +345,9 @@ class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
273
345
  foundConfirmation = true;
274
346
  let toolConfirmation = null;
275
347
  if (functionResponse.response && Object.keys(functionResponse.response).length === 1 && "response" in functionResponse.response) {
276
- toolConfirmation = JSON.parse(functionResponse.response["response"]);
348
+ toolConfirmation = JSON.parse(
349
+ functionResponse.response["response"]
350
+ );
277
351
  } else if (functionResponse.response) {
278
352
  toolConfirmation = new ToolConfirmation({
279
353
  hint: functionResponse.response["hint"],
@@ -337,8 +411,12 @@ class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
337
411
  if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
338
412
  continue;
339
413
  }
340
- const toolsList = yield new __await(agent.canonicalTools(new ReadonlyContext(invocationContext)));
341
- const toolsDict = Object.fromEntries(toolsList.map((tool) => [tool.name, tool]));
414
+ const toolsList = yield new __await(agent.canonicalTools(
415
+ new ReadonlyContext(invocationContext)
416
+ ));
417
+ const toolsDict = Object.fromEntries(
418
+ toolsList.map((tool) => [tool.name, tool])
419
+ );
342
420
  const functionResponseEvent = yield new __await(handleFunctionCallList({
343
421
  invocationContext,
344
422
  functionCalls: Object.values(toolsToResumeWithArgs),
@@ -381,12 +459,12 @@ class CodeExecutionRequestProcessor extends BaseLlmRequestProcessor {
381
459
  throw error[0];
382
460
  }
383
461
  }
384
- if (!(invocationContext.agent.codeExecutor instanceof BaseCodeExecutor)) {
462
+ if (!isBaseCodeExecutor(invocationContext.agent.codeExecutor)) {
385
463
  return;
386
464
  }
387
465
  for (const content of llmRequest.contents) {
388
466
  const delimeters = invocationContext.agent.codeExecutor.codeBlockDelimiters.length ? invocationContext.agent.codeExecutor.codeBlockDelimiters[0] : ["", ""];
389
- const codeExecutionParts = convertCodeExecutionParts(
467
+ convertCodeExecutionParts(
390
468
  content,
391
469
  delimeters,
392
470
  invocationContext.agent.codeExecutor.executionResultDelimiters
@@ -416,7 +494,10 @@ class CodeExecutionResponseProcessor {
416
494
  return;
417
495
  }
418
496
  try {
419
- for (var iter = __forAwait(runPostProcessor(invocationContext, llmResponse)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
497
+ for (var iter = __forAwait(runPostProcessor(
498
+ invocationContext,
499
+ llmResponse
500
+ )), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
420
501
  const event = temp.value;
421
502
  yield event;
422
503
  }
@@ -437,27 +518,36 @@ const responseProcessor = new CodeExecutionResponseProcessor();
437
518
  function runPreProcessor(invocationContext, llmRequest) {
438
519
  return __asyncGenerator(this, null, function* () {
439
520
  const agent = invocationContext.agent;
440
- if (!(agent instanceof LlmAgent)) {
521
+ if (!isLlmAgent(agent)) {
441
522
  return;
442
523
  }
443
524
  const codeExecutor = agent.codeExecutor;
444
- if (!codeExecutor || !(codeExecutor instanceof BaseCodeExecutor)) {
525
+ if (!codeExecutor || !isBaseCodeExecutor(codeExecutor)) {
445
526
  return;
446
527
  }
447
- if (codeExecutor instanceof BuiltInCodeExecutor) {
528
+ if (isBuiltInCodeExecutor(codeExecutor)) {
448
529
  codeExecutor.processLlmRequest(llmRequest);
449
530
  return;
450
531
  }
451
532
  if (!codeExecutor.optimizeDataFile) {
452
533
  return;
453
534
  }
454
- const codeExecutorContext = new CodeExecutorContext(new State(invocationContext.session.state));
535
+ const codeExecutorContext = new CodeExecutorContext(
536
+ new State(invocationContext.session.state)
537
+ );
455
538
  if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
456
539
  return;
457
540
  }
458
- const allInputFiles = extractAndReplaceInlineFiles(codeExecutorContext, llmRequest);
459
- const processedFileNames = new Set(codeExecutorContext.getProcessedFileNames());
460
- const filesToProcess = allInputFiles.filter((f) => !processedFileNames.has(f.name));
541
+ const allInputFiles = extractAndReplaceInlineFiles(
542
+ codeExecutorContext,
543
+ llmRequest
544
+ );
545
+ const processedFileNames = new Set(
546
+ codeExecutorContext.getProcessedFileNames()
547
+ );
548
+ const filesToProcess = allInputFiles.filter(
549
+ (f) => !processedFileNames.has(f.name)
550
+ );
461
551
  for (const file of filesToProcess) {
462
552
  const codeStr = getDataFilePreprocessingCode(file);
463
553
  if (!codeStr) {
@@ -477,7 +567,10 @@ function runPreProcessor(invocationContext, llmRequest) {
477
567
  branch: invocationContext.branch,
478
568
  content: codeContent
479
569
  });
480
- const executionId = getOrSetExecutionId(invocationContext, codeExecutorContext);
570
+ const executionId = getOrSetExecutionId(
571
+ invocationContext,
572
+ codeExecutorContext
573
+ );
481
574
  const codeExecutionResult = yield new __await(codeExecutor.executeCode({
482
575
  invocationContext,
483
576
  codeExecutionInput: {
@@ -506,20 +599,22 @@ function runPreProcessor(invocationContext, llmRequest) {
506
599
  function runPostProcessor(invocationContext, llmResponse) {
507
600
  return __asyncGenerator(this, null, function* () {
508
601
  const agent = invocationContext.agent;
509
- if (!(agent instanceof LlmAgent)) {
602
+ if (!isLlmAgent(agent)) {
510
603
  return;
511
604
  }
512
605
  const codeExecutor = agent.codeExecutor;
513
- if (!codeExecutor || !(codeExecutor instanceof BaseCodeExecutor)) {
606
+ if (!codeExecutor || !isBaseCodeExecutor(codeExecutor)) {
514
607
  return;
515
608
  }
516
609
  if (!llmResponse || !llmResponse.content) {
517
610
  return;
518
611
  }
519
- if (codeExecutor instanceof BuiltInCodeExecutor) {
612
+ if (isBuiltInCodeExecutor(codeExecutor)) {
520
613
  return;
521
614
  }
522
- const codeExecutorContext = new CodeExecutorContext(new State(invocationContext.session.state));
615
+ const codeExecutorContext = new CodeExecutorContext(
616
+ new State(invocationContext.session.state)
617
+ );
523
618
  if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
524
619
  return;
525
620
  }
@@ -537,7 +632,10 @@ function runPostProcessor(invocationContext, llmResponse) {
537
632
  branch: invocationContext.branch,
538
633
  content: responseContent
539
634
  });
540
- const executionId = getOrSetExecutionId(invocationContext, codeExecutorContext);
635
+ const executionId = getOrSetExecutionId(
636
+ invocationContext,
637
+ codeExecutorContext
638
+ );
541
639
  const codeExecutionResult = yield new __await(codeExecutor.executeCode({
542
640
  invocationContext,
543
641
  codeExecutionInput: {
@@ -557,11 +655,11 @@ function runPostProcessor(invocationContext, llmResponse) {
557
655
  codeExecutorContext,
558
656
  codeExecutionResult
559
657
  ));
560
- llmResponse.content = null;
658
+ llmResponse.content = void 0;
561
659
  });
562
660
  }
563
661
  function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
564
- var _a;
662
+ var _a2;
565
663
  const allInputFiles = codeExecutorContext.getInputFiles();
566
664
  const savedFileNames = new Set(allInputFiles.map((f) => f.name));
567
665
  for (let i = 0; i < llmRequest.contents.length; i++) {
@@ -571,7 +669,7 @@ function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
571
669
  }
572
670
  for (let j = 0; j < content.parts.length; j++) {
573
671
  const part = content.parts[j];
574
- const mimeType = (_a = part.inlineData) == null ? void 0 : _a.mimeType;
672
+ const mimeType = (_a2 = part.inlineData) == null ? void 0 : _a2.mimeType;
575
673
  if (!mimeType || !part.inlineData || !DATA_FILE_UTIL_MAP[mimeType]) {
576
674
  continue;
577
675
  }
@@ -591,9 +689,9 @@ function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
591
689
  return allInputFiles;
592
690
  }
593
691
  function getOrSetExecutionId(invocationContext, codeExecutorContext) {
594
- var _a;
692
+ var _a2;
595
693
  const agent = invocationContext.agent;
596
- if (!(agent instanceof LlmAgent) || !((_a = agent.codeExecutor) == null ? void 0 : _a.stateful)) {
694
+ if (!isLlmAgent(agent) || !((_a2 = agent.codeExecutor) == null ? void 0 : _a2.stateful)) {
597
695
  return void 0;
598
696
  }
599
697
  let executionId = codeExecutorContext.getExecutionId();
@@ -611,7 +709,9 @@ async function postProcessCodeExecutionResult(invocationContext, codeExecutorCon
611
709
  role: "model",
612
710
  parts: [buildCodeExecutionResultPart(codeExecutionResult)]
613
711
  };
614
- const eventActions = createEventActions({ stateDelta: codeExecutorContext.getStateDelta() });
712
+ const eventActions = createEventActions({
713
+ stateDelta: codeExecutorContext.getStateDelta()
714
+ });
615
715
  if (codeExecutionResult.stderr) {
616
716
  codeExecutorContext.incrementErrorCount(invocationContext.invocationId);
617
717
  } else {
@@ -650,20 +750,23 @@ function getDataFilePreprocessingCode(file) {
650
750
  return void 0;
651
751
  }
652
752
  const varName = getNormalizedFileName(file.name);
653
- const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace(
654
- "{filename}",
655
- file.name
656
- );
753
+ const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace("{filename}", file.name);
657
754
  return "\n".concat(DATA_FILE_HELPER_LIB, "\n\n# Load the dataframe.\n").concat(varName, " = ").concat(loaderCode, "\n\n# Use `explore_df` to guide my analysis.\nexplore_df(").concat(varName, ")\n");
658
755
  }
659
756
  const CODE_EXECUTION_REQUEST_PROCESSOR = new CodeExecutionRequestProcessor();
660
- class LlmAgent extends BaseAgent {
757
+ const LLM_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.llmAgent");
758
+ function isLlmAgent(obj) {
759
+ return typeof obj === "object" && obj !== null && LLM_AGENT_SIGNATURE_SYMBOL in obj && obj[LLM_AGENT_SIGNATURE_SYMBOL] === true;
760
+ }
761
+ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
661
762
  constructor(config) {
662
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
763
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
663
764
  super(config);
765
+ /** A unique symbol to identify ADK LLM agent class. */
766
+ this[_a] = true;
664
767
  this.model = config.model;
665
- this.instruction = (_a = config.instruction) != null ? _a : "";
666
- this.globalInstruction = (_b = config.globalInstruction) != null ? _b : "";
768
+ this.instruction = (_a2 = config.instruction) != null ? _a2 : "";
769
+ this.globalInstruction = (_b2 = config.globalInstruction) != null ? _b2 : "";
667
770
  this.tools = (_c = config.tools) != null ? _c : [];
668
771
  this.generateContentConfig = config.generateContentConfig;
669
772
  this.disallowTransferToParent = (_d = config.disallowTransferToParent) != null ? _d : false;
@@ -741,7 +844,7 @@ class LlmAgent extends BaseAgent {
741
844
  }
742
845
  let ancestorAgent = this.parentAgent;
743
846
  while (ancestorAgent) {
744
- if (ancestorAgent instanceof LlmAgent) {
847
+ if (isLlmAgent(ancestorAgent)) {
745
848
  return ancestorAgent.canonicalModel;
746
849
  }
747
850
  ancestorAgent = ancestorAgent.parentAgent;
@@ -749,47 +852,50 @@ class LlmAgent extends BaseAgent {
749
852
  throw new Error("No model found for ".concat(this.name, "."));
750
853
  }
751
854
  /**
752
- * The resolved self.instruction field to construct instruction for this
855
+ * The resolved instruction field to construct instruction for this
753
856
  * agent.
754
857
  *
755
858
  * This method is only for use by Agent Development Kit.
756
859
  * @param context The context to retrieve the session state.
757
- * @returns The resolved self.instruction field.
860
+ * @returns The resolved instruction field.
758
861
  */
759
- async canonicalInstruction(context) {
862
+ async canonicalInstruction(context2) {
760
863
  if (typeof this.instruction === "string") {
761
864
  return { instruction: this.instruction, requireStateInjection: true };
762
865
  }
763
866
  return {
764
- instruction: await this.instruction(context),
867
+ instruction: await this.instruction(context2),
765
868
  requireStateInjection: false
766
869
  };
767
870
  }
768
871
  /**
769
- * The resolved self.instruction field to construct global instruction.
872
+ * The resolved globalInstruction field to construct global instruction.
770
873
  *
771
874
  * This method is only for use by Agent Development Kit.
772
875
  * @param context The context to retrieve the session state.
773
- * @returns The resolved self.global_instruction field.
876
+ * @returns The resolved globalInstruction field.
774
877
  */
775
- async canonicalGlobalInstruction(context) {
878
+ async canonicalGlobalInstruction(context2) {
776
879
  if (typeof this.globalInstruction === "string") {
777
- return { instruction: this.globalInstruction, requireStateInjection: true };
880
+ return {
881
+ instruction: this.globalInstruction,
882
+ requireStateInjection: true
883
+ };
778
884
  }
779
885
  return {
780
- instruction: await this.globalInstruction(context),
886
+ instruction: await this.globalInstruction(context2),
781
887
  requireStateInjection: false
782
888
  };
783
889
  }
784
890
  /**
785
- * The resolved self.tools field as a list of BaseTool based on the context.
891
+ * The resolved tools field as a list of BaseTool based on the context.
786
892
  *
787
893
  * This method is only for use by Agent Development Kit.
788
894
  */
789
- async canonicalTools(context) {
895
+ async canonicalTools(context2) {
790
896
  const resolvedTools = [];
791
897
  for (const toolUnion of this.tools) {
792
- const tools = await convertToolUnionToTools(toolUnion, context);
898
+ const tools = await convertToolUnionToTools(toolUnion, context2);
793
899
  resolvedTools.push(...tools);
794
900
  }
795
901
  return resolvedTools;
@@ -810,7 +916,7 @@ class LlmAgent extends BaseAgent {
810
916
  return [callback];
811
917
  }
812
918
  /**
813
- * The resolved self.before_model_callback field as a list of
919
+ * The resolved beforeModelCallback field as a list of
814
920
  * SingleBeforeModelCallback.
815
921
  *
816
922
  * This method is only for use by Agent Development Kit.
@@ -819,7 +925,7 @@ class LlmAgent extends BaseAgent {
819
925
  return LlmAgent.normalizeCallbackArray(this.beforeModelCallback);
820
926
  }
821
927
  /**
822
- * The resolved self.after_model_callback field as a list of
928
+ * The resolved afterModelCallback field as a list of
823
929
  * SingleAfterModelCallback.
824
930
  *
825
931
  * This method is only for use by Agent Development Kit.
@@ -828,7 +934,7 @@ class LlmAgent extends BaseAgent {
828
934
  return LlmAgent.normalizeCallbackArray(this.afterModelCallback);
829
935
  }
830
936
  /**
831
- * The resolved self.before_tool_callback field as a list of
937
+ * The resolved beforeToolCallback field as a list of
832
938
  * BeforeToolCallback.
833
939
  *
834
940
  * This method is only for use by Agent Development Kit.
@@ -837,7 +943,7 @@ class LlmAgent extends BaseAgent {
837
943
  return LlmAgent.normalizeCallbackArray(this.beforeToolCallback);
838
944
  }
839
945
  /**
840
- * The resolved self.after_tool_callback field as a list of AfterToolCallback.
946
+ * The resolved afterToolCallback field as a list of AfterToolCallback.
841
947
  *
842
948
  * This method is only for use by Agent Development Kit.
843
949
  */
@@ -854,7 +960,7 @@ class LlmAgent extends BaseAgent {
854
960
  * @param event The event to process.
855
961
  */
856
962
  maybeSaveOutputToState(event) {
857
- var _a, _b;
963
+ var _a2, _b2;
858
964
  if (event.author !== this.name) {
859
965
  logger.debug(
860
966
  "Skipping output save for agent ".concat(this.name, ": event authored by ").concat(event.author)
@@ -873,7 +979,7 @@ class LlmAgent extends BaseAgent {
873
979
  );
874
980
  return;
875
981
  }
876
- if (!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.length)) {
982
+ if (!((_b2 = (_a2 = event.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2.length)) {
877
983
  logger.debug(
878
984
  "Skipping output save for agent ".concat(this.name, ": event content is empty")
879
985
  );
@@ -893,12 +999,12 @@ class LlmAgent extends BaseAgent {
893
999
  }
894
1000
  event.actions.stateDelta[this.outputKey] = result;
895
1001
  }
896
- runAsyncImpl(context) {
1002
+ runAsyncImpl(context2) {
897
1003
  return __asyncGenerator(this, null, function* () {
898
1004
  while (true) {
899
1005
  let lastEvent = void 0;
900
1006
  try {
901
- for (var iter = __forAwait(this.runOneStepAsync(context)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
1007
+ for (var iter = __forAwait(this.runOneStepAsync(context2)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
902
1008
  const event = temp.value;
903
1009
  lastEvent = event;
904
1010
  this.maybeSaveOutputToState(event);
@@ -924,10 +1030,10 @@ class LlmAgent extends BaseAgent {
924
1030
  }
925
1031
  });
926
1032
  }
927
- runLiveImpl(context) {
1033
+ runLiveImpl(context2) {
928
1034
  return __asyncGenerator(this, null, function* () {
929
1035
  try {
930
- for (var iter = __forAwait(this.runLiveFlow(context)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
1036
+ for (var iter = __forAwait(this.runLiveFlow(context2)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
931
1037
  const event = temp.value;
932
1038
  this.maybeSaveOutputToState(event);
933
1039
  yield event;
@@ -942,7 +1048,7 @@ class LlmAgent extends BaseAgent {
942
1048
  throw error[0];
943
1049
  }
944
1050
  }
945
- if (context.endInvocation) {
1051
+ if (context2.endInvocation) {
946
1052
  return;
947
1053
  }
948
1054
  });
@@ -950,7 +1056,8 @@ class LlmAgent extends BaseAgent {
950
1056
  // --------------------------------------------------------------------------
951
1057
  // #START LlmFlow Logic
952
1058
  // --------------------------------------------------------------------------
953
- runLiveFlow(invocationContext) {
1059
+ // eslint-disable-next-line require-yield
1060
+ runLiveFlow(_invocationContext) {
954
1061
  return __asyncGenerator(this, null, function* () {
955
1062
  yield new __await(Promise.resolve());
956
1063
  throw new Error("LlmAgent.runLiveFlow not implemented");
@@ -965,7 +1072,10 @@ class LlmAgent extends BaseAgent {
965
1072
  };
966
1073
  for (const processor of this.requestProcessors) {
967
1074
  try {
968
- for (var iter = __forAwait(processor.runAsync(invocationContext, llmRequest)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
1075
+ for (var iter = __forAwait(processor.runAsync(
1076
+ invocationContext,
1077
+ llmRequest
1078
+ )), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
969
1079
  const event = temp.value;
970
1080
  yield event;
971
1081
  }
@@ -998,54 +1108,68 @@ class LlmAgent extends BaseAgent {
998
1108
  author: this.name,
999
1109
  branch: invocationContext.branch
1000
1110
  });
1001
- try {
1002
- for (var iter3 = __forAwait(this.callLlmAsync(
1003
- invocationContext,
1004
- llmRequest,
1005
- modelResponseEvent
1006
- )), more3, temp3, error3; more3 = !(temp3 = yield new __await(iter3.next())).done; more3 = false) {
1007
- const llmResponse = temp3.value;
1008
- try {
1009
- for (var iter2 = __forAwait(this.postprocess(
1010
- invocationContext,
1011
- llmRequest,
1012
- llmResponse,
1013
- modelResponseEvent
1014
- )), more2, temp2, error2; more2 = !(temp2 = yield new __await(iter2.next())).done; more2 = false) {
1015
- const event = temp2.value;
1016
- modelResponseEvent.id = createNewEventId();
1017
- modelResponseEvent.timestamp = (/* @__PURE__ */ new Date()).getTime();
1018
- yield event;
1019
- }
1020
- } catch (temp2) {
1021
- error2 = [temp2];
1022
- } finally {
1111
+ const span = tracer.startSpan("call_llm");
1112
+ const ctx = trace.setSpan(context.active(), span);
1113
+ yield* __yieldStar(runAsyncGeneratorWithOtelContext(
1114
+ ctx,
1115
+ this,
1116
+ function() {
1117
+ return __asyncGenerator(this, null, function* () {
1023
1118
  try {
1024
- more2 && (temp2 = iter2.return) && (yield new __await(temp2.call(iter2)));
1119
+ for (var iter3 = __forAwait(this.callLlmAsync(
1120
+ invocationContext,
1121
+ llmRequest,
1122
+ modelResponseEvent
1123
+ )), more3, temp3, error3; more3 = !(temp3 = yield new __await(iter3.next())).done; more3 = false) {
1124
+ const llmResponse = temp3.value;
1125
+ try {
1126
+ for (var iter2 = __forAwait(this.postprocess(
1127
+ invocationContext,
1128
+ llmRequest,
1129
+ llmResponse,
1130
+ modelResponseEvent
1131
+ )), more2, temp2, error2; more2 = !(temp2 = yield new __await(iter2.next())).done; more2 = false) {
1132
+ const event = temp2.value;
1133
+ modelResponseEvent.id = createNewEventId();
1134
+ modelResponseEvent.timestamp = (/* @__PURE__ */ new Date()).getTime();
1135
+ yield event;
1136
+ }
1137
+ } catch (temp2) {
1138
+ error2 = [temp2];
1139
+ } finally {
1140
+ try {
1141
+ more2 && (temp2 = iter2.return) && (yield new __await(temp2.call(iter2)));
1142
+ } finally {
1143
+ if (error2)
1144
+ throw error2[0];
1145
+ }
1146
+ }
1147
+ }
1148
+ } catch (temp3) {
1149
+ error3 = [temp3];
1025
1150
  } finally {
1026
- if (error2)
1027
- throw error2[0];
1151
+ try {
1152
+ more3 && (temp3 = iter3.return) && (yield new __await(temp3.call(iter3)));
1153
+ } finally {
1154
+ if (error3)
1155
+ throw error3[0];
1156
+ }
1028
1157
  }
1029
- }
1030
- }
1031
- } catch (temp3) {
1032
- error3 = [temp3];
1033
- } finally {
1034
- try {
1035
- more3 && (temp3 = iter3.return) && (yield new __await(temp3.call(iter3)));
1036
- } finally {
1037
- if (error3)
1038
- throw error3[0];
1158
+ });
1039
1159
  }
1040
- }
1160
+ ));
1161
+ span.end();
1041
1162
  });
1042
1163
  }
1043
1164
  postprocess(invocationContext, llmRequest, llmResponse, modelResponseEvent) {
1044
1165
  return __asyncGenerator(this, null, function* () {
1045
- var _a;
1166
+ var _a2;
1046
1167
  for (const processor of this.responseProcessors) {
1047
1168
  try {
1048
- for (var iter = __forAwait(processor.runAsync(invocationContext, llmResponse)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
1169
+ for (var iter = __forAwait(processor.runAsync(
1170
+ invocationContext,
1171
+ llmResponse
1172
+ )), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
1049
1173
  const event = temp.value;
1050
1174
  yield event;
1051
1175
  }
@@ -1074,7 +1198,7 @@ class LlmAgent extends BaseAgent {
1074
1198
  }
1075
1199
  }
1076
1200
  yield mergedEvent;
1077
- if (!((_a = getFunctionCalls(mergedEvent)) == null ? void 0 : _a.length)) {
1201
+ if (!((_a2 = getFunctionCalls(mergedEvent)) == null ? void 0 : _a2.length)) {
1078
1202
  return;
1079
1203
  }
1080
1204
  const functionResponseEvent = yield new __await(handleFunctionCallsAsync({
@@ -1087,7 +1211,10 @@ class LlmAgent extends BaseAgent {
1087
1211
  if (!functionResponseEvent) {
1088
1212
  return;
1089
1213
  }
1090
- const authEvent = generateAuthEvent(invocationContext, functionResponseEvent);
1214
+ const authEvent = generateAuthEvent(
1215
+ invocationContext,
1216
+ functionResponseEvent
1217
+ );
1091
1218
  if (authEvent) {
1092
1219
  yield authEvent;
1093
1220
  }
@@ -1143,7 +1270,7 @@ class LlmAgent extends BaseAgent {
1143
1270
  }
1144
1271
  callLlmAsync(invocationContext, llmRequest, modelResponseEvent) {
1145
1272
  return __asyncGenerator(this, null, function* () {
1146
- var _a, _b, _c, _d, _e;
1273
+ var _a2, _b2, _c, _d, _e;
1147
1274
  const beforeModelResponse = yield new __await(this.handleBeforeModelCallback(
1148
1275
  invocationContext,
1149
1276
  llmRequest,
@@ -1153,8 +1280,8 @@ class LlmAgent extends BaseAgent {
1153
1280
  yield beforeModelResponse;
1154
1281
  return;
1155
1282
  }
1156
- (_a = llmRequest.config) != null ? _a : llmRequest.config = {};
1157
- (_c = (_b = llmRequest.config).labels) != null ? _c : _b.labels = {};
1283
+ (_a2 = llmRequest.config) != null ? _a2 : llmRequest.config = {};
1284
+ (_c = (_b2 = llmRequest.config).labels) != null ? _c : _b2.labels = {};
1158
1285
  if (!llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY]) {
1159
1286
  llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY] = this.name;
1160
1287
  }
@@ -1176,6 +1303,12 @@ class LlmAgent extends BaseAgent {
1176
1303
  modelResponseEvent
1177
1304
  )), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
1178
1305
  const llmResponse = temp.value;
1306
+ traceCallLlm({
1307
+ invocationContext,
1308
+ eventId: modelResponseEvent.id,
1309
+ llmRequest,
1310
+ llmResponse
1311
+ });
1179
1312
  const alteredLlmResponse = yield new __await(this.handleAfterModelCallback(
1180
1313
  invocationContext,
1181
1314
  llmResponse,
@@ -1197,17 +1330,22 @@ class LlmAgent extends BaseAgent {
1197
1330
  });
1198
1331
  }
1199
1332
  async handleBeforeModelCallback(invocationContext, llmRequest, modelResponseEvent) {
1200
- const callbackContext = new CallbackContext(
1201
- { invocationContext, eventActions: modelResponseEvent.actions }
1202
- );
1203
- const beforeModelCallbackResponse = await invocationContext.pluginManager.runBeforeModelCallback(
1204
- { callbackContext, llmRequest }
1205
- );
1333
+ const callbackContext = new CallbackContext({
1334
+ invocationContext,
1335
+ eventActions: modelResponseEvent.actions
1336
+ });
1337
+ const beforeModelCallbackResponse = await invocationContext.pluginManager.runBeforeModelCallback({
1338
+ callbackContext,
1339
+ llmRequest
1340
+ });
1206
1341
  if (beforeModelCallbackResponse) {
1207
1342
  return beforeModelCallbackResponse;
1208
1343
  }
1209
1344
  for (const callback of this.canonicalBeforeModelCallbacks) {
1210
- const callbackResponse = await callback({ context: callbackContext, request: llmRequest });
1345
+ const callbackResponse = await callback({
1346
+ context: callbackContext,
1347
+ request: llmRequest
1348
+ });
1211
1349
  if (callbackResponse) {
1212
1350
  return callbackResponse;
1213
1351
  }
@@ -1215,17 +1353,22 @@ class LlmAgent extends BaseAgent {
1215
1353
  return void 0;
1216
1354
  }
1217
1355
  async handleAfterModelCallback(invocationContext, llmResponse, modelResponseEvent) {
1218
- const callbackContext = new CallbackContext(
1219
- { invocationContext, eventActions: modelResponseEvent.actions }
1220
- );
1221
- const afterModelCallbackResponse = await invocationContext.pluginManager.runAfterModelCallback(
1222
- { callbackContext, llmResponse }
1223
- );
1356
+ const callbackContext = new CallbackContext({
1357
+ invocationContext,
1358
+ eventActions: modelResponseEvent.actions
1359
+ });
1360
+ const afterModelCallbackResponse = await invocationContext.pluginManager.runAfterModelCallback({
1361
+ callbackContext,
1362
+ llmResponse
1363
+ });
1224
1364
  if (afterModelCallbackResponse) {
1225
1365
  return afterModelCallbackResponse;
1226
1366
  }
1227
1367
  for (const callback of this.canonicalAfterModelCallbacks) {
1228
- const callbackResponse = await callback({ context: callbackContext, response: llmResponse });
1368
+ const callbackResponse = await callback({
1369
+ context: callbackContext,
1370
+ response: llmResponse
1371
+ });
1229
1372
  if (callbackResponse) {
1230
1373
  return callbackResponse;
1231
1374
  }
@@ -1251,9 +1394,10 @@ class LlmAgent extends BaseAgent {
1251
1394
  }
1252
1395
  }
1253
1396
  } catch (modelError) {
1254
- const callbackContext = new CallbackContext(
1255
- { invocationContext, eventActions: modelResponseEvent.actions }
1256
- );
1397
+ const callbackContext = new CallbackContext({
1398
+ invocationContext,
1399
+ eventActions: modelResponseEvent.actions
1400
+ });
1257
1401
  if (modelError instanceof Error) {
1258
1402
  const onModelErrorCallbackResponse = yield new __await(invocationContext.pluginManager.runOnModelErrorCallback({
1259
1403
  callbackContext,
@@ -1286,5 +1430,6 @@ class LlmAgent extends BaseAgent {
1286
1430
  export {
1287
1431
  LlmAgent,
1288
1432
  REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
1433
+ isLlmAgent,
1289
1434
  responseProcessor
1290
1435
  };