@google/adk 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/dist/cjs/agents/base_agent.js +43 -21
  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/llm_agent.js +59 -33
  6. package/dist/cjs/agents/loop_agent.js +2 -1
  7. package/dist/cjs/agents/parallel_agent.js +3 -4
  8. package/dist/cjs/artifacts/gcs_artifact_service.js +28 -20
  9. package/dist/cjs/artifacts/in_memory_artifact_service.js +18 -4
  10. package/dist/cjs/auth/auth_handler.js +3 -1
  11. package/dist/cjs/code_executors/base_code_executor.js +3 -1
  12. package/dist/cjs/code_executors/built_in_code_executor.js +7 -3
  13. package/dist/cjs/code_executors/code_executor_context.js +5 -5
  14. package/dist/cjs/common.js +4 -0
  15. package/dist/cjs/events/event.js +1 -3
  16. package/dist/cjs/index.js +19 -19
  17. package/dist/cjs/index.js.map +4 -4
  18. package/dist/cjs/memory/in_memory_memory_service.js +3 -1
  19. package/dist/cjs/models/base_llm.js +8 -4
  20. package/dist/cjs/models/gemini_llm_connection.js +1 -0
  21. package/dist/cjs/models/google_llm.js +3 -3
  22. package/dist/cjs/plugins/base_plugin.js +12 -0
  23. package/dist/cjs/plugins/logging_plugin.js +50 -13
  24. package/dist/cjs/plugins/plugin_manager.js +56 -24
  25. package/dist/cjs/plugins/security_plugin.js +1 -1
  26. package/dist/cjs/runner/runner.js +110 -95
  27. package/dist/cjs/sessions/in_memory_session_service.js +38 -14
  28. package/dist/cjs/telemetry/google_cloud.js +7 -9
  29. package/dist/cjs/telemetry/setup.js +15 -7
  30. package/dist/cjs/telemetry/tracing.js +37 -15
  31. package/dist/cjs/tools/agent_tool.js +8 -4
  32. package/dist/cjs/tools/base_tool.js +4 -2
  33. package/dist/cjs/tools/forwarding_artifact_service.js +1 -1
  34. package/dist/cjs/tools/function_tool.js +1 -2
  35. package/dist/cjs/tools/google_search_tool.js +1 -2
  36. package/dist/cjs/tools/mcp/mcp_session_manager.js +16 -10
  37. package/dist/cjs/tools/mcp/mcp_tool.js +1 -3
  38. package/dist/cjs/tools/mcp/mcp_toolset.js +1 -1
  39. package/dist/cjs/utils/env_aware_utils.js +1 -1
  40. package/dist/cjs/utils/gemini_schema_util.js +9 -4
  41. package/dist/cjs/utils/logger.js +47 -3
  42. package/dist/cjs/utils/simple_zod_to_json.js +100 -141
  43. package/dist/cjs/utils/variant_utils.js +1 -1
  44. package/dist/cjs/version.js +1 -1
  45. package/dist/esm/agents/base_agent.js +48 -22
  46. package/dist/esm/agents/callback_context.js +4 -1
  47. package/dist/esm/agents/content_processor_utils.js +25 -9
  48. package/dist/esm/agents/functions.js +83 -29
  49. package/dist/esm/agents/llm_agent.js +63 -33
  50. package/dist/esm/agents/loop_agent.js +2 -1
  51. package/dist/esm/agents/parallel_agent.js +3 -4
  52. package/dist/esm/artifacts/gcs_artifact_service.js +28 -20
  53. package/dist/esm/artifacts/in_memory_artifact_service.js +18 -4
  54. package/dist/esm/auth/auth_handler.js +3 -1
  55. package/dist/esm/code_executors/base_code_executor.js +3 -1
  56. package/dist/esm/code_executors/built_in_code_executor.js +7 -3
  57. package/dist/esm/code_executors/code_executor_context.js +5 -5
  58. package/dist/esm/common.js +3 -1
  59. package/dist/esm/events/event.js +1 -3
  60. package/dist/esm/index.js +19 -19
  61. package/dist/esm/index.js.map +4 -4
  62. package/dist/esm/memory/in_memory_memory_service.js +3 -1
  63. package/dist/esm/models/base_llm.js +8 -4
  64. package/dist/esm/models/gemini_llm_connection.js +1 -0
  65. package/dist/esm/models/google_llm.js +8 -4
  66. package/dist/esm/plugins/base_plugin.js +12 -0
  67. package/dist/esm/plugins/logging_plugin.js +55 -14
  68. package/dist/esm/plugins/plugin_manager.js +56 -24
  69. package/dist/esm/plugins/security_plugin.js +1 -1
  70. package/dist/esm/runner/runner.js +114 -96
  71. package/dist/esm/sessions/in_memory_session_service.js +41 -15
  72. package/dist/esm/telemetry/google_cloud.js +7 -9
  73. package/dist/esm/telemetry/setup.js +23 -9
  74. package/dist/esm/telemetry/tracing.js +37 -15
  75. package/dist/esm/tools/agent_tool.js +8 -4
  76. package/dist/esm/tools/base_tool.js +4 -2
  77. package/dist/esm/tools/forwarding_artifact_service.js +1 -1
  78. package/dist/esm/tools/function_tool.js +1 -2
  79. package/dist/esm/tools/google_search_tool.js +2 -5
  80. package/dist/esm/tools/long_running_tool.js +3 -1
  81. package/dist/esm/tools/mcp/mcp_session_manager.js +22 -12
  82. package/dist/esm/tools/mcp/mcp_tool.js +1 -3
  83. package/dist/esm/tools/mcp/mcp_toolset.js +1 -1
  84. package/dist/esm/utils/env_aware_utils.js +1 -1
  85. package/dist/esm/utils/gemini_schema_util.js +9 -4
  86. package/dist/esm/utils/logger.js +43 -2
  87. package/dist/esm/utils/simple_zod_to_json.js +102 -141
  88. package/dist/esm/utils/variant_utils.js +1 -1
  89. package/dist/esm/version.js +1 -1
  90. package/dist/types/agents/base_agent.d.ts +2 -1
  91. package/dist/types/agents/callback_context.d.ts +1 -1
  92. package/dist/types/agents/llm_agent.d.ts +1 -1
  93. package/dist/types/agents/loop_agent.d.ts +1 -1
  94. package/dist/types/agents/parallel_agent.d.ts +1 -1
  95. package/dist/types/artifacts/in_memory_artifact_service.d.ts +3 -3
  96. package/dist/types/code_executors/built_in_code_executor.d.ts +1 -1
  97. package/dist/types/code_executors/code_executor_context.d.ts +2 -4
  98. package/dist/types/common.d.ts +2 -1
  99. package/dist/types/index.d.ts +3 -3
  100. package/dist/types/models/base_llm_connection.d.ts +1 -1
  101. package/dist/types/models/llm_response.d.ts +1 -1
  102. package/dist/types/plugins/logging_plugin.d.ts +12 -12
  103. package/dist/types/plugins/plugin_manager.d.ts +12 -12
  104. package/dist/types/plugins/security_plugin.d.ts +1 -1
  105. package/dist/types/runner/runner.d.ts +1 -1
  106. package/dist/types/sessions/in_memory_session_service.d.ts +5 -5
  107. package/dist/types/telemetry/setup.d.ts +1 -1
  108. package/dist/types/telemetry/tracing.d.ts +7 -6
  109. package/dist/types/tools/agent_tool.d.ts +1 -1
  110. package/dist/types/tools/base_tool.d.ts +1 -1
  111. package/dist/types/tools/base_toolset.d.ts +2 -1
  112. package/dist/types/tools/forwarding_artifact_service.d.ts +2 -2
  113. package/dist/types/tools/function_tool.d.ts +4 -3
  114. package/dist/types/tools/google_search_tool.d.ts +3 -3
  115. package/dist/types/tools/mcp/mcp_session_manager.d.ts +10 -3
  116. package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -2
  117. package/dist/types/utils/gemini_schema_util.d.ts +4 -12
  118. package/dist/types/utils/logger.d.ts +11 -10
  119. package/dist/types/utils/simple_zod_to_json.d.ts +5 -4
  120. package/dist/types/version.d.ts +1 -1
  121. package/dist/web/agents/base_agent.js +94 -33
  122. package/dist/web/agents/callback_context.js +4 -1
  123. package/dist/web/agents/content_processor_utils.js +25 -9
  124. package/dist/web/agents/functions.js +83 -29
  125. package/dist/web/agents/llm_agent.js +117 -54
  126. package/dist/web/agents/loop_agent.js +2 -1
  127. package/dist/web/agents/parallel_agent.js +3 -4
  128. package/dist/web/artifacts/gcs_artifact_service.js +25 -17
  129. package/dist/web/artifacts/in_memory_artifact_service.js +18 -4
  130. package/dist/web/auth/auth_handler.js +3 -1
  131. package/dist/web/code_executors/base_code_executor.js +3 -1
  132. package/dist/web/code_executors/built_in_code_executor.js +7 -3
  133. package/dist/web/code_executors/code_executor_context.js +5 -5
  134. package/dist/web/common.js +3 -1
  135. package/dist/web/events/event.js +1 -3
  136. package/dist/web/index.js +1 -1
  137. package/dist/web/index.js.map +4 -4
  138. package/dist/web/memory/in_memory_memory_service.js +3 -1
  139. package/dist/web/models/base_llm.js +8 -4
  140. package/dist/web/models/gemini_llm_connection.js +1 -0
  141. package/dist/web/models/google_llm.js +8 -4
  142. package/dist/web/plugins/base_plugin.js +12 -0
  143. package/dist/web/plugins/logging_plugin.js +55 -14
  144. package/dist/web/plugins/plugin_manager.js +56 -24
  145. package/dist/web/plugins/security_plugin.js +1 -1
  146. package/dist/web/runner/runner.js +159 -108
  147. package/dist/web/sessions/in_memory_session_service.js +41 -15
  148. package/dist/web/telemetry/google_cloud.js +7 -9
  149. package/dist/web/telemetry/setup.js +23 -9
  150. package/dist/web/telemetry/tracing.js +37 -15
  151. package/dist/web/tools/agent_tool.js +8 -4
  152. package/dist/web/tools/base_tool.js +4 -2
  153. package/dist/web/tools/forwarding_artifact_service.js +1 -1
  154. package/dist/web/tools/function_tool.js +1 -2
  155. package/dist/web/tools/google_search_tool.js +2 -5
  156. package/dist/web/tools/long_running_tool.js +3 -1
  157. package/dist/web/tools/mcp/mcp_session_manager.js +22 -12
  158. package/dist/web/tools/mcp/mcp_tool.js +1 -3
  159. package/dist/web/tools/mcp/mcp_toolset.js +1 -1
  160. package/dist/web/utils/env_aware_utils.js +1 -1
  161. package/dist/web/utils/gemini_schema_util.js +9 -4
  162. package/dist/web/utils/logger.js +43 -2
  163. package/dist/web/utils/simple_zod_to_json.js +102 -155
  164. package/dist/web/utils/variant_utils.js +1 -1
  165. package/dist/web/version.js +1 -1
  166. package/package.json +4 -2
