@google/adk 1.1.0 → 1.3.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/base_agent.js +19 -1
- package/dist/cjs/agents/functions.js +4 -0
- package/dist/cjs/agents/instructions.js +81 -33
- package/dist/cjs/agents/invocation_context.js +0 -1
- package/dist/cjs/agents/llm_agent.js +52 -5
- 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/content_processor_utils.js +37 -24
- package/dist/cjs/agents/processors/content_request_processor.js +7 -0
- package/dist/cjs/agents/processors/context_compactor_request_processor.js +12 -0
- 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/request_confirmation_llm_request_processor.js +9 -1
- package/dist/cjs/agents/processors/tool_filter_request_processor.js +7 -1
- package/dist/cjs/auth/auth_preprocessor.js +12 -10
- package/dist/cjs/auth/oauth2/oauth2_discovery.js +26 -2
- package/dist/cjs/code_executors/agent_engine_sandbox_code_executor.js +305 -0
- package/dist/cjs/code_executors/code_execution_utils.js +1 -1
- package/dist/cjs/code_executors/code_executor_context.js +7 -7
- package/dist/cjs/common.js +50 -0
- package/dist/cjs/context/summarizers/llm_summarizer.js +11 -0
- package/dist/cjs/context/token_based_context_compactor.js +1 -1
- package/dist/cjs/events/event.js +4 -2
- package/dist/cjs/index.js +14 -0
- package/dist/cjs/integrations/agent_registry/agent_registry.js +362 -0
- package/dist/cjs/integrations/agent_registry/agent_registry_mcp_toolset.js +122 -0
- package/dist/cjs/integrations/agent_registry/helpers.js +57 -0
- package/dist/cjs/integrations/agent_registry/types.js +50 -0
- package/dist/cjs/memory/vertex_ai_memory_bank_service.js +496 -0
- package/dist/cjs/models/gemini_llm_connection.js +62 -19
- package/dist/cjs/models/google_llm.js +26 -8
- package/dist/cjs/plugins/security_plugin.js +16 -0
- package/dist/cjs/runner/in_memory_runner.js +10 -5
- package/dist/cjs/runner/runner.js +32 -2
- package/dist/cjs/sessions/base_session_service.js +13 -2
- package/dist/cjs/sessions/database_session_service.js +51 -5
- package/dist/cjs/sessions/db/schema.js +45 -10
- package/dist/cjs/sessions/in_memory_session_service.js +72 -15
- package/dist/cjs/sessions/registry.js +4 -0
- package/dist/cjs/sessions/vertex_ai_session_service.js +429 -0
- package/dist/cjs/skills/gcp_skill_registry.js +104 -0
- package/dist/cjs/skills/loader.js +65 -0
- package/dist/cjs/skills/skill_registry.js +27 -0
- package/dist/cjs/tools/agent_tool.js +10 -5
- package/dist/cjs/tools/exit_loop_tool.js +9 -0
- package/dist/cjs/tools/function_tool.js +7 -2
- package/dist/cjs/tools/google_maps_grounding_tool.js +80 -0
- package/dist/cjs/tools/long_running_tool.js +2 -1
- package/dist/cjs/tools/mcp/mcp_session_manager.js +11 -0
- package/dist/cjs/tools/mcp/mcp_tool.js +10 -6
- package/dist/cjs/tools/mcp/mcp_toolset.js +26 -3
- package/dist/cjs/tools/openapi_tool/auth/auth_helpers.js +78 -0
- package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +84 -0
- package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +133 -0
- 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 +127 -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 +27 -28
- 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/tools/vertex_rag_retrieval_tool.js +64 -0
- package/dist/cjs/utils/async_queue.js +88 -0
- package/dist/cjs/utils/case_utils.js +53 -0
- package/dist/cjs/utils/env_aware_utils.js +4 -0
- package/dist/cjs/utils/file_utils.js +28 -0
- package/dist/cjs/utils/gemini_schema_util.js +18 -0
- package/dist/cjs/utils/live_connection_utils.js +284 -0
- package/dist/cjs/utils/model_name.js +9 -0
- package/dist/cjs/utils/partial_copy.js +51 -0
- package/dist/cjs/utils/streaming_utils.js +169 -122
- package/dist/cjs/utils/vertex_ai_utils.js +50 -0
- package/dist/cjs/version.js +1 -1
- package/dist/esm/agents/base_agent.js +19 -1
- package/dist/esm/agents/functions.js +4 -0
- package/dist/esm/agents/instructions.js +81 -33
- package/dist/esm/agents/invocation_context.js +0 -1
- package/dist/esm/agents/llm_agent.js +53 -5
- 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/content_processor_utils.js +35 -23
- package/dist/esm/agents/processors/content_request_processor.js +7 -0
- package/dist/esm/agents/processors/context_compactor_request_processor.js +12 -0
- 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/request_confirmation_llm_request_processor.js +9 -1
- package/dist/esm/agents/processors/tool_filter_request_processor.js +7 -1
- package/dist/esm/auth/auth_preprocessor.js +12 -10
- package/dist/esm/auth/oauth2/oauth2_discovery.js +26 -2
- package/dist/esm/code_executors/agent_engine_sandbox_code_executor.js +281 -0
- package/dist/esm/code_executors/code_execution_utils.js +1 -1
- package/dist/esm/code_executors/code_executor_context.js +7 -7
- package/dist/esm/common.js +43 -1
- package/dist/esm/context/summarizers/llm_summarizer.js +11 -0
- package/dist/esm/context/token_based_context_compactor.js +1 -1
- package/dist/esm/events/event.js +4 -2
- package/dist/esm/index.js +11 -0
- package/dist/esm/integrations/agent_registry/agent_registry.js +333 -0
- package/dist/esm/integrations/agent_registry/agent_registry_mcp_toolset.js +96 -0
- package/dist/esm/integrations/agent_registry/helpers.js +28 -0
- package/dist/esm/integrations/agent_registry/types.js +20 -0
- package/dist/esm/memory/vertex_ai_memory_bank_service.js +468 -0
- package/dist/esm/models/gemini_llm_connection.js +62 -19
- package/dist/esm/models/google_llm.js +26 -8
- package/dist/esm/plugins/security_plugin.js +16 -0
- package/dist/esm/runner/in_memory_runner.js +10 -5
- package/dist/esm/runner/runner.js +32 -2
- package/dist/esm/sessions/base_session_service.js +11 -1
- package/dist/esm/sessions/database_session_service.js +51 -5
- package/dist/esm/sessions/db/schema.js +44 -10
- package/dist/esm/sessions/in_memory_session_service.js +74 -16
- package/dist/esm/sessions/registry.js +7 -0
- package/dist/esm/sessions/vertex_ai_session_service.js +405 -0
- package/dist/esm/skills/gcp_skill_registry.js +78 -0
- package/dist/esm/skills/loader.js +64 -0
- package/dist/esm/skills/skill_registry.js +7 -0
- package/dist/esm/tools/agent_tool.js +10 -5
- package/dist/esm/tools/exit_loop_tool.js +9 -0
- package/dist/esm/tools/function_tool.js +7 -2
- package/dist/esm/tools/google_maps_grounding_tool.js +54 -0
- package/dist/esm/tools/long_running_tool.js +2 -1
- package/dist/esm/tools/mcp/mcp_session_manager.js +11 -0
- package/dist/esm/tools/mcp/mcp_tool.js +10 -6
- package/dist/esm/tools/mcp/mcp_toolset.js +26 -3
- package/dist/esm/tools/openapi_tool/auth/auth_helpers.js +48 -0
- package/dist/esm/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +60 -0
- package/dist/esm/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +111 -0
- 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 +101 -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 +26 -27
- 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/tools/vertex_rag_retrieval_tool.js +36 -0
- package/dist/esm/utils/async_queue.js +60 -0
- package/dist/esm/utils/case_utils.js +25 -0
- package/dist/esm/utils/env_aware_utils.js +4 -0
- package/dist/esm/utils/file_utils.js +26 -0
- package/dist/esm/utils/gemini_schema_util.js +18 -0
- package/dist/esm/utils/live_connection_utils.js +256 -0
- package/dist/esm/utils/model_name.js +8 -0
- package/dist/esm/utils/partial_copy.js +23 -0
- package/dist/esm/utils/streaming_utils.js +167 -121
- package/dist/esm/utils/vertex_ai_utils.js +22 -0
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_remote_agent.d.ts +14 -0
- 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/invocation_context.d.ts +0 -9
- 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/base_llm_processor.d.ts +12 -4
- package/dist/types/agents/processors/basic_llm_request_processor.d.ts +12 -0
- package/dist/types/agents/processors/content_processor_utils.d.ts +26 -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 +11 -0
- package/dist/types/agents/processors/request_confirmation_llm_request_processor.d.ts +14 -1
- package/dist/types/agents/processors/tool_filter_request_processor.d.ts +12 -1
- package/dist/types/agents/run_config.d.ts +15 -0
- package/dist/types/code_executors/agent_engine_sandbox_code_executor.d.ts +51 -0
- package/dist/types/common.d.ts +19 -1
- package/dist/types/context/summarizers/llm_summarizer.d.ts +17 -0
- package/dist/types/events/event_actions.d.ts +25 -8
- package/dist/types/events/structured_events.d.ts +13 -0
- package/dist/types/examples/example_util.d.ts +25 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/integrations/agent_registry/agent_registry.d.ts +94 -0
- package/dist/types/integrations/agent_registry/agent_registry_mcp_toolset.d.ts +62 -0
- package/dist/types/integrations/agent_registry/helpers.d.ts +7 -0
- package/dist/types/integrations/agent_registry/types.d.ts +101 -0
- package/dist/types/memory/vertex_ai_memory_bank_service.d.ts +52 -0
- 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/llm_response.d.ts +10 -1
- package/dist/types/plugins/security_plugin.d.ts +30 -0
- package/dist/types/runner/in_memory_runner.d.ts +28 -1
- package/dist/types/runner/runner.d.ts +47 -0
- package/dist/types/sessions/base_session_service.d.ts +22 -0
- package/dist/types/sessions/database_session_service.d.ts +1 -1
- package/dist/types/sessions/db/schema.d.ts +1 -0
- package/dist/types/sessions/in_memory_session_service.d.ts +1 -1
- package/dist/types/sessions/vertex_ai_session_service.d.ts +37 -0
- 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/exit_loop_tool.d.ts +9 -0
- package/dist/types/tools/function_tool.d.ts +22 -2
- package/dist/types/tools/google_maps_grounding_tool.d.ts +22 -0
- package/dist/types/tools/long_running_tool.d.ts +11 -1
- package/dist/types/tools/mcp/mcp_session_manager.d.ts +3 -0
- package/dist/types/tools/mcp/mcp_toolset.d.ts +2 -1
- package/dist/types/tools/openapi_tool/auth/auth_helpers.d.ts +25 -0
- package/dist/types/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.d.ts +20 -0
- package/dist/types/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.d.ts +20 -0
- 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/tools/vertex_rag_retrieval_tool.d.ts +42 -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/file_utils.d.ts +2 -0
- 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/partial_copy.d.ts +13 -0
- package/dist/types/utils/streaming_utils.d.ts +7 -17
- package/dist/types/utils/vertex_ai_utils.d.ts +14 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/agents/base_agent.js +31 -1
- package/dist/web/agents/functions.js +4 -0
- package/dist/web/agents/instructions.js +81 -33
- package/dist/web/agents/invocation_context.js +0 -1
- package/dist/web/agents/llm_agent.js +53 -5
- 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/content_processor_utils.js +53 -23
- package/dist/web/agents/processors/content_request_processor.js +7 -0
- package/dist/web/agents/processors/context_compactor_request_processor.js +12 -0
- 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/request_confirmation_llm_request_processor.js +9 -1
- package/dist/web/agents/processors/tool_filter_request_processor.js +7 -1
- package/dist/web/auth/auth_preprocessor.js +12 -10
- package/dist/web/auth/oauth2/oauth2_discovery.js +26 -2
- package/dist/web/code_executors/agent_engine_sandbox_code_executor.js +281 -0
- package/dist/web/code_executors/code_execution_utils.js +1 -1
- package/dist/web/code_executors/code_executor_context.js +7 -7
- package/dist/web/common.js +43 -1
- package/dist/web/context/summarizers/llm_summarizer.js +11 -0
- package/dist/web/context/token_based_context_compactor.js +1 -1
- package/dist/web/events/event.js +4 -2
- package/dist/web/index.js +11 -0
- package/dist/web/integrations/agent_registry/agent_registry.js +333 -0
- package/dist/web/integrations/agent_registry/agent_registry_mcp_toolset.js +109 -0
- package/dist/web/integrations/agent_registry/helpers.js +28 -0
- package/dist/web/integrations/agent_registry/types.js +20 -0
- package/dist/web/memory/vertex_ai_memory_bank_service.js +475 -0
- package/dist/web/models/gemini_llm_connection.js +75 -19
- package/dist/web/models/google_llm.js +26 -8
- package/dist/web/plugins/security_plugin.js +16 -0
- package/dist/web/runner/in_memory_runner.js +10 -5
- package/dist/web/runner/runner.js +32 -2
- package/dist/web/sessions/base_session_service.js +11 -1
- package/dist/web/sessions/database_session_service.js +51 -5
- package/dist/web/sessions/db/schema.js +44 -10
- package/dist/web/sessions/in_memory_session_service.js +74 -16
- package/dist/web/sessions/registry.js +7 -0
- package/dist/web/sessions/vertex_ai_session_service.js +416 -0
- package/dist/web/skills/gcp_skill_registry.js +78 -0
- package/dist/web/skills/loader.js +64 -0
- package/dist/web/skills/skill_registry.js +7 -0
- package/dist/web/tools/agent_tool.js +10 -5
- package/dist/web/tools/exit_loop_tool.js +9 -0
- package/dist/web/tools/function_tool.js +7 -2
- package/dist/web/tools/google_maps_grounding_tool.js +54 -0
- package/dist/web/tools/long_running_tool.js +2 -1
- package/dist/web/tools/mcp/mcp_session_manager.js +11 -0
- package/dist/web/tools/mcp/mcp_tool.js +10 -6
- package/dist/web/tools/mcp/mcp_toolset.js +26 -3
- package/dist/web/tools/openapi_tool/auth/auth_helpers.js +48 -0
- package/dist/web/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +60 -0
- package/dist/web/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +111 -0
- 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 +101 -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 +26 -27
- 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/tools/vertex_rag_retrieval_tool.js +36 -0
- package/dist/web/utils/async_queue.js +60 -0
- package/dist/web/utils/case_utils.js +25 -0
- package/dist/web/utils/env_aware_utils.js +4 -0
- package/dist/web/utils/file_utils.js +26 -0
- package/dist/web/utils/gemini_schema_util.js +18 -0
- package/dist/web/utils/live_connection_utils.js +254 -0
- package/dist/web/utils/model_name.js +8 -0
- package/dist/web/utils/partial_copy.js +23 -0
- package/dist/web/utils/streaming_utils.js +221 -122
- package/dist/web/utils/vertex_ai_utils.js +22 -0
- package/dist/web/version.js +1 -1
- package/package.json +4 -1
|
@@ -0,0 +1,468 @@
|
|
|
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 { Client } from "@google-cloud/vertexai";
|
|
9
|
+
import { createUserContent } from "@google/genai";
|
|
10
|
+
import { logger } from "../utils/logger.js";
|
|
11
|
+
import { getExpressModeApiKey } from "../utils/vertex_ai_utils.js";
|
|
12
|
+
const GENERATE_MEMORIES_KNOWN_FIELDS = [
|
|
13
|
+
"disableConsolidation",
|
|
14
|
+
"waitForCompletion",
|
|
15
|
+
"revisionLabels",
|
|
16
|
+
"revisionExpireTime",
|
|
17
|
+
"revisionTtl",
|
|
18
|
+
"disableMemoryRevisions",
|
|
19
|
+
"metadataMergeStrategy",
|
|
20
|
+
"allowedTopics"
|
|
21
|
+
];
|
|
22
|
+
const CREATE_MEMORY_KNOWN_FIELDS = [
|
|
23
|
+
"displayName",
|
|
24
|
+
"description",
|
|
25
|
+
"waitForCompletion",
|
|
26
|
+
"ttl",
|
|
27
|
+
"expireTime",
|
|
28
|
+
"revisionExpireTime",
|
|
29
|
+
"revisionTtl",
|
|
30
|
+
"disableMemoryRevisions",
|
|
31
|
+
"topics",
|
|
32
|
+
"memoryId"
|
|
33
|
+
];
|
|
34
|
+
const ENABLE_CONSOLIDATION_KEY = "enable_consolidation";
|
|
35
|
+
const MAX_DIRECT_MEMORIES_PER_GENERATE_CALL = 5;
|
|
36
|
+
function shouldFilterOutEvent(content) {
|
|
37
|
+
return !((content == null ? void 0 : content.parts) || []).some(
|
|
38
|
+
(p) => p.text || p.inlineData || p.fileData
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
function toVertexMetadataValue(key, value) {
|
|
42
|
+
if (typeof value === "boolean") {
|
|
43
|
+
return { boolValue: value };
|
|
44
|
+
}
|
|
45
|
+
if (typeof value === "number") {
|
|
46
|
+
return { doubleValue: value };
|
|
47
|
+
}
|
|
48
|
+
if (typeof value === "string") {
|
|
49
|
+
return { stringValue: value };
|
|
50
|
+
}
|
|
51
|
+
if (value instanceof Date) {
|
|
52
|
+
return { timestampValue: value.toISOString() };
|
|
53
|
+
}
|
|
54
|
+
if (typeof value === "object" && value !== null) {
|
|
55
|
+
const v = value;
|
|
56
|
+
if (v.boolValue !== void 0 || v.doubleValue !== void 0 || v.stringValue !== void 0 || v.timestampValue !== void 0) {
|
|
57
|
+
return v;
|
|
58
|
+
}
|
|
59
|
+
return { stringValue: JSON.stringify(value) };
|
|
60
|
+
}
|
|
61
|
+
if (value === null || value === void 0) {
|
|
62
|
+
logger.warn(
|
|
63
|
+
`Ignoring custom metadata key ${key} because its value is null or undefined.`
|
|
64
|
+
);
|
|
65
|
+
return void 0;
|
|
66
|
+
}
|
|
67
|
+
return { stringValue: String(value) };
|
|
68
|
+
}
|
|
69
|
+
class VertexAiMemoryBankService {
|
|
70
|
+
constructor(options) {
|
|
71
|
+
if (!options.agentEngineId) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
"agentEngineId is required for VertexAiMemoryBankService."
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
this.projectId = options.projectId;
|
|
77
|
+
this.location = options.location;
|
|
78
|
+
this.agentEngineId = options.agentEngineId;
|
|
79
|
+
this.expressModeApiKey = getExpressModeApiKey(
|
|
80
|
+
options.projectId,
|
|
81
|
+
options.location,
|
|
82
|
+
options.expressModeApiKey
|
|
83
|
+
);
|
|
84
|
+
if (options.agentEngineId.includes("/")) {
|
|
85
|
+
logger.warn(
|
|
86
|
+
`agentEngineId appears to be a full resource path: '${options.agentEngineId}'. Expected just the ID (e.g., '456'). Extract the ID using: agentEngine.apiResource.name.split('/').pop()`
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
if (options.client) {
|
|
90
|
+
this.memories = options.client.agentEnginesInternal.memories;
|
|
91
|
+
} else {
|
|
92
|
+
const client = new Client({
|
|
93
|
+
project: this.projectId,
|
|
94
|
+
location: this.location
|
|
95
|
+
});
|
|
96
|
+
this.memories = client.agentEnginesInternal.memories;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async addSessionToMemory(session) {
|
|
100
|
+
await this.addEventsToMemoryFromEvents({
|
|
101
|
+
appName: session.appName,
|
|
102
|
+
userId: session.userId,
|
|
103
|
+
eventsToProcess: session.events
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Adds events to Vertex AI Memory Bank via memories.generate.
|
|
108
|
+
*/
|
|
109
|
+
async addEventsToMemory(request) {
|
|
110
|
+
await this.addEventsToMemoryFromEvents({
|
|
111
|
+
appName: request.appName,
|
|
112
|
+
userId: request.userId,
|
|
113
|
+
eventsToProcess: request.events,
|
|
114
|
+
customMetadata: request.customMetadata
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Adds explicit memory items using Vertex Memory Bank.
|
|
119
|
+
*/
|
|
120
|
+
async addMemory(request) {
|
|
121
|
+
if (isConsolidationEnabled(request.customMetadata)) {
|
|
122
|
+
return this.addMemoriesViaGenerateDirectMemoriesSource(request);
|
|
123
|
+
}
|
|
124
|
+
await this.addMemoriesViaCreate(request);
|
|
125
|
+
}
|
|
126
|
+
async searchMemory(request) {
|
|
127
|
+
const params = {
|
|
128
|
+
name: `reasoningEngines/${this.agentEngineId}`,
|
|
129
|
+
scope: {
|
|
130
|
+
app_name: request.appName,
|
|
131
|
+
user_id: request.userId
|
|
132
|
+
},
|
|
133
|
+
similaritySearchParams: {
|
|
134
|
+
searchQuery: request.query
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const retrievedMemoriesResponse = await this.memories.retrieveInternal(params);
|
|
138
|
+
logger.debug("Search memory response received.");
|
|
139
|
+
const memoryEvents = [];
|
|
140
|
+
const retrievedMemories = retrievedMemoriesResponse.retrievedMemories || [];
|
|
141
|
+
for (const retrievedMemory of retrievedMemories) {
|
|
142
|
+
logger.debug(`Retrieved memory: ${JSON.stringify(retrievedMemory)}`);
|
|
143
|
+
if (retrievedMemory.memory && retrievedMemory.memory.fact) {
|
|
144
|
+
const content = createUserContent(retrievedMemory.memory.fact);
|
|
145
|
+
memoryEvents.push({
|
|
146
|
+
author: "user",
|
|
147
|
+
content,
|
|
148
|
+
timestamp: retrievedMemory.memory.updateTime
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return { memories: memoryEvents };
|
|
153
|
+
}
|
|
154
|
+
async addEventsToMemoryFromEvents(request) {
|
|
155
|
+
const directEvents = [];
|
|
156
|
+
for (const event of request.eventsToProcess) {
|
|
157
|
+
if (shouldFilterOutEvent(event.content)) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
directEvents.push({
|
|
161
|
+
content: JSON.parse(JSON.stringify(event.content))
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
if (directEvents.length > 0) {
|
|
165
|
+
const config = buildGenerateMemoriesConfig(request.customMetadata);
|
|
166
|
+
const params = {
|
|
167
|
+
name: `reasoningEngines/${this.agentEngineId}`,
|
|
168
|
+
directContentsSource: { events: directEvents },
|
|
169
|
+
scope: {
|
|
170
|
+
app_name: request.appName,
|
|
171
|
+
user_id: request.userId
|
|
172
|
+
},
|
|
173
|
+
config
|
|
174
|
+
};
|
|
175
|
+
const operation = await this.memories.generateInternal(params);
|
|
176
|
+
logger.debug("Generate memory response received.");
|
|
177
|
+
logger.debug(`Generate memory response: ${JSON.stringify(operation)}`);
|
|
178
|
+
} else {
|
|
179
|
+
logger.info("No events to add to memory.");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async addMemoriesViaCreate(request) {
|
|
183
|
+
const validatedMemories = normalizeMemoriesForCreate(request.memories);
|
|
184
|
+
for (let index = 0; index < validatedMemories.length; index++) {
|
|
185
|
+
const memory = validatedMemories[index];
|
|
186
|
+
const memoryFact = memoryEntryToFact(memory, index);
|
|
187
|
+
const memoryMetadata = mergeCustomMetadataForMemory({
|
|
188
|
+
customMetadata: request.customMetadata,
|
|
189
|
+
memory
|
|
190
|
+
});
|
|
191
|
+
const memoryRevisionLabels = revisionLabelsForMemory(memory);
|
|
192
|
+
const config = buildCreateMemoryConfig({
|
|
193
|
+
customMetadata: memoryMetadata,
|
|
194
|
+
memoryRevisionLabels
|
|
195
|
+
});
|
|
196
|
+
const params = {
|
|
197
|
+
name: `reasoningEngines/${this.agentEngineId}`,
|
|
198
|
+
fact: memoryFact,
|
|
199
|
+
scope: {
|
|
200
|
+
app_name: request.appName,
|
|
201
|
+
user_id: request.userId
|
|
202
|
+
},
|
|
203
|
+
config
|
|
204
|
+
};
|
|
205
|
+
const operation = await this.memories.createInternal(params);
|
|
206
|
+
logger.info("Create memory response received.");
|
|
207
|
+
logger.debug(`Create memory response: ${JSON.stringify(operation)}`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
async addMemoriesViaGenerateDirectMemoriesSource(request) {
|
|
211
|
+
const validatedMemories = normalizeMemoriesForCreate(request.memories);
|
|
212
|
+
const memoryTexts = validatedMemories.map(
|
|
213
|
+
(m, i) => memoryEntryToFact(m, i)
|
|
214
|
+
);
|
|
215
|
+
const config = buildGenerateMemoriesConfig(request.customMetadata);
|
|
216
|
+
const memoryBatches = iterMemoryBatches(memoryTexts);
|
|
217
|
+
for (const memoryBatch of memoryBatches) {
|
|
218
|
+
const params = {
|
|
219
|
+
name: `reasoningEngines/${this.agentEngineId}`,
|
|
220
|
+
directMemoriesSource: {
|
|
221
|
+
directMemories: memoryBatch.map((fact) => ({ fact }))
|
|
222
|
+
},
|
|
223
|
+
scope: {
|
|
224
|
+
app_name: request.appName,
|
|
225
|
+
user_id: request.userId
|
|
226
|
+
},
|
|
227
|
+
config
|
|
228
|
+
};
|
|
229
|
+
const operation = await this.memories.generateInternal(params);
|
|
230
|
+
logger.info("Generate direct memory response received.");
|
|
231
|
+
logger.debug(
|
|
232
|
+
`Generate direct memory response: ${JSON.stringify(operation)}`
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function buildCreateMemoryConfig(params) {
|
|
238
|
+
const config = { waitForCompletion: false };
|
|
239
|
+
if (params.customMetadata) {
|
|
240
|
+
logger.debug(
|
|
241
|
+
`Memory creation metadata: ${JSON.stringify(params.customMetadata)}`
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
const metadataByKey = {};
|
|
245
|
+
const customRevisionLabels = {};
|
|
246
|
+
for (const [key, value] of Object.entries(params.customMetadata || {})) {
|
|
247
|
+
if (key === ENABLE_CONSOLIDATION_KEY) {
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (key === "metadata") {
|
|
251
|
+
if (value === null || value === void 0) continue;
|
|
252
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
253
|
+
config["metadata"] = buildVertexMetadata(
|
|
254
|
+
value
|
|
255
|
+
);
|
|
256
|
+
} else {
|
|
257
|
+
logger.warn(
|
|
258
|
+
'Ignoring metadata because customMetadata["metadata"] is not an object.'
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (key === "revisionLabels") {
|
|
264
|
+
if (value === null || value === void 0) continue;
|
|
265
|
+
const extractedLabels = extractRevisionLabels(
|
|
266
|
+
value,
|
|
267
|
+
'customMetadata["revisionLabels"]'
|
|
268
|
+
);
|
|
269
|
+
if (extractedLabels) {
|
|
270
|
+
Object.assign(customRevisionLabels, extractedLabels);
|
|
271
|
+
}
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (CREATE_MEMORY_KNOWN_FIELDS.includes(key)) {
|
|
275
|
+
if (value !== null && value !== void 0) {
|
|
276
|
+
config[key] = value;
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
metadataByKey[key] = value;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (Object.keys(metadataByKey).length > 0) {
|
|
283
|
+
const existingMetadata = config["metadata"];
|
|
284
|
+
if (!existingMetadata) {
|
|
285
|
+
config["metadata"] = buildVertexMetadata(metadataByKey);
|
|
286
|
+
} else {
|
|
287
|
+
config["metadata"] = {
|
|
288
|
+
...existingMetadata,
|
|
289
|
+
...buildVertexMetadata(metadataByKey)
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const revisionLabels = {
|
|
294
|
+
...customRevisionLabels,
|
|
295
|
+
...params.memoryRevisionLabels
|
|
296
|
+
};
|
|
297
|
+
if (Object.keys(revisionLabels).length > 0) {
|
|
298
|
+
config["revisionLabels"] = revisionLabels;
|
|
299
|
+
}
|
|
300
|
+
return config;
|
|
301
|
+
}
|
|
302
|
+
function extractRevisionLabels(value, source) {
|
|
303
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
304
|
+
logger.warn(`Ignoring ${source} because it is not an object.`);
|
|
305
|
+
return void 0;
|
|
306
|
+
}
|
|
307
|
+
const revisionLabels = {};
|
|
308
|
+
for (const [key, labelValue] of Object.entries(value)) {
|
|
309
|
+
if (typeof labelValue !== "string") {
|
|
310
|
+
logger.warn(
|
|
311
|
+
`Ignoring revision label ${key} from ${source} because its value is not a string.`
|
|
312
|
+
);
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
revisionLabels[key] = labelValue;
|
|
316
|
+
}
|
|
317
|
+
if (Object.keys(revisionLabels).length === 0) {
|
|
318
|
+
return void 0;
|
|
319
|
+
}
|
|
320
|
+
return revisionLabels;
|
|
321
|
+
}
|
|
322
|
+
function buildVertexMetadata(metadataByKey) {
|
|
323
|
+
const vertexMetadata = {};
|
|
324
|
+
for (const [key, value] of Object.entries(metadataByKey)) {
|
|
325
|
+
const convertedValue = toVertexMetadataValue(key, value);
|
|
326
|
+
if (convertedValue !== void 0) {
|
|
327
|
+
vertexMetadata[key] = convertedValue;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return vertexMetadata;
|
|
331
|
+
}
|
|
332
|
+
function buildGenerateMemoriesConfig(customMetadata) {
|
|
333
|
+
const config = { waitForCompletion: false };
|
|
334
|
+
if (!customMetadata) {
|
|
335
|
+
return config;
|
|
336
|
+
}
|
|
337
|
+
logger.debug(`Memory generation metadata: ${JSON.stringify(customMetadata)}`);
|
|
338
|
+
const metadataByKey = {};
|
|
339
|
+
for (const [key, value] of Object.entries(customMetadata)) {
|
|
340
|
+
if (key === ENABLE_CONSOLIDATION_KEY) {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (key === "ttl") {
|
|
344
|
+
if (value === null || value === void 0) continue;
|
|
345
|
+
if (customMetadata["revisionTtl"] === void 0) {
|
|
346
|
+
config["revisionTtl"] = value;
|
|
347
|
+
}
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (key === "metadata") {
|
|
351
|
+
if (value === null || value === void 0) continue;
|
|
352
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
353
|
+
config["metadata"] = buildVertexMetadata(
|
|
354
|
+
value
|
|
355
|
+
);
|
|
356
|
+
} else {
|
|
357
|
+
logger.warn(
|
|
358
|
+
'Ignoring metadata because customMetadata["metadata"] is not an object.'
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (GENERATE_MEMORIES_KNOWN_FIELDS.includes(key)) {
|
|
364
|
+
if (value !== null && value !== void 0) {
|
|
365
|
+
config[key] = value;
|
|
366
|
+
}
|
|
367
|
+
} else {
|
|
368
|
+
metadataByKey[key] = value;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (Object.keys(metadataByKey).length > 0) {
|
|
372
|
+
const existingMetadata = config["metadata"];
|
|
373
|
+
if (!existingMetadata) {
|
|
374
|
+
config["metadata"] = buildVertexMetadata(metadataByKey);
|
|
375
|
+
} else {
|
|
376
|
+
config["metadata"] = {
|
|
377
|
+
...existingMetadata,
|
|
378
|
+
...buildVertexMetadata(metadataByKey)
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return config;
|
|
383
|
+
}
|
|
384
|
+
function normalizeMemoriesForCreate(memories) {
|
|
385
|
+
if (!Array.isArray(memories)) {
|
|
386
|
+
throw new TypeError("memories must be a sequence of memory items.");
|
|
387
|
+
}
|
|
388
|
+
if (memories.length === 0) {
|
|
389
|
+
throw new Error("memories must contain at least one entry.");
|
|
390
|
+
}
|
|
391
|
+
return memories;
|
|
392
|
+
}
|
|
393
|
+
function memoryEntryToFact(memory, index) {
|
|
394
|
+
const textParts = [];
|
|
395
|
+
if (memory.content && memory.content.parts) {
|
|
396
|
+
for (const part of memory.content.parts) {
|
|
397
|
+
if (part.inlineData || part.fileData) {
|
|
398
|
+
throw new Error(
|
|
399
|
+
`memories[${index}] must include text only; inlineData and fileData are not supported.`
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
if (part.text) {
|
|
403
|
+
const strippedText = part.text.trim();
|
|
404
|
+
if (strippedText) {
|
|
405
|
+
textParts.push(strippedText);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (textParts.length === 0) {
|
|
411
|
+
throw new Error(`memories[${index}] must include non-whitespace text.`);
|
|
412
|
+
}
|
|
413
|
+
return textParts.join("\n");
|
|
414
|
+
}
|
|
415
|
+
function mergeCustomMetadataForMemory(params) {
|
|
416
|
+
const mergedMetadata = {};
|
|
417
|
+
if (params.customMetadata) {
|
|
418
|
+
Object.assign(mergedMetadata, params.customMetadata);
|
|
419
|
+
}
|
|
420
|
+
const memoryWithMetadata = params.memory;
|
|
421
|
+
if (memoryWithMetadata.customMetadata) {
|
|
422
|
+
Object.assign(mergedMetadata, memoryWithMetadata.customMetadata);
|
|
423
|
+
}
|
|
424
|
+
if (Object.keys(mergedMetadata).length === 0) {
|
|
425
|
+
return void 0;
|
|
426
|
+
}
|
|
427
|
+
return mergedMetadata;
|
|
428
|
+
}
|
|
429
|
+
function revisionLabelsForMemory(memory) {
|
|
430
|
+
const revisionLabels = {};
|
|
431
|
+
if (memory.author) {
|
|
432
|
+
revisionLabels["author"] = memory.author;
|
|
433
|
+
}
|
|
434
|
+
if (memory.timestamp) {
|
|
435
|
+
revisionLabels["timestamp"] = memory.timestamp;
|
|
436
|
+
}
|
|
437
|
+
if (Object.keys(revisionLabels).length === 0) {
|
|
438
|
+
return void 0;
|
|
439
|
+
}
|
|
440
|
+
return revisionLabels;
|
|
441
|
+
}
|
|
442
|
+
function isConsolidationEnabled(customMetadata) {
|
|
443
|
+
if (!customMetadata) {
|
|
444
|
+
return false;
|
|
445
|
+
}
|
|
446
|
+
const enableConsolidation = customMetadata[ENABLE_CONSOLIDATION_KEY];
|
|
447
|
+
if (enableConsolidation === void 0) {
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
if (typeof enableConsolidation !== "boolean") {
|
|
451
|
+
throw new TypeError(
|
|
452
|
+
`customMetadata["${ENABLE_CONSOLIDATION_KEY}"] must be a bool.`
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
return enableConsolidation;
|
|
456
|
+
}
|
|
457
|
+
function iterMemoryBatches(memories) {
|
|
458
|
+
const memoryBatches = [];
|
|
459
|
+
for (let index = 0; index < memories.length; index += MAX_DIRECT_MEMORIES_PER_GENERATE_CALL) {
|
|
460
|
+
memoryBatches.push(
|
|
461
|
+
memories.slice(index, index + MAX_DIRECT_MEMORIES_PER_GENERATE_CALL)
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
return memoryBatches;
|
|
465
|
+
}
|
|
466
|
+
export {
|
|
467
|
+
VertexAiMemoryBankService
|
|
468
|
+
};
|
|
@@ -2,13 +2,17 @@ import {createRequire as topLevelCreateRequire} from 'module';
|
|
|
2
2
|
const require = topLevelCreateRequire(import.meta.url);
|
|
3
3
|
/**
|
|
4
4
|
* @license
|
|
5
|
-
* Copyright
|
|
5
|
+
* Copyright 2026 Google LLC
|
|
6
6
|
* SPDX-License-Identifier: Apache-2.0
|
|
7
7
|
*/
|
|
8
|
+
import { LiveResponseAggregator } from "../utils/live_connection_utils.js";
|
|
8
9
|
import { logger } from "../utils/logger.js";
|
|
10
|
+
import { isGemini3xFlashLive } from "../utils/model_name.js";
|
|
9
11
|
class GeminiLlmConnection {
|
|
10
|
-
constructor(geminiSession) {
|
|
12
|
+
constructor(geminiSession, modelVersion, messageQueue) {
|
|
11
13
|
this.geminiSession = geminiSession;
|
|
14
|
+
this.modelVersion = modelVersion;
|
|
15
|
+
this.messageQueue = messageQueue;
|
|
12
16
|
}
|
|
13
17
|
/**
|
|
14
18
|
* Sends the conversation history to the gemini model.
|
|
@@ -27,9 +31,10 @@ class GeminiLlmConnection {
|
|
|
27
31
|
}
|
|
28
32
|
);
|
|
29
33
|
if (contents.length > 0) {
|
|
34
|
+
const isGemini3x = isGemini3xFlashLive(this.modelVersion);
|
|
30
35
|
this.geminiSession.sendClientContent({
|
|
31
36
|
turns: contents,
|
|
32
|
-
turnComplete: contents[contents.length - 1].role === "user"
|
|
37
|
+
turnComplete: isGemini3x ? true : contents[contents.length - 1].role === "user"
|
|
33
38
|
});
|
|
34
39
|
} else {
|
|
35
40
|
logger.info("no content is sent");
|
|
@@ -56,10 +61,16 @@ class GeminiLlmConnection {
|
|
|
56
61
|
});
|
|
57
62
|
} else {
|
|
58
63
|
logger.debug("Sending LLM new content", content);
|
|
59
|
-
this.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
const isGemini3x = isGemini3xFlashLive(this.modelVersion);
|
|
65
|
+
if (isGemini3x && content.parts.length === 1 && content.parts[0].text) {
|
|
66
|
+
logger.debug("Using sendRealtimeInput for Gemini 3.x text input");
|
|
67
|
+
this.geminiSession.sendRealtimeInput({ text: content.parts[0].text });
|
|
68
|
+
} else {
|
|
69
|
+
this.geminiSession.sendClientContent({
|
|
70
|
+
turns: [content],
|
|
71
|
+
turnComplete: true
|
|
72
|
+
});
|
|
73
|
+
}
|
|
63
74
|
}
|
|
64
75
|
}
|
|
65
76
|
/**
|
|
@@ -68,8 +79,36 @@ class GeminiLlmConnection {
|
|
|
68
79
|
* @param blob The blob to send to the model.
|
|
69
80
|
*/
|
|
70
81
|
async sendRealtime(blob) {
|
|
82
|
+
var _a, _b, _c;
|
|
71
83
|
logger.debug("Sending LLM Blob:", blob);
|
|
72
|
-
|
|
84
|
+
const isGemini3x = isGemini3xFlashLive(this.modelVersion);
|
|
85
|
+
const isNativeAudio = (_a = this.modelVersion) == null ? void 0 : _a.includes("native-audio");
|
|
86
|
+
if (isGemini3x || isNativeAudio) {
|
|
87
|
+
if ((_b = blob.mimeType) == null ? void 0 : _b.startsWith("audio/")) {
|
|
88
|
+
this.geminiSession.sendRealtimeInput({ audio: blob });
|
|
89
|
+
} else if ((_c = blob.mimeType) == null ? void 0 : _c.startsWith("image/")) {
|
|
90
|
+
this.geminiSession.sendRealtimeInput({ video: blob });
|
|
91
|
+
} else {
|
|
92
|
+
logger.warn(
|
|
93
|
+
"Blob not sent. Unknown or empty mime type for sendRealtimeInput:",
|
|
94
|
+
blob.mimeType
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
this.geminiSession.sendRealtimeInput({ media: blob });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Sends an activity start signal to the model.
|
|
103
|
+
*/
|
|
104
|
+
async sendActivityStart() {
|
|
105
|
+
this.geminiSession.sendRealtimeInput({ activityStart: {} });
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Sends an activity end signal to the model.
|
|
109
|
+
*/
|
|
110
|
+
async sendActivityEnd() {
|
|
111
|
+
this.geminiSession.sendRealtimeInput({ activityEnd: {} });
|
|
73
112
|
}
|
|
74
113
|
/**
|
|
75
114
|
* Builds a full text response.
|
|
@@ -78,20 +117,24 @@ class GeminiLlmConnection {
|
|
|
78
117
|
* partial.
|
|
79
118
|
*
|
|
80
119
|
* @param text The text to be included in the response.
|
|
120
|
+
* @param isThought Whether the text is a thought.
|
|
121
|
+
* @param groundingMetadata The grounding metadata to include.
|
|
81
122
|
* @returns An LlmResponse containing the full text.
|
|
82
123
|
*/
|
|
83
|
-
buildFullTextResponse(text) {
|
|
84
|
-
return {
|
|
85
|
-
content: {
|
|
86
|
-
role: "model",
|
|
87
|
-
parts: [{ text }]
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
// TODO(b/425992518): GenAI SDK inconsistent API, missing methods.
|
|
92
|
-
// eslint-disable-next-line require-yield
|
|
93
124
|
async *receive() {
|
|
94
|
-
|
|
125
|
+
if (!this.messageQueue) {
|
|
126
|
+
throw new Error("Message queue is not initialized.");
|
|
127
|
+
}
|
|
128
|
+
const aggregator = new LiveResponseAggregator(this.modelVersion);
|
|
129
|
+
for await (const message of this.messageQueue) {
|
|
130
|
+
logger.debug("Got LLM Live message:", message);
|
|
131
|
+
for (const response of aggregator.processMessage(message)) {
|
|
132
|
+
yield response;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const response of aggregator.close()) {
|
|
136
|
+
yield response;
|
|
137
|
+
}
|
|
95
138
|
}
|
|
96
139
|
/**
|
|
97
140
|
* Closes the llm server connection.
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
import { getBooleanEnvVar, isBrowser } from "../utils/env_aware_utils.js";
|
|
13
13
|
import { logger } from "../utils/logger.js";
|
|
14
14
|
import { GoogleLLMVariant } from "../utils/variant_utils.js";
|
|
15
|
+
import { AsyncQueue } from "../utils/async_queue.js";
|
|
15
16
|
import { StreamingResponseAggregator } from "../utils/streaming_utils.js";
|
|
16
17
|
import { BaseLlm } from "./base_llm.js";
|
|
17
18
|
import { GeminiLlmConnection } from "./gemini_llm_connection.js";
|
|
@@ -143,10 +144,19 @@ class Gemini extends BaseLlm {
|
|
|
143
144
|
}
|
|
144
145
|
get liveApiClient() {
|
|
145
146
|
if (!this._liveApiClient) {
|
|
146
|
-
this.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
if (this.vertexai) {
|
|
148
|
+
this._liveApiClient = new GoogleGenAI({
|
|
149
|
+
vertexai: this.vertexai,
|
|
150
|
+
project: this.project,
|
|
151
|
+
location: this.location || "global",
|
|
152
|
+
httpOptions: this.getLiveHttpOptions()
|
|
153
|
+
});
|
|
154
|
+
} else {
|
|
155
|
+
this._liveApiClient = new GoogleGenAI({
|
|
156
|
+
apiKey: this.apiKey,
|
|
157
|
+
httpOptions: this.getLiveHttpOptions()
|
|
158
|
+
});
|
|
159
|
+
}
|
|
150
160
|
}
|
|
151
161
|
return this._liveApiClient;
|
|
152
162
|
}
|
|
@@ -178,16 +188,24 @@ class Gemini extends BaseLlm {
|
|
|
178
188
|
};
|
|
179
189
|
}
|
|
180
190
|
llmRequest.liveConnectConfig.tools = (_c = llmRequest.config) == null ? void 0 : _c.tools;
|
|
191
|
+
const modelVersion = (_d = llmRequest.model) != null ? _d : this.model;
|
|
192
|
+
const messageQueue = new AsyncQueue();
|
|
181
193
|
const liveSession = await this.liveApiClient.live.connect({
|
|
182
|
-
model:
|
|
194
|
+
model: modelVersion,
|
|
183
195
|
config: llmRequest.liveConnectConfig,
|
|
184
196
|
callbacks: {
|
|
185
|
-
|
|
186
|
-
|
|
197
|
+
onmessage: (message) => {
|
|
198
|
+
messageQueue.push(message);
|
|
199
|
+
},
|
|
200
|
+
onerror: (error) => {
|
|
201
|
+
messageQueue.error(error);
|
|
202
|
+
},
|
|
203
|
+
onclose: () => {
|
|
204
|
+
messageQueue.close();
|
|
187
205
|
}
|
|
188
206
|
}
|
|
189
207
|
});
|
|
190
|
-
return new GeminiLlmConnection(liveSession);
|
|
208
|
+
return new GeminiLlmConnection(liveSession, modelVersion, messageQueue);
|
|
191
209
|
}
|
|
192
210
|
preprocessRequest(llmRequest) {
|
|
193
211
|
if (this.apiBackend === GoogleLLMVariant.GEMINI_API) {
|
|
@@ -16,6 +16,11 @@ var PolicyOutcome = /* @__PURE__ */ ((PolicyOutcome2) => {
|
|
|
16
16
|
return PolicyOutcome2;
|
|
17
17
|
})(PolicyOutcome || {});
|
|
18
18
|
class InMemoryPolicyEngine {
|
|
19
|
+
/**
|
|
20
|
+
* Always returns {@link PolicyOutcome.ALLOW} for every tool call.
|
|
21
|
+
*
|
|
22
|
+
* @returns A promise resolving to an ALLOW result.
|
|
23
|
+
*/
|
|
19
24
|
async evaluate() {
|
|
20
25
|
return Promise.resolve({
|
|
21
26
|
outcome: "ALLOW" /* ALLOW */,
|
|
@@ -24,11 +29,22 @@ class InMemoryPolicyEngine {
|
|
|
24
29
|
}
|
|
25
30
|
}
|
|
26
31
|
class SecurityPlugin extends BasePlugin {
|
|
32
|
+
/**
|
|
33
|
+
* @param params - Optional configuration. Defaults to {@link InMemoryPolicyEngine}
|
|
34
|
+
* when no policy engine is provided.
|
|
35
|
+
*/
|
|
27
36
|
constructor(params) {
|
|
28
37
|
var _a;
|
|
29
38
|
super("security_plugin");
|
|
30
39
|
this.policyEngine = (_a = params == null ? void 0 : params.policyEngine) != null ? _a : new InMemoryPolicyEngine();
|
|
31
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Intercepts tool calls, evaluating them against the policy engine before
|
|
43
|
+
* execution and handling confirmation flows for calls that require it.
|
|
44
|
+
*
|
|
45
|
+
* @returns A partial or error response object if the call is blocked or
|
|
46
|
+
* awaiting confirmation, or `undefined` to allow execution to proceed.
|
|
47
|
+
*/
|
|
32
48
|
async beforeToolCallback({
|
|
33
49
|
tool,
|
|
34
50
|
toolArgs,
|