@google/adk 0.4.0 → 0.5.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 (132) hide show
  1. package/dist/cjs/a2a/a2a_event.js +290 -0
  2. package/dist/cjs/a2a/event_converter_utils.js +201 -0
  3. package/dist/cjs/a2a/executor_context.js +53 -0
  4. package/dist/cjs/a2a/metadata_converter_utils.js +125 -0
  5. package/dist/cjs/a2a/part_converter_utils.js +23 -21
  6. package/dist/cjs/agents/base_agent.js +3 -3
  7. package/dist/cjs/{tools/tool_context.js → agents/context.js} +70 -15
  8. package/dist/cjs/agents/functions.js +2 -2
  9. package/dist/cjs/agents/llm_agent.js +21 -674
  10. package/dist/cjs/agents/processors/agent_transfer_llm_request_processor.js +132 -0
  11. package/dist/cjs/agents/processors/basic_llm_request_processor.js +68 -0
  12. package/dist/cjs/agents/processors/code_execution_request_processor.js +389 -0
  13. package/dist/cjs/agents/processors/content_request_processor.js +66 -0
  14. package/dist/cjs/agents/processors/identity_llm_request_processor.js +54 -0
  15. package/dist/cjs/agents/processors/instructions_llm_request_processor.js +85 -0
  16. package/dist/cjs/agents/processors/request_confirmation_llm_request_processor.js +165 -0
  17. package/dist/cjs/common.js +11 -7
  18. package/dist/cjs/index.js +89 -53
  19. package/dist/cjs/index.js.map +7 -0
  20. package/dist/cjs/models/llm_response.js +2 -0
  21. package/dist/cjs/plugins/base_plugin.js +1 -1
  22. package/dist/cjs/runner/runner.js +1 -1
  23. package/dist/cjs/sessions/database_session_service.js +4 -1
  24. package/dist/cjs/sessions/db/operations.js +24 -12
  25. package/dist/cjs/tools/base_tool.js +3 -0
  26. package/dist/cjs/tools/base_toolset.js +13 -3
  27. package/dist/cjs/tools/exit_loop_tool.js +63 -0
  28. package/dist/cjs/tools/mcp/mcp_toolset.js +9 -5
  29. package/dist/cjs/utils/logger.js +61 -54
  30. package/dist/cjs/version.js +1 -1
  31. package/dist/esm/a2a/a2a_event.js +243 -0
  32. package/dist/esm/a2a/event_converter_utils.js +187 -0
  33. package/dist/esm/a2a/executor_context.js +23 -0
  34. package/dist/esm/a2a/metadata_converter_utils.js +90 -0
  35. package/dist/esm/a2a/part_converter_utils.js +25 -21
  36. package/dist/esm/agents/base_agent.js +3 -3
  37. package/dist/esm/{tools/tool_context.js → agents/context.js} +66 -11
  38. package/dist/esm/agents/functions.js +2 -2
  39. package/dist/esm/agents/llm_agent.js +14 -683
  40. package/dist/esm/agents/processors/agent_transfer_llm_request_processor.js +101 -0
  41. package/dist/esm/agents/processors/basic_llm_request_processor.js +37 -0
  42. package/dist/esm/agents/processors/code_execution_request_processor.js +363 -0
  43. package/dist/esm/agents/processors/content_request_processor.js +38 -0
  44. package/dist/esm/agents/processors/identity_llm_request_processor.js +23 -0
  45. package/dist/esm/agents/processors/instructions_llm_request_processor.js +54 -0
  46. package/dist/esm/agents/processors/request_confirmation_llm_request_processor.js +140 -0
  47. package/dist/esm/common.js +11 -9
  48. package/dist/esm/index.js +95 -18
  49. package/dist/esm/index.js.map +7 -0
  50. package/dist/esm/models/llm_response.js +2 -0
  51. package/dist/esm/plugins/base_plugin.js +1 -1
  52. package/dist/esm/runner/runner.js +1 -1
  53. package/dist/esm/sessions/database_session_service.js +4 -1
  54. package/dist/esm/sessions/db/operations.js +31 -7
  55. package/dist/esm/tools/base_tool.js +3 -0
  56. package/dist/esm/tools/base_toolset.js +11 -2
  57. package/dist/esm/tools/exit_loop_tool.js +32 -0
  58. package/dist/esm/tools/mcp/mcp_toolset.js +9 -5
  59. package/dist/esm/utils/logger.js +51 -54
  60. package/dist/esm/version.js +1 -1
  61. package/dist/types/a2a/a2a_event.d.ts +122 -0
  62. package/dist/types/a2a/event_converter_utils.d.ts +20 -0
  63. package/dist/types/a2a/executor_context.d.ts +33 -0
  64. package/dist/types/a2a/metadata_converter_utils.d.ts +62 -0
  65. package/dist/types/a2a/part_converter_utils.d.ts +4 -3
  66. package/dist/types/agents/base_agent.d.ts +2 -2
  67. package/dist/types/{tools/tool_context.d.ts → agents/context.d.ts} +43 -8
  68. package/dist/types/agents/llm_agent.d.ts +8 -31
  69. package/dist/types/agents/processors/agent_transfer_llm_request_processor.d.ts +18 -0
  70. package/dist/types/agents/{base_llm_processor.d.ts → processors/base_llm_processor.d.ts} +4 -4
  71. package/dist/types/agents/processors/basic_llm_request_processor.d.ts +13 -0
  72. package/dist/types/agents/processors/code_execution_request_processor.d.ts +34 -0
  73. package/dist/types/agents/processors/content_request_processor.d.ts +13 -0
  74. package/dist/types/agents/processors/identity_llm_request_processor.d.ts +13 -0
  75. package/dist/types/agents/processors/instructions_llm_request_processor.d.ts +16 -0
  76. package/dist/types/agents/processors/request_confirmation_llm_request_processor.d.ts +13 -0
  77. package/dist/types/auth/credential_service/base_credential_service.d.ts +3 -3
  78. package/dist/types/auth/credential_service/in_memory_credential_service.d.ts +3 -3
  79. package/dist/types/common.d.ts +4 -4
  80. package/dist/types/models/llm_response.d.ts +5 -1
  81. package/dist/types/plugins/base_plugin.d.ts +12 -13
  82. package/dist/types/plugins/logging_plugin.d.ts +9 -10
  83. package/dist/types/plugins/plugin_manager.d.ts +9 -10
  84. package/dist/types/plugins/security_plugin.d.ts +2 -2
  85. package/dist/types/sessions/database_session_service.d.ts +2 -1
  86. package/dist/types/sessions/db/operations.d.ts +1 -1
  87. package/dist/types/tools/base_tool.d.ts +3 -3
  88. package/dist/types/tools/base_toolset.d.ts +12 -3
  89. package/dist/types/tools/exit_loop_tool.d.ts +24 -0
  90. package/dist/types/tools/forwarding_artifact_service.d.ts +2 -2
  91. package/dist/types/tools/function_tool.d.ts +2 -2
  92. package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -1
  93. package/dist/types/utils/logger.d.ts +5 -9
  94. package/dist/types/version.d.ts +1 -1
  95. package/dist/web/a2a/a2a_event.js +243 -0
  96. package/dist/web/a2a/event_converter_utils.js +201 -0
  97. package/dist/web/a2a/executor_context.js +23 -0
  98. package/dist/web/a2a/metadata_converter_utils.js +107 -0
  99. package/dist/web/a2a/part_converter_utils.js +25 -21
  100. package/dist/web/agents/base_agent.js +3 -3
  101. package/dist/web/{tools/tool_context.js → agents/context.js} +66 -11
  102. package/dist/web/agents/functions.js +2 -2
  103. package/dist/web/agents/llm_agent.js +14 -661
  104. package/dist/web/agents/processors/agent_transfer_llm_request_processor.js +100 -0
  105. package/dist/web/agents/processors/basic_llm_request_processor.js +71 -0
  106. package/dist/web/agents/processors/code_execution_request_processor.js +365 -0
  107. package/dist/web/agents/processors/content_request_processor.js +56 -0
  108. package/dist/web/agents/processors/identity_llm_request_processor.js +41 -0
  109. package/dist/web/agents/processors/instructions_llm_request_processor.js +72 -0
  110. package/dist/web/agents/processors/request_confirmation_llm_request_processor.js +158 -0
  111. package/dist/web/common.js +11 -9
  112. package/dist/web/index.js +13 -18
  113. package/dist/web/index.js.map +7 -0
  114. package/dist/web/models/llm_response.js +2 -0
  115. package/dist/web/plugins/base_plugin.js +1 -1
  116. package/dist/web/runner/runner.js +1 -1
  117. package/dist/web/sessions/database_session_service.js +4 -1
  118. package/dist/web/sessions/db/operations.js +31 -7
  119. package/dist/web/tools/base_tool.js +3 -0
  120. package/dist/web/tools/base_toolset.js +11 -2
  121. package/dist/web/tools/exit_loop_tool.js +32 -0
  122. package/dist/web/tools/mcp/mcp_toolset.js +27 -5
  123. package/dist/web/utils/logger.js +51 -54
  124. package/dist/web/version.js +1 -1
  125. package/package.json +3 -2
  126. package/dist/cjs/agents/callback_context.js +0 -101
  127. package/dist/esm/agents/callback_context.js +0 -71
  128. package/dist/types/agents/callback_context.d.ts +0 -42
  129. package/dist/web/agents/callback_context.js +0 -71
  130. /package/dist/cjs/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
  131. /package/dist/esm/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
  132. /package/dist/web/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
