@google/adk 0.5.0 → 0.6.1
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/README.md +1 -1
- package/dist/cjs/a2a/a2a_event.js +53 -26
- package/dist/cjs/a2a/a2a_remote_agent.js +179 -0
- package/dist/cjs/a2a/a2a_remote_agent_run_processor.js +205 -0
- package/dist/cjs/a2a/a2a_remote_agent_utils.js +165 -0
- package/dist/cjs/a2a/agent_card.js +380 -0
- package/dist/cjs/a2a/agent_executor.js +221 -0
- package/dist/cjs/a2a/agent_to_a2a.js +115 -0
- package/dist/cjs/a2a/event_processor_utils.js +180 -0
- package/dist/cjs/a2a/executor_context.js +1 -1
- package/dist/cjs/a2a/metadata_converter_utils.js +1 -0
- package/dist/cjs/a2a/part_converter_utils.js +24 -13
- package/dist/cjs/agents/llm_agent.js +17 -0
- package/dist/cjs/agents/{content_processor_utils.js → processors/content_processor_utils.js} +21 -2
- package/dist/cjs/agents/processors/content_request_processor.js +24 -3
- package/dist/cjs/agents/processors/context_compactor_request_processor.js +61 -0
- package/dist/cjs/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/cjs/artifacts/file_artifact_service.js +35 -4
- package/dist/cjs/common.js +37 -0
- package/dist/cjs/context/base_context_compactor.js +27 -0
- package/dist/cjs/context/summarizers/base_summarizer.js +27 -0
- package/dist/cjs/context/summarizers/llm_summarizer.js +93 -0
- package/dist/cjs/context/token_based_context_compactor.js +135 -0
- package/dist/cjs/context/truncating_context_compactor.js +58 -0
- package/dist/cjs/events/compacted_event.js +53 -0
- package/dist/cjs/index.js +38 -14
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/memory/in_memory_memory_service.js +1 -1
- package/dist/cjs/runner/runner.js +19 -10
- package/dist/cjs/sessions/db/operations.js +4 -14
- package/dist/cjs/sessions/state.js +2 -2
- package/dist/cjs/tools/agent_tool.js +6 -5
- package/dist/cjs/tools/load_artifacts_tool.js +188 -0
- package/dist/cjs/tools/load_memory_tool.js +107 -0
- package/dist/cjs/tools/preload_memory_tool.js +109 -0
- package/dist/cjs/utils/gemini_schema_util.js +54 -12
- package/dist/cjs/utils/logger.js +1 -0
- package/dist/cjs/version.js +1 -1
- package/dist/esm/a2a/a2a_event.js +52 -26
- package/dist/esm/a2a/a2a_remote_agent.js +153 -0
- package/dist/esm/a2a/a2a_remote_agent_run_processor.js +182 -0
- package/dist/esm/a2a/a2a_remote_agent_utils.js +131 -0
- package/dist/esm/a2a/agent_card.js +340 -0
- package/dist/esm/a2a/agent_executor.js +202 -0
- package/dist/esm/a2a/agent_to_a2a.js +80 -0
- package/dist/esm/a2a/event_processor_utils.js +159 -0
- package/dist/esm/a2a/executor_context.js +1 -1
- package/dist/esm/a2a/metadata_converter_utils.js +1 -0
- package/dist/esm/a2a/part_converter_utils.js +24 -13
- package/dist/esm/agents/llm_agent.js +17 -0
- package/dist/esm/agents/{content_processor_utils.js → processors/content_processor_utils.js} +23 -2
- package/dist/esm/agents/processors/content_request_processor.js +25 -4
- package/dist/esm/agents/processors/context_compactor_request_processor.js +31 -0
- package/dist/esm/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/esm/artifacts/file_artifact_service.js +30 -3
- package/dist/esm/common.js +33 -1
- package/dist/esm/context/base_context_compactor.js +5 -0
- package/dist/esm/context/summarizers/base_summarizer.js +5 -0
- package/dist/esm/context/summarizers/llm_summarizer.js +65 -0
- package/dist/esm/context/token_based_context_compactor.js +105 -0
- package/dist/esm/context/truncating_context_compactor.js +28 -0
- package/dist/esm/events/compacted_event.js +22 -0
- package/dist/esm/index.js +38 -14
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/memory/in_memory_memory_service.js +1 -1
- package/dist/esm/runner/runner.js +17 -9
- package/dist/esm/sessions/db/operations.js +4 -14
- package/dist/esm/sessions/state.js +2 -2
- package/dist/esm/tools/agent_tool.js +6 -5
- package/dist/esm/tools/load_artifacts_tool.js +159 -0
- package/dist/esm/tools/load_memory_tool.js +78 -0
- package/dist/esm/tools/preload_memory_tool.js +80 -0
- package/dist/esm/utils/gemini_schema_util.js +54 -12
- package/dist/esm/utils/logger.js +1 -0
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_event.d.ts +23 -6
- package/dist/types/a2a/a2a_remote_agent.d.ts +67 -0
- package/dist/types/a2a/a2a_remote_agent_run_processor.d.ts +31 -0
- package/dist/types/a2a/a2a_remote_agent_utils.d.ts +38 -0
- package/dist/types/a2a/agent_card.d.ts +23 -0
- package/dist/types/a2a/agent_executor.d.ts +52 -0
- package/dist/types/a2a/agent_to_a2a.d.ts +45 -0
- package/dist/types/a2a/event_processor_utils.d.ts +24 -0
- package/dist/types/a2a/executor_context.d.ts +1 -1
- package/dist/types/a2a/metadata_converter_utils.d.ts +2 -1
- package/dist/types/agents/llm_agent.d.ts +6 -0
- package/dist/types/agents/loop_agent.d.ts +1 -1
- package/dist/types/agents/{content_processor_utils.d.ts → processors/content_processor_utils.d.ts} +1 -1
- package/dist/types/agents/processors/context_compactor_request_processor.d.ts +22 -0
- package/dist/types/artifacts/file_artifact_service.d.ts +4 -0
- package/dist/types/common.d.ts +16 -1
- package/dist/types/context/base_context_compactor.d.ts +24 -0
- package/dist/types/context/summarizers/base_summarizer.d.ts +19 -0
- package/dist/types/context/summarizers/llm_summarizer.d.ts +23 -0
- package/dist/types/context/token_based_context_compactor.d.ts +33 -0
- package/dist/types/context/truncating_context_compactor.d.ts +24 -0
- package/dist/types/events/compacted_event.d.ts +33 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/runner/runner.d.ts +13 -0
- package/dist/types/sessions/db/operations.d.ts +2 -3
- package/dist/types/tools/load_artifacts_tool.d.ts +21 -0
- package/dist/types/tools/load_memory_tool.d.ts +22 -0
- package/dist/types/tools/preload_memory_tool.d.ts +23 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/a2a/a2a_event.js +52 -26
- package/dist/web/a2a/a2a_remote_agent.js +184 -0
- package/dist/web/a2a/a2a_remote_agent_run_processor.js +182 -0
- package/dist/web/a2a/a2a_remote_agent_utils.js +131 -0
- package/dist/web/a2a/agent_card.js +340 -0
- package/dist/web/a2a/agent_executor.js +216 -0
- package/dist/web/a2a/agent_to_a2a.js +80 -0
- package/dist/web/a2a/event_processor_utils.js +168 -0
- package/dist/web/a2a/executor_context.js +1 -1
- package/dist/web/a2a/metadata_converter_utils.js +1 -0
- package/dist/web/a2a/part_converter_utils.js +24 -13
- package/dist/web/agents/llm_agent.js +17 -0
- package/dist/web/agents/{content_processor_utils.js → processors/content_processor_utils.js} +22 -2
- package/dist/web/agents/processors/content_request_processor.js +25 -4
- package/dist/web/agents/processors/context_compactor_request_processor.js +49 -0
- package/dist/web/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/web/artifacts/file_artifact_service.js +30 -3
- package/dist/web/common.js +33 -1
- package/dist/web/context/base_context_compactor.js +5 -0
- package/dist/web/context/summarizers/base_summarizer.js +5 -0
- package/dist/web/context/summarizers/llm_summarizer.js +74 -0
- package/dist/web/context/token_based_context_compactor.js +105 -0
- package/dist/web/context/truncating_context_compactor.js +28 -0
- package/dist/web/events/compacted_event.js +40 -0
- package/dist/web/index.js +1 -1
- package/dist/web/index.js.map +4 -4
- package/dist/web/memory/in_memory_memory_service.js +1 -1
- package/dist/web/runner/runner.js +17 -9
- package/dist/web/sessions/db/operations.js +4 -14
- package/dist/web/sessions/state.js +2 -2
- package/dist/web/tools/agent_tool.js +6 -5
- package/dist/web/tools/load_artifacts_tool.js +150 -0
- package/dist/web/tools/load_memory_tool.js +77 -0
- package/dist/web/tools/preload_memory_tool.js +75 -0
- package/dist/web/utils/gemini_schema_util.js +85 -12
- package/dist/web/utils/logger.js +1 -0
- package/dist/web/version.js +1 -1
- package/package.json +7 -5
package/dist/web/common.js
CHANGED
|
@@ -16,6 +16,11 @@ import {
|
|
|
16
16
|
BaseLlmRequestProcessor,
|
|
17
17
|
BaseLlmResponseProcessor
|
|
18
18
|
} from "./agents/processors/base_llm_processor.js";
|
|
19
|
+
import {
|
|
20
|
+
CONTENT_REQUEST_PROCESSOR,
|
|
21
|
+
ContentRequestProcessor
|
|
22
|
+
} from "./agents/processors/content_request_processor.js";
|
|
23
|
+
import { ContextCompactorRequestProcessor } from "./agents/processors/context_compactor_request_processor.js";
|
|
19
24
|
import { ReadonlyContext } from "./agents/readonly_context.js";
|
|
20
25
|
import { StreamingMode } from "./agents/run_config.js";
|
|
21
26
|
import { SequentialAgent, isSequentialAgent } from "./agents/sequential_agent.js";
|
|
@@ -23,6 +28,10 @@ import { InMemoryArtifactService } from "./artifacts/in_memory_artifact_service.
|
|
|
23
28
|
import { AuthCredentialTypes } from "./auth/auth_credential.js";
|
|
24
29
|
import { BaseCodeExecutor } from "./code_executors/base_code_executor.js";
|
|
25
30
|
import { BuiltInCodeExecutor } from "./code_executors/built_in_code_executor.js";
|
|
31
|
+
import { LlmSummarizer } from "./context/summarizers/llm_summarizer.js";
|
|
32
|
+
import { TokenBasedContextCompactor } from "./context/token_based_context_compactor.js";
|
|
33
|
+
import { TruncatingContextCompactor } from "./context/truncating_context_compactor.js";
|
|
34
|
+
import { isCompactedEvent } from "./events/compacted_event.js";
|
|
26
35
|
import {
|
|
27
36
|
createEvent,
|
|
28
37
|
getFunctionCalls,
|
|
@@ -53,7 +62,7 @@ import {
|
|
|
53
62
|
getAskUserConfirmationFunctionCalls
|
|
54
63
|
} from "./plugins/security_plugin.js";
|
|
55
64
|
import { InMemoryRunner } from "./runner/in_memory_runner.js";
|
|
56
|
-
import { Runner } from "./runner/runner.js";
|
|
65
|
+
import { Runner, isRunner } from "./runner/runner.js";
|
|
57
66
|
import { BaseSessionService } from "./sessions/base_session_service.js";
|
|
58
67
|
import { InMemorySessionService } from "./sessions/in_memory_session_service.js";
|
|
59
68
|
import { createSession } from "./sessions/session.js";
|
|
@@ -64,7 +73,16 @@ import { BaseToolset, isBaseToolset } from "./tools/base_toolset.js";
|
|
|
64
73
|
import { EXIT_LOOP, ExitLoopTool } from "./tools/exit_loop_tool.js";
|
|
65
74
|
import { FunctionTool, isFunctionTool } from "./tools/function_tool.js";
|
|
66
75
|
import { GOOGLE_SEARCH, GoogleSearchTool } from "./tools/google_search_tool.js";
|
|
76
|
+
import {
|
|
77
|
+
LOAD_ARTIFACTS,
|
|
78
|
+
LoadArtifactsTool
|
|
79
|
+
} from "./tools/load_artifacts_tool.js";
|
|
80
|
+
import { LOAD_MEMORY, LoadMemoryTool } from "./tools/load_memory_tool.js";
|
|
67
81
|
import { LongRunningFunctionTool } from "./tools/long_running_tool.js";
|
|
82
|
+
import {
|
|
83
|
+
PRELOAD_MEMORY,
|
|
84
|
+
PreloadMemoryTool
|
|
85
|
+
} from "./tools/preload_memory_tool.js";
|
|
68
86
|
import { ToolConfirmation } from "./tools/tool_confirmation.js";
|
|
69
87
|
import { LogLevel, getLogger, setLogLevel, setLogger } from "./utils/logger.js";
|
|
70
88
|
import { isGemini2OrAbove } from "./utils/model_name.js";
|
|
@@ -91,7 +109,10 @@ export {
|
|
|
91
109
|
BaseTool,
|
|
92
110
|
BaseToolset,
|
|
93
111
|
BuiltInCodeExecutor,
|
|
112
|
+
CONTENT_REQUEST_PROCESSOR,
|
|
113
|
+
ContentRequestProcessor,
|
|
94
114
|
Context,
|
|
115
|
+
ContextCompactorRequestProcessor,
|
|
95
116
|
EXIT_LOOP,
|
|
96
117
|
EventType,
|
|
97
118
|
ExitLoopTool,
|
|
@@ -107,15 +128,22 @@ export {
|
|
|
107
128
|
InMemorySessionService,
|
|
108
129
|
InvocationContext,
|
|
109
130
|
LLMRegistry,
|
|
131
|
+
LOAD_ARTIFACTS,
|
|
132
|
+
LOAD_MEMORY,
|
|
110
133
|
LiveRequestQueue,
|
|
111
134
|
LlmAgent,
|
|
135
|
+
LlmSummarizer,
|
|
136
|
+
LoadArtifactsTool,
|
|
137
|
+
LoadMemoryTool,
|
|
112
138
|
LogLevel,
|
|
113
139
|
LoggingPlugin,
|
|
114
140
|
LongRunningFunctionTool,
|
|
115
141
|
LoopAgent,
|
|
142
|
+
PRELOAD_MEMORY,
|
|
116
143
|
ParallelAgent,
|
|
117
144
|
PluginManager,
|
|
118
145
|
PolicyOutcome,
|
|
146
|
+
PreloadMemoryTool,
|
|
119
147
|
REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
|
|
120
148
|
ReadonlyContext,
|
|
121
149
|
Runner,
|
|
@@ -123,7 +151,9 @@ export {
|
|
|
123
151
|
SequentialAgent,
|
|
124
152
|
State,
|
|
125
153
|
StreamingMode,
|
|
154
|
+
TokenBasedContextCompactor,
|
|
126
155
|
ToolConfirmation,
|
|
156
|
+
TruncatingContextCompactor,
|
|
127
157
|
createEvent,
|
|
128
158
|
createEventActions,
|
|
129
159
|
createSession,
|
|
@@ -140,12 +170,14 @@ export {
|
|
|
140
170
|
isBaseLlm,
|
|
141
171
|
isBaseTool,
|
|
142
172
|
isBaseToolset,
|
|
173
|
+
isCompactedEvent,
|
|
143
174
|
isFinalResponse,
|
|
144
175
|
isFunctionTool,
|
|
145
176
|
isGemini2OrAbove,
|
|
146
177
|
isLlmAgent,
|
|
147
178
|
isLoopAgent,
|
|
148
179
|
isParallelAgent,
|
|
180
|
+
isRunner,
|
|
149
181
|
isSequentialAgent,
|
|
150
182
|
setLogLevel,
|
|
151
183
|
setLogger,
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
2
|
+
var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
|
|
3
|
+
/**
|
|
4
|
+
* @license
|
|
5
|
+
* Copyright 2026 Google LLC
|
|
6
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
createCompactedEvent
|
|
10
|
+
} from "../../events/compacted_event.js";
|
|
11
|
+
import { stringifyContent } from "../../events/event.js";
|
|
12
|
+
const DEFAULT_PROMPT = "The following is a conversation history between a user and an AI agent. Please summarize the conversation, focusing on key information and decisions made, as well as any unresolved questions or tasks. The summary should be concise and capture the essence of the interaction.";
|
|
13
|
+
class LlmSummarizer {
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.llm = options.llm;
|
|
16
|
+
this.prompt = options.prompt || DEFAULT_PROMPT;
|
|
17
|
+
}
|
|
18
|
+
async summarize(events) {
|
|
19
|
+
var _a, _b, _c, _d, _e, _f;
|
|
20
|
+
if (events.length === 0) {
|
|
21
|
+
throw new Error("Cannot summarize an empty list of events.");
|
|
22
|
+
}
|
|
23
|
+
const startTime = events[0].timestamp;
|
|
24
|
+
const endTime = events[events.length - 1].timestamp;
|
|
25
|
+
let formattedEvents = "";
|
|
26
|
+
for (let i = 0; i < events.length; i++) {
|
|
27
|
+
formattedEvents += "[Event ".concat(i + 1, " - Author: ").concat(events[i].author, "]\n");
|
|
28
|
+
formattedEvents += "".concat(stringifyContent(events[i]), "\n\n");
|
|
29
|
+
}
|
|
30
|
+
const fullPrompt = "".concat(this.prompt, "\n\n").concat(formattedEvents);
|
|
31
|
+
const request = {
|
|
32
|
+
contents: [{ role: "user", parts: [{ text: fullPrompt }] }],
|
|
33
|
+
toolsDict: {},
|
|
34
|
+
liveConnectConfig: {}
|
|
35
|
+
};
|
|
36
|
+
const responseGen = this.llm.generateContentAsync(request, false);
|
|
37
|
+
let compactedContent = "";
|
|
38
|
+
const firstResponse = await responseGen.next();
|
|
39
|
+
if (firstResponse.done || !((_c = (_b = (_a = firstResponse.value.content) == null ? void 0 : _a.parts) == null ? void 0 : _b[0]) == null ? void 0 : _c.text)) {
|
|
40
|
+
throw new Error("LLM failed to return a valid summary.");
|
|
41
|
+
}
|
|
42
|
+
compactedContent += firstResponse.value.content.parts[0].text;
|
|
43
|
+
try {
|
|
44
|
+
for (var iter = __forAwait(responseGen), more, temp, error; more = !(temp = await iter.next()).done; more = false) {
|
|
45
|
+
const chunk = temp.value;
|
|
46
|
+
if ((_f = (_e = (_d = chunk.content) == null ? void 0 : _d.parts) == null ? void 0 : _e[0]) == null ? void 0 : _f.text) {
|
|
47
|
+
compactedContent += chunk.content.parts[0].text;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} catch (temp) {
|
|
51
|
+
error = [temp];
|
|
52
|
+
} finally {
|
|
53
|
+
try {
|
|
54
|
+
more && (temp = iter.return) && await temp.call(iter);
|
|
55
|
+
} finally {
|
|
56
|
+
if (error)
|
|
57
|
+
throw error[0];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return createCompactedEvent({
|
|
61
|
+
author: "system",
|
|
62
|
+
content: {
|
|
63
|
+
role: "model",
|
|
64
|
+
parts: [{ text: compactedContent }]
|
|
65
|
+
},
|
|
66
|
+
startTime,
|
|
67
|
+
endTime,
|
|
68
|
+
compactedContent
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export {
|
|
73
|
+
LlmSummarizer
|
|
74
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { isCompactedEvent } from "../events/compacted_event.js";
|
|
7
|
+
import { stringifyContent } from "../events/event.js";
|
|
8
|
+
class TokenBasedContextCompactor {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.tokenThreshold = options.tokenThreshold;
|
|
11
|
+
this.eventRetentionSize = options.eventRetentionSize;
|
|
12
|
+
this.summarizer = options.summarizer;
|
|
13
|
+
}
|
|
14
|
+
getActiveEvents(events) {
|
|
15
|
+
let latestCompactedEvent = void 0;
|
|
16
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
17
|
+
const e = events[i];
|
|
18
|
+
if (isCompactedEvent(e)) {
|
|
19
|
+
if (!latestCompactedEvent || e.endTime > latestCompactedEvent.endTime) {
|
|
20
|
+
latestCompactedEvent = e;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (!latestCompactedEvent) {
|
|
25
|
+
return events;
|
|
26
|
+
}
|
|
27
|
+
const activeRawEvents = events.filter(
|
|
28
|
+
(e) => !isCompactedEvent(e) && e.timestamp > latestCompactedEvent.endTime
|
|
29
|
+
);
|
|
30
|
+
return [latestCompactedEvent, ...activeRawEvents];
|
|
31
|
+
}
|
|
32
|
+
shouldCompact(invocationContext) {
|
|
33
|
+
const events = invocationContext.session.events;
|
|
34
|
+
const activeEvents = this.getActiveEvents(events);
|
|
35
|
+
const rawEvents = activeEvents.filter((e) => !isCompactedEvent(e));
|
|
36
|
+
if (rawEvents.length <= this.eventRetentionSize) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
let totalTokens = 0;
|
|
40
|
+
for (const event of activeEvents) {
|
|
41
|
+
totalTokens += getEventTokens(event);
|
|
42
|
+
}
|
|
43
|
+
return totalTokens > this.tokenThreshold;
|
|
44
|
+
}
|
|
45
|
+
async compact(invocationContext) {
|
|
46
|
+
const events = invocationContext.session.events;
|
|
47
|
+
const activeEvents = this.getActiveEvents(events);
|
|
48
|
+
const rawEvents = activeEvents.filter((e) => !isCompactedEvent(e));
|
|
49
|
+
if (rawEvents.length <= this.eventRetentionSize) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
let retainStartIndex = Math.max(
|
|
53
|
+
0,
|
|
54
|
+
rawEvents.length - this.eventRetentionSize
|
|
55
|
+
);
|
|
56
|
+
while (retainStartIndex > 0) {
|
|
57
|
+
const eventToRetain = rawEvents[retainStartIndex];
|
|
58
|
+
const previousEvent = rawEvents[retainStartIndex - 1];
|
|
59
|
+
if (hasFunctionResponse(eventToRetain) && hasFunctionCall(previousEvent)) {
|
|
60
|
+
retainStartIndex--;
|
|
61
|
+
} else {
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (retainStartIndex === 0) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const rawEventsToCompact = rawEvents.slice(0, retainStartIndex);
|
|
69
|
+
const compactedEventPresent = activeEvents.find(isCompactedEvent);
|
|
70
|
+
const eventsToCompact = compactedEventPresent ? [compactedEventPresent, ...rawEventsToCompact] : rawEventsToCompact;
|
|
71
|
+
const compactedEvent = await this.summarizer.summarize(eventsToCompact);
|
|
72
|
+
if (!compactedEvent.actions) {
|
|
73
|
+
compactedEvent.actions = {
|
|
74
|
+
stateDelta: {},
|
|
75
|
+
artifactDelta: {},
|
|
76
|
+
requestedAuthConfigs: [],
|
|
77
|
+
requestedToolConfirmations: {}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
invocationContext.session.events.push(compactedEvent);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function getEventTokens(event) {
|
|
84
|
+
var _a;
|
|
85
|
+
if (((_a = event.usageMetadata) == null ? void 0 : _a.promptTokenCount) !== void 0) {
|
|
86
|
+
return event.usageMetadata.promptTokenCount;
|
|
87
|
+
}
|
|
88
|
+
const contentStr = stringifyContent(event);
|
|
89
|
+
return Math.ceil(contentStr.length / 4);
|
|
90
|
+
}
|
|
91
|
+
function hasFunctionCall(event) {
|
|
92
|
+
var _a, _b;
|
|
93
|
+
return !!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.some(
|
|
94
|
+
(part) => part.functionCall !== void 0
|
|
95
|
+
));
|
|
96
|
+
}
|
|
97
|
+
function hasFunctionResponse(event) {
|
|
98
|
+
var _a, _b;
|
|
99
|
+
return !!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.some(
|
|
100
|
+
(part) => part.functionResponse !== void 0
|
|
101
|
+
));
|
|
102
|
+
}
|
|
103
|
+
export {
|
|
104
|
+
TokenBasedContextCompactor
|
|
105
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
class TruncatingContextCompactor {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
var _a;
|
|
9
|
+
this.threshold = options.threshold;
|
|
10
|
+
this.preserveLeadingEvents = (_a = options.preserveLeadingEvents) != null ? _a : 0;
|
|
11
|
+
}
|
|
12
|
+
shouldCompact(invocationContext) {
|
|
13
|
+
const eventsLength = invocationContext.session.events.length;
|
|
14
|
+
return eventsLength > this.threshold + Math.max(0, this.preserveLeadingEvents);
|
|
15
|
+
}
|
|
16
|
+
compact(invocationContext) {
|
|
17
|
+
const events = invocationContext.session.events;
|
|
18
|
+
const excess = events.length - this.threshold - Math.max(0, this.preserveLeadingEvents);
|
|
19
|
+
if (excess <= 0) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const startIndexToRemove = Math.max(0, this.preserveLeadingEvents);
|
|
23
|
+
events.splice(startIndexToRemove, excess);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
TruncatingContextCompactor
|
|
28
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
/**
|
|
21
|
+
* @license
|
|
22
|
+
* Copyright 2026 Google LLC
|
|
23
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
24
|
+
*/
|
|
25
|
+
import { createEvent } from "./event.js";
|
|
26
|
+
function isCompactedEvent(event) {
|
|
27
|
+
return "isCompacted" in event && event.isCompacted === true;
|
|
28
|
+
}
|
|
29
|
+
function createCompactedEvent(params = {}) {
|
|
30
|
+
return __spreadProps(__spreadValues({}, createEvent(params)), {
|
|
31
|
+
isCompacted: params.isCompacted || true,
|
|
32
|
+
startTime: params.startTime,
|
|
33
|
+
endTime: params.endTime,
|
|
34
|
+
compactedContent: params.compactedContent
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
createCompactedEvent,
|
|
39
|
+
isCompactedEvent
|
|
40
|
+
};
|