@softactivate/adk 1.1.0 → 1.2.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/functions.js +4 -0
- 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/common.js +3 -0
- package/dist/cjs/events/event.js +2 -0
- 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 +103 -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/runner/in_memory_runner.js +10 -5
- package/dist/cjs/runner/runner.js +32 -2
- package/dist/cjs/sessions/database_session_service.js +49 -3
- package/dist/cjs/sessions/in_memory_session_service.js +70 -14
- package/dist/cjs/sessions/registry.js +4 -0
- package/dist/cjs/sessions/vertex_ai_session_service.js +428 -0
- package/dist/cjs/tools/agent_tool.js +17 -8
- package/dist/cjs/tools/exit_loop_tool.js +9 -0
- package/dist/cjs/tools/function_tool.js +7 -2
- 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 -7
- package/dist/cjs/tools/mcp/mcp_toolset.js +10 -2
- 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/skill/load_skill_resource_tool.js +2 -25
- package/dist/cjs/utils/file_utils.js +28 -0
- package/dist/cjs/utils/gemini_schema_util.js +18 -0
- package/dist/cjs/utils/partial_copy.js +51 -0
- package/dist/cjs/utils/streaming_utils.js +167 -118
- package/dist/cjs/utils/vertex_ai_utils.js +50 -0
- package/dist/cjs/version.js +1 -1
- package/dist/esm/agents/functions.js +4 -0
- 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/common.js +2 -0
- package/dist/esm/events/event.js +2 -0
- 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 +77 -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/runner/in_memory_runner.js +10 -5
- package/dist/esm/runner/runner.js +32 -2
- package/dist/esm/sessions/database_session_service.js +49 -3
- package/dist/esm/sessions/in_memory_session_service.js +70 -14
- package/dist/esm/sessions/registry.js +7 -0
- package/dist/esm/sessions/vertex_ai_session_service.js +403 -0
- package/dist/esm/tools/agent_tool.js +17 -8
- package/dist/esm/tools/exit_loop_tool.js +9 -0
- package/dist/esm/tools/function_tool.js +7 -2
- 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 -7
- package/dist/esm/tools/mcp/mcp_toolset.js +10 -2
- 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/skill/load_skill_resource_tool.js +1 -24
- package/dist/esm/utils/file_utils.js +26 -0
- package/dist/esm/utils/gemini_schema_util.js +18 -0
- package/dist/esm/utils/partial_copy.js +23 -0
- package/dist/esm/utils/streaming_utils.js +165 -117
- 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/agents/processors/base_llm_processor.d.ts +12 -4
- package/dist/types/agents/processors/basic_llm_request_processor.d.ts +5 -0
- package/dist/types/agents/processors/identity_llm_request_processor.d.ts +4 -0
- package/dist/types/agents/processors/request_confirmation_llm_request_processor.d.ts +5 -0
- 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 +2 -0
- package/dist/types/events/event_actions.d.ts +24 -6
- 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 +43 -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/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 +18 -0
- package/dist/types/sessions/database_session_service.d.ts +1 -1
- 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/tools/exit_loop_tool.d.ts +9 -0
- package/dist/types/tools/function_tool.d.ts +22 -2
- 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/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/utils/file_utils.d.ts +2 -0
- package/dist/types/utils/partial_copy.d.ts +13 -0
- package/dist/types/utils/streaming_utils.d.ts +8 -17
- package/dist/types/utils/vertex_ai_utils.d.ts +14 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/agents/functions.js +4 -0
- 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/common.js +2 -0
- package/dist/web/events/event.js +2 -0
- 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 +90 -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/runner/in_memory_runner.js +10 -5
- package/dist/web/runner/runner.js +32 -2
- package/dist/web/sessions/database_session_service.js +49 -3
- package/dist/web/sessions/in_memory_session_service.js +70 -14
- package/dist/web/sessions/registry.js +7 -0
- package/dist/web/sessions/vertex_ai_session_service.js +414 -0
- package/dist/web/tools/agent_tool.js +17 -8
- package/dist/web/tools/exit_loop_tool.js +9 -0
- package/dist/web/tools/function_tool.js +7 -2
- 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 -7
- package/dist/web/tools/mcp/mcp_toolset.js +10 -2
- 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/skill/load_skill_resource_tool.js +1 -24
- package/dist/web/utils/file_utils.js +26 -0
- package/dist/web/utils/gemini_schema_util.js +18 -0
- package/dist/web/utils/partial_copy.js +23 -0
- package/dist/web/utils/streaming_utils.js +218 -117
- package/dist/web/utils/vertex_ai_utils.js +22 -0
- package/dist/web/version.js +1 -1
- package/package.json +2 -1
|
@@ -193,7 +193,11 @@ class DatabaseSessionService extends BaseSessionService {
|
|
|
193
193
|
}
|
|
194
194
|
async listSessions({
|
|
195
195
|
appName,
|
|
196
|
-
userId
|
|
196
|
+
userId,
|
|
197
|
+
limit,
|
|
198
|
+
offset,
|
|
199
|
+
page,
|
|
200
|
+
order
|
|
197
201
|
}) {
|
|
198
202
|
await this.init();
|
|
199
203
|
const em = this.orm.em.fork();
|
|
@@ -201,7 +205,49 @@ class DatabaseSessionService extends BaseSessionService {
|
|
|
201
205
|
if (userId) {
|
|
202
206
|
where.userId = userId;
|
|
203
207
|
}
|
|
204
|
-
const
|
|
208
|
+
const orderBy = order === "asc" ? { updateTime: "ASC", id: "ASC" } : order === "desc" ? { updateTime: "DESC", id: "ASC" } : void 0;
|
|
209
|
+
let storageSessions;
|
|
210
|
+
let paginationMeta;
|
|
211
|
+
if (limit !== void 0) {
|
|
212
|
+
const totalItems = await em.count(StorageSession, where);
|
|
213
|
+
const totalPages = limit === 0 ? 0 : Math.ceil(totalItems / limit);
|
|
214
|
+
let effectiveOffset;
|
|
215
|
+
let effectivePage;
|
|
216
|
+
if (page !== void 0) {
|
|
217
|
+
effectiveOffset = (page - 1) * limit;
|
|
218
|
+
effectivePage = page;
|
|
219
|
+
} else {
|
|
220
|
+
effectiveOffset = offset != null ? offset : 0;
|
|
221
|
+
effectivePage = limit === 0 ? 1 : Math.floor(effectiveOffset / limit) + 1;
|
|
222
|
+
}
|
|
223
|
+
storageSessions = await em.find(StorageSession, where, {
|
|
224
|
+
orderBy,
|
|
225
|
+
limit,
|
|
226
|
+
offset: effectiveOffset
|
|
227
|
+
});
|
|
228
|
+
paginationMeta = { page: effectivePage, limit, totalItems, totalPages };
|
|
229
|
+
} else if (offset) {
|
|
230
|
+
const totalItems = await em.count(StorageSession, where);
|
|
231
|
+
storageSessions = await em.find(StorageSession, where, {
|
|
232
|
+
orderBy,
|
|
233
|
+
offset
|
|
234
|
+
});
|
|
235
|
+
paginationMeta = {
|
|
236
|
+
page: 1,
|
|
237
|
+
limit: totalItems,
|
|
238
|
+
totalItems,
|
|
239
|
+
totalPages: totalItems === 0 ? 0 : 1
|
|
240
|
+
};
|
|
241
|
+
} else {
|
|
242
|
+
storageSessions = await em.find(StorageSession, where, { orderBy });
|
|
243
|
+
const totalItems = storageSessions.length;
|
|
244
|
+
paginationMeta = {
|
|
245
|
+
page: 1,
|
|
246
|
+
limit: totalItems,
|
|
247
|
+
totalItems,
|
|
248
|
+
totalPages: totalItems === 0 ? 0 : 1
|
|
249
|
+
};
|
|
250
|
+
}
|
|
205
251
|
const appStateModel = await em.findOne(StorageAppState, { appName });
|
|
206
252
|
const appState = (appStateModel == null ? void 0 : appStateModel.state) || {};
|
|
207
253
|
const userStateMap = {};
|
|
@@ -226,7 +272,7 @@ class DatabaseSessionService extends BaseSessionService {
|
|
|
226
272
|
lastUpdateTime: ss.updateTime.getTime()
|
|
227
273
|
});
|
|
228
274
|
});
|
|
229
|
-
return { sessions };
|
|
275
|
+
return { sessions, ...paginationMeta };
|
|
230
276
|
}
|
|
231
277
|
async deleteSession({
|
|
232
278
|
appName,
|
|
@@ -104,25 +104,81 @@ class InMemorySessionService extends BaseSessionService {
|
|
|
104
104
|
}
|
|
105
105
|
listSessions({
|
|
106
106
|
appName,
|
|
107
|
-
userId
|
|
107
|
+
userId,
|
|
108
|
+
limit,
|
|
109
|
+
offset,
|
|
110
|
+
page,
|
|
111
|
+
order
|
|
108
112
|
}) {
|
|
109
113
|
if (!this.sessions[appName] || !this.sessions[appName][userId]) {
|
|
110
|
-
|
|
114
|
+
if (limit !== void 0) {
|
|
115
|
+
const effectiveOffset2 = page !== void 0 ? (page - 1) * limit : offset != null ? offset : 0;
|
|
116
|
+
const effectivePage2 = page !== void 0 ? page : limit === 0 ? 1 : Math.floor(effectiveOffset2 / limit) + 1;
|
|
117
|
+
return Promise.resolve({
|
|
118
|
+
sessions: [],
|
|
119
|
+
page: effectivePage2,
|
|
120
|
+
limit,
|
|
121
|
+
totalItems: 0,
|
|
122
|
+
totalPages: 0
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return Promise.resolve({
|
|
126
|
+
sessions: [],
|
|
127
|
+
page: 1,
|
|
128
|
+
limit: 0,
|
|
129
|
+
totalItems: 0,
|
|
130
|
+
totalPages: 0
|
|
131
|
+
});
|
|
111
132
|
}
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
133
|
+
const all = Object.values(this.sessions[appName][userId]).map(
|
|
134
|
+
(session) => createSession({
|
|
135
|
+
id: session.id,
|
|
136
|
+
appName: session.appName,
|
|
137
|
+
userId: session.userId,
|
|
138
|
+
state: {},
|
|
139
|
+
events: [],
|
|
140
|
+
lastUpdateTime: session.lastUpdateTime
|
|
141
|
+
})
|
|
142
|
+
);
|
|
143
|
+
if (order === "asc") {
|
|
144
|
+
all.sort(
|
|
145
|
+
(a, b) => a.lastUpdateTime - b.lastUpdateTime || a.id.localeCompare(b.id)
|
|
146
|
+
);
|
|
147
|
+
} else if (order === "desc") {
|
|
148
|
+
all.sort(
|
|
149
|
+
(a, b) => b.lastUpdateTime - a.lastUpdateTime || a.id.localeCompare(b.id)
|
|
123
150
|
);
|
|
124
151
|
}
|
|
125
|
-
|
|
152
|
+
if (limit === void 0) {
|
|
153
|
+
const totalItems2 = all.length;
|
|
154
|
+
const sliced = offset ? all.slice(offset) : all;
|
|
155
|
+
return Promise.resolve({
|
|
156
|
+
sessions: sliced,
|
|
157
|
+
page: 1,
|
|
158
|
+
limit: totalItems2,
|
|
159
|
+
totalItems: totalItems2,
|
|
160
|
+
totalPages: totalItems2 === 0 ? 0 : 1
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
const totalItems = all.length;
|
|
164
|
+
const totalPages = limit === 0 ? 0 : Math.ceil(totalItems / limit);
|
|
165
|
+
let effectiveOffset;
|
|
166
|
+
let effectivePage;
|
|
167
|
+
if (page !== void 0) {
|
|
168
|
+
effectiveOffset = (page - 1) * limit;
|
|
169
|
+
effectivePage = page;
|
|
170
|
+
} else {
|
|
171
|
+
effectiveOffset = offset != null ? offset : 0;
|
|
172
|
+
effectivePage = limit === 0 ? 1 : Math.floor(effectiveOffset / limit) + 1;
|
|
173
|
+
}
|
|
174
|
+
const paginated = all.slice(effectiveOffset, effectiveOffset + limit);
|
|
175
|
+
return Promise.resolve({
|
|
176
|
+
sessions: paginated,
|
|
177
|
+
page: effectivePage,
|
|
178
|
+
limit,
|
|
179
|
+
totalItems,
|
|
180
|
+
totalPages
|
|
181
|
+
});
|
|
126
182
|
}
|
|
127
183
|
async deleteSession({
|
|
128
184
|
appName,
|
|
@@ -13,6 +13,10 @@ import {
|
|
|
13
13
|
InMemorySessionService,
|
|
14
14
|
isInMemoryConnectionString
|
|
15
15
|
} from "./in_memory_session_service.js";
|
|
16
|
+
import {
|
|
17
|
+
VertexAiSessionService,
|
|
18
|
+
isVertexAiConnectionString
|
|
19
|
+
} from "./vertex_ai_session_service.js";
|
|
16
20
|
function getSessionServiceFromUri(uri) {
|
|
17
21
|
if (isInMemoryConnectionString(uri)) {
|
|
18
22
|
return new InMemorySessionService();
|
|
@@ -20,6 +24,9 @@ function getSessionServiceFromUri(uri) {
|
|
|
20
24
|
if (isDatabaseConnectionString(uri)) {
|
|
21
25
|
return new DatabaseSessionService(uri);
|
|
22
26
|
}
|
|
27
|
+
if (isVertexAiConnectionString(uri)) {
|
|
28
|
+
return new VertexAiSessionService({});
|
|
29
|
+
}
|
|
23
30
|
throw new Error(`Unsupported session service URI: ${uri}`);
|
|
24
31
|
}
|
|
25
32
|
export {
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import {createRequire as topLevelCreateRequire} from 'module';
|
|
2
|
+
const require = topLevelCreateRequire(import.meta.url);
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
6
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
7
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
8
|
+
if (decorator = decorators[i])
|
|
9
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
10
|
+
if (kind && result) __defProp(target, key, result);
|
|
11
|
+
return result;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* @license
|
|
15
|
+
* Copyright 2026 Google LLC
|
|
16
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
*/
|
|
18
|
+
import { Client } from "@google-cloud/vertexai/build/src/genai/client.js";
|
|
19
|
+
import { isCompactedEvent } from "../events/compacted_event.js";
|
|
20
|
+
import { experimental } from "../utils/experimental.js";
|
|
21
|
+
import { logger } from "../utils/logger.js";
|
|
22
|
+
import { getExpressModeApiKey } from "../utils/vertex_ai_utils.js";
|
|
23
|
+
import { partialCopy } from "../utils/partial_copy.js";
|
|
24
|
+
import {
|
|
25
|
+
BaseSessionService
|
|
26
|
+
} from "./base_session_service.js";
|
|
27
|
+
import { createSession } from "./session.js";
|
|
28
|
+
const DEFAULT_MAX_ATTEMPTS = 30;
|
|
29
|
+
const GRPC_NOT_FOUND = 5;
|
|
30
|
+
const HTTP_NOT_FOUND = 404;
|
|
31
|
+
function isVertexAiConnectionString(uri) {
|
|
32
|
+
return (uri == null ? void 0 : uri.startsWith("vertexai://")) || false;
|
|
33
|
+
}
|
|
34
|
+
let VertexAiSessionService = class extends BaseSessionService {
|
|
35
|
+
constructor(options) {
|
|
36
|
+
super();
|
|
37
|
+
this.agentEngineId = options.agentEngineId;
|
|
38
|
+
this.projectId = options.projectId;
|
|
39
|
+
this.location = options.location;
|
|
40
|
+
this.expressModeApiKey = getExpressModeApiKey(
|
|
41
|
+
this.projectId,
|
|
42
|
+
this.location,
|
|
43
|
+
options.expressModeApiKey
|
|
44
|
+
);
|
|
45
|
+
if (!options.sessions) {
|
|
46
|
+
if (!this.expressModeApiKey && (!this.projectId || !this.location)) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
"Either (Project ID and Location) or an expressModeApiKey is required."
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (options.sessions) {
|
|
53
|
+
this.sessions = options.sessions;
|
|
54
|
+
} else {
|
|
55
|
+
const client = new Client({
|
|
56
|
+
project: this.projectId,
|
|
57
|
+
location: this.location
|
|
58
|
+
});
|
|
59
|
+
this.sessions = client.agentEnginesInternal.sessions;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
getReasoningEngineId(appName) {
|
|
63
|
+
if (this.agentEngineId) {
|
|
64
|
+
return this.agentEngineId;
|
|
65
|
+
}
|
|
66
|
+
if (/^\d+$/.test(appName)) {
|
|
67
|
+
return appName;
|
|
68
|
+
}
|
|
69
|
+
const pattern = /^projects\/([a-zA-Z0-9-_]+)\/locations\/([a-zA-Z0-9-_]+)\/reasoningEngines\/(\d+)$/;
|
|
70
|
+
const match = appName.match(pattern);
|
|
71
|
+
if (!match) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`App name ${appName} is not valid. It should either be the full ReasoningEngine resource name, or the reasoning engine id.`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return match[3];
|
|
77
|
+
}
|
|
78
|
+
async createSession({
|
|
79
|
+
appName,
|
|
80
|
+
userId,
|
|
81
|
+
state,
|
|
82
|
+
sessionId
|
|
83
|
+
}) {
|
|
84
|
+
var _a;
|
|
85
|
+
const reasoningEngineId = this.getReasoningEngineId(appName);
|
|
86
|
+
let apiResponse = await this.sessions.createInternal({
|
|
87
|
+
name: `reasoningEngines/${reasoningEngineId}`,
|
|
88
|
+
userId,
|
|
89
|
+
config: {
|
|
90
|
+
...state ? { sessionState: state } : {},
|
|
91
|
+
...sessionId ? { sessionId } : {}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
const operationName = apiResponse.name;
|
|
95
|
+
let attempts = 0;
|
|
96
|
+
while (!apiResponse.done && attempts < DEFAULT_MAX_ATTEMPTS) {
|
|
97
|
+
const [nextResponse] = await Promise.all([
|
|
98
|
+
this.sessions.getSessionOperationInternal({
|
|
99
|
+
operationName
|
|
100
|
+
}),
|
|
101
|
+
new Promise((resolve) => setTimeout(resolve, 1e3))
|
|
102
|
+
]);
|
|
103
|
+
apiResponse = nextResponse;
|
|
104
|
+
attempts++;
|
|
105
|
+
}
|
|
106
|
+
if (!apiResponse.done) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`Session creation operation ${operationName} did not complete in time.`
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
const getSessionResponse = apiResponse.response;
|
|
112
|
+
const id = ((_a = getSessionResponse.name) == null ? void 0 : _a.split("/").pop()) || "";
|
|
113
|
+
return createSession({
|
|
114
|
+
id,
|
|
115
|
+
appName,
|
|
116
|
+
userId,
|
|
117
|
+
state: getSessionResponse.sessionState,
|
|
118
|
+
events: [],
|
|
119
|
+
lastUpdateTime: getSessionResponse.updateTime ? Date.parse(getSessionResponse.updateTime) : Date.now()
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async getSession({
|
|
123
|
+
appName,
|
|
124
|
+
userId,
|
|
125
|
+
sessionId,
|
|
126
|
+
config
|
|
127
|
+
}) {
|
|
128
|
+
const reasoningEngineId = this.getReasoningEngineId(appName);
|
|
129
|
+
const sessionResourceName = `reasoningEngines/${reasoningEngineId}/sessions/${sessionId}`;
|
|
130
|
+
try {
|
|
131
|
+
let getSessionResponse;
|
|
132
|
+
let eventsIterator = [];
|
|
133
|
+
if (config && config.numRecentEvents === 0) {
|
|
134
|
+
getSessionResponse = await this.sessions.get({
|
|
135
|
+
name: sessionResourceName
|
|
136
|
+
});
|
|
137
|
+
} else {
|
|
138
|
+
const listConfig = {};
|
|
139
|
+
if (config && config.afterTimestamp) {
|
|
140
|
+
listConfig.filter = `timestamp>="${new Date(
|
|
141
|
+
config.afterTimestamp
|
|
142
|
+
).toISOString()}"`;
|
|
143
|
+
}
|
|
144
|
+
const [sessionRes, eventsRes] = await Promise.all([
|
|
145
|
+
this.sessions.get({ name: sessionResourceName }),
|
|
146
|
+
this.sessions.events.listInternal({
|
|
147
|
+
name: sessionResourceName,
|
|
148
|
+
config: listConfig
|
|
149
|
+
})
|
|
150
|
+
]);
|
|
151
|
+
getSessionResponse = sessionRes;
|
|
152
|
+
eventsIterator = eventsRes.sessionEvents || [];
|
|
153
|
+
}
|
|
154
|
+
const sessionObj = getSessionResponse;
|
|
155
|
+
if (sessionObj.userId !== userId) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
`Session ${sessionId} does not belong to user ${userId}.`
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
const session = createSession({
|
|
161
|
+
id: sessionId,
|
|
162
|
+
appName,
|
|
163
|
+
userId,
|
|
164
|
+
state: sessionObj.sessionState,
|
|
165
|
+
events: [],
|
|
166
|
+
lastUpdateTime: sessionObj.updateTime ? Date.parse(sessionObj.updateTime) : Date.now()
|
|
167
|
+
});
|
|
168
|
+
for (const event of eventsIterator) {
|
|
169
|
+
session.events.push(_fromApiEvent(event));
|
|
170
|
+
}
|
|
171
|
+
if (config && config.numRecentEvents) {
|
|
172
|
+
session.events = session.events.slice(-config.numRecentEvents);
|
|
173
|
+
}
|
|
174
|
+
return session;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
const err = error;
|
|
177
|
+
if (err.code === GRPC_NOT_FOUND || err.code === HTTP_NOT_FOUND) {
|
|
178
|
+
return void 0;
|
|
179
|
+
}
|
|
180
|
+
logger.error(`Error getting session from Vertex AI: ${err.message}`);
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async listSessions({
|
|
185
|
+
appName,
|
|
186
|
+
userId,
|
|
187
|
+
limit,
|
|
188
|
+
offset,
|
|
189
|
+
page,
|
|
190
|
+
order
|
|
191
|
+
}) {
|
|
192
|
+
var _a;
|
|
193
|
+
const reasoningEngineId = this.getReasoningEngineId(appName);
|
|
194
|
+
const adkSessions = [];
|
|
195
|
+
let pageToken = void 0;
|
|
196
|
+
do {
|
|
197
|
+
const response = await this.sessions.listInternal({
|
|
198
|
+
name: `reasoningEngines/${reasoningEngineId}`,
|
|
199
|
+
config: {
|
|
200
|
+
...userId ? { filter: `user_id="${userId}"` } : {},
|
|
201
|
+
...pageToken ? { pageToken } : {}
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
const sessions = response.sessions || [];
|
|
205
|
+
for (const sessionObj of sessions) {
|
|
206
|
+
const id = ((_a = sessionObj.name) == null ? void 0 : _a.split("/").pop()) || "";
|
|
207
|
+
adkSessions.push(
|
|
208
|
+
createSession({
|
|
209
|
+
id,
|
|
210
|
+
appName,
|
|
211
|
+
userId: sessionObj.userId,
|
|
212
|
+
state: sessionObj.sessionState,
|
|
213
|
+
events: [],
|
|
214
|
+
lastUpdateTime: sessionObj.updateTime ? new Date(sessionObj.updateTime).getTime() : Date.now()
|
|
215
|
+
})
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
pageToken = response.nextPageToken;
|
|
219
|
+
} while (pageToken);
|
|
220
|
+
if (order === "asc") {
|
|
221
|
+
adkSessions.sort(
|
|
222
|
+
(a, b) => a.lastUpdateTime - b.lastUpdateTime || a.id.localeCompare(b.id)
|
|
223
|
+
);
|
|
224
|
+
} else if (order === "desc") {
|
|
225
|
+
adkSessions.sort(
|
|
226
|
+
(a, b) => b.lastUpdateTime - a.lastUpdateTime || a.id.localeCompare(b.id)
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
if (limit === void 0) {
|
|
230
|
+
const totalItems2 = adkSessions.length;
|
|
231
|
+
const sliced = offset ? adkSessions.slice(offset) : adkSessions;
|
|
232
|
+
return {
|
|
233
|
+
sessions: sliced,
|
|
234
|
+
page: 1,
|
|
235
|
+
limit: totalItems2,
|
|
236
|
+
totalItems: totalItems2,
|
|
237
|
+
totalPages: totalItems2 === 0 ? 0 : 1
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
const totalItems = adkSessions.length;
|
|
241
|
+
const totalPages = limit === 0 ? 0 : Math.ceil(totalItems / limit);
|
|
242
|
+
let effectiveOffset;
|
|
243
|
+
let effectivePage;
|
|
244
|
+
if (page !== void 0) {
|
|
245
|
+
effectiveOffset = (page - 1) * limit;
|
|
246
|
+
effectivePage = page;
|
|
247
|
+
} else {
|
|
248
|
+
effectiveOffset = offset != null ? offset : 0;
|
|
249
|
+
effectivePage = limit === 0 ? 1 : Math.floor(effectiveOffset / limit) + 1;
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
sessions: adkSessions.slice(effectiveOffset, effectiveOffset + limit),
|
|
253
|
+
page: effectivePage,
|
|
254
|
+
limit,
|
|
255
|
+
totalItems,
|
|
256
|
+
totalPages
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
async deleteSession({
|
|
260
|
+
appName,
|
|
261
|
+
userId: _userId,
|
|
262
|
+
sessionId
|
|
263
|
+
}) {
|
|
264
|
+
const reasoningEngineId = this.getReasoningEngineId(appName);
|
|
265
|
+
await this.sessions.delete({
|
|
266
|
+
name: `reasoningEngines/${reasoningEngineId}/sessions/${sessionId}`
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
async appendEvent({
|
|
270
|
+
session,
|
|
271
|
+
event
|
|
272
|
+
}) {
|
|
273
|
+
await super.appendEvent({ session, event });
|
|
274
|
+
session.lastUpdateTime = event.timestamp;
|
|
275
|
+
const reasoningEngineId = this.getReasoningEngineId(session.appName);
|
|
276
|
+
const customMetadata = { ...event.customMetadata };
|
|
277
|
+
if (isCompactedEvent(event)) {
|
|
278
|
+
customMetadata._compaction = {
|
|
279
|
+
startTime: event.startTime,
|
|
280
|
+
endTime: event.endTime,
|
|
281
|
+
compactedContent: event.compactedContent
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
if (event.usageMetadata) {
|
|
285
|
+
customMetadata._usage_metadata = event.usageMetadata;
|
|
286
|
+
}
|
|
287
|
+
const config = partialCopy(event, [
|
|
288
|
+
"content",
|
|
289
|
+
"actions",
|
|
290
|
+
"errorCode",
|
|
291
|
+
"errorMessage"
|
|
292
|
+
]);
|
|
293
|
+
config.eventMetadata = {
|
|
294
|
+
...partialCopy(event, [
|
|
295
|
+
"partial",
|
|
296
|
+
"turnComplete",
|
|
297
|
+
"interrupted",
|
|
298
|
+
"branch",
|
|
299
|
+
"longRunningToolIds",
|
|
300
|
+
"groundingMetadata"
|
|
301
|
+
]),
|
|
302
|
+
customMetadata: Object.keys(customMetadata).length > 0 ? customMetadata : void 0
|
|
303
|
+
};
|
|
304
|
+
config.rawEvent = JSON.parse(JSON.stringify(event));
|
|
305
|
+
const params = {
|
|
306
|
+
name: `reasoningEngines/${reasoningEngineId}/sessions/${session.id}`,
|
|
307
|
+
author: event.author || "user",
|
|
308
|
+
invocationId: event.invocationId || `inv-${Date.now()}`,
|
|
309
|
+
timestamp: new Date(event.timestamp).toISOString(),
|
|
310
|
+
config
|
|
311
|
+
};
|
|
312
|
+
try {
|
|
313
|
+
await this.sessions.events.append(params);
|
|
314
|
+
} catch (error) {
|
|
315
|
+
logger.warn(
|
|
316
|
+
"Failed to append event with rawEvent, falling back...",
|
|
317
|
+
error
|
|
318
|
+
);
|
|
319
|
+
delete config.rawEvent;
|
|
320
|
+
await this.sessions.events.append({
|
|
321
|
+
name: `reasoningEngines/${reasoningEngineId}/sessions/${session.id}`,
|
|
322
|
+
author: event.author || "user",
|
|
323
|
+
invocationId: event.invocationId || `inv-${Date.now()}`,
|
|
324
|
+
timestamp: new Date(event.timestamp).toISOString(),
|
|
325
|
+
config
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
return event;
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
VertexAiSessionService = __decorateClass([
|
|
332
|
+
experimental
|
|
333
|
+
], VertexAiSessionService);
|
|
334
|
+
function _fromApiEvent(apiEventObj) {
|
|
335
|
+
var _a, _b, _c;
|
|
336
|
+
const rawEvent = apiEventObj.rawEvent;
|
|
337
|
+
if (rawEvent) {
|
|
338
|
+
const event2 = JSON.parse(JSON.stringify(rawEvent));
|
|
339
|
+
event2.id = ((_a = apiEventObj.name) == null ? void 0 : _a.split("/").pop()) || "";
|
|
340
|
+
event2.invocationId = apiEventObj.invocationId || "";
|
|
341
|
+
event2.author = apiEventObj.author;
|
|
342
|
+
if (apiEventObj.timestamp) {
|
|
343
|
+
event2.timestamp = new Date(apiEventObj.timestamp).getTime();
|
|
344
|
+
}
|
|
345
|
+
return event2;
|
|
346
|
+
}
|
|
347
|
+
const actions = apiEventObj.actions || {};
|
|
348
|
+
const eventMetadata = apiEventObj.eventMetadata || {};
|
|
349
|
+
let customMetadata = eventMetadata.customMetadata;
|
|
350
|
+
let compactionData = null;
|
|
351
|
+
let usageMetadataData = null;
|
|
352
|
+
if (customMetadata) {
|
|
353
|
+
if (customMetadata._compaction) {
|
|
354
|
+
compactionData = customMetadata._compaction;
|
|
355
|
+
delete customMetadata._compaction;
|
|
356
|
+
}
|
|
357
|
+
if (customMetadata._usage_metadata) {
|
|
358
|
+
usageMetadataData = customMetadata._usage_metadata;
|
|
359
|
+
delete customMetadata._usage_metadata;
|
|
360
|
+
}
|
|
361
|
+
if (Object.keys(customMetadata).length === 0) {
|
|
362
|
+
customMetadata = void 0;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const eventActions = {
|
|
366
|
+
stateDelta: actions["stateDelta"] || {},
|
|
367
|
+
artifactDelta: actions["artifactDelta"] || {},
|
|
368
|
+
requestedAuthConfigs: actions.requestedAuthConfigs || {},
|
|
369
|
+
requestedToolConfirmations: actions["requestedToolConfirmations"] || {},
|
|
370
|
+
skipSummarization: actions["skipSummarization"],
|
|
371
|
+
transferToAgent: actions["transferAgent"],
|
|
372
|
+
escalate: actions["escalate"],
|
|
373
|
+
compaction: compactionData || void 0
|
|
374
|
+
};
|
|
375
|
+
const event = {
|
|
376
|
+
id: ((_b = apiEventObj.name) == null ? void 0 : _b.split("/").pop()) || "",
|
|
377
|
+
invocationId: apiEventObj.invocationId || "",
|
|
378
|
+
author: apiEventObj.author,
|
|
379
|
+
actions: eventActions,
|
|
380
|
+
content: apiEventObj.content,
|
|
381
|
+
timestamp: apiEventObj.timestamp ? new Date(apiEventObj.timestamp).getTime() : Date.now(),
|
|
382
|
+
errorCode: (_c = apiEventObj.errorCode) == null ? void 0 : _c.toString(),
|
|
383
|
+
errorMessage: apiEventObj.errorMessage,
|
|
384
|
+
partial: eventMetadata["partial"],
|
|
385
|
+
turnComplete: eventMetadata["turnComplete"],
|
|
386
|
+
interrupted: eventMetadata["interrupted"],
|
|
387
|
+
branch: eventMetadata["branch"],
|
|
388
|
+
customMetadata,
|
|
389
|
+
longRunningToolIds: eventMetadata["longRunningToolIds"],
|
|
390
|
+
usageMetadata: usageMetadataData
|
|
391
|
+
};
|
|
392
|
+
if (compactionData) {
|
|
393
|
+
event.isCompacted = true;
|
|
394
|
+
event.startTime = compactionData.startTime;
|
|
395
|
+
event.endTime = compactionData.endTime;
|
|
396
|
+
event.compactedContent = compactionData.compactedContent;
|
|
397
|
+
}
|
|
398
|
+
return event;
|
|
399
|
+
}
|
|
400
|
+
export {
|
|
401
|
+
VertexAiSessionService,
|
|
402
|
+
isVertexAiConnectionString
|
|
403
|
+
};
|
|
@@ -12,6 +12,7 @@ import { InMemoryMemoryService } from "../memory/in_memory_memory_service.js";
|
|
|
12
12
|
import { Runner } from "../runner/runner.js";
|
|
13
13
|
import { InMemorySessionService } from "../sessions/in_memory_session_service.js";
|
|
14
14
|
import { GoogleLLMVariant } from "../utils/variant_utils.js";
|
|
15
|
+
import { State } from "../sessions/state.js";
|
|
15
16
|
import { BaseTool } from "./base_tool.js";
|
|
16
17
|
import { ForwardingArtifactService } from "./forwarding_artifact_service.js";
|
|
17
18
|
const AGENT_TOOL_SIGNATURE_SYMBOL = Symbol.for("google.adk.agentTool");
|
|
@@ -122,9 +123,6 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
|
122
123
|
toolContext
|
|
123
124
|
}) {
|
|
124
125
|
var _a2, _b2;
|
|
125
|
-
if (this.skipSummarization) {
|
|
126
|
-
toolContext.actions.skipSummarization = true;
|
|
127
|
-
}
|
|
128
126
|
const { runner, content, sessionUserId, sessionId } = await this.setupRunnerAndSession({ args, toolContext });
|
|
129
127
|
if ((_a2 = toolContext.abortSignal) == null ? void 0 : _a2.aborted) {
|
|
130
128
|
return "";
|
|
@@ -141,7 +139,14 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
|
141
139
|
return;
|
|
142
140
|
}
|
|
143
141
|
if (event.actions.stateDelta) {
|
|
144
|
-
|
|
142
|
+
const filteredDelta = Object.fromEntries(
|
|
143
|
+
Object.entries(event.actions.stateDelta).filter(
|
|
144
|
+
([key]) => !key.startsWith(State.TEMP_PREFIX)
|
|
145
|
+
)
|
|
146
|
+
);
|
|
147
|
+
if (Object.keys(filteredDelta).length > 0) {
|
|
148
|
+
toolContext.state.update(filteredDelta);
|
|
149
|
+
}
|
|
145
150
|
}
|
|
146
151
|
lastEvent = event;
|
|
147
152
|
}
|
|
@@ -160,9 +165,6 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
|
160
165
|
toolContext
|
|
161
166
|
}) {
|
|
162
167
|
var _a2, _b2;
|
|
163
|
-
if (this.skipSummarization) {
|
|
164
|
-
toolContext.actions.skipSummarization = true;
|
|
165
|
-
}
|
|
166
168
|
const { runner, content, sessionUserId, sessionId } = await this.setupRunnerAndSession({ args, toolContext });
|
|
167
169
|
if ((_a2 = toolContext.abortSignal) == null ? void 0 : _a2.aborted) {
|
|
168
170
|
return;
|
|
@@ -178,7 +180,14 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
|
178
180
|
return;
|
|
179
181
|
}
|
|
180
182
|
if (event.actions.stateDelta) {
|
|
181
|
-
|
|
183
|
+
const filteredDelta = Object.fromEntries(
|
|
184
|
+
Object.entries(event.actions.stateDelta).filter(
|
|
185
|
+
([key]) => !key.startsWith(State.TEMP_PREFIX)
|
|
186
|
+
)
|
|
187
|
+
);
|
|
188
|
+
if (Object.keys(filteredDelta).length > 0) {
|
|
189
|
+
toolContext.state.update(filteredDelta);
|
|
190
|
+
}
|
|
182
191
|
}
|
|
183
192
|
yield event;
|
|
184
193
|
}
|
|
@@ -7,18 +7,27 @@ const require = topLevelCreateRequire(import.meta.url);
|
|
|
7
7
|
*/
|
|
8
8
|
import { BaseTool } from "./base_tool.js";
|
|
9
9
|
class ExitLoopTool extends BaseTool {
|
|
10
|
+
/** Creates an ExitLoopTool with its fixed name and description. */
|
|
10
11
|
constructor() {
|
|
11
12
|
super({
|
|
12
13
|
name: "exit_loop",
|
|
13
14
|
description: "Exits the loop.\n\nCall this function only when you are instructed to do so."
|
|
14
15
|
});
|
|
15
16
|
}
|
|
17
|
+
/** Returns the function declaration for this tool. */
|
|
16
18
|
_getDeclaration() {
|
|
17
19
|
return {
|
|
18
20
|
name: this.name,
|
|
19
21
|
description: this.description
|
|
20
22
|
};
|
|
21
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Sets `escalate` and `skipSummarization` on the event actions, signalling
|
|
26
|
+
* the {@link LoopAgent} to stop iterating.
|
|
27
|
+
*
|
|
28
|
+
* @param request The tool request containing the current tool context.
|
|
29
|
+
* @returns An empty string response.
|
|
30
|
+
*/
|
|
22
31
|
async runAsync({
|
|
23
32
|
toolContext
|
|
24
33
|
}) {
|