@google/adk 0.2.3 → 0.2.5
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 +9 -3
- package/dist/cjs/agents/content_processor_utils.js +2 -12
- package/dist/cjs/agents/functions.js +3 -13
- package/dist/cjs/agents/invocation_context.js +3 -1
- package/dist/cjs/agents/llm_agent.js +132 -88
- package/dist/cjs/agents/loop_agent.js +16 -5
- package/dist/cjs/agents/parallel_agent.js +17 -3
- package/dist/cjs/agents/readonly_context.js +4 -1
- package/dist/cjs/agents/sequential_agent.js +34 -12
- package/dist/cjs/code_executors/base_code_executor.js +12 -2
- package/dist/cjs/code_executors/built_in_code_executor.js +15 -3
- package/dist/cjs/code_executors/code_execution_utils.js +2 -12
- package/dist/cjs/code_executors/code_executor_context.js +2 -12
- package/dist/cjs/common.js +41 -0
- package/dist/cjs/examples/base_example_provider.js +18 -2
- package/dist/cjs/examples/example_util.js +1 -1
- package/dist/cjs/index.js +11 -11
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/plugins/base_plugin.js +59 -49
- package/dist/cjs/plugins/security_plugin.js +4 -1
- package/dist/cjs/runner/runner.js +38 -27
- package/dist/cjs/sessions/in_memory_session_service.js +3 -13
- package/dist/cjs/tools/agent_tool.js +18 -9
- package/dist/cjs/tools/base_tool.js +16 -6
- package/dist/cjs/tools/function_tool.js +14 -5
- package/dist/cjs/tools/google_search_tool.js +8 -3
- package/dist/cjs/tools/tool_context.js +4 -9
- package/dist/cjs/utils/gemini_schema_util.js +1 -0
- package/dist/cjs/version.js +1 -1
- package/dist/esm/agents/base_agent.js +9 -3
- package/dist/esm/agents/content_processor_utils.js +1 -1
- package/dist/esm/agents/functions.js +1 -1
- package/dist/esm/agents/invocation_context.js +3 -1
- package/dist/esm/agents/llm_agent.js +166 -84
- package/dist/esm/agents/loop_agent.js +14 -4
- package/dist/esm/agents/parallel_agent.js +15 -2
- package/dist/esm/agents/readonly_context.js +4 -1
- package/dist/esm/agents/sequential_agent.js +33 -12
- package/dist/esm/code_executors/base_code_executor.js +10 -1
- package/dist/esm/code_executors/built_in_code_executor.js +13 -2
- package/dist/esm/code_executors/code_execution_utils.js +1 -1
- package/dist/esm/code_executors/code_executor_context.js +1 -1
- package/dist/esm/common.js +53 -10
- package/dist/esm/examples/base_example_provider.js +16 -1
- package/dist/esm/examples/example_util.js +4 -2
- package/dist/esm/index.js +11 -11
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/plugins/base_plugin.js +59 -49
- package/dist/esm/plugins/security_plugin.js +4 -1
- package/dist/esm/runner/runner.js +47 -30
- package/dist/esm/sessions/in_memory_session_service.js +1 -1
- package/dist/esm/tools/agent_tool.js +17 -9
- package/dist/esm/tools/base_tool.js +14 -5
- package/dist/esm/tools/function_tool.js +13 -7
- package/dist/esm/tools/google_search_tool.js +9 -3
- package/dist/esm/tools/tool_context.js +4 -9
- package/dist/esm/utils/gemini_schema_util.js +1 -0
- package/dist/esm/version.js +1 -1
- package/dist/types/agents/base_agent.d.ts +14 -3
- package/dist/types/agents/invocation_context.d.ts +4 -2
- package/dist/types/agents/llm_agent.d.ts +42 -30
- package/dist/types/agents/loop_agent.d.ts +16 -0
- package/dist/types/agents/parallel_agent.d.ts +16 -0
- package/dist/types/agents/sequential_agent.d.ts +17 -1
- package/dist/types/auth/auth_schemes.d.ts +5 -2
- package/dist/types/code_executors/base_code_executor.d.ts +14 -0
- package/dist/types/code_executors/built_in_code_executor.d.ts +19 -0
- package/dist/types/common.d.ts +38 -14
- package/dist/types/examples/base_example_provider.d.ts +16 -0
- package/dist/types/plugins/base_plugin.d.ts +50 -40
- package/dist/types/runner/runner.d.ts +17 -9
- package/dist/types/tools/agent_tool.d.ts +14 -0
- package/dist/types/tools/base_tool.d.ts +14 -0
- package/dist/types/tools/function_tool.d.ts +14 -1
- package/dist/types/tools/google_search_tool.d.ts +3 -4
- package/dist/types/tools/tool_context.d.ts +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/web/agents/base_agent.js +9 -3
- package/dist/web/agents/content_processor_utils.js +1 -1
- package/dist/web/agents/functions.js +1 -1
- package/dist/web/agents/invocation_context.js +3 -1
- package/dist/web/agents/llm_agent.js +166 -84
- package/dist/web/agents/loop_agent.js +14 -4
- package/dist/web/agents/parallel_agent.js +15 -2
- package/dist/web/agents/readonly_context.js +4 -1
- package/dist/web/agents/sequential_agent.js +33 -12
- package/dist/web/code_executors/base_code_executor.js +10 -1
- package/dist/web/code_executors/built_in_code_executor.js +13 -2
- package/dist/web/code_executors/code_execution_utils.js +1 -1
- package/dist/web/code_executors/code_executor_context.js +1 -1
- package/dist/web/common.js +53 -10
- package/dist/web/examples/base_example_provider.js +16 -1
- package/dist/web/examples/example_util.js +4 -2
- package/dist/web/index.js +1 -1
- package/dist/web/index.js.map +4 -4
- package/dist/web/plugins/base_plugin.js +59 -49
- package/dist/web/plugins/security_plugin.js +4 -1
- package/dist/web/runner/runner.js +48 -31
- package/dist/web/sessions/in_memory_session_service.js +1 -1
- package/dist/web/tools/agent_tool.js +17 -9
- package/dist/web/tools/base_tool.js +14 -5
- package/dist/web/tools/function_tool.js +13 -7
- package/dist/web/tools/google_search_tool.js +9 -3
- package/dist/web/tools/tool_context.js +4 -9
- package/dist/web/utils/gemini_schema_util.js +1 -0
- package/dist/web/version.js +1 -1
- package/package.json +3 -3
|
@@ -40,6 +40,7 @@ const MCPToolSchema = import_zod.z.object({
|
|
|
40
40
|
required: import_zod.z.string().array().optional()
|
|
41
41
|
});
|
|
42
42
|
function toGeminiType(mcpType) {
|
|
43
|
+
if (!mcpType) return import_genai.Type.TYPE_UNSPECIFIED;
|
|
43
44
|
switch (mcpType.toLowerCase()) {
|
|
44
45
|
case "text":
|
|
45
46
|
case "string":
|
package/dist/cjs/version.js
CHANGED
|
@@ -32,7 +32,7 @@ module.exports = __toCommonJS(version_exports);
|
|
|
32
32
|
* Copyright 2025 Google LLC
|
|
33
33
|
* SPDX-License-Identifier: Apache-2.0
|
|
34
34
|
*/
|
|
35
|
-
const version = "0.2.
|
|
35
|
+
const version = "0.2.5";
|
|
36
36
|
// Annotate the CommonJS export names for ESM import in node:
|
|
37
37
|
0 && (module.exports = {
|
|
38
38
|
version
|
|
@@ -24,7 +24,9 @@ class BaseAgent {
|
|
|
24
24
|
this.parentAgent = config.parentAgent;
|
|
25
25
|
this.subAgents = config.subAgents || [];
|
|
26
26
|
this.rootAgent = getRootAgent(this);
|
|
27
|
-
this.beforeAgentCallback = getCannonicalCallback(
|
|
27
|
+
this.beforeAgentCallback = getCannonicalCallback(
|
|
28
|
+
config.beforeAgentCallback
|
|
29
|
+
);
|
|
28
30
|
this.afterAgentCallback = getCannonicalCallback(config.afterAgentCallback);
|
|
29
31
|
this.setParentAgentForSubAgents();
|
|
30
32
|
}
|
|
@@ -186,7 +188,9 @@ class BaseAgent {
|
|
|
186
188
|
setParentAgentForSubAgents() {
|
|
187
189
|
for (const subAgent of this.subAgents) {
|
|
188
190
|
if (subAgent.parentAgent) {
|
|
189
|
-
throw new Error(
|
|
191
|
+
throw new Error(
|
|
192
|
+
`Agent "${subAgent.name}" already has a parent agent, current parent: "${subAgent.parentAgent.name}", trying to add: "${this.name}"`
|
|
193
|
+
);
|
|
190
194
|
}
|
|
191
195
|
subAgent.parentAgent = this;
|
|
192
196
|
}
|
|
@@ -194,7 +198,9 @@ class BaseAgent {
|
|
|
194
198
|
}
|
|
195
199
|
function validateAgentName(name) {
|
|
196
200
|
if (!isIdentifier(name)) {
|
|
197
|
-
throw new Error(
|
|
201
|
+
throw new Error(
|
|
202
|
+
`Found invalid agent name: "${name}". Agent name must be a valid identifier. It should start with a letter (a-z, A-Z) or an underscore (_), and can only contain letters, digits (0-9), and underscores.`
|
|
203
|
+
);
|
|
198
204
|
}
|
|
199
205
|
if (name === "user") {
|
|
200
206
|
throw new Error(
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import cloneDeep from "lodash-es
|
|
6
|
+
import { cloneDeep } from "lodash-es";
|
|
7
7
|
import { createEvent, getFunctionCalls, getFunctionResponses } from "../events/event.js";
|
|
8
8
|
import { removeClientFunctionCallId, REQUEST_CONFIRMATION_FUNCTION_CALL_NAME, REQUEST_EUC_FUNCTION_CALL_NAME } from "./functions.js";
|
|
9
9
|
function getContents(events, agentName, currentBranch) {
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
import { createUserContent } from "@google/genai";
|
|
7
|
-
import isEmpty from "lodash-es
|
|
7
|
+
import { isEmpty } from "lodash-es";
|
|
8
8
|
import { createEvent, getFunctionCalls } from "../events/event.js";
|
|
9
9
|
import { mergeEventActions } from "../events/event_actions.js";
|
|
10
10
|
import { ToolContext } from "../tools/tool_context.js";
|
|
@@ -17,7 +17,9 @@ class InvocationCostManager {
|
|
|
17
17
|
incrementAndEnforceLlmCallsLimit(runConfig) {
|
|
18
18
|
this.numberOfLlmCalls++;
|
|
19
19
|
if (runConfig && runConfig.maxLlmCalls > 0 && this.numberOfLlmCalls > runConfig.maxLlmCalls) {
|
|
20
|
-
throw new Error(
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Max number of llm calls limit of ${runConfig.maxLlmCalls} exceeded`
|
|
22
|
+
);
|
|
21
23
|
}
|
|
22
24
|
}
|
|
23
25
|
}
|
|
@@ -3,16 +3,33 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
var _a, _b;
|
|
7
|
+
import { cloneDeep } from "lodash-es";
|
|
7
8
|
import { z } from "zod";
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
import {
|
|
10
|
+
isBaseCodeExecutor
|
|
11
|
+
} from "../code_executors/base_code_executor.js";
|
|
12
|
+
import { isBuiltInCodeExecutor } from "../code_executors/built_in_code_executor.js";
|
|
13
|
+
import {
|
|
14
|
+
buildCodeExecutionResultPart,
|
|
15
|
+
buildExecutableCodePart,
|
|
16
|
+
convertCodeExecutionParts,
|
|
17
|
+
extractCodeAndTruncateContent
|
|
18
|
+
} from "../code_executors/code_execution_utils.js";
|
|
11
19
|
import { CodeExecutorContext } from "../code_executors/code_executor_context.js";
|
|
12
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
createEvent,
|
|
22
|
+
createNewEventId,
|
|
23
|
+
getFunctionCalls,
|
|
24
|
+
getFunctionResponses,
|
|
25
|
+
isFinalResponse
|
|
26
|
+
} from "../events/event.js";
|
|
13
27
|
import { createEventActions } from "../events/event_actions.js";
|
|
14
28
|
import { isBaseLlm } from "../models/base_llm.js";
|
|
15
|
-
import {
|
|
29
|
+
import {
|
|
30
|
+
appendInstructions,
|
|
31
|
+
setOutputSchema
|
|
32
|
+
} from "../models/llm_request.js";
|
|
16
33
|
import { LLMRegistry } from "../models/registry.js";
|
|
17
34
|
import { State } from "../sessions/state.js";
|
|
18
35
|
import { BaseTool } from "../tools/base_tool.js";
|
|
@@ -22,10 +39,23 @@ import { ToolContext } from "../tools/tool_context.js";
|
|
|
22
39
|
import { base64Decode } from "../utils/env_aware_utils.js";
|
|
23
40
|
import { logger } from "../utils/logger.js";
|
|
24
41
|
import { BaseAgent } from "./base_agent.js";
|
|
25
|
-
import {
|
|
42
|
+
import {
|
|
43
|
+
BaseLlmRequestProcessor
|
|
44
|
+
} from "./base_llm_processor.js";
|
|
26
45
|
import { CallbackContext } from "./callback_context.js";
|
|
27
|
-
import {
|
|
28
|
-
|
|
46
|
+
import {
|
|
47
|
+
getContents,
|
|
48
|
+
getCurrentTurnContents
|
|
49
|
+
} from "./content_processor_utils.js";
|
|
50
|
+
import {
|
|
51
|
+
generateAuthEvent,
|
|
52
|
+
generateRequestConfirmationEvent,
|
|
53
|
+
getLongRunningFunctionCalls,
|
|
54
|
+
handleFunctionCallList,
|
|
55
|
+
handleFunctionCallsAsync,
|
|
56
|
+
populateClientFunctionCallId,
|
|
57
|
+
REQUEST_CONFIRMATION_FUNCTION_CALL_NAME
|
|
58
|
+
} from "./functions.js";
|
|
29
59
|
import { injectSessionState } from "./instructions.js";
|
|
30
60
|
import { ReadonlyContext } from "./readonly_context.js";
|
|
31
61
|
import { StreamingMode } from "./run_config.js";
|
|
@@ -38,13 +68,13 @@ async function convertToolUnionToTools(toolUnion, context) {
|
|
|
38
68
|
}
|
|
39
69
|
class BasicLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
40
70
|
async *runAsync(invocationContext, llmRequest) {
|
|
41
|
-
var
|
|
71
|
+
var _a2;
|
|
42
72
|
const agent = invocationContext.agent;
|
|
43
|
-
if (!(agent
|
|
73
|
+
if (!isLlmAgent(agent)) {
|
|
44
74
|
return;
|
|
45
75
|
}
|
|
46
76
|
llmRequest.model = agent.canonicalModel.model;
|
|
47
|
-
llmRequest.config = { ...(
|
|
77
|
+
llmRequest.config = { ...(_a2 = agent.generateContentConfig) != null ? _a2 : {} };
|
|
48
78
|
if (agent.outputSchema) {
|
|
49
79
|
setOutputSchema(llmRequest, agent.outputSchema);
|
|
50
80
|
}
|
|
@@ -81,7 +111,7 @@ class InstructionsLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
81
111
|
return;
|
|
82
112
|
}
|
|
83
113
|
const rootAgent = agent.rootAgent;
|
|
84
|
-
if (rootAgent
|
|
114
|
+
if (isLlmAgent(rootAgent) && rootAgent.globalInstruction) {
|
|
85
115
|
const { instruction, requireStateInjection } = await rootAgent.canonicalGlobalInstruction(
|
|
86
116
|
new ReadonlyContext(invocationContext)
|
|
87
117
|
);
|
|
@@ -113,7 +143,7 @@ const INSTRUCTIONS_LLM_REQUEST_PROCESSOR = new InstructionsLlmRequestProcessor()
|
|
|
113
143
|
class ContentRequestProcessor {
|
|
114
144
|
async *runAsync(invocationContext, llmRequest) {
|
|
115
145
|
const agent = invocationContext.agent;
|
|
116
|
-
if (!agent || !(agent
|
|
146
|
+
if (!agent || !isLlmAgent(agent)) {
|
|
117
147
|
return;
|
|
118
148
|
}
|
|
119
149
|
if (agent.includeContents === "default") {
|
|
@@ -201,7 +231,7 @@ to your parent agent.
|
|
|
201
231
|
getTransferTargets(agent) {
|
|
202
232
|
const targets = [];
|
|
203
233
|
targets.push(...agent.subAgents);
|
|
204
|
-
if (!agent.parentAgent || !(agent.parentAgent
|
|
234
|
+
if (!agent.parentAgent || !isLlmAgent(agent.parentAgent)) {
|
|
205
235
|
return targets;
|
|
206
236
|
}
|
|
207
237
|
if (!agent.disallowTransferToParent) {
|
|
@@ -222,7 +252,7 @@ class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
222
252
|
/** Handles tool confirmation information to build the LLM request. */
|
|
223
253
|
async *runAsync(invocationContext, llmRequest) {
|
|
224
254
|
const agent = invocationContext.agent;
|
|
225
|
-
if (!(agent
|
|
255
|
+
if (!isLlmAgent(agent)) {
|
|
226
256
|
return;
|
|
227
257
|
}
|
|
228
258
|
const events = invocationContext.session.events;
|
|
@@ -248,7 +278,9 @@ class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
248
278
|
foundConfirmation = true;
|
|
249
279
|
let toolConfirmation = null;
|
|
250
280
|
if (functionResponse.response && Object.keys(functionResponse.response).length === 1 && "response" in functionResponse.response) {
|
|
251
|
-
toolConfirmation = JSON.parse(
|
|
281
|
+
toolConfirmation = JSON.parse(
|
|
282
|
+
functionResponse.response["response"]
|
|
283
|
+
);
|
|
252
284
|
} else if (functionResponse.response) {
|
|
253
285
|
toolConfirmation = new ToolConfirmation({
|
|
254
286
|
hint: functionResponse.response["hint"],
|
|
@@ -312,8 +344,12 @@ class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
312
344
|
if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
|
|
313
345
|
continue;
|
|
314
346
|
}
|
|
315
|
-
const toolsList = await agent.canonicalTools(
|
|
316
|
-
|
|
347
|
+
const toolsList = await agent.canonicalTools(
|
|
348
|
+
new ReadonlyContext(invocationContext)
|
|
349
|
+
);
|
|
350
|
+
const toolsDict = Object.fromEntries(
|
|
351
|
+
toolsList.map((tool) => [tool.name, tool])
|
|
352
|
+
);
|
|
317
353
|
const functionResponseEvent = await handleFunctionCallList({
|
|
318
354
|
invocationContext,
|
|
319
355
|
functionCalls: Object.values(toolsToResumeWithArgs),
|
|
@@ -342,7 +378,7 @@ class CodeExecutionRequestProcessor extends BaseLlmRequestProcessor {
|
|
|
342
378
|
for await (const event of runPreProcessor(invocationContext, llmRequest)) {
|
|
343
379
|
yield event;
|
|
344
380
|
}
|
|
345
|
-
if (!(invocationContext.agent.codeExecutor
|
|
381
|
+
if (!isBaseCodeExecutor(invocationContext.agent.codeExecutor)) {
|
|
346
382
|
return;
|
|
347
383
|
}
|
|
348
384
|
for (const content of llmRequest.contents) {
|
|
@@ -411,7 +447,10 @@ class CodeExecutionResponseProcessor {
|
|
|
411
447
|
if (llmResponse.partial) {
|
|
412
448
|
return;
|
|
413
449
|
}
|
|
414
|
-
for await (const event of runPostProcessor(
|
|
450
|
+
for await (const event of runPostProcessor(
|
|
451
|
+
invocationContext,
|
|
452
|
+
llmResponse
|
|
453
|
+
)) {
|
|
415
454
|
yield event;
|
|
416
455
|
}
|
|
417
456
|
}
|
|
@@ -419,27 +458,36 @@ class CodeExecutionResponseProcessor {
|
|
|
419
458
|
const responseProcessor = new CodeExecutionResponseProcessor();
|
|
420
459
|
async function* runPreProcessor(invocationContext, llmRequest) {
|
|
421
460
|
const agent = invocationContext.agent;
|
|
422
|
-
if (!(agent
|
|
461
|
+
if (!isLlmAgent(agent)) {
|
|
423
462
|
return;
|
|
424
463
|
}
|
|
425
464
|
const codeExecutor = agent.codeExecutor;
|
|
426
|
-
if (!codeExecutor || !(codeExecutor
|
|
465
|
+
if (!codeExecutor || !isBaseCodeExecutor(codeExecutor)) {
|
|
427
466
|
return;
|
|
428
467
|
}
|
|
429
|
-
if (codeExecutor
|
|
468
|
+
if (isBuiltInCodeExecutor(codeExecutor)) {
|
|
430
469
|
codeExecutor.processLlmRequest(llmRequest);
|
|
431
470
|
return;
|
|
432
471
|
}
|
|
433
472
|
if (!codeExecutor.optimizeDataFile) {
|
|
434
473
|
return;
|
|
435
474
|
}
|
|
436
|
-
const codeExecutorContext = new CodeExecutorContext(
|
|
475
|
+
const codeExecutorContext = new CodeExecutorContext(
|
|
476
|
+
new State(invocationContext.session.state)
|
|
477
|
+
);
|
|
437
478
|
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
438
479
|
return;
|
|
439
480
|
}
|
|
440
|
-
const allInputFiles = extractAndReplaceInlineFiles(
|
|
441
|
-
|
|
442
|
-
|
|
481
|
+
const allInputFiles = extractAndReplaceInlineFiles(
|
|
482
|
+
codeExecutorContext,
|
|
483
|
+
llmRequest
|
|
484
|
+
);
|
|
485
|
+
const processedFileNames = new Set(
|
|
486
|
+
codeExecutorContext.getProcessedFileNames()
|
|
487
|
+
);
|
|
488
|
+
const filesToProcess = allInputFiles.filter(
|
|
489
|
+
(f) => !processedFileNames.has(f.name)
|
|
490
|
+
);
|
|
443
491
|
for (const file of filesToProcess) {
|
|
444
492
|
const codeStr = getDataFilePreprocessingCode(file);
|
|
445
493
|
if (!codeStr) {
|
|
@@ -459,7 +507,10 @@ async function* runPreProcessor(invocationContext, llmRequest) {
|
|
|
459
507
|
branch: invocationContext.branch,
|
|
460
508
|
content: codeContent
|
|
461
509
|
});
|
|
462
|
-
const executionId = getOrSetExecutionId(
|
|
510
|
+
const executionId = getOrSetExecutionId(
|
|
511
|
+
invocationContext,
|
|
512
|
+
codeExecutorContext
|
|
513
|
+
);
|
|
463
514
|
const codeExecutionResult = await codeExecutor.executeCode({
|
|
464
515
|
invocationContext,
|
|
465
516
|
codeExecutionInput: {
|
|
@@ -486,20 +537,22 @@ async function* runPreProcessor(invocationContext, llmRequest) {
|
|
|
486
537
|
}
|
|
487
538
|
async function* runPostProcessor(invocationContext, llmResponse) {
|
|
488
539
|
const agent = invocationContext.agent;
|
|
489
|
-
if (!(agent
|
|
540
|
+
if (!isLlmAgent(agent)) {
|
|
490
541
|
return;
|
|
491
542
|
}
|
|
492
543
|
const codeExecutor = agent.codeExecutor;
|
|
493
|
-
if (!codeExecutor || !(codeExecutor
|
|
544
|
+
if (!codeExecutor || !isBaseCodeExecutor(codeExecutor)) {
|
|
494
545
|
return;
|
|
495
546
|
}
|
|
496
547
|
if (!llmResponse || !llmResponse.content) {
|
|
497
548
|
return;
|
|
498
549
|
}
|
|
499
|
-
if (codeExecutor
|
|
550
|
+
if (isBuiltInCodeExecutor(codeExecutor)) {
|
|
500
551
|
return;
|
|
501
552
|
}
|
|
502
|
-
const codeExecutorContext = new CodeExecutorContext(
|
|
553
|
+
const codeExecutorContext = new CodeExecutorContext(
|
|
554
|
+
new State(invocationContext.session.state)
|
|
555
|
+
);
|
|
503
556
|
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
504
557
|
return;
|
|
505
558
|
}
|
|
@@ -517,7 +570,10 @@ async function* runPostProcessor(invocationContext, llmResponse) {
|
|
|
517
570
|
branch: invocationContext.branch,
|
|
518
571
|
content: responseContent
|
|
519
572
|
});
|
|
520
|
-
const executionId = getOrSetExecutionId(
|
|
573
|
+
const executionId = getOrSetExecutionId(
|
|
574
|
+
invocationContext,
|
|
575
|
+
codeExecutorContext
|
|
576
|
+
);
|
|
521
577
|
const codeExecutionResult = await codeExecutor.executeCode({
|
|
522
578
|
invocationContext,
|
|
523
579
|
codeExecutionInput: {
|
|
@@ -540,7 +596,7 @@ async function* runPostProcessor(invocationContext, llmResponse) {
|
|
|
540
596
|
llmResponse.content = null;
|
|
541
597
|
}
|
|
542
598
|
function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
543
|
-
var
|
|
599
|
+
var _a2;
|
|
544
600
|
const allInputFiles = codeExecutorContext.getInputFiles();
|
|
545
601
|
const savedFileNames = new Set(allInputFiles.map((f) => f.name));
|
|
546
602
|
for (let i = 0; i < llmRequest.contents.length; i++) {
|
|
@@ -550,7 +606,7 @@ function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
|
550
606
|
}
|
|
551
607
|
for (let j = 0; j < content.parts.length; j++) {
|
|
552
608
|
const part = content.parts[j];
|
|
553
|
-
const mimeType = (
|
|
609
|
+
const mimeType = (_a2 = part.inlineData) == null ? void 0 : _a2.mimeType;
|
|
554
610
|
if (!mimeType || !part.inlineData || !DATA_FILE_UTIL_MAP[mimeType]) {
|
|
555
611
|
continue;
|
|
556
612
|
}
|
|
@@ -572,9 +628,9 @@ Available file: \`${fileName}\`
|
|
|
572
628
|
return allInputFiles;
|
|
573
629
|
}
|
|
574
630
|
function getOrSetExecutionId(invocationContext, codeExecutorContext) {
|
|
575
|
-
var
|
|
631
|
+
var _a2;
|
|
576
632
|
const agent = invocationContext.agent;
|
|
577
|
-
if (!(agent
|
|
633
|
+
if (!isLlmAgent(agent) || !((_a2 = agent.codeExecutor) == null ? void 0 : _a2.stateful)) {
|
|
578
634
|
return void 0;
|
|
579
635
|
}
|
|
580
636
|
let executionId = codeExecutorContext.getExecutionId();
|
|
@@ -592,7 +648,9 @@ async function postProcessCodeExecutionResult(invocationContext, codeExecutorCon
|
|
|
592
648
|
role: "model",
|
|
593
649
|
parts: [buildCodeExecutionResultPart(codeExecutionResult)]
|
|
594
650
|
};
|
|
595
|
-
const eventActions = createEventActions({
|
|
651
|
+
const eventActions = createEventActions({
|
|
652
|
+
stateDelta: codeExecutorContext.getStateDelta()
|
|
653
|
+
});
|
|
596
654
|
if (codeExecutionResult.stderr) {
|
|
597
655
|
codeExecutorContext.incrementErrorCount(invocationContext.invocationId);
|
|
598
656
|
} else {
|
|
@@ -631,10 +689,7 @@ function getDataFilePreprocessingCode(file) {
|
|
|
631
689
|
return void 0;
|
|
632
690
|
}
|
|
633
691
|
const varName = getNormalizedFileName(file.name);
|
|
634
|
-
const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace(
|
|
635
|
-
"{filename}",
|
|
636
|
-
file.name
|
|
637
|
-
);
|
|
692
|
+
const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace("{filename}", file.name);
|
|
638
693
|
return `
|
|
639
694
|
${DATA_FILE_HELPER_LIB}
|
|
640
695
|
|
|
@@ -646,13 +701,19 @@ explore_df(${varName})
|
|
|
646
701
|
`;
|
|
647
702
|
}
|
|
648
703
|
const CODE_EXECUTION_REQUEST_PROCESSOR = new CodeExecutionRequestProcessor();
|
|
649
|
-
|
|
704
|
+
const LLM_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.llmAgent");
|
|
705
|
+
function isLlmAgent(obj) {
|
|
706
|
+
return typeof obj === "object" && obj !== null && LLM_AGENT_SIGNATURE_SYMBOL in obj && obj[LLM_AGENT_SIGNATURE_SYMBOL] === true;
|
|
707
|
+
}
|
|
708
|
+
class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
650
709
|
constructor(config) {
|
|
651
|
-
var
|
|
710
|
+
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
|
|
652
711
|
super(config);
|
|
712
|
+
/** A unique symbol to identify ADK LLM agent class. */
|
|
713
|
+
this[_a] = true;
|
|
653
714
|
this.model = config.model;
|
|
654
|
-
this.instruction = (
|
|
655
|
-
this.globalInstruction = (
|
|
715
|
+
this.instruction = (_a2 = config.instruction) != null ? _a2 : "";
|
|
716
|
+
this.globalInstruction = (_b2 = config.globalInstruction) != null ? _b2 : "";
|
|
656
717
|
this.tools = (_c = config.tools) != null ? _c : [];
|
|
657
718
|
this.generateContentConfig = config.generateContentConfig;
|
|
658
719
|
this.disallowTransferToParent = (_d = config.disallowTransferToParent) != null ? _d : false;
|
|
@@ -730,7 +791,7 @@ class LlmAgent extends BaseAgent {
|
|
|
730
791
|
}
|
|
731
792
|
let ancestorAgent = this.parentAgent;
|
|
732
793
|
while (ancestorAgent) {
|
|
733
|
-
if (ancestorAgent
|
|
794
|
+
if (isLlmAgent(ancestorAgent)) {
|
|
734
795
|
return ancestorAgent.canonicalModel;
|
|
735
796
|
}
|
|
736
797
|
ancestorAgent = ancestorAgent.parentAgent;
|
|
@@ -738,12 +799,12 @@ class LlmAgent extends BaseAgent {
|
|
|
738
799
|
throw new Error(`No model found for ${this.name}.`);
|
|
739
800
|
}
|
|
740
801
|
/**
|
|
741
|
-
* The resolved
|
|
802
|
+
* The resolved instruction field to construct instruction for this
|
|
742
803
|
* agent.
|
|
743
804
|
*
|
|
744
805
|
* This method is only for use by Agent Development Kit.
|
|
745
806
|
* @param context The context to retrieve the session state.
|
|
746
|
-
* @returns The resolved
|
|
807
|
+
* @returns The resolved instruction field.
|
|
747
808
|
*/
|
|
748
809
|
async canonicalInstruction(context) {
|
|
749
810
|
if (typeof this.instruction === "string") {
|
|
@@ -755,11 +816,11 @@ class LlmAgent extends BaseAgent {
|
|
|
755
816
|
};
|
|
756
817
|
}
|
|
757
818
|
/**
|
|
758
|
-
* The resolved
|
|
819
|
+
* The resolved globalInstruction field to construct global instruction.
|
|
759
820
|
*
|
|
760
821
|
* This method is only for use by Agent Development Kit.
|
|
761
822
|
* @param context The context to retrieve the session state.
|
|
762
|
-
* @returns The resolved
|
|
823
|
+
* @returns The resolved globalInstruction field.
|
|
763
824
|
*/
|
|
764
825
|
async canonicalGlobalInstruction(context) {
|
|
765
826
|
if (typeof this.globalInstruction === "string") {
|
|
@@ -771,7 +832,7 @@ class LlmAgent extends BaseAgent {
|
|
|
771
832
|
};
|
|
772
833
|
}
|
|
773
834
|
/**
|
|
774
|
-
* The resolved
|
|
835
|
+
* The resolved tools field as a list of BaseTool based on the context.
|
|
775
836
|
*
|
|
776
837
|
* This method is only for use by Agent Development Kit.
|
|
777
838
|
*/
|
|
@@ -799,7 +860,7 @@ class LlmAgent extends BaseAgent {
|
|
|
799
860
|
return [callback];
|
|
800
861
|
}
|
|
801
862
|
/**
|
|
802
|
-
* The resolved
|
|
863
|
+
* The resolved beforeModelCallback field as a list of
|
|
803
864
|
* SingleBeforeModelCallback.
|
|
804
865
|
*
|
|
805
866
|
* This method is only for use by Agent Development Kit.
|
|
@@ -808,7 +869,7 @@ class LlmAgent extends BaseAgent {
|
|
|
808
869
|
return LlmAgent.normalizeCallbackArray(this.beforeModelCallback);
|
|
809
870
|
}
|
|
810
871
|
/**
|
|
811
|
-
* The resolved
|
|
872
|
+
* The resolved afterModelCallback field as a list of
|
|
812
873
|
* SingleAfterModelCallback.
|
|
813
874
|
*
|
|
814
875
|
* This method is only for use by Agent Development Kit.
|
|
@@ -817,7 +878,7 @@ class LlmAgent extends BaseAgent {
|
|
|
817
878
|
return LlmAgent.normalizeCallbackArray(this.afterModelCallback);
|
|
818
879
|
}
|
|
819
880
|
/**
|
|
820
|
-
* The resolved
|
|
881
|
+
* The resolved beforeToolCallback field as a list of
|
|
821
882
|
* BeforeToolCallback.
|
|
822
883
|
*
|
|
823
884
|
* This method is only for use by Agent Development Kit.
|
|
@@ -826,7 +887,7 @@ class LlmAgent extends BaseAgent {
|
|
|
826
887
|
return LlmAgent.normalizeCallbackArray(this.beforeToolCallback);
|
|
827
888
|
}
|
|
828
889
|
/**
|
|
829
|
-
* The resolved
|
|
890
|
+
* The resolved afterToolCallback field as a list of AfterToolCallback.
|
|
830
891
|
*
|
|
831
892
|
* This method is only for use by Agent Development Kit.
|
|
832
893
|
*/
|
|
@@ -843,7 +904,7 @@ class LlmAgent extends BaseAgent {
|
|
|
843
904
|
* @param event The event to process.
|
|
844
905
|
*/
|
|
845
906
|
maybeSaveOutputToState(event) {
|
|
846
|
-
var
|
|
907
|
+
var _a2, _b2;
|
|
847
908
|
if (event.author !== this.name) {
|
|
848
909
|
logger.debug(
|
|
849
910
|
`Skipping output save for agent ${this.name}: event authored by ${event.author}`
|
|
@@ -862,7 +923,7 @@ class LlmAgent extends BaseAgent {
|
|
|
862
923
|
);
|
|
863
924
|
return;
|
|
864
925
|
}
|
|
865
|
-
if (!((
|
|
926
|
+
if (!((_b2 = (_a2 = event.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2.length)) {
|
|
866
927
|
logger.debug(
|
|
867
928
|
`Skipping output save for agent ${this.name}: event content is empty`
|
|
868
929
|
);
|
|
@@ -922,7 +983,10 @@ class LlmAgent extends BaseAgent {
|
|
|
922
983
|
liveConnectConfig: {}
|
|
923
984
|
};
|
|
924
985
|
for (const processor of this.requestProcessors) {
|
|
925
|
-
for await (const event of processor.runAsync(
|
|
986
|
+
for await (const event of processor.runAsync(
|
|
987
|
+
invocationContext,
|
|
988
|
+
llmRequest
|
|
989
|
+
)) {
|
|
926
990
|
yield event;
|
|
927
991
|
}
|
|
928
992
|
}
|
|
@@ -962,9 +1026,12 @@ class LlmAgent extends BaseAgent {
|
|
|
962
1026
|
}
|
|
963
1027
|
}
|
|
964
1028
|
async *postprocess(invocationContext, llmRequest, llmResponse, modelResponseEvent) {
|
|
965
|
-
var
|
|
1029
|
+
var _a2;
|
|
966
1030
|
for (const processor of this.responseProcessors) {
|
|
967
|
-
for await (const event of processor.runAsync(
|
|
1031
|
+
for await (const event of processor.runAsync(
|
|
1032
|
+
invocationContext,
|
|
1033
|
+
llmResponse
|
|
1034
|
+
)) {
|
|
968
1035
|
yield event;
|
|
969
1036
|
}
|
|
970
1037
|
}
|
|
@@ -985,7 +1052,7 @@ class LlmAgent extends BaseAgent {
|
|
|
985
1052
|
}
|
|
986
1053
|
}
|
|
987
1054
|
yield mergedEvent;
|
|
988
|
-
if (!((
|
|
1055
|
+
if (!((_a2 = getFunctionCalls(mergedEvent)) == null ? void 0 : _a2.length)) {
|
|
989
1056
|
return;
|
|
990
1057
|
}
|
|
991
1058
|
const functionResponseEvent = await handleFunctionCallsAsync({
|
|
@@ -998,7 +1065,10 @@ class LlmAgent extends BaseAgent {
|
|
|
998
1065
|
if (!functionResponseEvent) {
|
|
999
1066
|
return;
|
|
1000
1067
|
}
|
|
1001
|
-
const authEvent = generateAuthEvent(
|
|
1068
|
+
const authEvent = generateAuthEvent(
|
|
1069
|
+
invocationContext,
|
|
1070
|
+
functionResponseEvent
|
|
1071
|
+
);
|
|
1002
1072
|
if (authEvent) {
|
|
1003
1073
|
yield authEvent;
|
|
1004
1074
|
}
|
|
@@ -1040,7 +1110,7 @@ class LlmAgent extends BaseAgent {
|
|
|
1040
1110
|
return agentToRun;
|
|
1041
1111
|
}
|
|
1042
1112
|
async *callLlmAsync(invocationContext, llmRequest, modelResponseEvent) {
|
|
1043
|
-
var
|
|
1113
|
+
var _a2, _b2, _c, _d, _e;
|
|
1044
1114
|
const beforeModelResponse = await this.handleBeforeModelCallback(
|
|
1045
1115
|
invocationContext,
|
|
1046
1116
|
llmRequest,
|
|
@@ -1050,8 +1120,8 @@ class LlmAgent extends BaseAgent {
|
|
|
1050
1120
|
yield beforeModelResponse;
|
|
1051
1121
|
return;
|
|
1052
1122
|
}
|
|
1053
|
-
(
|
|
1054
|
-
(_c = (
|
|
1123
|
+
(_a2 = llmRequest.config) != null ? _a2 : llmRequest.config = {};
|
|
1124
|
+
(_c = (_b2 = llmRequest.config).labels) != null ? _c : _b2.labels = {};
|
|
1055
1125
|
if (!llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY]) {
|
|
1056
1126
|
llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY] = this.name;
|
|
1057
1127
|
}
|
|
@@ -1081,17 +1151,22 @@ class LlmAgent extends BaseAgent {
|
|
|
1081
1151
|
}
|
|
1082
1152
|
}
|
|
1083
1153
|
async handleBeforeModelCallback(invocationContext, llmRequest, modelResponseEvent) {
|
|
1084
|
-
const callbackContext = new CallbackContext(
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1154
|
+
const callbackContext = new CallbackContext({
|
|
1155
|
+
invocationContext,
|
|
1156
|
+
eventActions: modelResponseEvent.actions
|
|
1157
|
+
});
|
|
1158
|
+
const beforeModelCallbackResponse = await invocationContext.pluginManager.runBeforeModelCallback({
|
|
1159
|
+
callbackContext,
|
|
1160
|
+
llmRequest
|
|
1161
|
+
});
|
|
1090
1162
|
if (beforeModelCallbackResponse) {
|
|
1091
1163
|
return beforeModelCallbackResponse;
|
|
1092
1164
|
}
|
|
1093
1165
|
for (const callback of this.canonicalBeforeModelCallbacks) {
|
|
1094
|
-
const callbackResponse = await callback({
|
|
1166
|
+
const callbackResponse = await callback({
|
|
1167
|
+
context: callbackContext,
|
|
1168
|
+
request: llmRequest
|
|
1169
|
+
});
|
|
1095
1170
|
if (callbackResponse) {
|
|
1096
1171
|
return callbackResponse;
|
|
1097
1172
|
}
|
|
@@ -1099,17 +1174,22 @@ class LlmAgent extends BaseAgent {
|
|
|
1099
1174
|
return void 0;
|
|
1100
1175
|
}
|
|
1101
1176
|
async handleAfterModelCallback(invocationContext, llmResponse, modelResponseEvent) {
|
|
1102
|
-
const callbackContext = new CallbackContext(
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1177
|
+
const callbackContext = new CallbackContext({
|
|
1178
|
+
invocationContext,
|
|
1179
|
+
eventActions: modelResponseEvent.actions
|
|
1180
|
+
});
|
|
1181
|
+
const afterModelCallbackResponse = await invocationContext.pluginManager.runAfterModelCallback({
|
|
1182
|
+
callbackContext,
|
|
1183
|
+
llmResponse
|
|
1184
|
+
});
|
|
1108
1185
|
if (afterModelCallbackResponse) {
|
|
1109
1186
|
return afterModelCallbackResponse;
|
|
1110
1187
|
}
|
|
1111
1188
|
for (const callback of this.canonicalAfterModelCallbacks) {
|
|
1112
|
-
const callbackResponse = await callback({
|
|
1189
|
+
const callbackResponse = await callback({
|
|
1190
|
+
context: callbackContext,
|
|
1191
|
+
response: llmResponse
|
|
1192
|
+
});
|
|
1113
1193
|
if (callbackResponse) {
|
|
1114
1194
|
return callbackResponse;
|
|
1115
1195
|
}
|
|
@@ -1122,9 +1202,10 @@ class LlmAgent extends BaseAgent {
|
|
|
1122
1202
|
yield response;
|
|
1123
1203
|
}
|
|
1124
1204
|
} catch (modelError) {
|
|
1125
|
-
const callbackContext = new CallbackContext(
|
|
1126
|
-
|
|
1127
|
-
|
|
1205
|
+
const callbackContext = new CallbackContext({
|
|
1206
|
+
invocationContext,
|
|
1207
|
+
eventActions: modelResponseEvent.actions
|
|
1208
|
+
});
|
|
1128
1209
|
if (modelError instanceof Error) {
|
|
1129
1210
|
const onModelErrorCallbackResponse = await invocationContext.pluginManager.runOnModelErrorCallback({
|
|
1130
1211
|
callbackContext,
|
|
@@ -1156,5 +1237,6 @@ class LlmAgent extends BaseAgent {
|
|
|
1156
1237
|
export {
|
|
1157
1238
|
LlmAgent,
|
|
1158
1239
|
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
1240
|
+
isLlmAgent,
|
|
1159
1241
|
responseProcessor
|
|
1160
1242
|
};
|