@paean-ai/adk 0.2.19 → 0.2.21
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 +44 -0
- package/dist/cjs/agents/base_agent.js +245 -0
- package/dist/cjs/agents/base_llm_processor.js +44 -0
- package/dist/cjs/agents/callback_context.js +98 -0
- package/dist/cjs/agents/content_processor_utils.js +311 -0
- package/dist/cjs/agents/functions.js +425 -0
- package/dist/cjs/agents/instructions.js +110 -0
- package/dist/cjs/agents/invocation_context.js +107 -0
- package/dist/cjs/agents/live_request_queue.js +136 -0
- package/dist/cjs/agents/llm_agent.js +1279 -0
- package/dist/cjs/agents/loop_agent.js +68 -0
- package/dist/cjs/agents/parallel_agent.js +78 -0
- package/dist/cjs/agents/readonly_context.js +68 -0
- package/dist/cjs/agents/run_config.js +70 -0
- package/dist/cjs/agents/sequential_agent.js +84 -0
- package/dist/cjs/agents/transcription_entry.js +27 -0
- package/dist/cjs/artifacts/base_artifact_service.js +27 -0
- package/dist/cjs/artifacts/gcs_artifact_service.js +140 -0
- package/dist/cjs/artifacts/in_memory_artifact_service.js +119 -0
- package/dist/cjs/auth/auth_credential.js +46 -0
- package/dist/cjs/auth/auth_handler.js +92 -0
- package/dist/cjs/auth/auth_schemes.js +62 -0
- package/dist/cjs/auth/auth_tool.js +27 -0
- package/dist/cjs/auth/credential_service/base_credential_service.js +27 -0
- package/dist/cjs/auth/credential_service/in_memory_credential_service.js +63 -0
- package/dist/cjs/auth/exchanger/base_credential_exchanger.js +40 -0
- package/dist/cjs/auth/exchanger/credential_exchanger_registry.js +59 -0
- package/dist/cjs/code_executors/base_code_executor.js +76 -0
- package/dist/cjs/code_executors/built_in_code_executor.js +58 -0
- package/dist/cjs/code_executors/code_execution_utils.js +142 -0
- package/dist/cjs/code_executors/code_executor_context.js +198 -0
- package/dist/cjs/common.js +181 -0
- package/dist/cjs/events/event.js +119 -0
- package/dist/cjs/events/event_actions.js +83 -0
- package/dist/cjs/examples/base_example_provider.js +40 -0
- package/dist/cjs/examples/example.js +27 -0
- package/dist/cjs/examples/example_util.js +107 -0
- package/dist/cjs/index.js +12 -12
- package/dist/cjs/index.js.map +3 -3
- package/dist/cjs/index_web.js +33 -0
- package/dist/cjs/memory/base_memory_service.js +27 -0
- package/dist/cjs/memory/in_memory_memory_service.js +97 -0
- package/dist/cjs/memory/memory_entry.js +27 -0
- package/dist/cjs/models/base_llm.js +95 -0
- package/dist/cjs/models/base_llm_connection.js +27 -0
- package/dist/cjs/models/gemini_llm_connection.js +132 -0
- package/dist/cjs/models/google_llm.js +472 -0
- package/dist/cjs/models/llm_request.js +82 -0
- package/dist/cjs/models/llm_response.js +71 -0
- package/dist/cjs/models/registry.js +121 -0
- package/dist/cjs/plugins/base_plugin.js +236 -0
- package/dist/cjs/plugins/logging_plugin.js +222 -0
- package/dist/cjs/plugins/plugin_manager.js +239 -0
- package/dist/cjs/plugins/security_plugin.js +153 -0
- package/dist/cjs/runner/in_memory_runner.js +58 -0
- package/dist/cjs/runner/runner.js +277 -0
- package/dist/cjs/sessions/base_session_service.js +71 -0
- package/dist/cjs/sessions/in_memory_session_service.js +184 -0
- package/dist/cjs/sessions/session.js +48 -0
- package/dist/cjs/sessions/state.js +101 -0
- package/dist/cjs/telemetry/google_cloud.js +85 -0
- package/dist/cjs/telemetry/setup.js +97 -0
- package/dist/cjs/telemetry/tracing.js +231 -0
- package/dist/cjs/tools/agent_tool.js +134 -0
- package/dist/cjs/tools/base_tool.js +107 -0
- package/dist/cjs/tools/base_toolset.js +76 -0
- package/dist/cjs/tools/forwarding_artifact_service.js +71 -0
- package/dist/cjs/tools/function_tool.js +101 -0
- package/dist/cjs/tools/google_search_tool.js +77 -0
- package/dist/cjs/tools/long_running_tool.js +63 -0
- package/dist/cjs/tools/mcp/mcp_session_manager.js +65 -0
- package/dist/cjs/tools/mcp/mcp_tool.js +65 -0
- package/dist/cjs/tools/mcp/mcp_toolset.js +61 -0
- package/dist/cjs/tools/tool_confirmation.js +49 -0
- package/dist/cjs/tools/tool_context.js +129 -0
- package/dist/cjs/utils/client_labels.js +56 -0
- package/dist/cjs/utils/deep_clone.js +44 -0
- package/dist/cjs/utils/env_aware_utils.js +83 -0
- package/dist/cjs/utils/gemini_schema_util.js +88 -0
- package/dist/cjs/utils/logger.js +121 -0
- package/dist/cjs/utils/model_name.js +76 -0
- package/dist/cjs/utils/simple_zod_to_json.js +191 -0
- package/dist/cjs/utils/variant_utils.js +55 -0
- package/dist/cjs/version.js +39 -0
- package/dist/esm/agents/active_streaming_tool.js +14 -0
- package/dist/esm/agents/base_agent.js +214 -0
- package/dist/esm/agents/base_llm_processor.js +13 -0
- package/dist/esm/agents/callback_context.js +68 -0
- package/dist/esm/agents/content_processor_utils.js +280 -0
- package/dist/esm/agents/functions.js +384 -0
- package/dist/esm/agents/instructions.js +80 -0
- package/dist/esm/agents/invocation_context.js +76 -0
- package/dist/esm/agents/live_request_queue.js +106 -0
- package/dist/esm/agents/llm_agent.js +1247 -0
- package/dist/esm/agents/loop_agent.js +38 -0
- package/dist/esm/agents/parallel_agent.js +48 -0
- package/dist/esm/agents/readonly_context.js +38 -0
- package/dist/esm/agents/run_config.js +39 -0
- package/dist/esm/agents/sequential_agent.js +54 -0
- package/dist/esm/agents/transcription_entry.js +5 -0
- package/dist/esm/artifacts/base_artifact_service.js +5 -0
- package/dist/esm/artifacts/gcs_artifact_service.js +110 -0
- package/dist/esm/artifacts/in_memory_artifact_service.js +89 -0
- package/dist/esm/auth/auth_credential.js +16 -0
- package/dist/esm/auth/auth_handler.js +62 -0
- package/dist/esm/auth/auth_schemes.js +31 -0
- package/dist/esm/auth/auth_tool.js +5 -0
- package/dist/esm/auth/credential_service/base_credential_service.js +5 -0
- package/dist/esm/auth/credential_service/in_memory_credential_service.js +33 -0
- package/dist/esm/auth/exchanger/base_credential_exchanger.js +10 -0
- package/dist/esm/auth/exchanger/credential_exchanger_registry.js +29 -0
- package/dist/esm/code_executors/base_code_executor.js +46 -0
- package/dist/esm/code_executors/built_in_code_executor.js +28 -0
- package/dist/esm/code_executors/code_execution_utils.js +108 -0
- package/dist/esm/code_executors/code_executor_context.js +168 -0
- package/dist/esm/common.js +98 -0
- package/dist/esm/events/event.js +83 -0
- package/dist/esm/events/event_actions.js +52 -0
- package/dist/esm/examples/base_example_provider.js +10 -0
- package/dist/esm/examples/example.js +5 -0
- package/dist/esm/examples/example_util.js +76 -0
- package/dist/esm/index.js +12 -12
- package/dist/esm/index.js.map +3 -3
- package/dist/esm/index_web.js +6 -0
- package/dist/esm/memory/base_memory_service.js +5 -0
- package/dist/esm/memory/in_memory_memory_service.js +67 -0
- package/dist/esm/memory/memory_entry.js +5 -0
- package/dist/esm/models/base_llm.js +64 -0
- package/dist/esm/models/base_llm_connection.js +5 -0
- package/dist/esm/models/gemini_llm_connection.js +102 -0
- package/dist/esm/models/google_llm.js +446 -0
- package/dist/esm/models/llm_request.js +50 -0
- package/dist/esm/models/llm_response.js +41 -0
- package/dist/esm/models/registry.js +91 -0
- package/dist/esm/plugins/base_plugin.js +206 -0
- package/dist/esm/plugins/logging_plugin.js +192 -0
- package/dist/esm/plugins/plugin_manager.js +209 -0
- package/dist/esm/plugins/security_plugin.js +119 -0
- package/dist/esm/runner/in_memory_runner.js +28 -0
- package/dist/esm/runner/runner.js +247 -0
- package/dist/esm/sessions/base_session_service.js +41 -0
- package/dist/esm/sessions/in_memory_session_service.js +154 -0
- package/dist/esm/sessions/session.js +18 -0
- package/dist/esm/sessions/state.js +71 -0
- package/dist/esm/telemetry/google_cloud.js +54 -0
- package/dist/esm/telemetry/setup.js +67 -0
- package/dist/esm/telemetry/tracing.js +195 -0
- package/dist/esm/tools/agent_tool.js +104 -0
- package/dist/esm/tools/base_tool.js +77 -0
- package/dist/esm/tools/base_toolset.js +46 -0
- package/dist/esm/tools/forwarding_artifact_service.js +41 -0
- package/dist/esm/tools/function_tool.js +71 -0
- package/dist/esm/tools/google_search_tool.js +47 -0
- package/dist/esm/tools/long_running_tool.js +33 -0
- package/dist/esm/tools/mcp/mcp_session_manager.js +35 -0
- package/dist/esm/tools/mcp/mcp_tool.js +35 -0
- package/dist/esm/tools/mcp/mcp_toolset.js +31 -0
- package/dist/esm/tools/tool_confirmation.js +19 -0
- package/dist/esm/tools/tool_context.js +99 -0
- package/dist/esm/utils/client_labels.js +26 -0
- package/dist/esm/utils/deep_clone.js +14 -0
- package/dist/esm/utils/env_aware_utils.js +49 -0
- package/dist/esm/utils/gemini_schema_util.js +58 -0
- package/dist/esm/utils/logger.js +89 -0
- package/dist/esm/utils/model_name.js +41 -0
- package/dist/esm/utils/simple_zod_to_json.js +160 -0
- package/dist/esm/utils/variant_utils.js +24 -0
- package/dist/esm/version.js +9 -0
- package/dist/types/agents/llm_agent.d.ts +1 -0
- package/dist/types/models/google_llm.d.ts +0 -7
- package/dist/web/agents/active_streaming_tool.js +14 -0
- package/dist/web/agents/base_agent.js +265 -0
- package/dist/web/agents/base_llm_processor.js +13 -0
- package/dist/web/agents/callback_context.js +68 -0
- package/dist/web/agents/content_processor_utils.js +280 -0
- package/dist/web/agents/functions.js +384 -0
- package/dist/web/agents/instructions.js +80 -0
- package/dist/web/agents/invocation_context.js +76 -0
- package/dist/web/agents/live_request_queue.js +124 -0
- package/dist/web/agents/llm_agent.js +1377 -0
- package/dist/web/agents/loop_agent.js +71 -0
- package/dist/web/agents/parallel_agent.js +83 -0
- package/dist/web/agents/readonly_context.js +38 -0
- package/dist/web/agents/run_config.js +54 -0
- package/dist/web/agents/sequential_agent.js +99 -0
- package/dist/web/agents/transcription_entry.js +5 -0
- package/dist/web/artifacts/base_artifact_service.js +5 -0
- package/dist/web/artifacts/gcs_artifact_service.js +126 -0
- package/dist/web/artifacts/in_memory_artifact_service.js +89 -0
- package/dist/web/auth/auth_credential.js +16 -0
- package/dist/web/auth/auth_handler.js +62 -0
- package/dist/web/auth/auth_schemes.js +31 -0
- package/dist/web/auth/auth_tool.js +5 -0
- package/dist/web/auth/credential_service/base_credential_service.js +5 -0
- package/dist/web/auth/credential_service/in_memory_credential_service.js +33 -0
- package/dist/web/auth/exchanger/base_credential_exchanger.js +10 -0
- package/dist/web/auth/exchanger/credential_exchanger_registry.js +29 -0
- package/dist/web/code_executors/base_code_executor.js +46 -0
- package/dist/web/code_executors/built_in_code_executor.js +28 -0
- package/dist/web/code_executors/code_execution_utils.js +105 -0
- package/dist/web/code_executors/code_executor_context.js +168 -0
- package/dist/web/common.js +98 -0
- package/dist/web/events/event.js +101 -0
- package/dist/web/events/event_actions.js +67 -0
- package/dist/web/examples/base_example_provider.js +10 -0
- package/dist/web/examples/example.js +5 -0
- package/dist/web/examples/example_util.js +75 -0
- package/dist/web/index.js +1 -1
- package/dist/web/index.js.map +3 -3
- package/dist/web/index_web.js +6 -0
- package/dist/web/memory/base_memory_service.js +5 -0
- package/dist/web/memory/in_memory_memory_service.js +67 -0
- package/dist/web/memory/memory_entry.js +5 -0
- package/dist/web/models/base_llm.js +64 -0
- package/dist/web/models/base_llm_connection.js +5 -0
- package/dist/web/models/gemini_llm_connection.js +120 -0
- package/dist/web/models/google_llm.js +487 -0
- package/dist/web/models/llm_request.js +50 -0
- package/dist/web/models/llm_response.js +41 -0
- package/dist/web/models/registry.js +91 -0
- package/dist/web/plugins/base_plugin.js +206 -0
- package/dist/web/plugins/logging_plugin.js +192 -0
- package/dist/web/plugins/plugin_manager.js +209 -0
- package/dist/web/plugins/security_plugin.js +119 -0
- package/dist/web/runner/in_memory_runner.js +28 -0
- package/dist/web/runner/runner.js +278 -0
- package/dist/web/sessions/base_session_service.js +41 -0
- package/dist/web/sessions/in_memory_session_service.js +154 -0
- package/dist/web/sessions/session.js +18 -0
- package/dist/web/sessions/state.js +87 -0
- package/dist/web/telemetry/google_cloud.js +54 -0
- package/dist/web/telemetry/setup.js +67 -0
- package/dist/web/telemetry/tracing.js +210 -0
- package/dist/web/tools/agent_tool.js +118 -0
- package/dist/web/tools/base_tool.js +77 -0
- package/dist/web/tools/base_toolset.js +46 -0
- package/dist/web/tools/forwarding_artifact_service.js +41 -0
- package/dist/web/tools/function_tool.js +71 -0
- package/dist/web/tools/google_search_tool.js +47 -0
- package/dist/web/tools/long_running_tool.js +50 -0
- package/dist/web/tools/mcp/mcp_session_manager.js +35 -0
- package/dist/web/tools/mcp/mcp_tool.js +35 -0
- package/dist/web/tools/mcp/mcp_toolset.js +31 -0
- package/dist/web/tools/tool_confirmation.js +19 -0
- package/dist/web/tools/tool_context.js +99 -0
- package/dist/web/utils/client_labels.js +26 -0
- package/dist/web/utils/deep_clone.js +14 -0
- package/dist/web/utils/env_aware_utils.js +49 -0
- package/dist/web/utils/gemini_schema_util.js +58 -0
- package/dist/web/utils/logger.js +89 -0
- package/dist/web/utils/model_name.js +41 -0
- package/dist/web/utils/simple_zod_to_json.js +174 -0
- package/dist/web/utils/variant_utils.js +24 -0
- package/dist/web/version.js +9 -0
- package/package.json +1 -1
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
class InMemoryMemoryService {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.memories = [];
|
|
9
|
+
this.sessionEvents = {};
|
|
10
|
+
}
|
|
11
|
+
async addSessionToMemory(session) {
|
|
12
|
+
const userKey = getUserKey(session.appName, session.userId);
|
|
13
|
+
if (!this.sessionEvents[userKey]) {
|
|
14
|
+
this.sessionEvents[userKey] = {};
|
|
15
|
+
}
|
|
16
|
+
this.sessionEvents[userKey][session.id] = session.events.filter(
|
|
17
|
+
(event) => {
|
|
18
|
+
var _a, _b, _c;
|
|
19
|
+
return ((_c = (_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.length) != null ? _c : 0) > 0;
|
|
20
|
+
}
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
async searchMemory(req) {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
const userKey = getUserKey(req.appName, req.userId);
|
|
26
|
+
if (!this.sessionEvents[userKey]) {
|
|
27
|
+
return Promise.resolve({ memories: [] });
|
|
28
|
+
}
|
|
29
|
+
const wordsInQuery = req.query.toLowerCase().split(/\s+/);
|
|
30
|
+
const response = { memories: [] };
|
|
31
|
+
for (const sessionEvents of Object.values(this.sessionEvents[userKey])) {
|
|
32
|
+
for (const event of sessionEvents) {
|
|
33
|
+
if (!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.length)) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const joinedText = event.content.parts.map((part) => part.text).filter((text) => !!text).join(" ");
|
|
37
|
+
const wordsInEvent = extractWordsLower(joinedText);
|
|
38
|
+
if (!wordsInEvent.size) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const matchQuery = wordsInQuery.some((queryWord) => wordsInEvent.has(queryWord));
|
|
42
|
+
if (matchQuery) {
|
|
43
|
+
response.memories.push({
|
|
44
|
+
content: event.content,
|
|
45
|
+
author: event.author,
|
|
46
|
+
timestamp: formatTimestamp(event.timestamp)
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return response;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function getUserKey(appName, userId) {
|
|
55
|
+
return `${appName}/${userId}`;
|
|
56
|
+
}
|
|
57
|
+
function extractWordsLower(text) {
|
|
58
|
+
return new Set(
|
|
59
|
+
[...text.matchAll(/[A-Za-z]+/)].map((match) => match[0].toLowerCase())
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
function formatTimestamp(timestamp) {
|
|
63
|
+
return new Date(timestamp).toISOString();
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
InMemoryMemoryService
|
|
67
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
var _a;
|
|
7
|
+
import { getClientLabels } from "../utils/client_labels.js";
|
|
8
|
+
const BASE_MODEL_SYMBOL = Symbol("baseModel");
|
|
9
|
+
function isBaseLlm(obj) {
|
|
10
|
+
return typeof obj === "object" && obj !== null && BASE_MODEL_SYMBOL in obj && obj[BASE_MODEL_SYMBOL] === true;
|
|
11
|
+
}
|
|
12
|
+
_a = BASE_MODEL_SYMBOL;
|
|
13
|
+
class BaseLlm {
|
|
14
|
+
/**
|
|
15
|
+
* Creates an instance of BaseLLM.
|
|
16
|
+
* @param params The parameters for creating a BaseLlm instance.
|
|
17
|
+
* @param params.model The name of the LLM, e.g. gemini-1.5-flash or
|
|
18
|
+
* gemini-1.5-flash-001.
|
|
19
|
+
*/
|
|
20
|
+
constructor({ model }) {
|
|
21
|
+
this[_a] = true;
|
|
22
|
+
this.model = model;
|
|
23
|
+
}
|
|
24
|
+
get trackingHeaders() {
|
|
25
|
+
const labels = getClientLabels();
|
|
26
|
+
const headerValue = labels.join(" ");
|
|
27
|
+
return {
|
|
28
|
+
"x-goog-api-client": headerValue,
|
|
29
|
+
"user-agent": headerValue
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Appends a user content, so that model can continue to output.
|
|
34
|
+
*
|
|
35
|
+
* @param llmRequest LlmRequest, the request to send to the LLM.
|
|
36
|
+
*/
|
|
37
|
+
maybeAppendUserContent(llmRequest) {
|
|
38
|
+
var _a2;
|
|
39
|
+
if (llmRequest.contents.length === 0) {
|
|
40
|
+
llmRequest.contents.push({
|
|
41
|
+
role: "user",
|
|
42
|
+
parts: [
|
|
43
|
+
{ text: "Handle the requests as specified in the System Instruction." }
|
|
44
|
+
]
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
if (((_a2 = llmRequest.contents[llmRequest.contents.length - 1]) == null ? void 0 : _a2.role) !== "user") {
|
|
48
|
+
llmRequest.contents.push({
|
|
49
|
+
role: "user",
|
|
50
|
+
parts: [{
|
|
51
|
+
text: "Continue processing previous requests as instructed. Exit or provide a summary if no more outputs are needed."
|
|
52
|
+
}]
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* List of supported models in regex for LlmRegistry.
|
|
59
|
+
*/
|
|
60
|
+
BaseLlm.supportedModels = [];
|
|
61
|
+
export {
|
|
62
|
+
BaseLlm,
|
|
63
|
+
isBaseLlm
|
|
64
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { logger } from "../utils/logger.js";
|
|
7
|
+
class GeminiLlmConnection {
|
|
8
|
+
constructor(geminiSession) {
|
|
9
|
+
this.geminiSession = geminiSession;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Sends the conversation history to the gemini model.
|
|
13
|
+
*
|
|
14
|
+
* You call this method right after setting up the model connection.
|
|
15
|
+
* The model will respond if the last content is from user, otherwise it will
|
|
16
|
+
* wait for new user input before responding.
|
|
17
|
+
*
|
|
18
|
+
* @param history The conversation history to send to the model.
|
|
19
|
+
*/
|
|
20
|
+
async sendHistory(history) {
|
|
21
|
+
const contents = history.filter(
|
|
22
|
+
(content) => {
|
|
23
|
+
var _a;
|
|
24
|
+
return content.parts && ((_a = content.parts[0]) == null ? void 0 : _a.text);
|
|
25
|
+
}
|
|
26
|
+
);
|
|
27
|
+
if (contents.length > 0) {
|
|
28
|
+
this.geminiSession.sendClientContent({
|
|
29
|
+
turns: contents,
|
|
30
|
+
turnComplete: contents[contents.length - 1].role === "user"
|
|
31
|
+
});
|
|
32
|
+
} else {
|
|
33
|
+
logger.info("no content is sent");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Sends a user content to the gemini model.
|
|
38
|
+
*
|
|
39
|
+
* The model will respond immediately upon receiving the content.
|
|
40
|
+
* If you send function responses, all parts in the content should be function
|
|
41
|
+
* responses.
|
|
42
|
+
*
|
|
43
|
+
* @param content The content to send to the model.
|
|
44
|
+
*/
|
|
45
|
+
async sendContent(content) {
|
|
46
|
+
if (!content.parts) {
|
|
47
|
+
throw new Error("Content must have parts.");
|
|
48
|
+
}
|
|
49
|
+
if (content.parts[0].functionResponse) {
|
|
50
|
+
const functionResponses = content.parts.map((part) => part.functionResponse).filter((fr) => !!fr);
|
|
51
|
+
logger.debug("Sending LLM function response:", functionResponses);
|
|
52
|
+
this.geminiSession.sendToolResponse({
|
|
53
|
+
functionResponses
|
|
54
|
+
});
|
|
55
|
+
} else {
|
|
56
|
+
logger.debug("Sending LLM new content", content);
|
|
57
|
+
this.geminiSession.sendClientContent({
|
|
58
|
+
turns: [content],
|
|
59
|
+
turnComplete: true
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Sends a chunk of audio or a frame of video to the model in realtime.
|
|
65
|
+
*
|
|
66
|
+
* @param blob The blob to send to the model.
|
|
67
|
+
*/
|
|
68
|
+
async sendRealtime(blob) {
|
|
69
|
+
logger.debug("Sending LLM Blob:", blob);
|
|
70
|
+
this.geminiSession.sendRealtimeInput({ media: blob });
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Builds a full text response.
|
|
74
|
+
*
|
|
75
|
+
* The text should not be partial and the returned LlmResponse is not be
|
|
76
|
+
* partial.
|
|
77
|
+
*
|
|
78
|
+
* @param text The text to be included in the response.
|
|
79
|
+
* @returns An LlmResponse containing the full text.
|
|
80
|
+
*/
|
|
81
|
+
buildFullTextResponse(text) {
|
|
82
|
+
return {
|
|
83
|
+
content: {
|
|
84
|
+
role: "model",
|
|
85
|
+
parts: [{ text }]
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
// TODO(b/425992518): GenAI SDK inconsistent API, missing methods.
|
|
90
|
+
async *receive() {
|
|
91
|
+
throw new Error("Not Implemented.");
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Closes the llm server connection.
|
|
95
|
+
*/
|
|
96
|
+
async close() {
|
|
97
|
+
this.geminiSession.close();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
export {
|
|
101
|
+
GeminiLlmConnection
|
|
102
|
+
};
|
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
createPartFromText,
|
|
8
|
+
FinishReason,
|
|
9
|
+
GoogleGenAI
|
|
10
|
+
} from "@google/genai";
|
|
11
|
+
import { logger } from "../utils/logger.js";
|
|
12
|
+
import { isGemini3PreviewModel } from "../utils/model_name.js";
|
|
13
|
+
import { GoogleLLMVariant } from "../utils/variant_utils.js";
|
|
14
|
+
import { BaseLlm } from "./base_llm.js";
|
|
15
|
+
import { GeminiLlmConnection } from "./gemini_llm_connection.js";
|
|
16
|
+
import { createLlmResponse } from "./llm_response.js";
|
|
17
|
+
const AGENT_ENGINE_TELEMETRY_TAG = "remote_reasoning_engine";
|
|
18
|
+
const AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME = "GOOGLE_CLOUD_AGENT_ENGINE_ID";
|
|
19
|
+
const GEMINI3_PREVIEW_API_ENDPOINT = "https://aiplatform.googleapis.com/v1/publishers/google";
|
|
20
|
+
class Gemini extends BaseLlm {
|
|
21
|
+
/**
|
|
22
|
+
* @param params The parameters for creating a Gemini instance.
|
|
23
|
+
*/
|
|
24
|
+
constructor({
|
|
25
|
+
model,
|
|
26
|
+
apiKey,
|
|
27
|
+
vertexai,
|
|
28
|
+
project,
|
|
29
|
+
location,
|
|
30
|
+
headers,
|
|
31
|
+
apiEndpoint
|
|
32
|
+
}) {
|
|
33
|
+
if (!model) {
|
|
34
|
+
model = "gemini-2.5-flash";
|
|
35
|
+
}
|
|
36
|
+
super({ model });
|
|
37
|
+
this.project = project;
|
|
38
|
+
this.location = location;
|
|
39
|
+
this.apiKey = apiKey;
|
|
40
|
+
this.headers = headers;
|
|
41
|
+
this.isGemini3Preview = isGemini3PreviewModel(model);
|
|
42
|
+
const canReadEnv = typeof process === "object";
|
|
43
|
+
this.apiEndpoint = apiEndpoint;
|
|
44
|
+
if (!this.apiEndpoint && canReadEnv) {
|
|
45
|
+
this.apiEndpoint = process.env["GEMINI_API_ENDPOINT"];
|
|
46
|
+
}
|
|
47
|
+
if (!this.apiEndpoint && this.isGemini3Preview) {
|
|
48
|
+
this.apiEndpoint = GEMINI3_PREVIEW_API_ENDPOINT;
|
|
49
|
+
logger.info(`Using Gemini 3 preview endpoint: ${this.apiEndpoint}`);
|
|
50
|
+
}
|
|
51
|
+
let useVertexAI = !!vertexai;
|
|
52
|
+
if (!useVertexAI && canReadEnv) {
|
|
53
|
+
const vertexAIfromEnv = process.env["GOOGLE_GENAI_USE_VERTEXAI"];
|
|
54
|
+
if (vertexAIfromEnv) {
|
|
55
|
+
useVertexAI = vertexAIfromEnv.toLowerCase() === "true" || vertexAIfromEnv === "1";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (this.isGemini3Preview && useVertexAI) {
|
|
59
|
+
const availableApiKey = apiKey || (canReadEnv ? process.env["GOOGLE_GENAI_API_KEY"] || process.env["GEMINI_API_KEY"] : void 0);
|
|
60
|
+
if (availableApiKey) {
|
|
61
|
+
logger.info(
|
|
62
|
+
"Gemini 3 preview detected with Vertex AI mode. Switching to API key mode for correct endpoint handling."
|
|
63
|
+
);
|
|
64
|
+
useVertexAI = false;
|
|
65
|
+
this.apiKey = availableApiKey;
|
|
66
|
+
} else {
|
|
67
|
+
logger.warn(
|
|
68
|
+
"Gemini 3 preview requires API key authentication for correct endpoint handling. Set GEMINI_API_KEY or GOOGLE_GENAI_API_KEY environment variable for best compatibility."
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
this.vertexai = useVertexAI;
|
|
73
|
+
if (this.vertexai) {
|
|
74
|
+
if (canReadEnv && !this.project) {
|
|
75
|
+
this.project = process.env["GOOGLE_CLOUD_PROJECT"];
|
|
76
|
+
}
|
|
77
|
+
if (canReadEnv && !this.location) {
|
|
78
|
+
this.location = process.env["GOOGLE_CLOUD_LOCATION"];
|
|
79
|
+
}
|
|
80
|
+
if (!this.project) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
"VertexAI project must be provided via constructor or GOOGLE_CLOUD_PROJECT environment variable."
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
if (!this.location) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
"VertexAI location must be provided via constructor or GOOGLE_CLOUD_LOCATION environment variable."
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
if (!this.apiKey && canReadEnv) {
|
|
92
|
+
this.apiKey = process.env["GOOGLE_GENAI_API_KEY"] || process.env["GEMINI_API_KEY"];
|
|
93
|
+
}
|
|
94
|
+
if (!this.apiKey) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
"API key must be provided via constructor or GOOGLE_GENAI_API_KEY or GEMINI_API_KEY environment variable."
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Sends a request to the Gemini model.
|
|
103
|
+
*
|
|
104
|
+
* @param llmRequest LlmRequest, the request to send to the Gemini model.
|
|
105
|
+
* @param stream bool = false, whether to do streaming call.
|
|
106
|
+
* @yields LlmResponse: The model response.
|
|
107
|
+
*/
|
|
108
|
+
async *generateContentAsync(llmRequest, stream = false) {
|
|
109
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
110
|
+
this.preprocessRequest(llmRequest);
|
|
111
|
+
this.maybeAppendUserContent(llmRequest);
|
|
112
|
+
logger.info(
|
|
113
|
+
`Sending out request, model: ${llmRequest.model}, backend: ${this.apiBackend}, stream: ${stream}`
|
|
114
|
+
);
|
|
115
|
+
if ((_a = llmRequest.config) == null ? void 0 : _a.httpOptions) {
|
|
116
|
+
llmRequest.config.httpOptions.headers = {
|
|
117
|
+
...llmRequest.config.httpOptions.headers,
|
|
118
|
+
...this.trackingHeaders
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (stream) {
|
|
122
|
+
const streamResult = await this.apiClient.models.generateContentStream({
|
|
123
|
+
model: (_b = llmRequest.model) != null ? _b : this.model,
|
|
124
|
+
contents: llmRequest.contents,
|
|
125
|
+
config: llmRequest.config
|
|
126
|
+
});
|
|
127
|
+
let thoughtText = "";
|
|
128
|
+
let thoughtSignature;
|
|
129
|
+
let text = "";
|
|
130
|
+
let usageMetadata;
|
|
131
|
+
let lastResponse;
|
|
132
|
+
for await (const response of streamResult) {
|
|
133
|
+
lastResponse = response;
|
|
134
|
+
const llmResponse = createLlmResponse(response);
|
|
135
|
+
usageMetadata = llmResponse.usageMetadata;
|
|
136
|
+
const firstPart = (_d = (_c = llmResponse.content) == null ? void 0 : _c.parts) == null ? void 0 : _d[0];
|
|
137
|
+
const hasFunctionCalls = (_f = (_e = llmResponse.content) == null ? void 0 : _e.parts) == null ? void 0 : _f.some(
|
|
138
|
+
(part) => part.functionCall
|
|
139
|
+
);
|
|
140
|
+
if (this.isGemini3Preview && ((_g = llmResponse.content) == null ? void 0 : _g.parts)) {
|
|
141
|
+
for (const part of llmResponse.content.parts) {
|
|
142
|
+
if (part.thoughtSignature && !thoughtSignature) {
|
|
143
|
+
thoughtSignature = part.thoughtSignature;
|
|
144
|
+
logger.debug(
|
|
145
|
+
`[Gemini3] Captured thoughtSignature (length: ${thoughtSignature.length})`
|
|
146
|
+
);
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (firstPart == null ? void 0 : firstPart.text) {
|
|
152
|
+
if ("thought" in firstPart && firstPart.thought) {
|
|
153
|
+
thoughtText += firstPart.text;
|
|
154
|
+
if ("thoughtSignature" in firstPart && firstPart.thoughtSignature) {
|
|
155
|
+
thoughtSignature = firstPart.thoughtSignature;
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
text += firstPart.text;
|
|
159
|
+
}
|
|
160
|
+
llmResponse.partial = true;
|
|
161
|
+
if (this.isGemini3Preview && hasFunctionCalls) {
|
|
162
|
+
thoughtText = "";
|
|
163
|
+
thoughtSignature = void 0;
|
|
164
|
+
text = "";
|
|
165
|
+
}
|
|
166
|
+
} else if ((thoughtText || text) && (!firstPart || !firstPart.inlineData)) {
|
|
167
|
+
if (this.isGemini3Preview && hasFunctionCalls && llmResponse.content) {
|
|
168
|
+
const prependParts = [];
|
|
169
|
+
if (thoughtText) {
|
|
170
|
+
const thoughtPart = { text: thoughtText, thought: true };
|
|
171
|
+
if (thoughtSignature) {
|
|
172
|
+
thoughtPart.thoughtSignature = thoughtSignature;
|
|
173
|
+
}
|
|
174
|
+
prependParts.push(thoughtPart);
|
|
175
|
+
}
|
|
176
|
+
if (text) {
|
|
177
|
+
prependParts.push(createPartFromText(text));
|
|
178
|
+
}
|
|
179
|
+
if (!thoughtText && thoughtSignature) {
|
|
180
|
+
for (const part of llmResponse.content.parts || []) {
|
|
181
|
+
if (part.functionCall && !part.thoughtSignature) {
|
|
182
|
+
part.thoughtSignature = thoughtSignature;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
llmResponse.content.parts = [
|
|
188
|
+
...prependParts,
|
|
189
|
+
...llmResponse.content.parts || []
|
|
190
|
+
];
|
|
191
|
+
thoughtText = "";
|
|
192
|
+
thoughtSignature = void 0;
|
|
193
|
+
text = "";
|
|
194
|
+
} else {
|
|
195
|
+
const parts = [];
|
|
196
|
+
if (thoughtText) {
|
|
197
|
+
const thoughtPart = { text: thoughtText, thought: true };
|
|
198
|
+
if (thoughtSignature) {
|
|
199
|
+
thoughtPart.thoughtSignature = thoughtSignature;
|
|
200
|
+
}
|
|
201
|
+
parts.push(thoughtPart);
|
|
202
|
+
}
|
|
203
|
+
if (text) {
|
|
204
|
+
parts.push(createPartFromText(text));
|
|
205
|
+
}
|
|
206
|
+
yield {
|
|
207
|
+
content: {
|
|
208
|
+
role: "model",
|
|
209
|
+
parts
|
|
210
|
+
},
|
|
211
|
+
usageMetadata: llmResponse.usageMetadata
|
|
212
|
+
};
|
|
213
|
+
thoughtText = "";
|
|
214
|
+
thoughtSignature = void 0;
|
|
215
|
+
text = "";
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (this.isGemini3Preview && hasFunctionCalls && ((_h = llmResponse.content) == null ? void 0 : _h.parts)) {
|
|
219
|
+
const hasExistingSignature = llmResponse.content.parts.some(
|
|
220
|
+
(p) => p.thoughtSignature
|
|
221
|
+
);
|
|
222
|
+
if (!hasExistingSignature && thoughtSignature) {
|
|
223
|
+
for (const part of llmResponse.content.parts) {
|
|
224
|
+
if (part.functionCall) {
|
|
225
|
+
part.thoughtSignature = thoughtSignature;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const partsWithSig = llmResponse.content.parts.filter(
|
|
231
|
+
(p) => p.thoughtSignature
|
|
232
|
+
).length;
|
|
233
|
+
if (partsWithSig === 0) {
|
|
234
|
+
logger.warn(
|
|
235
|
+
`[Gemini3] No thoughtSignature on function call parts \u2014 may cause 400 on next request`
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
yield llmResponse;
|
|
240
|
+
}
|
|
241
|
+
if ((text || thoughtText) && ((_j = (_i = lastResponse == null ? void 0 : lastResponse.candidates) == null ? void 0 : _i[0]) == null ? void 0 : _j.finishReason) === FinishReason.STOP) {
|
|
242
|
+
const parts = [];
|
|
243
|
+
if (thoughtText) {
|
|
244
|
+
const thoughtPart = { text: thoughtText, thought: true };
|
|
245
|
+
if (thoughtSignature) {
|
|
246
|
+
thoughtPart.thoughtSignature = thoughtSignature;
|
|
247
|
+
}
|
|
248
|
+
parts.push(thoughtPart);
|
|
249
|
+
}
|
|
250
|
+
if (text) {
|
|
251
|
+
parts.push({ text });
|
|
252
|
+
}
|
|
253
|
+
yield {
|
|
254
|
+
content: {
|
|
255
|
+
role: "model",
|
|
256
|
+
parts
|
|
257
|
+
},
|
|
258
|
+
usageMetadata
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
} else {
|
|
262
|
+
const response = await this.apiClient.models.generateContent({
|
|
263
|
+
model: (_k = llmRequest.model) != null ? _k : this.model,
|
|
264
|
+
contents: llmRequest.contents,
|
|
265
|
+
config: llmRequest.config
|
|
266
|
+
});
|
|
267
|
+
const llmResponse = createLlmResponse(response);
|
|
268
|
+
if (this.isGemini3Preview && ((_l = llmResponse.content) == null ? void 0 : _l.parts)) {
|
|
269
|
+
let thoughtSig;
|
|
270
|
+
let hasThoughtPartWithSignature = false;
|
|
271
|
+
for (const part of llmResponse.content.parts) {
|
|
272
|
+
if (part.thoughtSignature) {
|
|
273
|
+
thoughtSig = part.thoughtSignature;
|
|
274
|
+
if (part.thought) {
|
|
275
|
+
hasThoughtPartWithSignature = true;
|
|
276
|
+
}
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (thoughtSig && !hasThoughtPartWithSignature) {
|
|
281
|
+
for (const part of llmResponse.content.parts) {
|
|
282
|
+
if (part.functionCall && !part.thoughtSignature) {
|
|
283
|
+
part.thoughtSignature = thoughtSig;
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const hasFunctionCalls = llmResponse.content.parts.some(
|
|
289
|
+
(p) => p.functionCall
|
|
290
|
+
);
|
|
291
|
+
if (hasFunctionCalls) {
|
|
292
|
+
const partsWithSig = llmResponse.content.parts.filter(
|
|
293
|
+
(p) => p.thoughtSignature
|
|
294
|
+
).length;
|
|
295
|
+
if (partsWithSig === 0) {
|
|
296
|
+
logger.warn(
|
|
297
|
+
`[Gemini3] No thoughtSignature on function call parts \u2014 may cause 400 on next request`
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
yield llmResponse;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
get apiClient() {
|
|
306
|
+
if (this._apiClient) {
|
|
307
|
+
return this._apiClient;
|
|
308
|
+
}
|
|
309
|
+
const combinedHeaders = {
|
|
310
|
+
...this.trackingHeaders,
|
|
311
|
+
...this.headers
|
|
312
|
+
};
|
|
313
|
+
if (this.vertexai) {
|
|
314
|
+
this._apiClient = new GoogleGenAI({
|
|
315
|
+
vertexai: this.vertexai,
|
|
316
|
+
project: this.project,
|
|
317
|
+
location: this.location,
|
|
318
|
+
httpOptions: { headers: combinedHeaders }
|
|
319
|
+
});
|
|
320
|
+
} else {
|
|
321
|
+
const httpOptions = { headers: combinedHeaders };
|
|
322
|
+
if (this.apiEndpoint) {
|
|
323
|
+
httpOptions.baseUrl = this.apiEndpoint;
|
|
324
|
+
logger.debug(`Using custom API endpoint: ${this.apiEndpoint}`);
|
|
325
|
+
if (this.isGemini3Preview) {
|
|
326
|
+
httpOptions.apiVersion = "";
|
|
327
|
+
logger.info(
|
|
328
|
+
`Gemini 3 preview mode: using direct API path without version prefix`
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
this._apiClient = new GoogleGenAI({
|
|
333
|
+
apiKey: this.apiKey,
|
|
334
|
+
vertexai: false,
|
|
335
|
+
httpOptions
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
return this._apiClient;
|
|
339
|
+
}
|
|
340
|
+
get apiBackend() {
|
|
341
|
+
if (!this._apiBackend) {
|
|
342
|
+
this._apiBackend = this.apiClient.vertexai ? GoogleLLMVariant.VERTEX_AI : GoogleLLMVariant.GEMINI_API;
|
|
343
|
+
}
|
|
344
|
+
return this._apiBackend;
|
|
345
|
+
}
|
|
346
|
+
get liveApiVersion() {
|
|
347
|
+
if (!this._liveApiVersion) {
|
|
348
|
+
this._liveApiVersion = this.apiBackend === GoogleLLMVariant.VERTEX_AI ? "v1beta1" : "v1alpha";
|
|
349
|
+
}
|
|
350
|
+
return this._liveApiVersion;
|
|
351
|
+
}
|
|
352
|
+
get liveApiClient() {
|
|
353
|
+
if (!this._liveApiClient) {
|
|
354
|
+
const httpOptions = {
|
|
355
|
+
headers: this.trackingHeaders,
|
|
356
|
+
apiVersion: this.liveApiVersion
|
|
357
|
+
};
|
|
358
|
+
if (this.apiEndpoint) {
|
|
359
|
+
httpOptions.baseUrl = this.apiEndpoint;
|
|
360
|
+
if (this.isGemini3Preview) {
|
|
361
|
+
httpOptions.apiVersion = "";
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
this._liveApiClient = new GoogleGenAI({
|
|
365
|
+
apiKey: this.apiKey,
|
|
366
|
+
httpOptions
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
return this._liveApiClient;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Connects to the Gemini model and returns an llm connection.
|
|
373
|
+
*
|
|
374
|
+
* @param llmRequest LlmRequest, the request to send to the Gemini model.
|
|
375
|
+
* @returns BaseLlmConnection, the connection to the Gemini model.
|
|
376
|
+
*/
|
|
377
|
+
async connect(llmRequest) {
|
|
378
|
+
var _a, _b, _c, _d;
|
|
379
|
+
if ((_a = llmRequest.liveConnectConfig) == null ? void 0 : _a.httpOptions) {
|
|
380
|
+
if (!llmRequest.liveConnectConfig.httpOptions.headers) {
|
|
381
|
+
llmRequest.liveConnectConfig.httpOptions.headers = {};
|
|
382
|
+
}
|
|
383
|
+
Object.assign(
|
|
384
|
+
llmRequest.liveConnectConfig.httpOptions.headers,
|
|
385
|
+
this.trackingHeaders
|
|
386
|
+
);
|
|
387
|
+
llmRequest.liveConnectConfig.httpOptions.apiVersion = this.isGemini3Preview ? "" : this.liveApiVersion;
|
|
388
|
+
}
|
|
389
|
+
if ((_b = llmRequest.config) == null ? void 0 : _b.systemInstruction) {
|
|
390
|
+
llmRequest.liveConnectConfig.systemInstruction = {
|
|
391
|
+
role: "system",
|
|
392
|
+
// TODO - b/425992518: validate type casting works well.
|
|
393
|
+
parts: [
|
|
394
|
+
createPartFromText(llmRequest.config.systemInstruction)
|
|
395
|
+
]
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
llmRequest.liveConnectConfig.tools = (_c = llmRequest.config) == null ? void 0 : _c.tools;
|
|
399
|
+
const liveSession = await this.liveApiClient.live.connect({
|
|
400
|
+
model: (_d = llmRequest.model) != null ? _d : this.model,
|
|
401
|
+
config: llmRequest.liveConnectConfig,
|
|
402
|
+
callbacks: {
|
|
403
|
+
// TODO - b/425992518: GenAI SDK inconsistent API, missing methods.
|
|
404
|
+
onmessage: () => {
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
return new GeminiLlmConnection(liveSession);
|
|
409
|
+
}
|
|
410
|
+
preprocessRequest(llmRequest) {
|
|
411
|
+
if (this.apiBackend === GoogleLLMVariant.GEMINI_API) {
|
|
412
|
+
if (llmRequest.config) {
|
|
413
|
+
llmRequest.config.labels = void 0;
|
|
414
|
+
}
|
|
415
|
+
if (llmRequest.contents) {
|
|
416
|
+
for (const content of llmRequest.contents) {
|
|
417
|
+
if (!content.parts) continue;
|
|
418
|
+
for (const part of content.parts) {
|
|
419
|
+
removeDisplayNameIfPresent(part.inlineData);
|
|
420
|
+
removeDisplayNameIfPresent(part.fileData);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* A list of model name patterns that are supported by this LLM.
|
|
429
|
+
*
|
|
430
|
+
* @returns A list of supported models.
|
|
431
|
+
*/
|
|
432
|
+
Gemini.supportedModels = [
|
|
433
|
+
/gemini-.*/,
|
|
434
|
+
// fine-tuned vertex endpoint pattern
|
|
435
|
+
/projects\/.+\/locations\/.+\/endpoints\/.+/,
|
|
436
|
+
// vertex gemini long name
|
|
437
|
+
/projects\/.+\/locations\/.+\/publishers\/google\/models\/gemini.+/
|
|
438
|
+
];
|
|
439
|
+
function removeDisplayNameIfPresent(dataObj) {
|
|
440
|
+
if (dataObj && dataObj.displayName) {
|
|
441
|
+
dataObj.displayName = void 0;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
export {
|
|
445
|
+
Gemini
|
|
446
|
+
};
|