@google/adk 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/agents/active_streaming_tool.js +6 -1
- package/dist/cjs/agents/context.js +1 -11
- package/dist/cjs/agents/functions.js +35 -16
- package/dist/cjs/agents/instructions.js +23 -5
- package/dist/cjs/agents/invocation_context.js +2 -5
- package/dist/cjs/agents/llm_agent.js +8 -6
- package/dist/cjs/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/cjs/agents/processors/content_processor_utils.js +16 -3
- package/dist/cjs/agents/processors/context_compactor_request_processor.js +4 -2
- package/dist/cjs/agents/processors/interactions_request_processor.js +63 -0
- package/dist/cjs/apps/app.js +79 -0
- package/dist/cjs/apps/resumability_config.js +44 -0
- package/dist/cjs/artifacts/file_artifact_service.js +14 -1
- package/dist/cjs/artifacts/gcs_artifact_service.js +57 -15
- package/dist/cjs/artifacts/in_memory_artifact_service.js +5 -1
- package/dist/cjs/artifacts/scoped_artifact_service.js +101 -0
- package/dist/cjs/artifacts/session_artifact_service.js +27 -0
- package/dist/cjs/auth/oauth2/oauth2_discovery.js +17 -5
- package/dist/cjs/auth/oauth2/oauth2_utils.js +12 -1
- package/dist/cjs/common.js +47 -0
- package/dist/cjs/context/agent_controlled_context_compactor.js +109 -0
- package/dist/cjs/context/token_based_context_compactor.js +42 -10
- package/dist/cjs/context/trajectory_thought_pruning_compactor.js +71 -0
- package/dist/cjs/events/event.js +24 -0
- package/dist/cjs/models/google_llm.js +25 -10
- package/dist/cjs/models/interactions_utils.js +732 -0
- package/dist/cjs/plugins/base_plugin.js +1 -0
- package/dist/cjs/runner/in_memory_runner.js +13 -2
- package/dist/cjs/runner/runner.js +93 -72
- package/dist/cjs/sessions/base_session_service.js +6 -1
- package/dist/cjs/sessions/database_session_service.js +24 -7
- package/dist/cjs/sessions/vertex_ai_session_service.js +19 -4
- package/dist/cjs/skills/loader.js +2 -2
- package/dist/cjs/tools/base_toolset.js +1 -1
- package/dist/cjs/tools/consolidate_context_tool.js +79 -0
- package/dist/cjs/tools/forwarding_artifact_service.js +16 -28
- package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +2 -12
- package/dist/cjs/utils/async_hooks_shim.js +52 -0
- package/dist/cjs/utils/client_labels.js +41 -3
- package/dist/cjs/utils/env_aware_utils.js +10 -1
- package/dist/cjs/utils/streaming_utils.js +10 -4
- package/dist/cjs/utils/task.js +62 -0
- package/dist/cjs/version.js +1 -1
- package/dist/esm/agents/active_streaming_tool.js +6 -1
- package/dist/esm/agents/context.js +1 -11
- package/dist/esm/agents/functions.js +36 -15
- package/dist/esm/agents/instructions.js +23 -5
- package/dist/esm/agents/invocation_context.js +2 -5
- package/dist/esm/agents/llm_agent.js +8 -6
- package/dist/esm/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/esm/agents/processors/content_processor_utils.js +14 -2
- package/dist/esm/agents/processors/context_compactor_request_processor.js +4 -2
- package/dist/esm/agents/processors/interactions_request_processor.js +34 -0
- package/dist/esm/apps/app.js +49 -0
- package/dist/esm/apps/resumability_config.js +16 -0
- package/dist/esm/artifacts/file_artifact_service.js +14 -1
- package/dist/esm/artifacts/gcs_artifact_service.js +57 -15
- package/dist/esm/artifacts/in_memory_artifact_service.js +5 -1
- package/dist/esm/artifacts/scoped_artifact_service.js +73 -0
- package/dist/esm/artifacts/session_artifact_service.js +7 -0
- package/dist/esm/auth/oauth2/oauth2_discovery.js +14 -4
- package/dist/esm/auth/oauth2/oauth2_utils.js +12 -1
- package/dist/esm/common.js +42 -2
- package/dist/esm/context/agent_controlled_context_compactor.js +81 -0
- package/dist/esm/context/token_based_context_compactor.js +42 -10
- package/dist/esm/context/trajectory_thought_pruning_compactor.js +43 -0
- package/dist/esm/events/event.js +22 -0
- package/dist/esm/models/google_llm.js +23 -9
- package/dist/esm/models/interactions_utils.js +697 -0
- package/dist/esm/plugins/base_plugin.js +1 -0
- package/dist/esm/runner/in_memory_runner.js +13 -2
- package/dist/esm/runner/runner.js +91 -73
- package/dist/esm/sessions/base_session_service.js +6 -1
- package/dist/esm/sessions/database_session_service.js +24 -7
- package/dist/esm/sessions/vertex_ai_session_service.js +17 -3
- package/dist/esm/skills/loader.js +2 -2
- package/dist/esm/tools/base_toolset.js +1 -1
- package/dist/esm/tools/consolidate_context_tool.js +51 -0
- package/dist/esm/tools/forwarding_artifact_service.js +16 -28
- package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +2 -12
- package/dist/esm/utils/async_hooks_shim.js +24 -0
- package/dist/esm/utils/client_labels.js +38 -2
- package/dist/esm/utils/env_aware_utils.js +10 -1
- package/dist/esm/utils/streaming_utils.js +10 -4
- package/dist/esm/utils/task.js +34 -0
- package/dist/esm/version.js +1 -1
- package/dist/types/agents/active_streaming_tool.d.ts +3 -4
- package/dist/types/agents/functions.d.ts +12 -9
- package/dist/types/agents/invocation_context.d.ts +7 -3
- package/dist/types/agents/processors/content_processor_utils.d.ts +8 -0
- package/dist/types/agents/processors/interactions_request_processor.d.ts +17 -0
- package/dist/types/apps/app.d.ts +52 -0
- package/dist/types/apps/resumability_config.d.ts +27 -0
- package/dist/types/artifacts/base_artifact_service.d.ts +6 -36
- package/dist/types/artifacts/gcs_artifact_service.d.ts +2 -1
- package/dist/types/artifacts/scoped_artifact_service.d.ts +25 -0
- package/dist/types/artifacts/session_artifact_service.d.ts +25 -0
- package/dist/types/auth/oauth2/oauth2_discovery.d.ts +11 -0
- package/dist/types/common.d.ts +20 -3
- package/dist/types/context/agent_controlled_context_compactor.d.ts +24 -0
- package/dist/types/context/base_context_compactor.d.ts +3 -0
- package/dist/types/context/token_based_context_compactor.d.ts +6 -1
- package/dist/types/context/trajectory_thought_pruning_compactor.d.ts +27 -0
- package/dist/types/events/event.d.ts +8 -0
- package/dist/types/models/google_llm.d.ts +15 -1
- package/dist/types/models/interactions_utils.d.ts +92 -0
- package/dist/types/models/llm_request.d.ts +4 -0
- package/dist/types/models/llm_response.d.ts +4 -0
- package/dist/types/plugins/base_plugin.d.ts +2 -1
- package/dist/types/runner/in_memory_runner.d.ts +7 -1
- package/dist/types/runner/runner.d.ts +43 -9
- package/dist/types/sessions/base_session_service.d.ts +3 -16
- package/dist/types/sessions/session.d.ts +11 -0
- package/dist/types/sessions/vertex_ai_session_service.d.ts +9 -1
- package/dist/types/tools/consolidate_context_tool.d.ts +19 -0
- package/dist/types/tools/forwarding_artifact_service.d.ts +3 -2
- package/dist/types/utils/async_hooks_shim.d.ts +10 -0
- package/dist/types/utils/client_labels.d.ts +10 -0
- package/dist/types/utils/env_aware_utils.d.ts +3 -3
- package/dist/types/utils/task.d.ts +24 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/agents/active_streaming_tool.js +6 -1
- package/dist/web/agents/context.js +1 -11
- package/dist/web/agents/functions.js +36 -15
- package/dist/web/agents/instructions.js +23 -5
- package/dist/web/agents/invocation_context.js +2 -5
- package/dist/web/agents/llm_agent.js +8 -6
- package/dist/web/agents/processors/code_execution_request_processor.js +0 -3
- package/dist/web/agents/processors/content_processor_utils.js +14 -2
- package/dist/web/agents/processors/context_compactor_request_processor.js +4 -2
- package/dist/web/agents/processors/interactions_request_processor.js +52 -0
- package/dist/web/apps/app.js +49 -0
- package/dist/web/apps/resumability_config.js +31 -0
- package/dist/web/artifacts/file_artifact_service.js +14 -1
- package/dist/web/artifacts/gcs_artifact_service.js +55 -15
- package/dist/web/artifacts/in_memory_artifact_service.js +5 -1
- package/dist/web/artifacts/scoped_artifact_service.js +86 -0
- package/dist/web/artifacts/session_artifact_service.js +7 -0
- package/dist/web/auth/oauth2/oauth2_discovery.js +14 -4
- package/dist/web/auth/oauth2/oauth2_utils.js +12 -1
- package/dist/web/common.js +42 -2
- package/dist/web/context/agent_controlled_context_compactor.js +81 -0
- package/dist/web/context/token_based_context_compactor.js +42 -10
- package/dist/web/context/trajectory_thought_pruning_compactor.js +43 -0
- package/dist/web/events/event.js +20 -0
- package/dist/web/models/google_llm.js +54 -9
- package/dist/web/models/interactions_utils.js +728 -0
- package/dist/web/plugins/base_plugin.js +1 -0
- package/dist/web/runner/in_memory_runner.js +13 -2
- package/dist/web/runner/runner.js +111 -75
- package/dist/web/sessions/base_session_service.js +6 -1
- package/dist/web/sessions/database_session_service.js +24 -7
- package/dist/web/sessions/vertex_ai_session_service.js +17 -3
- package/dist/web/skills/loader.js +2 -2
- package/dist/web/tools/base_toolset.js +1 -1
- package/dist/web/tools/consolidate_context_tool.js +51 -0
- package/dist/web/tools/forwarding_artifact_service.js +16 -28
- package/dist/web/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +2 -12
- package/dist/web/utils/async_hooks_shim.js +24 -0
- package/dist/web/utils/client_labels.js +38 -2
- package/dist/web/utils/env_aware_utils.js +10 -1
- package/dist/web/utils/streaming_utils.js +10 -4
- package/dist/web/utils/task.js +34 -0
- package/dist/web/version.js +1 -1
- package/package.json +2 -2
|
@@ -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;
|
|
@@ -109,13 +113,41 @@ class TokenBasedContextCompactor {
|
|
|
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
|
|
@@ -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
|
|
@@ -25,7 +25,8 @@ 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");
|
|
@@ -36,13 +37,19 @@ var import_async_queue = require("../utils/async_queue.js");
|
|
|
36
37
|
var import_streaming_utils = require("../utils/streaming_utils.js");
|
|
37
38
|
var import_base_llm = require("./base_llm.js");
|
|
38
39
|
var import_gemini_llm_connection = require("./gemini_llm_connection.js");
|
|
40
|
+
var import_interactions_utils = require("./interactions_utils.js");
|
|
39
41
|
var import_llm_response = require("./llm_response.js");
|
|
40
42
|
/**
|
|
41
43
|
* @license
|
|
42
44
|
* Copyright 2025 Google LLC
|
|
43
45
|
* SPDX-License-Identifier: Apache-2.0
|
|
44
46
|
*/
|
|
45
|
-
|
|
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) {
|
|
46
53
|
/**
|
|
47
54
|
* @param params The parameters for creating a Gemini instance.
|
|
48
55
|
*/
|
|
@@ -52,12 +59,14 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
52
59
|
vertexai,
|
|
53
60
|
project,
|
|
54
61
|
location,
|
|
55
|
-
headers
|
|
62
|
+
headers,
|
|
63
|
+
useInteractionsApi
|
|
56
64
|
}) {
|
|
57
65
|
if (!model) {
|
|
58
66
|
model = "gemini-2.5-flash";
|
|
59
67
|
}
|
|
60
68
|
super({ model });
|
|
69
|
+
this[_a] = true;
|
|
61
70
|
const params = geminiInitParams({
|
|
62
71
|
model,
|
|
63
72
|
vertexai,
|
|
@@ -75,6 +84,7 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
75
84
|
this.apiKey = params.apiKey;
|
|
76
85
|
this.headers = headers;
|
|
77
86
|
this.vertexai = !!params.vertexai;
|
|
87
|
+
this.useInteractionsApi = !!useInteractionsApi;
|
|
78
88
|
}
|
|
79
89
|
/**
|
|
80
90
|
* Sends a request to the Gemini model.
|
|
@@ -84,11 +94,15 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
84
94
|
* @yields LlmResponse: The model response.
|
|
85
95
|
*/
|
|
86
96
|
async *generateContentAsync(llmRequest, stream = false, abortSignal) {
|
|
87
|
-
var
|
|
97
|
+
var _a2, _b2, _c;
|
|
98
|
+
if (this.useInteractionsApi) {
|
|
99
|
+
yield* (0, import_interactions_utils.generateContentViaInteractions)(this.apiClient, llmRequest, stream);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
88
102
|
this.preprocessRequest(llmRequest);
|
|
89
103
|
this.maybeAppendUserContent(llmRequest);
|
|
90
104
|
import_logger.logger.info(
|
|
91
|
-
`Sending out request, model: ${(
|
|
105
|
+
`Sending out request, model: ${(_a2 = llmRequest.model) != null ? _a2 : this.model}, backend: ${this.apiBackend}, stream: ${stream}`
|
|
92
106
|
);
|
|
93
107
|
if (!llmRequest.config) {
|
|
94
108
|
llmRequest.config = {};
|
|
@@ -104,7 +118,7 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
104
118
|
}
|
|
105
119
|
if (stream) {
|
|
106
120
|
const streamResult = await this.apiClient.models.generateContentStream({
|
|
107
|
-
model: (
|
|
121
|
+
model: (_b2 = llmRequest.model) != null ? _b2 : this.model,
|
|
108
122
|
contents: llmRequest.contents,
|
|
109
123
|
config: llmRequest.config
|
|
110
124
|
});
|
|
@@ -192,8 +206,8 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
192
206
|
* @returns BaseLlmConnection, the connection to the Gemini model.
|
|
193
207
|
*/
|
|
194
208
|
async connect(llmRequest) {
|
|
195
|
-
var
|
|
196
|
-
if ((
|
|
209
|
+
var _a2, _b2, _c, _d;
|
|
210
|
+
if ((_a2 = llmRequest.liveConnectConfig) == null ? void 0 : _a2.httpOptions) {
|
|
197
211
|
if (!llmRequest.liveConnectConfig.httpOptions.headers) {
|
|
198
212
|
llmRequest.liveConnectConfig.httpOptions.headers = {};
|
|
199
213
|
}
|
|
@@ -203,7 +217,7 @@ class Gemini extends import_base_llm.BaseLlm {
|
|
|
203
217
|
);
|
|
204
218
|
llmRequest.liveConnectConfig.httpOptions.apiVersion = this.liveApiVersion;
|
|
205
219
|
}
|
|
206
|
-
if ((
|
|
220
|
+
if ((_b2 = llmRequest.config) == null ? void 0 : _b2.systemInstruction) {
|
|
207
221
|
llmRequest.liveConnectConfig.systemInstruction = {
|
|
208
222
|
role: "system",
|
|
209
223
|
// TODO - b/425992518: validate type casting works well.
|
|
@@ -305,5 +319,6 @@ function geminiInitParams({
|
|
|
305
319
|
// Annotate the CommonJS export names for ESM import in node:
|
|
306
320
|
0 && (module.exports = {
|
|
307
321
|
Gemini,
|
|
308
|
-
geminiInitParams
|
|
322
|
+
geminiInitParams,
|
|
323
|
+
isGemini
|
|
309
324
|
});
|