@@ -1,4 +1,7 @@
1
1
  var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
2
+ var __typeError = (msg) => {
3
+ throw TypeError(msg);
4
+ };
2
5
  var __await = function(promise, isYieldStar) {
3
6
  this[0] = promise;
4
7
  this[1] = isYieldStar;
@@ -14,6 +17,34 @@ var __asyncGenerator = (__this, __arguments, generator) => {
14
17
  }, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
15
18
  return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
16
19
  };
20
+ var __yieldStar = (value) => {
21
+ var obj = value[__knownSymbol("asyncIterator")], isAwait = false, method, it = {};
22
+ if (obj == null) {
23
+ obj = value[__knownSymbol("iterator")]();
24
+ method = (k) => it[k] = (x) => obj[k](x);
25
+ } else {
26
+ obj = obj.call(value);
27
+ method = (k) => it[k] = (v) => {
28
+ if (isAwait) {
29
+ isAwait = false;
30
+ if (k === "throw") throw v;
31
+ return v;
32
+ }
33
+ isAwait = true;
34
+ return {
35
+ done: false,
36
+ value: new __await(new Promise((resolve) => {
37
+ var x = obj[k](v);
38
+ if (!(x instanceof Object)) __typeError("Object expected");
39
+ resolve(x);
40
+ }), 1)
41
+ };
42
+ };
43
+ }
44
+ return it[__knownSymbol("iterator")] = () => it, method("next"), "throw" in obj ? method("throw") : it.throw = (x) => {
45
+ throw x;
46
+ }, "return" in obj && method("return"), it;
47
+ };
17
48
  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);
