@google/adk 1.3.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/active_streaming_tool.js +6 -1
- package/dist/cjs/agents/base_agent.js +39 -0
- package/dist/cjs/agents/context.js +1 -11
- package/dist/cjs/agents/functions.js +39 -35
- package/dist/cjs/agents/instructions.js +23 -5
- package/dist/cjs/agents/invocation_context.js +2 -5
- package/dist/cjs/agents/llm_agent.js +10 -7
- package/dist/cjs/agents/loop_agent.js +15 -3
- package/dist/cjs/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/cjs/agents/processors/content_processor_utils.js +18 -4
- 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/agents/routed_agent.js +31 -0
- package/dist/cjs/agents/run_config.js +3 -2
- 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 +12 -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 +76 -0
- package/dist/cjs/context/agent_controlled_context_compactor.js +109 -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 +52 -33
- package/dist/cjs/context/trajectory_thought_pruning_compactor.js +71 -0
- package/dist/cjs/events/compacted_event.js +9 -3
- package/dist/cjs/events/event.js +52 -0
- package/dist/cjs/index.js +4 -0
- package/dist/cjs/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
- 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/plugins/global_instruction_plugin.js +97 -0
- package/dist/cjs/runner/in_memory_runner.js +13 -2
- package/dist/cjs/runner/runner.js +143 -83
- 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/enterprise_web_search_tool.js +81 -0
- package/dist/cjs/tools/example_tool.js +68 -0
- package/dist/cjs/tools/forwarding_artifact_service.js +16 -28
- 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 +15 -22
- 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/async_hooks_shim.js +52 -0
- package/dist/cjs/utils/branch_trie.js +41 -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 +15 -9
- package/dist/cjs/utils/task.js +62 -0
- 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/active_streaming_tool.js +6 -1
- package/dist/esm/agents/base_agent.js +39 -0
- package/dist/esm/agents/context.js +1 -11
- package/dist/esm/agents/functions.js +43 -32
- package/dist/esm/agents/instructions.js +23 -5
- package/dist/esm/agents/invocation_context.js +2 -5
- package/dist/esm/agents/llm_agent.js +12 -9
- package/dist/esm/agents/loop_agent.js +15 -3
- package/dist/esm/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/esm/agents/processors/content_processor_utils.js +16 -3
- 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/agents/routed_agent.js +31 -0
- package/dist/esm/agents/run_config.js +3 -2
- 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 +12 -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 +67 -3
- package/dist/esm/context/agent_controlled_context_compactor.js +81 -0
- 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 +52 -33
- package/dist/esm/context/trajectory_thought_pruning_compactor.js +43 -0
- package/dist/esm/events/compacted_event.js +7 -2
- package/dist/esm/events/event.js +46 -0
- package/dist/esm/index.js +6 -1
- package/dist/esm/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
- 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/plugins/global_instruction_plugin.js +69 -0
- package/dist/esm/runner/in_memory_runner.js +13 -2
- package/dist/esm/runner/runner.js +141 -84
- 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/enterprise_web_search_tool.js +55 -0
- package/dist/esm/tools/example_tool.js +42 -0
- package/dist/esm/tools/forwarding_artifact_service.js +16 -28
- 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 +15 -22
- 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/async_hooks_shim.js +24 -0
- package/dist/esm/utils/branch_trie.js +13 -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 +11 -5
- package/dist/esm/utils/task.js +34 -0
- 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/active_streaming_tool.d.ts +3 -4
- package/dist/types/agents/base_agent.d.ts +29 -1
- package/dist/types/agents/functions.d.ts +12 -17
- package/dist/types/agents/invocation_context.d.ts +7 -3
- package/dist/types/agents/llm_agent.d.ts +5 -1
- package/dist/types/agents/loop_agent.d.ts +2 -2
- 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/agents/routed_agent.d.ts +24 -1
- package/dist/types/agents/run_config.d.ts +1 -1
- 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 +29 -5
- package/dist/types/context/agent_controlled_context_compactor.d.ts +24 -0
- package/dist/types/context/anchored_context_compactor.d.ts +35 -0
- package/dist/types/context/base_context_compactor.d.ts +3 -0
- package/dist/types/context/compaction_utils.d.ts +15 -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/compacted_event.d.ts +10 -0
- package/dist/types/events/event.d.ts +21 -0
- package/dist/types/index.d.ts +3 -2
- 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/plugins/global_instruction_plugin.d.ts +38 -0
- package/dist/types/runner/in_memory_runner.d.ts +7 -1
- package/dist/types/runner/runner.d.ts +44 -10
- 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/enterprise_web_search_tool.d.ts +26 -0
- package/dist/types/tools/example_tool.d.ts +21 -0
- package/dist/types/tools/forwarding_artifact_service.d.ts +3 -2
- 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/async_hooks_shim.d.ts +10 -0
- package/dist/types/utils/branch_trie.d.ts +14 -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/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/active_streaming_tool.js +6 -1
- package/dist/web/agents/base_agent.js +39 -0
- package/dist/web/agents/context.js +1 -11
- package/dist/web/agents/functions.js +43 -32
- package/dist/web/agents/instructions.js +23 -5
- package/dist/web/agents/invocation_context.js +2 -5
- package/dist/web/agents/llm_agent.js +12 -9
- package/dist/web/agents/loop_agent.js +27 -3
- package/dist/web/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/web/agents/processors/content_processor_utils.js +16 -3
- 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/agents/routed_agent.js +31 -0
- package/dist/web/agents/run_config.js +8 -3
- 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 +12 -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 +67 -3
- package/dist/web/context/agent_controlled_context_compactor.js +81 -0
- 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 +52 -33
- package/dist/web/context/trajectory_thought_pruning_compactor.js +43 -0
- package/dist/web/events/compacted_event.js +7 -2
- package/dist/web/events/event.js +44 -0
- package/dist/web/index.js +6 -1
- package/dist/web/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
- 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/plugins/global_instruction_plugin.js +67 -0
- package/dist/web/runner/in_memory_runner.js +13 -2
- package/dist/web/runner/runner.js +157 -84
- 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/enterprise_web_search_tool.js +55 -0
- package/dist/web/tools/example_tool.js +42 -0
- package/dist/web/tools/forwarding_artifact_service.js +16 -28
- 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 +15 -22
- 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/async_hooks_shim.js +24 -0
- package/dist/web/utils/branch_trie.js +13 -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 +11 -5
- package/dist/web/utils/task.js +34 -0
- package/dist/web/version.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,99 @@
|
|
|
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 { isScratchpadEvent } from "../events/compacted_event.js";
|
|
9
|
+
import { getEventTokens } from "../events/event.js";
|
|
10
|
+
import { calculateRetainStartIndex } from "./compaction_utils.js";
|
|
11
|
+
class AnchoredContextCompactor {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.tokenThreshold = options.tokenThreshold;
|
|
14
|
+
this.eventRetentionSize = options.eventRetentionSize;
|
|
15
|
+
this.summarizer = options.summarizer;
|
|
16
|
+
}
|
|
17
|
+
getActiveEvents(events) {
|
|
18
|
+
let latestScratchpad = void 0;
|
|
19
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
20
|
+
const e = events[i];
|
|
21
|
+
if (isScratchpadEvent(e)) {
|
|
22
|
+
latestScratchpad = e;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (!latestScratchpad) {
|
|
27
|
+
return events;
|
|
28
|
+
}
|
|
29
|
+
const activeRawEvents = events.filter(
|
|
30
|
+
(e) => e.timestamp > latestScratchpad.endTime && !isScratchpadEvent(e)
|
|
31
|
+
);
|
|
32
|
+
return [latestScratchpad, ...activeRawEvents];
|
|
33
|
+
}
|
|
34
|
+
shouldCompact(invocationContext) {
|
|
35
|
+
const events = invocationContext.session.events;
|
|
36
|
+
const activeEvents = this.getActiveEvents(events);
|
|
37
|
+
const hasScratchpad = activeEvents.length > 0 && isScratchpadEvent(activeEvents[0]);
|
|
38
|
+
const rawEvents = hasScratchpad ? activeEvents.slice(1) : activeEvents;
|
|
39
|
+
if (rawEvents.length <= this.eventRetentionSize) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
const retainStartIndex = calculateRetainStartIndex(
|
|
43
|
+
rawEvents,
|
|
44
|
+
this.eventRetentionSize
|
|
45
|
+
);
|
|
46
|
+
if (retainStartIndex === 0) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
const totalTokens = activeEvents.reduce(
|
|
50
|
+
(sum, event) => sum + getEventTokens(event),
|
|
51
|
+
0
|
|
52
|
+
);
|
|
53
|
+
return totalTokens > this.tokenThreshold;
|
|
54
|
+
}
|
|
55
|
+
async compact(invocationContext) {
|
|
56
|
+
const events = invocationContext.session.events;
|
|
57
|
+
const activeEvents = this.getActiveEvents(events);
|
|
58
|
+
const hasScratchpad = activeEvents.length > 0 && isScratchpadEvent(activeEvents[0]);
|
|
59
|
+
const rawEvents = hasScratchpad ? activeEvents.slice(1) : activeEvents;
|
|
60
|
+
if (rawEvents.length <= this.eventRetentionSize) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const retainStartIndex = calculateRetainStartIndex(
|
|
64
|
+
rawEvents,
|
|
65
|
+
this.eventRetentionSize
|
|
66
|
+
);
|
|
67
|
+
if (retainStartIndex === 0) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const rawEventsToCompact = rawEvents.slice(0, retainStartIndex);
|
|
71
|
+
let scratchpadEvent;
|
|
72
|
+
if (hasScratchpad) {
|
|
73
|
+
const existingScratchpad = activeEvents[0];
|
|
74
|
+
scratchpadEvent = await this.summarizer.summarize([
|
|
75
|
+
existingScratchpad,
|
|
76
|
+
...rawEventsToCompact
|
|
77
|
+
]);
|
|
78
|
+
} else {
|
|
79
|
+
scratchpadEvent = await this.summarizer.summarize(rawEventsToCompact);
|
|
80
|
+
}
|
|
81
|
+
const updatedScratchpad = {
|
|
82
|
+
...scratchpadEvent,
|
|
83
|
+
isScratchpad: true,
|
|
84
|
+
author: "system"
|
|
85
|
+
};
|
|
86
|
+
const inactiveEvents = events.slice(0, events.indexOf(activeEvents[0]));
|
|
87
|
+
const retainedRawEvents = rawEvents.slice(retainStartIndex);
|
|
88
|
+
const newEventsList = [
|
|
89
|
+
...inactiveEvents,
|
|
90
|
+
updatedScratchpad,
|
|
91
|
+
...retainedRawEvents
|
|
92
|
+
];
|
|
93
|
+
events.length = 0;
|
|
94
|
+
events.push(...newEventsList);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export {
|
|
98
|
+
AnchoredContextCompactor
|
|
99
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
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 {
|
|
9
|
+
getFunctionCalls,
|
|
10
|
+
getFunctionResponses
|
|
11
|
+
} from "../events/event.js";
|
|
12
|
+
function calculateRetainStartIndex(rawEvents, eventRetentionSize) {
|
|
13
|
+
let retainStartIndex = Math.max(0, rawEvents.length - eventRetentionSize);
|
|
14
|
+
while (retainStartIndex > 0) {
|
|
15
|
+
const eventToRetain = rawEvents[retainStartIndex];
|
|
16
|
+
const previousEvent = rawEvents[retainStartIndex - 1];
|
|
17
|
+
if (getFunctionResponses(eventToRetain).length > 0 && getFunctionCalls(previousEvent).length > 0) {
|
|
18
|
+
retainStartIndex--;
|
|
19
|
+
} else {
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return retainStartIndex;
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
calculateRetainStartIndex
|
|
27
|
+
};
|
|
@@ -5,8 +5,10 @@ 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
|
-
import {
|
|
10
|
+
import { calculateRetainStartIndex } from "./compaction_utils.js";
|
|
11
|
+
const CHARS_PER_TOKEN = 4;
|
|
10
12
|
class TokenBasedContextCompactor {
|
|
11
13
|
constructor(options) {
|
|
12
14
|
this.tokenThreshold = options.tokenThreshold;
|
|
@@ -38,11 +40,21 @@ class TokenBasedContextCompactor {
|
|
|
38
40
|
if (rawEvents.length <= this.eventRetentionSize) {
|
|
39
41
|
return false;
|
|
40
42
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
const retainStartIndex = calculateRetainStartIndex(
|
|
44
|
+
rawEvents,
|
|
45
|
+
this.eventRetentionSize
|
|
46
|
+
);
|
|
47
|
+
if (retainStartIndex === 0) {
|
|
48
|
+
return false;
|
|
44
49
|
}
|
|
45
|
-
|
|
50
|
+
const promptTokenCount = latestPromptTokenCount(
|
|
51
|
+
activeEvents,
|
|
52
|
+
invocationContext
|
|
53
|
+
);
|
|
54
|
+
if (promptTokenCount === void 0) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return promptTokenCount > this.tokenThreshold;
|
|
46
58
|
}
|
|
47
59
|
async compact(invocationContext) {
|
|
48
60
|
const events = invocationContext.session.events;
|
|
@@ -51,19 +63,10 @@ class TokenBasedContextCompactor {
|
|
|
51
63
|
if (rawEvents.length <= this.eventRetentionSize) {
|
|
52
64
|
return;
|
|
53
65
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
66
|
+
const retainStartIndex = calculateRetainStartIndex(
|
|
67
|
+
rawEvents,
|
|
68
|
+
this.eventRetentionSize
|
|
57
69
|
);
|
|
58
|
-
while (retainStartIndex > 0) {
|
|
59
|
-
const eventToRetain = rawEvents[retainStartIndex];
|
|
60
|
-
const previousEvent = rawEvents[retainStartIndex - 1];
|
|
61
|
-
if (hasFunctionResponse(eventToRetain) && hasFunctionCall(previousEvent)) {
|
|
62
|
-
retainStartIndex--;
|
|
63
|
-
} else {
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
70
|
if (retainStartIndex === 0) {
|
|
68
71
|
return;
|
|
69
72
|
}
|
|
@@ -82,25 +85,41 @@ class TokenBasedContextCompactor {
|
|
|
82
85
|
invocationContext.session.events.push(compactedEvent);
|
|
83
86
|
}
|
|
84
87
|
}
|
|
85
|
-
function
|
|
88
|
+
function latestPromptTokenCount(activeEvents, invocationContext) {
|
|
86
89
|
var _a;
|
|
87
|
-
|
|
88
|
-
|
|
90
|
+
for (let i = activeEvents.length - 1; i >= 0; i--) {
|
|
91
|
+
const count = (_a = activeEvents[i].usageMetadata) == null ? void 0 : _a.promptTokenCount;
|
|
92
|
+
if (count !== void 0) {
|
|
93
|
+
return count;
|
|
94
|
+
}
|
|
89
95
|
}
|
|
90
|
-
|
|
91
|
-
return Math.ceil(contentStr.length / 4);
|
|
96
|
+
return estimatePromptTokenCount(activeEvents, invocationContext);
|
|
92
97
|
}
|
|
93
|
-
function
|
|
94
|
-
var _a
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
(part
|
|
103
|
-
|
|
98
|
+
function estimatePromptTokenCount(activeEvents, invocationContext) {
|
|
99
|
+
var _a;
|
|
100
|
+
const contents = getContents(
|
|
101
|
+
activeEvents,
|
|
102
|
+
invocationContext.agent.name,
|
|
103
|
+
invocationContext.branch
|
|
104
|
+
);
|
|
105
|
+
let totalChars = 0;
|
|
106
|
+
for (const content of contents) {
|
|
107
|
+
for (const part of (_a = content.parts) != null ? _a : []) {
|
|
108
|
+
if (part.text) {
|
|
109
|
+
totalChars += part.text.length;
|
|
110
|
+
}
|
|
111
|
+
if (part.functionCall) {
|
|
112
|
+
totalChars += JSON.stringify(part.functionCall).length;
|
|
113
|
+
}
|
|
114
|
+
if (part.functionResponse) {
|
|
115
|
+
totalChars += JSON.stringify(part.functionResponse).length;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (totalChars <= 0) {
|
|
120
|
+
return void 0;
|
|
121
|
+
}
|
|
122
|
+
return Math.ceil(totalChars / CHARS_PER_TOKEN);
|
|
104
123
|
}
|
|
105
124
|
export {
|
|
106
125
|
TokenBasedContextCompactor
|
|
@@ -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
|
+
};
|
|
@@ -9,16 +9,21 @@ import { createEvent } from "./event.js";
|
|
|
9
9
|
function isCompactedEvent(event) {
|
|
10
10
|
return "isCompacted" in event && event.isCompacted === true;
|
|
11
11
|
}
|
|
12
|
+
function isScratchpadEvent(event) {
|
|
13
|
+
return isCompactedEvent(event) && event.isScratchpad === true;
|
|
14
|
+
}
|
|
12
15
|
function createCompactedEvent(params = {}) {
|
|
13
16
|
return {
|
|
14
17
|
...createEvent(params),
|
|
15
18
|
isCompacted: params.isCompacted || true,
|
|
16
19
|
startTime: params.startTime,
|
|
17
20
|
endTime: params.endTime,
|
|
18
|
-
compactedContent: params.compactedContent
|
|
21
|
+
compactedContent: params.compactedContent,
|
|
22
|
+
isScratchpad: params.isScratchpad
|
|
19
23
|
};
|
|
20
24
|
}
|
|
21
25
|
export {
|
|
22
26
|
createCompactedEvent,
|
|
23
|
-
isCompactedEvent
|
|
27
|
+
isCompactedEvent,
|
|
28
|
+
isScratchpadEvent
|
|
24
29
|
};
|
package/dist/esm/events/event.js
CHANGED
|
@@ -5,6 +5,7 @@ const require = topLevelCreateRequire(import.meta.url);
|
|
|
5
5
|
* Copyright 2026 Google LLC
|
|
6
6
|
* SPDX-License-Identifier: Apache-2.0
|
|
7
7
|
*/
|
|
8
|
+
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
8
9
|
import { toCamelCase, toSnakeCase } from "../utils/object_notation_utils.js";
|
|
9
10
|
import { createEventActions } from "./event_actions.js";
|
|
10
11
|
function createEvent(params = {}) {
|
|
@@ -36,6 +37,17 @@ function getFunctionCalls(event) {
|
|
|
36
37
|
}
|
|
37
38
|
return funcCalls;
|
|
38
39
|
}
|
|
40
|
+
const AF_FUNCTION_CALL_ID_PREFIX = "adk-";
|
|
41
|
+
function generateClientFunctionCallId() {
|
|
42
|
+
return `${AF_FUNCTION_CALL_ID_PREFIX}${randomUUID()}`;
|
|
43
|
+
}
|
|
44
|
+
function populateClientFunctionCallId(modelResponseEvent) {
|
|
45
|
+
for (const functionCall of getFunctionCalls(modelResponseEvent)) {
|
|
46
|
+
if (!functionCall.id) {
|
|
47
|
+
functionCall.id = generateClientFunctionCallId();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
39
51
|
function getFunctionResponses(event) {
|
|
40
52
|
const funcResponses = [];
|
|
41
53
|
if (event.content && event.content.parts) {
|
|
@@ -65,6 +77,34 @@ function stringifyContent(event) {
|
|
|
65
77
|
return (_a2 = part.text) != null ? _a2 : "";
|
|
66
78
|
}).join("");
|
|
67
79
|
}
|
|
80
|
+
function getEventTokens(event) {
|
|
81
|
+
var _a;
|
|
82
|
+
if (((_a = event.usageMetadata) == null ? void 0 : _a.promptTokenCount) !== void 0) {
|
|
83
|
+
return event.usageMetadata.promptTokenCount;
|
|
84
|
+
}
|
|
85
|
+
const contentStr = stringifyContent(event);
|
|
86
|
+
return Math.ceil(contentStr.length / 4);
|
|
87
|
+
}
|
|
88
|
+
function hasThoughts(event) {
|
|
89
|
+
var _a, _b;
|
|
90
|
+
return !!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.some((part) => part.thought === true));
|
|
91
|
+
}
|
|
92
|
+
function pruneThoughts(event) {
|
|
93
|
+
var _a;
|
|
94
|
+
if (!((_a = event.content) == null ? void 0 : _a.parts)) {
|
|
95
|
+
return event;
|
|
96
|
+
}
|
|
97
|
+
const prunedParts = event.content.parts.filter(
|
|
98
|
+
(part) => part.thought !== true
|
|
99
|
+
);
|
|
100
|
+
return {
|
|
101
|
+
...event,
|
|
102
|
+
content: {
|
|
103
|
+
...event.content,
|
|
104
|
+
parts: prunedParts
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
68
108
|
const ASCII_LETTERS_AND_NUMBERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
69
109
|
function createNewEventId() {
|
|
70
110
|
let id = "";
|
|
@@ -100,12 +140,18 @@ function transformToSnakeCaseEvent(event) {
|
|
|
100
140
|
return toSnakeCase(event, PRESERVE_KEYS_CAMEL_CASE);
|
|
101
141
|
}
|
|
102
142
|
export {
|
|
143
|
+
AF_FUNCTION_CALL_ID_PREFIX,
|
|
103
144
|
createEvent,
|
|
104
145
|
createNewEventId,
|
|
146
|
+
generateClientFunctionCallId,
|
|
147
|
+
getEventTokens,
|
|
105
148
|
getFunctionCalls,
|
|
106
149
|
getFunctionResponses,
|
|
150
|
+
hasThoughts,
|
|
107
151
|
hasTrailingCodeExecutionResult,
|
|
108
152
|
isFinalResponse,
|
|
153
|
+
populateClientFunctionCallId,
|
|
154
|
+
pruneThoughts,
|
|
109
155
|
stringifyContent,
|
|
110
156
|
transformToCamelCaseEvent,
|
|
111
157
|
transformToSnakeCaseEvent
|
package/dist/esm/index.js
CHANGED
|
@@ -29,11 +29,15 @@ import {
|
|
|
29
29
|
loadSkillFromDir,
|
|
30
30
|
validateSkillDir
|
|
31
31
|
} from "./skills/loader.js";
|
|
32
|
-
import {
|
|
32
|
+
import {
|
|
33
|
+
RunSkillInlineScriptErrorCode,
|
|
34
|
+
RunSkillInlineScriptTool
|
|
35
|
+
} from "./tools/skill/run_skill_inline_script_tool.js";
|
|
33
36
|
import { RunSkillScriptTool } from "./tools/skill/run_skill_script_tool.js";
|
|
34
37
|
export * from "./integrations/agent_registry/agent_registry.js";
|
|
35
38
|
export * from "./telemetry/google_cloud.js";
|
|
36
39
|
export * from "./telemetry/setup.js";
|
|
40
|
+
export * from "./tools/mcp/load_mcp_resource_tool.js";
|
|
37
41
|
export * from "./tools/mcp/mcp_session_manager.js";
|
|
38
42
|
export * from "./tools/mcp/mcp_tool.js";
|
|
39
43
|
export * from "./tools/mcp/mcp_toolset.js";
|
|
@@ -47,6 +51,7 @@ export {
|
|
|
47
51
|
GcsArtifactService,
|
|
48
52
|
InvocationContext,
|
|
49
53
|
RemoteA2AAgent,
|
|
54
|
+
RunSkillInlineScriptErrorCode,
|
|
50
55
|
RunSkillInlineScriptTool,
|
|
51
56
|
RunSkillScriptTool,
|
|
52
57
|
UnsafeLocalCodeExecutor,
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
MCPSessionManager
|
|
11
11
|
} from "../../tools/mcp/mcp_session_manager.js";
|
|
12
12
|
import { MCPTool } from "../../tools/mcp/mcp_tool.js";
|
|
13
|
+
import { logger } from "../../utils/logger.js";
|
|
13
14
|
import { GCP_MCP_SERVER_DESTINATION_ID } from "./types.js";
|
|
14
15
|
class AgentRegistrySingleMCPToolset extends BaseToolset {
|
|
15
16
|
/**
|
|
@@ -65,7 +66,14 @@ class AgentRegistrySingleMCPToolset extends BaseToolset {
|
|
|
65
66
|
};
|
|
66
67
|
const sessionManager = new MCPSessionManager(connectionParamsCopy);
|
|
67
68
|
const session = await sessionManager.createSession();
|
|
68
|
-
|
|
69
|
+
let listResult;
|
|
70
|
+
try {
|
|
71
|
+
listResult = await session.listTools();
|
|
72
|
+
} finally {
|
|
73
|
+
await sessionManager.closeSession(session).catch((e) => {
|
|
74
|
+
logger.warn("Failed to close MCP discovery session", e);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
69
77
|
const tools = listResult.tools.map((tool) => {
|
|
70
78
|
const prefixedName = this.prefix ? `${this.prefix}_${tool.name}` : tool.name;
|
|
71
79
|
const mcpTool = new MCPTool(
|
|
@@ -5,6 +5,7 @@ const require = topLevelCreateRequire(import.meta.url);
|
|
|
5
5
|
* Copyright 2025 Google LLC
|
|
6
6
|
* SPDX-License-Identifier: Apache-2.0
|
|
7
7
|
*/
|
|
8
|
+
var _a, _b;
|
|
8
9
|
import {
|
|
9
10
|
createPartFromText,
|
|
10
11
|
GoogleGenAI
|
|
@@ -16,8 +17,13 @@ import { AsyncQueue } from "../utils/async_queue.js";
|
|
|
16
17
|
import { StreamingResponseAggregator } from "../utils/streaming_utils.js";
|
|
17
18
|
import { BaseLlm } from "./base_llm.js";
|
|
18
19
|
import { GeminiLlmConnection } from "./gemini_llm_connection.js";
|
|
20
|
+
import { generateContentViaInteractions } from "./interactions_utils.js";
|
|
19
21
|
import { createLlmResponse } from "./llm_response.js";
|
|
20
|
-
|
|
22
|
+
const GEMINI_MODEL_SYMBOL = Symbol.for("google.adk.geminiModel");
|
|
23
|
+
function isGemini(obj) {
|
|
24
|
+
return typeof obj === "object" && obj !== null && GEMINI_MODEL_SYMBOL in obj && obj[GEMINI_MODEL_SYMBOL] === true;
|
|
25
|
+
}
|
|
26
|
+
class Gemini extends (_b = BaseLlm, _a = GEMINI_MODEL_SYMBOL, _b) {
|
|
21
27
|
/**
|
|
22
28
|
* @param params The parameters for creating a Gemini instance.
|
|
23
29
|
*/
|
|
@@ -27,12 +33,14 @@ class Gemini extends BaseLlm {
|
|
|
27
33
|
vertexai,
|
|
28
34
|
project,
|
|
29
35
|
location,
|
|
30
|
-
headers
|
|
36
|
+
headers,
|
|
37
|
+
useInteractionsApi
|
|
31
38
|
}) {
|
|
32
39
|
if (!model) {
|
|
33
40
|
model = "gemini-2.5-flash";
|
|
34
41
|
}
|
|
35
42
|
super({ model });
|
|
43
|
+
this[_a] = true;
|
|
36
44
|
const params = geminiInitParams({
|
|
37
45
|
model,
|
|
38
46
|
vertexai,
|
|
@@ -50,6 +58,7 @@ class Gemini extends BaseLlm {
|
|
|
50
58
|
this.apiKey = params.apiKey;
|
|
51
59
|
this.headers = headers;
|
|
52
60
|
this.vertexai = !!params.vertexai;
|
|
61
|
+
this.useInteractionsApi = !!useInteractionsApi;
|
|
53
62
|
}
|
|
54
63
|
/**
|
|
55
64
|
* Sends a request to the Gemini model.
|
|
@@ -59,11 +68,15 @@ class Gemini extends BaseLlm {
|
|
|
59
68
|
* @yields LlmResponse: The model response.
|
|
60
69
|
*/
|
|
61
70
|
async *generateContentAsync(llmRequest, stream = false, abortSignal) {
|
|
62
|
-
var
|
|
71
|
+
var _a2, _b2, _c;
|
|
72
|
+
if (this.useInteractionsApi) {
|
|
73
|
+
yield* generateContentViaInteractions(this.apiClient, llmRequest, stream);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
63
76
|
this.preprocessRequest(llmRequest);
|
|
64
77
|
this.maybeAppendUserContent(llmRequest);
|
|
65
78
|
logger.info(
|
|
66
|
-
`Sending out request, model: ${(
|
|
79
|
+
`Sending out request, model: ${(_a2 = llmRequest.model) != null ? _a2 : this.model}, backend: ${this.apiBackend}, stream: ${stream}`
|
|
67
80
|
);
|
|
68
81
|
if (!llmRequest.config) {
|
|
69
82
|
llmRequest.config = {};
|
|
@@ -79,7 +92,7 @@ class Gemini extends BaseLlm {
|
|
|
79
92
|
}
|
|
80
93
|
if (stream) {
|
|
81
94
|
const streamResult = await this.apiClient.models.generateContentStream({
|
|
82
|
-
model: (
|
|
95
|
+
model: (_b2 = llmRequest.model) != null ? _b2 : this.model,
|
|
83
96
|
contents: llmRequest.contents,
|
|
84
97
|
config: llmRequest.config
|
|
85
98
|
});
|
|
@@ -167,8 +180,8 @@ class Gemini extends BaseLlm {
|
|
|
167
180
|
* @returns BaseLlmConnection, the connection to the Gemini model.
|
|
168
181
|
*/
|
|
169
182
|
async connect(llmRequest) {
|
|
170
|
-
var
|
|
171
|
-
if ((
|
|
183
|
+
var _a2, _b2, _c, _d;
|
|
184
|
+
if ((_a2 = llmRequest.liveConnectConfig) == null ? void 0 : _a2.httpOptions) {
|
|
172
185
|
if (!llmRequest.liveConnectConfig.httpOptions.headers) {
|
|
173
186
|
llmRequest.liveConnectConfig.httpOptions.headers = {};
|
|
174
187
|
}
|
|
@@ -178,7 +191,7 @@ class Gemini extends BaseLlm {
|
|
|
178
191
|
);
|
|
179
192
|
llmRequest.liveConnectConfig.httpOptions.apiVersion = this.liveApiVersion;
|
|
180
193
|
}
|
|
181
|
-
if ((
|
|
194
|
+
if ((_b2 = llmRequest.config) == null ? void 0 : _b2.systemInstruction) {
|
|
182
195
|
llmRequest.liveConnectConfig.systemInstruction = {
|
|
183
196
|
role: "system",
|
|
184
197
|
// TODO - b/425992518: validate type casting works well.
|
|
@@ -279,5 +292,6 @@ function geminiInitParams({
|
|
|
279
292
|
}
|
|
280
293
|
export {
|
|
281
294
|
Gemini,
|
|
282
|
-
geminiInitParams
|
|
295
|
+
geminiInitParams,
|
|
296
|
+
isGemini
|
|
283
297
|
};
|