@google/adk 0.2.1 → 0.2.2
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 +2 -2
- package/dist/cjs/agents/functions.js +6 -3
- package/dist/cjs/agents/llm_agent.js +331 -4
- package/dist/cjs/auth/exchanger/base_credential_exchanger.js +40 -0
- package/dist/cjs/auth/exchanger/credential_exchanger_registry.js +59 -0
- package/dist/cjs/code_executors/code_execution_utils.js +2 -2
- package/dist/cjs/code_executors/code_executor_context.js +2 -2
- package/dist/cjs/common.js +7 -0
- package/dist/cjs/index.js +63 -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 +3 -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/version.js +2 -2
- 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/code_execution_utils.js +2 -2
- package/dist/esm/code_executors/code_executor_context.js +2 -2
- package/dist/esm/common.js +6 -2
- package/dist/esm/index.js +63 -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/version.js +2 -2
- 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 +3 -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/version.d.ts +2 -2
- 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/code_execution_utils.js +2 -2
- package/dist/web/code_executors/code_executor_context.js +2 -2
- package/dist/web/common.js +6 -2
- package/dist/web/index.js +6 -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/version.js +2 -2
- package/package.json +3 -1
|
@@ -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
|
});
|
|
@@ -28,8 +28,8 @@ __export(content_processor_utils_exports, {
|
|
|
28
28
|
getCurrentTurnContents: () => getCurrentTurnContents
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(content_processor_utils_exports);
|
|
31
|
+
var import_lodash = require("lodash");
|
|
31
32
|
var import_event = require("../events/event.js");
|
|
32
|
-
var import_deep_clone = require("../utils/deep_clone.js");
|
|
33
33
|
var import_functions = require("./functions.js");
|
|
34
34
|
/**
|
|
35
35
|
* @license
|
|
@@ -60,7 +60,7 @@ function getContents(events, agentName, currentBranch) {
|
|
|
60
60
|
resultEvents = rearrangeEventsForAsyncFunctionResponsesInHistory(resultEvents);
|
|
61
61
|
const contents = [];
|
|
62
62
|
for (const event of resultEvents) {
|
|
63
|
-
const content = (0,
|
|
63
|
+
const content = (0, import_lodash.cloneDeep)(event.content);
|
|
64
64
|
(0, import_functions.removeClientFunctionCallId)(content);
|
|
65
65
|
contents.push(content);
|
|
66
66
|
}
|
|
@@ -39,6 +39,7 @@ __export(functions_exports, {
|
|
|
39
39
|
});
|
|
40
40
|
module.exports = __toCommonJS(functions_exports);
|
|
41
41
|
var import_genai = require("@google/genai");
|
|
42
|
+
var import_lodash = require("lodash");
|
|
42
43
|
var import_event = require("../events/event.js");
|
|
43
44
|
var import_event_actions = require("../events/event_actions.js");
|
|
44
45
|
var import_tool_context = require("../tools/tool_context.js");
|
|
@@ -53,7 +54,9 @@ const AF_FUNCTION_CALL_ID_PREFIX = "adk-";
|
|
|
53
54
|
const REQUEST_EUC_FUNCTION_CALL_NAME = "adk_request_credential";
|
|
54
55
|
const REQUEST_CONFIRMATION_FUNCTION_CALL_NAME = "adk_request_confirmation";
|
|
55
56
|
const functionsExportedForTestingOnly = {
|
|
56
|
-
handleFunctionCallList
|
|
57
|
+
handleFunctionCallList,
|
|
58
|
+
generateAuthEvent,
|
|
59
|
+
generateRequestConfirmationEvent
|
|
57
60
|
};
|
|
58
61
|
function generateClientFunctionCallId() {
|
|
59
62
|
return `${AF_FUNCTION_CALL_ID_PREFIX}${(0, import_env_aware_utils.randomUUID)()}`;
|
|
@@ -92,7 +95,7 @@ function getLongRunningFunctionCalls(functionCalls, toolsDict) {
|
|
|
92
95
|
}
|
|
93
96
|
function generateAuthEvent(invocationContext, functionResponseEvent) {
|
|
94
97
|
var _a;
|
|
95
|
-
if (!((_a = functionResponseEvent.actions) == null ? void 0 : _a.requestedAuthConfigs)) {
|
|
98
|
+
if (!((_a = functionResponseEvent.actions) == null ? void 0 : _a.requestedAuthConfigs) || (0, import_lodash.isEmpty)(functionResponseEvent.actions.requestedAuthConfigs)) {
|
|
96
99
|
return void 0;
|
|
97
100
|
}
|
|
98
101
|
const parts = [];
|
|
@@ -128,7 +131,7 @@ function generateRequestConfirmationEvent({
|
|
|
128
131
|
functionResponseEvent
|
|
129
132
|
}) {
|
|
130
133
|
var _a, _b;
|
|
131
|
-
if (!((_a = functionResponseEvent.actions) == null ? void 0 : _a.requestedToolConfirmations)) {
|
|
134
|
+
if (!((_a = functionResponseEvent.actions) == null ? void 0 : _a.requestedToolConfirmations) || (0, import_lodash.isEmpty)(functionResponseEvent.actions.requestedToolConfirmations)) {
|
|
132
135
|
return;
|
|
133
136
|
}
|
|
134
137
|
const parts = [];
|
|
@@ -25,18 +25,27 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
25
25
|
var llm_agent_exports = {};
|
|
26
26
|
__export(llm_agent_exports, {
|
|
27
27
|
LlmAgent: () => LlmAgent,
|
|
28
|
-
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR: () => REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR
|
|
28
|
+
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR: () => REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
29
|
+
responseProcessor: () => responseProcessor
|
|
29
30
|
});
|
|
30
31
|
module.exports = __toCommonJS(llm_agent_exports);
|
|
32
|
+
var import_lodash = require("lodash");
|
|
31
33
|
var import_zod = require("zod");
|
|
34
|
+
var import_base_code_executor = require("../code_executors/base_code_executor.js");
|
|
35
|
+
var import_built_in_code_executor = require("../code_executors/built_in_code_executor.js");
|
|
36
|
+
var import_code_execution_utils = require("../code_executors/code_execution_utils.js");
|
|
37
|
+
var import_code_executor_context = require("../code_executors/code_executor_context.js");
|
|
32
38
|
var import_event = require("../events/event.js");
|
|
39
|
+
var import_event_actions = require("../events/event_actions.js");
|
|
33
40
|
var import_base_llm = require("../models/base_llm.js");
|
|
34
41
|
var import_llm_request = require("../models/llm_request.js");
|
|
35
42
|
var import_registry = require("../models/registry.js");
|
|
43
|
+
var import_state = require("../sessions/state.js");
|
|
36
44
|
var import_base_tool = require("../tools/base_tool.js");
|
|
37
45
|
var import_function_tool = require("../tools/function_tool.js");
|
|
38
46
|
var import_tool_confirmation = require("../tools/tool_confirmation.js");
|
|
39
47
|
var import_tool_context = require("../tools/tool_context.js");
|
|
48
|
+
var import_env_aware_utils = require("../utils/env_aware_utils.js");
|
|
40
49
|
var import_logger = require("../utils/logger.js");
|
|
41
50
|
var import_base_agent = require("./base_agent.js");
|
|
42
51
|
var import_base_llm_processor = require("./base_llm_processor.js");
|
|
@@ -353,6 +362,321 @@ class RequestConfirmationLlmRequestProcessor extends import_base_llm_processor.B
|
|
|
353
362
|
}
|
|
354
363
|
}
|
|
355
364
|
const REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR = new RequestConfirmationLlmRequestProcessor();
|
|
365
|
+
class CodeExecutionRequestProcessor extends import_base_llm_processor.BaseLlmRequestProcessor {
|
|
366
|
+
async *runAsync(invocationContext, llmRequest) {
|
|
367
|
+
if (!(invocationContext.agent instanceof LlmAgent)) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (!invocationContext.agent.codeExecutor) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
for await (const event of runPreProcessor(invocationContext, llmRequest)) {
|
|
374
|
+
yield event;
|
|
375
|
+
}
|
|
376
|
+
if (!(invocationContext.agent.codeExecutor instanceof import_base_code_executor.BaseCodeExecutor)) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
for (const content of llmRequest.contents) {
|
|
380
|
+
const delimeters = invocationContext.agent.codeExecutor.codeBlockDelimiters.length ? invocationContext.agent.codeExecutor.codeBlockDelimiters[0] : ["", ""];
|
|
381
|
+
const codeExecutionParts = (0, import_code_execution_utils.convertCodeExecutionParts)(
|
|
382
|
+
content,
|
|
383
|
+
delimeters,
|
|
384
|
+
invocationContext.agent.codeExecutor.executionResultDelimiters
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
const DATA_FILE_UTIL_MAP = {
|
|
390
|
+
"text/csv": {
|
|
391
|
+
extension: ".csv",
|
|
392
|
+
loaderCodeTemplate: "pd.read_csv('{filename}')"
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
const DATA_FILE_HELPER_LIB = `
|
|
396
|
+
import pandas as pd
|
|
397
|
+
|
|
398
|
+
def explore_df(df: pd.DataFrame) -> None:
|
|
399
|
+
"""Prints some information about a pandas DataFrame."""
|
|
400
|
+
|
|
401
|
+
with pd.option_context(
|
|
402
|
+
'display.max_columns', None, 'display.expand_frame_repr', False
|
|
403
|
+
):
|
|
404
|
+
# Print the column names to never encounter KeyError when selecting one.
|
|
405
|
+
df_dtypes = df.dtypes
|
|
406
|
+
|
|
407
|
+
# Obtain information about data types and missing values.
|
|
408
|
+
df_nulls = (len(df) - df.isnull().sum()).apply(
|
|
409
|
+
lambda x: f'{x} / {df.shape[0]} non-null'
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
# Explore unique total values in columns using \`.unique()\`.
|
|
413
|
+
df_unique_count = df.apply(lambda x: len(x.unique()))
|
|
414
|
+
|
|
415
|
+
# Explore unique values in columns using \`.unique()\`.
|
|
416
|
+
df_unique = df.apply(lambda x: crop(str(list(x.unique()))))
|
|
417
|
+
|
|
418
|
+
df_info = pd.concat(
|
|
419
|
+
(
|
|
420
|
+
df_dtypes.rename('Dtype'),
|
|
421
|
+
df_nulls.rename('Non-Null Count'),
|
|
422
|
+
df_unique_count.rename('Unique Values Count'),
|
|
423
|
+
df_unique.rename('Unique Values'),
|
|
424
|
+
),
|
|
425
|
+
axis=1,
|
|
426
|
+
)
|
|
427
|
+
df_info.index.name = 'Columns'
|
|
428
|
+
print(f"""Total rows: {df.shape[0]}
|
|
429
|
+
Total columns: {df.shape[1]}
|
|
430
|
+
|
|
431
|
+
{df_info}""")
|
|
432
|
+
`;
|
|
433
|
+
class CodeExecutionResponseProcessor {
|
|
434
|
+
/**
|
|
435
|
+
* Processes the LLM response asynchronously.
|
|
436
|
+
*
|
|
437
|
+
* @param invocationContext The invocation context
|
|
438
|
+
* @param llmResponse The LLM response to process
|
|
439
|
+
* @returns An async generator yielding events
|
|
440
|
+
*/
|
|
441
|
+
async *runAsync(invocationContext, llmResponse) {
|
|
442
|
+
if (llmResponse.partial) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
for await (const event of runPostProcessor(invocationContext, llmResponse)) {
|
|
446
|
+
yield event;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
const responseProcessor = new CodeExecutionResponseProcessor();
|
|
451
|
+
async function* runPreProcessor(invocationContext, llmRequest) {
|
|
452
|
+
const agent = invocationContext.agent;
|
|
453
|
+
if (!(agent instanceof LlmAgent)) {
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
const codeExecutor = agent.codeExecutor;
|
|
457
|
+
if (!codeExecutor || !(codeExecutor instanceof import_base_code_executor.BaseCodeExecutor)) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (codeExecutor instanceof import_built_in_code_executor.BuiltInCodeExecutor) {
|
|
461
|
+
codeExecutor.processLlmRequest(llmRequest);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (!codeExecutor.optimizeDataFile) {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
const codeExecutorContext = new import_code_executor_context.CodeExecutorContext(new import_state.State(invocationContext.session.state));
|
|
468
|
+
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
const allInputFiles = extractAndReplaceInlineFiles(codeExecutorContext, llmRequest);
|
|
472
|
+
const processedFileNames = new Set(codeExecutorContext.getProcessedFileNames());
|
|
473
|
+
const filesToProcess = allInputFiles.filter((f) => !processedFileNames.has(f.name));
|
|
474
|
+
for (const file of filesToProcess) {
|
|
475
|
+
const codeStr = getDataFilePreprocessingCode(file);
|
|
476
|
+
if (!codeStr) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const codeContent = {
|
|
480
|
+
role: "model",
|
|
481
|
+
parts: [
|
|
482
|
+
{ text: `Processing input file: \`${file.name}\`` },
|
|
483
|
+
(0, import_code_execution_utils.buildExecutableCodePart)(codeStr)
|
|
484
|
+
]
|
|
485
|
+
};
|
|
486
|
+
llmRequest.contents.push((0, import_lodash.cloneDeep)(codeContent));
|
|
487
|
+
yield (0, import_event.createEvent)({
|
|
488
|
+
invocationId: invocationContext.invocationId,
|
|
489
|
+
author: agent.name,
|
|
490
|
+
branch: invocationContext.branch,
|
|
491
|
+
content: codeContent
|
|
492
|
+
});
|
|
493
|
+
const executionId = getOrSetExecutionId(invocationContext, codeExecutorContext);
|
|
494
|
+
const codeExecutionResult = await codeExecutor.executeCode({
|
|
495
|
+
invocationContext,
|
|
496
|
+
codeExecutionInput: {
|
|
497
|
+
code: codeStr,
|
|
498
|
+
inputFiles: [file],
|
|
499
|
+
executionId
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
codeExecutorContext.updateCodeExecutionResult({
|
|
503
|
+
invocationId: invocationContext.invocationId,
|
|
504
|
+
code: codeStr,
|
|
505
|
+
resultStdout: codeExecutionResult.stdout,
|
|
506
|
+
resultStderr: codeExecutionResult.stderr
|
|
507
|
+
});
|
|
508
|
+
codeExecutorContext.addProcessedFileNames([file.name]);
|
|
509
|
+
const executionResultEvent = await postProcessCodeExecutionResult(
|
|
510
|
+
invocationContext,
|
|
511
|
+
codeExecutorContext,
|
|
512
|
+
codeExecutionResult
|
|
513
|
+
);
|
|
514
|
+
yield executionResultEvent;
|
|
515
|
+
llmRequest.contents.push((0, import_lodash.cloneDeep)(executionResultEvent.content));
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
async function* runPostProcessor(invocationContext, llmResponse) {
|
|
519
|
+
const agent = invocationContext.agent;
|
|
520
|
+
if (!(agent instanceof LlmAgent)) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
const codeExecutor = agent.codeExecutor;
|
|
524
|
+
if (!codeExecutor || !(codeExecutor instanceof import_base_code_executor.BaseCodeExecutor)) {
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
if (!llmResponse || !llmResponse.content) {
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
if (codeExecutor instanceof import_built_in_code_executor.BuiltInCodeExecutor) {
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
const codeExecutorContext = new import_code_executor_context.CodeExecutorContext(new import_state.State(invocationContext.session.state));
|
|
534
|
+
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const responseContent = llmResponse.content;
|
|
538
|
+
const codeStr = (0, import_code_execution_utils.extractCodeAndTruncateContent)(
|
|
539
|
+
responseContent,
|
|
540
|
+
codeExecutor.codeBlockDelimiters
|
|
541
|
+
);
|
|
542
|
+
if (!codeStr) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
yield (0, import_event.createEvent)({
|
|
546
|
+
invocationId: invocationContext.invocationId,
|
|
547
|
+
author: agent.name,
|
|
548
|
+
branch: invocationContext.branch,
|
|
549
|
+
content: responseContent
|
|
550
|
+
});
|
|
551
|
+
const executionId = getOrSetExecutionId(invocationContext, codeExecutorContext);
|
|
552
|
+
const codeExecutionResult = await codeExecutor.executeCode({
|
|
553
|
+
invocationContext,
|
|
554
|
+
codeExecutionInput: {
|
|
555
|
+
code: codeStr,
|
|
556
|
+
inputFiles: codeExecutorContext.getInputFiles(),
|
|
557
|
+
executionId
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
codeExecutorContext.updateCodeExecutionResult({
|
|
561
|
+
invocationId: invocationContext.invocationId,
|
|
562
|
+
code: codeStr,
|
|
563
|
+
resultStdout: codeExecutionResult.stdout,
|
|
564
|
+
resultStderr: codeExecutionResult.stderr
|
|
565
|
+
});
|
|
566
|
+
yield await postProcessCodeExecutionResult(
|
|
567
|
+
invocationContext,
|
|
568
|
+
codeExecutorContext,
|
|
569
|
+
codeExecutionResult
|
|
570
|
+
);
|
|
571
|
+
llmResponse.content = null;
|
|
572
|
+
}
|
|
573
|
+
function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
574
|
+
var _a;
|
|
575
|
+
const allInputFiles = codeExecutorContext.getInputFiles();
|
|
576
|
+
const savedFileNames = new Set(allInputFiles.map((f) => f.name));
|
|
577
|
+
for (let i = 0; i < llmRequest.contents.length; i++) {
|
|
578
|
+
const content = llmRequest.contents[i];
|
|
579
|
+
if (content.role !== "user" || !content.parts) {
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
for (let j = 0; j < content.parts.length; j++) {
|
|
583
|
+
const part = content.parts[j];
|
|
584
|
+
const mimeType = (_a = part.inlineData) == null ? void 0 : _a.mimeType;
|
|
585
|
+
if (!mimeType || !part.inlineData || !DATA_FILE_UTIL_MAP[mimeType]) {
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
const fileName = `data_${i + 1}_${j + 1}${DATA_FILE_UTIL_MAP[mimeType].extension}`;
|
|
589
|
+
part.text = `
|
|
590
|
+
Available file: \`${fileName}\`
|
|
591
|
+
`;
|
|
592
|
+
const file = {
|
|
593
|
+
name: fileName,
|
|
594
|
+
content: (0, import_env_aware_utils.base64Decode)(part.inlineData.data),
|
|
595
|
+
mimeType
|
|
596
|
+
};
|
|
597
|
+
if (!savedFileNames.has(fileName)) {
|
|
598
|
+
codeExecutorContext.addInputFiles([file]);
|
|
599
|
+
allInputFiles.push(file);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
return allInputFiles;
|
|
604
|
+
}
|
|
605
|
+
function getOrSetExecutionId(invocationContext, codeExecutorContext) {
|
|
606
|
+
var _a;
|
|
607
|
+
const agent = invocationContext.agent;
|
|
608
|
+
if (!(agent instanceof LlmAgent) || !((_a = agent.codeExecutor) == null ? void 0 : _a.stateful)) {
|
|
609
|
+
return void 0;
|
|
610
|
+
}
|
|
611
|
+
let executionId = codeExecutorContext.getExecutionId();
|
|
612
|
+
if (!executionId) {
|
|
613
|
+
executionId = invocationContext.session.id;
|
|
614
|
+
codeExecutorContext.setExecutionId(executionId);
|
|
615
|
+
}
|
|
616
|
+
return executionId;
|
|
617
|
+
}
|
|
618
|
+
async function postProcessCodeExecutionResult(invocationContext, codeExecutorContext, codeExecutionResult) {
|
|
619
|
+
if (!invocationContext.artifactService) {
|
|
620
|
+
throw new Error("Artifact service is not initialized.");
|
|
621
|
+
}
|
|
622
|
+
const resultContent = {
|
|
623
|
+
role: "model",
|
|
624
|
+
parts: [(0, import_code_execution_utils.buildCodeExecutionResultPart)(codeExecutionResult)]
|
|
625
|
+
};
|
|
626
|
+
const eventActions = (0, import_event_actions.createEventActions)({ stateDelta: codeExecutorContext.getStateDelta() });
|
|
627
|
+
if (codeExecutionResult.stderr) {
|
|
628
|
+
codeExecutorContext.incrementErrorCount(invocationContext.invocationId);
|
|
629
|
+
} else {
|
|
630
|
+
codeExecutorContext.resetErrorCount(invocationContext.invocationId);
|
|
631
|
+
}
|
|
632
|
+
for (const outputFile of codeExecutionResult.outputFiles) {
|
|
633
|
+
const version = await invocationContext.artifactService.saveArtifact({
|
|
634
|
+
appName: invocationContext.appName || "",
|
|
635
|
+
userId: invocationContext.userId || "",
|
|
636
|
+
sessionId: invocationContext.session.id,
|
|
637
|
+
filename: outputFile.name,
|
|
638
|
+
artifact: {
|
|
639
|
+
inlineData: { data: outputFile.content, mimeType: outputFile.mimeType }
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
eventActions.artifactDelta[outputFile.name] = version;
|
|
643
|
+
}
|
|
644
|
+
return (0, import_event.createEvent)({
|
|
645
|
+
invocationId: invocationContext.invocationId,
|
|
646
|
+
author: invocationContext.agent.name,
|
|
647
|
+
branch: invocationContext.branch,
|
|
648
|
+
content: resultContent,
|
|
649
|
+
actions: eventActions
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
function getDataFilePreprocessingCode(file) {
|
|
653
|
+
function getNormalizedFileName(fileName) {
|
|
654
|
+
const [varName2] = fileName.split(".");
|
|
655
|
+
let normalizedName = varName2.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
656
|
+
if (/^\d/.test(normalizedName)) {
|
|
657
|
+
normalizedName = "_" + normalizedName;
|
|
658
|
+
}
|
|
659
|
+
return normalizedName;
|
|
660
|
+
}
|
|
661
|
+
if (!DATA_FILE_UTIL_MAP[file.mimeType]) {
|
|
662
|
+
return void 0;
|
|
663
|
+
}
|
|
664
|
+
const varName = getNormalizedFileName(file.name);
|
|
665
|
+
const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace(
|
|
666
|
+
"{filename}",
|
|
667
|
+
file.name
|
|
668
|
+
);
|
|
669
|
+
return `
|
|
670
|
+
${DATA_FILE_HELPER_LIB}
|
|
671
|
+
|
|
672
|
+
# Load the dataframe.
|
|
673
|
+
${varName} = ${loaderCode}
|
|
674
|
+
|
|
675
|
+
# Use \`explore_df\` to guide my analysis.
|
|
676
|
+
explore_df(${varName})
|
|
677
|
+
`;
|
|
678
|
+
}
|
|
679
|
+
const CODE_EXECUTION_REQUEST_PROCESSOR = new CodeExecutionRequestProcessor();
|
|
356
680
|
class LlmAgent extends import_base_agent.BaseAgent {
|
|
357
681
|
constructor(config) {
|
|
358
682
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
@@ -372,12 +696,14 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
372
696
|
this.afterModelCallback = config.afterModelCallback;
|
|
373
697
|
this.beforeToolCallback = config.beforeToolCallback;
|
|
374
698
|
this.afterToolCallback = config.afterToolCallback;
|
|
699
|
+
this.codeExecutor = config.codeExecutor;
|
|
375
700
|
this.requestProcessors = (_g = config.requestProcessors) != null ? _g : [
|
|
376
701
|
BASIC_LLM_REQUEST_PROCESSOR,
|
|
377
702
|
IDENTITY_LLM_REQUEST_PROCESSOR,
|
|
378
703
|
INSTRUCTIONS_LLM_REQUEST_PROCESSOR,
|
|
379
704
|
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
380
|
-
CONTENT_REQUEST_PROCESSOR
|
|
705
|
+
CONTENT_REQUEST_PROCESSOR,
|
|
706
|
+
CODE_EXECUTION_REQUEST_PROCESSOR
|
|
381
707
|
];
|
|
382
708
|
this.responseProcessors = (_h = config.responseProcessors) != null ? _h : [];
|
|
383
709
|
const agentTransferDisabled = this.disallowTransferToParent && this.disallowTransferToPeers && !((_i = this.subAgents) == null ? void 0 : _i.length);
|
|
@@ -427,7 +753,7 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
427
753
|
* When not set, the agent will inherit the model from its ancestor.
|
|
428
754
|
*/
|
|
429
755
|
get canonicalModel() {
|
|
430
|
-
if (
|
|
756
|
+
if ((0, import_base_llm.isBaseLlm)(this.model)) {
|
|
431
757
|
return this.model;
|
|
432
758
|
}
|
|
433
759
|
if (typeof this.model === "string" && this.model) {
|
|
@@ -861,5 +1187,6 @@ class LlmAgent extends import_base_agent.BaseAgent {
|
|
|
861
1187
|
// Annotate the CommonJS export names for ESM import in node:
|
|
862
1188
|
0 && (module.exports = {
|
|
863
1189
|
LlmAgent,
|
|
864
|
-
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR
|
|
1190
|
+
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
1191
|
+
responseProcessor
|
|
865
1192
|
});
|
|
@@ -0,0 +1,40 @@
|
|
|
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 base_credential_exchanger_exports = {};
|
|
26
|
+
__export(base_credential_exchanger_exports, {
|
|
27
|
+
CredentialExchangeError: () => CredentialExchangeError
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(base_credential_exchanger_exports);
|
|
30
|
+
/**
|
|
31
|
+
* @license
|
|
32
|
+
* Copyright 2025 Google LLC
|
|
33
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
34
|
+
*/
|
|
35
|
+
class CredentialExchangeError extends Error {
|
|
36
|
+
}
|
|
37
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
38
|
+
0 && (module.exports = {
|
|
39
|
+
CredentialExchangeError
|
|
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
|
+
});
|
|
@@ -32,7 +32,7 @@ __export(code_execution_utils_exports, {
|
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(code_execution_utils_exports);
|
|
34
34
|
var import_genai = require("@google/genai");
|
|
35
|
-
var
|
|
35
|
+
var import_lodash = require("lodash");
|
|
36
36
|
var import_env_aware_utils = require("../utils/env_aware_utils.js");
|
|
37
37
|
/**
|
|
38
38
|
* @license
|
|
@@ -58,7 +58,7 @@ function extractCodeAndTruncateContent(content, codeBlockDelimiters) {
|
|
|
58
58
|
if (!textParts.length) {
|
|
59
59
|
return "";
|
|
60
60
|
}
|
|
61
|
-
const firstTextPart = (0,
|
|
61
|
+
const firstTextPart = (0, import_lodash.cloneDeep)(textParts[0]);
|
|
62
62
|
const responseText = textParts.map((part) => part.text).join("\n");
|
|
63
63
|
const leadingDelimiterPattern = codeBlockDelimiters.map((d) => d[0]).join("|");
|
|
64
64
|
const trailingDelimiterPattern = codeBlockDelimiters.map((d) => d[1]).join("|");
|
|
@@ -27,7 +27,7 @@ __export(code_executor_context_exports, {
|
|
|
27
27
|
CodeExecutorContext: () => CodeExecutorContext
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(code_executor_context_exports);
|
|
30
|
-
var
|
|
30
|
+
var import_lodash = require("lodash");
|
|
31
31
|
/**
|
|
32
32
|
* @license
|
|
33
33
|
* Copyright 2025 Google LLC
|
|
@@ -52,7 +52,7 @@ class CodeExecutorContext {
|
|
|
52
52
|
*/
|
|
53
53
|
getStateDelta() {
|
|
54
54
|
return {
|
|
55
|
-
[CONTEXT_KEY]: (0,
|
|
55
|
+
[CONTEXT_KEY]: (0, import_lodash.cloneDeep)(this.context)
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
58
|
/**
|
package/dist/cjs/common.js
CHANGED
|
@@ -31,6 +31,7 @@ __export(common_exports, {
|
|
|
31
31
|
BasePlugin: () => import_base_plugin.BasePlugin,
|
|
32
32
|
BaseTool: () => import_base_tool.BaseTool,
|
|
33
33
|
BaseToolset: () => import_base_toolset.BaseToolset,
|
|
34
|
+
BuiltInCodeExecutor: () => import_built_in_code_executor.BuiltInCodeExecutor,
|
|
34
35
|
CallbackContext: () => import_callback_context.CallbackContext,
|
|
35
36
|
FunctionTool: () => import_function_tool.FunctionTool,
|
|
36
37
|
GOOGLE_SEARCH: () => import_google_search_tool.GOOGLE_SEARCH,
|
|
@@ -67,6 +68,8 @@ __export(common_exports, {
|
|
|
67
68
|
getFunctionCalls: () => import_event.getFunctionCalls,
|
|
68
69
|
getFunctionResponses: () => import_event.getFunctionResponses,
|
|
69
70
|
hasTrailingCodeExecutionResult: () => import_event.hasTrailingCodeExecutionResult,
|
|
71
|
+
isBaseAgent: () => import_base_agent.isBaseAgent,
|
|
72
|
+
isBaseLlm: () => import_base_llm.isBaseLlm,
|
|
70
73
|
isFinalResponse: () => import_event.isFinalResponse,
|
|
71
74
|
setLogLevel: () => import_logger.setLogLevel,
|
|
72
75
|
stringifyContent: () => import_event.stringifyContent,
|
|
@@ -85,6 +88,7 @@ var import_parallel_agent = require("./agents/parallel_agent.js");
|
|
|
85
88
|
var import_run_config = require("./agents/run_config.js");
|
|
86
89
|
var import_sequential_agent = require("./agents/sequential_agent.js");
|
|
87
90
|
var import_in_memory_artifact_service = require("./artifacts/in_memory_artifact_service.js");
|
|
91
|
+
var import_built_in_code_executor = require("./code_executors/built_in_code_executor.js");
|
|
88
92
|
var import_event = require("./events/event.js");
|
|
89
93
|
var import_event_actions = require("./events/event_actions.js");
|
|
90
94
|
var import_in_memory_memory_service = require("./memory/in_memory_memory_service.js");
|
|
@@ -128,6 +132,7 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
|
|
|
128
132
|
BasePlugin,
|
|
129
133
|
BaseTool,
|
|
130
134
|
BaseToolset,
|
|
135
|
+
BuiltInCodeExecutor,
|
|
131
136
|
CallbackContext,
|
|
132
137
|
FunctionTool,
|
|
133
138
|
GOOGLE_SEARCH,
|
|
@@ -164,6 +169,8 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
|
|
|
164
169
|
getFunctionCalls,
|
|
165
170
|
getFunctionResponses,
|
|
166
171
|
hasTrailingCodeExecutionResult,
|
|
172
|
+
isBaseAgent,
|
|
173
|
+
isBaseLlm,
|
|
167
174
|
isFinalResponse,
|
|
168
175
|
setLogLevel,
|
|
169
176
|
stringifyContent,
|