@google/adk 1.4.0 → 1.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 (122) hide show
  1. package/dist/cjs/a2a/agent_to_a2a.js +18 -2
  2. package/dist/cjs/a2a/event_converter_utils.js +8 -4
  3. package/dist/cjs/a2a/metadata_converter_utils.js +1 -1
  4. package/dist/cjs/agents/base_agent.js +39 -0
  5. package/dist/cjs/agents/functions.js +6 -21
  6. package/dist/cjs/agents/llm_agent.js +2 -1
  7. package/dist/cjs/agents/loop_agent.js +15 -3
  8. package/dist/cjs/agents/processors/content_processor_utils.js +2 -1
  9. package/dist/cjs/agents/routed_agent.js +31 -0
  10. package/dist/cjs/agents/run_config.js +3 -2
  11. package/dist/cjs/artifacts/in_memory_artifact_service.js +7 -0
  12. package/dist/cjs/common.js +29 -0
  13. package/dist/cjs/context/anchored_context_compactor.js +127 -0
  14. package/dist/cjs/context/compaction_utils.js +52 -0
  15. package/dist/cjs/context/token_based_context_compactor.js +11 -24
  16. package/dist/cjs/events/compacted_event.js +9 -3
  17. package/dist/cjs/events/event.js +28 -0
  18. package/dist/cjs/index.js +4 -0
  19. package/dist/cjs/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
  20. package/dist/cjs/plugins/global_instruction_plugin.js +97 -0
  21. package/dist/cjs/runner/runner.js +51 -12
  22. package/dist/cjs/tools/enterprise_web_search_tool.js +81 -0
  23. package/dist/cjs/tools/example_tool.js +68 -0
  24. package/dist/cjs/tools/load_web_page.js +250 -0
  25. package/dist/cjs/tools/mcp/load_mcp_resource_tool.js +140 -0
  26. package/dist/cjs/tools/mcp/mcp_toolset.js +63 -0
  27. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +13 -10
  28. package/dist/cjs/tools/skill/run_skill_inline_script_tool.js +54 -4
  29. package/dist/cjs/tools/skill/skill_toolset.js +4 -2
  30. package/dist/cjs/utils/branch_trie.js +41 -0
  31. package/dist/cjs/utils/streaming_utils.js +5 -5
  32. package/dist/cjs/version.js +1 -1
  33. package/dist/esm/a2a/agent_to_a2a.js +18 -2
  34. package/dist/esm/a2a/event_converter_utils.js +8 -4
  35. package/dist/esm/a2a/metadata_converter_utils.js +1 -1
  36. package/dist/esm/agents/base_agent.js +39 -0
  37. package/dist/esm/agents/functions.js +7 -17
  38. package/dist/esm/agents/llm_agent.js +4 -3
  39. package/dist/esm/agents/loop_agent.js +15 -3
  40. package/dist/esm/agents/processors/content_processor_utils.js +2 -1
  41. package/dist/esm/agents/routed_agent.js +31 -0
  42. package/dist/esm/agents/run_config.js +3 -2
  43. package/dist/esm/artifacts/in_memory_artifact_service.js +7 -0
  44. package/dist/esm/common.js +25 -1
  45. package/dist/esm/context/anchored_context_compactor.js +99 -0
  46. package/dist/esm/context/compaction_utils.js +27 -0
  47. package/dist/esm/context/token_based_context_compactor.js +11 -24
  48. package/dist/esm/events/compacted_event.js +7 -2
  49. package/dist/esm/events/event.js +24 -0
  50. package/dist/esm/index.js +6 -1
  51. package/dist/esm/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
  52. package/dist/esm/plugins/global_instruction_plugin.js +69 -0
  53. package/dist/esm/runner/runner.js +51 -12
  54. package/dist/esm/tools/enterprise_web_search_tool.js +55 -0
  55. package/dist/esm/tools/example_tool.js +42 -0
  56. package/dist/esm/tools/load_web_page.js +221 -0
  57. package/dist/esm/tools/mcp/load_mcp_resource_tool.js +114 -0
  58. package/dist/esm/tools/mcp/mcp_toolset.js +63 -0
  59. package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +13 -10
  60. package/dist/esm/tools/skill/run_skill_inline_script_tool.js +53 -4
  61. package/dist/esm/tools/skill/skill_toolset.js +4 -2
  62. package/dist/esm/utils/branch_trie.js +13 -0
  63. package/dist/esm/utils/streaming_utils.js +1 -1
  64. package/dist/esm/version.js +1 -1
  65. package/dist/types/a2a/a2a_remote_agent.d.ts +5 -1
  66. package/dist/types/a2a/agent_to_a2a.d.ts +38 -0
  67. package/dist/types/agents/base_agent.d.ts +29 -1
  68. package/dist/types/agents/functions.d.ts +1 -9
  69. package/dist/types/agents/llm_agent.d.ts +5 -1
  70. package/dist/types/agents/loop_agent.d.ts +2 -2
  71. package/dist/types/agents/routed_agent.d.ts +24 -1
  72. package/dist/types/agents/run_config.d.ts +1 -1
  73. package/dist/types/common.d.ts +9 -2
  74. package/dist/types/context/anchored_context_compactor.d.ts +35 -0
  75. package/dist/types/context/compaction_utils.d.ts +15 -0
  76. package/dist/types/events/compacted_event.d.ts +10 -0
  77. package/dist/types/events/event.d.ts +13 -0
  78. package/dist/types/index.d.ts +3 -2
  79. package/dist/types/plugins/global_instruction_plugin.d.ts +38 -0
  80. package/dist/types/runner/runner.d.ts +1 -1
  81. package/dist/types/tools/enterprise_web_search_tool.d.ts +26 -0
  82. package/dist/types/tools/example_tool.d.ts +21 -0
  83. package/dist/types/tools/load_web_page.d.ts +33 -0
  84. package/dist/types/tools/mcp/load_mcp_resource_tool.d.ts +26 -0
  85. package/dist/types/tools/mcp/mcp_toolset.d.ts +27 -0
  86. package/dist/types/tools/skill/run_skill_inline_script_tool.d.ts +25 -0
  87. package/dist/types/tools/skill/skill_toolset.d.ts +9 -0
  88. package/dist/types/utils/branch_trie.d.ts +14 -0
  89. package/dist/types/version.d.ts +1 -1
  90. package/dist/web/a2a/agent_to_a2a.js +18 -2
  91. package/dist/web/a2a/event_converter_utils.js +8 -4
  92. package/dist/web/a2a/metadata_converter_utils.js +1 -1
  93. package/dist/web/agents/base_agent.js +39 -0
  94. package/dist/web/agents/functions.js +7 -17
  95. package/dist/web/agents/llm_agent.js +4 -3
  96. package/dist/web/agents/loop_agent.js +27 -3
  97. package/dist/web/agents/processors/content_processor_utils.js +2 -1
  98. package/dist/web/agents/routed_agent.js +31 -0
  99. package/dist/web/agents/run_config.js +8 -3
  100. package/dist/web/artifacts/in_memory_artifact_service.js +7 -0
  101. package/dist/web/common.js +25 -1
  102. package/dist/web/context/anchored_context_compactor.js +117 -0
  103. package/dist/web/context/compaction_utils.js +27 -0
  104. package/dist/web/context/token_based_context_compactor.js +11 -24
  105. package/dist/web/events/compacted_event.js +7 -2
  106. package/dist/web/events/event.js +24 -0
  107. package/dist/web/index.js +6 -1
  108. package/dist/web/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
  109. package/dist/web/plugins/global_instruction_plugin.js +67 -0
  110. package/dist/web/runner/runner.js +48 -11
  111. package/dist/web/tools/enterprise_web_search_tool.js +55 -0
  112. package/dist/web/tools/example_tool.js +42 -0
  113. package/dist/web/tools/load_web_page.js +221 -0
  114. package/dist/web/tools/mcp/load_mcp_resource_tool.js +107 -0
  115. package/dist/web/tools/mcp/mcp_toolset.js +63 -0
  116. package/dist/web/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +13 -10
  117. package/dist/web/tools/skill/run_skill_inline_script_tool.js +51 -4
  118. package/dist/web/tools/skill/skill_toolset.js +4 -2
  119. package/dist/web/utils/branch_trie.js +13 -0
  120. package/dist/web/utils/streaming_utils.js +1 -1
  121. package/dist/web/version.js +1 -1
  122. package/package.json +1 -1
