@google/adk 1.2.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/base_agent.js +19 -1
- package/dist/cjs/agents/context.js +1 -11
- package/dist/cjs/agents/functions.js +35 -16
- package/dist/cjs/agents/instructions.js +99 -33
- package/dist/cjs/agents/invocation_context.js +2 -6
- package/dist/cjs/agents/llm_agent.js +60 -11
- package/dist/cjs/agents/processors/agent_transfer_llm_request_processor.js +7 -0
- package/dist/cjs/agents/processors/basic_llm_request_processor.js +8 -1
- package/dist/cjs/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/cjs/agents/processors/content_processor_utils.js +51 -25
- package/dist/cjs/agents/processors/content_request_processor.js +7 -0
- package/dist/cjs/agents/processors/context_compactor_request_processor.js +16 -2
- package/dist/cjs/agents/processors/identity_llm_request_processor.js +7 -0
- package/dist/cjs/agents/processors/instructions_llm_request_processor.js +5 -0
- package/dist/cjs/agents/processors/interactions_request_processor.js +63 -0
- package/dist/cjs/agents/processors/request_confirmation_llm_request_processor.js +9 -1
- package/dist/cjs/agents/processors/tool_filter_request_processor.js +7 -1
- 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/auth_preprocessor.js +12 -10
- package/dist/cjs/auth/oauth2/oauth2_discovery.js +17 -5
- package/dist/cjs/auth/oauth2/oauth2_utils.js +12 -1
- package/dist/cjs/code_executors/code_executor_context.js +7 -7
- package/dist/cjs/common.js +86 -0
- package/dist/cjs/context/agent_controlled_context_compactor.js +109 -0
- package/dist/cjs/context/summarizers/llm_summarizer.js +11 -0
- package/dist/cjs/context/token_based_context_compactor.js +43 -11
- package/dist/cjs/context/trajectory_thought_pruning_compactor.js +71 -0
- package/dist/cjs/events/event.js +25 -1
- package/dist/cjs/integrations/agent_registry/agent_registry_mcp_toolset.js +21 -2
- package/dist/cjs/models/gemini_llm_connection.js +62 -19
- package/dist/cjs/models/google_llm.js +51 -18
- package/dist/cjs/models/interactions_utils.js +732 -0
- package/dist/cjs/plugins/base_plugin.js +1 -0
- package/dist/cjs/plugins/security_plugin.js +16 -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 +19 -3
- package/dist/cjs/sessions/database_session_service.js +26 -9
- package/dist/cjs/sessions/db/schema.js +45 -10
- package/dist/cjs/sessions/in_memory_session_service.js +2 -1
- package/dist/cjs/sessions/vertex_ai_session_service.js +21 -5
- package/dist/cjs/skills/gcp_skill_registry.js +104 -0
- package/dist/cjs/skills/loader.js +67 -2
- package/dist/cjs/skills/skill_registry.js +27 -0
- 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/openapi_spec_parser.js +232 -0
- package/dist/cjs/tools/openapi_tool/openapi_spec_parser/operation_parser.js +224 -0
- package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +117 -0
- package/dist/cjs/tools/openapi_tool/openapi_toolset.js +133 -0
- package/dist/cjs/tools/openapi_tool/rest_api_tool.js +256 -0
- package/dist/cjs/tools/skill/load_skill_resource_tool.js +25 -3
- package/dist/cjs/tools/skill/load_skill_tool.js +12 -1
- package/dist/cjs/tools/skill/run_skill_script_tool.js +12 -1
- package/dist/cjs/tools/skill/search_skills_tool.js +108 -0
- package/dist/cjs/tools/skill/skill_toolset.js +40 -5
- package/dist/cjs/utils/async_hooks_shim.js +52 -0
- package/dist/cjs/utils/async_queue.js +88 -0
- package/dist/cjs/utils/case_utils.js +53 -0
- package/dist/cjs/utils/client_labels.js +41 -3
- package/dist/cjs/utils/env_aware_utils.js +14 -1
- package/dist/cjs/utils/live_connection_utils.js +284 -0
- package/dist/cjs/utils/model_name.js +9 -0
- package/dist/cjs/utils/streaming_utils.js +18 -11
- 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/base_agent.js +19 -1
- package/dist/esm/agents/context.js +1 -11
- package/dist/esm/agents/functions.js +36 -15
- package/dist/esm/agents/instructions.js +99 -33
- package/dist/esm/agents/invocation_context.js +2 -6
- package/dist/esm/agents/llm_agent.js +61 -11
- package/dist/esm/agents/processors/agent_transfer_llm_request_processor.js +7 -0
- package/dist/esm/agents/processors/basic_llm_request_processor.js +8 -1
- package/dist/esm/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/esm/agents/processors/content_processor_utils.js +48 -24
- package/dist/esm/agents/processors/content_request_processor.js +7 -0
- package/dist/esm/agents/processors/context_compactor_request_processor.js +16 -2
- package/dist/esm/agents/processors/identity_llm_request_processor.js +7 -0
- package/dist/esm/agents/processors/instructions_llm_request_processor.js +5 -0
- package/dist/esm/agents/processors/interactions_request_processor.js +34 -0
- package/dist/esm/agents/processors/request_confirmation_llm_request_processor.js +9 -1
- package/dist/esm/agents/processors/tool_filter_request_processor.js +7 -1
- 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/auth_preprocessor.js +12 -10
- package/dist/esm/auth/oauth2/oauth2_discovery.js +14 -4
- package/dist/esm/auth/oauth2/oauth2_utils.js +12 -1
- package/dist/esm/code_executors/code_executor_context.js +7 -7
- package/dist/esm/common.js +75 -3
- package/dist/esm/context/agent_controlled_context_compactor.js +81 -0
- package/dist/esm/context/summarizers/llm_summarizer.js +11 -0
- package/dist/esm/context/token_based_context_compactor.js +43 -11
- package/dist/esm/context/trajectory_thought_pruning_compactor.js +43 -0
- package/dist/esm/events/event.js +23 -1
- package/dist/esm/integrations/agent_registry/agent_registry_mcp_toolset.js +21 -2
- package/dist/esm/models/gemini_llm_connection.js +62 -19
- package/dist/esm/models/google_llm.js +49 -17
- package/dist/esm/models/interactions_utils.js +697 -0
- package/dist/esm/plugins/base_plugin.js +1 -0
- package/dist/esm/plugins/security_plugin.js +16 -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 +17 -2
- package/dist/esm/sessions/database_session_service.js +26 -9
- package/dist/esm/sessions/db/schema.js +44 -10
- package/dist/esm/sessions/in_memory_session_service.js +4 -2
- package/dist/esm/sessions/vertex_ai_session_service.js +21 -5
- package/dist/esm/skills/gcp_skill_registry.js +78 -0
- package/dist/esm/skills/loader.js +66 -2
- package/dist/esm/skills/skill_registry.js +7 -0
- 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/openapi_spec_parser.js +206 -0
- package/dist/esm/tools/openapi_tool/openapi_spec_parser/operation_parser.js +198 -0
- package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +91 -0
- package/dist/esm/tools/openapi_tool/openapi_toolset.js +97 -0
- package/dist/esm/tools/openapi_tool/rest_api_tool.js +229 -0
- package/dist/esm/tools/skill/load_skill_resource_tool.js +25 -3
- package/dist/esm/tools/skill/load_skill_tool.js +12 -1
- package/dist/esm/tools/skill/run_skill_script_tool.js +12 -1
- package/dist/esm/tools/skill/search_skills_tool.js +82 -0
- package/dist/esm/tools/skill/skill_toolset.js +40 -5
- package/dist/esm/utils/async_hooks_shim.js +24 -0
- package/dist/esm/utils/async_queue.js +60 -0
- package/dist/esm/utils/case_utils.js +25 -0
- package/dist/esm/utils/client_labels.js +38 -2
- package/dist/esm/utils/env_aware_utils.js +14 -1
- package/dist/esm/utils/live_connection_utils.js +256 -0
- package/dist/esm/utils/model_name.js +8 -0
- package/dist/esm/utils/streaming_utils.js +18 -11
- package/dist/esm/utils/task.js +34 -0
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_remote_agent_utils.d.ts +29 -4
- package/dist/types/a2a/event_converter_utils.d.ts +14 -1
- package/dist/types/a2a/metadata_converter_utils.d.ts +18 -0
- package/dist/types/a2a/part_converter_utils.d.ts +51 -6
- 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 -12
- package/dist/types/agents/llm_agent.d.ts +14 -0
- package/dist/types/agents/processors/agent_transfer_llm_request_processor.d.ts +14 -0
- package/dist/types/agents/processors/basic_llm_request_processor.d.ts +7 -0
- package/dist/types/agents/processors/content_processor_utils.d.ts +34 -0
- package/dist/types/agents/processors/content_request_processor.d.ts +16 -0
- package/dist/types/agents/processors/context_compactor_request_processor.d.ts +12 -0
- package/dist/types/agents/processors/identity_llm_request_processor.d.ts +7 -0
- package/dist/types/agents/processors/interactions_request_processor.d.ts +17 -0
- package/dist/types/agents/processors/request_confirmation_llm_request_processor.d.ts +9 -1
- package/dist/types/agents/processors/tool_filter_request_processor.d.ts +12 -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 +35 -4
- 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/summarizers/llm_summarizer.d.ts +17 -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/events/event_actions.d.ts +1 -2
- package/dist/types/integrations/agent_registry/agent_registry_mcp_toolset.d.ts +21 -2
- package/dist/types/models/base_llm_connection.d.ts +10 -0
- package/dist/types/models/gemini_llm_connection.d.ts +15 -4
- 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 +14 -1
- package/dist/types/plugins/base_plugin.d.ts +2 -1
- package/dist/types/plugins/security_plugin.d.ts +30 -0
- 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 +7 -16
- package/dist/types/sessions/db/schema.d.ts +1 -0
- package/dist/types/sessions/session.d.ts +11 -0
- package/dist/types/sessions/vertex_ai_session_service.d.ts +9 -1
- package/dist/types/skills/gcp_skill_registry.d.ts +24 -0
- package/dist/types/skills/loader.d.ts +7 -0
- package/dist/types/skills/skill.d.ts +1 -1
- package/dist/types/skills/skill_registry.d.ts +29 -0
- 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/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.d.ts +34 -0
- package/dist/types/tools/openapi_tool/openapi_spec_parser/operation_parser.d.ts +59 -0
- package/dist/types/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.d.ts +23 -0
- package/dist/types/tools/openapi_tool/openapi_toolset.d.ts +27 -0
- package/dist/types/tools/openapi_tool/rest_api_tool.d.ts +50 -0
- package/dist/types/tools/skill/load_skill_resource_tool.d.ts +1 -1
- package/dist/types/tools/skill/search_skills_tool.d.ts +14 -0
- package/dist/types/tools/skill/skill_toolset.d.ts +5 -0
- package/dist/types/utils/async_hooks_shim.d.ts +10 -0
- package/dist/types/utils/async_queue.d.ts +18 -0
- package/dist/types/utils/case_utils.d.ts +12 -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/live_connection_utils.d.ts +30 -0
- package/dist/types/utils/model_name.d.ts +7 -0
- package/dist/types/utils/streaming_utils.d.ts +0 -1
- 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/base_agent.js +31 -1
- package/dist/web/agents/context.js +1 -11
- package/dist/web/agents/functions.js +36 -15
- package/dist/web/agents/instructions.js +99 -33
- package/dist/web/agents/invocation_context.js +2 -6
- package/dist/web/agents/llm_agent.js +61 -11
- package/dist/web/agents/processors/agent_transfer_llm_request_processor.js +7 -0
- package/dist/web/agents/processors/basic_llm_request_processor.js +8 -1
- package/dist/web/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/web/agents/processors/content_processor_utils.js +66 -24
- package/dist/web/agents/processors/content_request_processor.js +7 -0
- package/dist/web/agents/processors/context_compactor_request_processor.js +16 -2
- package/dist/web/agents/processors/identity_llm_request_processor.js +7 -0
- package/dist/web/agents/processors/instructions_llm_request_processor.js +5 -0
- package/dist/web/agents/processors/interactions_request_processor.js +52 -0
- package/dist/web/agents/processors/request_confirmation_llm_request_processor.js +9 -1
- package/dist/web/agents/processors/tool_filter_request_processor.js +7 -1
- 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/auth_preprocessor.js +12 -10
- package/dist/web/auth/oauth2/oauth2_discovery.js +14 -4
- package/dist/web/auth/oauth2/oauth2_utils.js +12 -1
- package/dist/web/code_executors/code_executor_context.js +7 -7
- package/dist/web/common.js +75 -3
- package/dist/web/context/agent_controlled_context_compactor.js +81 -0
- package/dist/web/context/summarizers/llm_summarizer.js +11 -0
- package/dist/web/context/token_based_context_compactor.js +43 -11
- package/dist/web/context/trajectory_thought_pruning_compactor.js +43 -0
- package/dist/web/events/event.js +21 -1
- package/dist/web/integrations/agent_registry/agent_registry_mcp_toolset.js +21 -2
- package/dist/web/models/gemini_llm_connection.js +75 -19
- package/dist/web/models/google_llm.js +80 -17
- package/dist/web/models/interactions_utils.js +728 -0
- package/dist/web/plugins/base_plugin.js +1 -0
- package/dist/web/plugins/security_plugin.js +16 -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 +17 -2
- package/dist/web/sessions/database_session_service.js +26 -9
- package/dist/web/sessions/db/schema.js +44 -10
- package/dist/web/sessions/in_memory_session_service.js +4 -2
- package/dist/web/sessions/vertex_ai_session_service.js +21 -5
- package/dist/web/skills/gcp_skill_registry.js +78 -0
- package/dist/web/skills/loader.js +66 -2
- package/dist/web/skills/skill_registry.js +7 -0
- 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/openapi_spec_parser.js +221 -0
- package/dist/web/tools/openapi_tool/openapi_spec_parser/operation_parser.js +198 -0
- package/dist/web/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +91 -0
- package/dist/web/tools/openapi_tool/openapi_toolset.js +97 -0
- package/dist/web/tools/openapi_tool/rest_api_tool.js +229 -0
- package/dist/web/tools/skill/load_skill_resource_tool.js +25 -3
- package/dist/web/tools/skill/load_skill_tool.js +12 -1
- package/dist/web/tools/skill/run_skill_script_tool.js +12 -1
- package/dist/web/tools/skill/search_skills_tool.js +82 -0
- package/dist/web/tools/skill/skill_toolset.js +40 -5
- package/dist/web/utils/async_hooks_shim.js +24 -0
- package/dist/web/utils/async_queue.js +60 -0
- package/dist/web/utils/case_utils.js +25 -0
- package/dist/web/utils/client_labels.js +38 -2
- package/dist/web/utils/env_aware_utils.js +14 -1
- package/dist/web/utils/live_connection_utils.js +254 -0
- package/dist/web/utils/model_name.js +8 -0
- package/dist/web/utils/streaming_utils.js +18 -11
- package/dist/web/utils/task.js +34 -0
- package/dist/web/version.js +1 -1
- package/package.json +4 -2
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
for (var name in all)
|
|
14
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
var agent_controlled_context_compactor_exports = {};
|
|
26
|
+
__export(agent_controlled_context_compactor_exports, {
|
|
27
|
+
AgentControlledContextCompactor: () => AgentControlledContextCompactor
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(agent_controlled_context_compactor_exports);
|
|
30
|
+
var import_compacted_event = require("../events/compacted_event.js");
|
|
31
|
+
var import_event = require("../events/event.js");
|
|
32
|
+
var import_base_plugin = require("../plugins/base_plugin.js");
|
|
33
|
+
/**
|
|
34
|
+
* @license
|
|
35
|
+
* Copyright 2026 Google LLC
|
|
36
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
37
|
+
*/
|
|
38
|
+
class AgentControlledContextCompactor {
|
|
39
|
+
constructor(options) {
|
|
40
|
+
this.trigger = import_base_plugin.ContextCompactionTrigger.AgentControlled;
|
|
41
|
+
this.summarizer = options.summarizer;
|
|
42
|
+
}
|
|
43
|
+
shouldCompact(invocationContext) {
|
|
44
|
+
return invocationContext.session.state["temp:consolidate_context"] === true;
|
|
45
|
+
}
|
|
46
|
+
async compact(invocationContext) {
|
|
47
|
+
const events = invocationContext.session.events;
|
|
48
|
+
const activeEvents = this.getActiveEvents(events);
|
|
49
|
+
const consolidateToolCallIndex = activeEvents.reduce(
|
|
50
|
+
(acc, e, idx) => {
|
|
51
|
+
var _a, _b;
|
|
52
|
+
return ((_b = (_a = e.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.some(
|
|
53
|
+
(p) => {
|
|
54
|
+
var _a2;
|
|
55
|
+
return ((_a2 = p.functionCall) == null ? void 0 : _a2.name) === "consolidate_context";
|
|
56
|
+
}
|
|
57
|
+
)) ? idx : acc;
|
|
58
|
+
},
|
|
59
|
+
-1
|
|
60
|
+
);
|
|
61
|
+
if (consolidateToolCallIndex <= 0) {
|
|
62
|
+
this.clearFlags(invocationContext);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const eventsToCompact = activeEvents.slice(0, consolidateToolCallIndex);
|
|
66
|
+
const detail = invocationContext.session.state["temp:consolidate_context_detail"];
|
|
67
|
+
if (detail) {
|
|
68
|
+
eventsToCompact.push(
|
|
69
|
+
(0, import_event.createEvent)({
|
|
70
|
+
author: "system",
|
|
71
|
+
timestamp: eventsToCompact[eventsToCompact.length - 1].timestamp,
|
|
72
|
+
content: {
|
|
73
|
+
role: "user",
|
|
74
|
+
parts: [
|
|
75
|
+
{
|
|
76
|
+
text: `CRITICAL INSTRUCTION FOR SUMMARY: Please summarize the history. Focus especially on: ${detail}`
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
const compactedEvent = await this.summarizer.summarize(eventsToCompact);
|
|
85
|
+
invocationContext.session.events.push(compactedEvent);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error("Compaction failed:", error);
|
|
88
|
+
} finally {
|
|
89
|
+
this.clearFlags(invocationContext);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
clearFlags(invocationContext) {
|
|
93
|
+
delete invocationContext.session.state["temp:consolidate_context"];
|
|
94
|
+
delete invocationContext.session.state["temp:consolidate_context_detail"];
|
|
95
|
+
}
|
|
96
|
+
getActiveEvents(events) {
|
|
97
|
+
const latest = events.filter(import_compacted_event.isCompactedEvent).pop();
|
|
98
|
+
return latest ? [
|
|
99
|
+
latest,
|
|
100
|
+
...events.filter(
|
|
101
|
+
(e) => !(0, import_compacted_event.isCompactedEvent)(e) && e.timestamp > latest.endTime
|
|
102
|
+
)
|
|
103
|
+
] : events;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
107
|
+
0 && (module.exports = {
|
|
108
|
+
AgentControlledContextCompactor
|
|
109
|
+
});
|
|
@@ -36,10 +36,21 @@ var import_event = require("../../events/event.js");
|
|
|
36
36
|
*/
|
|
37
37
|
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.";
|
|
38
38
|
class LlmSummarizer {
|
|
39
|
+
/**
|
|
40
|
+
* @param options - Configuration specifying the LLM and optional prompt.
|
|
41
|
+
*/
|
|
39
42
|
constructor(options) {
|
|
40
43
|
this.llm = options.llm;
|
|
41
44
|
this.prompt = options.prompt || DEFAULT_PROMPT;
|
|
42
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Summarizes a list of events into a single {@link CompactedEvent} using the
|
|
48
|
+
* configured LLM.
|
|
49
|
+
*
|
|
50
|
+
* @param events - The events to summarize. Must be non-empty.
|
|
51
|
+
* @returns A promise resolving to the compacted representation.
|
|
52
|
+
* @throws {Error} If `events` is empty or the LLM returns no content.
|
|
53
|
+
*/
|
|
43
54
|
async summarize(events) {
|
|
44
55
|
var _a, _b, _c, _d, _e, _f;
|
|
45
56
|
if (events.length === 0) {
|
|
@@ -27,13 +27,14 @@ __export(token_based_context_compactor_exports, {
|
|
|
27
27
|
TokenBasedContextCompactor: () => TokenBasedContextCompactor
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(token_based_context_compactor_exports);
|
|
30
|
+
var import_content_processor_utils = require("../agents/processors/content_processor_utils.js");
|
|
30
31
|
var import_compacted_event = require("../events/compacted_event.js");
|
|
31
|
-
var import_event = require("../events/event.js");
|
|
32
32
|
/**
|
|
33
33
|
* @license
|
|
34
34
|
* Copyright 2026 Google LLC
|
|
35
35
|
* SPDX-License-Identifier: Apache-2.0
|
|
36
36
|
*/
|
|
37
|
+
const CHARS_PER_TOKEN = 4;
|
|
37
38
|
class TokenBasedContextCompactor {
|
|
38
39
|
constructor(options) {
|
|
39
40
|
this.tokenThreshold = options.tokenThreshold;
|
|
@@ -65,11 +66,14 @@ class TokenBasedContextCompactor {
|
|
|
65
66
|
if (rawEvents.length <= this.eventRetentionSize) {
|
|
66
67
|
return false;
|
|
67
68
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
const promptTokenCount = latestPromptTokenCount(
|
|
70
|
+
activeEvents,
|
|
71
|
+
invocationContext
|
|
72
|
+
);
|
|
73
|
+
if (promptTokenCount === void 0) {
|
|
74
|
+
return false;
|
|
71
75
|
}
|
|
72
|
-
return
|
|
76
|
+
return promptTokenCount > this.tokenThreshold;
|
|
73
77
|
}
|
|
74
78
|
async compact(invocationContext) {
|
|
75
79
|
const events = invocationContext.session.events;
|
|
@@ -102,20 +106,48 @@ class TokenBasedContextCompactor {
|
|
|
102
106
|
compactedEvent.actions = {
|
|
103
107
|
stateDelta: {},
|
|
104
108
|
artifactDelta: {},
|
|
105
|
-
requestedAuthConfigs:
|
|
109
|
+
requestedAuthConfigs: {},
|
|
106
110
|
requestedToolConfirmations: {}
|
|
107
111
|
};
|
|
108
112
|
}
|
|
109
113
|
invocationContext.session.events.push(compactedEvent);
|
|
110
114
|
}
|
|
111
115
|
}
|
|
112
|
-
function
|
|
116
|
+
function latestPromptTokenCount(activeEvents, invocationContext) {
|
|
117
|
+
var _a;
|
|
118
|
+
for (let i = activeEvents.length - 1; i >= 0; i--) {
|
|
119
|
+
const count = (_a = activeEvents[i].usageMetadata) == null ? void 0 : _a.promptTokenCount;
|
|
120
|
+
if (count !== void 0) {
|
|
121
|
+
return count;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return estimatePromptTokenCount(activeEvents, invocationContext);
|
|
125
|
+
}
|
|
126
|
+
function estimatePromptTokenCount(activeEvents, invocationContext) {
|
|
113
127
|
var _a;
|
|
114
|
-
|
|
115
|
-
|
|
128
|
+
const contents = (0, import_content_processor_utils.getContents)(
|
|
129
|
+
activeEvents,
|
|
130
|
+
invocationContext.agent.name,
|
|
131
|
+
invocationContext.branch
|
|
132
|
+
);
|
|
133
|
+
let totalChars = 0;
|
|
134
|
+
for (const content of contents) {
|
|
135
|
+
for (const part of (_a = content.parts) != null ? _a : []) {
|
|
136
|
+
if (part.text) {
|
|
137
|
+
totalChars += part.text.length;
|
|
138
|
+
}
|
|
139
|
+
if (part.functionCall) {
|
|
140
|
+
totalChars += JSON.stringify(part.functionCall).length;
|
|
141
|
+
}
|
|
142
|
+
if (part.functionResponse) {
|
|
143
|
+
totalChars += JSON.stringify(part.functionResponse).length;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (totalChars <= 0) {
|
|
148
|
+
return void 0;
|
|
116
149
|
}
|
|
117
|
-
|
|
118
|
-
return Math.ceil(contentStr.length / 4);
|
|
150
|
+
return Math.ceil(totalChars / CHARS_PER_TOKEN);
|
|
119
151
|
}
|
|
120
152
|
function hasFunctionCall(event) {
|
|
121
153
|
var _a, _b;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
"use strict";
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
for (var name in all)
|
|
14
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
var trajectory_thought_pruning_compactor_exports = {};
|
|
26
|
+
__export(trajectory_thought_pruning_compactor_exports, {
|
|
27
|
+
TrajectoryThoughtPruningCompactor: () => TrajectoryThoughtPruningCompactor
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(trajectory_thought_pruning_compactor_exports);
|
|
30
|
+
var import_event = require("../events/event.js");
|
|
31
|
+
/**
|
|
32
|
+
* @license
|
|
33
|
+
* Copyright 2026 Google LLC
|
|
34
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
35
|
+
*/
|
|
36
|
+
class TrajectoryThoughtPruningCompactor {
|
|
37
|
+
constructor(options) {
|
|
38
|
+
if (options.eventRetentionSize < 0) {
|
|
39
|
+
throw new Error("eventRetentionSize must be a non-negative integer.");
|
|
40
|
+
}
|
|
41
|
+
this.eventRetentionSize = options.eventRetentionSize;
|
|
42
|
+
}
|
|
43
|
+
shouldCompact(invocationContext) {
|
|
44
|
+
const events = invocationContext.session.events;
|
|
45
|
+
if (events.length <= this.eventRetentionSize) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const olderEvents = events.slice(
|
|
49
|
+
0,
|
|
50
|
+
events.length - this.eventRetentionSize
|
|
51
|
+
);
|
|
52
|
+
return olderEvents.some((event) => (0, import_event.hasThoughts)(event));
|
|
53
|
+
}
|
|
54
|
+
compact(invocationContext) {
|
|
55
|
+
const events = invocationContext.session.events;
|
|
56
|
+
if (events.length <= this.eventRetentionSize) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const pruneLimit = events.length - this.eventRetentionSize;
|
|
60
|
+
for (let i = 0; i < pruneLimit; i++) {
|
|
61
|
+
const event = events[i];
|
|
62
|
+
if ((0, import_event.hasThoughts)(event)) {
|
|
63
|
+
events[i] = (0, import_event.pruneThoughts)(event);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
+
0 && (module.exports = {
|
|
70
|
+
TrajectoryThoughtPruningCompactor
|
|
71
|
+
});
|
package/dist/cjs/events/event.js
CHANGED
|
@@ -28,8 +28,10 @@ __export(event_exports, {
|
|
|
28
28
|
createNewEventId: () => createNewEventId,
|
|
29
29
|
getFunctionCalls: () => getFunctionCalls,
|
|
30
30
|
getFunctionResponses: () => getFunctionResponses,
|
|
31
|
+
hasThoughts: () => hasThoughts,
|
|
31
32
|
hasTrailingCodeExecutionResult: () => hasTrailingCodeExecutionResult,
|
|
32
33
|
isFinalResponse: () => isFinalResponse,
|
|
34
|
+
pruneThoughts: () => pruneThoughts,
|
|
33
35
|
stringifyContent: () => stringifyContent,
|
|
34
36
|
transformToCamelCaseEvent: () => transformToCamelCaseEvent,
|
|
35
37
|
transformToSnakeCaseEvent: () => transformToSnakeCaseEvent
|
|
@@ -55,7 +57,7 @@ function createEvent(params = {}) {
|
|
|
55
57
|
};
|
|
56
58
|
}
|
|
57
59
|
function isFinalResponse(event) {
|
|
58
|
-
if (event.actions.skipSummarization || event.longRunningToolIds && event.longRunningToolIds.length > 0) {
|
|
60
|
+
if (event.actions.skipSummarization || event.longRunningToolIds && event.longRunningToolIds.length > 0 || event.actions.requestedAuthConfigs && Object.keys(event.actions.requestedAuthConfigs).length > 0) {
|
|
59
61
|
return true;
|
|
60
62
|
}
|
|
61
63
|
return getFunctionCalls(event).length === 0 && getFunctionResponses(event).length === 0 && !event.partial && !hasTrailingCodeExecutionResult(event);
|
|
@@ -100,6 +102,26 @@ function stringifyContent(event) {
|
|
|
100
102
|
return (_a2 = part.text) != null ? _a2 : "";
|
|
101
103
|
}).join("");
|
|
102
104
|
}
|
|
105
|
+
function hasThoughts(event) {
|
|
106
|
+
var _a, _b;
|
|
107
|
+
return !!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.some((part) => part.thought === true));
|
|
108
|
+
}
|
|
109
|
+
function pruneThoughts(event) {
|
|
110
|
+
var _a;
|
|
111
|
+
if (!((_a = event.content) == null ? void 0 : _a.parts)) {
|
|
112
|
+
return event;
|
|
113
|
+
}
|
|
114
|
+
const prunedParts = event.content.parts.filter(
|
|
115
|
+
(part) => part.thought !== true
|
|
116
|
+
);
|
|
117
|
+
return {
|
|
118
|
+
...event,
|
|
119
|
+
content: {
|
|
120
|
+
...event.content,
|
|
121
|
+
parts: prunedParts
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
103
125
|
const ASCII_LETTERS_AND_NUMBERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
104
126
|
function createNewEventId() {
|
|
105
127
|
let id = "";
|
|
@@ -140,8 +162,10 @@ function transformToSnakeCaseEvent(event) {
|
|
|
140
162
|
createNewEventId,
|
|
141
163
|
getFunctionCalls,
|
|
142
164
|
getFunctionResponses,
|
|
165
|
+
hasThoughts,
|
|
143
166
|
hasTrailingCodeExecutionResult,
|
|
144
167
|
isFinalResponse,
|
|
168
|
+
pruneThoughts,
|
|
145
169
|
stringifyContent,
|
|
146
170
|
transformToCamelCaseEvent,
|
|
147
171
|
transformToSnakeCaseEvent
|
|
@@ -37,6 +37,19 @@ var import_types2 = require("./types.js");
|
|
|
37
37
|
* SPDX-License-Identifier: Apache-2.0
|
|
38
38
|
*/
|
|
39
39
|
class AgentRegistrySingleMCPToolset extends import_base_toolset.BaseToolset {
|
|
40
|
+
/**
|
|
41
|
+
* @param options - Configuration for the MCP toolset.
|
|
42
|
+
* @param options.destinationResourceId - Telemetry identifier injected as
|
|
43
|
+
* `gcp.mcp.server.destination.id` into each resolved tool's custom metadata.
|
|
44
|
+
* @param options.connectionParams - HTTP connection parameters for the MCP server.
|
|
45
|
+
* @param options.toolFilter - Optional predicate or list of tool names to include.
|
|
46
|
+
* When omitted, all tools from the server are returned.
|
|
47
|
+
* @param options.prefix - Optional prefix prepended to each tool name (e.g. `myServer_toolName`).
|
|
48
|
+
* @param options.headerProvider - Optional async function called immediately before each
|
|
49
|
+
* {@link getTools} invocation to supply or refresh request headers (e.g. GCP auth tokens).
|
|
50
|
+
* @param options.authScheme - Optional auth scheme forwarded to each resolved tool.
|
|
51
|
+
* @param options.authCredential - Optional credential forwarded to each resolved tool.
|
|
52
|
+
*/
|
|
40
53
|
constructor(options) {
|
|
41
54
|
super(options.toolFilter || [], options.prefix);
|
|
42
55
|
this.destinationResourceId = options.destinationResourceId;
|
|
@@ -46,8 +59,14 @@ class AgentRegistrySingleMCPToolset extends import_base_toolset.BaseToolset {
|
|
|
46
59
|
this.authCredential = options.authCredential;
|
|
47
60
|
}
|
|
48
61
|
/**
|
|
49
|
-
* Connects to the underlying MCP server, retrieves tool definitions, prefixes
|
|
50
|
-
* and injects destination telemetry metadata.
|
|
62
|
+
* Connects to the underlying MCP server, retrieves tool definitions, prefixes
|
|
63
|
+
* tool names, and injects destination telemetry metadata into each tool.
|
|
64
|
+
*
|
|
65
|
+
* The `headerProvider`, if configured, is invoked immediately before the
|
|
66
|
+
* connection is established so that tokens are always fresh.
|
|
67
|
+
*
|
|
68
|
+
* @param context - Optional readonly agent context passed to the header provider.
|
|
69
|
+
* @returns The resolved and optionally filtered list of {@link MCPTool} instances.
|
|
51
70
|
*/
|
|
52
71
|
async getTools(context) {
|
|
53
72
|
var _a, _b, _c;
|
|
@@ -27,15 +27,19 @@ __export(gemini_llm_connection_exports, {
|
|
|
27
27
|
GeminiLlmConnection: () => GeminiLlmConnection
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(gemini_llm_connection_exports);
|
|
30
|
+
var import_live_connection_utils = require("../utils/live_connection_utils.js");
|
|
30
31
|
var import_logger = require("../utils/logger.js");
|
|
32
|
+
var import_model_name = require("../utils/model_name.js");
|
|
31
33
|
/**
|
|
32
34
|
* @license
|
|
33
|
-
* Copyright
|
|
35
|
+
* Copyright 2026 Google LLC
|
|
34
36
|
* SPDX-License-Identifier: Apache-2.0
|
|
35
37
|
*/
|
|
36
38
|
class GeminiLlmConnection {
|
|
37
|
-
constructor(geminiSession) {
|
|
39
|
+
constructor(geminiSession, modelVersion, messageQueue) {
|
|
38
40
|
this.geminiSession = geminiSession;
|
|
41
|
+
this.modelVersion = modelVersion;
|
|
42
|
+
this.messageQueue = messageQueue;
|
|
39
43
|
}
|
|
40
44
|
/**
|
|
41
45
|
* Sends the conversation history to the gemini model.
|
|
@@ -54,9 +58,10 @@ class GeminiLlmConnection {
|
|
|
54
58
|
}
|
|
55
59
|
);
|
|
56
60
|
if (contents.length > 0) {
|
|
61
|
+
const isGemini3x = (0, import_model_name.isGemini3xFlashLive)(this.modelVersion);
|
|
57
62
|
this.geminiSession.sendClientContent({
|
|
58
63
|
turns: contents,
|
|
59
|
-
turnComplete: contents[contents.length - 1].role === "user"
|
|
64
|
+
turnComplete: isGemini3x ? true : contents[contents.length - 1].role === "user"
|
|
60
65
|
});
|
|
61
66
|
} else {
|
|
62
67
|
import_logger.logger.info("no content is sent");
|
|
@@ -83,10 +88,16 @@ class GeminiLlmConnection {
|
|
|
83
88
|
});
|
|
84
89
|
} else {
|
|
85
90
|
import_logger.logger.debug("Sending LLM new content", content);
|
|
86
|
-
this.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
91
|
+
const isGemini3x = (0, import_model_name.isGemini3xFlashLive)(this.modelVersion);
|
|
92
|
+
if (isGemini3x && content.parts.length === 1 && content.parts[0].text) {
|
|
93
|
+
import_logger.logger.debug("Using sendRealtimeInput for Gemini 3.x text input");
|
|
94
|
+
this.geminiSession.sendRealtimeInput({ text: content.parts[0].text });
|
|
95
|
+
} else {
|
|
96
|
+
this.geminiSession.sendClientContent({
|
|
97
|
+
turns: [content],
|
|
98
|
+
turnComplete: true
|
|
99
|
+
});
|
|
100
|
+
}
|
|
90
101
|
}
|
|
91
102
|
}
|
|
92
103
|
/**
|
|
@@ -95,8 +106,36 @@ class GeminiLlmConnection {
|
|
|
95
106
|
* @param blob The blob to send to the model.
|
|
96
107
|
*/
|
|
97
108
|
async sendRealtime(blob) {
|
|
109
|
+
var _a, _b, _c;
|
|
98
110
|
import_logger.logger.debug("Sending LLM Blob:", blob);
|
|
99
|
-
|
|
111
|
+
const isGemini3x = (0, import_model_name.isGemini3xFlashLive)(this.modelVersion);
|
|
112
|
+
const isNativeAudio = (_a = this.modelVersion) == null ? void 0 : _a.includes("native-audio");
|
|
113
|
+
if (isGemini3x || isNativeAudio) {
|
|
114
|
+
if ((_b = blob.mimeType) == null ? void 0 : _b.startsWith("audio/")) {
|
|
115
|
+
this.geminiSession.sendRealtimeInput({ audio: blob });
|
|
116
|
+
} else if ((_c = blob.mimeType) == null ? void 0 : _c.startsWith("image/")) {
|
|
117
|
+
this.geminiSession.sendRealtimeInput({ video: blob });
|
|
118
|
+
} else {
|
|
119
|
+
import_logger.logger.warn(
|
|
120
|
+
"Blob not sent. Unknown or empty mime type for sendRealtimeInput:",
|
|
121
|
+
blob.mimeType
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
this.geminiSession.sendRealtimeInput({ media: blob });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Sends an activity start signal to the model.
|
|
130
|
+
*/
|
|
131
|
+
async sendActivityStart() {
|
|
132
|
+
this.geminiSession.sendRealtimeInput({ activityStart: {} });
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Sends an activity end signal to the model.
|
|
136
|
+
*/
|
|
137
|
+
async sendActivityEnd() {
|
|
138
|
+
this.geminiSession.sendRealtimeInput({ activityEnd: {} });
|
|
100
139
|
}
|
|
101
140
|
/**
|
|
102
141
|
* Builds a full text response.
|
|
@@ -105,20 +144,24 @@ class GeminiLlmConnection {
|
|
|
105
144
|
* partial.
|
|
106
145
|
*
|
|
107
146
|
* @param text The text to be included in the response.
|
|
147
|
+
* @param isThought Whether the text is a thought.
|
|
148
|
+
* @param groundingMetadata The grounding metadata to include.
|
|
108
149
|
* @returns An LlmResponse containing the full text.
|
|
109
150
|
*/
|
|
110
|
-
buildFullTextResponse(text) {
|
|
111
|
-
return {
|
|
112
|
-
content: {
|
|
113
|
-
role: "model",
|
|
114
|
-
parts: [{ text }]
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
// TODO(b/425992518): GenAI SDK inconsistent API, missing methods.
|
|
119
|
-
// eslint-disable-next-line require-yield
|
|
120
151
|
async *receive() {
|
|
121
|
-
|
|
152
|
+
if (!this.messageQueue) {
|
|
153
|
+
throw new Error("Message queue is not initialized.");
|
|
154
|
+
}
|
|
155
|
+
const aggregator = new import_live_connection_utils.LiveResponseAggregator(this.modelVersion);
|
|
156
|
+
for await (const message of this.messageQueue) {
|
|
157
|
+
import_logger.logger.debug("Got LLM Live message:", message);
|
|
158
|
+
for (const response of aggregator.processMessage(message)) {
|
|
159
|
+
yield response;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
for (const response of aggregator.close()) {
|
|
163
|
+
yield response;
|
|
164
|
+
}
|
|
122
165
|
}
|
|
123
166
|
/**
|
|
124
167
|
* Closes the llm server connection.
|
|
@@ -25,23 +25,31 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
25
25
|
var google_llm_exports = {};
|
|
26
26
|
__export(google_llm_exports, {
|
|
27
27
|
Gemini: () => Gemini,
|
|
28
|
-
geminiInitParams: () => geminiInitParams
|
|
28
|
+
geminiInitParams: () => geminiInitParams,
|
|
29
|
+
isGemini: () => isGemini
|
|
29
30
|
});
|
|
30
31
|
module.exports = __toCommonJS(google_llm_exports);
|
|
31
32
|
var import_genai = require("@google/genai");
|
|
32
33
|
var import_env_aware_utils = require("../utils/env_aware_utils.js");
|
|
33
34
|
var import_logger = require("../utils/logger.js");
|
|
34
35
|
var import_variant_utils = require("../utils/variant_utils.js");
|
|
36
|
+
var import_async_queue = require("../utils/async_queue.js");
|
|
35
37
|
var import_streaming_utils = require("../utils/streaming_utils.js");
|
|
36
38
|
var import_base_llm = require("./base_llm.js");
|
|
37
39
|
var import_gemini_llm_connection = require("./gemini_llm_connection.js");
|
|
40
|
+
var import_interactions_utils = require("./interactions_utils.js");
|
|
38
41
|
var import_llm_response = require("./llm_response.js");
|
|
39
42
|
/**
|
|
40
43
|
* @license
|
|
41
44
|
* Copyright 2025 Google LLC
|
|
42
45
|
* SPDX-License-Identifier: Apache-2.0
|
|
43
46
|
*/
|
|
44
|
-
|
|
47
|
+
var _a, _b;
|
|
48
|
+
const GEMINI_MODEL_SYMBOL = Symbol.for("google.adk.geminiModel");
|
|
49
|
+
function isGemini(obj) {
|
|
50
|
+
return typeof obj === "object" && obj !== null && GEMINI_MODEL_SYMBOL in obj && obj[GEMINI_MODEL_SYMBOL] === true;
|
|
51
|
+
}
|
|
52
|
+
class Gemini extends (_b = import_base_llm.BaseLlm, _a = GEMINI_MODEL_SYMBOL, _b) {
|
|
45
53
|
/**
|
|
46
54
|
* @param params The parameters for creating a Gemini instance.
|
|
47
55
|
*/
|
|
@@ -51,12 +59,14 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
51
59
|
vertexai,
|
|
52
60
|
project,
|
|
53
61
|
location,
|
|
54
|
-
headers
|
|
62
|
+
headers,
|
|
63
|
+
useInteractionsApi
|
|
55
64
|
}) {
|
|
56
65
|
if (!model) {
|
|
57
66
|
model = "gemini-2.5-flash";
|
|
58
67
|
}
|
|
59
68
|
super({ model });
|
|
69
|
+
this[_a] = true;
|
|
60
70
|
const params = geminiInitParams({
|
|
61
71
|
model,
|
|
62
72
|
vertexai,
|
|
@@ -74,6 +84,7 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
74
84
|
this.apiKey = params.apiKey;
|
|
75
85
|
this.headers = headers;
|
|
76
86
|
this.vertexai = !!params.vertexai;
|
|
87
|
+
this.useInteractionsApi = !!useInteractionsApi;
|
|
77
88
|
}
|
|
78
89
|
/**
|
|
79
90
|
* Sends a request to the Gemini model.
|
|
@@ -83,11 +94,15 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
83
94
|
* @yields LlmResponse: The model response.
|
|
84
95
|
*/
|
|
85
96
|
async *generateContentAsync(llmRequest, stream = false, abortSignal) {
|
|
86
|
-
var
|
|
97
|
+
var _a2, _b2, _c;
|
|
98
|
+
if (this.useInteractionsApi) {
|
|
99
|
+
yield* (0, import_interactions_utils.generateContentViaInteractions)(this.apiClient, llmRequest, stream);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
87
102
|
this.preprocessRequest(llmRequest);
|
|
88
103
|
this.maybeAppendUserContent(llmRequest);
|
|
89
104
|
import_logger.logger.info(
|
|
90
|
-
`Sending out request, model: ${(
|
|
105
|
+
`Sending out request, model: ${(_a2 = llmRequest.model) != null ? _a2 : this.model}, backend: ${this.apiBackend}, stream: ${stream}`
|
|
91
106
|
);
|
|
92
107
|
if (!llmRequest.config) {
|
|
93
108
|
llmRequest.config = {};
|
|
@@ -103,7 +118,7 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
103
118
|
}
|
|
104
119
|
if (stream) {
|
|
105
120
|
const streamResult = await this.apiClient.models.generateContentStream({
|
|
106
|
-
model: (
|
|
121
|
+
model: (_b2 = llmRequest.model) != null ? _b2 : this.model,
|
|
107
122
|
contents: llmRequest.contents,
|
|
108
123
|
config: llmRequest.config
|
|
109
124
|
});
|
|
@@ -168,10 +183,19 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
168
183
|
}
|
|
169
184
|
get liveApiClient() {
|
|
170
185
|
if (!this._liveApiClient) {
|
|
171
|
-
this.
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
186
|
+
if (this.vertexai) {
|
|
187
|
+
this._liveApiClient = new import_genai.GoogleGenAI({
|
|
188
|
+
vertexai: this.vertexai,
|
|
189
|
+
project: this.project,
|
|
190
|
+
location: this.location || "global",
|
|
191
|
+
httpOptions: this.getLiveHttpOptions()
|
|
192
|
+
});
|
|
193
|
+
} else {
|
|
194
|
+
this._liveApiClient = new import_genai.GoogleGenAI({
|
|
195
|
+
apiKey: this.apiKey,
|
|
196
|
+
httpOptions: this.getLiveHttpOptions()
|
|
197
|
+
});
|
|
198
|
+
}
|
|
175
199
|
}
|
|
176
200
|
return this._liveApiClient;
|
|
177
201
|
}
|
|
@@ -182,8 +206,8 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
182
206
|
* @returns BaseLlmConnection, the connection to the Gemini model.
|
|
183
207
|
*/
|
|
184
208
|
async connect(llmRequest) {
|
|
185
|
-
var
|
|
186
|
-
if ((
|
|
209
|
+
var _a2, _b2, _c, _d;
|
|
210
|
+
if ((_a2 = llmRequest.liveConnectConfig) == null ? void 0 : _a2.httpOptions) {
|
|
187
211
|
if (!llmRequest.liveConnectConfig.httpOptions.headers) {
|
|
188
212
|
llmRequest.liveConnectConfig.httpOptions.headers = {};
|
|
189
213
|
}
|
|
@@ -193,7 +217,7 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
193
217
|
);
|
|
194
218
|
llmRequest.liveConnectConfig.httpOptions.apiVersion = this.liveApiVersion;
|
|
195
219
|
}
|
|
196
|
-
if ((
|
|
220
|
+
if ((_b2 = llmRequest.config) == null ? void 0 : _b2.systemInstruction) {
|
|
197
221
|
llmRequest.liveConnectConfig.systemInstruction = {
|
|
198
222
|
role: "system",
|
|
199
223
|
// TODO - b/425992518: validate type casting works well.
|
|
@@ -203,16 +227,24 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
203
227
|
};
|
|
204
228
|
}
|
|
205
229
|
llmRequest.liveConnectConfig.tools = (_c = llmRequest.config) == null ? void 0 : _c.tools;
|
|
230
|
+
const modelVersion = (_d = llmRequest.model) != null ? _d : this.model;
|
|
231
|
+
const messageQueue = new import_async_queue.AsyncQueue();
|
|
206
232
|
const liveSession = await this.liveApiClient.live.connect({
|
|
207
|
-
model:
|
|
233
|
+
model: modelVersion,
|
|
208
234
|
config: llmRequest.liveConnectConfig,
|
|
209
235
|
callbacks: {
|
|
210
|
-
|
|
211
|
-
|
|
236
|
+
onmessage: (message) => {
|
|
237
|
+
messageQueue.push(message);
|
|
238
|
+
},
|
|
239
|
+
onerror: (error) => {
|
|
240
|
+
messageQueue.error(error);
|
|
241
|
+
},
|
|
242
|
+
onclose: () => {
|
|
243
|
+
messageQueue.close();
|
|
212
244
|
}
|
|
213
245
|
}
|
|
214
246
|
});
|
|
215
|
-
return new import_gemini_llm_connection.GeminiLlmConnection(liveSession);
|
|
247
|
+
return new import_gemini_llm_connection.GeminiLlmConnection(liveSession, modelVersion, messageQueue);
|
|
216
248
|
}
|
|
217
249
|
preprocessRequest(llmRequest) {
|
|
218
250
|
if (this.apiBackend === import_variant_utils.GoogleLLMVariant.GEMINI_API) {
|
|
@@ -287,5 +319,6 @@ function geminiInitParams({
|
|
|
287
319
|
// Annotate the CommonJS export names for ESM import in node:
|
|
288
320
|
0 && (module.exports = {
|
|
289
321
|
Gemini,
|
|
290
|
-
geminiInitParams
|
|
322
|
+
geminiInitParams,
|
|
323
|
+
isGemini
|
|
291
324
|
});
|