@google/adk 1.3.0 → 1.4.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/agents/active_streaming_tool.js +6 -1
- package/dist/cjs/agents/context.js +1 -11
- package/dist/cjs/agents/functions.js +35 -16
- package/dist/cjs/agents/instructions.js +23 -5
- package/dist/cjs/agents/invocation_context.js +2 -5
- package/dist/cjs/agents/llm_agent.js +8 -6
- package/dist/cjs/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/cjs/agents/processors/content_processor_utils.js +16 -3
- package/dist/cjs/agents/processors/context_compactor_request_processor.js +4 -2
- package/dist/cjs/agents/processors/interactions_request_processor.js +63 -0
- package/dist/cjs/apps/app.js +79 -0
- package/dist/cjs/apps/resumability_config.js +44 -0
- package/dist/cjs/artifacts/file_artifact_service.js +14 -1
- package/dist/cjs/artifacts/gcs_artifact_service.js +57 -15
- package/dist/cjs/artifacts/in_memory_artifact_service.js +5 -1
- package/dist/cjs/artifacts/scoped_artifact_service.js +101 -0
- package/dist/cjs/artifacts/session_artifact_service.js +27 -0
- package/dist/cjs/auth/oauth2/oauth2_discovery.js +17 -5
- package/dist/cjs/auth/oauth2/oauth2_utils.js +12 -1
- package/dist/cjs/common.js +47 -0
- package/dist/cjs/context/agent_controlled_context_compactor.js +109 -0
- package/dist/cjs/context/token_based_context_compactor.js +42 -10
- package/dist/cjs/context/trajectory_thought_pruning_compactor.js +71 -0
- package/dist/cjs/events/event.js +24 -0
- package/dist/cjs/models/google_llm.js +25 -10
- package/dist/cjs/models/interactions_utils.js +732 -0
- package/dist/cjs/plugins/base_plugin.js +1 -0
- package/dist/cjs/runner/in_memory_runner.js +13 -2
- package/dist/cjs/runner/runner.js +93 -72
- package/dist/cjs/sessions/base_session_service.js +6 -1
- package/dist/cjs/sessions/database_session_service.js +24 -7
- package/dist/cjs/sessions/vertex_ai_session_service.js +19 -4
- package/dist/cjs/skills/loader.js +2 -2
- package/dist/cjs/tools/base_toolset.js +1 -1
- package/dist/cjs/tools/consolidate_context_tool.js +79 -0
- package/dist/cjs/tools/forwarding_artifact_service.js +16 -28
- package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +2 -12
- package/dist/cjs/utils/async_hooks_shim.js +52 -0
- package/dist/cjs/utils/client_labels.js +41 -3
- package/dist/cjs/utils/env_aware_utils.js +10 -1
- package/dist/cjs/utils/streaming_utils.js +10 -4
- package/dist/cjs/utils/task.js +62 -0
- package/dist/cjs/version.js +1 -1
- package/dist/esm/agents/active_streaming_tool.js +6 -1
- package/dist/esm/agents/context.js +1 -11
- package/dist/esm/agents/functions.js +36 -15
- package/dist/esm/agents/instructions.js +23 -5
- package/dist/esm/agents/invocation_context.js +2 -5
- package/dist/esm/agents/llm_agent.js +8 -6
- package/dist/esm/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/esm/agents/processors/content_processor_utils.js +14 -2
- package/dist/esm/agents/processors/context_compactor_request_processor.js +4 -2
- package/dist/esm/agents/processors/interactions_request_processor.js +34 -0
- package/dist/esm/apps/app.js +49 -0
- package/dist/esm/apps/resumability_config.js +16 -0
- package/dist/esm/artifacts/file_artifact_service.js +14 -1
- package/dist/esm/artifacts/gcs_artifact_service.js +57 -15
- package/dist/esm/artifacts/in_memory_artifact_service.js +5 -1
- package/dist/esm/artifacts/scoped_artifact_service.js +73 -0
- package/dist/esm/artifacts/session_artifact_service.js +7 -0
- package/dist/esm/auth/oauth2/oauth2_discovery.js +14 -4
- package/dist/esm/auth/oauth2/oauth2_utils.js +12 -1
- package/dist/esm/common.js +42 -2
- package/dist/esm/context/agent_controlled_context_compactor.js +81 -0
- package/dist/esm/context/token_based_context_compactor.js +42 -10
- package/dist/esm/context/trajectory_thought_pruning_compactor.js +43 -0
- package/dist/esm/events/event.js +22 -0
- package/dist/esm/models/google_llm.js +23 -9
- package/dist/esm/models/interactions_utils.js +697 -0
- package/dist/esm/plugins/base_plugin.js +1 -0
- package/dist/esm/runner/in_memory_runner.js +13 -2
- package/dist/esm/runner/runner.js +91 -73
- package/dist/esm/sessions/base_session_service.js +6 -1
- package/dist/esm/sessions/database_session_service.js +24 -7
- package/dist/esm/sessions/vertex_ai_session_service.js +17 -3
- package/dist/esm/skills/loader.js +2 -2
- package/dist/esm/tools/base_toolset.js +1 -1
- package/dist/esm/tools/consolidate_context_tool.js +51 -0
- package/dist/esm/tools/forwarding_artifact_service.js +16 -28
- package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +2 -12
- package/dist/esm/utils/async_hooks_shim.js +24 -0
- package/dist/esm/utils/client_labels.js +38 -2
- package/dist/esm/utils/env_aware_utils.js +10 -1
- package/dist/esm/utils/streaming_utils.js +10 -4
- package/dist/esm/utils/task.js +34 -0
- package/dist/esm/version.js +1 -1
- package/dist/types/agents/active_streaming_tool.d.ts +3 -4
- package/dist/types/agents/functions.d.ts +12 -9
- package/dist/types/agents/invocation_context.d.ts +7 -3
- package/dist/types/agents/processors/content_processor_utils.d.ts +8 -0
- package/dist/types/agents/processors/interactions_request_processor.d.ts +17 -0
- package/dist/types/apps/app.d.ts +52 -0
- package/dist/types/apps/resumability_config.d.ts +27 -0
- package/dist/types/artifacts/base_artifact_service.d.ts +6 -36
- package/dist/types/artifacts/gcs_artifact_service.d.ts +2 -1
- package/dist/types/artifacts/scoped_artifact_service.d.ts +25 -0
- package/dist/types/artifacts/session_artifact_service.d.ts +25 -0
- package/dist/types/auth/oauth2/oauth2_discovery.d.ts +11 -0
- package/dist/types/common.d.ts +20 -3
- package/dist/types/context/agent_controlled_context_compactor.d.ts +24 -0
- package/dist/types/context/base_context_compactor.d.ts +3 -0
- package/dist/types/context/token_based_context_compactor.d.ts +6 -1
- package/dist/types/context/trajectory_thought_pruning_compactor.d.ts +27 -0
- package/dist/types/events/event.d.ts +8 -0
- package/dist/types/models/google_llm.d.ts +15 -1
- package/dist/types/models/interactions_utils.d.ts +92 -0
- package/dist/types/models/llm_request.d.ts +4 -0
- package/dist/types/models/llm_response.d.ts +4 -0
- package/dist/types/plugins/base_plugin.d.ts +2 -1
- package/dist/types/runner/in_memory_runner.d.ts +7 -1
- package/dist/types/runner/runner.d.ts +43 -9
- package/dist/types/sessions/base_session_service.d.ts +3 -16
- package/dist/types/sessions/session.d.ts +11 -0
- package/dist/types/sessions/vertex_ai_session_service.d.ts +9 -1
- package/dist/types/tools/consolidate_context_tool.d.ts +19 -0
- package/dist/types/tools/forwarding_artifact_service.d.ts +3 -2
- package/dist/types/utils/async_hooks_shim.d.ts +10 -0
- package/dist/types/utils/client_labels.d.ts +10 -0
- package/dist/types/utils/env_aware_utils.d.ts +3 -3
- package/dist/types/utils/task.d.ts +24 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/agents/active_streaming_tool.js +6 -1
- package/dist/web/agents/context.js +1 -11
- package/dist/web/agents/functions.js +36 -15
- package/dist/web/agents/instructions.js +23 -5
- package/dist/web/agents/invocation_context.js +2 -5
- package/dist/web/agents/llm_agent.js +8 -6
- package/dist/web/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/web/agents/processors/content_processor_utils.js +14 -2
- package/dist/web/agents/processors/context_compactor_request_processor.js +4 -2
- package/dist/web/agents/processors/interactions_request_processor.js +52 -0
- package/dist/web/apps/app.js +49 -0
- package/dist/web/apps/resumability_config.js +31 -0
- package/dist/web/artifacts/file_artifact_service.js +14 -1
- package/dist/web/artifacts/gcs_artifact_service.js +55 -15
- package/dist/web/artifacts/in_memory_artifact_service.js +5 -1
- package/dist/web/artifacts/scoped_artifact_service.js +86 -0
- package/dist/web/artifacts/session_artifact_service.js +7 -0
- package/dist/web/auth/oauth2/oauth2_discovery.js +14 -4
- package/dist/web/auth/oauth2/oauth2_utils.js +12 -1
- package/dist/web/common.js +42 -2
- package/dist/web/context/agent_controlled_context_compactor.js +81 -0
- package/dist/web/context/token_based_context_compactor.js +42 -10
- package/dist/web/context/trajectory_thought_pruning_compactor.js +43 -0
- package/dist/web/events/event.js +20 -0
- package/dist/web/models/google_llm.js +54 -9
- package/dist/web/models/interactions_utils.js +728 -0
- package/dist/web/plugins/base_plugin.js +1 -0
- package/dist/web/runner/in_memory_runner.js +13 -2
- package/dist/web/runner/runner.js +111 -75
- package/dist/web/sessions/base_session_service.js +6 -1
- package/dist/web/sessions/database_session_service.js +24 -7
- package/dist/web/sessions/vertex_ai_session_service.js +17 -3
- package/dist/web/skills/loader.js +2 -2
- package/dist/web/tools/base_toolset.js +1 -1
- package/dist/web/tools/consolidate_context_tool.js +51 -0
- package/dist/web/tools/forwarding_artifact_service.js +16 -28
- package/dist/web/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +2 -12
- package/dist/web/utils/async_hooks_shim.js +24 -0
- package/dist/web/utils/client_labels.js +38 -2
- package/dist/web/utils/env_aware_utils.js +10 -1
- package/dist/web/utils/streaming_utils.js +10 -4
- package/dist/web/utils/task.js +34 -0
- package/dist/web/version.js +1 -1
- package/package.json +2 -2
|
@@ -6,6 +6,7 @@ const require = topLevelCreateRequire(import.meta.url);
|
|
|
6
6
|
* SPDX-License-Identifier: Apache-2.0
|
|
7
7
|
*/
|
|
8
8
|
import { logger } from "../../utils/logger.js";
|
|
9
|
+
import { validateDiscoveryUrl } from "./oauth2_discovery.js";
|
|
9
10
|
function getTokenEndpoint(authScheme) {
|
|
10
11
|
if (authScheme.type === "openIdConnect" && authScheme.tokenEndpoint) {
|
|
11
12
|
return authScheme.tokenEndpoint;
|
|
@@ -20,13 +21,23 @@ function getTokenEndpoint(authScheme) {
|
|
|
20
21
|
return void 0;
|
|
21
22
|
}
|
|
22
23
|
async function fetchOAuth2Tokens(endpoint, body) {
|
|
24
|
+
if (!validateDiscoveryUrl(endpoint)) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
"SSRF protection: OAuth2 token endpoint '".concat(endpoint, "' is not allowed. Must use HTTPS and must not target private/loopback/cloud-metadata addresses.")
|
|
27
|
+
);
|
|
28
|
+
}
|
|
23
29
|
try {
|
|
24
30
|
const response = await fetch(endpoint, {
|
|
25
31
|
method: "POST",
|
|
26
32
|
headers: {
|
|
27
33
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
28
34
|
},
|
|
29
|
-
body: body.toString()
|
|
35
|
+
body: body.toString(),
|
|
36
|
+
// Never follow redirects: the SSRF blocklist only validates `endpoint`,
|
|
37
|
+
// so a validated host that responds with a 3xx could otherwise redirect
|
|
38
|
+
// this credential-bearing POST (client_secret/refresh_token) to a
|
|
39
|
+
// private/cloud-metadata address (CWE-918).
|
|
40
|
+
redirect: "error"
|
|
30
41
|
});
|
|
31
42
|
if (!response.ok) {
|
|
32
43
|
throw new Error("Token request failed with status ".concat(response.status));
|
package/dist/web/common.js
CHANGED
|
@@ -8,7 +8,11 @@ const require = topLevelCreateRequire(import.meta.url);
|
|
|
8
8
|
import { ActiveStreamingTool } from "./agents/active_streaming_tool.js";
|
|
9
9
|
import { BaseAgent, isBaseAgent } from "./agents/base_agent.js";
|
|
10
10
|
import { Context } from "./agents/context.js";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
findEventByFunctionCallId,
|
|
13
|
+
findMatchingFunctionCall,
|
|
14
|
+
functionsExportedForTestingOnly
|
|
15
|
+
} from "./agents/functions.js";
|
|
12
16
|
import { InvocationContext } from "./agents/invocation_context.js";
|
|
13
17
|
import { LiveRequestQueue } from "./agents/live_request_queue.js";
|
|
14
18
|
import { LlmAgent, LlmAgent as LlmAgent2, isLlmAgent } from "./agents/llm_agent.js";
|
|
@@ -24,10 +28,15 @@ import {
|
|
|
24
28
|
ContentRequestProcessor
|
|
25
29
|
} from "./agents/processors/content_request_processor.js";
|
|
26
30
|
import { ContextCompactorRequestProcessor } from "./agents/processors/context_compactor_request_processor.js";
|
|
31
|
+
import {
|
|
32
|
+
INTERACTIONS_REQUEST_PROCESSOR,
|
|
33
|
+
InteractionsRequestProcessor
|
|
34
|
+
} from "./agents/processors/interactions_request_processor.js";
|
|
27
35
|
import { ReadonlyContext } from "./agents/readonly_context.js";
|
|
28
36
|
import { RoutedAgent, isRoutedAgent } from "./agents/routed_agent.js";
|
|
29
37
|
import { StreamingMode } from "./agents/run_config.js";
|
|
30
38
|
import { SequentialAgent, isSequentialAgent } from "./agents/sequential_agent.js";
|
|
39
|
+
import { createResumabilityConfig } from "./apps/resumability_config.js";
|
|
31
40
|
import { InMemoryArtifactService } from "./artifacts/in_memory_artifact_service.js";
|
|
32
41
|
import { AuthCredentialTypes } from "./auth/auth_credential.js";
|
|
33
42
|
import { AuthHandler } from "./auth/auth_handler.js";
|
|
@@ -46,8 +55,10 @@ import {
|
|
|
46
55
|
CodeExecutionLanguage,
|
|
47
56
|
FileContentEncoding
|
|
48
57
|
} from "./code_executors/code_execution_utils.js";
|
|
58
|
+
import { AgentControlledContextCompactor } from "./context/agent_controlled_context_compactor.js";
|
|
49
59
|
import { LlmSummarizer } from "./context/summarizers/llm_summarizer.js";
|
|
50
60
|
import { TokenBasedContextCompactor } from "./context/token_based_context_compactor.js";
|
|
61
|
+
import { TrajectoryThoughtPruningCompactor } from "./context/trajectory_thought_pruning_compactor.js";
|
|
51
62
|
import { TruncatingContextCompactor } from "./context/truncating_context_compactor.js";
|
|
52
63
|
import { isCompactedEvent } from "./events/compacted_event.js";
|
|
53
64
|
import {
|
|
@@ -82,7 +93,13 @@ import {
|
|
|
82
93
|
getAskUserConfirmationFunctionCalls
|
|
83
94
|
} from "./plugins/security_plugin.js";
|
|
84
95
|
import { InMemoryRunner } from "./runner/in_memory_runner.js";
|
|
85
|
-
import {
|
|
96
|
+
import {
|
|
97
|
+
Runner,
|
|
98
|
+
determineAgentForResumption,
|
|
99
|
+
findEventByLastFunctionResponseId,
|
|
100
|
+
isRoutableLlmAgent,
|
|
101
|
+
isRunner
|
|
102
|
+
} from "./runner/runner.js";
|
|
86
103
|
import { BaseSessionService } from "./sessions/base_session_service.js";
|
|
87
104
|
import { InMemorySessionService } from "./sessions/in_memory_session_service.js";
|
|
88
105
|
import { createSession } from "./sessions/session.js";
|
|
@@ -90,6 +107,7 @@ import { State } from "./sessions/state.js";
|
|
|
90
107
|
import { AgentTool, isAgentTool } from "./tools/agent_tool.js";
|
|
91
108
|
import { BaseTool, isBaseTool } from "./tools/base_tool.js";
|
|
92
109
|
import { BaseToolset, isBaseToolset } from "./tools/base_toolset.js";
|
|
110
|
+
import { ConsolidateContextTool } from "./tools/consolidate_context_tool.js";
|
|
93
111
|
import { EXIT_LOOP, ExitLoopTool } from "./tools/exit_loop_tool.js";
|
|
94
112
|
import { FunctionTool, isFunctionTool } from "./tools/function_tool.js";
|
|
95
113
|
import {
|
|
@@ -111,9 +129,11 @@ import { ToolConfirmation } from "./tools/tool_confirmation.js";
|
|
|
111
129
|
import { URL_CONTEXT, UrlContextTool } from "./tools/url_context_tool.js";
|
|
112
130
|
import { VertexAiSearchTool } from "./tools/vertex_ai_search_tool.js";
|
|
113
131
|
import { VertexRagRetrievalTool } from "./tools/vertex_rag_retrieval_tool.js";
|
|
132
|
+
import { getClientLabels, runWithClientLabel } from "./utils/client_labels.js";
|
|
114
133
|
import { LogLevel, getLogger, setLogLevel, setLogger } from "./utils/logger.js";
|
|
115
134
|
import { isGemini2OrAbove, isGemini3xFlashLive } from "./utils/model_name.js";
|
|
116
135
|
import { zodObjectToSchema } from "./utils/simple_zod_to_json.js";
|
|
136
|
+
import { Task } from "./utils/task.js";
|
|
117
137
|
import { GoogleLLMVariant } from "./utils/variant_utils.js";
|
|
118
138
|
import { version } from "./version.js";
|
|
119
139
|
import { GCPSkillRegistry } from "./skills/gcp_skill_registry.js";
|
|
@@ -128,6 +148,11 @@ import { LoadSkillResourceTool } from "./tools/skill/load_skill_resource_tool.js
|
|
|
128
148
|
import { LoadSkillTool } from "./tools/skill/load_skill_tool.js";
|
|
129
149
|
import { SearchSkillsTool } from "./tools/skill/search_skills_tool.js";
|
|
130
150
|
import { SkillToolset } from "./tools/skill/skill_toolset.js";
|
|
151
|
+
export * from "./artifacts/base_artifact_service.js";
|
|
152
|
+
export * from "./features/feature_registry.js";
|
|
153
|
+
export * from "./memory/base_memory_service.js";
|
|
154
|
+
export * from "./sessions/base_session_service.js";
|
|
155
|
+
export * from "./tools/base_tool.js";
|
|
131
156
|
import { OpenApiSpecParser } from "./tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.js";
|
|
132
157
|
import { OperationParser } from "./tools/openapi_tool/openapi_spec_parser/operation_parser.js";
|
|
133
158
|
import { ToolAuthHandler } from "./tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js";
|
|
@@ -136,6 +161,7 @@ import {
|
|
|
136
161
|
RestApiTool,
|
|
137
162
|
createRestApiTool
|
|
138
163
|
} from "./tools/openapi_tool/rest_api_tool.js";
|
|
164
|
+
export * from "./apps/app.js";
|
|
139
165
|
export * from "./artifacts/base_artifact_service.js";
|
|
140
166
|
export * from "./features/feature_registry.js";
|
|
141
167
|
export * from "./memory/base_memory_service.js";
|
|
@@ -145,6 +171,7 @@ export {
|
|
|
145
171
|
AUTH_PREPROCESSOR,
|
|
146
172
|
ActiveStreamingTool,
|
|
147
173
|
LlmAgent as Agent,
|
|
174
|
+
AgentControlledContextCompactor,
|
|
148
175
|
AgentTool,
|
|
149
176
|
AgentTransferLlmRequestProcessor,
|
|
150
177
|
ApigeeLlm,
|
|
@@ -165,6 +192,7 @@ export {
|
|
|
165
192
|
BuiltInCodeExecutor,
|
|
166
193
|
CONTENT_REQUEST_PROCESSOR,
|
|
167
194
|
CodeExecutionLanguage,
|
|
195
|
+
ConsolidateContextTool,
|
|
168
196
|
ContentRequestProcessor,
|
|
169
197
|
Context,
|
|
170
198
|
ContextCompactionTrigger,
|
|
@@ -183,12 +211,14 @@ export {
|
|
|
183
211
|
GoogleLLMVariant,
|
|
184
212
|
GoogleMapsGroundingTool,
|
|
185
213
|
GoogleSearchTool,
|
|
214
|
+
INTERACTIONS_REQUEST_PROCESSOR,
|
|
186
215
|
InMemoryArtifactService,
|
|
187
216
|
InMemoryCredentialService,
|
|
188
217
|
InMemoryMemoryService,
|
|
189
218
|
InMemoryPolicyEngine,
|
|
190
219
|
InMemoryRunner,
|
|
191
220
|
InMemorySessionService,
|
|
221
|
+
InteractionsRequestProcessor,
|
|
192
222
|
InvocationContext,
|
|
193
223
|
LLMRegistry,
|
|
194
224
|
LOAD_ARTIFACTS,
|
|
@@ -229,9 +259,11 @@ export {
|
|
|
229
259
|
SkillToolset,
|
|
230
260
|
State,
|
|
231
261
|
StreamingMode,
|
|
262
|
+
Task,
|
|
232
263
|
TokenBasedContextCompactor,
|
|
233
264
|
ToolAuthHandler,
|
|
234
265
|
ToolConfirmation,
|
|
266
|
+
TrajectoryThoughtPruningCompactor,
|
|
235
267
|
TruncatingContextCompactor,
|
|
236
268
|
URL_CONTEXT,
|
|
237
269
|
UrlContextTool,
|
|
@@ -241,10 +273,16 @@ export {
|
|
|
241
273
|
createEvent,
|
|
242
274
|
createEventActions,
|
|
243
275
|
createRestApiTool,
|
|
276
|
+
createResumabilityConfig,
|
|
244
277
|
createSession,
|
|
278
|
+
determineAgentForResumption,
|
|
279
|
+
findEventByFunctionCallId,
|
|
280
|
+
findEventByLastFunctionResponseId,
|
|
281
|
+
findMatchingFunctionCall,
|
|
245
282
|
functionsExportedForTestingOnly,
|
|
246
283
|
geminiInitParams,
|
|
247
284
|
getAskUserConfirmationFunctionCalls,
|
|
285
|
+
getClientLabels,
|
|
248
286
|
getFunctionCalls,
|
|
249
287
|
getFunctionResponses,
|
|
250
288
|
getLogger,
|
|
@@ -263,12 +301,14 @@ export {
|
|
|
263
301
|
isLlmAgent,
|
|
264
302
|
isLoopAgent,
|
|
265
303
|
isParallelAgent,
|
|
304
|
+
isRoutableLlmAgent,
|
|
266
305
|
isRoutedAgent,
|
|
267
306
|
isRunner,
|
|
268
307
|
isSequentialAgent,
|
|
269
308
|
loadAllSkillsInDir,
|
|
270
309
|
loadSkillFromDir,
|
|
271
310
|
loadSkillFromZipBuffer,
|
|
311
|
+
runWithClientLabel,
|
|
272
312
|
setLogLevel,
|
|
273
313
|
setLogger,
|
|
274
314
|
stringifyContent,
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import {createRequire as topLevelCreateRequire} from 'module';
|
|
2
|
+
const require = topLevelCreateRequire(import.meta.url);
|
|
3
|
+
/**
|
|
4
|
+
* @license
|
|
5
|
+
* Copyright 2026 Google LLC
|
|
6
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
7
|
+
*/
|
|
8
|
+
import { isCompactedEvent } from "../events/compacted_event.js";
|
|
9
|
+
import { createEvent } from "../events/event.js";
|
|
10
|
+
import { ContextCompactionTrigger } from "../plugins/base_plugin.js";
|
|
11
|
+
class AgentControlledContextCompactor {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.trigger = ContextCompactionTrigger.AgentControlled;
|
|
14
|
+
this.summarizer = options.summarizer;
|
|
15
|
+
}
|
|
16
|
+
shouldCompact(invocationContext) {
|
|
17
|
+
return invocationContext.session.state["temp:consolidate_context"] === true;
|
|
18
|
+
}
|
|
19
|
+
async compact(invocationContext) {
|
|
20
|
+
const events = invocationContext.session.events;
|
|
21
|
+
const activeEvents = this.getActiveEvents(events);
|
|
22
|
+
const consolidateToolCallIndex = activeEvents.reduce(
|
|
23
|
+
(acc, e, idx) => {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
return ((_b = (_a = e.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.some(
|
|
26
|
+
(p) => {
|
|
27
|
+
var _a2;
|
|
28
|
+
return ((_a2 = p.functionCall) == null ? void 0 : _a2.name) === "consolidate_context";
|
|
29
|
+
}
|
|
30
|
+
)) ? idx : acc;
|
|
31
|
+
},
|
|
32
|
+
-1
|
|
33
|
+
);
|
|
34
|
+
if (consolidateToolCallIndex <= 0) {
|
|
35
|
+
this.clearFlags(invocationContext);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const eventsToCompact = activeEvents.slice(0, consolidateToolCallIndex);
|
|
39
|
+
const detail = invocationContext.session.state["temp:consolidate_context_detail"];
|
|
40
|
+
if (detail) {
|
|
41
|
+
eventsToCompact.push(
|
|
42
|
+
createEvent({
|
|
43
|
+
author: "system",
|
|
44
|
+
timestamp: eventsToCompact[eventsToCompact.length - 1].timestamp,
|
|
45
|
+
content: {
|
|
46
|
+
role: "user",
|
|
47
|
+
parts: [
|
|
48
|
+
{
|
|
49
|
+
text: "CRITICAL INSTRUCTION FOR SUMMARY: Please summarize the history. Focus especially on: ".concat(detail)
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
const compactedEvent = await this.summarizer.summarize(eventsToCompact);
|
|
58
|
+
invocationContext.session.events.push(compactedEvent);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error("Compaction failed:", error);
|
|
61
|
+
} finally {
|
|
62
|
+
this.clearFlags(invocationContext);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
clearFlags(invocationContext) {
|
|
66
|
+
delete invocationContext.session.state["temp:consolidate_context"];
|
|
67
|
+
delete invocationContext.session.state["temp:consolidate_context_detail"];
|
|
68
|
+
}
|
|
69
|
+
getActiveEvents(events) {
|
|
70
|
+
const latest = events.filter(isCompactedEvent).pop();
|
|
71
|
+
return latest ? [
|
|
72
|
+
latest,
|
|
73
|
+
...events.filter(
|
|
74
|
+
(e) => !isCompactedEvent(e) && e.timestamp > latest.endTime
|
|
75
|
+
)
|
|
76
|
+
] : events;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
AgentControlledContextCompactor
|
|
81
|
+
};
|
|
@@ -5,8 +5,9 @@ const require = topLevelCreateRequire(import.meta.url);
|
|
|
5
5
|
* Copyright 2026 Google LLC
|
|
6
6
|
* SPDX-License-Identifier: Apache-2.0
|
|
7
7
|
*/
|
|
8
|
+
import { getContents } from "../agents/processors/content_processor_utils.js";
|
|
8
9
|
import { isCompactedEvent } from "../events/compacted_event.js";
|
|
9
|
-
|
|
10
|
+
const CHARS_PER_TOKEN = 4;
|
|
10
11
|
class TokenBasedContextCompactor {
|
|
11
12
|
constructor(options) {
|
|
12
13
|
this.tokenThreshold = options.tokenThreshold;
|
|
@@ -38,11 +39,14 @@ class TokenBasedContextCompactor {
|
|
|
38
39
|
if (rawEvents.length <= this.eventRetentionSize) {
|
|
39
40
|
return false;
|
|
40
41
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
const promptTokenCount = latestPromptTokenCount(
|
|
43
|
+
activeEvents,
|
|
44
|
+
invocationContext
|
|
45
|
+
);
|
|
46
|
+
if (promptTokenCount === void 0) {
|
|
47
|
+
return false;
|
|
44
48
|
}
|
|
45
|
-
return
|
|
49
|
+
return promptTokenCount > this.tokenThreshold;
|
|
46
50
|
}
|
|
47
51
|
async compact(invocationContext) {
|
|
48
52
|
const events = invocationContext.session.events;
|
|
@@ -82,13 +86,41 @@ class TokenBasedContextCompactor {
|
|
|
82
86
|
invocationContext.session.events.push(compactedEvent);
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
|
-
function
|
|
89
|
+
function latestPromptTokenCount(activeEvents, invocationContext) {
|
|
90
|
+
var _a;
|
|
91
|
+
for (let i = activeEvents.length - 1; i >= 0; i--) {
|
|
92
|
+
const count = (_a = activeEvents[i].usageMetadata) == null ? void 0 : _a.promptTokenCount;
|
|
93
|
+
if (count !== void 0) {
|
|
94
|
+
return count;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return estimatePromptTokenCount(activeEvents, invocationContext);
|
|
98
|
+
}
|
|
99
|
+
function estimatePromptTokenCount(activeEvents, invocationContext) {
|
|
86
100
|
var _a;
|
|
87
|
-
|
|
88
|
-
|
|
101
|
+
const contents = getContents(
|
|
102
|
+
activeEvents,
|
|
103
|
+
invocationContext.agent.name,
|
|
104
|
+
invocationContext.branch
|
|
105
|
+
);
|
|
106
|
+
let totalChars = 0;
|
|
107
|
+
for (const content of contents) {
|
|
108
|
+
for (const part of (_a = content.parts) != null ? _a : []) {
|
|
109
|
+
if (part.text) {
|
|
110
|
+
totalChars += part.text.length;
|
|
111
|
+
}
|
|
112
|
+
if (part.functionCall) {
|
|
113
|
+
totalChars += JSON.stringify(part.functionCall).length;
|
|
114
|
+
}
|
|
115
|
+
if (part.functionResponse) {
|
|
116
|
+
totalChars += JSON.stringify(part.functionResponse).length;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (totalChars <= 0) {
|
|
121
|
+
return void 0;
|
|
89
122
|
}
|
|
90
|
-
|
|
91
|
-
return Math.ceil(contentStr.length / 4);
|
|
123
|
+
return Math.ceil(totalChars / CHARS_PER_TOKEN);
|
|
92
124
|
}
|
|
93
125
|
function hasFunctionCall(event) {
|
|
94
126
|
var _a, _b;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {createRequire as topLevelCreateRequire} from 'module';
|
|
2
|
+
const require = topLevelCreateRequire(import.meta.url);
|
|
3
|
+
/**
|
|
4
|
+
* @license
|
|
5
|
+
* Copyright 2026 Google LLC
|
|
6
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
7
|
+
*/
|
|
8
|
+
import { hasThoughts, pruneThoughts } from "../events/event.js";
|
|
9
|
+
class TrajectoryThoughtPruningCompactor {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
if (options.eventRetentionSize < 0) {
|
|
12
|
+
throw new Error("eventRetentionSize must be a non-negative integer.");
|
|
13
|
+
}
|
|
14
|
+
this.eventRetentionSize = options.eventRetentionSize;
|
|
15
|
+
}
|
|
16
|
+
shouldCompact(invocationContext) {
|
|
17
|
+
const events = invocationContext.session.events;
|
|
18
|
+
if (events.length <= this.eventRetentionSize) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
const olderEvents = events.slice(
|
|
22
|
+
0,
|
|
23
|
+
events.length - this.eventRetentionSize
|
|
24
|
+
);
|
|
25
|
+
return olderEvents.some((event) => hasThoughts(event));
|
|
26
|
+
}
|
|
27
|
+
compact(invocationContext) {
|
|
28
|
+
const events = invocationContext.session.events;
|
|
29
|
+
if (events.length <= this.eventRetentionSize) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const pruneLimit = events.length - this.eventRetentionSize;
|
|
33
|
+
for (let i = 0; i < pruneLimit; i++) {
|
|
34
|
+
const event = events[i];
|
|
35
|
+
if (hasThoughts(event)) {
|
|
36
|
+
events[i] = pruneThoughts(event);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export {
|
|
42
|
+
TrajectoryThoughtPruningCompactor
|
|
43
|
+
};
|
package/dist/web/events/event.js
CHANGED
|
@@ -83,6 +83,24 @@ function stringifyContent(event) {
|
|
|
83
83
|
return (_a2 = part.text) != null ? _a2 : "";
|
|
84
84
|
}).join("");
|
|
85
85
|
}
|
|
86
|
+
function hasThoughts(event) {
|
|
87
|
+
var _a, _b;
|
|
88
|
+
return !!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.some((part) => part.thought === true));
|
|
89
|
+
}
|
|
90
|
+
function pruneThoughts(event) {
|
|
91
|
+
var _a;
|
|
92
|
+
if (!((_a = event.content) == null ? void 0 : _a.parts)) {
|
|
93
|
+
return event;
|
|
94
|
+
}
|
|
95
|
+
const prunedParts = event.content.parts.filter(
|
|
96
|
+
(part) => part.thought !== true
|
|
97
|
+
);
|
|
98
|
+
return __spreadProps(__spreadValues({}, event), {
|
|
99
|
+
content: __spreadProps(__spreadValues({}, event.content), {
|
|
100
|
+
parts: prunedParts
|
|
101
|
+
})
|
|
102
|
+
});
|
|
103
|
+
}
|
|
86
104
|
const ASCII_LETTERS_AND_NUMBERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
87
105
|
function createNewEventId() {
|
|
88
106
|
let id = "";
|
|
@@ -122,8 +140,10 @@ export {
|
|
|
122
140
|
createNewEventId,
|
|
123
141
|
getFunctionCalls,
|
|
124
142
|
getFunctionResponses,
|
|
143
|
+
hasThoughts,
|
|
125
144
|
hasTrailingCodeExecutionResult,
|
|
126
145
|
isFinalResponse,
|
|
146
|
+
pruneThoughts,
|
|
127
147
|
stringifyContent,
|
|
128
148
|
transformToCamelCaseEvent,
|
|
129
149
|
transformToSnakeCaseEvent
|
|
@@ -5,6 +5,9 @@ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
6
|
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
7
|
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
8
|
+
var __typeError = (msg) => {
|
|
9
|
+
throw TypeError(msg);
|
|
10
|
+
};
|
|
8
11
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
12
|
var __spreadValues = (a, b) => {
|
|
10
13
|
for (var prop in b || (b = {}))
|
|
@@ -32,12 +35,41 @@ var __asyncGenerator = (__this, __arguments, generator) => {
|
|
|
32
35
|
}, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
|
|
33
36
|
return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
|
|
34
37
|
};
|
|
38
|
+
var __yieldStar = (value) => {
|
|
39
|
+
var obj = value[__knownSymbol("asyncIterator")], isAwait = false, method, it = {};
|
|
40
|
+
if (obj == null) {
|
|
41
|
+
obj = value[__knownSymbol("iterator")]();
|
|
42
|
+
method = (k) => it[k] = (x) => obj[k](x);
|
|
43
|
+
} else {
|
|
44
|
+
obj = obj.call(value);
|
|
45
|
+
method = (k) => it[k] = (v) => {
|
|
46
|
+
if (isAwait) {
|
|
47
|
+
isAwait = false;
|
|
48
|
+
if (k === "throw") throw v;
|
|
49
|
+
return v;
|
|
50
|
+
}
|
|
51
|
+
isAwait = true;
|
|
52
|
+
return {
|
|
53
|
+
done: false,
|
|
54
|
+
value: new __await(new Promise((resolve) => {
|
|
55
|
+
var x = obj[k](v);
|
|
56
|
+
if (!(x instanceof Object)) __typeError("Object expected");
|
|
57
|
+
resolve(x);
|
|
58
|
+
}), 1)
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return it[__knownSymbol("iterator")] = () => it, method("next"), "throw" in obj ? method("throw") : it.throw = (x) => {
|
|
63
|
+
throw x;
|
|
64
|
+
}, "return" in obj && method("return"), it;
|
|
65
|
+
};
|
|
35
66
|
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);
|
|
36
67
|
/**
|
|
37
68
|
* @license
|
|
38
69
|
* Copyright 2025 Google LLC
|
|
39
70
|
* SPDX-License-Identifier: Apache-2.0
|
|
40
71
|
*/
|
|
72
|
+
var _a, _b;
|
|
41
73
|
import {
|
|
42
74
|
createPartFromText,
|
|
43
75
|
GoogleGenAI
|
|
@@ -49,8 +81,13 @@ import { AsyncQueue } from "../utils/async_queue.js";
|
|
|
49
81
|
import { StreamingResponseAggregator } from "../utils/streaming_utils.js";
|
|
50
82
|
import { BaseLlm } from "./base_llm.js";
|
|
51
83
|
import { GeminiLlmConnection } from "./gemini_llm_connection.js";
|
|
84
|
+
import { generateContentViaInteractions } from "./interactions_utils.js";
|
|
52
85
|
import { createLlmResponse } from "./llm_response.js";
|
|
53
|
-
|
|
86
|
+
const GEMINI_MODEL_SYMBOL = Symbol.for("google.adk.geminiModel");
|
|
87
|
+
function isGemini(obj) {
|
|
88
|
+
return typeof obj === "object" && obj !== null && GEMINI_MODEL_SYMBOL in obj && obj[GEMINI_MODEL_SYMBOL] === true;
|
|
89
|
+
}
|
|
90
|
+
class Gemini extends (_b = BaseLlm, _a = GEMINI_MODEL_SYMBOL, _b) {
|
|
54
91
|
/**
|
|
55
92
|
* @param params The parameters for creating a Gemini instance.
|
|
56
93
|
*/
|
|
@@ -60,12 +97,14 @@ class Gemini extends BaseLlm {
|
|
|
60
97
|
vertexai,
|
|
61
98
|
project,
|
|
62
99
|
location,
|
|
63
|
-
headers
|
|
100
|
+
headers,
|
|
101
|
+
useInteractionsApi
|
|
64
102
|
}) {
|
|
65
103
|
if (!model) {
|
|
66
104
|
model = "gemini-2.5-flash";
|
|
67
105
|
}
|
|
68
106
|
super({ model });
|
|
107
|
+
this[_a] = true;
|
|
69
108
|
const params = geminiInitParams({
|
|
70
109
|
model,
|
|
71
110
|
vertexai,
|
|
@@ -83,6 +122,7 @@ class Gemini extends BaseLlm {
|
|
|
83
122
|
this.apiKey = params.apiKey;
|
|
84
123
|
this.headers = headers;
|
|
85
124
|
this.vertexai = !!params.vertexai;
|
|
125
|
+
this.useInteractionsApi = !!useInteractionsApi;
|
|
86
126
|
}
|
|
87
127
|
/**
|
|
88
128
|
* Sends a request to the Gemini model.
|
|
@@ -93,11 +133,15 @@ class Gemini extends BaseLlm {
|
|
|
93
133
|
*/
|
|
94
134
|
generateContentAsync(llmRequest, stream = false, abortSignal) {
|
|
95
135
|
return __asyncGenerator(this, null, function* () {
|
|
96
|
-
var
|
|
136
|
+
var _a2, _b2, _c;
|
|
137
|
+
if (this.useInteractionsApi) {
|
|
138
|
+
yield* __yieldStar(generateContentViaInteractions(this.apiClient, llmRequest, stream));
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
97
141
|
this.preprocessRequest(llmRequest);
|
|
98
142
|
this.maybeAppendUserContent(llmRequest);
|
|
99
143
|
logger.info(
|
|
100
|
-
"Sending out request, model: ".concat((
|
|
144
|
+
"Sending out request, model: ".concat((_a2 = llmRequest.model) != null ? _a2 : this.model, ", backend: ").concat(this.apiBackend, ", stream: ").concat(stream)
|
|
101
145
|
);
|
|
102
146
|
if (!llmRequest.config) {
|
|
103
147
|
llmRequest.config = {};
|
|
@@ -110,7 +154,7 @@ class Gemini extends BaseLlm {
|
|
|
110
154
|
}
|
|
111
155
|
if (stream) {
|
|
112
156
|
const streamResult = yield new __await(this.apiClient.models.generateContentStream({
|
|
113
|
-
model: (
|
|
157
|
+
model: (_b2 = llmRequest.model) != null ? _b2 : this.model,
|
|
114
158
|
contents: llmRequest.contents,
|
|
115
159
|
config: llmRequest.config
|
|
116
160
|
}));
|
|
@@ -223,8 +267,8 @@ class Gemini extends BaseLlm {
|
|
|
223
267
|
* @returns BaseLlmConnection, the connection to the Gemini model.
|
|
224
268
|
*/
|
|
225
269
|
async connect(llmRequest) {
|
|
226
|
-
var
|
|
227
|
-
if ((
|
|
270
|
+
var _a2, _b2, _c, _d;
|
|
271
|
+
if ((_a2 = llmRequest.liveConnectConfig) == null ? void 0 : _a2.httpOptions) {
|
|
228
272
|
if (!llmRequest.liveConnectConfig.httpOptions.headers) {
|
|
229
273
|
llmRequest.liveConnectConfig.httpOptions.headers = {};
|
|
230
274
|
}
|
|
@@ -234,7 +278,7 @@ class Gemini extends BaseLlm {
|
|
|
234
278
|
);
|
|
235
279
|
llmRequest.liveConnectConfig.httpOptions.apiVersion = this.liveApiVersion;
|
|
236
280
|
}
|
|
237
|
-
if ((
|
|
281
|
+
if ((_b2 = llmRequest.config) == null ? void 0 : _b2.systemInstruction) {
|
|
238
282
|
llmRequest.liveConnectConfig.systemInstruction = {
|
|
239
283
|
role: "system",
|
|
240
284
|
// TODO - b/425992518: validate type casting works well.
|
|
@@ -335,5 +379,6 @@ function geminiInitParams({
|
|
|
335
379
|
}
|
|
336
380
|
export {
|
|
337
381
|
Gemini,
|
|
338
|
-
geminiInitParams
|
|
382
|
+
geminiInitParams,
|
|
383
|
+
isGemini
|
|
339
384
|
};
|