@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
|
@@ -36,15 +36,23 @@ var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")])
|
|
|
36
36
|
* Copyright 2025 Google LLC
|
|
37
37
|
* SPDX-License-Identifier: Apache-2.0
|
|
38
38
|
*/
|
|
39
|
+
import { cloneDeep } from "lodash";
|
|
39
40
|
import { z } from "zod";
|
|
41
|
+
import { BaseCodeExecutor } from "../code_executors/base_code_executor.js";
|
|
42
|
+
import { BuiltInCodeExecutor } from "../code_executors/built_in_code_executor.js";
|
|
43
|
+
import { buildCodeExecutionResultPart, buildExecutableCodePart, convertCodeExecutionParts, extractCodeAndTruncateContent } from "../code_executors/code_execution_utils.js";
|
|
44
|
+
import { CodeExecutorContext } from "../code_executors/code_executor_context.js";
|
|
40
45
|
import { createEvent, createNewEventId, getFunctionCalls, getFunctionResponses, isFinalResponse } from "../events/event.js";
|
|
41
|
-
import {
|
|
46
|
+
import { createEventActions } from "../events/event_actions.js";
|
|
47
|
+
import { isBaseLlm } from "../models/base_llm.js";
|
|
42
48
|
import { appendInstructions, setOutputSchema } from "../models/llm_request.js";
|
|
43
49
|
import { LLMRegistry } from "../models/registry.js";
|
|
50
|
+
import { State } from "../sessions/state.js";
|
|
44
51
|
import { BaseTool } from "../tools/base_tool.js";
|
|
45
52
|
import { FunctionTool } from "../tools/function_tool.js";
|
|
46
53
|
import { ToolConfirmation } from "../tools/tool_confirmation.js";
|
|
47
54
|
import { ToolContext } from "../tools/tool_context.js";
|
|
55
|
+
import { base64Decode } from "../utils/env_aware_utils.js";
|
|
48
56
|
import { logger } from "../utils/logger.js";
|
|
49
57
|
import { BaseAgent } from "./base_agent.js";
|
|
50
58
|
import { BaseLlmRequestProcessor } from "./base_llm_processor.js";
|
|
@@ -349,6 +357,306 @@ class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
349
357
|
}
|
|
350
358
|
}
|
|
351
359
|
const REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR = new RequestConfirmationLlmRequestProcessor();
|
|
360
|
+
class CodeExecutionRequestProcessor extends BaseLlmRequestProcessor {
|
|
361
|
+
runAsync(invocationContext, llmRequest) {
|
|
362
|
+
return __asyncGenerator(this, null, function* () {
|
|
363
|
+
if (!(invocationContext.agent instanceof LlmAgent)) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (!invocationContext.agent.codeExecutor) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
try {
|
|
370
|
+
for (var iter = __forAwait(runPreProcessor(invocationContext, llmRequest)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
371
|
+
const event = temp.value;
|
|
372
|
+
yield event;
|
|
373
|
+
}
|
|
374
|
+
} catch (temp) {
|
|
375
|
+
error = [temp];
|
|
376
|
+
} finally {
|
|
377
|
+
try {
|
|
378
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
379
|
+
} finally {
|
|
380
|
+
if (error)
|
|
381
|
+
throw error[0];
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (!(invocationContext.agent.codeExecutor instanceof BaseCodeExecutor)) {
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
for (const content of llmRequest.contents) {
|
|
388
|
+
const delimeters = invocationContext.agent.codeExecutor.codeBlockDelimiters.length ? invocationContext.agent.codeExecutor.codeBlockDelimiters[0] : ["", ""];
|
|
389
|
+
const codeExecutionParts = convertCodeExecutionParts(
|
|
390
|
+
content,
|
|
391
|
+
delimeters,
|
|
392
|
+
invocationContext.agent.codeExecutor.executionResultDelimiters
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const DATA_FILE_UTIL_MAP = {
|
|
399
|
+
"text/csv": {
|
|
400
|
+
extension: ".csv",
|
|
401
|
+
loaderCodeTemplate: "pd.read_csv('{filename}')"
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
const DATA_FILE_HELPER_LIB = "\nimport pandas as pd\n\ndef explore_df(df: pd.DataFrame) -> None:\n \"\"\"Prints some information about a pandas DataFrame.\"\"\"\n\n with pd.option_context(\n 'display.max_columns', None, 'display.expand_frame_repr', False\n ):\n # Print the column names to never encounter KeyError when selecting one.\n df_dtypes = df.dtypes\n\n # Obtain information about data types and missing values.\n df_nulls = (len(df) - df.isnull().sum()).apply(\n lambda x: f'{x} / {df.shape[0]} non-null'\n )\n\n # Explore unique total values in columns using `.unique()`.\n df_unique_count = df.apply(lambda x: len(x.unique()))\n\n # Explore unique values in columns using `.unique()`.\n df_unique = df.apply(lambda x: crop(str(list(x.unique()))))\n\n df_info = pd.concat(\n (\n df_dtypes.rename('Dtype'),\n df_nulls.rename('Non-Null Count'),\n df_unique_count.rename('Unique Values Count'),\n df_unique.rename('Unique Values'),\n ),\n axis=1,\n )\n df_info.index.name = 'Columns'\n print(f\"\"\"Total rows: {df.shape[0]}\nTotal columns: {df.shape[1]}\n\n{df_info}\"\"\")\n";
|
|
405
|
+
class CodeExecutionResponseProcessor {
|
|
406
|
+
/**
|
|
407
|
+
* Processes the LLM response asynchronously.
|
|
408
|
+
*
|
|
409
|
+
* @param invocationContext The invocation context
|
|
410
|
+
* @param llmResponse The LLM response to process
|
|
411
|
+
* @returns An async generator yielding events
|
|
412
|
+
*/
|
|
413
|
+
runAsync(invocationContext, llmResponse) {
|
|
414
|
+
return __asyncGenerator(this, null, function* () {
|
|
415
|
+
if (llmResponse.partial) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
try {
|
|
419
|
+
for (var iter = __forAwait(runPostProcessor(invocationContext, llmResponse)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
420
|
+
const event = temp.value;
|
|
421
|
+
yield event;
|
|
422
|
+
}
|
|
423
|
+
} catch (temp) {
|
|
424
|
+
error = [temp];
|
|
425
|
+
} finally {
|
|
426
|
+
try {
|
|
427
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
428
|
+
} finally {
|
|
429
|
+
if (error)
|
|
430
|
+
throw error[0];
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
const responseProcessor = new CodeExecutionResponseProcessor();
|
|
437
|
+
function runPreProcessor(invocationContext, llmRequest) {
|
|
438
|
+
return __asyncGenerator(this, null, function* () {
|
|
439
|
+
const agent = invocationContext.agent;
|
|
440
|
+
if (!(agent instanceof LlmAgent)) {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
const codeExecutor = agent.codeExecutor;
|
|
444
|
+
if (!codeExecutor || !(codeExecutor instanceof BaseCodeExecutor)) {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
if (codeExecutor instanceof BuiltInCodeExecutor) {
|
|
448
|
+
codeExecutor.processLlmRequest(llmRequest);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (!codeExecutor.optimizeDataFile) {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const codeExecutorContext = new CodeExecutorContext(new State(invocationContext.session.state));
|
|
455
|
+
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const allInputFiles = extractAndReplaceInlineFiles(codeExecutorContext, llmRequest);
|
|
459
|
+
const processedFileNames = new Set(codeExecutorContext.getProcessedFileNames());
|
|
460
|
+
const filesToProcess = allInputFiles.filter((f) => !processedFileNames.has(f.name));
|
|
461
|
+
for (const file of filesToProcess) {
|
|
462
|
+
const codeStr = getDataFilePreprocessingCode(file);
|
|
463
|
+
if (!codeStr) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const codeContent = {
|
|
467
|
+
role: "model",
|
|
468
|
+
parts: [
|
|
469
|
+
{ text: "Processing input file: `".concat(file.name, "`") },
|
|
470
|
+
buildExecutableCodePart(codeStr)
|
|
471
|
+
]
|
|
472
|
+
};
|
|
473
|
+
llmRequest.contents.push(cloneDeep(codeContent));
|
|
474
|
+
yield createEvent({
|
|
475
|
+
invocationId: invocationContext.invocationId,
|
|
476
|
+
author: agent.name,
|
|
477
|
+
branch: invocationContext.branch,
|
|
478
|
+
content: codeContent
|
|
479
|
+
});
|
|
480
|
+
const executionId = getOrSetExecutionId(invocationContext, codeExecutorContext);
|
|
481
|
+
const codeExecutionResult = yield new __await(codeExecutor.executeCode({
|
|
482
|
+
invocationContext,
|
|
483
|
+
codeExecutionInput: {
|
|
484
|
+
code: codeStr,
|
|
485
|
+
inputFiles: [file],
|
|
486
|
+
executionId
|
|
487
|
+
}
|
|
488
|
+
}));
|
|
489
|
+
codeExecutorContext.updateCodeExecutionResult({
|
|
490
|
+
invocationId: invocationContext.invocationId,
|
|
491
|
+
code: codeStr,
|
|
492
|
+
resultStdout: codeExecutionResult.stdout,
|
|
493
|
+
resultStderr: codeExecutionResult.stderr
|
|
494
|
+
});
|
|
495
|
+
codeExecutorContext.addProcessedFileNames([file.name]);
|
|
496
|
+
const executionResultEvent = yield new __await(postProcessCodeExecutionResult(
|
|
497
|
+
invocationContext,
|
|
498
|
+
codeExecutorContext,
|
|
499
|
+
codeExecutionResult
|
|
500
|
+
));
|
|
501
|
+
yield executionResultEvent;
|
|
502
|
+
llmRequest.contents.push(cloneDeep(executionResultEvent.content));
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
function runPostProcessor(invocationContext, llmResponse) {
|
|
507
|
+
return __asyncGenerator(this, null, function* () {
|
|
508
|
+
const agent = invocationContext.agent;
|
|
509
|
+
if (!(agent instanceof LlmAgent)) {
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const codeExecutor = agent.codeExecutor;
|
|
513
|
+
if (!codeExecutor || !(codeExecutor instanceof BaseCodeExecutor)) {
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
if (!llmResponse || !llmResponse.content) {
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
if (codeExecutor instanceof BuiltInCodeExecutor) {
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
const codeExecutorContext = new CodeExecutorContext(new State(invocationContext.session.state));
|
|
523
|
+
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
const responseContent = llmResponse.content;
|
|
527
|
+
const codeStr = extractCodeAndTruncateContent(
|
|
528
|
+
responseContent,
|
|
529
|
+
codeExecutor.codeBlockDelimiters
|
|
530
|
+
);
|
|
531
|
+
if (!codeStr) {
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
yield createEvent({
|
|
535
|
+
invocationId: invocationContext.invocationId,
|
|
536
|
+
author: agent.name,
|
|
537
|
+
branch: invocationContext.branch,
|
|
538
|
+
content: responseContent
|
|
539
|
+
});
|
|
540
|
+
const executionId = getOrSetExecutionId(invocationContext, codeExecutorContext);
|
|
541
|
+
const codeExecutionResult = yield new __await(codeExecutor.executeCode({
|
|
542
|
+
invocationContext,
|
|
543
|
+
codeExecutionInput: {
|
|
544
|
+
code: codeStr,
|
|
545
|
+
inputFiles: codeExecutorContext.getInputFiles(),
|
|
546
|
+
executionId
|
|
547
|
+
}
|
|
548
|
+
}));
|
|
549
|
+
codeExecutorContext.updateCodeExecutionResult({
|
|
550
|
+
invocationId: invocationContext.invocationId,
|
|
551
|
+
code: codeStr,
|
|
552
|
+
resultStdout: codeExecutionResult.stdout,
|
|
553
|
+
resultStderr: codeExecutionResult.stderr
|
|
554
|
+
});
|
|
555
|
+
yield yield new __await(postProcessCodeExecutionResult(
|
|
556
|
+
invocationContext,
|
|
557
|
+
codeExecutorContext,
|
|
558
|
+
codeExecutionResult
|
|
559
|
+
));
|
|
560
|
+
llmResponse.content = null;
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
564
|
+
var _a;
|
|
565
|
+
const allInputFiles = codeExecutorContext.getInputFiles();
|
|
566
|
+
const savedFileNames = new Set(allInputFiles.map((f) => f.name));
|
|
567
|
+
for (let i = 0; i < llmRequest.contents.length; i++) {
|
|
568
|
+
const content = llmRequest.contents[i];
|
|
569
|
+
if (content.role !== "user" || !content.parts) {
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
for (let j = 0; j < content.parts.length; j++) {
|
|
573
|
+
const part = content.parts[j];
|
|
574
|
+
const mimeType = (_a = part.inlineData) == null ? void 0 : _a.mimeType;
|
|
575
|
+
if (!mimeType || !part.inlineData || !DATA_FILE_UTIL_MAP[mimeType]) {
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
const fileName = "data_".concat(i + 1, "_").concat(j + 1).concat(DATA_FILE_UTIL_MAP[mimeType].extension);
|
|
579
|
+
part.text = "\nAvailable file: `".concat(fileName, "`\n");
|
|
580
|
+
const file = {
|
|
581
|
+
name: fileName,
|
|
582
|
+
content: base64Decode(part.inlineData.data),
|
|
583
|
+
mimeType
|
|
584
|
+
};
|
|
585
|
+
if (!savedFileNames.has(fileName)) {
|
|
586
|
+
codeExecutorContext.addInputFiles([file]);
|
|
587
|
+
allInputFiles.push(file);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return allInputFiles;
|
|
592
|
+
}
|
|
593
|
+
function getOrSetExecutionId(invocationContext, codeExecutorContext) {
|
|
594
|
+
var _a;
|
|
595
|
+
const agent = invocationContext.agent;
|
|
596
|
+
if (!(agent instanceof LlmAgent) || !((_a = agent.codeExecutor) == null ? void 0 : _a.stateful)) {
|
|
597
|
+
return void 0;
|
|
598
|
+
}
|
|
599
|
+
let executionId = codeExecutorContext.getExecutionId();
|
|
600
|
+
if (!executionId) {
|
|
601
|
+
executionId = invocationContext.session.id;
|
|
602
|
+
codeExecutorContext.setExecutionId(executionId);
|
|
603
|
+
}
|
|
604
|
+
return executionId;
|
|
605
|
+
}
|
|
606
|
+
async function postProcessCodeExecutionResult(invocationContext, codeExecutorContext, codeExecutionResult) {
|
|
607
|
+
if (!invocationContext.artifactService) {
|
|
608
|
+
throw new Error("Artifact service is not initialized.");
|
|
609
|
+
}
|
|
610
|
+
const resultContent = {
|
|
611
|
+
role: "model",
|
|
612
|
+
parts: [buildCodeExecutionResultPart(codeExecutionResult)]
|
|
613
|
+
};
|
|
614
|
+
const eventActions = createEventActions({ stateDelta: codeExecutorContext.getStateDelta() });
|
|
615
|
+
if (codeExecutionResult.stderr) {
|
|
616
|
+
codeExecutorContext.incrementErrorCount(invocationContext.invocationId);
|
|
617
|
+
} else {
|
|
618
|
+
codeExecutorContext.resetErrorCount(invocationContext.invocationId);
|
|
619
|
+
}
|
|
620
|
+
for (const outputFile of codeExecutionResult.outputFiles) {
|
|
621
|
+
const version = await invocationContext.artifactService.saveArtifact({
|
|
622
|
+
appName: invocationContext.appName || "",
|
|
623
|
+
userId: invocationContext.userId || "",
|
|
624
|
+
sessionId: invocationContext.session.id,
|
|
625
|
+
filename: outputFile.name,
|
|
626
|
+
artifact: {
|
|
627
|
+
inlineData: { data: outputFile.content, mimeType: outputFile.mimeType }
|
|
628
|
+
}
|
|
629
|
+
});
|
|
630
|
+
eventActions.artifactDelta[outputFile.name] = version;
|
|
631
|
+
}
|
|
632
|
+
return createEvent({
|
|
633
|
+
invocationId: invocationContext.invocationId,
|
|
634
|
+
author: invocationContext.agent.name,
|
|
635
|
+
branch: invocationContext.branch,
|
|
636
|
+
content: resultContent,
|
|
637
|
+
actions: eventActions
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
function getDataFilePreprocessingCode(file) {
|
|
641
|
+
function getNormalizedFileName(fileName) {
|
|
642
|
+
const [varName2] = fileName.split(".");
|
|
643
|
+
let normalizedName = varName2.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
644
|
+
if (/^\d/.test(normalizedName)) {
|
|
645
|
+
normalizedName = "_" + normalizedName;
|
|
646
|
+
}
|
|
647
|
+
return normalizedName;
|
|
648
|
+
}
|
|
649
|
+
if (!DATA_FILE_UTIL_MAP[file.mimeType]) {
|
|
650
|
+
return void 0;
|
|
651
|
+
}
|
|
652
|
+
const varName = getNormalizedFileName(file.name);
|
|
653
|
+
const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace(
|
|
654
|
+
"{filename}",
|
|
655
|
+
file.name
|
|
656
|
+
);
|
|
657
|
+
return "\n".concat(DATA_FILE_HELPER_LIB, "\n\n# Load the dataframe.\n").concat(varName, " = ").concat(loaderCode, "\n\n# Use `explore_df` to guide my analysis.\nexplore_df(").concat(varName, ")\n");
|
|
658
|
+
}
|
|
659
|
+
const CODE_EXECUTION_REQUEST_PROCESSOR = new CodeExecutionRequestProcessor();
|
|
352
660
|
class LlmAgent extends BaseAgent {
|
|
353
661
|
constructor(config) {
|
|
354
662
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
@@ -368,12 +676,14 @@ class LlmAgent extends BaseAgent {
|
|
|
368
676
|
this.afterModelCallback = config.afterModelCallback;
|
|
369
677
|
this.beforeToolCallback = config.beforeToolCallback;
|
|
370
678
|
this.afterToolCallback = config.afterToolCallback;
|
|
679
|
+
this.codeExecutor = config.codeExecutor;
|
|
371
680
|
this.requestProcessors = (_g = config.requestProcessors) != null ? _g : [
|
|
372
681
|
BASIC_LLM_REQUEST_PROCESSOR,
|
|
373
682
|
IDENTITY_LLM_REQUEST_PROCESSOR,
|
|
374
683
|
INSTRUCTIONS_LLM_REQUEST_PROCESSOR,
|
|
375
684
|
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
376
|
-
CONTENT_REQUEST_PROCESSOR
|
|
685
|
+
CONTENT_REQUEST_PROCESSOR,
|
|
686
|
+
CODE_EXECUTION_REQUEST_PROCESSOR
|
|
377
687
|
];
|
|
378
688
|
this.responseProcessors = (_h = config.responseProcessors) != null ? _h : [];
|
|
379
689
|
const agentTransferDisabled = this.disallowTransferToParent && this.disallowTransferToPeers && !((_i = this.subAgents) == null ? void 0 : _i.length);
|
|
@@ -423,7 +733,7 @@ class LlmAgent extends BaseAgent {
|
|
|
423
733
|
* When not set, the agent will inherit the model from its ancestor.
|
|
424
734
|
*/
|
|
425
735
|
get canonicalModel() {
|
|
426
|
-
if (this.model
|
|
736
|
+
if (isBaseLlm(this.model)) {
|
|
427
737
|
return this.model;
|
|
428
738
|
}
|
|
429
739
|
if (typeof this.model === "string" && this.model) {
|
|
@@ -975,5 +1285,6 @@ class LlmAgent extends BaseAgent {
|
|
|
975
1285
|
}
|
|
976
1286
|
export {
|
|
977
1287
|
LlmAgent,
|
|
978
|
-
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR
|
|
1288
|
+
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
1289
|
+
responseProcessor
|
|
979
1290
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
class CredentialExchangerRegistry {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.exchangers = {};
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Register an exchanger instance for a credential type.
|
|
12
|
+
* @param credentialType - The credential type to register for.
|
|
13
|
+
* @param exchangerInstance - The exchanger instance to register.
|
|
14
|
+
*/
|
|
15
|
+
register(credentialType, exchangerInstance) {
|
|
16
|
+
this.exchangers[credentialType] = exchangerInstance;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Get the exchanger instance for a credential type.
|
|
20
|
+
* @param credentialType - The credential type to get exchanger for.
|
|
21
|
+
* @returns The exchanger instance if registered, undefined otherwise.
|
|
22
|
+
*/
|
|
23
|
+
getExchanger(credentialType) {
|
|
24
|
+
return this.exchangers[credentialType];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
CredentialExchangerRegistry
|
|
29
|
+
};
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
import { Language, Outcome } from "@google/genai";
|
|
7
|
-
import {
|
|
7
|
+
import { cloneDeep } from "lodash";
|
|
8
8
|
import { base64Encode, isBase64Encoded } from "../utils/env_aware_utils.js";
|
|
9
9
|
function getEncodedFileContent(data) {
|
|
10
10
|
return isBase64Encoded(data) ? data : base64Encode(data);
|
|
@@ -25,7 +25,7 @@ function extractCodeAndTruncateContent(content, codeBlockDelimiters) {
|
|
|
25
25
|
if (!textParts.length) {
|
|
26
26
|
return "";
|
|
27
27
|
}
|
|
28
|
-
const firstTextPart =
|
|
28
|
+
const firstTextPart = cloneDeep(textParts[0]);
|
|
29
29
|
const responseText = textParts.map((part) => part.text).join("\n");
|
|
30
30
|
const leadingDelimiterPattern = codeBlockDelimiters.map((d) => d[0]).join("|");
|
|
31
31
|
const trailingDelimiterPattern = codeBlockDelimiters.map((d) => d[1]).join("|");
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { cloneDeep } from "lodash";
|
|
7
7
|
const CONTEXT_KEY = "_code_execution_context";
|
|
8
8
|
const SESSION_ID_KEY = "execution_session_id";
|
|
9
9
|
const PROCESSED_FILE_NAMES_KEY = "processed_input_files";
|
|
@@ -23,7 +23,7 @@ class CodeExecutorContext {
|
|
|
23
23
|
*/
|
|
24
24
|
getStateDelta() {
|
|
25
25
|
return {
|
|
26
|
-
[CONTEXT_KEY]:
|
|
26
|
+
[CONTEXT_KEY]: cloneDeep(this.context)
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
/**
|
package/dist/web/common.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import { BaseAgent } from "./agents/base_agent.js";
|
|
6
|
+
import { BaseAgent, isBaseAgent } from "./agents/base_agent.js";
|
|
7
7
|
import { CallbackContext } from "./agents/callback_context.js";
|
|
8
8
|
import { functionsExportedForTestingOnly } from "./agents/functions.js";
|
|
9
9
|
import { InvocationContext } from "./agents/invocation_context.js";
|
|
@@ -14,10 +14,11 @@ import { ParallelAgent } from "./agents/parallel_agent.js";
|
|
|
14
14
|
import { StreamingMode } from "./agents/run_config.js";
|
|
15
15
|
import { SequentialAgent } from "./agents/sequential_agent.js";
|
|
16
16
|
import { InMemoryArtifactService } from "./artifacts/in_memory_artifact_service.js";
|
|
17
|
+
import { BuiltInCodeExecutor } from "./code_executors/built_in_code_executor.js";
|
|
17
18
|
import { createEvent, getFunctionCalls, getFunctionResponses, hasTrailingCodeExecutionResult, isFinalResponse, stringifyContent } from "./events/event.js";
|
|
18
19
|
import { createEventActions } from "./events/event_actions.js";
|
|
19
20
|
import { InMemoryMemoryService } from "./memory/in_memory_memory_service.js";
|
|
20
|
-
import { BaseLlm } from "./models/base_llm.js";
|
|
21
|
+
import { BaseLlm, isBaseLlm } from "./models/base_llm.js";
|
|
21
22
|
import { Gemini } from "./models/google_llm.js";
|
|
22
23
|
import { LLMRegistry } from "./models/registry.js";
|
|
23
24
|
import { BasePlugin } from "./plugins/base_plugin.js";
|
|
@@ -51,6 +52,7 @@ export {
|
|
|
51
52
|
BasePlugin,
|
|
52
53
|
BaseTool,
|
|
53
54
|
BaseToolset,
|
|
55
|
+
BuiltInCodeExecutor,
|
|
54
56
|
CallbackContext,
|
|
55
57
|
FunctionTool,
|
|
56
58
|
GOOGLE_SEARCH,
|
|
@@ -87,6 +89,8 @@ export {
|
|
|
87
89
|
getFunctionCalls,
|
|
88
90
|
getFunctionResponses,
|
|
89
91
|
hasTrailingCodeExecutionResult,
|
|
92
|
+
isBaseAgent,
|
|
93
|
+
isBaseLlm,
|
|
90
94
|
isFinalResponse,
|
|
91
95
|
setLogLevel,
|
|
92
96
|
stringifyContent,
|