@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
@@ -41,8 +41,7 @@ class MCPTool extends import_base_tool.BaseTool {
41
41
  this.mcpSessionManager = mcpSessionManager;
42
42
  }
43
43
  _getDeclaration() {
44
- let declaration;
45
- declaration = {
44
+ return {
46
45
  name: this.mcpTool.name,
47
46
  description: this.mcpTool.description,
48
47
  parameters: (0, import_gemini_schema_util.toGeminiSchema)(this.mcpTool.inputSchema),
@@ -50,7 +49,6 @@ class MCPTool extends import_base_tool.BaseTool {
50
49
  // https://modelcontextprotocol.io/specification/2025-06-18/server/tools#tool-result
51
50
  response: (0, import_gemini_schema_util.toGeminiSchema)(this.mcpTool.outputSchema)
52
51
  };
53
- return declaration;
54
52
  }
55
53
  async runAsync(request) {
56
54
  const session = await this.mcpSessionManager.createSession();
@@ -41,7 +41,7 @@ class MCPToolset extends import_base_toolset.BaseToolset {
41
41
  super(toolFilter);
42
42
  this.mcpSessionManager = new import_mcp_session_manager.MCPSessionManager(connectionParams);
43
43
  }
44
- async getTools(context) {
44
+ async getTools() {
45
45
  const session = await this.mcpSessionManager.createSession();
46
46
  const listResult = await session.listTools();
47
47
  import_logger.logger.debug(`number of tools: ${listResult.tools.length}`);
@@ -69,7 +69,7 @@ function base64Decode(data) {
69
69
  function isBase64Encoded(data) {
70
70
  try {
71
71
  return base64Encode(base64Decode(data)) === data;
72
- } catch (e) {
72
+ } catch (_e) {
73
73
  return false;
74
74
  }
75
75
  }
@@ -34,9 +34,9 @@ var import_zod = require("zod");
34
34
  * Copyright 2025 Google LLC
35
35
  * SPDX-License-Identifier: Apache-2.0
36
36
  */
37
- const MCPToolSchema = import_zod.z.object({
37
+ const MCPToolSchemaObject = import_zod.z.object({
38
38
  type: import_zod.z.literal("object"),
39
- properties: import_zod.z.record(import_zod.z.unknown()).optional(),
39
+ properties: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional(),
40
40
  required: import_zod.z.string().array().optional()
41
41
  });
42
42
  function toGeminiType(mcpType) {
@@ -81,12 +81,17 @@ function toGeminiSchema(mcpSchema) {
81
81
  }
82
82
  }
83
83
  const geminiType = toGeminiType(mcp.type);
84
- const geminiSchema = { type: geminiType, description: mcp.description };
84
+ const geminiSchema = {
85
+ type: geminiType,
86
+ description: mcp.description
87
+ };
85
88
  if (geminiType === import_genai.Type.OBJECT) {
86
89
  geminiSchema.properties = {};
87
90
  if (mcp.properties) {
88
91
  for (const name in mcp.properties) {
89
- geminiSchema.properties[name] = recursiveConvert(mcp.properties[name]);
92
+ geminiSchema.properties[name] = recursiveConvert(
93
+ mcp.properties[name]
94
+ );
90
95
  }
91
96
  }
92
97
  geminiSchema.required = mcp.required;
@@ -25,8 +25,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
25
25
  var logger_exports = {};
26
26
  __export(logger_exports, {
27
27
  LogLevel: () => LogLevel,
28
+ getLogger: () => getLogger,
28
29
  logger: () => logger,
29
- setLogLevel: () => setLogLevel
30
+ resetLogger: () => resetLogger,
31
+ setLogLevel: () => setLogLevel,
32
+ setLogger: () => setLogger
30
33
  });
31
34
  module.exports = __toCommonJS(logger_exports);
32
35
  /**
@@ -92,6 +95,18 @@ class SimpleLogger {
92
95
  console.error(getColoredPrefix(3 /* ERROR */), ...args);
93
96
  }
94
97
  }
98
+ class NoOpLogger {
99
+ log(_level, ..._args) {
100
+ }
101
+ debug(..._args) {
102
+ }
103
+ info(..._args) {
104
+ }
105
+ warn(..._args) {
106
+ }
107
+ error(..._args) {
108
+ }
109
+ }
95
110
  const LOG_LEVEL_STR = {
96
111
  [0 /* DEBUG */]: "DEBUG",
97
112
  [1 /* INFO */]: "INFO",
@@ -112,10 +127,39 @@ const RESET_COLOR = "\x1B[0m";
112
127
  function getColoredPrefix(level) {
113
128
  return `${CONSOLE_COLOR_MAP[level]}[ADK ${LOG_LEVEL_STR[level]}]:${RESET_COLOR}`;
114
129
  }
115
- const logger = new SimpleLogger();
130
+ let currentLogger = new SimpleLogger();
131
+ function setLogger(customLogger) {
132
+ currentLogger = customLogger != null ? customLogger : new NoOpLogger();
133
+ }
134
+ function getLogger() {
135
+ return currentLogger;
136
+ }
137
+ function resetLogger() {
138
+ currentLogger = new SimpleLogger();
139
+ }
140
+ const logger = {
141
+ log(level, ...args) {
142
+ currentLogger.log(level, ...args);
143
+ },
144
+ debug(...args) {
145
+ currentLogger.debug(...args);
146
+ },
147
+ info(...args) {
148
+ currentLogger.info(...args);
149
+ },
150
+ warn(...args) {
151
+ currentLogger.warn(...args);
152
+ },
153
+ error(...args) {
154
+ currentLogger.error(...args);
155
+ }
156
+ };
116
157
  // Annotate the CommonJS export names for ESM import in node:
117
158
  0 && (module.exports = {
118
159
  LogLevel,
160
+ getLogger,
119
161
  logger,
120
- setLogLevel
162
+ resetLogger,
163
+ setLogLevel,
164
+ setLogger
121
165
  });
@@ -29,160 +29,119 @@ __export(simple_zod_to_json_exports, {
29
29
  });
30
30
  module.exports = __toCommonJS(simple_zod_to_json_exports);
31
31
  var import_genai = require("@google/genai");
32
- var import_zod = require("zod");
32
+ var import_zod_to_json_schema = require("zod-to-json-schema");
33
+ var import_v4 = require("zod/v4");
33
34
  /**
34
35
  * @license
35
36
  * Copyright 2025 Google LLC
36
37
  * SPDX-License-Identifier: Apache-2.0
37
38
  */
38
- function isZodObject(obj) {
39
- var _a;
40
- return obj !== null && typeof obj === "object" && ((_a = obj._def) == null ? void 0 : _a.typeName) === "ZodObject";
39
+ function isZodSchema(obj) {
40
+ return obj !== null && typeof obj === "object" && "parse" in obj && typeof obj.parse === "function" && "safeParse" in obj && typeof obj.safeParse === "function";
41
+ }
42
+ function isZodV3Schema(obj) {
43
+ return isZodSchema(obj) && !("_zod" in obj);
44
+ }
45
+ function isZodV4Schema(obj) {
46
+ return isZodSchema(obj) && "_zod" in obj;
41
47
  }
42
- function parseZodType(zodType) {
43
- const def = zodType._def;
44
- if (!def) {
45
- return {};
48
+ function getZodTypeName(schema) {
49
+ var _a, _b;
50
+ const schemaAny = schema;
51
+ if ((_a = schemaAny._def) == null ? void 0 : _a.typeName) {
52
+ return schemaAny._def.typeName;
46
53
  }
47
- const description = def.description;
48
- const result = {};
49
- if (description) result.description = description;
50
- const returnResult = (result2) => {
51
- if (result2.description === void 0) {
52
- delete result2.description;
53
- }
54
- return result2;
55
- };
56
- switch (def.typeName) {
57
- case import_zod.z.ZodFirstPartyTypeKind.ZodString:
58
- result.type = import_genai.Type.STRING;
59
- for (const check of def.checks || []) {
60
- if (check.kind === "min")
61
- result.minLength = check.value.toString();
62
- else if (check.kind === "max")
63
- result.maxLength = check.value.toString();
64
- else if (check.kind === "email")
65
- result.format = "email";
66
- else if (check.kind === "uuid")
67
- result.format = "uuid";
68
- else if (check.kind === "url")
69
- result.format = "uri";
70
- else if (check.kind === "regex")
71
- result.pattern = check.regex.source;
72
- }
73
- return returnResult(result);
74
- case import_zod.z.ZodFirstPartyTypeKind.ZodNumber:
75
- result.type = import_genai.Type.NUMBER;
76
- for (const check of def.checks || []) {
77
- if (check.kind === "min")
78
- result.minimum = check.value;
79
- else if (check.kind === "max")
80
- result.maximum = check.value;
81
- else if (check.kind === "int")
82
- result.type = import_genai.Type.INTEGER;
83
- }
84
- return returnResult(result);
85
- case import_zod.z.ZodFirstPartyTypeKind.ZodBoolean:
86
- result.type = import_genai.Type.BOOLEAN;
87
- return returnResult(result);
88
- case import_zod.z.ZodFirstPartyTypeKind.ZodArray:
89
- result.type = import_genai.Type.ARRAY;
90
- result.items = parseZodType(def.type);
91
- if (def.minLength) result.minItems = def.minLength.value.toString();
92
- if (def.maxLength) result.maxItems = def.maxLength.value.toString();
93
- return returnResult(result);
94
- case import_zod.z.ZodFirstPartyTypeKind.ZodObject: {
95
- const nestedSchema = zodObjectToSchema(zodType);
96
- return nestedSchema;
97
- }
98
- case import_zod.z.ZodFirstPartyTypeKind.ZodLiteral:
99
- const literalType = typeof def.value;
100
- result.enum = [def.value.toString()];
101
- if (literalType === "string") {
102
- result.type = import_genai.Type.STRING;
103
- } else if (literalType === "number") {
104
- result.type = import_genai.Type.NUMBER;
105
- } else if (literalType === "boolean") {
106
- result.type = import_genai.Type.BOOLEAN;
107
- } else if (def.value === null) {
108
- result.type = import_genai.Type.NULL;
109
- } else {
110
- throw new Error(`Unsupported ZodLiteral value type: ${literalType}`);
111
- }
112
- return returnResult(result);
113
- case import_zod.z.ZodFirstPartyTypeKind.ZodEnum:
114
- result.type = import_genai.Type.STRING;
115
- result.enum = def.values;
116
- return returnResult(result);
117
- case import_zod.z.ZodFirstPartyTypeKind.ZodNativeEnum:
118
- result.type = import_genai.Type.STRING;
119
- result.enum = Object.values(def.values);
120
- return returnResult(result);
121
- case import_zod.z.ZodFirstPartyTypeKind.ZodUnion:
122
- result.anyOf = def.options.map(parseZodType);
123
- return returnResult(result);
124
- case import_zod.z.ZodFirstPartyTypeKind.ZodOptional:
125
- return parseZodType(def.innerType);
126
- case import_zod.z.ZodFirstPartyTypeKind.ZodNullable:
127
- const nullableInner = parseZodType(def.innerType);
128
- return nullableInner ? returnResult({
129
- anyOf: [nullableInner, { type: import_genai.Type.NULL }],
130
- ...description && { description }
131
- }) : returnResult({ type: import_genai.Type.NULL, ...description && { description } });
132
- case import_zod.z.ZodFirstPartyTypeKind.ZodDefault:
133
- const defaultInner = parseZodType(def.innerType);
134
- if (defaultInner) defaultInner.default = def.defaultValue();
135
- return defaultInner;
136
- case import_zod.z.ZodFirstPartyTypeKind.ZodBranded:
137
- return parseZodType(def.type);
138
- case import_zod.z.ZodFirstPartyTypeKind.ZodReadonly:
139
- return parseZodType(def.innerType);
140
- case import_zod.z.ZodFirstPartyTypeKind.ZodNull:
141
- result.type = import_genai.Type.NULL;
142
- return returnResult(result);
143
- case import_zod.z.ZodFirstPartyTypeKind.ZodAny:
144
- case import_zod.z.ZodFirstPartyTypeKind.ZodUnknown:
145
- return returnResult({ ...description && { description } });
146
- default:
147
- throw new Error(`Unsupported Zod type: ${def.typeName}`);
54
+ const zod4Type = (_b = schemaAny._def) == null ? void 0 : _b.type;
55
+ if (typeof zod4Type === "string" && zod4Type) {
56
+ return "Zod" + zod4Type.charAt(0).toUpperCase() + zod4Type.slice(1);
148
57
  }
58
+ return void 0;
59
+ }
60
+ function isZodObject(obj) {
61
+ return isZodSchema(obj) && getZodTypeName(obj) === "ZodObject";
149
62
  }
150
63
  function zodObjectToSchema(schema) {
151
- if (schema._def.typeName !== import_zod.z.ZodFirstPartyTypeKind.ZodObject) {
152
- throw new Error("Expected a ZodObject");
64
+ if (!isZodObject(schema)) {
65
+ throw new Error("Expected a Zod Object");
153
66
  }
154
- const shape = schema.shape;
155
- const properties = {};
156
- const required = [];
157
- for (const key in shape) {
158
- const fieldSchema = shape[key];
159
- const parsedField = parseZodType(fieldSchema);
160
- if (parsedField) {
161
- properties[key] = parsedField;
162
- }
163
- let currentSchema = fieldSchema;
164
- let isOptional = false;
165
- while (currentSchema._def.typeName === import_zod.z.ZodFirstPartyTypeKind.ZodOptional || currentSchema._def.typeName === import_zod.z.ZodFirstPartyTypeKind.ZodDefault) {
166
- isOptional = true;
167
- currentSchema = currentSchema._def.innerType;
168
- }
169
- if (!isOptional) {
170
- required.push(key);
171
- }
67
+ if (isZodV4Schema(schema)) {
68
+ return (0, import_v4.toJSONSchema)(schema, {
69
+ target: "openapi-3.0",
70
+ io: "input",
71
+ override: (ctx) => {
72
+ var _a;
73
+ const { jsonSchema } = ctx;
74
+ if (jsonSchema.additionalProperties !== void 0) {
75
+ delete jsonSchema.additionalProperties;
76
+ }
77
+ if (jsonSchema.readOnly !== void 0) {
78
+ delete jsonSchema.readOnly;
79
+ }
80
+ if (jsonSchema.maxItems !== void 0) {
81
+ jsonSchema.maxItems = jsonSchema.maxItems.toString();
82
+ }
83
+ if (jsonSchema.format === "email" || jsonSchema.format === "uuid") {
84
+ delete jsonSchema.pattern;
85
+ }
86
+ if (jsonSchema.minItems !== void 0) {
87
+ jsonSchema.minItems = jsonSchema.minItems.toString();
88
+ }
89
+ if (jsonSchema.minLength !== void 0) {
90
+ jsonSchema.minLength = jsonSchema.minLength.toString();
91
+ }
92
+ if (jsonSchema.maxLength !== void 0) {
93
+ jsonSchema.maxLength = jsonSchema.maxLength.toString();
94
+ }
95
+ if (((_a = jsonSchema.enum) == null ? void 0 : _a.length) === 1 && jsonSchema.enum[0] === null) {
96
+ jsonSchema.type = import_genai.Type.NULL;
97
+ delete jsonSchema.enum;
98
+ }
99
+ if (jsonSchema.type !== void 0) {
100
+ jsonSchema.type = jsonSchema.type.toUpperCase();
101
+ }
102
+ }
103
+ });
172
104
  }
173
- const catchall = schema._def.catchall;
174
- let additionalProperties = false;
175
- if (catchall && catchall._def.typeName !== import_zod.z.ZodFirstPartyTypeKind.ZodNever) {
176
- additionalProperties = parseZodType(catchall) || true;
177
- } else {
178
- additionalProperties = schema._def.unknownKeys === "passthrough";
105
+ if (isZodV3Schema(schema)) {
106
+ return (0, import_zod_to_json_schema.zodToJsonSchema)(schema, {
107
+ target: "openApi3",
108
+ emailStrategy: "format:email",
109
+ postProcess: (jsonSchema) => {
110
+ var _a, _b, _c, _d, _e, _f, _g;
111
+ if (!jsonSchema) {
112
+ return;
113
+ }
114
+ if (jsonSchema.additionalProperties !== void 0) {
115
+ delete jsonSchema.additionalProperties;
116
+ }
117
+ if (jsonSchema.maxItems !== void 0) {
118
+ jsonSchema.maxItems = (_a = jsonSchema.maxItems) == null ? void 0 : _a.toString();
119
+ }
120
+ if (jsonSchema.minItems !== void 0) {
121
+ jsonSchema.minItems = (_b = jsonSchema.minItems) == null ? void 0 : _b.toString();
122
+ }
123
+ if (jsonSchema.minLength !== void 0) {
124
+ jsonSchema.minLength = (_c = jsonSchema.minLength) == null ? void 0 : _c.toString();
125
+ }
126
+ if (jsonSchema.maxLength !== void 0) {
127
+ jsonSchema.maxLength = (_d = jsonSchema.maxLength) == null ? void 0 : _d.toString();
128
+ }
129
+ if (((_e = jsonSchema.enum) == null ? void 0 : _e.length) === 1 && jsonSchema.enum[0] === "null") {
130
+ jsonSchema.type = import_genai.Type.NULL;
131
+ delete jsonSchema.enum;
132
+ }
133
+ if (jsonSchema.type === "integer" && jsonSchema.format !== "int64") {
134
+ (_f = jsonSchema.minimum) != null ? _f : jsonSchema.minimum = Number.MIN_SAFE_INTEGER;
135
+ (_g = jsonSchema.maximum) != null ? _g : jsonSchema.maximum = Number.MAX_SAFE_INTEGER;
136
+ }
137
+ if (jsonSchema.type !== void 0) {
138
+ jsonSchema.type = jsonSchema.type.toUpperCase();
139
+ }
140
+ return jsonSchema;
141
+ }
142
+ });
179
143
  }
180
- return {
181
- type: import_genai.Type.OBJECT,
182
- properties,
183
- required: required.length > 0 ? required : [],
184
- ...schema._def.description ? { description: schema._def.description } : {}
185
- };
144
+ throw new Error("Unsupported Zod schema version.");
186
145
  }
187
146
  // Annotate the CommonJS export names for ESM import in node:
188
147
  0 && (module.exports = {
@@ -46,7 +46,7 @@ function getBooleanEnvVar(envVar) {
46
46
  return false;
47
47
  }
48
48
  const envVarValue = (process.env[envVar] || "").toLowerCase();
49
- return ["true", "1"].includes(envVar.toLowerCase());
49
+ return ["true", "1"].includes(envVarValue);
50
50
  }
51
51
  // Annotate the CommonJS export names for ESM import in node:
52
52
  0 && (module.exports = {
@@ -32,7 +32,7 @@ module.exports = __toCommonJS(version_exports);
32
32
  * Copyright 2025 Google LLC
33
33
  * SPDX-License-Identifier: Apache-2.0
34
34
  */
35
- const version = "0.2.5";
35
+ const version = "0.3.0";
36
36
  // Annotate the CommonJS export names for ESM import in node:
37
37
  0 && (module.exports = {
38
38
  version
@@ -4,8 +4,13 @@
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
6
  var _a;
7
- import { trace } from "@opentelemetry/api";
7
+ import { context, trace } from "@opentelemetry/api";
8
8
  import { createEvent } from "../events/event.js";
9
+ import {
10
+ runAsyncGeneratorWithOtelContext,
11
+ traceAgentInvocation,
12
+ tracer
13
+ } from "../telemetry/tracing.js";
9
14
  import { CallbackContext } from "./callback_context.js";
10
15
  import { InvocationContext } from "./invocation_context.js";
11
16
  const BASE_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.baseAgent");
@@ -23,13 +28,19 @@ class BaseAgent {
23
28
  this.description = config.description;
24
29
  this.parentAgent = config.parentAgent;
25
30
  this.subAgents = config.subAgents || [];
26
- this.rootAgent = getRootAgent(this);
27
31
  this.beforeAgentCallback = getCannonicalCallback(
28
32
  config.beforeAgentCallback
29
33
  );
30
34
  this.afterAgentCallback = getCannonicalCallback(config.afterAgentCallback);
31
35
  this.setParentAgentForSubAgents();
32
36
  }
37
+ /**
38
+ * Root agent of this agent.
39
+ * Computed dynamically by traversing up the parent chain.
40
+ */
41
+ get rootAgent() {
42
+ return getRootAgent(this);
43
+ }
33
44
  /**
34
45
  * Entry method to run an agent via text-based conversation.
35
46
  *
@@ -38,26 +49,34 @@ class BaseAgent {
38
49
  * @returns An AsyncGenerator that yields the events generated by the agent.
39
50
  */
40
51
  async *runAsync(parentContext) {
41
- const span = trace.getTracer("gcp.vertex.agent").startSpan(`agent_run [${this.name}]`);
52
+ const span = tracer.startSpan(`invoke_agent ${this.name}`);
53
+ const ctx = trace.setSpan(context.active(), span);
42
54
  try {
43
- const context = this.createInvocationContext(parentContext);
44
- const beforeAgentCallbackEvent = await this.handleBeforeAgentCallback(context);
45
- if (beforeAgentCallbackEvent) {
46
- yield beforeAgentCallbackEvent;
47
- }
48
- if (context.endInvocation) {
49
- return;
50
- }
51
- for await (const event of this.runAsyncImpl(context)) {
52
- yield event;
53
- }
54
- if (context.endInvocation) {
55
- return;
56
- }
57
- const afterAgentCallbackEvent = await this.handleAfterAgentCallback(context);
58
- if (afterAgentCallbackEvent) {
59
- yield afterAgentCallbackEvent;
60
- }
55
+ yield* runAsyncGeneratorWithOtelContext(
56
+ ctx,
57
+ this,
58
+ async function* () {
59
+ const context2 = this.createInvocationContext(parentContext);
60
+ const beforeAgentCallbackEvent = await this.handleBeforeAgentCallback(context2);
61
+ if (beforeAgentCallbackEvent) {
62
+ yield beforeAgentCallbackEvent;
63
+ }
64
+ if (context2.endInvocation) {
65
+ return;
66
+ }
67
+ traceAgentInvocation({ agent: this, invocationContext: context2 });
68
+ for await (const event of this.runAsyncImpl(context2)) {
69
+ yield event;
70
+ }
71
+ if (context2.endInvocation) {
72
+ return;
73
+ }
74
+ const afterAgentCallbackEvent = await this.handleAfterAgentCallback(context2);
75
+ if (afterAgentCallbackEvent) {
76
+ yield afterAgentCallbackEvent;
77
+ }
78
+ }
79
+ );
61
80
  } finally {
62
81
  span.end();
63
82
  }
@@ -70,8 +89,15 @@ class BaseAgent {
70
89
  * @returns An AsyncGenerator that yields the events generated by the agent.
71
90
  */
72
91
  async *runLive(parentContext) {
73
- const span = trace.getTracer("gcp.vertex.agent").startSpan(`agent_run [${this.name}]`);
92
+ const span = tracer.startSpan(`invoke_agent ${this.name}`);
93
+ const ctx = trace.setSpan(context.active(), span);
74
94
  try {
95
+ yield* runAsyncGeneratorWithOtelContext(
96
+ ctx,
97
+ this,
98
+ async function* () {
99
+ }
100
+ );
75
101
  throw new Error("Live mode is not implemented yet.");
76
102
  } finally {
77
103
  span.end();
@@ -7,7 +7,10 @@ import { createEventActions } from "../events/event_actions.js";
7
7
  import { State } from "../sessions/state.js";
8
8
  import { ReadonlyContext } from "./readonly_context.js";
9
9
  class CallbackContext extends ReadonlyContext {
10
- constructor({ invocationContext, eventActions }) {
10
+ constructor({
11
+ invocationContext,
12
+ eventActions
13
+ }) {
11
14
  super(invocationContext);
12
15
  this.eventActions = eventActions || createEventActions();
13
16
  this._state = new State(
@@ -4,8 +4,16 @@
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
6
  import { cloneDeep } from "lodash-es";
7
- import { createEvent, getFunctionCalls, getFunctionResponses } from "../events/event.js";
8
- import { removeClientFunctionCallId, REQUEST_CONFIRMATION_FUNCTION_CALL_NAME, REQUEST_EUC_FUNCTION_CALL_NAME } from "./functions.js";
7
+ import {
8
+ createEvent,
9
+ getFunctionCalls,
10
+ getFunctionResponses
11
+ } from "../events/event.js";
12
+ import {
13
+ removeClientFunctionCallId,
14
+ REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
15
+ REQUEST_EUC_FUNCTION_CALL_NAME
16
+ } from "./functions.js";
9
17
  function getContents(events, agentName, currentBranch) {
10
18
  var _a, _b, _c;
11
19
  const filteredEvents = [];
@@ -79,9 +87,11 @@ function convertForeignEvent(event) {
79
87
  }
80
88
  const content = {
81
89
  role: "user",
82
- parts: [{
83
- text: "For context:"
84
- }]
90
+ parts: [
91
+ {
92
+ text: "For context:"
93
+ }
94
+ ]
85
95
  };
86
96
  for (const part of event.content.parts) {
87
97
  if (part.text && !part.thought) {
@@ -183,10 +193,14 @@ function rearrangeEventsForLatestFunctionResponse(events) {
183
193
  const functionCallIds = new Set(
184
194
  functionCalls.map((fc) => fc.id).filter((id) => !!id)
185
195
  );
186
- const isSubset = Array.from(functionResponsesIds).every((id) => functionCallIds.has(id));
196
+ const isSubset = Array.from(functionResponsesIds).every(
197
+ (id) => functionCallIds.has(id)
198
+ );
187
199
  if (!isSubset) {
188
200
  throw new Error(
189
- `Last response event should only contain the responses for the function calls in the same function call event. Function call ids found : ${Array.from(functionCallIds).join(", ")}, function response ids provided: ${Array.from(functionResponsesIds).join(", ")}`
201
+ `Last response event should only contain the responses for the function calls in the same function call event. Function call ids found : ${Array.from(functionCallIds).join(
202
+ ", "
203
+ )}, function response ids provided: ${Array.from(functionResponsesIds).join(", ")}`
190
204
  );
191
205
  }
192
206
  functionResponsesIds = functionCallIds;
@@ -254,7 +268,9 @@ function rearrangeEventsForAsyncFunctionResponsesInHistory(events) {
254
268
  const [responseIndex] = [...functionResponseEventsIndices];
255
269
  resultEvents.push(events[responseIndex]);
256
270
  } else {
257
- const indicesArray = Array.from(functionResponseEventsIndices).sort((a, b) => a - b);
271
+ const indicesArray = Array.from(functionResponseEventsIndices).sort(
272
+ (a, b) => a - b
273
+ );
258
274
  const eventsToMerge = indicesArray.map((index) => events[index]);
259
275
  resultEvents.push(mergeFunctionResponseEvents(eventsToMerge));
260
276
  }
@@ -270,7 +286,7 @@ function safeStringify(obj) {
270
286
  }
271
287
  try {
272
288
  return JSON.stringify(obj);
273
- } catch (e) {
289
+ } catch (_e) {
274
290
  return String(obj);
275
291
  }
276
292
  }