18
49
  /**
19
50
  * @license
@@ -21,7 +52,7 @@ var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")])
21
52
  * SPDX-License-Identifier: Apache-2.0
22
53
  */
23
54
  import { createPartFromText } from "@google/genai";
24
- import { trace } from "@opentelemetry/api";
55
+ import { context, trace } from "@opentelemetry/api";
25
56
  import {
26
57
  InvocationContext,
27
58
  newInvocationContextId
@@ -35,6 +66,10 @@ import {
35
66
  import { createEvent, getFunctionCalls } from "../events/event.js";
36
67
  import { createEventActions } from "../events/event_actions.js";
37
68
  import { PluginManager } from "../plugins/plugin_manager.js";
69
+ import {
70
+ runAsyncGeneratorWithOtelContext,
71
+ tracer
72
+ } from "../telemetry/tracing.js";
38
73
  import { logger } from "../utils/logger.js";
39
74
  import { isGemini2OrAbove } from "../utils/model_name.js";
40
75
  class Runner {
@@ -62,121 +97,137 @@ class Runner {
62
97
  // TODO - b/425992518: user, sessionId, and runConfig can be internalized.
63
98
  runAsync(params) {
64
99
  return __asyncGenerator(this, null, function* () {
65
- var _a;
66
100
  const { userId, sessionId, stateDelta } = params;
67
101
  const runConfig = createRunConfig(params.runConfig);
68
102
  let newMessage = params.newMessage;
69
- const span = trace.getTracer("gcp.vertex.agent").startSpan("invocation");
103
+ const span = tracer.startSpan("invocation");
104
+ const ctx = trace.setSpan(context.active(), span);
70
105
  try {
71
- const session = yield new __await(this.sessionService.getSession({
72
- appName: this.appName,
73
- userId,
74
- sessionId
75
- }));
76
- if (!session) {
77
- if (!this.appName) {
78
- throw new Error(
79
- "Session lookup failed: appName must be provided in runner constructor"
80
- );
81
- }
82
- throw new Error("Session not found: ".concat(sessionId));
83
- }
84
- if (runConfig.supportCfc && isLlmAgent(this.agent)) {
85
- const modelName = this.agent.canonicalModel.model;
86
- if (!isGemini2OrAbove(modelName)) {
87
- throw new Error(
88
- "CFC is not supported for model: ".concat(modelName, " in agent: ").concat(this.agent.name)
89
- );
90
- }
91
- if (!isBuiltInCodeExecutor(this.agent.codeExecutor)) {
92
- this.agent.codeExecutor = new BuiltInCodeExecutor();
93
- }
94
- }
95
- const invocationContext = new InvocationContext({
96
- artifactService: this.artifactService,
97
- sessionService: this.sessionService,
98
- memoryService: this.memoryService,
99
- credentialService: this.credentialService,
100
- invocationId: newInvocationContextId(),
101
- agent: this.agent,
102
- session,
103
- userContent: newMessage,
104
- runConfig,
105
- pluginManager: this.pluginManager
106
- });
107
- const pluginUserMessage = yield new __await(this.pluginManager.runOnUserMessageCallback({
108
- userMessage: newMessage,
109
- invocationContext
110
- }));
111
- if (pluginUserMessage) {
112
- newMessage = pluginUserMessage;
113
- }
114
- if (newMessage) {
115
- if (!((_a = newMessage.parts) == null ? void 0 : _a.length)) {
116
- throw new Error("No parts in the newMessage.");
117
- }
118
- if (runConfig.saveInputBlobsAsArtifacts) {
119
- yield new __await(this.saveArtifacts(
120
- invocationContext.invocationId,
121
- session.userId,
122
- session.id,
123
- newMessage
124
- ));
125
- }
126
- yield new __await(this.sessionService.appendEvent({
127
- session,
128
- event: createEvent({
129
- invocationId: invocationContext.invocationId,
130
- author: "user",
131
- actions: stateDelta ? createEventActions({ stateDelta }) : void 0,
132
- content: newMessage
133
- })
134
- }));
135
- }
136
- invocationContext.agent = this.determineAgentForResumption(
137
- session,
138
- this.agent
139
- );
140
- const beforeRunCallbackResponse = yield new __await(this.pluginManager.runBeforeRunCallback({ invocationContext }));
141
- if (beforeRunCallbackResponse) {
142
- const earlyExitEvent = createEvent({
143
- invocationId: invocationContext.invocationId,
144
- author: "model",
145
- content: beforeRunCallbackResponse
146
- });
147
- yield new __await(this.sessionService.appendEvent({ session, event: earlyExitEvent }));
148
- yield earlyExitEvent;
149
- } else {
150
- try {
151
- for (var iter = __forAwait(invocationContext.agent.runAsync(
152
- invocationContext
153
- )), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
154
- const event = temp.value;
155
- if (!event.partial) {
156
- yield new __await(this.sessionService.appendEvent({ session, event }));
106
+ yield* __yieldStar(runAsyncGeneratorWithOtelContext(
107
+ ctx,
108
+ this,
109
+ function() {
110
+ return __asyncGenerator(this, null, function* () {
111
+ var _a;
112
+ const session = yield new __await(this.sessionService.getSession({
113
+ appName: this.appName,
114
+ userId,
115
+ sessionId
116
+ }));
117
+ if (!session) {
118
+ if (!this.appName) {
119
+ throw new Error(
120
+ "Session lookup failed: appName must be provided in runner constructor"
121
+ );
122
+ }
123
+ throw new Error("Session not found: ".concat(sessionId));
157
124
  }
158
- const modifiedEvent = yield new __await(this.pluginManager.runOnEventCallback({
159
- invocationContext,
160
- event
125
+ if (runConfig.supportCfc && isLlmAgent(this.agent)) {
126
+ const modelName = this.agent.canonicalModel.model;
127
+ if (!isGemini2OrAbove(modelName)) {
128
+ throw new Error(
129
+ "CFC is not supported for model: ".concat(modelName, " in agent: ").concat(this.agent.name)
130
+ );
131
+ }
132
+ if (!isBuiltInCodeExecutor(this.agent.codeExecutor)) {
133
+ this.agent.codeExecutor = new BuiltInCodeExecutor();
134
+ }
135
+ }
136
+ const invocationContext = new InvocationContext({
137
+ artifactService: this.artifactService,
138
+ sessionService: this.sessionService,
139
+ memoryService: this.memoryService,
140
+ credentialService: this.credentialService,
141
+ invocationId: newInvocationContextId(),
142
+ agent: this.agent,
143
+ session,
144
+ userContent: newMessage,
145
+ runConfig,
146
+ pluginManager: this.pluginManager
147
+ });
148
+ const pluginUserMessage = yield new __await(this.pluginManager.runOnUserMessageCallback({
149
+ userMessage: newMessage,
150
+ invocationContext
161
151
  }));
162
- if (modifiedEvent) {
163
- yield modifiedEvent;
164
- } else {
165
- yield event;
152
+ if (pluginUserMessage) {
153
+ newMessage = pluginUserMessage;
154
+ }
155
+ if (newMessage) {
156
+ if (!((_a = newMessage.parts) == null ? void 0 : _a.length)) {
157
+ throw new Error("No parts in the newMessage.");
158
+ }
159
+ if (runConfig.saveInputBlobsAsArtifacts) {
160
+ yield new __await(this.saveArtifacts(
161
+ invocationContext.invocationId,
162
+ session.userId,
163
+ session.id,
164
+ newMessage
165
+ ));
166
+ }
167
+ yield new __await(this.sessionService.appendEvent({
168
+ session,
169
+ event: createEvent({
170
+ invocationId: invocationContext.invocationId,
171
+ author: "user",
172
+ actions: stateDelta ? createEventActions({ stateDelta }) : void 0,
173
+ content: newMessage
174
+ })
175
+ }));
176
+ }
177
+ invocationContext.agent = this.determineAgentForResumption(
178
+ session,
179
+ this.agent
180
+ );
181
+ if (newMessage) {
182
+ const beforeRunCallbackResponse = yield new __await(this.pluginManager.runBeforeRunCallback({
183
+ invocationContext
184
+ }));
185
+ if (beforeRunCallbackResponse) {
186
+ const earlyExitEvent = createEvent({
187
+ invocationId: invocationContext.invocationId,
188
+ author: "model",
189
+ content: beforeRunCallbackResponse
190
+ });
191
+ yield new __await(this.sessionService.appendEvent({
192
+ session,
193
+ event: earlyExitEvent
194
+ }));
195
+ yield earlyExitEvent;
196
+ } else {
197
+ try {
198
+ for (var iter = __forAwait(invocationContext.agent.runAsync(
199
+ invocationContext
200
+ )), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
201
+ const event = temp.value;
202
+ if (!event.partial) {
203
+ yield new __await(this.sessionService.appendEvent({ session, event }));
204
+ }
205
+ const modifiedEvent = yield new __await(this.pluginManager.runOnEventCallback({
206
+ invocationContext,
207
+ event
208
+ }));
209
+ if (modifiedEvent) {
210
+ yield modifiedEvent;
211
+ } else {
212
+ yield event;
213
+ }
214
+ }
215
+ } catch (temp) {
216
+ error = [temp];
217
+ } finally {
218
+ try {
219
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
220
+ } finally {
221
+ if (error)
222
+ throw error[0];
223
+ }
224
+ }
225
+ yield new __await(this.pluginManager.runAfterRunCallback({ invocationContext }));
226
+ }
166
227
  }
167
- }
168
- } catch (temp) {
169
- error = [temp];
170
- } finally {
171
- try {
172
- more && (temp = iter.return) && (yield new __await(temp.call(iter)));
173
- } finally {
174
- if (error)
175
- throw error[0];
176
- }
228
+ });
177
229
  }
178
- }
179
- yield new __await(this.pluginManager.runAfterRunCallback({ invocationContext }));
230
+ ));
180
231
  } finally {
181
232
  span.end();
182
233
  }
@@ -6,7 +6,9 @@
6
6
  import { cloneDeep } from "lodash-es";
7
7
  import { randomUUID } from "../utils/env_aware_utils.js";
8
8
  import { logger } from "../utils/logger.js";
9
- import { BaseSessionService } from "./base_session_service.js";
9
+ import {
10
+ BaseSessionService
11
+ } from "./base_session_service.js";
10
12
  import { createSession } from "./session.js";
11
13
  import { State } from "./state.js";
12
14
  class InMemorySessionService extends BaseSessionService {
@@ -26,7 +28,12 @@ class InMemorySessionService extends BaseSessionService {
26
28
  */
27
29
  this.appState = {};
28
30
  }
29
- createSession({ appName, userId, state, sessionId }) {
31
+ createSession({
32
+ appName,
33
+ userId,
34
+ state,
35
+ sessionId
36
+ }) {
30
37
  const session = createSession({
31
38
  id: sessionId || randomUUID(),
32
39
  appName,
@@ -46,7 +53,12 @@ class InMemorySessionService extends BaseSessionService {
46
53
  this.mergeState(appName, userId, cloneDeep(session))
47
54
  );
48
55
  }
49
- getSession({ appName, userId, sessionId, config }) {
56
+ getSession({
57
+ appName,
58
+ userId,
59
+ sessionId,
60
+ config
61
+ }) {
50
62
  if (!this.sessions[appName] || !this.sessions[appName][userId] || !this.sessions[appName][userId][sessionId]) {
51
63
  return Promise.resolve(void 0);
52
64
  }
@@ -54,7 +66,9 @@ class InMemorySessionService extends BaseSessionService {
54
66
  const copiedSession = cloneDeep(session);
55
67
  if (config) {
56
68
  if (config.numRecentEvents) {
57
- copiedSession.events = copiedSession.events.slice(-config.numRecentEvents);
69
+ copiedSession.events = copiedSession.events.slice(
70
+ -config.numRecentEvents
71
+ );
58
72
  }
59
73
  if (config.afterTimestamp) {
60
74
  let i = copiedSession.events.length - 1;
@@ -71,31 +85,43 @@ class InMemorySessionService extends BaseSessionService {
71
85
  }
72
86
  return Promise.resolve(this.mergeState(appName, userId, copiedSession));
73
87
  }
74
- listSessions({ appName, userId }) {
88
+ listSessions({
89
+ appName,
90
+ userId
91
+ }) {
75
92
  if (!this.sessions[appName] || !this.sessions[appName][userId]) {
76
93
  return Promise.resolve({ sessions: [] });
77
94
  }
78
95
  const sessionsWithoutEvents = [];
79
96
  for (const session of Object.values(this.sessions[appName][userId])) {
80
- sessionsWithoutEvents.push(createSession({
81
- id: session.id,
82
- appName: session.appName,
83
- userId: session.userId,
84
- state: {},
85
- events: [],
86
- lastUpdateTime: session.lastUpdateTime
87
- }));
97
+ sessionsWithoutEvents.push(
98
+ createSession({
99
+ id: session.id,
100
+ appName: session.appName,
101
+ userId: session.userId,
102
+ state: {},
103
+ events: [],
104
+ lastUpdateTime: session.lastUpdateTime
105
+ })
106
+ );
88
107
  }
89
108
  return Promise.resolve({ sessions: sessionsWithoutEvents });
90
109
  }
91
- async deleteSession({ appName, userId, sessionId }) {
110
+ async deleteSession({
111
+ appName,
112
+ userId,
113
+ sessionId
114
+ }) {
92
115
  const session = await this.getSession({ appName, userId, sessionId });
93
116
  if (!session) {
94
117
  return;
95
118
  }
96
119
  delete this.sessions[appName][userId][sessionId];
97
120
  }
98
- async appendEvent({ session, event }) {
121
+ async appendEvent({
122
+ session,
123
+ event
124
+ }) {
99
125
  await super.appendEvent({ session, event });
100
126
  session.lastUpdateTime = event.timestamp;
101
127
  const appName = session.appName;
@@ -3,13 +3,13 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- import { GoogleAuth } from "google-auth-library";
7
- import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
8
- import { detectResources } from "@opentelemetry/resources";
9
- import { gcpDetector } from "@opentelemetry/resource-detector-gcp";
6
+ import { MetricExporter } from "@google-cloud/opentelemetry-cloud-monitoring-exporter";
10
7
  import { TraceExporter } from "@google-cloud/opentelemetry-cloud-trace-exporter";
8
+ import { gcpDetector } from "@opentelemetry/resource-detector-gcp";
9
+ import { detectResources } from "@opentelemetry/resources";
10
+ import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
11
11
  import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
12
- import { MetricExporter } from "@google-cloud/opentelemetry-cloud-monitoring-exporter";
12
+ import { GoogleAuth } from "google-auth-library";
13
13
  import { logger } from "../utils/logger.js";
14
14
  const GCP_PROJECT_ERROR_MESSAGE = "Cannot determine GCP Project. OTel GCP Exporters cannot be set up. Please make sure to log into correct GCP Project.";
15
15
  async function getGcpProjectId() {
@@ -17,7 +17,7 @@ async function getGcpProjectId() {
17
17
  const auth = new GoogleAuth();
18
18
  const projectId = await auth.getProjectId();
19
19
  return projectId || void 0;
20
- } catch (error) {
20
+ } catch (_e) {
21
21
  return void 0;
22
22
  }
23
23
  }
@@ -33,9 +33,7 @@ async function getGcpExporters(config = {}) {
33
33
  return {};
34
34
  }
35
35
  return {
36
- spanProcessors: enableTracing ? [
37
- new BatchSpanProcessor(new TraceExporter({ projectId }))
38
- ] : [],
36
+ spanProcessors: enableTracing ? [new BatchSpanProcessor(new TraceExporter({ projectId }))] : [],
39
37
  metricReaders: enableMetrics ? [
40
38
  new PeriodicExportingMetricReader({
41
39
  exporter: new MetricExporter({ projectId }),
@@ -3,22 +3,32 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- import { trace, metrics } from "@opentelemetry/api";
6
+ import { metrics, trace } from "@opentelemetry/api";
7
7
  import { logs } from "@opentelemetry/api-logs";
8
- import { LoggerProvider, BatchLogRecordProcessor } from "@opentelemetry/sdk-logs";
9
- import { MeterProvider, PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
8
+ import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
9
+ import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
10
+ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
10
11
  import { detectResources } from "@opentelemetry/resources";
12
+ import {
13
+ BatchLogRecordProcessor,
14
+ LoggerProvider
15
+ } from "@opentelemetry/sdk-logs";
16
+ import {
17
+ MeterProvider,
18
+ PeriodicExportingMetricReader
19
+ } from "@opentelemetry/sdk-metrics";
11
20
  import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
12
21
  import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
13
- import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
14
- import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
15
- import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
16
22
  function maybeSetOtelProviders(otelHooksToSetup = [], otelResource) {
17
23
  const resource = otelResource || getOtelResource();
18
24
  const allHooks = [...otelHooksToSetup, getOtelExporters()];
19
- const spanProcessors = allHooks.flatMap((hooks) => hooks.spanProcessors || []);
25
+ const spanProcessors = allHooks.flatMap(
26
+ (hooks) => hooks.spanProcessors || []
27
+ );
20
28
  const metricReaders = allHooks.flatMap((hooks) => hooks.metricReaders || []);
21
- const logRecordProcessors = allHooks.flatMap((hooks) => hooks.logRecordProcessors || []);
29
+ const logRecordProcessors = allHooks.flatMap(
30
+ (hooks) => hooks.logRecordProcessors || []
31
+ );
22
32
  if (spanProcessors.length > 0) {
23
33
  const tracerProvider = new NodeTracerProvider({
24
34
  resource,
@@ -58,7 +68,11 @@ function getOtelExporters(config = getOtelExportersConfig()) {
58
68
  const { enableTracing, enableMetrics, enableLogging } = config;
59
69
  return {
60
70
  spanProcessors: enableTracing ? [new BatchSpanProcessor(new OTLPTraceExporter())] : [],
61
- metricReaders: enableMetrics ? [new PeriodicExportingMetricReader({ exporter: new OTLPMetricExporter() })] : [],
71
+ metricReaders: enableMetrics ? [
72
+ new PeriodicExportingMetricReader({
73
+ exporter: new OTLPMetricExporter()
74
+ })
75
+ ] : [],
62
76
  logRecordProcessors: enableLogging ? [new BatchLogRecordProcessor(new OTLPLogExporter())] : []
63
77
  };
64
78
  }
@@ -18,7 +18,7 @@ var __objRest = (source, exclude) => {
18
18
  * Copyright 2025 Google LLC
19
19
  * SPDX-License-Identifier: Apache-2.0
20
20
  */
21
- import { trace, context } from "@opentelemetry/api";
21
+ import { context, trace } from "@opentelemetry/api";
22
22
  import { version } from "../version.js";
23
23
  const GEN_AI_AGENT_DESCRIPTION = "gen_ai.agent.description";
24
24
  const GEN_AI_AGENT_NAME = "gen_ai.agent.name";
@@ -28,14 +28,11 @@ const GEN_AI_TOOL_CALL_ID = "gen_ai.tool.call.id";
28
28
  const GEN_AI_TOOL_DESCRIPTION = "gen_ai.tool.description";
29
29
  const GEN_AI_TOOL_NAME = "gen_ai.tool.name";
30
30
  const GEN_AI_TOOL_TYPE = "gen_ai.tool.type";
31
- const tracer = trace.getTracer(
32
- "gcp.vertex.agent",
33
- version
34
- );
31
+ const tracer = trace.getTracer("gcp.vertex.agent", version);
35
32
  function safeJsonSerialize(obj) {
36
33
  try {
37
34
  return JSON.stringify(obj);
38
- } catch (error) {
35
+ } catch (_e) {
39
36
  return "<not serializable>";
40
37
  }
41
38
  }
@@ -112,7 +109,10 @@ function traceMergedToolCalls({
112
109
  "gcp.vertex.agent.llm_request": "{}",
113
110
  "gcp.vertex.agent.llm_response": "{}"
114
111
  });
115
- span.setAttribute("gcp.vertex.agent.tool_response", shouldAddRequestResponseToSpans() ? safeJsonSerialize(functionResponseEvent) : "{}");
112
+ span.setAttribute(
113
+ "gcp.vertex.agent.tool_response",
114
+ shouldAddRequestResponseToSpans() ? safeJsonSerialize(functionResponseEvent) : "{}"
115
+ );
116
116
  }
117
117
  function traceCallLlm({
118
118
  invocationContext,
@@ -136,14 +136,26 @@ function traceCallLlm({
136
136
  span.setAttribute("gen_ai.request.top_p", llmRequest.config.topP);
137
137
  }
138
138
  if (((_b = llmRequest.config) == null ? void 0 : _b.maxOutputTokens) !== void 0) {
139
- span.setAttribute("gen_ai.request.max_tokens", llmRequest.config.maxOutputTokens);
139
+ span.setAttribute(
140
+ "gen_ai.request.max_tokens",
141
+ llmRequest.config.maxOutputTokens
142
+ );
140
143
  }
141
- span.setAttribute("gcp.vertex.agent.llm_response", shouldAddRequestResponseToSpans() ? safeJsonSerialize(llmResponse) : "{}");
144
+ span.setAttribute(
145
+ "gcp.vertex.agent.llm_response",
146
+ shouldAddRequestResponseToSpans() ? safeJsonSerialize(llmResponse) : "{}"
147
+ );
142
148
  if (llmResponse.usageMetadata) {
143
- span.setAttribute("gen_ai.usage.input_tokens", llmResponse.usageMetadata.promptTokenCount || 0);
149
+ span.setAttribute(
150
+ "gen_ai.usage.input_tokens",
151
+ llmResponse.usageMetadata.promptTokenCount || 0
152
+ );
144
153
  }
145
154
  if ((_c = llmResponse.usageMetadata) == null ? void 0 : _c.candidatesTokenCount) {
146
- span.setAttribute("gen_ai.usage.output_tokens", llmResponse.usageMetadata.candidatesTokenCount);
155
+ span.setAttribute(
156
+ "gen_ai.usage.output_tokens",
157
+ llmResponse.usageMetadata.candidatesTokenCount
158
+ );
147
159
  }
148
160
  if (llmResponse.finishReason) {
149
161
  const finishReasonValue = typeof llmResponse.finishReason === "string" ? llmResponse.finishReason.toLowerCase() : String(llmResponse.finishReason).toLowerCase();
@@ -161,7 +173,10 @@ function traceSendData({
161
173
  "gcp.vertex.agent.invocation_id": invocationContext.invocationId,
162
174
  "gcp.vertex.agent.event_id": eventId
163
175
  });
164
- span.setAttribute("gcp.vertex.agent.data", shouldAddRequestResponseToSpans() ? safeJsonSerialize(data) : "{}");
176
+ span.setAttribute(
177
+ "gcp.vertex.agent.data",
178
+ shouldAddRequestResponseToSpans() ? safeJsonSerialize(data) : "{}"
179
+ );
165
180
  }
166
181
  function buildLlmRequestForTrace(llmRequest) {
167
182
  const result = {
@@ -181,7 +196,7 @@ function buildLlmRequestForTrace(llmRequest) {
181
196
  });
182
197
  return result;
183
198
  }
184
- function bindAsyncGenerator(ctx, generator) {
199
+ function bindOtelContextToAsyncGenerator(ctx, generator) {
185
200
  return {
186
201
  // Bind the next() method to execute within the provided context
187
202
  next: context.bind(ctx, generator.next.bind(generator)),
@@ -191,16 +206,23 @@ function bindAsyncGenerator(ctx, generator) {
191
206
  throw: context.bind(ctx, generator.throw.bind(generator)),
192
207
  // Ensure the async iterator symbol also returns a context-bound generator
193
208
  [Symbol.asyncIterator]() {
194
- return bindAsyncGenerator(ctx, generator[Symbol.asyncIterator]());
209
+ return bindOtelContextToAsyncGenerator(
210
+ ctx,
211
+ generator[Symbol.asyncIterator]()
212
+ );
195
213
  }
196
214
  };
197
215
  }
216
+ function runAsyncGeneratorWithOtelContext(otelContext, generatorFnContext, generatorFn) {
217
+ const generator = generatorFn.call(generatorFnContext);
218
+ return bindOtelContextToAsyncGenerator(otelContext, generator);
219
+ }
198
220
  function shouldAddRequestResponseToSpans() {
199
221
  const envValue = process.env.ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS || "true";
200
222
  return envValue === "true" || envValue === "1";
201
223
  }
202
224
  export {
203
- bindAsyncGenerator,
225
+ runAsyncGeneratorWithOtelContext,
204
226
  traceAgentInvocation,
205
227
  traceCallLlm,
206
228
  traceMergedToolCalls,
@@ -20,9 +20,10 @@ function isAgentTool(obj) {
20
20
  }
21
21
  class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
22
22
  constructor(config) {
23
- super(
24
- { name: config.agent.name, description: config.agent.description || "" }
25
- );
23
+ super({
24
+ name: config.agent.name,
25
+ description: config.agent.description || ""
26
+ });
26
27
  /** A unique symbol to identify ADK agent tool class. */
27
28
  this[_a] = true;
28
29
  this.agent = config.agent;
@@ -60,7 +61,10 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
60
61
  }
61
62
  return declaration;
62
63
  }
63
- async runAsync({ args, toolContext }) {
64
+ async runAsync({
65
+ args,
66
+ toolContext
67
+ }) {
64
68
  var _a2, _b2;
65
69
  if (this.skipSummarization) {
66
70
  toolContext.actions.skipSummarization = true;
@@ -49,7 +49,7 @@ class BaseTool {
49
49
  *
50
50
  * @param request The request to process the LLM request.
51
51
  */
52
- async processLlmRequest({ toolContext, llmRequest }) {
52
+ async processLlmRequest({ llmRequest }) {
53
53
  const functionDeclaration = this._getDeclaration();
54
54
  if (!functionDeclaration) {
55
55
  return;
@@ -78,7 +78,9 @@ class BaseTool {
78
78
  }
79
79
  function findToolWithFunctionDeclarations(llmRequest) {
80
80
  var _a2;
81
- return (((_a2 = llmRequest.config) == null ? void 0 : _a2.tools) || []).find((tool) => "functionDeclarations" in tool);
81
+ return (((_a2 = llmRequest.config) == null ? void 0 : _a2.tools) || []).find(
82
+ (tool) => "functionDeclarations" in tool
83
+ );
82
84
  }
83
85
  export {
84
86
  BaseTool,
@@ -16,7 +16,7 @@ class ForwardingArtifactService {
16
16
  async loadArtifact(request) {
17
17
  return this.toolContext.loadArtifact(request.filename, request.version);
18
18
  }
19
- async listArtifactKeys(request) {
19
+ async listArtifactKeys() {
20
20
  return this.toolContext.listArtifacts();
21
21
  }
22
22
  async deleteArtifact(request) {