@google/adk 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/agents/base_agent.js +14 -2
- package/dist/cjs/agents/content_processor_utils.js +12 -2
- package/dist/cjs/agents/functions.js +16 -3
- package/dist/cjs/agents/llm_agent.js +341 -4
- package/dist/cjs/{utils/deep_clone.js → auth/exchanger/base_credential_exchanger.js} +6 -10
- package/dist/cjs/auth/exchanger/credential_exchanger_registry.js +59 -0
- package/dist/cjs/code_executors/built_in_code_executor.js +1 -1
- package/dist/cjs/code_executors/code_execution_utils.js +12 -2
- package/dist/cjs/code_executors/code_executor_context.js +12 -2
- package/dist/cjs/common.js +10 -0
- package/dist/cjs/index.js +58 -5
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/models/base_llm.js +16 -4
- package/dist/cjs/runner/runner.js +10 -1
- package/dist/cjs/sessions/in_memory_session_service.js +13 -3
- package/dist/cjs/sessions/state.js +1 -1
- package/dist/cjs/tools/agent_tool.js +2 -2
- package/dist/cjs/tools/mcp/mcp_session_manager.js +7 -1
- package/dist/cjs/utils/gemini_schema_util.js +16 -0
- package/dist/cjs/utils/model_name.js +24 -4
- package/dist/cjs/version.js +1 -1
- package/dist/esm/agents/base_agent.js +12 -1
- package/dist/esm/agents/content_processor_utils.js +2 -2
- package/dist/esm/agents/functions.js +6 -3
- package/dist/esm/agents/llm_agent.js +330 -4
- 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/built_in_code_executor.js +2 -2
- package/dist/esm/code_executors/code_execution_utils.js +2 -2
- package/dist/esm/code_executors/code_executor_context.js +2 -2
- package/dist/esm/common.js +8 -2
- package/dist/esm/index.js +58 -5
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/models/base_llm.js +14 -3
- package/dist/esm/runner/runner.js +10 -1
- package/dist/esm/sessions/in_memory_session_service.js +3 -3
- package/dist/esm/sessions/state.js +1 -1
- package/dist/esm/tools/agent_tool.js +2 -2
- package/dist/esm/tools/function_tool.js +3 -1
- package/dist/esm/tools/mcp/mcp_session_manager.js +7 -1
- package/dist/esm/utils/gemini_schema_util.js +16 -0
- package/dist/esm/utils/model_name.js +23 -3
- package/dist/esm/version.js +1 -1
- package/dist/types/agents/base_agent.d.ts +15 -0
- package/dist/types/agents/functions.d.ts +2 -0
- package/dist/types/agents/llm_agent.d.ts +23 -0
- package/dist/types/auth/exchanger/base_credential_exchanger.d.ts +32 -0
- package/dist/types/auth/exchanger/credential_exchanger_registry.d.ts +28 -0
- package/dist/types/code_executors/code_executor_context.d.ts +0 -5
- package/dist/types/common.d.ts +4 -2
- package/dist/types/models/base_llm.d.ts +16 -0
- package/dist/types/sessions/in_memory_session_service.d.ts +0 -5
- package/dist/types/sessions/state.d.ts +2 -2
- package/dist/types/tools/function_tool.d.ts +3 -3
- package/dist/types/tools/tool_confirmation.d.ts +1 -1
- package/dist/types/utils/gemini_schema_util.d.ts +2 -2
- package/dist/types/utils/model_name.d.ts +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/web/agents/base_agent.js +12 -1
- package/dist/web/agents/content_processor_utils.js +2 -2
- package/dist/web/agents/functions.js +6 -3
- package/dist/web/agents/llm_agent.js +315 -4
- 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/built_in_code_executor.js +2 -2
- package/dist/web/code_executors/code_execution_utils.js +2 -2
- package/dist/web/code_executors/code_executor_context.js +2 -2
- package/dist/web/common.js +8 -2
- package/dist/web/index.js +1 -1
- package/dist/web/index.js.map +4 -4
- package/dist/web/models/base_llm.js +14 -3
- package/dist/web/runner/runner.js +10 -1
- package/dist/web/sessions/in_memory_session_service.js +3 -3
- package/dist/web/sessions/state.js +1 -1
- package/dist/web/tools/agent_tool.js +2 -2
- package/dist/web/tools/function_tool.js +3 -1
- package/dist/web/tools/mcp/mcp_session_manager.js +7 -1
- package/dist/web/utils/gemini_schema_util.js +16 -0
- package/dist/web/utils/model_name.js +23 -3
- package/dist/web/version.js +1 -1
- package/package.json +3 -1
- package/dist/esm/utils/deep_clone.js +0 -14
- package/dist/types/utils/deep_clone.d.ts +0 -1
- package/dist/web/utils/deep_clone.js +0 -14
|
@@ -25,7 +25,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
25
25
|
var base_agent_exports = {};
|
|
26
26
|
__export(base_agent_exports, {
|
|
27
27
|
BaseAgent: () => BaseAgent,
|
|
28
|
-
getCannonicalCallback: () => getCannonicalCallback
|
|
28
|
+
getCannonicalCallback: () => getCannonicalCallback,
|
|
29
|
+
isBaseAgent: () => isBaseAgent
|
|
29
30
|
});
|
|
30
31
|
module.exports = __toCommonJS(base_agent_exports);
|
|
31
32
|
var import_api = require("@opentelemetry/api");
|
|
@@ -37,8 +38,18 @@ var import_invocation_context = require("./invocation_context.js");
|
|
|
37
38
|
* Copyright 2025 Google LLC
|
|
38
39
|
* SPDX-License-Identifier: Apache-2.0
|
|
39
40
|
*/
|
|
41
|
+
var _a;
|
|
42
|
+
const BASE_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.baseAgent");
|
|
43
|
+
function isBaseAgent(obj) {
|
|
44
|
+
return typeof obj === "object" && obj !== null && BASE_AGENT_SIGNATURE_SYMBOL in obj && obj[BASE_AGENT_SIGNATURE_SYMBOL] === true;
|
|
45
|
+
}
|
|
46
|
+
_a = BASE_AGENT_SIGNATURE_SYMBOL;
|
|
40
47
|
class BaseAgent {
|
|
41
48
|
constructor(config) {
|
|
49
|
+
/**
|
|
50
|
+
* A unique symbol to identify ADK agent classes.
|
|
51
|
+
*/
|
|
52
|
+
this[_a] = true;
|
|
42
53
|
this.name = validateAgentName(config.name);
|
|
43
54
|
this.description = config.description;
|
|
44
55
|
this.parentAgent = config.parentAgent;
|
|
@@ -241,5 +252,6 @@ function getCannonicalCallback(callbacks) {
|
|
|
241
252
|
// Annotate the CommonJS export names for ESM import in node:
|
|
242
253
|
0 && (module.exports = {
|
|
243
254
|
BaseAgent,
|
|
244
|
-
getCannonicalCallback
|
|
255
|
+
getCannonicalCallback,
|
|
256
|
+
isBaseAgent
|
|
245
257
|
});
|
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
"use strict";
|
|
8
|
+
var __create = Object.create;
|
|
8
9
|
var __defProp = Object.defineProperty;
|
|
9
10
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
11
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
12
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
11
13
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
14
|
var __export = (target, all) => {
|
|
13
15
|
for (var name in all)
|
|
@@ -21,6 +23,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
21
23
|
}
|
|
22
24
|
return to;
|
|
23
25
|
};
|
|
26
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
27
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
28
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
29
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
30
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
31
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
32
|
+
mod
|
|
33
|
+
));
|
|
24
34
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
35
|
var content_processor_utils_exports = {};
|
|
26
36
|
__export(content_processor_utils_exports, {
|
|
@@ -28,8 +38,8 @@ __export(content_processor_utils_exports, {
|
|
|
28
38
|
getCurrentTurnContents: () => getCurrentTurnContents
|
|
29
39
|
});
|
|
30
40
|
module.exports = __toCommonJS(content_processor_utils_exports);
|
|
41
|
+
var import_cloneDeep = __toESM(require("lodash-es/cloneDeep.js"), 1);
|
|
31
42
|
var import_event = require("../events/event.js");
|
|
32
|
-
var import_deep_clone = require("../utils/deep_clone.js");
|
|
33
43
|
var import_functions = require("./functions.js");
|
|
34
44
|
/**
|
|
35
45
|
* @license
|
|
@@ -60,7 +70,7 @@ function getContents(events, agentName, currentBranch) {
|
|
|
60
70
|
resultEvents = rearrangeEventsForAsyncFunctionResponsesInHistory(resultEvents);
|
|
61
71
|
const contents = [];
|
|
62
72
|
for (const event of resultEvents) {
|
|
63
|
-
const content = (0,
|
|
73
|
+
const content = (0, import_cloneDeep.default)(event.content);
|
|
64
74
|
(0, import_functions.removeClientFunctionCallId)(content);
|
|
65
75
|
contents.push(content);
|
|
66
76
|
}
|
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
"use strict";
|
|
8
|
+
var __create = Object.create;
|
|
8
9
|
var __defProp = Object.defineProperty;
|
|
9
10
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
11
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
12
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
11
13
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
14
|
var __export = (target, all) => {
|
|
13
15
|
for (var name in all)
|
|
@@ -21,6 +23,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
21
23
|
}
|
|
22
24
|
return to;
|
|
23
25
|
};
|
|
26
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
27
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
28
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
29
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
30
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
31
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
32
|
+
mod
|
|
33
|
+
));
|
|
24
34
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
35
|
var functions_exports = {};
|
|
26
36
|
__export(functions_exports, {
|
|
@@ -39,6 +49,7 @@ __export(functions_exports, {
|
|
|
39
49
|
});
|
|
40
50
|
module.exports = __toCommonJS(functions_exports);
|
|
41
51
|
var import_genai = require("@google/genai");
|
|
52
|
+
var import_isEmpty = __toESM(require("lodash-es/isEmpty.js"), 1);
|
|
42
53
|
var import_event = require("../events/event.js");
|
|
43
54
|
var import_event_actions = require("../events/event_actions.js");
|
|
44
55
|
var import_tool_context = require("../tools/tool_context.js");
|
|
@@ -53,7 +64,9 @@ const AF_FUNCTION_CALL_ID_PREFIX = "adk-";
|
|
|
53
64
|
const REQUEST_EUC_FUNCTION_CALL_NAME = "adk_request_credential";
|
|
54
65
|
const REQUEST_CONFIRMATION_FUNCTION_CALL_NAME = "adk_request_confirmation";
|
|
55
66
|
const functionsExportedForTestingOnly = {
|
|
56
|
-
handleFunctionCallList
|
|
67
|
+
handleFunctionCallList,
|
|
68
|
+
generateAuthEvent,
|
|
69
|
+
generateRequestConfirmationEvent
|
|
57
70
|
};
|
|
58
71
|
function generateClientFunctionCallId() {
|
|
59
72
|
return `${AF_FUNCTION_CALL_ID_PREFIX}${(0, import_env_aware_utils.randomUUID)()}`;
|
|
@@ -92,7 +105,7 @@ function getLongRunningFunctionCalls(functionCalls, toolsDict) {
|
|
|
92
105
|
}
|
|
93
106
|
function generateAuthEvent(invocationContext, functionResponseEvent) {
|
|
94
107
|
var _a;
|
|
95
|
-
if (!((_a = functionResponseEvent.actions) == null ? void 0 : _a.requestedAuthConfigs)) {
|
|
108
|
+
if (!((_a = functionResponseEvent.actions) == null ? void 0 : _a.requestedAuthConfigs) || (0, import_isEmpty.default)(functionResponseEvent.actions.requestedAuthConfigs)) {
|
|
96
109
|
return void 0;
|
|
97
110
|
}
|
|
98
111
|
const parts = [];
|
|
@@ -128,7 +141,7 @@ function generateRequestConfirmationEvent({
|
|
|
128
141
|
functionResponseEvent
|
|
129
142
|
}) {
|
|
130
143
|
var _a, _b;
|
|
131
|
-
if (!((_a = functionResponseEvent.actions) == null ? void 0 : _a.requestedToolConfirmations)) {
|
|
144
|
+
if (!((_a = functionResponseEvent.actions) == null ? void 0 : _a.requestedToolConfirmations) || (0, import_isEmpty.default)(functionResponseEvent.actions.requestedToolConfirmations)) {
|
|
132
145
|
return;
|
|
133
146
|
}
|
|
134
147
|
const parts = [];
|
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
"use strict";
|
|
8
|
+
var __create = Object.create;
|
|
8
9
|
var __defProp = Object.defineProperty;
|
|
9
10
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
11
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
12
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
11
13
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
14
|
var __export = (target, all) => {
|
|
13
15
|
for (var name in all)
|
|
@@ -21,22 +23,39 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
21
23
|
}
|
|
22
24
|
return to;
|
|
23
25
|
};
|
|
26
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
27
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
28
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
29
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
30
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
31
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
32
|
+
mod
|
|
33
|
+
));
|
|
24
34
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
35
|
var llm_agent_exports = {};
|
|
26
36
|
__export(llm_agent_exports, {
|
|
27
37
|
LlmAgent: () => LlmAgent,
|
|
28
|
-
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR: () => REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR
|
|
38
|
+
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR: () => REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
39
|
+
responseProcessor: () => responseProcessor
|
|
29
40
|
});
|
|
30
41
|
module.exports = __toCommonJS(llm_agent_exports);
|
|
42
|
+
var import_cloneDeep = __toESM(require("lodash-es/cloneDeep.js"), 1);
|
|
31
43
|
var import_zod = require("zod");
|
|
44
|
+
var import_base_code_executor = require("../code_executors/base_code_executor.js");
|
|
45
|
+
var import_built_in_code_executor = require("../code_executors/built_in_code_executor.js");
|
|
46
|
+
var import_code_execution_utils = require("../code_executors/code_execution_utils.js");
|
|
47
|
+
var import_code_executor_context = require("../code_executors/code_executor_context.js");
|
|
32
48
|
var import_event = require("../events/event.js");
|
|
49
|
+
var import_event_actions = require("../events/event_actions.js");
|
|
33
50
|
var import_base_llm = require("../models/base_llm.js");
|
|
34
51
|
var import_llm_request = require("../models/llm_request.js");
|
|
35
52
|
var import_registry = require("../models/registry.js");
|
|
53
|
+
var import_state = require("../sessions/state.js");
|
|
36
54
|
var import_base_tool = require("../tools/base_tool.js");
|
|
37
55
|
var import_function_tool = require("../tools/function_tool.js");
|
|
38
56
|
var import_tool_confirmation = require("../tools/tool_confirmation.js");
|
|
39
57
|
var import_tool_context = require("../tools/tool_context.js");
|
|
58
|
+
var import_env_aware_utils = require("../utils/env_aware_utils.js");
|
|
40
59
|
var import_logger = require("../utils/logger.js");
|
|
41
60
|
var import_base_agent = require("./base_agent.js");
|
|
42
61
|
var import_base_llm_processor = require("./base_llm_processor.js");
|
|
@@ -353,6 +372,321 @@ class RequestConfirmationLlmRequestProcessor extends import_base_llm_processor.B
|
|
|
353
372
|
}
|
|
354
373
|
}
|
|
355
374
|
const REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR = new RequestConfirmationLlmRequestProcessor();
|
|
375
|
+
class CodeExecutionRequestProcessor extends import_base_llm_processor.BaseLlmRequestProcessor {
|
|
376
|
+
async *runAsync(invocationContext, llmRequest) {
|
|
377
|
+
if (!(invocationContext.agent instanceof LlmAgent)) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (!invocationContext.agent.codeExecutor) {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
for await (const event of runPreProcessor(invocationContext, llmRequest)) {
|
|
384
|
+
yield event;
|
|
385
|
+
}
|
|
386
|
+
if (!(invocationContext.agent.codeExecutor instanceof import_base_code_executor.BaseCodeExecutor)) {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
for (const content of llmRequest.contents) {
|
|
390
|
+
const delimeters = invocationContext.agent.codeExecutor.codeBlockDelimiters.length ? invocationContext.agent.codeExecutor.codeBlockDelimiters[0] : ["", ""];
|
|
391
|
+
const codeExecutionParts = (0, import_code_execution_utils.convertCodeExecutionParts)(
|
|
392
|
+
content,
|
|
393
|
+
delimeters,
|
|
394
|
+
invocationContext.agent.codeExecutor.executionResultDelimiters
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
const DATA_FILE_UTIL_MAP = {
|
|
400
|
+
"text/csv": {
|
|
401
|
+
extension: ".csv",
|
|
402
|
+
loaderCodeTemplate: "pd.read_csv('{filename}')"
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
const DATA_FILE_HELPER_LIB = `
|
|
406
|
+
import pandas as pd
|
|
407
|
+
|
|
408
|
+
def explore_df(df: pd.DataFrame) -> None:
|
|
409
|
+
"""Prints some information about a pandas DataFrame."""
|
|
410
|
+
|
|
411
|
+
with pd.option_context(
|
|
412
|
+
'display.max_columns', None, 'display.expand_frame_repr', False
|
|
413
|
+
):
|
|
414
|
+
# Print the column names to never encounter KeyError when selecting one.
|
|
415
|
+
df_dtypes = df.dtypes
|
|
416
|
+
|
|
417
|
+
# Obtain information about data types and missing values.
|
|
418
|
+
df_nulls = (len(df) - df.isnull().sum()).apply(
|
|
419
|
+
lambda x: f'{x} / {df.shape[0]} non-null'
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
# Explore unique total values in columns using \`.unique()\`.
|
|
423
|
+
df_unique_count = df.apply(lambda x: len(x.unique()))
|
|
424
|
+
|
|
425
|
+
# Explore unique values in columns using \`.unique()\`.
|
|
426
|
+
df_unique = df.apply(lambda x: crop(str(list(x.unique()))))
|
|
427
|
+
|
|
428
|
+
df_info = pd.concat(
|
|
429
|
+
(
|
|
430
|
+
df_dtypes.rename('Dtype'),
|
|
431
|
+
df_nulls.rename('Non-Null Count'),
|
|
432
|
+
df_unique_count.rename('Unique Values Count'),
|
|
433
|
+
df_unique.rename('Unique Values'),
|
|
434
|
+
),
|
|
435
|
+
axis=1,
|
|
436
|
+
)
|
|
437
|
+
df_info.index.name = 'Columns'
|
|
438
|
+
print(f"""Total rows: {df.shape[0]}
|
|
439
|
+
Total columns: {df.shape[1]}
|
|
440
|
+
|
|
441
|
+
{df_info}""")
|
|
442
|
+
`;
|
|
443
|
+
class CodeExecutionResponseProcessor {
|
|
444
|
+
/**
|
|
445
|
+
* Processes the LLM response asynchronously.
|
|
446
|
+
*
|
|
447
|
+
* @param invocationContext The invocation context
|
|
448
|
+
* @param llmResponse The LLM response to process
|
|
449
|
+
* @returns An async generator yielding events
|
|
450
|
+
*/
|
|
451
|
+
async *runAsync(invocationContext, llmResponse) {
|
|
452
|
+
if (llmResponse.partial) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
for await (const event of runPostProcessor(invocationContext, llmResponse)) {
|
|
456
|
+
yield event;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
const responseProcessor = new CodeExecutionResponseProcessor();
|
|
461
|
+
async function* runPreProcessor(invocationContext, llmRequest) {
|
|
462
|
+
const agent = invocationContext.agent;
|
|
463
|
+
if (!(agent instanceof LlmAgent)) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const codeExecutor = agent.codeExecutor;
|
|
467
|
+
if (!codeExecutor || !(codeExecutor instanceof import_base_code_executor.BaseCodeExecutor)) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
if (codeExecutor instanceof import_built_in_code_executor.BuiltInCodeExecutor) {
|
|
471
|
+
codeExecutor.processLlmRequest(llmRequest);
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
if (!codeExecutor.optimizeDataFile) {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
const codeExecutorContext = new import_code_executor_context.CodeExecutorContext(new import_state.State(invocationContext.session.state));
|
|
478
|
+
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
const allInputFiles = extractAndReplaceInlineFiles(codeExecutorContext, llmRequest);
|
|
482
|
+
const processedFileNames = new Set(codeExecutorContext.getProcessedFileNames());
|
|
483
|
+
const filesToProcess = allInputFiles.filter((f) => !processedFileNames.has(f.name));
|
|
484
|
+
for (const file of filesToProcess) {
|
|
485
|
+
const codeStr = getDataFilePreprocessingCode(file);
|
|
486
|
+
if (!codeStr) {
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const codeContent = {
|
|
490
|
+
role: "model",
|
|
491
|
+
parts: [
|
|
492
|
+
{ text: `Processing input file: \`${file.name}\`` },
|
|
493
|
+
(0, import_code_execution_utils.buildExecutableCodePart)(codeStr)
|
|
494
|
+
]
|
|
495
|
+
};
|
|
496
|
+
llmRequest.contents.push((0, import_cloneDeep.default)(codeContent));
|
|
497
|
+
yield (0, import_event.createEvent)({
|
|
498
|
+
invocationId: invocationContext.invocationId,
|
|
499
|
+
author: agent.name,
|
|
500
|
+
branch: invocationContext.branch,
|
|
501
|
+
content: codeContent
|
|
502
|
+
});
|
|
503
|
+
const executionId = getOrSetExecutionId(invocationContext, codeExecutorContext);
|
|
504
|
+
const codeExecutionResult = await codeExecutor.executeCode({
|
|
505
|
+
invocationContext,
|
|
506
|
+
codeExecutionInput: {
|
|
507
|
+
code: codeStr,
|
|
508
|
+
inputFiles: [file],
|
|
509
|
+
executionId
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
codeExecutorContext.updateCodeExecutionResult({
|
|
513
|
+
invocationId: invocationContext.invocationId,
|
|
514
|
+
code: codeStr,
|
|
515
|
+
resultStdout: codeExecutionResult.stdout,
|
|
516
|
+
resultStderr: codeExecutionResult.stderr
|
|
517
|
+
});
|
|
518
|
+
codeExecutorContext.addProcessedFileNames([file.name]);
|
|
519
|
+
const executionResultEvent = await postProcessCodeExecutionResult(
|
|
520
|
+
invocationContext,
|
|
521
|
+
codeExecutorContext,
|
|
522
|
+
codeExecutionResult
|
|
523
|
+
);
|
|
524
|
+
yield executionResultEvent;
|
|
525
|
+
llmRequest.contents.push((0, import_cloneDeep.default)(executionResultEvent.content));
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
async function* runPostProcessor(invocationContext, llmResponse) {
|
|
529
|
+
const agent = invocationContext.agent;
|
|
530
|
+
if (!(agent instanceof LlmAgent)) {
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
const codeExecutor = agent.codeExecutor;
|
|
534
|
+
if (!codeExecutor || !(codeExecutor instanceof import_base_code_executor.BaseCodeExecutor)) {
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
if (!llmResponse || !llmResponse.content) {
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
if (codeExecutor instanceof import_built_in_code_executor.BuiltInCodeExecutor) {
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
const codeExecutorContext = new import_code_executor_context.CodeExecutorContext(new import_state.State(invocationContext.session.state));
|
|
544
|
+
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
const responseContent = llmResponse.content;
|
|
548
|
+
const codeStr = (0, import_code_execution_utils.extractCodeAndTruncateContent)(
|
|
549
|
+
responseContent,
|
|
550
|
+
codeExecutor.codeBlockDelimiters
|
|
551
|
+
);
|
|
552
|
+
if (!codeStr) {
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
yield (0, import_event.createEvent)({
|
|
556
|
+
invocationId: invocationContext.invocationId,
|
|
557
|
+
author: agent.name,
|
|
558
|
+
branch: invocationContext.branch,
|
|
559
|
+
content: responseContent
|
|
560
|
+
});
|
|
561
|
+
const executionId = getOrSetExecutionId(invocationContext, codeExecutorContext);
|
|
562
|
+
const codeExecutionResult = await codeExecutor.executeCode({
|
|
563
|
+
invocationContext,
|
|
564
|
+
codeExecutionInput: {
|
|
565
|
+
code: codeStr,
|
|
566
|
+
inputFiles: codeExecutorContext.getInputFiles(),
|
|
567
|
+
executionId
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
codeExecutorContext.updateCodeExecutionResult({
|
|
571
|
+
invocationId: invocationContext.invocationId,
|
|
572
|
+
code: codeStr,
|
|
573
|
+
resultStdout: codeExecutionResult.stdout,
|
|
574
|
+
resultStderr: codeExecutionResult.stderr
|
|
575
|
+
});
|
|
576
|
+
yield await postProcessCodeExecutionResult(
|
|
577
|
+
invocationContext,
|
|
578
|
+
codeExecutorContext,
|
|
579
|
+
codeExecutionResult
|
|
580
|
+
);
|
|
581
|
+
llmResponse.content = null;
|
|
582
|
+
}
|
|
583
|
+
function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
584
|
+
var _a;
|
|
585
|
+
const allInputFiles = codeExecutorContext.getInputFiles();
|
|
586
|
+
const savedFileNames = new Set(allInputFiles.map((f) => f.name));
|
|
587
|
+
for (let i = 0; i < llmRequest.contents.length; i++) {
|
|
588
|
+
const content = llmRequest.contents[i];
|
|
589
|
+
if (content.role !== "user" || !content.parts) {
|
|
590
|
+
continue;
|
|
591
|
+
}
|
|
592
|
+
for (let j = 0; j < content.parts.length; j++) {
|
|
593
|
+
const part = content.parts[j];
|
|
594
|
+
const mimeType = (_a = part.inlineData) == null ? void 0 : _a.mimeType;
|
|
595
|
+
if (!mimeType || !part.inlineData || !DATA_FILE_UTIL_MAP[mimeType]) {
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
const fileName = `data_${i + 1}_${j + 1}${DATA_FILE_UTIL_MAP[mimeType].extension}`;
|
|
599
|
+
part.text = `
|
|
600
|
+
Available file: \`${fileName}\`
|
|
601
|
+
`;
|
|
602
|
+
const file = {
|
|
603
|
+
name: fileName,
|
|
604
|
+
content: (0, import_env_aware_utils.base64Decode)(part.inlineData.data),
|
|
605
|
+
mimeType
|
|
606
|
+
};
|
|
607
|
+
if (!savedFileNames.has(fileName)) {
|
|
608
|
+
codeExecutorContext.addInputFiles([file]);
|
|
609
|
+
allInputFiles.push(file);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return allInputFiles;
|
|
614
|
+
}
|
|
615
|
+
function getOrSetExecutionId(invocationContext, codeExecutorContext) {
|
|
616
|
+
var _a;
|
|
617
|
+
const agent = invocationContext.agent;
|
|
618
|
+
if (!(agent instanceof LlmAgent) || !((_a = agent.codeExecutor) == null ? void 0 : _a.stateful)) {
|
|
619
|
+
return void 0;
|
|
620
|
+
}
|
|
621
|
+
let executionId = codeExecutorContext.getExecutionId();
|
|
622
|
+
if (!executionId) {
|
|
623
|
+
executionId = invocationContext.session.id;
|
|
624
|
+
codeExecutorContext.setExecutionId(executionId);
|
|
625
|
+
}
|
|
626
|
+
return executionId;
|
|
627
|
+
}
|
|
628
|
+
async function postProcessCodeExecutionResult(invocationContext, codeExecutorContext, codeExecutionResult) {
|
|
629
|
+
if (!invocationContext.artifactService) {
|
|
630
|
+
throw new Error("Artifact service is not initialized.");
|
|
631
|
+
}
|
|
632
|
+
const resultContent = {
|
|
633
|
+
role: "model",
|
|
634
|
+
parts: [(0, import_code_execution_utils.buildCodeExecutionResultPart)(codeExecutionResult)]
|
|
635
|
+
};
|
|
636
|
+
const eventActions = (0, import_event_actions.createEventActions)({ stateDelta: codeExecutorContext.getStateDelta() });
|
|
637
|
+
if (codeExecutionResult.stderr) {
|
|
638
|
+
codeExecutorContext.incrementErrorCount(invocationContext.invocationId);
|
|
639
|
+
} else {
|
|
640
|
+
codeExecutorContext.resetErrorCount(invocationContext.invocationId);
|
|
641
|
+
}
|
|
642
|
+
for (const outputFile of codeExecutionResult.outputFiles) {
|
|
643
|
+
const version = await invocationContext.artifactService.saveArtifact({
|
|
644
|
+
appName: invocationContext.appName || "",
|
|
645
|
+
userId: invocationContext.userId || "",
|
|
646
|
+
sessionId: invocationContext.session.id,
|
|
647
|
+
filename: outputFile.name,
|
|
648
|
+
artifact: {
|
|
649
|
+
inlineData: { data: outputFile.content, mimeType: outputFile.mimeType }
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
eventActions.artifactDelta[outputFile.name] = version;
|
|
653
|
+
}
|
|
654
|
+
return (0, import_event.createEvent)({
|
|
655
|
+
invocationId: invocationContext.invocationId,
|
|
656
|
+
author: invocationContext.agent.name,
|
|
657
|
+
branch: invocationContext.branch,
|
|
658
|
+
content: resultContent,
|
|
659
|
+
actions: eventActions
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
function getDataFilePreprocessingCode(file) {
|
|
663
|
+
function getNormalizedFileName(fileName) {
|
|
664
|
+
const [varName2] = fileName.split(".");
|
|
665
|
+
let normalizedName = varName2.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
666
|
+
if (/^\d/.test(normalizedName)) {
|
|
667
|
+
normalizedName = "_" + normalizedName;
|
|
668
|
+
}
|
|
669
|
+
return normalizedName;
|
|
670
|
+
}
|
|
671
|
+
if (!DATA_FILE_UTIL_MAP[file.mimeType]) {
|
|
672
|
+
return void 0;
|
|
673
|
+
}
|
|
674
|
+
const varName = getNormalizedFileName(file.name);
|
|
675
|
+
const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace(
|
|
676
|
+
"{filename}",
|
|
677
|
+
file.name
|
|
678
|
+
);
|
|
679
|
+
return `
|
|
680
|
+
${DATA_FILE_HELPER_LIB}
|
|
681
|
+
|
|
682
|
+
# Load the dataframe.
|
|
683
|
+
${varName} = ${loaderCode}
|
|
684
|
+
|
|
685
|
+
# Use \`explore_df\` to guide my analysis.
|
|
686
|
+
explore_df(${varName})
|
|
687
|
+
`;
|
|
688
|
+
}
|
|
689
|
+
const CODE_EXECUTION_REQUEST_PROCESSOR = new CodeExecutionRequestProcessor();
|
|
356
690
|
class LlmAgent extends import_base_agent.BaseAgent {
|
|
357
691
|
constructor(config) {
|
|
358
692
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
@@ -372,12 +706,14 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
372
706
|
this.afterModelCallback = config.afterModelCallback;
|
|
373
707
|
this.beforeToolCallback = config.beforeToolCallback;
|
|
374
708
|
this.afterToolCallback = config.afterToolCallback;
|
|
709
|
+
this.codeExecutor = config.codeExecutor;
|
|
375
710
|
this.requestProcessors = (_g = config.requestProcessors) != null ? _g : [
|
|
376
711
|
BASIC_LLM_REQUEST_PROCESSOR,
|
|
377
712
|
IDENTITY_LLM_REQUEST_PROCESSOR,
|
|
378
713
|
INSTRUCTIONS_LLM_REQUEST_PROCESSOR,
|
|
379
714
|
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
380
|
-
CONTENT_REQUEST_PROCESSOR
|
|
715
|
+
CONTENT_REQUEST_PROCESSOR,
|
|
716
|
+
CODE_EXECUTION_REQUEST_PROCESSOR
|
|
381
717
|
];
|
|
382
718
|
this.responseProcessors = (_h = config.responseProcessors) != null ? _h : [];
|
|
383
719
|
const agentTransferDisabled = this.disallowTransferToParent && this.disallowTransferToPeers && !((_i = this.subAgents) == null ? void 0 : _i.length);
|
|
@@ -427,7 +763,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
427
763
|
* When not set, the agent will inherit the model from its ancestor.
|
|
428
764
|
*/
|
|
429
765
|
get canonicalModel() {
|
|
430
|
-
if (
|
|
766
|
+
if ((0, import_base_llm.isBaseLlm)(this.model)) {
|
|
431
767
|
return this.model;
|
|
432
768
|
}
|
|
433
769
|
if (typeof this.model === "string" && this.model) {
|
|
@@ -861,5 +1197,6 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
861
1197
|
// Annotate the CommonJS export names for ESM import in node:
|
|
862
1198
|
0 && (module.exports = {
|
|
863
1199
|
LlmAgent,
|
|
864
|
-
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR
|
|
1200
|
+
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
1201
|
+
responseProcessor
|
|
865
1202
|
});
|
|
@@ -22,23 +22,19 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
22
22
|
return to;
|
|
23
23
|
};
|
|
24
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
-
var
|
|
26
|
-
__export(
|
|
27
|
-
|
|
25
|
+
var base_credential_exchanger_exports = {};
|
|
26
|
+
__export(base_credential_exchanger_exports, {
|
|
27
|
+
CredentialExchangeError: () => CredentialExchangeError
|
|
28
28
|
});
|
|
29
|
-
module.exports = __toCommonJS(
|
|
29
|
+
module.exports = __toCommonJS(base_credential_exchanger_exports);
|
|
30
30
|
/**
|
|
31
31
|
* @license
|
|
32
32
|
* Copyright 2025 Google LLC
|
|
33
33
|
* SPDX-License-Identifier: Apache-2.0
|
|
34
34
|
*/
|
|
35
|
-
|
|
36
|
-
if (obj === void 0) {
|
|
37
|
-
return void 0;
|
|
38
|
-
}
|
|
39
|
-
return JSON.parse(JSON.stringify(obj));
|
|
35
|
+
class CredentialExchangeError extends Error {
|
|
40
36
|
}
|
|
41
37
|
// Annotate the CommonJS export names for ESM import in node:
|
|
42
38
|
0 && (module.exports = {
|
|
43
|
-
|
|
39
|
+
CredentialExchangeError
|
|
44
40
|
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 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 credential_exchanger_registry_exports = {};
|
|
26
|
+
__export(credential_exchanger_registry_exports, {
|
|
27
|
+
CredentialExchangerRegistry: () => CredentialExchangerRegistry
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(credential_exchanger_registry_exports);
|
|
30
|
+
/**
|
|
31
|
+
* @license
|
|
32
|
+
* Copyright 2025 Google LLC
|
|
33
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
34
|
+
*/
|
|
35
|
+
class CredentialExchangerRegistry {
|
|
36
|
+
constructor() {
|
|
37
|
+
this.exchangers = {};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Register an exchanger instance for a credential type.
|
|
41
|
+
* @param credentialType - The credential type to register for.
|
|
42
|
+
* @param exchangerInstance - The exchanger instance to register.
|
|
43
|
+
*/
|
|
44
|
+
register(credentialType, exchangerInstance) {
|
|
45
|
+
this.exchangers[credentialType] = exchangerInstance;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get the exchanger instance for a credential type.
|
|
49
|
+
* @param credentialType - The credential type to get exchanger for.
|
|
50
|
+
* @returns The exchanger instance if registered, undefined otherwise.
|
|
51
|
+
*/
|
|
52
|
+
getExchanger(credentialType) {
|
|
53
|
+
return this.exchangers[credentialType];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
57
|
+
0 && (module.exports = {
|
|
58
|
+
CredentialExchangerRegistry
|
|
59
|
+
});
|
|
@@ -43,7 +43,7 @@ class BuiltInCodeExecutor extends import_base_code_executor.BaseCodeExecutor {
|
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
processLlmRequest(llmRequest) {
|
|
46
|
-
if (llmRequest.model && (0, import_model_name.
|
|
46
|
+
if (llmRequest.model && (0, import_model_name.isGemini2OrAbove)(llmRequest.model)) {
|
|
47
47
|
llmRequest.config = llmRequest.config || {};
|
|
48
48
|
llmRequest.config.tools = llmRequest.config.tools || [];
|
|
49
49
|
llmRequest.config.tools.push({ codeExecution: {} });
|