@google/adk 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/agents/base_agent.js +52 -24
- package/dist/cjs/agents/callback_context.js +4 -1
- package/dist/cjs/agents/content_processor_utils.js +15 -7
- package/dist/cjs/agents/functions.js +79 -29
- package/dist/cjs/agents/invocation_context.js +3 -1
- package/dist/cjs/agents/llm_agent.js +188 -108
- package/dist/cjs/agents/loop_agent.js +18 -6
- package/dist/cjs/agents/parallel_agent.js +20 -7
- package/dist/cjs/agents/readonly_context.js +4 -1
- package/dist/cjs/agents/sequential_agent.js +34 -12
- package/dist/cjs/artifacts/gcs_artifact_service.js +28 -20
- package/dist/cjs/artifacts/in_memory_artifact_service.js +18 -4
- package/dist/cjs/auth/auth_handler.js +3 -1
- package/dist/cjs/code_executors/base_code_executor.js +14 -2
- package/dist/cjs/code_executors/built_in_code_executor.js +21 -5
- package/dist/cjs/code_executors/code_executor_context.js +5 -5
- package/dist/cjs/common.js +45 -0
- package/dist/cjs/events/event.js +1 -3
- package/dist/cjs/examples/base_example_provider.js +18 -2
- package/dist/cjs/examples/example_util.js +1 -1
- package/dist/cjs/index.js +19 -19
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/memory/in_memory_memory_service.js +3 -1
- package/dist/cjs/models/base_llm.js +8 -4
- package/dist/cjs/models/gemini_llm_connection.js +1 -0
- package/dist/cjs/models/google_llm.js +3 -3
- package/dist/cjs/plugins/base_plugin.js +71 -49
- package/dist/cjs/plugins/logging_plugin.js +50 -13
- package/dist/cjs/plugins/plugin_manager.js +56 -24
- package/dist/cjs/plugins/security_plugin.js +5 -2
- package/dist/cjs/runner/runner.js +126 -101
- package/dist/cjs/sessions/in_memory_session_service.js +38 -14
- package/dist/cjs/telemetry/google_cloud.js +7 -9
- package/dist/cjs/telemetry/setup.js +15 -7
- package/dist/cjs/telemetry/tracing.js +37 -15
- package/dist/cjs/tools/agent_tool.js +26 -13
- package/dist/cjs/tools/base_tool.js +19 -7
- package/dist/cjs/tools/forwarding_artifact_service.js +1 -1
- package/dist/cjs/tools/function_tool.js +15 -7
- package/dist/cjs/tools/google_search_tool.js +8 -4
- package/dist/cjs/tools/mcp/mcp_session_manager.js +16 -10
- package/dist/cjs/tools/mcp/mcp_tool.js +1 -3
- package/dist/cjs/tools/mcp/mcp_toolset.js +1 -1
- package/dist/cjs/tools/tool_context.js +4 -9
- package/dist/cjs/utils/env_aware_utils.js +1 -1
- package/dist/cjs/utils/gemini_schema_util.js +10 -4
- package/dist/cjs/utils/logger.js +47 -3
- package/dist/cjs/utils/simple_zod_to_json.js +100 -141
- package/dist/cjs/utils/variant_utils.js +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/agents/base_agent.js +57 -25
- package/dist/esm/agents/callback_context.js +4 -1
- package/dist/esm/agents/content_processor_utils.js +25 -9
- package/dist/esm/agents/functions.js +83 -29
- package/dist/esm/agents/invocation_context.js +3 -1
- package/dist/esm/agents/llm_agent.js +228 -116
- package/dist/esm/agents/loop_agent.js +16 -5
- package/dist/esm/agents/parallel_agent.js +18 -6
- package/dist/esm/agents/readonly_context.js +4 -1
- package/dist/esm/agents/sequential_agent.js +33 -12
- package/dist/esm/artifacts/gcs_artifact_service.js +28 -20
- package/dist/esm/artifacts/in_memory_artifact_service.js +18 -4
- package/dist/esm/auth/auth_handler.js +3 -1
- package/dist/esm/code_executors/base_code_executor.js +12 -1
- package/dist/esm/code_executors/built_in_code_executor.js +19 -4
- package/dist/esm/code_executors/code_executor_context.js +5 -5
- package/dist/esm/common.js +56 -11
- package/dist/esm/events/event.js +1 -3
- package/dist/esm/examples/base_example_provider.js +16 -1
- package/dist/esm/examples/example_util.js +4 -2
- package/dist/esm/index.js +19 -19
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/memory/in_memory_memory_service.js +3 -1
- package/dist/esm/models/base_llm.js +8 -4
- package/dist/esm/models/gemini_llm_connection.js +1 -0
- package/dist/esm/models/google_llm.js +8 -4
- package/dist/esm/plugins/base_plugin.js +71 -49
- package/dist/esm/plugins/logging_plugin.js +55 -14
- package/dist/esm/plugins/plugin_manager.js +56 -24
- package/dist/esm/plugins/security_plugin.js +5 -2
- package/dist/esm/runner/runner.js +139 -105
- package/dist/esm/sessions/in_memory_session_service.js +41 -15
- package/dist/esm/telemetry/google_cloud.js +7 -9
- package/dist/esm/telemetry/setup.js +23 -9
- package/dist/esm/telemetry/tracing.js +37 -15
- package/dist/esm/tools/agent_tool.js +25 -13
- package/dist/esm/tools/base_tool.js +17 -6
- package/dist/esm/tools/forwarding_artifact_service.js +1 -1
- package/dist/esm/tools/function_tool.js +13 -8
- package/dist/esm/tools/google_search_tool.js +6 -3
- package/dist/esm/tools/long_running_tool.js +3 -1
- package/dist/esm/tools/mcp/mcp_session_manager.js +22 -12
- package/dist/esm/tools/mcp/mcp_tool.js +1 -3
- package/dist/esm/tools/mcp/mcp_toolset.js +1 -1
- package/dist/esm/tools/tool_context.js +4 -9
- package/dist/esm/utils/env_aware_utils.js +1 -1
- package/dist/esm/utils/gemini_schema_util.js +10 -4
- package/dist/esm/utils/logger.js +43 -2
- package/dist/esm/utils/simple_zod_to_json.js +102 -141
- package/dist/esm/utils/variant_utils.js +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/agents/base_agent.d.ts +16 -4
- package/dist/types/agents/callback_context.d.ts +1 -1
- package/dist/types/agents/invocation_context.d.ts +4 -2
- package/dist/types/agents/llm_agent.d.ts +43 -31
- package/dist/types/agents/loop_agent.d.ts +17 -1
- package/dist/types/agents/parallel_agent.d.ts +17 -1
- package/dist/types/agents/sequential_agent.d.ts +17 -1
- package/dist/types/artifacts/in_memory_artifact_service.d.ts +3 -3
- 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 +20 -1
- package/dist/types/code_executors/code_executor_context.d.ts +2 -4
- package/dist/types/common.d.ts +40 -15
- package/dist/types/examples/base_example_provider.d.ts +16 -0
- package/dist/types/index.d.ts +3 -3
- package/dist/types/models/base_llm_connection.d.ts +1 -1
- package/dist/types/models/llm_response.d.ts +1 -1
- package/dist/types/plugins/base_plugin.d.ts +50 -40
- package/dist/types/plugins/logging_plugin.d.ts +12 -12
- package/dist/types/plugins/plugin_manager.d.ts +12 -12
- package/dist/types/plugins/security_plugin.d.ts +1 -1
- package/dist/types/runner/runner.d.ts +18 -10
- package/dist/types/sessions/in_memory_session_service.d.ts +5 -5
- package/dist/types/telemetry/setup.d.ts +1 -1
- package/dist/types/telemetry/tracing.d.ts +7 -6
- package/dist/types/tools/agent_tool.d.ts +15 -1
- package/dist/types/tools/base_tool.d.ts +15 -1
- package/dist/types/tools/base_toolset.d.ts +2 -1
- package/dist/types/tools/forwarding_artifact_service.d.ts +2 -2
- package/dist/types/tools/function_tool.d.ts +18 -4
- package/dist/types/tools/google_search_tool.d.ts +5 -6
- package/dist/types/tools/mcp/mcp_session_manager.d.ts +10 -3
- package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -2
- package/dist/types/tools/tool_context.d.ts +1 -1
- package/dist/types/utils/gemini_schema_util.d.ts +4 -12
- package/dist/types/utils/logger.d.ts +11 -10
- package/dist/types/utils/simple_zod_to_json.d.ts +5 -4
- package/dist/types/version.d.ts +1 -1
- package/dist/web/agents/base_agent.js +103 -36
- package/dist/web/agents/callback_context.js +4 -1
- package/dist/web/agents/content_processor_utils.js +25 -9
- package/dist/web/agents/functions.js +83 -29
- package/dist/web/agents/invocation_context.js +3 -1
- package/dist/web/agents/llm_agent.js +282 -137
- package/dist/web/agents/loop_agent.js +16 -5
- package/dist/web/agents/parallel_agent.js +18 -6
- package/dist/web/agents/readonly_context.js +4 -1
- package/dist/web/agents/sequential_agent.js +33 -12
- package/dist/web/artifacts/gcs_artifact_service.js +25 -17
- package/dist/web/artifacts/in_memory_artifact_service.js +18 -4
- package/dist/web/auth/auth_handler.js +3 -1
- package/dist/web/code_executors/base_code_executor.js +12 -1
- package/dist/web/code_executors/built_in_code_executor.js +19 -4
- package/dist/web/code_executors/code_executor_context.js +5 -5
- package/dist/web/common.js +56 -11
- package/dist/web/events/event.js +1 -3
- 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/memory/in_memory_memory_service.js +3 -1
- package/dist/web/models/base_llm.js +8 -4
- package/dist/web/models/gemini_llm_connection.js +1 -0
- package/dist/web/models/google_llm.js +8 -4
- package/dist/web/plugins/base_plugin.js +71 -49
- package/dist/web/plugins/logging_plugin.js +55 -14
- package/dist/web/plugins/plugin_manager.js +56 -24
- package/dist/web/plugins/security_plugin.js +5 -2
- package/dist/web/runner/runner.js +186 -119
- package/dist/web/sessions/in_memory_session_service.js +41 -15
- package/dist/web/telemetry/google_cloud.js +7 -9
- package/dist/web/telemetry/setup.js +23 -9
- package/dist/web/telemetry/tracing.js +37 -15
- package/dist/web/tools/agent_tool.js +25 -13
- package/dist/web/tools/base_tool.js +17 -6
- package/dist/web/tools/forwarding_artifact_service.js +1 -1
- package/dist/web/tools/function_tool.js +13 -8
- package/dist/web/tools/google_search_tool.js +6 -3
- package/dist/web/tools/long_running_tool.js +3 -1
- package/dist/web/tools/mcp/mcp_session_manager.js +22 -12
- package/dist/web/tools/mcp/mcp_tool.js +1 -3
- package/dist/web/tools/mcp/mcp_toolset.js +1 -1
- package/dist/web/tools/tool_context.js +4 -9
- package/dist/web/utils/env_aware_utils.js +1 -1
- package/dist/web/utils/gemini_schema_util.js +10 -4
- package/dist/web/utils/logger.js +43 -2
- package/dist/web/utils/simple_zod_to_json.js +102 -155
- package/dist/web/utils/variant_utils.js +1 -1
- package/dist/web/version.js +1 -1
- package/package.json +5 -3
|
@@ -38,7 +38,9 @@ class InMemoryMemoryService {
|
|
|
38
38
|
if (!wordsInEvent.size) {
|
|
39
39
|
continue;
|
|
40
40
|
}
|
|
41
|
-
const matchQuery = wordsInQuery.some(
|
|
41
|
+
const matchQuery = wordsInQuery.some(
|
|
42
|
+
(queryWord) => wordsInEvent.has(queryWord)
|
|
43
|
+
);
|
|
42
44
|
if (matchQuery) {
|
|
43
45
|
response.memories.push({
|
|
44
46
|
content: event.content,
|
|
@@ -43,16 +43,20 @@ class BaseLlm {
|
|
|
43
43
|
llmRequest.contents.push({
|
|
44
44
|
role: "user",
|
|
45
45
|
parts: [
|
|
46
|
-
{
|
|
46
|
+
{
|
|
47
|
+
text: "Handle the requests as specified in the System Instruction."
|
|
48
|
+
}
|
|
47
49
|
]
|
|
48
50
|
});
|
|
49
51
|
}
|
|
50
52
|
if (((_a2 = llmRequest.contents[llmRequest.contents.length - 1]) == null ? void 0 : _a2.role) !== "user") {
|
|
51
53
|
llmRequest.contents.push({
|
|
52
54
|
role: "user",
|
|
53
|
-
parts: [
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
parts: [
|
|
56
|
+
{
|
|
57
|
+
text: "Continue processing previous requests as instructed. Exit or provide a summary if no more outputs are needed."
|
|
58
|
+
}
|
|
59
|
+
]
|
|
56
60
|
});
|
|
57
61
|
}
|
|
58
62
|
}
|
|
@@ -3,14 +3,16 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
createPartFromText,
|
|
8
|
+
FinishReason,
|
|
9
|
+
GoogleGenAI
|
|
10
|
+
} from "@google/genai";
|
|
7
11
|
import { logger } from "../utils/logger.js";
|
|
8
12
|
import { GoogleLLMVariant } from "../utils/variant_utils.js";
|
|
9
13
|
import { BaseLlm } from "./base_llm.js";
|
|
10
14
|
import { GeminiLlmConnection } from "./gemini_llm_connection.js";
|
|
11
15
|
import { createLlmResponse } from "./llm_response.js";
|
|
12
|
-
const AGENT_ENGINE_TELEMETRY_TAG = "remote_reasoning_engine";
|
|
13
|
-
const AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME = "GOOGLE_CLOUD_AGENT_ENGINE_ID";
|
|
14
16
|
class Gemini extends BaseLlm {
|
|
15
17
|
/**
|
|
16
18
|
* @param params The parameters for creating a Gemini instance.
|
|
@@ -223,7 +225,9 @@ class Gemini extends BaseLlm {
|
|
|
223
225
|
llmRequest.liveConnectConfig.systemInstruction = {
|
|
224
226
|
role: "system",
|
|
225
227
|
// TODO - b/425992518: validate type casting works well.
|
|
226
|
-
parts: [
|
|
228
|
+
parts: [
|
|
229
|
+
createPartFromText(llmRequest.config.systemInstruction)
|
|
230
|
+
]
|
|
227
231
|
};
|
|
228
232
|
}
|
|
229
233
|
llmRequest.liveConnectConfig.tools = (_c = llmRequest.config) == null ? void 0 : _c.tools;
|
|
@@ -19,13 +19,14 @@ class BasePlugin {
|
|
|
19
19
|
* This callback helps logging and modifying the user message before the
|
|
20
20
|
* runner starts the invocation.
|
|
21
21
|
*
|
|
22
|
-
* @param invocationContext The context for the entire invocation.
|
|
23
|
-
* @param userMessage The message content input by user.
|
|
22
|
+
* @param params.invocationContext The context for the entire invocation.
|
|
23
|
+
* @param params.userMessage The message content input by user.
|
|
24
24
|
* @returns An optional `Content` to be returned to the ADK. Returning a
|
|
25
25
|
* value to replace the user message. Returning `undefined` to proceed
|
|
26
26
|
* normally.
|
|
27
27
|
*/
|
|
28
|
-
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
29
|
+
async onUserMessageCallback(params) {
|
|
29
30
|
return;
|
|
30
31
|
}
|
|
31
32
|
/**
|
|
@@ -34,13 +35,14 @@ class BasePlugin {
|
|
|
34
35
|
* This is the first callback to be called in the lifecycle, ideal for global
|
|
35
36
|
* setup or initialization tasks.
|
|
36
37
|
*
|
|
37
|
-
* @param invocationContext The context for the entire invocation, containing
|
|
38
|
+
* @param params.invocationContext The context for the entire invocation, containing
|
|
38
39
|
* session information, the root agent, etc.
|
|
39
40
|
* @returns An optional `Event` to be returned to the ADK. Returning a value
|
|
40
41
|
* to halt execution of the runner and ends the runner with that event.
|
|
41
42
|
* Return `undefined` to proceed normally.
|
|
42
43
|
*/
|
|
43
|
-
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
45
|
+
async beforeRunCallback(params) {
|
|
44
46
|
return;
|
|
45
47
|
}
|
|
46
48
|
/**
|
|
@@ -49,13 +51,14 @@ class BasePlugin {
|
|
|
49
51
|
* This is the ideal place to make modification to the event before the event
|
|
50
52
|
* is handled by the underlying agent app.
|
|
51
53
|
*
|
|
52
|
-
* @param invocationContext The context for the entire invocation.
|
|
53
|
-
* @param event The event raised by the runner.
|
|
54
|
+
* @param params.invocationContext The context for the entire invocation.
|
|
55
|
+
* @param params.event The event raised by the runner.
|
|
54
56
|
* @returns An optional value. A non-`undefined` return may be used by the
|
|
55
57
|
* framework to modify or replace the response. Returning `undefined`
|
|
56
58
|
* allows the original response to be used.
|
|
57
59
|
*/
|
|
58
|
-
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
61
|
+
async onEventCallback(params) {
|
|
59
62
|
return;
|
|
60
63
|
}
|
|
61
64
|
/**
|
|
@@ -64,10 +67,11 @@ class BasePlugin {
|
|
|
64
67
|
* This is the final callback in the ADK lifecycle, suitable for cleanup,
|
|
65
68
|
* final logging, or reporting tasks.
|
|
66
69
|
*
|
|
67
|
-
* @param invocationContext The context for the entire invocation.
|
|
70
|
+
* @param params.invocationContext The context for the entire invocation.
|
|
68
71
|
* @returns undefined
|
|
69
72
|
*/
|
|
70
|
-
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
74
|
+
async afterRunCallback(params) {
|
|
71
75
|
return;
|
|
72
76
|
}
|
|
73
77
|
/**
|
|
@@ -76,13 +80,14 @@ class BasePlugin {
|
|
|
76
80
|
* This callback can be used for logging, setup, or to short-circuit the
|
|
77
81
|
* agent's execution by returning a value.
|
|
78
82
|
*
|
|
79
|
-
* @param agent The agent that is about to run.
|
|
80
|
-
* @param callbackContext The context for the agent invocation.
|
|
83
|
+
* @param params.agent The agent that is about to run.
|
|
84
|
+
* @param params.callbackContext The context for the agent invocation.
|
|
81
85
|
* @returns An optional `Content` object. If a value is returned, it will
|
|
82
86
|
* bypass the agent's callbacks and its execution, and return this value
|
|
83
87
|
* directly. Returning `undefined` allows the agent to proceed normally.
|
|
84
88
|
*/
|
|
85
|
-
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
90
|
+
async beforeAgentCallback(params) {
|
|
86
91
|
return;
|
|
87
92
|
}
|
|
88
93
|
/**
|
|
@@ -91,13 +96,14 @@ class BasePlugin {
|
|
|
91
96
|
* This callback can be used to inspect, log, or modify the agent's final
|
|
92
97
|
* result before it is returned.
|
|
93
98
|
*
|
|
94
|
-
* @param agent The agent that has just run.
|
|
95
|
-
* @param callbackContext The context for the agent invocation.
|
|
99
|
+
* @param params.agent The agent that has just run.
|
|
100
|
+
* @param params.callbackContext The context for the agent invocation.
|
|
96
101
|
* @returns An optional `Content` object. If a value is returned, it will
|
|
97
102
|
* replace the agent's original result. Returning `undefined` uses the
|
|
98
103
|
* original, unmodified result.
|
|
99
104
|
*/
|
|
100
|
-
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
106
|
+
async afterAgentCallback(params) {
|
|
101
107
|
return;
|
|
102
108
|
}
|
|
103
109
|
/**
|
|
@@ -107,13 +113,14 @@ class BasePlugin {
|
|
|
107
113
|
* object. It can also be used to implement caching by returning a cached
|
|
108
114
|
* `LlmResponse`, which would skip the actual model call.
|
|
109
115
|
*
|
|
110
|
-
* @param callbackContext The context for the current agent call.
|
|
111
|
-
* @param llmRequest The prepared request object to be sent to the model.
|
|
116
|
+
* @param params.callbackContext The context for the current agent call.
|
|
117
|
+
* @param params.llmRequest The prepared request object to be sent to the model.
|
|
112
118
|
* @returns An optional value. The interpretation of a non-`undefined`
|
|
113
119
|
* trigger an early exit and returns the response immediately. Returning
|
|
114
120
|
* `undefined` allows the LLM request to proceed normally.
|
|
115
121
|
*/
|
|
116
|
-
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
123
|
+
async beforeModelCallback(params) {
|
|
117
124
|
return;
|
|
118
125
|
}
|
|
119
126
|
/**
|
|
@@ -122,13 +129,14 @@ class BasePlugin {
|
|
|
122
129
|
* This is the ideal place to log model responses, collect metrics on token
|
|
123
130
|
* usage, or perform post-processing on the raw `LlmResponse`.
|
|
124
131
|
*
|
|
125
|
-
* @param callbackContext The context for the current agent call.
|
|
126
|
-
* @param llmResponse The response object received from the model.
|
|
132
|
+
* @param params.callbackContext The context for the current agent call.
|
|
133
|
+
* @param params.llmResponse The response object received from the model.
|
|
127
134
|
* @returns An optional value. A non-`undefined` return may be used by the
|
|
128
135
|
* framework to modify or replace the response. Returning `undefined`
|
|
129
136
|
* allows the original response to be used.
|
|
130
137
|
*/
|
|
131
|
-
|
|
138
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
139
|
+
async afterModelCallback(params) {
|
|
132
140
|
return;
|
|
133
141
|
}
|
|
134
142
|
/**
|
|
@@ -137,15 +145,16 @@ class BasePlugin {
|
|
|
137
145
|
* This callback provides an opportunity to handle model errors gracefully,
|
|
138
146
|
* potentially providing alternative responses or recovery mechanisms.
|
|
139
147
|
*
|
|
140
|
-
* @param callbackContext The context for the current agent call.
|
|
141
|
-
* @param llmRequest The request that was sent to the model when the error
|
|
148
|
+
* @param params.callbackContext The context for the current agent call.
|
|
149
|
+
* @param params.llmRequest The request that was sent to the model when the error
|
|
142
150
|
* occurred.
|
|
143
|
-
* @param error The exception that was raised during model execution.
|
|
151
|
+
* @param params.error The exception that was raised during model execution.
|
|
144
152
|
* @returns An optional LlmResponse. If an LlmResponse is returned, it will be
|
|
145
153
|
* used instead of propagating the error. Returning `undefined` allows
|
|
146
154
|
* the original error to be raised.
|
|
147
155
|
*/
|
|
148
|
-
|
|
156
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
157
|
+
async onModelErrorCallback(params) {
|
|
149
158
|
return;
|
|
150
159
|
}
|
|
151
160
|
/**
|
|
@@ -154,15 +163,16 @@ class BasePlugin {
|
|
|
154
163
|
* This callback is useful for logging tool usage, input validation, or
|
|
155
164
|
* modifying the arguments before they are passed to the tool.
|
|
156
165
|
*
|
|
157
|
-
* @param tool The tool instance that is about to be executed.
|
|
158
|
-
* @param toolArgs The dictionary of arguments to be used for invoking the
|
|
166
|
+
* @param params.tool The tool instance that is about to be executed.
|
|
167
|
+
* @param params.toolArgs The dictionary of arguments to be used for invoking the
|
|
159
168
|
* tool.
|
|
160
|
-
* @param toolContext The context specific to the tool execution.
|
|
169
|
+
* @param params.toolContext The context specific to the tool execution.
|
|
161
170
|
* @returns An optional dictionary. If a dictionary is returned, it will stop
|
|
162
171
|
* the tool execution and return this response immediately. Returning
|
|
163
172
|
* `undefined` uses the original, unmodified arguments.
|
|
164
173
|
*/
|
|
165
|
-
|
|
174
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
175
|
+
async beforeToolCallback(params) {
|
|
166
176
|
return;
|
|
167
177
|
}
|
|
168
178
|
/**
|
|
@@ -171,33 +181,45 @@ class BasePlugin {
|
|
|
171
181
|
* This callback allows for inspecting, logging, or modifying the result
|
|
172
182
|
* returned by a tool.
|
|
173
183
|
*
|
|
174
|
-
* @param tool The tool instance that has just been executed.
|
|
175
|
-
* @param toolArgs The original arguments that were passed to the tool.
|
|
176
|
-
* @param toolContext The context specific to the tool execution.
|
|
177
|
-
* @param result The dictionary returned by the tool invocation.
|
|
184
|
+
* @param params.tool The tool instance that has just been executed.
|
|
185
|
+
* @param params.toolArgs The original arguments that were passed to the tool.
|
|
186
|
+
* @param params.toolContext The context specific to the tool execution.
|
|
187
|
+
* @param params.result The dictionary returned by the tool invocation.
|
|
178
188
|
* @returns An optional dictionary. If a dictionary is returned, it will
|
|
179
189
|
* **replace** the original result from the tool. This allows for
|
|
180
190
|
* post-processing or altering tool outputs. Returning `undefined` uses
|
|
181
191
|
* the original, unmodified result.
|
|
182
192
|
*/
|
|
183
|
-
|
|
193
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
194
|
+
async afterToolCallback(params) {
|
|
184
195
|
return;
|
|
185
196
|
}
|
|
186
197
|
/**
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
198
|
+
* Callback executed when a tool call encounters an error.
|
|
199
|
+
tool: BaseTool;
|
|
200
|
+
toolArgs: Record<string, unknown>;
|
|
201
|
+
toolContext: ToolContext;
|
|
202
|
+
result: Record<string, unknown>;
|
|
203
|
+
}): Promise<Record<string, unknown> | undefined> {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Callback executed when a tool call encounters an error.
|
|
209
|
+
*
|
|
210
|
+
* This callback provides an opportunity to handle tool errors gracefully,
|
|
211
|
+
* potentially providing alternative responses or recovery mechanisms.
|
|
212
|
+
*
|
|
213
|
+
* @param params.tool The tool instance that encountered an error.
|
|
214
|
+
* @param params.toolArgs The arguments that were passed to the tool.
|
|
215
|
+
* @param params.toolContext The context specific to the tool execution.
|
|
216
|
+
* @param params.error The exception that was raised during tool execution.
|
|
217
|
+
* @returns An optional dictionary. If a dictionary is returned, it will be
|
|
218
|
+
* used as the tool response instead of propagating the error. Returning
|
|
219
|
+
* `undefined` allows the original error to be raised.
|
|
220
|
+
*/
|
|
221
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
222
|
+
async onToolErrorCallback(params) {
|
|
201
223
|
return;
|
|
202
224
|
}
|
|
203
225
|
}
|
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
getFunctionCalls,
|
|
8
|
+
getFunctionResponses,
|
|
9
|
+
isFinalResponse
|
|
10
|
+
} from "../events/event.js";
|
|
7
11
|
import { logger } from "../utils/logger.js";
|
|
8
12
|
import { BasePlugin } from "./base_plugin.js";
|
|
9
13
|
class LoggingPlugin extends BasePlugin {
|
|
@@ -15,7 +19,10 @@ class LoggingPlugin extends BasePlugin {
|
|
|
15
19
|
constructor(name = "logging_plugin") {
|
|
16
20
|
super(name);
|
|
17
21
|
}
|
|
18
|
-
async onUserMessageCallback({
|
|
22
|
+
async onUserMessageCallback({
|
|
23
|
+
invocationContext,
|
|
24
|
+
userMessage
|
|
25
|
+
}) {
|
|
19
26
|
var _a;
|
|
20
27
|
this.log("\u{1F680} USER MESSAGE RECEIVED");
|
|
21
28
|
this.log(` Invocation ID: ${invocationContext.invocationId}`);
|
|
@@ -29,14 +36,18 @@ class LoggingPlugin extends BasePlugin {
|
|
|
29
36
|
}
|
|
30
37
|
return void 0;
|
|
31
38
|
}
|
|
32
|
-
async beforeRunCallback({
|
|
39
|
+
async beforeRunCallback({
|
|
40
|
+
invocationContext
|
|
41
|
+
}) {
|
|
33
42
|
var _a;
|
|
34
43
|
this.log("\u{1F3C3} INVOCATION STARTING");
|
|
35
44
|
this.log(` Invocation ID: ${invocationContext.invocationId}`);
|
|
36
45
|
this.log(` Starting Agent: ${(_a = invocationContext.agent.name) != null ? _a : "Unknown"}`);
|
|
37
46
|
return void 0;
|
|
38
47
|
}
|
|
39
|
-
async onEventCallback({
|
|
48
|
+
async onEventCallback({
|
|
49
|
+
event
|
|
50
|
+
}) {
|
|
40
51
|
this.log("\u{1F4E2} EVENT YIELDED");
|
|
41
52
|
this.log(` Event ID: ${event.id}`);
|
|
42
53
|
this.log(` Author: ${event.author}`);
|
|
@@ -57,14 +68,18 @@ class LoggingPlugin extends BasePlugin {
|
|
|
57
68
|
}
|
|
58
69
|
return void 0;
|
|
59
70
|
}
|
|
60
|
-
async afterRunCallback({
|
|
71
|
+
async afterRunCallback({
|
|
72
|
+
invocationContext
|
|
73
|
+
}) {
|
|
61
74
|
var _a;
|
|
62
75
|
this.log("\u2705 INVOCATION COMPLETED");
|
|
63
76
|
this.log(` Invocation ID: ${invocationContext.invocationId}`);
|
|
64
77
|
this.log(` Final Agent: ${(_a = invocationContext.agent.name) != null ? _a : "Unknown"}`);
|
|
65
78
|
return void 0;
|
|
66
79
|
}
|
|
67
|
-
async beforeAgentCallback({
|
|
80
|
+
async beforeAgentCallback({
|
|
81
|
+
callbackContext
|
|
82
|
+
}) {
|
|
68
83
|
this.log("\u{1F916} AGENT STARTING");
|
|
69
84
|
this.log(` Agent Name: ${callbackContext.agentName}`);
|
|
70
85
|
this.log(` Invocation ID: ${callbackContext.invocationId}`);
|
|
@@ -73,13 +88,18 @@ class LoggingPlugin extends BasePlugin {
|
|
|
73
88
|
}
|
|
74
89
|
return void 0;
|
|
75
90
|
}
|
|
76
|
-
async afterAgentCallback({
|
|
91
|
+
async afterAgentCallback({
|
|
92
|
+
callbackContext
|
|
93
|
+
}) {
|
|
77
94
|
this.log("\u{1F916} AGENT COMPLETED");
|
|
78
95
|
this.log(` Agent Name: ${callbackContext.agentName}`);
|
|
79
96
|
this.log(` Invocation ID: ${callbackContext.invocationId}`);
|
|
80
97
|
return void 0;
|
|
81
98
|
}
|
|
82
|
-
async beforeModelCallback({
|
|
99
|
+
async beforeModelCallback({
|
|
100
|
+
callbackContext,
|
|
101
|
+
llmRequest
|
|
102
|
+
}) {
|
|
83
103
|
var _a;
|
|
84
104
|
this.log("\u{1F9E0} LLM REQUEST");
|
|
85
105
|
this.log(` Model: ${(_a = llmRequest.model) != null ? _a : "default"}`);
|
|
@@ -97,7 +117,10 @@ class LoggingPlugin extends BasePlugin {
|
|
|
97
117
|
}
|
|
98
118
|
return void 0;
|
|
99
119
|
}
|
|
100
|
-
async afterModelCallback({
|
|
120
|
+
async afterModelCallback({
|
|
121
|
+
callbackContext,
|
|
122
|
+
llmResponse
|
|
123
|
+
}) {
|
|
101
124
|
this.log("\u{1F9E0} LLM RESPONSE");
|
|
102
125
|
this.log(` Agent: ${callbackContext.agentName}`);
|
|
103
126
|
if (llmResponse.errorCode) {
|
|
@@ -113,11 +136,17 @@ class LoggingPlugin extends BasePlugin {
|
|
|
113
136
|
}
|
|
114
137
|
}
|
|
115
138
|
if (llmResponse.usageMetadata) {
|
|
116
|
-
this.log(
|
|
139
|
+
this.log(
|
|
140
|
+
` Token Usage - Input: ${llmResponse.usageMetadata.promptTokenCount}, Output: ${llmResponse.usageMetadata.candidatesTokenCount}`
|
|
141
|
+
);
|
|
117
142
|
}
|
|
118
143
|
return void 0;
|
|
119
144
|
}
|
|
120
|
-
async beforeToolCallback({
|
|
145
|
+
async beforeToolCallback({
|
|
146
|
+
tool,
|
|
147
|
+
toolArgs,
|
|
148
|
+
toolContext
|
|
149
|
+
}) {
|
|
121
150
|
this.log("\u{1F527} TOOL STARTING");
|
|
122
151
|
this.log(` Tool Name: ${tool.name}`);
|
|
123
152
|
this.log(` Agent: ${toolContext.agentName}`);
|
|
@@ -125,7 +154,11 @@ class LoggingPlugin extends BasePlugin {
|
|
|
125
154
|
this.log(` Arguments: ${this.formatArgs(toolArgs)}`);
|
|
126
155
|
return void 0;
|
|
127
156
|
}
|
|
128
|
-
async afterToolCallback({
|
|
157
|
+
async afterToolCallback({
|
|
158
|
+
tool,
|
|
159
|
+
toolContext,
|
|
160
|
+
result
|
|
161
|
+
}) {
|
|
129
162
|
this.log("\u{1F527} TOOL COMPLETED");
|
|
130
163
|
this.log(` Tool Name: ${tool.name}`);
|
|
131
164
|
this.log(` Agent: ${toolContext.agentName}`);
|
|
@@ -133,13 +166,21 @@ class LoggingPlugin extends BasePlugin {
|
|
|
133
166
|
this.log(` Result: ${this.formatArgs(result)}`);
|
|
134
167
|
return void 0;
|
|
135
168
|
}
|
|
136
|
-
async onModelErrorCallback({
|
|
169
|
+
async onModelErrorCallback({
|
|
170
|
+
callbackContext,
|
|
171
|
+
error
|
|
172
|
+
}) {
|
|
137
173
|
this.log("\u{1F9E0} LLM ERROR");
|
|
138
174
|
this.log(` Agent: ${callbackContext.agentName}`);
|
|
139
175
|
this.log(` Error: ${error}`);
|
|
140
176
|
return void 0;
|
|
141
177
|
}
|
|
142
|
-
async onToolErrorCallback({
|
|
178
|
+
async onToolErrorCallback({
|
|
179
|
+
tool,
|
|
180
|
+
toolArgs,
|
|
181
|
+
toolContext,
|
|
182
|
+
error
|
|
183
|
+
}) {
|
|
143
184
|
this.log("\u{1F527} TOOL ERROR");
|
|
144
185
|
this.log(` Tool Name: ${tool.name}`);
|
|
145
186
|
this.log(` Agent: ${toolContext.agentName}`);
|
|
@@ -78,19 +78,22 @@ class PluginManager {
|
|
|
78
78
|
/**
|
|
79
79
|
* Runs the `onUserMessageCallback` for all plugins.
|
|
80
80
|
*/
|
|
81
|
-
async runOnUserMessageCallback({
|
|
81
|
+
async runOnUserMessageCallback({
|
|
82
|
+
userMessage,
|
|
83
|
+
invocationContext
|
|
84
|
+
}) {
|
|
82
85
|
return await this.runCallbacks(
|
|
83
86
|
this.plugins,
|
|
84
|
-
(plugin) => plugin.onUserMessageCallback(
|
|
85
|
-
{ userMessage, invocationContext }
|
|
86
|
-
),
|
|
87
|
+
(plugin) => plugin.onUserMessageCallback({ userMessage, invocationContext }),
|
|
87
88
|
"onUserMessageCallback"
|
|
88
89
|
);
|
|
89
90
|
}
|
|
90
91
|
/**
|
|
91
92
|
* Runs the `beforeRunCallback` for all plugins.
|
|
92
93
|
*/
|
|
93
|
-
async runBeforeRunCallback({
|
|
94
|
+
async runBeforeRunCallback({
|
|
95
|
+
invocationContext
|
|
96
|
+
}) {
|
|
94
97
|
return await this.runCallbacks(
|
|
95
98
|
this.plugins,
|
|
96
99
|
(plugin) => plugin.beforeRunCallback({ invocationContext }),
|
|
@@ -100,7 +103,9 @@ class PluginManager {
|
|
|
100
103
|
/**
|
|
101
104
|
* Runs the `afterRunCallback` for all plugins.
|
|
102
105
|
*/
|
|
103
|
-
async runAfterRunCallback({
|
|
106
|
+
async runAfterRunCallback({
|
|
107
|
+
invocationContext
|
|
108
|
+
}) {
|
|
104
109
|
await this.runCallbacks(
|
|
105
110
|
this.plugins,
|
|
106
111
|
(plugin) => plugin.afterRunCallback({ invocationContext }),
|
|
@@ -110,7 +115,10 @@ class PluginManager {
|
|
|
110
115
|
/**
|
|
111
116
|
* Runs the `onEventCallback` for all plugins.
|
|
112
117
|
*/
|
|
113
|
-
async runOnEventCallback({
|
|
118
|
+
async runOnEventCallback({
|
|
119
|
+
invocationContext,
|
|
120
|
+
event
|
|
121
|
+
}) {
|
|
114
122
|
return await this.runCallbacks(
|
|
115
123
|
this.plugins,
|
|
116
124
|
(plugin) => plugin.onEventCallback({ invocationContext, event }),
|
|
@@ -120,7 +128,10 @@ class PluginManager {
|
|
|
120
128
|
/**
|
|
121
129
|
* Runs the `beforeAgentCallback` for all plugins.
|
|
122
130
|
*/
|
|
123
|
-
async runBeforeAgentCallback({
|
|
131
|
+
async runBeforeAgentCallback({
|
|
132
|
+
agent,
|
|
133
|
+
callbackContext
|
|
134
|
+
}) {
|
|
124
135
|
return await this.runCallbacks(
|
|
125
136
|
this.plugins,
|
|
126
137
|
(plugin) => plugin.beforeAgentCallback({ agent, callbackContext }),
|
|
@@ -130,7 +141,10 @@ class PluginManager {
|
|
|
130
141
|
/**
|
|
131
142
|
* Runs the `afterAgentCallback` for all plugins.
|
|
132
143
|
*/
|
|
133
|
-
async runAfterAgentCallback({
|
|
144
|
+
async runAfterAgentCallback({
|
|
145
|
+
agent,
|
|
146
|
+
callbackContext
|
|
147
|
+
}) {
|
|
134
148
|
return await this.runCallbacks(
|
|
135
149
|
this.plugins,
|
|
136
150
|
(plugin) => plugin.afterAgentCallback({ agent, callbackContext }),
|
|
@@ -140,7 +154,11 @@ class PluginManager {
|
|
|
140
154
|
/**
|
|
141
155
|
* Runs the `beforeToolCallback` for all plugins.
|
|
142
156
|
*/
|
|
143
|
-
async runBeforeToolCallback({
|
|
157
|
+
async runBeforeToolCallback({
|
|
158
|
+
tool,
|
|
159
|
+
toolArgs,
|
|
160
|
+
toolContext
|
|
161
|
+
}) {
|
|
144
162
|
return await this.runCallbacks(
|
|
145
163
|
this.plugins,
|
|
146
164
|
(plugin) => plugin.beforeToolCallback({ tool, toolArgs, toolContext }),
|
|
@@ -150,31 +168,39 @@ class PluginManager {
|
|
|
150
168
|
/**
|
|
151
169
|
* Runs the `afterToolCallback` for all plugins.
|
|
152
170
|
*/
|
|
153
|
-
async runAfterToolCallback({
|
|
171
|
+
async runAfterToolCallback({
|
|
172
|
+
tool,
|
|
173
|
+
toolArgs,
|
|
174
|
+
toolContext,
|
|
175
|
+
result
|
|
176
|
+
}) {
|
|
154
177
|
return await this.runCallbacks(
|
|
155
178
|
this.plugins,
|
|
156
|
-
(plugin) => plugin.afterToolCallback(
|
|
157
|
-
{ tool, toolArgs, toolContext, result }
|
|
158
|
-
),
|
|
179
|
+
(plugin) => plugin.afterToolCallback({ tool, toolArgs, toolContext, result }),
|
|
159
180
|
"afterToolCallback"
|
|
160
181
|
);
|
|
161
182
|
}
|
|
162
183
|
/**
|
|
163
184
|
* Runs the `onModelErrorCallback` for all plugins.
|
|
164
185
|
*/
|
|
165
|
-
async runOnModelErrorCallback({
|
|
186
|
+
async runOnModelErrorCallback({
|
|
187
|
+
callbackContext,
|
|
188
|
+
llmRequest,
|
|
189
|
+
error
|
|
190
|
+
}) {
|
|
166
191
|
return await this.runCallbacks(
|
|
167
192
|
this.plugins,
|
|
168
|
-
(plugin) => plugin.onModelErrorCallback(
|
|
169
|
-
{ callbackContext, llmRequest, error }
|
|
170
|
-
),
|
|
193
|
+
(plugin) => plugin.onModelErrorCallback({ callbackContext, llmRequest, error }),
|
|
171
194
|
"onModelErrorCallback"
|
|
172
195
|
);
|
|
173
196
|
}
|
|
174
197
|
/**
|
|
175
198
|
* Runs the `beforeModelCallback` for all plugins.
|
|
176
199
|
*/
|
|
177
|
-
async runBeforeModelCallback({
|
|
200
|
+
async runBeforeModelCallback({
|
|
201
|
+
callbackContext,
|
|
202
|
+
llmRequest
|
|
203
|
+
}) {
|
|
178
204
|
return await this.runCallbacks(
|
|
179
205
|
this.plugins,
|
|
180
206
|
(plugin) => plugin.beforeModelCallback({ callbackContext, llmRequest }),
|
|
@@ -184,7 +210,10 @@ class PluginManager {
|
|
|
184
210
|
/**
|
|
185
211
|
* Runs the `afterModelCallback` for all plugins.
|
|
186
212
|
*/
|
|
187
|
-
async runAfterModelCallback({
|
|
213
|
+
async runAfterModelCallback({
|
|
214
|
+
callbackContext,
|
|
215
|
+
llmResponse
|
|
216
|
+
}) {
|
|
188
217
|
return await this.runCallbacks(
|
|
189
218
|
this.plugins,
|
|
190
219
|
(plugin) => plugin.afterModelCallback({ callbackContext, llmResponse }),
|
|
@@ -194,12 +223,15 @@ class PluginManager {
|
|
|
194
223
|
/**
|
|
195
224
|
* Runs the `onToolErrorCallback` for all plugins.
|
|
196
225
|
*/
|
|
197
|
-
async runOnToolErrorCallback({
|
|
226
|
+
async runOnToolErrorCallback({
|
|
227
|
+
tool,
|
|
228
|
+
toolArgs,
|
|
229
|
+
toolContext,
|
|
230
|
+
error
|
|
231
|
+
}) {
|
|
198
232
|
return await this.runCallbacks(
|
|
199
233
|
this.plugins,
|
|
200
|
-
(plugin) => plugin.onToolErrorCallback(
|
|
201
|
-
{ tool, toolArgs, toolContext, error }
|
|
202
|
-
),
|
|
234
|
+
(plugin) => plugin.onToolErrorCallback({ tool, toolArgs, toolContext, error }),
|
|
203
235
|
"onToolErrorCallback"
|
|
204
236
|
);
|
|
205
237
|
}
|
|
@@ -14,7 +14,7 @@ var PolicyOutcome = /* @__PURE__ */ ((PolicyOutcome2) => {
|
|
|
14
14
|
return PolicyOutcome2;
|
|
15
15
|
})(PolicyOutcome || {});
|
|
16
16
|
class InMemoryPolicyEngine {
|
|
17
|
-
async evaluate(
|
|
17
|
+
async evaluate() {
|
|
18
18
|
return Promise.resolve({
|
|
19
19
|
outcome: "ALLOW" /* ALLOW */,
|
|
20
20
|
reason: "For prototyping purpose, all tool calls are allowed."
|
|
@@ -79,7 +79,10 @@ class SecurityPlugin extends BasePlugin {
|
|
|
79
79
|
toolArgs,
|
|
80
80
|
toolContext
|
|
81
81
|
}) {
|
|
82
|
-
const policyCheckResult = await this.policyEngine.evaluate({
|
|
82
|
+
const policyCheckResult = await this.policyEngine.evaluate({
|
|
83
|
+
tool,
|
|
84
|
+
toolArgs
|
|
85
|
+
});
|
|
83
86
|
this.setToolCallCheckState(toolContext, policyCheckResult.outcome);
|
|
84
87
|
switch (policyCheckResult.outcome) {
|
|
85
88
|
case "DENY" /* DENY */:
|