@@ -0,0 +1,132 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ "use strict";
8
+ var __defProp = Object.defineProperty;
9
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
+ var __getOwnPropNames = Object.getOwnPropertyNames;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var agent_transfer_llm_request_processor_exports = {};
26
+ __export(agent_transfer_llm_request_processor_exports, {
27
+ AGENT_TRANSFER_LLM_REQUEST_PROCESSOR: () => AGENT_TRANSFER_LLM_REQUEST_PROCESSOR,
28
+ AgentTransferLlmRequestProcessor: () => AgentTransferLlmRequestProcessor
29
+ });
30
+ module.exports = __toCommonJS(agent_transfer_llm_request_processor_exports);
31
+ var import_zod = require("zod");
32
+ var import_llm_request = require("../../models/llm_request.js");
33
+ var import_function_tool = require("../../tools/function_tool.js");
34
+ var import_context = require("../context.js");
35
+ var import_llm_agent = require("../llm_agent.js");
36
+ var import_base_llm_processor = require("./base_llm_processor.js");
37
+ /**
38
+ * @license
39
+ * Copyright 2025 Google LLC
40
+ * SPDX-License-Identifier: Apache-2.0
41
+ */
42
+ class AgentTransferLlmRequestProcessor extends import_base_llm_processor.BaseLlmRequestProcessor {
43
+ constructor() {
44
+ super(...arguments);
45
+ this.toolName = "transfer_to_agent";
46
+ this.tool = new import_function_tool.FunctionTool({
47
+ name: this.toolName,
48
+ description: "Transfer the question to another agent. This tool hands off control to another agent when it is more suitable to answer the user question according to the agent description.",
49
+ parameters: import_zod.z.object({
50
+ agentName: import_zod.z.string().describe("the agent name to transfer to.")
51
+ }),
52
+ execute: function(args, toolContext) {
53
+ if (!toolContext) {
54
+ throw new Error("toolContext is required.");
55
+ }
56
+ toolContext.actions.transferToAgent = args.agentName;
57
+ return "Transfer queued";
58
+ }
59
+ });
60
+ }
61
+ // eslint-disable-next-line require-yield
62
+ async *runAsync(invocationContext, llmRequest) {
63
+ if (!(0, import_llm_agent.isLlmAgent)(invocationContext.agent)) {
64
+ return;
65
+ }
66
+ const transferTargets = this.getTransferTargets(invocationContext.agent);
67
+ if (!transferTargets.length) {
68
+ return;
69
+ }
70
+ (0, import_llm_request.appendInstructions)(llmRequest, [
71
+ this.buildTargetAgentsInstructions(
72
+ invocationContext.agent,
73
+ transferTargets
74
+ )
75
+ ]);
76
+ const toolContext = new import_context.Context({ invocationContext });
77
+ await this.tool.processLlmRequest({ toolContext, llmRequest });
78
+ }
79
+ buildTargetAgentsInfo(targetAgent) {
80
+ return `
81
+ Agent name: ${targetAgent.name}
82
+ Agent description: ${targetAgent.description}
83
+ `;
84
+ }
85
+ buildTargetAgentsInstructions(agent, targetAgents) {
86
+ let instructions = `
87
+ You have a list of other agents to transfer to:
88
+
89
+ ${targetAgents.map((t) => this.buildTargetAgentsInfo(t)).join("\n")}
90
+
91
+ If you are the best to answer the question according to your description, you
92
+ can answer it.
93
+
94
+ If another agent is better for answering the question according to its
95
+ description, call \`${this.toolName}\` function to transfer the
96
+ question to that agent. When transferring, do not generate any text other than
97
+ the function call.
98
+ `;
99
+ if (agent.parentAgent && !agent.disallowTransferToParent) {
100
+ instructions += `
101
+ Your parent agent is ${agent.parentAgent.name}. If neither the other agents nor
102
+ you are best for answering the question according to the descriptions, transfer
103
+ to your parent agent.
104
+ `;
105
+ }
106
+ return instructions;
107
+ }
108
+ getTransferTargets(agent) {
109
+ const targets = [];
110
+ targets.push(...agent.subAgents);
111
+ if (!agent.parentAgent || !(0, import_llm_agent.isLlmAgent)(agent.parentAgent)) {
112
+ return targets;
113
+ }
114
+ if (!agent.disallowTransferToParent) {
115
+ targets.push(agent.parentAgent);
116
+ }
117
+ if (!agent.disallowTransferToPeers) {
118
+ targets.push(
119
+ ...agent.parentAgent.subAgents.filter(
120
+ (peerAgent) => peerAgent.name !== agent.name
121
+ )
122
+ );
123
+ }
124
+ return targets;
125
+ }
126
+ }
127
+ const AGENT_TRANSFER_LLM_REQUEST_PROCESSOR = new AgentTransferLlmRequestProcessor();
128
+ // Annotate the CommonJS export names for ESM import in node:
129
+ 0 && (module.exports = {
130
+ AGENT_TRANSFER_LLM_REQUEST_PROCESSOR,
131
+ AgentTransferLlmRequestProcessor
132
+ });
@@ -0,0 +1,68 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ "use strict";
8
+ var __defProp = Object.defineProperty;
9
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
+ var __getOwnPropNames = Object.getOwnPropertyNames;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var basic_llm_request_processor_exports = {};
26
+ __export(basic_llm_request_processor_exports, {
27
+ BASIC_LLM_REQUEST_PROCESSOR: () => BASIC_LLM_REQUEST_PROCESSOR,
28
+ BasicLlmRequestProcessor: () => BasicLlmRequestProcessor
29
+ });
30
+ module.exports = __toCommonJS(basic_llm_request_processor_exports);
31
+ var import_llm_request = require("../../models/llm_request.js");
32
+ var import_llm_agent = require("../llm_agent.js");
33
+ var import_base_llm_processor = require("./base_llm_processor.js");
34
+ /**
35
+ * @license
36
+ * Copyright 2025 Google LLC
37
+ * SPDX-License-Identifier: Apache-2.0
38
+ */
39
+ class BasicLlmRequestProcessor extends import_base_llm_processor.BaseLlmRequestProcessor {
40
+ // eslint-disable-next-line require-yield
41
+ async *runAsync(invocationContext, llmRequest) {
42
+ var _a;
43
+ const agent = invocationContext.agent;
44
+ if (!(0, import_llm_agent.isLlmAgent)(agent)) {
45
+ return;
46
+ }
47
+ llmRequest.model = agent.canonicalModel.model;
48
+ llmRequest.config = { ...(_a = agent.generateContentConfig) != null ? _a : {} };
49
+ if (agent.outputSchema) {
50
+ (0, import_llm_request.setOutputSchema)(llmRequest, agent.outputSchema);
51
+ }
52
+ if (invocationContext.runConfig) {
53
+ llmRequest.liveConnectConfig.responseModalities = invocationContext.runConfig.responseModalities;
54
+ llmRequest.liveConnectConfig.speechConfig = invocationContext.runConfig.speechConfig;
55
+ llmRequest.liveConnectConfig.outputAudioTranscription = invocationContext.runConfig.outputAudioTranscription;
56
+ llmRequest.liveConnectConfig.inputAudioTranscription = invocationContext.runConfig.inputAudioTranscription;
57
+ llmRequest.liveConnectConfig.realtimeInputConfig = invocationContext.runConfig.realtimeInputConfig;
58
+ llmRequest.liveConnectConfig.enableAffectiveDialog = invocationContext.runConfig.enableAffectiveDialog;
59
+ llmRequest.liveConnectConfig.proactivity = invocationContext.runConfig.proactivity;
60
+ }
61
+ }
62
+ }
63
+ const BASIC_LLM_REQUEST_PROCESSOR = new BasicLlmRequestProcessor();
64
+ // Annotate the CommonJS export names for ESM import in node:
65
+ 0 && (module.exports = {
66
+ BASIC_LLM_REQUEST_PROCESSOR,
67
+ BasicLlmRequestProcessor
68
+ });
@@ -0,0 +1,389 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ "use strict";
8
+ var __defProp = Object.defineProperty;
9
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
+ var __getOwnPropNames = Object.getOwnPropertyNames;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var code_execution_request_processor_exports = {};
26
+ __export(code_execution_request_processor_exports, {
27
+ CODE_EXECUTION_REQUEST_PROCESSOR: () => CODE_EXECUTION_REQUEST_PROCESSOR,
28
+ CodeExecutionRequestProcessor: () => CodeExecutionRequestProcessor,
29
+ CodeExecutionResponseProcessor: () => CodeExecutionResponseProcessor,
30
+ responseProcessor: () => responseProcessor
31
+ });
32
+ module.exports = __toCommonJS(code_execution_request_processor_exports);
33
+ var import_lodash_es = require("lodash-es");
34
+ var import_base_code_executor = require("../../code_executors/base_code_executor.js");
35
+ var import_built_in_code_executor = require("../../code_executors/built_in_code_executor.js");
36
+ var import_code_execution_utils = require("../../code_executors/code_execution_utils.js");
37
+ var import_code_executor_context = require("../../code_executors/code_executor_context.js");
38
+ var import_event = require("../../events/event.js");
39
+ var import_event_actions = require("../../events/event_actions.js");
40
+ var import_state = require("../../sessions/state.js");
41
+ var import_env_aware_utils = require("../../utils/env_aware_utils.js");
42
+ var import_llm_agent = require("../llm_agent.js");
43
+ var import_base_llm_processor = require("./base_llm_processor.js");
44
+ /**
45
+ * @license
46
+ * Copyright 2025 Google LLC
47
+ * SPDX-License-Identifier: Apache-2.0
48
+ */
49
+ class CodeExecutionRequestProcessor extends import_base_llm_processor.BaseLlmRequestProcessor {
50
+ async *runAsync(invocationContext, llmRequest) {
51
+ if (!(0, import_llm_agent.isLlmAgent)(invocationContext.agent)) {
52
+ return;
53
+ }
54
+ if (!invocationContext.agent.codeExecutor) {
55
+ return;
56
+ }
57
+ for await (const event of runPreProcessor(invocationContext, llmRequest)) {
58
+ yield event;
59
+ }
60
+ if (!(0, import_base_code_executor.isBaseCodeExecutor)(invocationContext.agent.codeExecutor)) {
61
+ return;
62
+ }
63
+ for (const content of llmRequest.contents) {
64
+ const delimeters = invocationContext.agent.codeExecutor.codeBlockDelimiters.length ? invocationContext.agent.codeExecutor.codeBlockDelimiters[0] : ["", ""];
65
+ (0, import_code_execution_utils.convertCodeExecutionParts)(
66
+ content,
67
+ delimeters,
68
+ invocationContext.agent.codeExecutor.executionResultDelimiters
69
+ );
70
+ }
71
+ }
72
+ }
73
+ const DATA_FILE_UTIL_MAP = {
74
+ "text/csv": {
75
+ extension: ".csv",
76
+ loaderCodeTemplate: "pd.read_csv('{filename}')"
77
+ }
78
+ };
79
+ const DATA_FILE_HELPER_LIB = `
80
+ import pandas as pd
81
+
82
+ def explore_df(df: pd.DataFrame) -> None:
83
+ """Prints some information about a pandas DataFrame."""
84
+
85
+ with pd.option_context(
86
+ 'display.max_columns', None, 'display.expand_frame_repr', False
87
+ ):
88
+ # Print the column names to never encounter KeyError when selecting one.
89
+ df_dtypes = df.dtypes
90
+
91
+ # Obtain information about data types and missing values.
92
+ df_nulls = (len(df) - df.isnull().sum()).apply(
93
+ lambda x: f'{x} / {df.shape[0]} non-null'
94
+ )
95
+
96
+ # Explore unique total values in columns using \`.unique()\`.
97
+ df_unique_count = df.apply(lambda x: len(x.unique()))
98
+
99
+ # Explore unique values in columns using \`.unique()\`.
100
+ df_unique = df.apply(lambda x: crop(str(list(x.unique()))))
101
+
102
+ df_info = pd.concat(
103
+ (
104
+ df_dtypes.rename('Dtype'),
105
+ df_nulls.rename('Non-Null Count'),
106
+ df_unique_count.rename('Unique Values Count'),
107
+ df_unique.rename('Unique Values'),
108
+ ),
109
+ axis=1,
110
+ )
111
+ df_info.index.name = 'Columns'
112
+ print(f"""Total rows: {df.shape[0]}
113
+ Total columns: {df.shape[1]}
114
+
115
+ {df_info}""")
116
+ `;
117
+ class CodeExecutionResponseProcessor {
118
+ /**
119
+ * Processes the LLM response asynchronously.
120
+ *
121
+ * @param invocationContext The invocation context
122
+ * @param llmResponse The LLM response to process
123
+ * @returns An async generator yielding events
124
+ */
125
+ async *runAsync(invocationContext, llmResponse) {
126
+ if (llmResponse.partial) {
127
+ return;
128
+ }
129
+ for await (const event of runPostProcessor(
130
+ invocationContext,
131
+ llmResponse
132
+ )) {
133
+ yield event;
134
+ }
135
+ }
136
+ }
137
+ const responseProcessor = new CodeExecutionResponseProcessor();
138
+ async function* runPreProcessor(invocationContext, llmRequest) {
139
+ const agent = invocationContext.agent;
140
+ if (!(0, import_llm_agent.isLlmAgent)(agent)) {
141
+ return;
142
+ }
143
+ const codeExecutor = agent.codeExecutor;
144
+ if (!codeExecutor || !(0, import_base_code_executor.isBaseCodeExecutor)(codeExecutor)) {
145
+ return;
146
+ }
147
+ if ((0, import_built_in_code_executor.isBuiltInCodeExecutor)(codeExecutor)) {
148
+ codeExecutor.processLlmRequest(llmRequest);
149
+ return;
150
+ }
151
+ if (!codeExecutor.optimizeDataFile) {
152
+ return;
153
+ }
154
+ const codeExecutorContext = new import_code_executor_context.CodeExecutorContext(
155
+ new import_state.State(invocationContext.session.state)
156
+ );
157
+ if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
158
+ return;
159
+ }
160
+ const allInputFiles = extractAndReplaceInlineFiles(
161
+ codeExecutorContext,
162
+ llmRequest
163
+ );
164
+ const processedFileNames = new Set(
165
+ codeExecutorContext.getProcessedFileNames()
166
+ );
167
+ const filesToProcess = allInputFiles.filter(
168
+ (f) => !processedFileNames.has(f.name)
169
+ );
170
+ for (const file of filesToProcess) {
171
+ const codeStr = getDataFilePreprocessingCode(file);
172
+ if (!codeStr) {
173
+ return;
174
+ }
175
+ const codeContent = {
176
+ role: "model",
177
+ parts: [
178
+ { text: `Processing input file: \`${file.name}\`` },
179
+ (0, import_code_execution_utils.buildExecutableCodePart)(codeStr)
180
+ ]
181
+ };
182
+ llmRequest.contents.push((0, import_lodash_es.cloneDeep)(codeContent));
183
+ yield (0, import_event.createEvent)({
184
+ invocationId: invocationContext.invocationId,
185
+ author: agent.name,
186
+ branch: invocationContext.branch,
187
+ content: codeContent
188
+ });
189
+ const executionId = getOrSetExecutionId(
190
+ invocationContext,
191
+ codeExecutorContext
192
+ );
193
+ const codeExecutionResult = await codeExecutor.executeCode({
194
+ invocationContext,
195
+ codeExecutionInput: {
196
+ code: codeStr,
197
+ inputFiles: [file],
198
+ executionId
199
+ }
200
+ });
201
+ codeExecutorContext.updateCodeExecutionResult({
202
+ invocationId: invocationContext.invocationId,
203
+ code: codeStr,
204
+ resultStdout: codeExecutionResult.stdout,
205
+ resultStderr: codeExecutionResult.stderr
206
+ });
207
+ codeExecutorContext.addProcessedFileNames([file.name]);
208
+ const executionResultEvent = await postProcessCodeExecutionResult(
209
+ invocationContext,
210
+ codeExecutorContext,
211
+ codeExecutionResult
212
+ );
213
+ yield executionResultEvent;
214
+ llmRequest.contents.push((0, import_lodash_es.cloneDeep)(executionResultEvent.content));
215
+ }
216
+ }
217
+ async function* runPostProcessor(invocationContext, llmResponse) {
218
+ const agent = invocationContext.agent;
219
+ if (!(0, import_llm_agent.isLlmAgent)(agent)) {
220
+ return;
221
+ }
222
+ const codeExecutor = agent.codeExecutor;
223
+ if (!codeExecutor || !(0, import_base_code_executor.isBaseCodeExecutor)(codeExecutor)) {
224
+ return;
225
+ }
226
+ if (!llmResponse || !llmResponse.content) {
227
+ return;
228
+ }
229
+ if ((0, import_built_in_code_executor.isBuiltInCodeExecutor)(codeExecutor)) {
230
+ return;
231
+ }
232
+ const codeExecutorContext = new import_code_executor_context.CodeExecutorContext(
233
+ new import_state.State(invocationContext.session.state)
234
+ );
235
+ if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
236
+ return;
237
+ }
238
+ const responseContent = llmResponse.content;
239
+ const codeStr = (0, import_code_execution_utils.extractCodeAndTruncateContent)(
240
+ responseContent,
241
+ codeExecutor.codeBlockDelimiters
242
+ );
243
+ if (!codeStr) {
244
+ return;
245
+ }
246
+ yield (0, import_event.createEvent)({
247
+ invocationId: invocationContext.invocationId,
248
+ author: agent.name,
249
+ branch: invocationContext.branch,
250
+ content: responseContent
251
+ });
252
+ const executionId = getOrSetExecutionId(
253
+ invocationContext,
254
+ codeExecutorContext
255
+ );
256
+ const codeExecutionResult = await codeExecutor.executeCode({
257
+ invocationContext,
258
+ codeExecutionInput: {
259
+ code: codeStr,
260
+ inputFiles: codeExecutorContext.getInputFiles(),
261
+ executionId
262
+ }
263
+ });
264
+ codeExecutorContext.updateCodeExecutionResult({
265
+ invocationId: invocationContext.invocationId,
266
+ code: codeStr,
267
+ resultStdout: codeExecutionResult.stdout,
268
+ resultStderr: codeExecutionResult.stderr
269
+ });
270
+ yield await postProcessCodeExecutionResult(
271
+ invocationContext,
272
+ codeExecutorContext,
273
+ codeExecutionResult
274
+ );
275
+ llmResponse.content = void 0;
276
+ }
277
+ function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
278
+ var _a;
279
+ const allInputFiles = codeExecutorContext.getInputFiles();
280
+ const savedFileNames = new Set(allInputFiles.map((f) => f.name));
281
+ for (let i = 0; i < llmRequest.contents.length; i++) {
282
+ const content = llmRequest.contents[i];
283
+ if (content.role !== "user" || !content.parts) {
284
+ continue;
285
+ }
286
+ for (let j = 0; j < content.parts.length; j++) {
287
+ const part = content.parts[j];
288
+ const mimeType = (_a = part.inlineData) == null ? void 0 : _a.mimeType;
289
+ if (!mimeType || !part.inlineData || !DATA_FILE_UTIL_MAP[mimeType]) {
290
+ continue;
291
+ }
292
+ const fileName = `data_${i + 1}_${j + 1}${DATA_FILE_UTIL_MAP[mimeType].extension}`;
293
+ part.text = `
294
+ Available file: \`${fileName}\`
295
+ `;
296
+ const file = {
297
+ name: fileName,
298
+ content: (0, import_env_aware_utils.base64Decode)(part.inlineData.data),
299
+ mimeType
300
+ };
301
+ if (!savedFileNames.has(fileName)) {
302
+ codeExecutorContext.addInputFiles([file]);
303
+ allInputFiles.push(file);
304
+ }
305
+ }
306
+ }
307
+ return allInputFiles;
308
+ }
309
+ function getOrSetExecutionId(invocationContext, codeExecutorContext) {
310
+ var _a;
311
+ const agent = invocationContext.agent;
312
+ if (!(0, import_llm_agent.isLlmAgent)(agent) || !((_a = agent.codeExecutor) == null ? void 0 : _a.stateful)) {
313
+ return void 0;
314
+ }
315
+ let executionId = codeExecutorContext.getExecutionId();
316
+ if (!executionId) {
317
+ executionId = invocationContext.session.id;
318
+ codeExecutorContext.setExecutionId(executionId);
319
+ }
320
+ return executionId;
321
+ }
322
+ async function postProcessCodeExecutionResult(invocationContext, codeExecutorContext, codeExecutionResult) {
323
+ if (!invocationContext.artifactService) {
324
+ throw new Error("Artifact service is not initialized.");
325
+ }
326
+ const resultContent = {
327
+ role: "model",
328
+ parts: [(0, import_code_execution_utils.buildCodeExecutionResultPart)(codeExecutionResult)]
329
+ };
330
+ const eventActions = (0, import_event_actions.createEventActions)({
331
+ stateDelta: codeExecutorContext.getStateDelta()
332
+ });
333
+ if (codeExecutionResult.stderr) {
334
+ codeExecutorContext.incrementErrorCount(invocationContext.invocationId);
335
+ } else {
336
+ codeExecutorContext.resetErrorCount(invocationContext.invocationId);
337
+ }
338
+ for (const outputFile of codeExecutionResult.outputFiles) {
339
+ const version = await invocationContext.artifactService.saveArtifact({
340
+ appName: invocationContext.appName || "",
341
+ userId: invocationContext.userId || "",
342
+ sessionId: invocationContext.session.id,
343
+ filename: outputFile.name,
344
+ artifact: {
345
+ inlineData: { data: outputFile.content, mimeType: outputFile.mimeType }
346
+ }
347
+ });
348
+ eventActions.artifactDelta[outputFile.name] = version;
349
+ }
350
+ return (0, import_event.createEvent)({
351
+ invocationId: invocationContext.invocationId,
352
+ author: invocationContext.agent.name,
353
+ branch: invocationContext.branch,
354
+ content: resultContent,
355
+ actions: eventActions
356
+ });
357
+ }
358
+ function getDataFilePreprocessingCode(file) {
359
+ function getNormalizedFileName(fileName) {
360
+ const [varName2] = fileName.split(".");
361
+ let normalizedName = varName2.replace(/[^a-zA-Z0-9_]/g, "_");
362
+ if (/^\d/.test(normalizedName)) {
363
+ normalizedName = "_" + normalizedName;
364
+ }
365
+ return normalizedName;
366
+ }
367
+ if (!DATA_FILE_UTIL_MAP[file.mimeType]) {
368
+ return void 0;
369
+ }
370
+ const varName = getNormalizedFileName(file.name);
371
+ const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace("{filename}", file.name);
372
+ return `
373
+ ${DATA_FILE_HELPER_LIB}
374
+
375
+ # Load the dataframe.
376
+ ${varName} = ${loaderCode}
377
+
378
+ # Use \`explore_df\` to guide my analysis.
379
+ explore_df(${varName})
380
+ `;
381
+ }
382
+ const CODE_EXECUTION_REQUEST_PROCESSOR = new CodeExecutionRequestProcessor();
383
+ // Annotate the CommonJS export names for ESM import in node:
384
+ 0 && (module.exports = {
385
+ CODE_EXECUTION_REQUEST_PROCESSOR,
386
+ CodeExecutionRequestProcessor,
387
+ CodeExecutionResponseProcessor,
388
+ responseProcessor
389
+ });
@@ -0,0 +1,66 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ "use strict";
8
+ var __defProp = Object.defineProperty;
9
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
+ var __getOwnPropNames = Object.getOwnPropertyNames;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var content_request_processor_exports = {};
26
+ __export(content_request_processor_exports, {
27
+ CONTENT_REQUEST_PROCESSOR: () => CONTENT_REQUEST_PROCESSOR,
28
+ ContentRequestProcessor: () => ContentRequestProcessor
29
+ });
30
+ module.exports = __toCommonJS(content_request_processor_exports);
31
+ var import_content_processor_utils = require("../content_processor_utils.js");
32
+ var import_llm_agent = require("../llm_agent.js");
33
+ /**
34
+ * @license
35
+ * Copyright 2025 Google LLC
36
+ * SPDX-License-Identifier: Apache-2.0
37
+ */
38
+ class ContentRequestProcessor {
39
+ // eslint-disable-next-line require-yield
40
+ async *runAsync(invocationContext, llmRequest) {
41
+ const agent = invocationContext.agent;
42
+ if (!agent || !(0, import_llm_agent.isLlmAgent)(agent)) {
43
+ return;
44
+ }
45
+ if (agent.includeContents === "default") {
46
+ llmRequest.contents = (0, import_content_processor_utils.getContents)(
47
+ invocationContext.session.events,
48
+ agent.name,
49
+ invocationContext.branch
50
+ );
51
+ } else {
52
+ llmRequest.contents = (0, import_content_processor_utils.getCurrentTurnContents)(
53
+ invocationContext.session.events,
54
+ agent.name,
55
+ invocationContext.branch
56
+ );
57
+ }
58
+ return;
59
+ }
60
+ }
61
+ const CONTENT_REQUEST_PROCESSOR = new ContentRequestProcessor();
62
+ // Annotate the CommonJS export names for ESM import in node:
63
+ 0 && (module.exports = {
64
+ CONTENT_REQUEST_PROCESSOR,
65
+ ContentRequestProcessor
66
+ });