@@ -0,0 +1,52 @@
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 compaction_utils_exports = {};
26
+ __export(compaction_utils_exports, {
27
+ calculateRetainStartIndex: () => calculateRetainStartIndex
28
+ });
29
+ module.exports = __toCommonJS(compaction_utils_exports);
30
+ var import_event = require("../events/event.js");
31
+ /**
32
+ * @license
33
+ * Copyright 2026 Google LLC
34
+ * SPDX-License-Identifier: Apache-2.0
35
+ */
36
+ function calculateRetainStartIndex(rawEvents, eventRetentionSize) {
37
+ let retainStartIndex = Math.max(0, rawEvents.length - eventRetentionSize);
38
+ while (retainStartIndex > 0) {
39
+ const eventToRetain = rawEvents[retainStartIndex];
40
+ const previousEvent = rawEvents[retainStartIndex - 1];
41
+ if ((0, import_event.getFunctionResponses)(eventToRetain).length > 0 && (0, import_event.getFunctionCalls)(previousEvent).length > 0) {
42
+ retainStartIndex--;
43
+ } else {
44
+ break;
45
+ }
46
+ }
47
+ return retainStartIndex;
48
+ }
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ calculateRetainStartIndex
52
+ });
@@ -29,6 +29,7 @@ __export(token_based_context_compactor_exports, {
29
29
  module.exports = __toCommonJS(token_based_context_compactor_exports);
30
30
  var import_content_processor_utils = require("../agents/processors/content_processor_utils.js");
31
31
  var import_compacted_event = require("../events/compacted_event.js");
32
+ var import_compaction_utils = require("./compaction_utils.js");
32
33
  /**
33
34
  * @license
34
35
  * Copyright 2026 Google LLC
@@ -66,6 +67,13 @@ class TokenBasedContextCompactor {
66
67
  if (rawEvents.length <= this.eventRetentionSize) {
67
68
  return false;
68
69
  }
70
+ const retainStartIndex = (0, import_compaction_utils.calculateRetainStartIndex)(
71
+ rawEvents,
72
+ this.eventRetentionSize
73
+ );
74
+ if (retainStartIndex === 0) {
75
+ return false;
76
+ }
69
77
  const promptTokenCount = latestPromptTokenCount(
70
78
  activeEvents,
71
79
  invocationContext
@@ -82,19 +90,10 @@ class TokenBasedContextCompactor {
82
90
  if (rawEvents.length <= this.eventRetentionSize) {
83
91
  return;
84
92
  }
85
- let retainStartIndex = Math.max(
86
- 0,
87
- rawEvents.length - this.eventRetentionSize
93
+ const retainStartIndex = (0, import_compaction_utils.calculateRetainStartIndex)(
94
+ rawEvents,
95
+ this.eventRetentionSize
88
96
  );
89
- while (retainStartIndex > 0) {
90
- const eventToRetain = rawEvents[retainStartIndex];
91
- const previousEvent = rawEvents[retainStartIndex - 1];
92
- if (hasFunctionResponse(eventToRetain) && hasFunctionCall(previousEvent)) {
93
- retainStartIndex--;
94
- } else {
95
- break;
96
- }
97
- }
98
97
  if (retainStartIndex === 0) {
99
98
  return;
100
99
  }
@@ -149,18 +148,6 @@ function estimatePromptTokenCount(activeEvents, invocationContext) {
149
148
  }
150
149
  return Math.ceil(totalChars / CHARS_PER_TOKEN);
151
150
  }
152
- function hasFunctionCall(event) {
153
- var _a, _b;
154
- return !!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.some(
155
- (part) => part.functionCall !== void 0
156
- ));
157
- }
158
- function hasFunctionResponse(event) {
159
- var _a, _b;
160
- return !!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.some(
161
- (part) => part.functionResponse !== void 0
162
- ));
163
- }
164
151
  // Annotate the CommonJS export names for ESM import in node:
165
152
  0 && (module.exports = {
166
153
  TokenBasedContextCompactor
@@ -25,7 +25,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
25
25
  var compacted_event_exports = {};
26
26
  __export(compacted_event_exports, {
27
27
  createCompactedEvent: () => createCompactedEvent,
28
- isCompactedEvent: () => isCompactedEvent
28
+ isCompactedEvent: () => isCompactedEvent,
29
+ isScratchpadEvent: () => isScratchpadEvent
29
30
  });
30
31
  module.exports = __toCommonJS(compacted_event_exports);
31
32
  var import_event = require("./event.js");
@@ -37,17 +38,22 @@ var import_event = require("./event.js");
37
38
  function isCompactedEvent(event) {
38
39
  return "isCompacted" in event && event.isCompacted === true;
39
40
  }
41
+ function isScratchpadEvent(event) {
42
+ return isCompactedEvent(event) && event.isScratchpad === true;
43
+ }
40
44
  function createCompactedEvent(params = {}) {
41
45
  return {
42
46
  ...(0, import_event.createEvent)(params),
43
47
  isCompacted: params.isCompacted || true,
44
48
  startTime: params.startTime,
45
49
  endTime: params.endTime,
46
- compactedContent: params.compactedContent
50
+ compactedContent: params.compactedContent,
51
+ isScratchpad: params.isScratchpad
47
52
  };
48
53
  }
49
54
  // Annotate the CommonJS export names for ESM import in node:
50
55
  0 && (module.exports = {
51
56
  createCompactedEvent,
52
- isCompactedEvent
57
+ isCompactedEvent,
58
+ isScratchpadEvent
53
59
  });
@@ -24,19 +24,24 @@ var __copyProps = (to, from, except, desc) => {
24
24
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
25
  var event_exports = {};
26
26
  __export(event_exports, {
27
+ AF_FUNCTION_CALL_ID_PREFIX: () => AF_FUNCTION_CALL_ID_PREFIX,
27
28
  createEvent: () => createEvent,
28
29
  createNewEventId: () => createNewEventId,
30
+ generateClientFunctionCallId: () => generateClientFunctionCallId,
31
+ getEventTokens: () => getEventTokens,
29
32
  getFunctionCalls: () => getFunctionCalls,
30
33
  getFunctionResponses: () => getFunctionResponses,
31
34
  hasThoughts: () => hasThoughts,
32
35
  hasTrailingCodeExecutionResult: () => hasTrailingCodeExecutionResult,
33
36
  isFinalResponse: () => isFinalResponse,
37
+ populateClientFunctionCallId: () => populateClientFunctionCallId,
34
38
  pruneThoughts: () => pruneThoughts,
35
39
  stringifyContent: () => stringifyContent,
36
40
  transformToCamelCaseEvent: () => transformToCamelCaseEvent,
37
41
  transformToSnakeCaseEvent: () => transformToSnakeCaseEvent
38
42
  });
39
43
  module.exports = __toCommonJS(event_exports);
44
+ var import_env_aware_utils = require("../utils/env_aware_utils.js");
40
45
  var import_object_notation_utils = require("../utils/object_notation_utils.js");
41
46
  var import_event_actions = require("./event_actions.js");
42
47
  /**
@@ -73,6 +78,17 @@ function getFunctionCalls(event) {
73
78
  }
74
79
  return funcCalls;
75
80
  }
81
+ const AF_FUNCTION_CALL_ID_PREFIX = "adk-";
82
+ function generateClientFunctionCallId() {
83
+ return `${AF_FUNCTION_CALL_ID_PREFIX}${(0, import_env_aware_utils.randomUUID)()}`;
84
+ }
85
+ function populateClientFunctionCallId(modelResponseEvent) {
86
+ for (const functionCall of getFunctionCalls(modelResponseEvent)) {
87
+ if (!functionCall.id) {
88
+ functionCall.id = generateClientFunctionCallId();
89
+ }
90
+ }
91
+ }
76
92
  function getFunctionResponses(event) {
77
93
  const funcResponses = [];
78
94
  if (event.content && event.content.parts) {
@@ -102,6 +118,14 @@ function stringifyContent(event) {
102
118
  return (_a2 = part.text) != null ? _a2 : "";
103
119
  }).join("");
104
120
  }
121
+ function getEventTokens(event) {
122
+ var _a;
123
+ if (((_a = event.usageMetadata) == null ? void 0 : _a.promptTokenCount) !== void 0) {
124
+ return event.usageMetadata.promptTokenCount;
125
+ }
126
+ const contentStr = stringifyContent(event);
127
+ return Math.ceil(contentStr.length / 4);
128
+ }
105
129
  function hasThoughts(event) {
106
130
  var _a, _b;
107
131
  return !!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.some((part) => part.thought === true));
@@ -158,13 +182,17 @@ function transformToSnakeCaseEvent(event) {
158
182
  }
159
183
  // Annotate the CommonJS export names for ESM import in node:
160
184
  0 && (module.exports = {
185
+ AF_FUNCTION_CALL_ID_PREFIX,
161
186
  createEvent,
162
187
  createNewEventId,
188
+ generateClientFunctionCallId,
189
+ getEventTokens,
163
190
  getFunctionCalls,
164
191
  getFunctionResponses,
165
192
  hasThoughts,
166
193
  hasTrailingCodeExecutionResult,
167
194
  isFinalResponse,
195
+ populateClientFunctionCallId,
168
196
  pruneThoughts,
169
197
  stringifyContent,
170
198
  transformToCamelCaseEvent,
package/dist/cjs/index.js CHANGED
@@ -34,6 +34,7 @@ __export(index_exports, {
34
34
  GcsArtifactService: () => import_gcs_artifact_service.GcsArtifactService,
35
35
  InvocationContext: () => import_invocation_context.InvocationContext,
36
36
  RemoteA2AAgent: () => import_a2a_remote_agent.RemoteA2AAgent,
37
+ RunSkillInlineScriptErrorCode: () => import_run_skill_inline_script_tool.RunSkillInlineScriptErrorCode,
37
38
  RunSkillInlineScriptTool: () => import_run_skill_inline_script_tool.RunSkillInlineScriptTool,
38
39
  RunSkillScriptTool: () => import_run_skill_script_tool.RunSkillScriptTool,
39
40
  UnsafeLocalCodeExecutor: () => import_unsafe_local_code_executor.UnsafeLocalCodeExecutor,
@@ -68,6 +69,7 @@ var import_run_skill_script_tool = require("./tools/skill/run_skill_script_tool.
68
69
  __reExport(index_exports, require("./integrations/agent_registry/agent_registry.js"), module.exports);
69
70
  __reExport(index_exports, require("./telemetry/google_cloud.js"), module.exports);
70
71
  __reExport(index_exports, require("./telemetry/setup.js"), module.exports);
72
+ __reExport(index_exports, require("./tools/mcp/load_mcp_resource_tool.js"), module.exports);
71
73
  __reExport(index_exports, require("./tools/mcp/mcp_session_manager.js"), module.exports);
72
74
  __reExport(index_exports, require("./tools/mcp/mcp_tool.js"), module.exports);
73
75
  __reExport(index_exports, require("./tools/mcp/mcp_toolset.js"), module.exports);
@@ -87,6 +89,7 @@ __reExport(index_exports, require("./tools/mcp/mcp_toolset.js"), module.exports)
87
89
  GcsArtifactService,
88
90
  InvocationContext,
89
91
  RemoteA2AAgent,
92
+ RunSkillInlineScriptErrorCode,
90
93
  RunSkillInlineScriptTool,
91
94
  RunSkillScriptTool,
92
95
  UnsafeLocalCodeExecutor,
@@ -102,6 +105,7 @@ __reExport(index_exports, require("./tools/mcp/mcp_toolset.js"), module.exports)
102
105
  ...require("./integrations/agent_registry/agent_registry.js"),
103
106
  ...require("./telemetry/google_cloud.js"),
104
107
  ...require("./telemetry/setup.js"),
108
+ ...require("./tools/mcp/load_mcp_resource_tool.js"),
105
109
  ...require("./tools/mcp/mcp_session_manager.js"),
106
110
  ...require("./tools/mcp/mcp_tool.js"),
107
111
  ...require("./tools/mcp/mcp_toolset.js")
@@ -30,6 +30,7 @@ module.exports = __toCommonJS(agent_registry_mcp_toolset_exports);
30
30
  var import_base_toolset = require("../../tools/base_toolset.js");
31
31
  var import_mcp_session_manager = require("../../tools/mcp/mcp_session_manager.js");
32
32
  var import_mcp_tool = require("../../tools/mcp/mcp_tool.js");
33
+ var import_logger = require("../../utils/logger.js");
33
34
  var import_types2 = require("./types.js");
34
35
  /**
35
36
  * @license
@@ -90,7 +91,14 @@ class AgentRegistrySingleMCPToolset extends import_base_toolset.BaseToolset {
90
91
  };
91
92
  const sessionManager = new import_mcp_session_manager.MCPSessionManager(connectionParamsCopy);
92
93
  const session = await sessionManager.createSession();
93
- const listResult = await session.listTools();
94
+ let listResult;
95
+ try {
96
+ listResult = await session.listTools();
97
+ } finally {
98
+ await sessionManager.closeSession(session).catch((e) => {
99
+ import_logger.logger.warn("Failed to close MCP discovery session", e);
100
+ });
101
+ }
94
102
  const tools = listResult.tools.map((tool) => {
95
103
  const prefixedName = this.prefix ? `${this.prefix}_${tool.name}` : tool.name;
96
104
  const mcpTool = new import_mcp_tool.MCPTool(
@@ -0,0 +1,97 @@
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 global_instruction_plugin_exports = {};
26
+ __export(global_instruction_plugin_exports, {
27
+ GlobalInstructionPlugin: () => GlobalInstructionPlugin
28
+ });
29
+ module.exports = __toCommonJS(global_instruction_plugin_exports);
30
+ var import_instructions = require("../agents/instructions.js");
31
+ var import_readonly_context = require("../agents/readonly_context.js");
32
+ var import_base_plugin = require("./base_plugin.js");
33
+ /**
34
+ * @license
35
+ * Copyright 2026 Google LLC
36
+ * SPDX-License-Identifier: Apache-2.0
37
+ */
38
+ class GlobalInstructionPlugin extends import_base_plugin.BasePlugin {
39
+ /**
40
+ * Initializes the GlobalInstructionPlugin.
41
+ *
42
+ * @param globalInstruction The instruction to apply globally. Can be a string
43
+ * or an InstructionProvider function that takes ReadonlyContext and
44
+ * returns a string (sync or async).
45
+ * @param name The name of the plugin (defaults to 'global_instruction').
46
+ */
47
+ constructor(globalInstruction, name = "global_instruction") {
48
+ super(name);
49
+ this.globalInstruction = globalInstruction;
50
+ }
51
+ async beforeModelCallback(params) {
52
+ if (!this.globalInstruction) {
53
+ return;
54
+ }
55
+ const readonlyContext = new import_readonly_context.ReadonlyContext(
56
+ params.callbackContext.invocationContext
57
+ );
58
+ const finalGlobalInstruction = await this.resolveGlobalInstruction(readonlyContext);
59
+ if (!finalGlobalInstruction) {
60
+ return;
61
+ }
62
+ if (!params.llmRequest.config) {
63
+ params.llmRequest.config = {};
64
+ }
65
+ const existingInstruction = params.llmRequest.config.systemInstruction;
66
+ if (!existingInstruction || Array.isArray(existingInstruction) && existingInstruction.length === 0) {
67
+ params.llmRequest.config.systemInstruction = finalGlobalInstruction;
68
+ return;
69
+ }
70
+ if (typeof existingInstruction === "string") {
71
+ params.llmRequest.config.systemInstruction = `${finalGlobalInstruction}
72
+
73
+ ${existingInstruction}`;
74
+ } else if (Array.isArray(existingInstruction)) {
75
+ params.llmRequest.config.systemInstruction = [
76
+ finalGlobalInstruction,
77
+ ...existingInstruction
78
+ ];
79
+ } else {
80
+ params.llmRequest.config.systemInstruction = [
81
+ finalGlobalInstruction,
82
+ existingInstruction
83
+ ];
84
+ }
85
+ return;
86
+ }
87
+ async resolveGlobalInstruction(readonlyContext) {
88
+ if (typeof this.globalInstruction === "string") {
89
+ return await (0, import_instructions.injectSessionState)(this.globalInstruction, readonlyContext);
90
+ }
91
+ return await this.globalInstruction(readonlyContext);
92
+ }
93
+ }
94
+ // Annotate the CommonJS export names for ESM import in node:
95
+ 0 && (module.exports = {
96
+ GlobalInstructionPlugin
97
+ });
@@ -203,7 +203,7 @@ class Runner {
203
203
  throw new Error("No parts in the newMessage.");
204
204
  }
205
205
  if (runConfig.saveInputBlobsAsArtifacts) {
206
- await this.saveArtifacts(
206
+ newMessage = await this.saveArtifacts(
207
207
  invocationContext.invocationId,
208
208
  session.userId,
209
209
  session.id,
@@ -291,7 +291,7 @@ class Runner {
291
291
  }
292
292
  /**
293
293
  * Saves artifacts from the message parts and replaces the inline data with
294
- * a file name placeholder.
294
+ * a file name placeholder and optional file reference.
295
295
  *
296
296
  * @param invocationId The current invocation ID.
297
297
  * @param userId The user ID of the session.
@@ -301,28 +301,67 @@ class Runner {
301
301
  async saveArtifacts(invocationId, userId, sessionId, message) {
302
302
  var _a2;
303
303
  if (!this.artifactService || !((_a2 = message.parts) == null ? void 0 : _a2.length)) {
304
- return;
304
+ return message;
305
305
  }
306
306
  const sessionKey = {
307
307
  appName: this.appName,
308
308
  userId,
309
309
  sessionId
310
310
  };
311
+ const newParts = [];
312
+ let modified = false;
311
313
  for (let i = 0; i < message.parts.length; i++) {
312
314
  const part = message.parts[i];
313
315
  if (!part.inlineData) {
316
+ newParts.push(part);
314
317
  continue;
315
318
  }
316
- const fileName = `artifact_${invocationId}_${i}`;
317
- await this.artifactService.saveArtifact({
318
- ...sessionKey,
319
- filename: fileName,
320
- artifact: part
321
- });
322
- message.parts[i] = (0, import_genai.createPartFromText)(
323
- `Uploaded file: ${fileName}. It is saved into artifacts`
324
- );
319
+ try {
320
+ const inlineData = part.inlineData;
321
+ const fileName = inlineData.displayName || `artifact_${invocationId}_${i}`;
322
+ const version = await this.artifactService.saveArtifact({
323
+ ...sessionKey,
324
+ filename: fileName,
325
+ artifact: part
326
+ });
327
+ newParts.push((0, import_genai.createPartFromText)(`[Uploaded Artifact: "${fileName}"]`));
328
+ try {
329
+ const artifactVersion = await this.artifactService.getArtifactVersion(
330
+ {
331
+ ...sessionKey,
332
+ filename: fileName,
333
+ version
334
+ }
335
+ );
336
+ if ((artifactVersion == null ? void 0 : artifactVersion.canonicalUri) && /^(gs|https?):/i.test(artifactVersion.canonicalUri)) {
337
+ newParts.push({
338
+ fileData: {
339
+ fileUri: artifactVersion.canonicalUri,
340
+ mimeType: artifactVersion.mimeType || inlineData.mimeType || "",
341
+ displayName: fileName
342
+ }
343
+ });
344
+ }
345
+ } catch (error) {
346
+ import_logger.logger.warn(
347
+ `Failed to resolve artifact version for ${fileName}:`,
348
+ error
349
+ );
350
+ }
351
+ modified = true;
352
+ import_logger.logger.info(`Successfully saved artifact: ${fileName}`);
353
+ } catch (error) {
354
+ import_logger.logger.error(`Failed to save artifact for part ${i}:`, error);
355
+ newParts.push(part);
356
+ }
357
+ }
358
+ if (!modified) {
359
+ return message;
325
360
  }
361
+ return {
362
+ ...message,
363
+ parts: newParts
364
+ };
326
365
  }
327
366
  /**
328
367
  * Determines the next agent to run to continue the session. This is primarily
@@ -0,0 +1,81 @@
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 enterprise_web_search_tool_exports = {};
26
+ __export(enterprise_web_search_tool_exports, {
27
+ ENTERPRISE_WEB_SEARCH: () => ENTERPRISE_WEB_SEARCH,
28
+ EnterpriseWebSearchTool: () => EnterpriseWebSearchTool,
29
+ applyEnterpriseWebSearch: () => applyEnterpriseWebSearch
30
+ });
31
+ module.exports = __toCommonJS(enterprise_web_search_tool_exports);
32
+ var import_model_name = require("../utils/model_name.js");
33
+ var import_base_tool = require("./base_tool.js");
34
+ /**
35
+ * @license
36
+ * Copyright 2026 Google LLC
37
+ * SPDX-License-Identifier: Apache-2.0
38
+ */
39
+ function applyEnterpriseWebSearch(llmRequest) {
40
+ if (!llmRequest.model) {
41
+ return;
42
+ }
43
+ const modelCheckDisabled = (0, import_model_name.isGeminiModelIdCheckDisabled)();
44
+ llmRequest.config = llmRequest.config || {};
45
+ llmRequest.config.tools = llmRequest.config.tools || [];
46
+ if ((0, import_model_name.isGeminiModel)(llmRequest.model) || modelCheckDisabled) {
47
+ if ((0, import_model_name.isGemini1Model)(llmRequest.model) && llmRequest.config.tools.length > 0) {
48
+ throw new Error(
49
+ "Enterprise Web Search tool cannot be used with other tools in Gemini 1.x."
50
+ );
51
+ }
52
+ llmRequest.config.tools.push({ enterpriseWebSearch: {} });
53
+ return;
54
+ }
55
+ throw new Error(
56
+ `Enterprise Web Search tool is not supported for model ${llmRequest.model}`
57
+ );
58
+ }
59
+ class EnterpriseWebSearchTool extends import_base_tool.BaseTool {
60
+ constructor() {
61
+ super({
62
+ name: "enterprise_web_search",
63
+ description: "Enterprise Web Search Tool"
64
+ });
65
+ }
66
+ runAsync() {
67
+ return Promise.resolve();
68
+ }
69
+ async processLlmRequest({
70
+ llmRequest
71
+ }) {
72
+ applyEnterpriseWebSearch(llmRequest);
73
+ }
74
+ }
75
+ const ENTERPRISE_WEB_SEARCH = new EnterpriseWebSearchTool();
76
+ // Annotate the CommonJS export names for ESM import in node:
77
+ 0 && (module.exports = {
78
+ ENTERPRISE_WEB_SEARCH,
79
+ EnterpriseWebSearchTool,
80
+ applyEnterpriseWebSearch
81
+ });
@@ -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 example_tool_exports = {};
26
+ __export(example_tool_exports, {
27
+ ExampleTool: () => ExampleTool
28
+ });
29
+ module.exports = __toCommonJS(example_tool_exports);
30
+ var import_example_util = require("../examples/example_util.js");
31
+ var import_llm_request = require("../models/llm_request.js");
32
+ var import_base_tool = require("./base_tool.js");
33
+ /**
34
+ * @license
35
+ * Copyright 2026 Google LLC
36
+ * SPDX-License-Identifier: Apache-2.0
37
+ */
38
+ class ExampleTool extends import_base_tool.BaseTool {
39
+ constructor(examples) {
40
+ super({
41
+ // Name and description are not used because this tool only changes
42
+ // llmRequest.
43
+ name: "example_tool",
44
+ description: "example tool"
45
+ });
46
+ this.examples = examples;
47
+ }
48
+ async runAsync(_request) {
49
+ throw new Error("ExampleTool should not be called by model");
50
+ }
51
+ async processLlmRequest({
52
+ toolContext,
53
+ llmRequest
54
+ }) {
55
+ var _a, _b;
56
+ const parts = (_a = toolContext.userContent) == null ? void 0 : _a.parts;
57
+ if (!parts || !((_b = parts[0]) == null ? void 0 : _b.text)) {
58
+ return;
59
+ }
60
+ (0, import_llm_request.appendInstructions)(llmRequest, [
61
+ (0, import_example_util.buildExampleSi)(this.examples, parts[0].text, llmRequest.model)
62
+ ]);
63
+ }
64
+ }
65
+ // Annotate the CommonJS export names for ESM import in node:
66
+ 0 && (module.exports = {
67
+ ExampleTool
68
+ });