@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.
- package/dist/cjs/a2a/agent_to_a2a.js +18 -2
- package/dist/cjs/a2a/event_converter_utils.js +8 -4
- package/dist/cjs/a2a/metadata_converter_utils.js +1 -1
- package/dist/cjs/agents/base_agent.js +39 -0
- package/dist/cjs/agents/functions.js +6 -21
- package/dist/cjs/agents/llm_agent.js +2 -1
- package/dist/cjs/agents/loop_agent.js +15 -3
- package/dist/cjs/agents/processors/content_processor_utils.js +2 -1
- package/dist/cjs/agents/routed_agent.js +31 -0
- package/dist/cjs/agents/run_config.js +3 -2
- package/dist/cjs/artifacts/in_memory_artifact_service.js +7 -0
- package/dist/cjs/common.js +29 -0
- package/dist/cjs/context/anchored_context_compactor.js +127 -0
- package/dist/cjs/context/compaction_utils.js +52 -0
- package/dist/cjs/context/token_based_context_compactor.js +11 -24
- package/dist/cjs/events/compacted_event.js +9 -3
- package/dist/cjs/events/event.js +28 -0
- package/dist/cjs/index.js +4 -0
- package/dist/cjs/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
- package/dist/cjs/plugins/global_instruction_plugin.js +97 -0
- package/dist/cjs/runner/runner.js +51 -12
- package/dist/cjs/tools/enterprise_web_search_tool.js +81 -0
- package/dist/cjs/tools/example_tool.js +68 -0
- package/dist/cjs/tools/load_web_page.js +250 -0
- package/dist/cjs/tools/mcp/load_mcp_resource_tool.js +140 -0
- package/dist/cjs/tools/mcp/mcp_toolset.js +63 -0
- package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +13 -10
- package/dist/cjs/tools/skill/run_skill_inline_script_tool.js +54 -4
- package/dist/cjs/tools/skill/skill_toolset.js +4 -2
- package/dist/cjs/utils/branch_trie.js +41 -0
- package/dist/cjs/utils/streaming_utils.js +5 -5
- package/dist/cjs/version.js +1 -1
- package/dist/esm/a2a/agent_to_a2a.js +18 -2
- package/dist/esm/a2a/event_converter_utils.js +8 -4
- package/dist/esm/a2a/metadata_converter_utils.js +1 -1
- package/dist/esm/agents/base_agent.js +39 -0
- package/dist/esm/agents/functions.js +7 -17
- package/dist/esm/agents/llm_agent.js +4 -3
- package/dist/esm/agents/loop_agent.js +15 -3
- package/dist/esm/agents/processors/content_processor_utils.js +2 -1
- package/dist/esm/agents/routed_agent.js +31 -0
- package/dist/esm/agents/run_config.js +3 -2
- package/dist/esm/artifacts/in_memory_artifact_service.js +7 -0
- package/dist/esm/common.js +25 -1
- package/dist/esm/context/anchored_context_compactor.js +99 -0
- package/dist/esm/context/compaction_utils.js +27 -0
- package/dist/esm/context/token_based_context_compactor.js +11 -24
- package/dist/esm/events/compacted_event.js +7 -2
- package/dist/esm/events/event.js +24 -0
- package/dist/esm/index.js +6 -1
- package/dist/esm/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
- package/dist/esm/plugins/global_instruction_plugin.js +69 -0
- package/dist/esm/runner/runner.js +51 -12
- package/dist/esm/tools/enterprise_web_search_tool.js +55 -0
- package/dist/esm/tools/example_tool.js +42 -0
- package/dist/esm/tools/load_web_page.js +221 -0
- package/dist/esm/tools/mcp/load_mcp_resource_tool.js +114 -0
- package/dist/esm/tools/mcp/mcp_toolset.js +63 -0
- package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +13 -10
- package/dist/esm/tools/skill/run_skill_inline_script_tool.js +53 -4
- package/dist/esm/tools/skill/skill_toolset.js +4 -2
- package/dist/esm/utils/branch_trie.js +13 -0
- package/dist/esm/utils/streaming_utils.js +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_remote_agent.d.ts +5 -1
- package/dist/types/a2a/agent_to_a2a.d.ts +38 -0
- package/dist/types/agents/base_agent.d.ts +29 -1
- package/dist/types/agents/functions.d.ts +1 -9
- package/dist/types/agents/llm_agent.d.ts +5 -1
- package/dist/types/agents/loop_agent.d.ts +2 -2
- package/dist/types/agents/routed_agent.d.ts +24 -1
- package/dist/types/agents/run_config.d.ts +1 -1
- package/dist/types/common.d.ts +9 -2
- package/dist/types/context/anchored_context_compactor.d.ts +35 -0
- package/dist/types/context/compaction_utils.d.ts +15 -0
- package/dist/types/events/compacted_event.d.ts +10 -0
- package/dist/types/events/event.d.ts +13 -0
- package/dist/types/index.d.ts +3 -2
- package/dist/types/plugins/global_instruction_plugin.d.ts +38 -0
- package/dist/types/runner/runner.d.ts +1 -1
- package/dist/types/tools/enterprise_web_search_tool.d.ts +26 -0
- package/dist/types/tools/example_tool.d.ts +21 -0
- package/dist/types/tools/load_web_page.d.ts +33 -0
- package/dist/types/tools/mcp/load_mcp_resource_tool.d.ts +26 -0
- package/dist/types/tools/mcp/mcp_toolset.d.ts +27 -0
- package/dist/types/tools/skill/run_skill_inline_script_tool.d.ts +25 -0
- package/dist/types/tools/skill/skill_toolset.d.ts +9 -0
- package/dist/types/utils/branch_trie.d.ts +14 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/a2a/agent_to_a2a.js +18 -2
- package/dist/web/a2a/event_converter_utils.js +8 -4
- package/dist/web/a2a/metadata_converter_utils.js +1 -1
- package/dist/web/agents/base_agent.js +39 -0
- package/dist/web/agents/functions.js +7 -17
- package/dist/web/agents/llm_agent.js +4 -3
- package/dist/web/agents/loop_agent.js +27 -3
- package/dist/web/agents/processors/content_processor_utils.js +2 -1
- package/dist/web/agents/routed_agent.js +31 -0
- package/dist/web/agents/run_config.js +8 -3
- package/dist/web/artifacts/in_memory_artifact_service.js +7 -0
- package/dist/web/common.js +25 -1
- package/dist/web/context/anchored_context_compactor.js +117 -0
- package/dist/web/context/compaction_utils.js +27 -0
- package/dist/web/context/token_based_context_compactor.js +11 -24
- package/dist/web/events/compacted_event.js +7 -2
- package/dist/web/events/event.js +24 -0
- package/dist/web/index.js +6 -1
- package/dist/web/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
- package/dist/web/plugins/global_instruction_plugin.js +67 -0
- package/dist/web/runner/runner.js +48 -11
- package/dist/web/tools/enterprise_web_search_tool.js +55 -0
- package/dist/web/tools/example_tool.js +42 -0
- package/dist/web/tools/load_web_page.js +221 -0
- package/dist/web/tools/mcp/load_mcp_resource_tool.js +107 -0
- package/dist/web/tools/mcp/mcp_toolset.js +63 -0
- package/dist/web/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +13 -10
- package/dist/web/tools/skill/run_skill_inline_script_tool.js +51 -4
- package/dist/web/tools/skill/skill_toolset.js +4 -2
- package/dist/web/utils/branch_trie.js +13 -0
- package/dist/web/utils/streaming_utils.js +1 -1
- package/dist/web/version.js +1 -1
- package/package.json +1 -1
|
@@ -43,6 +43,7 @@ var import_express = require("@a2a-js/sdk/server/express");
|
|
|
43
43
|
var import_express2 = __toESM(require("express"), 1);
|
|
44
44
|
var import_run_config = require("../agents/run_config.js");
|
|
45
45
|
var import_in_memory_session_service = require("../sessions/in_memory_session_service.js");
|
|
46
|
+
var import_logger = require("../utils/logger.js");
|
|
46
47
|
var import_agent_card = require("./agent_card.js");
|
|
47
48
|
var import_agent_executor = require("./agent_executor.js");
|
|
48
49
|
/**
|
|
@@ -50,8 +51,23 @@ var import_agent_executor = require("./agent_executor.js");
|
|
|
50
51
|
* Copyright 2026 Google LLC
|
|
51
52
|
* SPDX-License-Identifier: Apache-2.0
|
|
52
53
|
*/
|
|
54
|
+
function resolveA2aUserBuilder(options) {
|
|
55
|
+
if (options.authentication) {
|
|
56
|
+
return options.authentication;
|
|
57
|
+
}
|
|
58
|
+
if (options.allowUnauthenticated === true) {
|
|
59
|
+
import_logger.logger.warn(
|
|
60
|
+
"SECURITY WARNING: Mounting the A2A server WITHOUT authentication because `allowUnauthenticated: true` was set. The agent and all of its tools are exposed to any network-reachable caller, which can invoke them with arbitrary input and read the output. Do NOT use this outside of local, trusted development."
|
|
61
|
+
);
|
|
62
|
+
return import_express.UserBuilder.noAuthentication;
|
|
63
|
+
}
|
|
64
|
+
throw new Error(
|
|
65
|
+
"toA2a: refusing to mount the A2A server without authentication. The A2A surface lets any network-reachable caller invoke this agent and its tools with arbitrary input and read the output, so it must be authenticated. Provide `authentication` (a UserBuilder that validates the request, e.g. a bearer token or OIDC credential) or, only for local/trusted development, explicitly set `allowUnauthenticated: true`."
|
|
66
|
+
);
|
|
67
|
+
}
|
|
53
68
|
async function toA2a(agent, options = {}) {
|
|
54
69
|
var _a, _b, _c, _d;
|
|
70
|
+
const userBuilder = resolveA2aUserBuilder(options);
|
|
55
71
|
const host = (_a = options.host) != null ? _a : "localhost";
|
|
56
72
|
const port = (_b = options.port) != null ? _b : 8e3;
|
|
57
73
|
const protocol = (_c = options.protocol) != null ? _c : "http";
|
|
@@ -97,14 +113,14 @@ async function toA2a(agent, options = {}) {
|
|
|
97
113
|
`${basePath}/rest`,
|
|
98
114
|
(0, import_express.restHandler)({
|
|
99
115
|
requestHandler,
|
|
100
|
-
userBuilder
|
|
116
|
+
userBuilder
|
|
101
117
|
})
|
|
102
118
|
);
|
|
103
119
|
app.use(
|
|
104
120
|
`${basePath}/jsonrpc`,
|
|
105
121
|
(0, import_express.jsonRpcHandler)({
|
|
106
122
|
requestHandler,
|
|
107
|
-
userBuilder
|
|
123
|
+
userBuilder
|
|
108
124
|
})
|
|
109
125
|
);
|
|
110
126
|
return app;
|
|
@@ -69,13 +69,14 @@ function toAdkEvent(event, invocationId, agentName) {
|
|
|
69
69
|
}
|
|
70
70
|
return void 0;
|
|
71
71
|
}
|
|
72
|
-
function messageToAdkEvent(msg, invocationId, agentName
|
|
72
|
+
function messageToAdkEvent(msg, invocationId, agentName) {
|
|
73
73
|
const parts = (0, import_part_converter_utils.toGenAIParts)(msg.parts);
|
|
74
|
+
const content = parts.length === 0 ? void 0 : msg.role === import_a2a_event.MessageRole.USER ? (0, import_genai.createUserContent)(parts) : (0, import_genai.createModelContent)(parts);
|
|
74
75
|
return {
|
|
75
|
-
...createAdkEventFromMetadata(
|
|
76
|
+
...createAdkEventFromMetadata(msg),
|
|
76
77
|
invocationId,
|
|
77
78
|
author: msg.role === import_a2a_event.MessageRole.USER ? import_a2a_event.MessageRole.USER : agentName,
|
|
78
|
-
content
|
|
79
|
+
content,
|
|
79
80
|
turnComplete: true,
|
|
80
81
|
partial: false
|
|
81
82
|
};
|
|
@@ -121,6 +122,9 @@ function taskStatusUpdateToAdkEvent(a2aEvent, invocationId, agentName) {
|
|
|
121
122
|
return void 0;
|
|
122
123
|
}
|
|
123
124
|
const parts = (0, import_part_converter_utils.toGenAIParts)(msg.parts);
|
|
125
|
+
if (parts.length === 0) {
|
|
126
|
+
return void 0;
|
|
127
|
+
}
|
|
124
128
|
return {
|
|
125
129
|
...createAdkEventFromMetadata(a2aEvent),
|
|
126
130
|
invocationId,
|
|
@@ -151,7 +155,7 @@ function taskToAdkEvent(a2aTask, invocationId, agentName) {
|
|
|
151
155
|
}
|
|
152
156
|
const isTerminal = (0, import_a2a_event.isTerminalTaskStatusUpdateEvent)(a2aTask) || (0, import_a2a_event.isInputRequiredTaskStatusUpdateEvent)(a2aTask);
|
|
153
157
|
const isFailed = (0, import_a2a_event.isFailedTaskStatusUpdateEvent)(a2aTask);
|
|
154
|
-
if (parts.length === 0 && !
|
|
158
|
+
if (parts.length === 0 && !isFailed) {
|
|
155
159
|
return void 0;
|
|
156
160
|
}
|
|
157
161
|
return {
|
|
@@ -88,7 +88,7 @@ function getA2AEventMetadata(adkEvent, {
|
|
|
88
88
|
[A2AMetadataKeys.INVOCATION_ID]: adkEvent.invocationId,
|
|
89
89
|
[A2AMetadataKeys.AUTHOR]: adkEvent.author,
|
|
90
90
|
[A2AMetadataKeys.BRANCH]: adkEvent.branch,
|
|
91
|
-
[A2AMetadataKeys.ERROR_CODE]: adkEvent.
|
|
91
|
+
[A2AMetadataKeys.ERROR_CODE]: adkEvent.errorCode,
|
|
92
92
|
[A2AMetadataKeys.ERROR_MESSAGE]: adkEvent.errorMessage,
|
|
93
93
|
[A2AMetadataKeys.CITATION_METADATA]: adkEvent.citationMetadata,
|
|
94
94
|
[A2AMetadataKeys.GROUNDING_METADATA]: adkEvent.groundingMetadata,
|
|
@@ -51,6 +51,7 @@ class BaseAgent {
|
|
|
51
51
|
* A unique symbol to identify ADK agent classes.
|
|
52
52
|
*/
|
|
53
53
|
this[_a] = true;
|
|
54
|
+
this.config = { ...config };
|
|
54
55
|
this.name = validateAgentName(config.name);
|
|
55
56
|
this.description = config.description;
|
|
56
57
|
this.parentAgent = config.parentAgent;
|
|
@@ -68,6 +69,44 @@ class BaseAgent {
|
|
|
68
69
|
get rootAgent() {
|
|
69
70
|
return getRootAgent(this);
|
|
70
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Creates a copy of this agent with the given config fields overridden.
|
|
74
|
+
*
|
|
75
|
+
* Mirrors adk-python's `BaseAgent.clone(update=...)`. The clone is a detached
|
|
76
|
+
* root: its `parentAgent` is always `undefined`. Sub-agents are recursively
|
|
77
|
+
* cloned (and re-parented to the clone) unless `subAgents` is overridden.
|
|
78
|
+
* Rebuilding via the concrete constructor re-derives all state, so a cloned
|
|
79
|
+
* `LlmAgent` gets a fresh `requestProcessors` array rather than sharing the
|
|
80
|
+
* original's. See google/adk-js#534.
|
|
81
|
+
*
|
|
82
|
+
* @param overrides Config fields to override on the clone. Overriding
|
|
83
|
+
* `parentAgent` is rejected, matching adk-python.
|
|
84
|
+
* @returns A new detached agent instance of the same concrete class.
|
|
85
|
+
*/
|
|
86
|
+
clone(overrides) {
|
|
87
|
+
if (overrides && "parentAgent" in overrides) {
|
|
88
|
+
throw new Error(
|
|
89
|
+
"Cannot update `parentAgent` field in clone. Parent agent is set only when the parent agent is instantiated with the sub-agents."
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const merged = { ...this.config, ...overrides };
|
|
93
|
+
merged.parentAgent = void 0;
|
|
94
|
+
const mergedRecord = merged;
|
|
95
|
+
for (const key of Object.keys(mergedRecord)) {
|
|
96
|
+
if (key === "subAgents" || overrides && key in overrides) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const value = mergedRecord[key];
|
|
100
|
+
if (Array.isArray(value)) {
|
|
101
|
+
mergedRecord[key] = value.slice();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (!overrides || !("subAgents" in overrides)) {
|
|
105
|
+
merged.subAgents = this.subAgents.map((subAgent) => subAgent.clone());
|
|
106
|
+
}
|
|
107
|
+
const ctor = this.constructor;
|
|
108
|
+
return new ctor(merged);
|
|
109
|
+
}
|
|
71
110
|
/**
|
|
72
111
|
* Entry method to run an agent via text-based conversation.
|
|
73
112
|
*
|
|
@@ -24,36 +24,35 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
24
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
25
|
var functions_exports = {};
|
|
26
26
|
__export(functions_exports, {
|
|
27
|
-
AF_FUNCTION_CALL_ID_PREFIX: () => AF_FUNCTION_CALL_ID_PREFIX,
|
|
27
|
+
AF_FUNCTION_CALL_ID_PREFIX: () => import_event2.AF_FUNCTION_CALL_ID_PREFIX,
|
|
28
28
|
REQUEST_CONFIRMATION_FUNCTION_CALL_NAME: () => REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
|
|
29
29
|
REQUEST_EUC_FUNCTION_CALL_NAME: () => REQUEST_EUC_FUNCTION_CALL_NAME,
|
|
30
30
|
findEventByFunctionCallId: () => findEventByFunctionCallId,
|
|
31
31
|
findMatchingFunctionCall: () => findMatchingFunctionCall,
|
|
32
32
|
functionsExportedForTestingOnly: () => functionsExportedForTestingOnly,
|
|
33
33
|
generateAuthEvent: () => generateAuthEvent,
|
|
34
|
-
generateClientFunctionCallId: () => generateClientFunctionCallId,
|
|
34
|
+
generateClientFunctionCallId: () => import_event2.generateClientFunctionCallId,
|
|
35
35
|
generateRequestConfirmationEvent: () => generateRequestConfirmationEvent,
|
|
36
36
|
getLongRunningFunctionCalls: () => getLongRunningFunctionCalls,
|
|
37
37
|
handleFunctionCallList: () => handleFunctionCallList,
|
|
38
38
|
handleFunctionCallsAsync: () => handleFunctionCallsAsync,
|
|
39
39
|
mergeParallelFunctionResponseEvents: () => mergeParallelFunctionResponseEvents,
|
|
40
|
-
populateClientFunctionCallId: () => populateClientFunctionCallId
|
|
40
|
+
populateClientFunctionCallId: () => import_event2.populateClientFunctionCallId
|
|
41
41
|
});
|
|
42
42
|
module.exports = __toCommonJS(functions_exports);
|
|
43
43
|
var import_genai = require("@google/genai");
|
|
44
44
|
var import_lodash_es = require("lodash-es");
|
|
45
45
|
var import_event = require("../events/event.js");
|
|
46
46
|
var import_event_actions = require("../events/event_actions.js");
|
|
47
|
-
var import_env_aware_utils = require("../utils/env_aware_utils.js");
|
|
48
47
|
var import_logger = require("../utils/logger.js");
|
|
49
48
|
var import_context = require("./context.js");
|
|
50
49
|
var import_tracing = require("../telemetry/tracing.js");
|
|
50
|
+
var import_event2 = require("../events/event.js");
|
|
51
51
|
/**
|
|
52
52
|
* @license
|
|
53
53
|
* Copyright 2025 Google LLC
|
|
54
54
|
* SPDX-License-Identifier: Apache-2.0
|
|
55
55
|
*/
|
|
56
|
-
const AF_FUNCTION_CALL_ID_PREFIX = "adk-";
|
|
57
56
|
const REQUEST_EUC_FUNCTION_CALL_NAME = "adk_request_credential";
|
|
58
57
|
const REQUEST_CONFIRMATION_FUNCTION_CALL_NAME = "adk_request_confirmation";
|
|
59
58
|
const functionsExportedForTestingOnly = {
|
|
@@ -61,20 +60,6 @@ const functionsExportedForTestingOnly = {
|
|
|
61
60
|
generateAuthEvent,
|
|
62
61
|
generateRequestConfirmationEvent
|
|
63
62
|
};
|
|
64
|
-
function generateClientFunctionCallId() {
|
|
65
|
-
return `${AF_FUNCTION_CALL_ID_PREFIX}${(0, import_env_aware_utils.randomUUID)()}`;
|
|
66
|
-
}
|
|
67
|
-
function populateClientFunctionCallId(modelResponseEvent) {
|
|
68
|
-
const functionCalls = (0, import_event.getFunctionCalls)(modelResponseEvent);
|
|
69
|
-
if (!functionCalls) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
for (const functionCall of functionCalls) {
|
|
73
|
-
if (!functionCall.id) {
|
|
74
|
-
functionCall.id = generateClientFunctionCallId();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
63
|
function getLongRunningFunctionCalls(functionCalls, toolsDict) {
|
|
79
64
|
const longRunningToolIds = /* @__PURE__ */ new Set();
|
|
80
65
|
for (const functionCall of functionCalls) {
|
|
@@ -100,7 +85,7 @@ function generateAuthEvent(invocationContext, functionResponseEvent) {
|
|
|
100
85
|
"function_call_id": functionCallId,
|
|
101
86
|
"auth_config": authConfig
|
|
102
87
|
},
|
|
103
|
-
id: generateClientFunctionCallId()
|
|
88
|
+
id: (0, import_event.generateClientFunctionCallId)()
|
|
104
89
|
};
|
|
105
90
|
longRunningToolIds.add(requestEucFunctionCall.id);
|
|
106
91
|
parts.push({ functionCall: requestEucFunctionCall });
|
|
@@ -141,7 +126,7 @@ function generateRequestConfirmationEvent({
|
|
|
141
126
|
"originalFunctionCall": originalFunctionCall,
|
|
142
127
|
"toolConfirmation": toolConfirmation
|
|
143
128
|
},
|
|
144
|
-
id: generateClientFunctionCallId()
|
|
129
|
+
id: (0, import_event.generateClientFunctionCallId)()
|
|
145
130
|
};
|
|
146
131
|
longRunningToolIds.add(requestConfirmationFunctionCall.id);
|
|
147
132
|
parts.push({ functionCall: requestConfirmationFunctionCall });
|
|
@@ -195,6 +195,7 @@ class LlmAgent extends (_b = import_base_agent.BaseAgent, _a = LLM_AGENT_SIGNATU
|
|
|
195
195
|
* This method is only for use by Agent Development Kit.
|
|
196
196
|
* @param context The context to retrieve the session state.
|
|
197
197
|
* @returns The resolved globalInstruction field.
|
|
198
|
+
* @deprecated Use GlobalInstructionPlugin instead.
|
|
198
199
|
*/
|
|
199
200
|
async canonicalGlobalInstruction(context2) {
|
|
200
201
|
if (typeof this.globalInstruction === "string") {
|
|
@@ -511,7 +512,7 @@ class LlmAgent extends (_b = import_base_agent.BaseAgent, _a = LLM_AGENT_SIGNATU
|
|
|
511
512
|
mergedEvent.content.parts = [{ text: JSON.stringify(args) }];
|
|
512
513
|
mergedEvent.actions.skipSummarization = true;
|
|
513
514
|
} else if (functionCalls && functionCalls.length) {
|
|
514
|
-
(0,
|
|
515
|
+
(0, import_event.populateClientFunctionCallId)(mergedEvent);
|
|
515
516
|
mergedEvent.longRunningToolIds = Array.from(
|
|
516
517
|
(0, import_functions.getLongRunningFunctionCalls)(functionCalls, llmRequest.toolsDict)
|
|
517
518
|
);
|
|
@@ -72,9 +72,21 @@ class LoopAgent extends (_b = import_base_agent.BaseAgent, _a = LOOP_AGENT_SIGNA
|
|
|
72
72
|
}
|
|
73
73
|
return;
|
|
74
74
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
async *runLiveImpl(context) {
|
|
76
|
+
var _a2, _b2;
|
|
77
|
+
for (let iteration = 0; iteration < this.maxIterations; iteration++) {
|
|
78
|
+
for (const subAgent of this.subAgents) {
|
|
79
|
+
for await (const event of subAgent.runLive(context)) {
|
|
80
|
+
if ((_a2 = context.abortSignal) == null ? void 0 : _a2.aborted) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
yield event;
|
|
84
|
+
if ((_b2 = event.actions) == null ? void 0 : _b2.escalate) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
78
90
|
}
|
|
79
91
|
}
|
|
80
92
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -34,6 +34,7 @@ var import_genai = require("@google/genai");
|
|
|
34
34
|
var import_lodash_es = require("lodash-es");
|
|
35
35
|
var import_compacted_event = require("../../events/compacted_event.js");
|
|
36
36
|
var import_event = require("../../events/event.js");
|
|
37
|
+
var import_branch_trie = require("../../utils/branch_trie.js");
|
|
37
38
|
var import_functions = require("../functions.js");
|
|
38
39
|
/**
|
|
39
40
|
* @license
|
|
@@ -62,7 +63,7 @@ function getContents(events, agentName, currentBranch) {
|
|
|
62
63
|
if (!((_a = event.content) == null ? void 0 : _a.role) || ((_c = (_b = event.content.parts) == null ? void 0 : _b[0]) == null ? void 0 : _c.text) === "") {
|
|
63
64
|
continue;
|
|
64
65
|
}
|
|
65
|
-
if (currentBranch && event.branch && !
|
|
66
|
+
if (currentBranch && event.branch && !(0, import_branch_trie.isSegmentPrefix)(currentBranch, event.branch)) {
|
|
66
67
|
continue;
|
|
67
68
|
}
|
|
68
69
|
if (isAuthEvent(event)) {
|
|
@@ -64,6 +64,29 @@ let RoutedAgent = class extends (_b = import_base_agent.BaseAgent, _a = ROUTED_A
|
|
|
64
64
|
}
|
|
65
65
|
this.router = config.router;
|
|
66
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Creates a detached copy of this routed agent.
|
|
69
|
+
*
|
|
70
|
+
* The inherited {@link BaseAgent.clone} only deep-clones `subAgents`, but a
|
|
71
|
+
* `RoutedAgent` derives its routing targets from `config.agents`. Rebuilding
|
|
72
|
+
* through the base clone alone would re-read the already-parented original
|
|
73
|
+
* agents and throw "already has a parent agent". This override deep-clones the
|
|
74
|
+
* routing targets (unless the caller overrides `agents`) so the rebuilt
|
|
75
|
+
* constructor re-parents fresh, detached copies. The array-vs-record shape
|
|
76
|
+
* and, for records, the keys are preserved so the router keeps selecting the
|
|
77
|
+
* same targets.
|
|
78
|
+
*
|
|
79
|
+
* @param overrides Config fields to override on the clone. Overriding
|
|
80
|
+
* `parentAgent` is rejected by the base implementation.
|
|
81
|
+
* @returns A new detached `RoutedAgent` of the same concrete class.
|
|
82
|
+
*/
|
|
83
|
+
clone(overrides) {
|
|
84
|
+
const nextOverrides = { ...overrides };
|
|
85
|
+
if (!("agents" in nextOverrides)) {
|
|
86
|
+
nextOverrides.agents = cloneRoutingTargets(this.config.agents);
|
|
87
|
+
}
|
|
88
|
+
return super.clone(nextOverrides);
|
|
89
|
+
}
|
|
67
90
|
/**
|
|
68
91
|
* Runs the selected agent via text-based conversation.
|
|
69
92
|
*/
|
|
@@ -90,6 +113,14 @@ let RoutedAgent = class extends (_b = import_base_agent.BaseAgent, _a = ROUTED_A
|
|
|
90
113
|
RoutedAgent = __decorateClass([
|
|
91
114
|
import_experimental.experimental
|
|
92
115
|
], RoutedAgent);
|
|
116
|
+
function cloneRoutingTargets(agents) {
|
|
117
|
+
if (Array.isArray(agents)) {
|
|
118
|
+
return agents.map((agent) => agent.clone());
|
|
119
|
+
}
|
|
120
|
+
return Object.fromEntries(
|
|
121
|
+
Object.entries(agents).map(([key, agent]) => [key, agent.clone()])
|
|
122
|
+
);
|
|
123
|
+
}
|
|
93
124
|
// Annotate the CommonJS export names for ESM import in node:
|
|
94
125
|
0 && (module.exports = {
|
|
95
126
|
RoutedAgent,
|
|
@@ -41,14 +41,15 @@ var StreamingMode = /* @__PURE__ */ ((StreamingMode2) => {
|
|
|
41
41
|
return StreamingMode2;
|
|
42
42
|
})(StreamingMode || {});
|
|
43
43
|
function createRunConfig(params = {}) {
|
|
44
|
+
var _a;
|
|
44
45
|
return {
|
|
45
46
|
saveInputBlobsAsArtifacts: false,
|
|
46
47
|
supportCfc: false,
|
|
47
48
|
enableAffectiveDialog: false,
|
|
48
49
|
streamingMode: "none" /* NONE */,
|
|
49
|
-
maxLlmCalls: validateMaxLlmCalls(params.maxLlmCalls || 500),
|
|
50
50
|
pauseOnToolCalls: false,
|
|
51
|
-
...params
|
|
51
|
+
...params,
|
|
52
|
+
maxLlmCalls: validateMaxLlmCalls((_a = params.maxLlmCalls) != null ? _a : 500)
|
|
52
53
|
};
|
|
53
54
|
}
|
|
54
55
|
function validateMaxLlmCalls(value) {
|
|
@@ -28,6 +28,7 @@ __export(in_memory_artifact_service_exports, {
|
|
|
28
28
|
isInMemoryConnectionString: () => isInMemoryConnectionString
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(in_memory_artifact_service_exports);
|
|
31
|
+
var import_logger = require("../utils/logger.js");
|
|
31
32
|
/**
|
|
32
33
|
* @license
|
|
33
34
|
* Copyright 2025 Google LLC
|
|
@@ -84,6 +85,12 @@ class InMemoryArtifactService {
|
|
|
84
85
|
if (version === void 0) {
|
|
85
86
|
version = versions.length - 1;
|
|
86
87
|
}
|
|
88
|
+
if (!versions[version]) {
|
|
89
|
+
import_logger.logger.warn(
|
|
90
|
+
`[InMemoryArtifactService] loadArtifact: Artifact ${filename} version ${version} not found`
|
|
91
|
+
);
|
|
92
|
+
return Promise.resolve(void 0);
|
|
93
|
+
}
|
|
87
94
|
return Promise.resolve(versions[version].part);
|
|
88
95
|
}
|
|
89
96
|
listArtifactKeys({
|
package/dist/cjs/common.js
CHANGED
|
@@ -31,6 +31,7 @@ __export(common_exports, {
|
|
|
31
31
|
AgentControlledContextCompactor: () => import_agent_controlled_context_compactor.AgentControlledContextCompactor,
|
|
32
32
|
AgentTool: () => import_agent_tool.AgentTool,
|
|
33
33
|
AgentTransferLlmRequestProcessor: () => import_agent_transfer_llm_request_processor.AgentTransferLlmRequestProcessor,
|
|
34
|
+
AnchoredContextCompactor: () => import_anchored_context_compactor.AnchoredContextCompactor,
|
|
34
35
|
ApigeeLlm: () => import_apigee_llm.ApigeeLlm,
|
|
35
36
|
AuthCredentialTypes: () => import_auth_credential.AuthCredentialTypes,
|
|
36
37
|
AuthHandler: () => import_auth_handler.AuthHandler,
|
|
@@ -56,8 +57,11 @@ __export(common_exports, {
|
|
|
56
57
|
ContextCompactorRequestProcessor: () => import_context_compactor_request_processor.ContextCompactorRequestProcessor,
|
|
57
58
|
CredentialExchangeError: () => import_base_credential_exchanger.CredentialExchangeError,
|
|
58
59
|
CredentialRefresherRegistry: () => import_credential_refresher_registry.CredentialRefresherRegistry,
|
|
60
|
+
ENTERPRISE_WEB_SEARCH: () => import_enterprise_web_search_tool.ENTERPRISE_WEB_SEARCH,
|
|
59
61
|
EXIT_LOOP: () => import_exit_loop_tool.EXIT_LOOP,
|
|
62
|
+
EnterpriseWebSearchTool: () => import_enterprise_web_search_tool.EnterpriseWebSearchTool,
|
|
60
63
|
EventType: () => import_structured_events.EventType,
|
|
64
|
+
ExampleTool: () => import_example_tool.ExampleTool,
|
|
61
65
|
ExitLoopTool: () => import_exit_loop_tool.ExitLoopTool,
|
|
62
66
|
FileContentEncoding: () => import_code_execution_utils.FileContentEncoding,
|
|
63
67
|
FunctionTool: () => import_function_tool.FunctionTool,
|
|
@@ -65,6 +69,7 @@ __export(common_exports, {
|
|
|
65
69
|
GOOGLE_MAPS_GROUNDING: () => import_google_maps_grounding_tool.GOOGLE_MAPS_GROUNDING,
|
|
66
70
|
GOOGLE_SEARCH: () => import_google_search_tool.GOOGLE_SEARCH,
|
|
67
71
|
Gemini: () => import_google_llm.Gemini,
|
|
72
|
+
GlobalInstructionPlugin: () => import_global_instruction_plugin.GlobalInstructionPlugin,
|
|
68
73
|
GoogleLLMVariant: () => import_variant_utils.GoogleLLMVariant,
|
|
69
74
|
GoogleMapsGroundingTool: () => import_google_maps_grounding_tool.GoogleMapsGroundingTool,
|
|
70
75
|
GoogleSearchTool: () => import_google_search_tool.GoogleSearchTool,
|
|
@@ -80,6 +85,7 @@ __export(common_exports, {
|
|
|
80
85
|
LLMRegistry: () => import_registry.LLMRegistry,
|
|
81
86
|
LOAD_ARTIFACTS: () => import_load_artifacts_tool.LOAD_ARTIFACTS,
|
|
82
87
|
LOAD_MEMORY: () => import_load_memory_tool.LOAD_MEMORY,
|
|
88
|
+
LOAD_WEB_PAGE: () => import_load_web_page.LOAD_WEB_PAGE,
|
|
83
89
|
ListSkillsTool: () => import_list_skills_tool.ListSkillsTool,
|
|
84
90
|
LiveRequestQueue: () => import_live_request_queue.LiveRequestQueue,
|
|
85
91
|
LlmAgent: () => import_llm_agent.LlmAgent,
|
|
@@ -138,11 +144,13 @@ __export(common_exports, {
|
|
|
138
144
|
findMatchingFunctionCall: () => import_functions.findMatchingFunctionCall,
|
|
139
145
|
functionsExportedForTestingOnly: () => import_functions.functionsExportedForTestingOnly,
|
|
140
146
|
geminiInitParams: () => import_google_llm.geminiInitParams,
|
|
147
|
+
generateClientFunctionCallId: () => import_event.generateClientFunctionCallId,
|
|
141
148
|
getAskUserConfirmationFunctionCalls: () => import_security_plugin.getAskUserConfirmationFunctionCalls,
|
|
142
149
|
getClientLabels: () => import_client_labels.getClientLabels,
|
|
143
150
|
getFunctionCalls: () => import_event.getFunctionCalls,
|
|
144
151
|
getFunctionResponses: () => import_event.getFunctionResponses,
|
|
145
152
|
getLogger: () => import_logger.getLogger,
|
|
153
|
+
hasThoughts: () => import_event.hasThoughts,
|
|
146
154
|
hasTrailingCodeExecutionResult: () => import_event.hasTrailingCodeExecutionResult,
|
|
147
155
|
isAgentTool: () => import_agent_tool.isAgentTool,
|
|
148
156
|
isBaseAgent: () => import_base_agent.isBaseAgent,
|
|
@@ -161,10 +169,14 @@ __export(common_exports, {
|
|
|
161
169
|
isRoutableLlmAgent: () => import_runner.isRoutableLlmAgent,
|
|
162
170
|
isRoutedAgent: () => import_routed_agent.isRoutedAgent,
|
|
163
171
|
isRunner: () => import_runner.isRunner,
|
|
172
|
+
isScratchpadEvent: () => import_compacted_event.isScratchpadEvent,
|
|
164
173
|
isSequentialAgent: () => import_sequential_agent.isSequentialAgent,
|
|
165
174
|
loadAllSkillsInDir: () => import_loader.loadAllSkillsInDir,
|
|
166
175
|
loadSkillFromDir: () => import_loader.loadSkillFromDir,
|
|
167
176
|
loadSkillFromZipBuffer: () => import_loader.loadSkillFromZipBuffer,
|
|
177
|
+
loadWebPage: () => import_load_web_page.loadWebPage,
|
|
178
|
+
populateClientFunctionCallId: () => import_event.populateClientFunctionCallId,
|
|
179
|
+
pruneThoughts: () => import_event.pruneThoughts,
|
|
168
180
|
runWithClientLabel: () => import_client_labels.runWithClientLabel,
|
|
169
181
|
setLogLevel: () => import_logger.setLogLevel,
|
|
170
182
|
setLogger: () => import_logger.setLogger,
|
|
@@ -210,6 +222,7 @@ var import_base_code_executor = require("./code_executors/base_code_executor.js"
|
|
|
210
222
|
var import_built_in_code_executor = require("./code_executors/built_in_code_executor.js");
|
|
211
223
|
var import_code_execution_utils = require("./code_executors/code_execution_utils.js");
|
|
212
224
|
var import_agent_controlled_context_compactor = require("./context/agent_controlled_context_compactor.js");
|
|
225
|
+
var import_anchored_context_compactor = require("./context/anchored_context_compactor.js");
|
|
213
226
|
var import_llm_summarizer = require("./context/summarizers/llm_summarizer.js");
|
|
214
227
|
var import_token_based_context_compactor = require("./context/token_based_context_compactor.js");
|
|
215
228
|
var import_trajectory_thought_pruning_compactor = require("./context/trajectory_thought_pruning_compactor.js");
|
|
@@ -227,6 +240,7 @@ var import_google_llm = require("./models/google_llm.js");
|
|
|
227
240
|
var import_registry = require("./models/registry.js");
|
|
228
241
|
var import_routed_llm = require("./models/routed_llm.js");
|
|
229
242
|
var import_base_plugin = require("./plugins/base_plugin.js");
|
|
243
|
+
var import_global_instruction_plugin = require("./plugins/global_instruction_plugin.js");
|
|
230
244
|
var import_logging_plugin = require("./plugins/logging_plugin.js");
|
|
231
245
|
var import_plugin_manager = require("./plugins/plugin_manager.js");
|
|
232
246
|
var import_security_plugin = require("./plugins/security_plugin.js");
|
|
@@ -240,12 +254,15 @@ var import_agent_tool = require("./tools/agent_tool.js");
|
|
|
240
254
|
var import_base_tool = require("./tools/base_tool.js");
|
|
241
255
|
var import_base_toolset = require("./tools/base_toolset.js");
|
|
242
256
|
var import_consolidate_context_tool = require("./tools/consolidate_context_tool.js");
|
|
257
|
+
var import_enterprise_web_search_tool = require("./tools/enterprise_web_search_tool.js");
|
|
258
|
+
var import_example_tool = require("./tools/example_tool.js");
|
|
243
259
|
var import_exit_loop_tool = require("./tools/exit_loop_tool.js");
|
|
244
260
|
var import_function_tool = require("./tools/function_tool.js");
|
|
245
261
|
var import_google_maps_grounding_tool = require("./tools/google_maps_grounding_tool.js");
|
|
246
262
|
var import_google_search_tool = require("./tools/google_search_tool.js");
|
|
247
263
|
var import_load_artifacts_tool = require("./tools/load_artifacts_tool.js");
|
|
248
264
|
var import_load_memory_tool = require("./tools/load_memory_tool.js");
|
|
265
|
+
var import_load_web_page = require("./tools/load_web_page.js");
|
|
249
266
|
var import_long_running_tool = require("./tools/long_running_tool.js");
|
|
250
267
|
var import_preload_memory_tool = require("./tools/preload_memory_tool.js");
|
|
251
268
|
var import_tool_confirmation = require("./tools/tool_confirmation.js");
|
|
@@ -295,6 +312,7 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
|
|
|
295
312
|
AgentControlledContextCompactor,
|
|
296
313
|
AgentTool,
|
|
297
314
|
AgentTransferLlmRequestProcessor,
|
|
315
|
+
AnchoredContextCompactor,
|
|
298
316
|
ApigeeLlm,
|
|
299
317
|
AuthCredentialTypes,
|
|
300
318
|
AuthHandler,
|
|
@@ -320,8 +338,11 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
|
|
|
320
338
|
ContextCompactorRequestProcessor,
|
|
321
339
|
CredentialExchangeError,
|
|
322
340
|
CredentialRefresherRegistry,
|
|
341
|
+
ENTERPRISE_WEB_SEARCH,
|
|
323
342
|
EXIT_LOOP,
|
|
343
|
+
EnterpriseWebSearchTool,
|
|
324
344
|
EventType,
|
|
345
|
+
ExampleTool,
|
|
325
346
|
ExitLoopTool,
|
|
326
347
|
FileContentEncoding,
|
|
327
348
|
FunctionTool,
|
|
@@ -329,6 +350,7 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
|
|
|
329
350
|
GOOGLE_MAPS_GROUNDING,
|
|
330
351
|
GOOGLE_SEARCH,
|
|
331
352
|
Gemini,
|
|
353
|
+
GlobalInstructionPlugin,
|
|
332
354
|
GoogleLLMVariant,
|
|
333
355
|
GoogleMapsGroundingTool,
|
|
334
356
|
GoogleSearchTool,
|
|
@@ -344,6 +366,7 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
|
|
|
344
366
|
LLMRegistry,
|
|
345
367
|
LOAD_ARTIFACTS,
|
|
346
368
|
LOAD_MEMORY,
|
|
369
|
+
LOAD_WEB_PAGE,
|
|
347
370
|
ListSkillsTool,
|
|
348
371
|
LiveRequestQueue,
|
|
349
372
|
LlmAgent,
|
|
@@ -402,11 +425,13 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
|
|
|
402
425
|
findMatchingFunctionCall,
|
|
403
426
|
functionsExportedForTestingOnly,
|
|
404
427
|
geminiInitParams,
|
|
428
|
+
generateClientFunctionCallId,
|
|
405
429
|
getAskUserConfirmationFunctionCalls,
|
|
406
430
|
getClientLabels,
|
|
407
431
|
getFunctionCalls,
|
|
408
432
|
getFunctionResponses,
|
|
409
433
|
getLogger,
|
|
434
|
+
hasThoughts,
|
|
410
435
|
hasTrailingCodeExecutionResult,
|
|
411
436
|
isAgentTool,
|
|
412
437
|
isBaseAgent,
|
|
@@ -425,10 +450,14 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
|
|
|
425
450
|
isRoutableLlmAgent,
|
|
426
451
|
isRoutedAgent,
|
|
427
452
|
isRunner,
|
|
453
|
+
isScratchpadEvent,
|
|
428
454
|
isSequentialAgent,
|
|
429
455
|
loadAllSkillsInDir,
|
|
430
456
|
loadSkillFromDir,
|
|
431
457
|
loadSkillFromZipBuffer,
|
|
458
|
+
loadWebPage,
|
|
459
|
+
populateClientFunctionCallId,
|
|
460
|
+
pruneThoughts,
|
|
432
461
|
runWithClientLabel,
|
|
433
462
|
setLogLevel,
|
|
434
463
|
setLogger,
|
|
@@ -0,0 +1,127 @@
|
|
|
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 anchored_context_compactor_exports = {};
|
|
26
|
+
__export(anchored_context_compactor_exports, {
|
|
27
|
+
AnchoredContextCompactor: () => AnchoredContextCompactor
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(anchored_context_compactor_exports);
|
|
30
|
+
var import_compacted_event = require("../events/compacted_event.js");
|
|
31
|
+
var import_event = require("../events/event.js");
|
|
32
|
+
var import_compaction_utils = require("./compaction_utils.js");
|
|
33
|
+
/**
|
|
34
|
+
* @license
|
|
35
|
+
* Copyright 2026 Google LLC
|
|
36
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
37
|
+
*/
|
|
38
|
+
class AnchoredContextCompactor {
|
|
39
|
+
constructor(options) {
|
|
40
|
+
this.tokenThreshold = options.tokenThreshold;
|
|
41
|
+
this.eventRetentionSize = options.eventRetentionSize;
|
|
42
|
+
this.summarizer = options.summarizer;
|
|
43
|
+
}
|
|
44
|
+
getActiveEvents(events) {
|
|
45
|
+
let latestScratchpad = void 0;
|
|
46
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
47
|
+
const e = events[i];
|
|
48
|
+
if ((0, import_compacted_event.isScratchpadEvent)(e)) {
|
|
49
|
+
latestScratchpad = e;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (!latestScratchpad) {
|
|
54
|
+
return events;
|
|
55
|
+
}
|
|
56
|
+
const activeRawEvents = events.filter(
|
|
57
|
+
(e) => e.timestamp > latestScratchpad.endTime && !(0, import_compacted_event.isScratchpadEvent)(e)
|
|
58
|
+
);
|
|
59
|
+
return [latestScratchpad, ...activeRawEvents];
|
|
60
|
+
}
|
|
61
|
+
shouldCompact(invocationContext) {
|
|
62
|
+
const events = invocationContext.session.events;
|
|
63
|
+
const activeEvents = this.getActiveEvents(events);
|
|
64
|
+
const hasScratchpad = activeEvents.length > 0 && (0, import_compacted_event.isScratchpadEvent)(activeEvents[0]);
|
|
65
|
+
const rawEvents = hasScratchpad ? activeEvents.slice(1) : activeEvents;
|
|
66
|
+
if (rawEvents.length <= this.eventRetentionSize) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const retainStartIndex = (0, import_compaction_utils.calculateRetainStartIndex)(
|
|
70
|
+
rawEvents,
|
|
71
|
+
this.eventRetentionSize
|
|
72
|
+
);
|
|
73
|
+
if (retainStartIndex === 0) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
const totalTokens = activeEvents.reduce(
|
|
77
|
+
(sum, event) => sum + (0, import_event.getEventTokens)(event),
|
|
78
|
+
0
|
|
79
|
+
);
|
|
80
|
+
return totalTokens > this.tokenThreshold;
|
|
81
|
+
}
|
|
82
|
+
async compact(invocationContext) {
|
|
83
|
+
const events = invocationContext.session.events;
|
|
84
|
+
const activeEvents = this.getActiveEvents(events);
|
|
85
|
+
const hasScratchpad = activeEvents.length > 0 && (0, import_compacted_event.isScratchpadEvent)(activeEvents[0]);
|
|
86
|
+
const rawEvents = hasScratchpad ? activeEvents.slice(1) : activeEvents;
|
|
87
|
+
if (rawEvents.length <= this.eventRetentionSize) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const retainStartIndex = (0, import_compaction_utils.calculateRetainStartIndex)(
|
|
91
|
+
rawEvents,
|
|
92
|
+
this.eventRetentionSize
|
|
93
|
+
);
|
|
94
|
+
if (retainStartIndex === 0) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const rawEventsToCompact = rawEvents.slice(0, retainStartIndex);
|
|
98
|
+
let scratchpadEvent;
|
|
99
|
+
if (hasScratchpad) {
|
|
100
|
+
const existingScratchpad = activeEvents[0];
|
|
101
|
+
scratchpadEvent = await this.summarizer.summarize([
|
|
102
|
+
existingScratchpad,
|
|
103
|
+
...rawEventsToCompact
|
|
104
|
+
]);
|
|
105
|
+
} else {
|
|
106
|
+
scratchpadEvent = await this.summarizer.summarize(rawEventsToCompact);
|
|
107
|
+
}
|
|
108
|
+
const updatedScratchpad = {
|
|
109
|
+
...scratchpadEvent,
|
|
110
|
+
isScratchpad: true,
|
|
111
|
+
author: "system"
|
|
112
|
+
};
|
|
113
|
+
const inactiveEvents = events.slice(0, events.indexOf(activeEvents[0]));
|
|
114
|
+
const retainedRawEvents = rawEvents.slice(retainStartIndex);
|
|
115
|
+
const newEventsList = [
|
|
116
|
+
...inactiveEvents,
|
|
117
|
+
updatedScratchpad,
|
|
118
|
+
...retainedRawEvents
|
|
119
|
+
];
|
|
120
|
+
events.length = 0;
|
|
121
|
+
events.push(...newEventsList);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
125
|
+
0 && (module.exports = {
|
|
126
|
+
AnchoredContextCompactor
|
|
127
|
+
});
|