@google/adk 0.1.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/LICENSE +202 -0
- package/README.md +9 -0
- package/dist/cjs/agents/active_streaming_tool.js +44 -0
- package/dist/cjs/agents/base_agent.js +245 -0
- package/dist/cjs/agents/base_llm_processor.js +44 -0
- package/dist/cjs/agents/callback_context.js +98 -0
- package/dist/cjs/agents/content_processor_utils.js +299 -0
- package/dist/cjs/agents/functions.js +394 -0
- package/dist/cjs/agents/instructions.js +110 -0
- package/dist/cjs/agents/invocation_context.js +109 -0
- package/dist/cjs/agents/live_request_queue.js +136 -0
- package/dist/cjs/agents/llm_agent.js +859 -0
- package/dist/cjs/agents/loop_agent.js +68 -0
- package/dist/cjs/agents/parallel_agent.js +78 -0
- package/dist/cjs/agents/readonly_context.js +68 -0
- package/dist/cjs/agents/run_config.js +74 -0
- package/dist/cjs/agents/sequential_agent.js +84 -0
- package/dist/cjs/agents/transcription_entry.js +27 -0
- package/dist/cjs/artifacts/base_artifact_service.js +27 -0
- package/dist/cjs/artifacts/in_memory_artifact_service.js +119 -0
- package/dist/cjs/auth/auth_credential.js +46 -0
- package/dist/cjs/auth/auth_handler.js +92 -0
- package/dist/cjs/auth/auth_schemes.js +62 -0
- package/dist/cjs/auth/auth_tool.js +27 -0
- package/dist/cjs/auth/credential_service/base_credential_service.js +27 -0
- package/dist/cjs/auth/credential_service/in_memory_credential_service.js +63 -0
- package/dist/cjs/code_executors/base_code_executor.js +76 -0
- package/dist/cjs/code_executors/built_in_code_executor.js +58 -0
- package/dist/cjs/code_executors/code_execution_utils.js +142 -0
- package/dist/cjs/code_executors/code_executor_context.js +198 -0
- package/dist/cjs/common.js +161 -0
- package/dist/cjs/events/event.js +107 -0
- package/dist/cjs/events/event_actions.js +83 -0
- package/dist/cjs/examples/base_example_provider.js +40 -0
- package/dist/cjs/examples/example.js +27 -0
- package/dist/cjs/examples/example_util.js +107 -0
- package/dist/cjs/index.js +40 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/index_web.js +33 -0
- package/dist/cjs/memory/base_memory_service.js +27 -0
- package/dist/cjs/memory/in_memory_memory_service.js +97 -0
- package/dist/cjs/memory/memory_entry.js +27 -0
- package/dist/cjs/models/base_llm.js +77 -0
- package/dist/cjs/models/base_llm_connection.js +27 -0
- package/dist/cjs/models/gemini_llm_connection.js +132 -0
- package/dist/cjs/models/google_llm.js +321 -0
- package/dist/cjs/models/llm_request.js +82 -0
- package/dist/cjs/models/llm_response.js +71 -0
- package/dist/cjs/models/registry.js +121 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/plugins/base_plugin.js +236 -0
- package/dist/cjs/plugins/logging_plugin.js +222 -0
- package/dist/cjs/plugins/plugin_manager.js +239 -0
- package/dist/cjs/plugins/security_plugin.js +153 -0
- package/dist/cjs/runner/in_memory_runner.js +58 -0
- package/dist/cjs/runner/runner.js +276 -0
- package/dist/cjs/sessions/base_session_service.js +71 -0
- package/dist/cjs/sessions/in_memory_session_service.js +184 -0
- package/dist/cjs/sessions/session.js +48 -0
- package/dist/cjs/sessions/state.js +101 -0
- package/dist/cjs/tools/agent_tool.js +134 -0
- package/dist/cjs/tools/base_tool.js +107 -0
- package/dist/cjs/tools/base_toolset.js +76 -0
- package/dist/cjs/tools/forwarding_artifact_service.js +71 -0
- package/dist/cjs/tools/function_tool.js +101 -0
- package/dist/cjs/tools/google_search_tool.js +76 -0
- package/dist/cjs/tools/long_running_tool.js +63 -0
- package/dist/cjs/tools/mcp/mcp_session_manager.js +65 -0
- package/dist/cjs/tools/mcp/mcp_tool.js +65 -0
- package/dist/cjs/tools/mcp/mcp_toolset.js +61 -0
- package/dist/cjs/tools/tool_confirmation.js +49 -0
- package/dist/cjs/tools/tool_context.js +129 -0
- package/dist/cjs/utils/deep_clone.js +44 -0
- package/dist/cjs/utils/env_aware_utils.js +83 -0
- package/dist/cjs/utils/gemini_schema_util.js +88 -0
- package/dist/cjs/utils/logger.js +121 -0
- package/dist/cjs/utils/model_name.js +64 -0
- package/dist/cjs/utils/simple_zod_to_json.js +191 -0
- package/dist/cjs/utils/variant_utils.js +55 -0
- package/dist/cjs/version.js +39 -0
- package/dist/esm/agents/active_streaming_tool.js +14 -0
- package/dist/esm/agents/base_agent.js +214 -0
- package/dist/esm/agents/base_llm_processor.js +13 -0
- package/dist/esm/agents/callback_context.js +68 -0
- package/dist/esm/agents/content_processor_utils.js +268 -0
- package/dist/esm/agents/functions.js +353 -0
- package/dist/esm/agents/instructions.js +80 -0
- package/dist/esm/agents/invocation_context.js +78 -0
- package/dist/esm/agents/live_request_queue.js +106 -0
- package/dist/esm/agents/llm_agent.js +828 -0
- package/dist/esm/agents/loop_agent.js +38 -0
- package/dist/esm/agents/parallel_agent.js +48 -0
- package/dist/esm/agents/readonly_context.js +38 -0
- package/dist/esm/agents/run_config.js +43 -0
- package/dist/esm/agents/sequential_agent.js +54 -0
- package/dist/esm/agents/transcription_entry.js +5 -0
- package/dist/esm/artifacts/base_artifact_service.js +5 -0
- package/dist/esm/artifacts/in_memory_artifact_service.js +89 -0
- package/dist/esm/auth/auth_credential.js +16 -0
- package/dist/esm/auth/auth_handler.js +62 -0
- package/dist/esm/auth/auth_schemes.js +31 -0
- package/dist/esm/auth/auth_tool.js +5 -0
- package/dist/esm/auth/credential_service/base_credential_service.js +5 -0
- package/dist/esm/auth/credential_service/in_memory_credential_service.js +33 -0
- package/dist/esm/code_executors/base_code_executor.js +46 -0
- package/dist/esm/code_executors/built_in_code_executor.js +28 -0
- package/dist/esm/code_executors/code_execution_utils.js +108 -0
- package/dist/esm/code_executors/code_executor_context.js +168 -0
- package/dist/esm/common.js +85 -0
- package/dist/esm/events/event.js +72 -0
- package/dist/esm/events/event_actions.js +52 -0
- package/dist/esm/examples/base_example_provider.js +10 -0
- package/dist/esm/examples/example.js +5 -0
- package/dist/esm/examples/example_util.js +76 -0
- package/dist/esm/index.js +40 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/index_web.js +6 -0
- package/dist/esm/memory/base_memory_service.js +5 -0
- package/dist/esm/memory/in_memory_memory_service.js +67 -0
- package/dist/esm/memory/memory_entry.js +5 -0
- package/dist/esm/models/base_llm.js +47 -0
- package/dist/esm/models/base_llm_connection.js +5 -0
- package/dist/esm/models/gemini_llm_connection.js +102 -0
- package/dist/esm/models/google_llm.js +291 -0
- package/dist/esm/models/llm_request.js +50 -0
- package/dist/esm/models/llm_response.js +41 -0
- package/dist/esm/models/registry.js +91 -0
- package/dist/esm/plugins/base_plugin.js +206 -0
- package/dist/esm/plugins/logging_plugin.js +192 -0
- package/dist/esm/plugins/plugin_manager.js +209 -0
- package/dist/esm/plugins/security_plugin.js +119 -0
- package/dist/esm/runner/in_memory_runner.js +28 -0
- package/dist/esm/runner/runner.js +246 -0
- package/dist/esm/sessions/base_session_service.js +41 -0
- package/dist/esm/sessions/in_memory_session_service.js +154 -0
- package/dist/esm/sessions/session.js +18 -0
- package/dist/esm/sessions/state.js +71 -0
- package/dist/esm/tools/agent_tool.js +104 -0
- package/dist/esm/tools/base_tool.js +77 -0
- package/dist/esm/tools/base_toolset.js +46 -0
- package/dist/esm/tools/forwarding_artifact_service.js +41 -0
- package/dist/esm/tools/function_tool.js +71 -0
- package/dist/esm/tools/google_search_tool.js +46 -0
- package/dist/esm/tools/long_running_tool.js +33 -0
- package/dist/esm/tools/mcp/mcp_session_manager.js +35 -0
- package/dist/esm/tools/mcp/mcp_tool.js +35 -0
- package/dist/esm/tools/mcp/mcp_toolset.js +31 -0
- package/dist/esm/tools/tool_confirmation.js +19 -0
- package/dist/esm/tools/tool_context.js +99 -0
- package/dist/esm/utils/deep_clone.js +14 -0
- package/dist/esm/utils/env_aware_utils.js +49 -0
- package/dist/esm/utils/gemini_schema_util.js +58 -0
- package/dist/esm/utils/logger.js +89 -0
- package/dist/esm/utils/model_name.js +31 -0
- package/dist/esm/utils/simple_zod_to_json.js +160 -0
- package/dist/esm/utils/variant_utils.js +24 -0
- package/dist/esm/version.js +9 -0
- package/dist/types/agents/active_streaming_tool.d.ts +29 -0
- package/dist/types/agents/base_agent.d.ts +167 -0
- package/dist/types/agents/base_llm_processor.d.ts +27 -0
- package/dist/types/agents/callback_context.d.ts +42 -0
- package/dist/types/agents/content_processor_utils.d.ts +36 -0
- package/dist/types/agents/functions.d.ts +90 -0
- package/dist/types/agents/instructions.d.ts +32 -0
- package/dist/types/agents/invocation_context.d.ts +155 -0
- package/dist/types/agents/live_request_queue.d.ts +67 -0
- package/dist/types/agents/llm_agent.d.ts +333 -0
- package/dist/types/agents/loop_agent.d.ts +31 -0
- package/dist/types/agents/parallel_agent.d.ts +21 -0
- package/dist/types/agents/readonly_context.d.ts +31 -0
- package/dist/types/agents/run_config.d.ts +76 -0
- package/dist/types/agents/sequential_agent.d.ts +26 -0
- package/dist/types/agents/transcription_entry.d.ts +17 -0
- package/dist/types/artifacts/base_artifact_service.d.ts +127 -0
- package/dist/types/artifacts/in_memory_artifact_service.d.ts +18 -0
- package/dist/types/auth/auth_credential.d.ts +227 -0
- package/dist/types/auth/auth_handler.d.ts +27 -0
- package/dist/types/auth/auth_schemes.d.ts +36 -0
- package/dist/types/auth/auth_tool.d.ts +51 -0
- package/dist/types/auth/credential_service/base_credential_service.d.ts +27 -0
- package/dist/types/auth/credential_service/in_memory_credential_service.d.ts +19 -0
- package/dist/types/code_executors/base_code_executor.d.ts +60 -0
- package/dist/types/code_executors/built_in_code_executor.d.ts +13 -0
- package/dist/types/code_executors/code_execution_utils.d.ts +99 -0
- package/dist/types/code_executors/code_executor_context.d.ts +92 -0
- package/dist/types/common.d.ts +51 -0
- package/dist/types/events/event.d.ts +81 -0
- package/dist/types/events/event_actions.d.ts +74 -0
- package/dist/types/examples/base_example_provider.d.ts +20 -0
- package/dist/types/examples/example.d.ts +19 -0
- package/dist/types/examples/example_util.d.ts +13 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index_web.d.ts +6 -0
- package/dist/types/memory/base_memory_service.d.ts +47 -0
- package/dist/types/memory/in_memory_memory_service.d.ts +18 -0
- package/dist/types/memory/memory_entry.d.ts +24 -0
- package/dist/types/models/base_llm.d.ts +46 -0
- package/dist/types/models/base_llm_connection.d.ts +51 -0
- package/dist/types/models/gemini_llm_connection.d.ts +54 -0
- package/dist/types/models/google_llm.d.ts +88 -0
- package/dist/types/models/llm_request.d.ts +49 -0
- package/dist/types/models/llm_response.d.ts +79 -0
- package/dist/types/models/registry.d.ts +45 -0
- package/dist/types/plugins/base_plugin.d.ts +310 -0
- package/dist/types/plugins/logging_plugin.d.ts +104 -0
- package/dist/types/plugins/plugin_manager.d.ts +155 -0
- package/dist/types/plugins/security_plugin.d.ts +60 -0
- package/dist/types/runner/in_memory_runner.d.ts +15 -0
- package/dist/types/runner/runner.d.ts +80 -0
- package/dist/types/sessions/base_session_service.d.ts +129 -0
- package/dist/types/sessions/in_memory_session_service.d.ts +32 -0
- package/dist/types/sessions/session.d.ts +46 -0
- package/dist/types/sessions/state.d.ts +57 -0
- package/dist/types/tools/agent_tool.d.ts +37 -0
- package/dist/types/tools/base_tool.d.ts +84 -0
- package/dist/types/tools/base_toolset.d.ts +64 -0
- package/dist/types/tools/forwarding_artifact_service.d.ts +21 -0
- package/dist/types/tools/function_tool.d.ts +48 -0
- package/dist/types/tools/google_search_tool.d.ts +18 -0
- package/dist/types/tools/long_running_tool.d.ts +18 -0
- package/dist/types/tools/mcp/mcp_session_manager.d.ts +57 -0
- package/dist/types/tools/mcp/mcp_tool.d.ts +30 -0
- package/dist/types/tools/mcp/mcp_toolset.d.ts +39 -0
- package/dist/types/tools/tool_confirmation.d.ts +25 -0
- package/dist/types/tools/tool_context.d.ts +63 -0
- package/dist/types/utils/deep_clone.d.ts +1 -0
- package/dist/types/utils/env_aware_utils.d.ts +31 -0
- package/dist/types/utils/gemini_schema_util.d.ts +23 -0
- package/dist/types/utils/logger.d.ts +41 -0
- package/dist/types/utils/model_name.d.ts +34 -0
- package/dist/types/utils/simple_zod_to_json.d.ts +12 -0
- package/dist/types/utils/variant_utils.d.ts +24 -0
- package/dist/types/version.d.ts +6 -0
- package/dist/web/agents/active_streaming_tool.js +14 -0
- package/dist/web/agents/base_agent.js +265 -0
- package/dist/web/agents/base_llm_processor.js +13 -0
- package/dist/web/agents/callback_context.js +68 -0
- package/dist/web/agents/content_processor_utils.js +268 -0
- package/dist/web/agents/functions.js +353 -0
- package/dist/web/agents/instructions.js +80 -0
- package/dist/web/agents/invocation_context.js +78 -0
- package/dist/web/agents/live_request_queue.js +124 -0
- package/dist/web/agents/llm_agent.js +973 -0
- package/dist/web/agents/loop_agent.js +71 -0
- package/dist/web/agents/parallel_agent.js +83 -0
- package/dist/web/agents/readonly_context.js +38 -0
- package/dist/web/agents/run_config.js +43 -0
- package/dist/web/agents/sequential_agent.js +99 -0
- package/dist/web/agents/transcription_entry.js +5 -0
- package/dist/web/artifacts/base_artifact_service.js +5 -0
- package/dist/web/artifacts/in_memory_artifact_service.js +89 -0
- package/dist/web/auth/auth_credential.js +16 -0
- package/dist/web/auth/auth_handler.js +62 -0
- package/dist/web/auth/auth_schemes.js +31 -0
- package/dist/web/auth/auth_tool.js +5 -0
- package/dist/web/auth/credential_service/base_credential_service.js +5 -0
- package/dist/web/auth/credential_service/in_memory_credential_service.js +33 -0
- package/dist/web/code_executors/base_code_executor.js +46 -0
- package/dist/web/code_executors/built_in_code_executor.js +28 -0
- package/dist/web/code_executors/code_execution_utils.js +105 -0
- package/dist/web/code_executors/code_executor_context.js +168 -0
- package/dist/web/common.js +85 -0
- package/dist/web/events/event.js +90 -0
- package/dist/web/events/event_actions.js +67 -0
- package/dist/web/examples/base_example_provider.js +10 -0
- package/dist/web/examples/example.js +5 -0
- package/dist/web/examples/example_util.js +75 -0
- package/dist/web/index.js +13 -0
- package/dist/web/index.js.map +7 -0
- package/dist/web/index_web.js +6 -0
- package/dist/web/memory/base_memory_service.js +5 -0
- package/dist/web/memory/in_memory_memory_service.js +67 -0
- package/dist/web/memory/memory_entry.js +5 -0
- package/dist/web/models/base_llm.js +47 -0
- package/dist/web/models/base_llm_connection.js +5 -0
- package/dist/web/models/gemini_llm_connection.js +120 -0
- package/dist/web/models/google_llm.js +332 -0
- package/dist/web/models/llm_request.js +50 -0
- package/dist/web/models/llm_response.js +41 -0
- package/dist/web/models/registry.js +91 -0
- package/dist/web/plugins/base_plugin.js +206 -0
- package/dist/web/plugins/logging_plugin.js +192 -0
- package/dist/web/plugins/plugin_manager.js +209 -0
- package/dist/web/plugins/security_plugin.js +119 -0
- package/dist/web/runner/in_memory_runner.js +28 -0
- package/dist/web/runner/runner.js +277 -0
- package/dist/web/sessions/base_session_service.js +41 -0
- package/dist/web/sessions/in_memory_session_service.js +154 -0
- package/dist/web/sessions/session.js +18 -0
- package/dist/web/sessions/state.js +87 -0
- package/dist/web/tools/agent_tool.js +118 -0
- package/dist/web/tools/base_tool.js +77 -0
- package/dist/web/tools/base_toolset.js +46 -0
- package/dist/web/tools/forwarding_artifact_service.js +41 -0
- package/dist/web/tools/function_tool.js +71 -0
- package/dist/web/tools/google_search_tool.js +46 -0
- package/dist/web/tools/long_running_tool.js +50 -0
- package/dist/web/tools/mcp/mcp_session_manager.js +35 -0
- package/dist/web/tools/mcp/mcp_tool.js +35 -0
- package/dist/web/tools/mcp/mcp_toolset.js +31 -0
- package/dist/web/tools/tool_confirmation.js +19 -0
- package/dist/web/tools/tool_context.js +99 -0
- package/dist/web/utils/deep_clone.js +14 -0
- package/dist/web/utils/env_aware_utils.js +49 -0
- package/dist/web/utils/gemini_schema_util.js +58 -0
- package/dist/web/utils/logger.js +89 -0
- package/dist/web/utils/model_name.js +31 -0
- package/dist/web/utils/simple_zod_to_json.js +174 -0
- package/dist/web/utils/variant_utils.js +24 -0
- package/dist/web/version.js +9 -0
- package/package.json +61 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { logger } from "../utils/logger.js";
|
|
7
|
+
import { Gemini } from "./google_llm.js";
|
|
8
|
+
class LRUCache {
|
|
9
|
+
constructor(maxSize) {
|
|
10
|
+
this.maxSize = maxSize;
|
|
11
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
12
|
+
}
|
|
13
|
+
get(key) {
|
|
14
|
+
const item = this.cache.get(key);
|
|
15
|
+
if (item) {
|
|
16
|
+
this.cache.delete(key);
|
|
17
|
+
this.cache.set(key, item);
|
|
18
|
+
}
|
|
19
|
+
return item;
|
|
20
|
+
}
|
|
21
|
+
set(key, value) {
|
|
22
|
+
if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
|
|
23
|
+
const lruKey = this.cache.keys().next().value;
|
|
24
|
+
if (lruKey !== void 0) {
|
|
25
|
+
this.cache.delete(lruKey);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
this.cache.set(key, value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const _LLMRegistry = class _LLMRegistry {
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new LLM instance.
|
|
34
|
+
* @param model The model name.
|
|
35
|
+
* @returns The LLM instance.
|
|
36
|
+
*/
|
|
37
|
+
static newLlm(model) {
|
|
38
|
+
return new (_LLMRegistry.resolve(model))(model);
|
|
39
|
+
}
|
|
40
|
+
static _register(modelNameRegex, llmCls) {
|
|
41
|
+
if (_LLMRegistry.llmRegistryDict.has(modelNameRegex)) {
|
|
42
|
+
logger.info(
|
|
43
|
+
"Updating LLM class for ".concat(modelNameRegex, " from ").concat(_LLMRegistry.llmRegistryDict.get(modelNameRegex), " to ").concat(llmCls)
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
_LLMRegistry.llmRegistryDict.set(modelNameRegex, llmCls);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Registers a new LLM class.
|
|
50
|
+
* @param llmCls The class that implements the model.
|
|
51
|
+
*/
|
|
52
|
+
static register(llmCls) {
|
|
53
|
+
for (const regex of llmCls.supportedModels) {
|
|
54
|
+
_LLMRegistry._register(regex, llmCls);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Resolves the model to a BaseLlm subclass.
|
|
59
|
+
* @param model The model name.
|
|
60
|
+
* @returns The BaseLlm subclass.
|
|
61
|
+
* @throws If the model is not found.
|
|
62
|
+
*/
|
|
63
|
+
static resolve(model) {
|
|
64
|
+
const cachedLlm = _LLMRegistry.resolveCache.get(model);
|
|
65
|
+
if (cachedLlm) {
|
|
66
|
+
return cachedLlm;
|
|
67
|
+
}
|
|
68
|
+
for (const [regex, llmClass] of _LLMRegistry.llmRegistryDict.entries()) {
|
|
69
|
+
const pattern = new RegExp(
|
|
70
|
+
"^".concat(regex instanceof RegExp ? regex.source : regex, "$"),
|
|
71
|
+
regex instanceof RegExp ? regex.flags : void 0
|
|
72
|
+
);
|
|
73
|
+
if (pattern.test(model)) {
|
|
74
|
+
_LLMRegistry.resolveCache.set(model, llmClass);
|
|
75
|
+
return llmClass;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
throw new Error("Model ".concat(model, " not found."));
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Key is the regex that matches the model name.
|
|
83
|
+
* Value is the class that implements the model.
|
|
84
|
+
*/
|
|
85
|
+
_LLMRegistry.llmRegistryDict = /* @__PURE__ */ new Map();
|
|
86
|
+
_LLMRegistry.resolveCache = new LRUCache(32);
|
|
87
|
+
let LLMRegistry = _LLMRegistry;
|
|
88
|
+
LLMRegistry.register(Gemini);
|
|
89
|
+
export {
|
|
90
|
+
LLMRegistry
|
|
91
|
+
};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
class BasePlugin {
|
|
7
|
+
/**
|
|
8
|
+
* Initializes the plugin.
|
|
9
|
+
*
|
|
10
|
+
* @param name A unique identifier for this plugin instance.
|
|
11
|
+
*/
|
|
12
|
+
constructor(name) {
|
|
13
|
+
this.name = name;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Callback executed when a user message is received before an invocation
|
|
17
|
+
* starts.
|
|
18
|
+
*
|
|
19
|
+
* This callback helps logging and modifying the user message before the
|
|
20
|
+
* runner starts the invocation.
|
|
21
|
+
*
|
|
22
|
+
* @param invocationContext The context for the entire invocation.
|
|
23
|
+
* @param userMessage The message content input by user.
|
|
24
|
+
* @returns An optional `Content` to be returned to the ADK. Returning a
|
|
25
|
+
* value to replace the user message. Returning `undefined` to proceed
|
|
26
|
+
* normally.
|
|
27
|
+
*/
|
|
28
|
+
async onUserMessageCallback({ invocationContext, userMessage }) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Callback executed before the ADK runner runs.
|
|
33
|
+
*
|
|
34
|
+
* This is the first callback to be called in the lifecycle, ideal for global
|
|
35
|
+
* setup or initialization tasks.
|
|
36
|
+
*
|
|
37
|
+
* @param invocationContext The context for the entire invocation, containing
|
|
38
|
+
* session information, the root agent, etc.
|
|
39
|
+
* @returns An optional `Event` to be returned to the ADK. Returning a value
|
|
40
|
+
* to halt execution of the runner and ends the runner with that event.
|
|
41
|
+
* Return `undefined` to proceed normally.
|
|
42
|
+
*/
|
|
43
|
+
async beforeRunCallback({ invocationContext }) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Callback executed after an event is yielded from runner.
|
|
48
|
+
*
|
|
49
|
+
* This is the ideal place to make modification to the event before the event
|
|
50
|
+
* is handled by the underlying agent app.
|
|
51
|
+
*
|
|
52
|
+
* @param invocationContext The context for the entire invocation.
|
|
53
|
+
* @param event The event raised by the runner.
|
|
54
|
+
* @returns An optional value. A non-`undefined` return may be used by the
|
|
55
|
+
* framework to modify or replace the response. Returning `undefined`
|
|
56
|
+
* allows the original response to be used.
|
|
57
|
+
*/
|
|
58
|
+
async onEventCallback({ invocationContext, event }) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Callback executed after an ADK runner run has completed.
|
|
63
|
+
*
|
|
64
|
+
* This is the final callback in the ADK lifecycle, suitable for cleanup,
|
|
65
|
+
* final logging, or reporting tasks.
|
|
66
|
+
*
|
|
67
|
+
* @param invocationContext The context for the entire invocation.
|
|
68
|
+
* @returns undefined
|
|
69
|
+
*/
|
|
70
|
+
async afterRunCallback({ invocationContext }) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Callback executed before an agent's primary logic is invoked.
|
|
75
|
+
*
|
|
76
|
+
* This callback can be used for logging, setup, or to short-circuit the
|
|
77
|
+
* agent's execution by returning a value.
|
|
78
|
+
*
|
|
79
|
+
* @param agent The agent that is about to run.
|
|
80
|
+
* @param callbackContext The context for the agent invocation.
|
|
81
|
+
* @returns An optional `Content` object. If a value is returned, it will
|
|
82
|
+
* bypass the agent's callbacks and its execution, and return this value
|
|
83
|
+
* directly. Returning `undefined` allows the agent to proceed normally.
|
|
84
|
+
*/
|
|
85
|
+
async beforeAgentCallback({ agent, callbackContext }) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Callback executed after an agent's primary logic has completed.
|
|
90
|
+
*
|
|
91
|
+
* This callback can be used to inspect, log, or modify the agent's final
|
|
92
|
+
* result before it is returned.
|
|
93
|
+
*
|
|
94
|
+
* @param agent The agent that has just run.
|
|
95
|
+
* @param callbackContext The context for the agent invocation.
|
|
96
|
+
* @returns An optional `Content` object. If a value is returned, it will
|
|
97
|
+
* replace the agent's original result. Returning `undefined` uses the
|
|
98
|
+
* original, unmodified result.
|
|
99
|
+
*/
|
|
100
|
+
async afterAgentCallback({ agent, callbackContext }) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Callback executed before a request is sent to the model.
|
|
105
|
+
*
|
|
106
|
+
* This provides an opportunity to inspect, log, or modify the `LlmRequest`
|
|
107
|
+
* object. It can also be used to implement caching by returning a cached
|
|
108
|
+
* `LlmResponse`, which would skip the actual model call.
|
|
109
|
+
*
|
|
110
|
+
* @param callbackContext The context for the current agent call.
|
|
111
|
+
* @param llmRequest The prepared request object to be sent to the model.
|
|
112
|
+
* @returns An optional value. The interpretation of a non-`undefined`
|
|
113
|
+
* trigger an early exit and returns the response immediately. Returning
|
|
114
|
+
* `undefined` allows the LLM request to proceed normally.
|
|
115
|
+
*/
|
|
116
|
+
async beforeModelCallback({ callbackContext, llmRequest }) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Callback executed after a response is received from the model.
|
|
121
|
+
*
|
|
122
|
+
* This is the ideal place to log model responses, collect metrics on token
|
|
123
|
+
* usage, or perform post-processing on the raw `LlmResponse`.
|
|
124
|
+
*
|
|
125
|
+
* @param callbackContext The context for the current agent call.
|
|
126
|
+
* @param llmResponse The response object received from the model.
|
|
127
|
+
* @returns An optional value. A non-`undefined` return may be used by the
|
|
128
|
+
* framework to modify or replace the response. Returning `undefined`
|
|
129
|
+
* allows the original response to be used.
|
|
130
|
+
*/
|
|
131
|
+
async afterModelCallback({ callbackContext, llmResponse }) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Callback executed when a model call encounters an error.
|
|
136
|
+
*
|
|
137
|
+
* This callback provides an opportunity to handle model errors gracefully,
|
|
138
|
+
* potentially providing alternative responses or recovery mechanisms.
|
|
139
|
+
*
|
|
140
|
+
* @param callbackContext The context for the current agent call.
|
|
141
|
+
* @param llmRequest The request that was sent to the model when the error
|
|
142
|
+
* occurred.
|
|
143
|
+
* @param error The exception that was raised during model execution.
|
|
144
|
+
* @returns An optional LlmResponse. If an LlmResponse is returned, it will be
|
|
145
|
+
* used instead of propagating the error. Returning `undefined` allows
|
|
146
|
+
* the original error to be raised.
|
|
147
|
+
*/
|
|
148
|
+
async onModelErrorCallback({ callbackContext, llmRequest, error }) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Callback executed before a tool is called.
|
|
153
|
+
*
|
|
154
|
+
* This callback is useful for logging tool usage, input validation, or
|
|
155
|
+
* modifying the arguments before they are passed to the tool.
|
|
156
|
+
*
|
|
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
|
|
159
|
+
* tool.
|
|
160
|
+
* @param toolContext The context specific to the tool execution.
|
|
161
|
+
* @returns An optional dictionary. If a dictionary is returned, it will stop
|
|
162
|
+
* the tool execution and return this response immediately. Returning
|
|
163
|
+
* `undefined` uses the original, unmodified arguments.
|
|
164
|
+
*/
|
|
165
|
+
async beforeToolCallback({ tool, toolArgs, toolContext }) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Callback executed after a tool has been called.
|
|
170
|
+
*
|
|
171
|
+
* This callback allows for inspecting, logging, or modifying the result
|
|
172
|
+
* returned by a tool.
|
|
173
|
+
*
|
|
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.
|
|
178
|
+
* @returns An optional dictionary. If a dictionary is returned, it will
|
|
179
|
+
* **replace** the original result from the tool. This allows for
|
|
180
|
+
* post-processing or altering tool outputs. Returning `undefined` uses
|
|
181
|
+
* the original, unmodified result.
|
|
182
|
+
*/
|
|
183
|
+
async afterToolCallback({ tool, toolArgs, toolContext, result }) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Callback executed when a tool call encounters an error.
|
|
188
|
+
*
|
|
189
|
+
* This callback provides an opportunity to handle tool errors gracefully,
|
|
190
|
+
* potentially providing alternative responses or recovery mechanisms.
|
|
191
|
+
*
|
|
192
|
+
* @param tool The tool instance that encountered an error.
|
|
193
|
+
* @param toolArgs The arguments that were passed to the tool.
|
|
194
|
+
* @param toolContext The context specific to the tool execution.
|
|
195
|
+
* @param error The exception that was raised during tool execution.
|
|
196
|
+
* @returns An optional dictionary. If a dictionary is returned, it will be
|
|
197
|
+
* used as the tool response instead of propagating the error. Returning
|
|
198
|
+
* `undefined` allows the original error to be raised.
|
|
199
|
+
*/
|
|
200
|
+
async onToolErrorCallback({ tool, toolArgs, toolContext, error }) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
export {
|
|
205
|
+
BasePlugin
|
|
206
|
+
};
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { getFunctionCalls, getFunctionResponses, isFinalResponse } from "../events/event.js";
|
|
7
|
+
import { logger } from "../utils/logger.js";
|
|
8
|
+
import { BasePlugin } from "./base_plugin.js";
|
|
9
|
+
class LoggingPlugin extends BasePlugin {
|
|
10
|
+
/**
|
|
11
|
+
* Initialize the logging plugin.
|
|
12
|
+
*
|
|
13
|
+
* @param name The name of the plugin instance.
|
|
14
|
+
*/
|
|
15
|
+
constructor(name = "logging_plugin") {
|
|
16
|
+
super(name);
|
|
17
|
+
}
|
|
18
|
+
async onUserMessageCallback({ invocationContext, userMessage }) {
|
|
19
|
+
var _a;
|
|
20
|
+
this.log("\u{1F680} USER MESSAGE RECEIVED");
|
|
21
|
+
this.log(" Invocation ID: ".concat(invocationContext.invocationId));
|
|
22
|
+
this.log(" Session ID: ".concat(invocationContext.session.id));
|
|
23
|
+
this.log(" User ID: ".concat(invocationContext.userId));
|
|
24
|
+
this.log(" App Name: ".concat(invocationContext.appName));
|
|
25
|
+
this.log(" Root Agent: ".concat((_a = invocationContext.agent.name) != null ? _a : "Unknown"));
|
|
26
|
+
this.log(" User Content: ".concat(this.formatContent(userMessage)));
|
|
27
|
+
if (invocationContext.branch) {
|
|
28
|
+
this.log(" Branch: ".concat(invocationContext.branch));
|
|
29
|
+
}
|
|
30
|
+
return void 0;
|
|
31
|
+
}
|
|
32
|
+
async beforeRunCallback({ invocationContext }) {
|
|
33
|
+
var _a;
|
|
34
|
+
this.log("\u{1F3C3} INVOCATION STARTING");
|
|
35
|
+
this.log(" Invocation ID: ".concat(invocationContext.invocationId));
|
|
36
|
+
this.log(" Starting Agent: ".concat((_a = invocationContext.agent.name) != null ? _a : "Unknown"));
|
|
37
|
+
return void 0;
|
|
38
|
+
}
|
|
39
|
+
async onEventCallback({ invocationContext, event }) {
|
|
40
|
+
this.log("\u{1F4E2} EVENT YIELDED");
|
|
41
|
+
this.log(" Event ID: ".concat(event.id));
|
|
42
|
+
this.log(" Author: ".concat(event.author));
|
|
43
|
+
this.log(" Content: ".concat(this.formatContent(event.content)));
|
|
44
|
+
this.log(" Final Response: ".concat(isFinalResponse(event)));
|
|
45
|
+
const functionCalls = getFunctionCalls(event);
|
|
46
|
+
if (functionCalls.length > 0) {
|
|
47
|
+
const funcCalls = functionCalls.map((fc) => fc.name);
|
|
48
|
+
this.log(" Function Calls: ".concat(funcCalls));
|
|
49
|
+
}
|
|
50
|
+
const functionResponses = getFunctionResponses(event);
|
|
51
|
+
if (functionResponses.length > 0) {
|
|
52
|
+
const funcResponses = functionResponses.map((fr) => fr.name);
|
|
53
|
+
this.log(" Function Responses: ".concat(funcResponses));
|
|
54
|
+
}
|
|
55
|
+
if (event.longRunningToolIds && event.longRunningToolIds.length > 0) {
|
|
56
|
+
this.log(" Long Running Tools: ".concat([...event.longRunningToolIds]));
|
|
57
|
+
}
|
|
58
|
+
return void 0;
|
|
59
|
+
}
|
|
60
|
+
async afterRunCallback({ invocationContext }) {
|
|
61
|
+
var _a;
|
|
62
|
+
this.log("\u2705 INVOCATION COMPLETED");
|
|
63
|
+
this.log(" Invocation ID: ".concat(invocationContext.invocationId));
|
|
64
|
+
this.log(" Final Agent: ".concat((_a = invocationContext.agent.name) != null ? _a : "Unknown"));
|
|
65
|
+
return void 0;
|
|
66
|
+
}
|
|
67
|
+
async beforeAgentCallback({ agent, callbackContext }) {
|
|
68
|
+
this.log("\u{1F916} AGENT STARTING");
|
|
69
|
+
this.log(" Agent Name: ".concat(callbackContext.agentName));
|
|
70
|
+
this.log(" Invocation ID: ".concat(callbackContext.invocationId));
|
|
71
|
+
if (callbackContext.invocationContext.branch) {
|
|
72
|
+
this.log(" Branch: ".concat(callbackContext.invocationContext.branch));
|
|
73
|
+
}
|
|
74
|
+
return void 0;
|
|
75
|
+
}
|
|
76
|
+
async afterAgentCallback({ agent, callbackContext }) {
|
|
77
|
+
this.log("\u{1F916} AGENT COMPLETED");
|
|
78
|
+
this.log(" Agent Name: ".concat(callbackContext.agentName));
|
|
79
|
+
this.log(" Invocation ID: ".concat(callbackContext.invocationId));
|
|
80
|
+
return void 0;
|
|
81
|
+
}
|
|
82
|
+
async beforeModelCallback({ callbackContext, llmRequest }) {
|
|
83
|
+
var _a;
|
|
84
|
+
this.log("\u{1F9E0} LLM REQUEST");
|
|
85
|
+
this.log(" Model: ".concat((_a = llmRequest.model) != null ? _a : "default"));
|
|
86
|
+
this.log(" Agent: ".concat(callbackContext.agentName));
|
|
87
|
+
if (llmRequest.config && llmRequest.config.systemInstruction) {
|
|
88
|
+
let sysInstruction = llmRequest.config.systemInstruction;
|
|
89
|
+
if (sysInstruction.length > 200) {
|
|
90
|
+
sysInstruction = sysInstruction.substring(0, 200) + "...";
|
|
91
|
+
}
|
|
92
|
+
this.log(" System Instruction: '".concat(sysInstruction, "'"));
|
|
93
|
+
}
|
|
94
|
+
if (llmRequest.toolsDict) {
|
|
95
|
+
const toolNames = Object.keys(llmRequest.toolsDict);
|
|
96
|
+
this.log(" Available Tools: ".concat(toolNames));
|
|
97
|
+
}
|
|
98
|
+
return void 0;
|
|
99
|
+
}
|
|
100
|
+
async afterModelCallback({ callbackContext, llmResponse }) {
|
|
101
|
+
this.log("\u{1F9E0} LLM RESPONSE");
|
|
102
|
+
this.log(" Agent: ".concat(callbackContext.agentName));
|
|
103
|
+
if (llmResponse.errorCode) {
|
|
104
|
+
this.log(" \u274C ERROR - Code: ".concat(llmResponse.errorCode));
|
|
105
|
+
this.log(" Error Message: ".concat(llmResponse.errorMessage));
|
|
106
|
+
} else {
|
|
107
|
+
this.log(" Content: ".concat(this.formatContent(llmResponse.content)));
|
|
108
|
+
if (llmResponse.partial) {
|
|
109
|
+
this.log(" Partial: ".concat(llmResponse.partial));
|
|
110
|
+
}
|
|
111
|
+
if (llmResponse.turnComplete !== void 0) {
|
|
112
|
+
this.log(" Turn Complete: ".concat(llmResponse.turnComplete));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (llmResponse.usageMetadata) {
|
|
116
|
+
this.log(" Token Usage - Input: ".concat(llmResponse.usageMetadata.promptTokenCount, ", Output: ").concat(llmResponse.usageMetadata.candidatesTokenCount));
|
|
117
|
+
}
|
|
118
|
+
return void 0;
|
|
119
|
+
}
|
|
120
|
+
async beforeToolCallback({ tool, toolArgs, toolContext }) {
|
|
121
|
+
this.log("\u{1F527} TOOL STARTING");
|
|
122
|
+
this.log(" Tool Name: ".concat(tool.name));
|
|
123
|
+
this.log(" Agent: ".concat(toolContext.agentName));
|
|
124
|
+
this.log(" Function Call ID: ".concat(toolContext.functionCallId));
|
|
125
|
+
this.log(" Arguments: ".concat(this.formatArgs(toolArgs)));
|
|
126
|
+
return void 0;
|
|
127
|
+
}
|
|
128
|
+
async afterToolCallback({ tool, toolArgs, toolContext, result }) {
|
|
129
|
+
this.log("\u{1F527} TOOL COMPLETED");
|
|
130
|
+
this.log(" Tool Name: ".concat(tool.name));
|
|
131
|
+
this.log(" Agent: ".concat(toolContext.agentName));
|
|
132
|
+
this.log(" Function Call ID: ".concat(toolContext.functionCallId));
|
|
133
|
+
this.log(" Result: ".concat(this.formatArgs(result)));
|
|
134
|
+
return void 0;
|
|
135
|
+
}
|
|
136
|
+
async onModelErrorCallback({ callbackContext, llmRequest, error }) {
|
|
137
|
+
this.log("\u{1F9E0} LLM ERROR");
|
|
138
|
+
this.log(" Agent: ".concat(callbackContext.agentName));
|
|
139
|
+
this.log(" Error: ".concat(error));
|
|
140
|
+
return void 0;
|
|
141
|
+
}
|
|
142
|
+
async onToolErrorCallback({ tool, toolArgs, toolContext, error }) {
|
|
143
|
+
this.log("\u{1F527} TOOL ERROR");
|
|
144
|
+
this.log(" Tool Name: ".concat(tool.name));
|
|
145
|
+
this.log(" Agent: ".concat(toolContext.agentName));
|
|
146
|
+
this.log(" Function Call ID: ".concat(toolContext.functionCallId));
|
|
147
|
+
this.log(" Arguments: ".concat(this.formatArgs(toolArgs)));
|
|
148
|
+
this.log(" Error: ".concat(error));
|
|
149
|
+
return void 0;
|
|
150
|
+
}
|
|
151
|
+
log(message) {
|
|
152
|
+
const formattedMessage = "\x1B[90m[".concat(this.name, "] ").concat(message, "\x1B[0m");
|
|
153
|
+
logger.info(formattedMessage);
|
|
154
|
+
}
|
|
155
|
+
formatContent(content, maxLength = 200) {
|
|
156
|
+
if (!content || !content.parts) {
|
|
157
|
+
return "None";
|
|
158
|
+
}
|
|
159
|
+
const parts = [];
|
|
160
|
+
for (const part of content.parts) {
|
|
161
|
+
if (part.text) {
|
|
162
|
+
let text = part.text.trim();
|
|
163
|
+
if (text.length > maxLength) {
|
|
164
|
+
text = text.substring(0, maxLength) + "...";
|
|
165
|
+
}
|
|
166
|
+
parts.push("text: '".concat(text, "'"));
|
|
167
|
+
} else if (part.functionCall) {
|
|
168
|
+
parts.push("function_call: ".concat(part.functionCall.name));
|
|
169
|
+
} else if (part.functionResponse) {
|
|
170
|
+
parts.push("function_response: ".concat(part.functionResponse.name));
|
|
171
|
+
} else if (part.codeExecutionResult) {
|
|
172
|
+
parts.push("code_execution_result");
|
|
173
|
+
} else {
|
|
174
|
+
parts.push("other_part");
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return parts.join(" | ");
|
|
178
|
+
}
|
|
179
|
+
formatArgs(args, maxLength = 300) {
|
|
180
|
+
if (!args) {
|
|
181
|
+
return "{}";
|
|
182
|
+
}
|
|
183
|
+
let formatted = JSON.stringify(args);
|
|
184
|
+
if (formatted.length > maxLength) {
|
|
185
|
+
formatted = formatted.substring(0, maxLength) + "...}";
|
|
186
|
+
}
|
|
187
|
+
return formatted;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
export {
|
|
191
|
+
LoggingPlugin
|
|
192
|
+
};
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { logger } from "../utils/logger.js";
|
|
7
|
+
class PluginManager {
|
|
8
|
+
/**
|
|
9
|
+
* Initializes the plugin service.
|
|
10
|
+
*
|
|
11
|
+
* @param plugins An optional list of plugins to register upon
|
|
12
|
+
* initialization.
|
|
13
|
+
*/
|
|
14
|
+
constructor(plugins) {
|
|
15
|
+
this.plugins = /* @__PURE__ */ new Set();
|
|
16
|
+
if (plugins) {
|
|
17
|
+
for (const plugin of plugins) {
|
|
18
|
+
this.registerPlugin(plugin);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Registers a new plugin.
|
|
24
|
+
*
|
|
25
|
+
* @param plugin The plugin instance to register.
|
|
26
|
+
* @throws If the same exact plugin or a plugin with the same name is already
|
|
27
|
+
* registered.
|
|
28
|
+
*/
|
|
29
|
+
registerPlugin(plugin) {
|
|
30
|
+
if (this.plugins.has(plugin)) {
|
|
31
|
+
throw new Error("Plugin '".concat(plugin.name, "' already registered."));
|
|
32
|
+
}
|
|
33
|
+
if (Array.from(this.plugins).some((p) => p.name === plugin.name)) {
|
|
34
|
+
throw new Error("Plugin with name '".concat(plugin.name, "' already registered."));
|
|
35
|
+
}
|
|
36
|
+
this.plugins.add(plugin);
|
|
37
|
+
logger.info("Plugin '".concat(plugin.name, "' registered."));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Retrieves a registered plugin by its name.
|
|
41
|
+
*
|
|
42
|
+
* @param pluginName The name of the plugin to retrieve.
|
|
43
|
+
* @returns The plugin instance if found, otherwise `undefined`.
|
|
44
|
+
*/
|
|
45
|
+
getPlugin(pluginName) {
|
|
46
|
+
return Array.from(this.plugins).find((p) => p.name === pluginName);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Runs the same callback for all plugins. This is a utility method to reduce
|
|
50
|
+
* duplication below.
|
|
51
|
+
*
|
|
52
|
+
* @param plugins The set of plugins to run
|
|
53
|
+
* @param callback A closure containing the callback method to run on each
|
|
54
|
+
* plugin
|
|
55
|
+
* @param callbackName The name of the function being called in the closure
|
|
56
|
+
* above. Used for logging purposes.
|
|
57
|
+
* @returns A promise containing the plugin method result. Must be casted to
|
|
58
|
+
* the proper type for the plugin method.
|
|
59
|
+
*/
|
|
60
|
+
async runCallbacks(plugins, callback, callbackName) {
|
|
61
|
+
for (const plugin of plugins) {
|
|
62
|
+
try {
|
|
63
|
+
const result = await callback(plugin);
|
|
64
|
+
if (result !== void 0) {
|
|
65
|
+
logger.debug(
|
|
66
|
+
"Plugin '".concat(plugin.name, "' returned a value for callback '").concat(callbackName, "', exiting early.")
|
|
67
|
+
);
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
} catch (e) {
|
|
71
|
+
const errorMessage = "Error in plugin '".concat(plugin.name, "' during '").concat(callbackName, "' callback: ").concat(e);
|
|
72
|
+
logger.error(errorMessage);
|
|
73
|
+
throw new Error(errorMessage);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return void 0;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Runs the `onUserMessageCallback` for all plugins.
|
|
80
|
+
*/
|
|
81
|
+
async runOnUserMessageCallback({ userMessage, invocationContext }) {
|
|
82
|
+
return await this.runCallbacks(
|
|
83
|
+
this.plugins,
|
|
84
|
+
(plugin) => plugin.onUserMessageCallback(
|
|
85
|
+
{ userMessage, invocationContext }
|
|
86
|
+
),
|
|
87
|
+
"onUserMessageCallback"
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Runs the `beforeRunCallback` for all plugins.
|
|
92
|
+
*/
|
|
93
|
+
async runBeforeRunCallback({ invocationContext }) {
|
|
94
|
+
return await this.runCallbacks(
|
|
95
|
+
this.plugins,
|
|
96
|
+
(plugin) => plugin.beforeRunCallback({ invocationContext }),
|
|
97
|
+
"beforeRunCallback"
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Runs the `afterRunCallback` for all plugins.
|
|
102
|
+
*/
|
|
103
|
+
async runAfterRunCallback({ invocationContext }) {
|
|
104
|
+
await this.runCallbacks(
|
|
105
|
+
this.plugins,
|
|
106
|
+
(plugin) => plugin.afterRunCallback({ invocationContext }),
|
|
107
|
+
"afterRunCallback"
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Runs the `onEventCallback` for all plugins.
|
|
112
|
+
*/
|
|
113
|
+
async runOnEventCallback({ invocationContext, event }) {
|
|
114
|
+
return await this.runCallbacks(
|
|
115
|
+
this.plugins,
|
|
116
|
+
(plugin) => plugin.onEventCallback({ invocationContext, event }),
|
|
117
|
+
"onEventCallback"
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Runs the `beforeAgentCallback` for all plugins.
|
|
122
|
+
*/
|
|
123
|
+
async runBeforeAgentCallback({ agent, callbackContext }) {
|
|
124
|
+
return await this.runCallbacks(
|
|
125
|
+
this.plugins,
|
|
126
|
+
(plugin) => plugin.beforeAgentCallback({ agent, callbackContext }),
|
|
127
|
+
"beforeAgentCallback"
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Runs the `afterAgentCallback` for all plugins.
|
|
132
|
+
*/
|
|
133
|
+
async runAfterAgentCallback({ agent, callbackContext }) {
|
|
134
|
+
return await this.runCallbacks(
|
|
135
|
+
this.plugins,
|
|
136
|
+
(plugin) => plugin.afterAgentCallback({ agent, callbackContext }),
|
|
137
|
+
"afterAgentCallback"
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Runs the `beforeToolCallback` for all plugins.
|
|
142
|
+
*/
|
|
143
|
+
async runBeforeToolCallback({ tool, toolArgs, toolContext }) {
|
|
144
|
+
return await this.runCallbacks(
|
|
145
|
+
this.plugins,
|
|
146
|
+
(plugin) => plugin.beforeToolCallback({ tool, toolArgs, toolContext }),
|
|
147
|
+
"beforeToolCallback"
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Runs the `afterToolCallback` for all plugins.
|
|
152
|
+
*/
|
|
153
|
+
async runAfterToolCallback({ tool, toolArgs, toolContext, result }) {
|
|
154
|
+
return await this.runCallbacks(
|
|
155
|
+
this.plugins,
|
|
156
|
+
(plugin) => plugin.afterToolCallback(
|
|
157
|
+
{ tool, toolArgs, toolContext, result }
|
|
158
|
+
),
|
|
159
|
+
"afterToolCallback"
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Runs the `onModelErrorCallback` for all plugins.
|
|
164
|
+
*/
|
|
165
|
+
async runOnModelErrorCallback({ callbackContext, llmRequest, error }) {
|
|
166
|
+
return await this.runCallbacks(
|
|
167
|
+
this.plugins,
|
|
168
|
+
(plugin) => plugin.onModelErrorCallback(
|
|
169
|
+
{ callbackContext, llmRequest, error }
|
|
170
|
+
),
|
|
171
|
+
"onModelErrorCallback"
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Runs the `beforeModelCallback` for all plugins.
|
|
176
|
+
*/
|
|
177
|
+
async runBeforeModelCallback({ callbackContext, llmRequest }) {
|
|
178
|
+
return await this.runCallbacks(
|
|
179
|
+
this.plugins,
|
|
180
|
+
(plugin) => plugin.beforeModelCallback({ callbackContext, llmRequest }),
|
|
181
|
+
"beforeModelCallback"
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Runs the `afterModelCallback` for all plugins.
|
|
186
|
+
*/
|
|
187
|
+
async runAfterModelCallback({ callbackContext, llmResponse }) {
|
|
188
|
+
return await this.runCallbacks(
|
|
189
|
+
this.plugins,
|
|
190
|
+
(plugin) => plugin.afterModelCallback({ callbackContext, llmResponse }),
|
|
191
|
+
"afterModelCallback"
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Runs the `onToolErrorCallback` for all plugins.
|
|
196
|
+
*/
|
|
197
|
+
async runOnToolErrorCallback({ tool, toolArgs, toolContext, error }) {
|
|
198
|
+
return await this.runCallbacks(
|
|
199
|
+
this.plugins,
|
|
200
|
+
(plugin) => plugin.onToolErrorCallback(
|
|
201
|
+
{ tool, toolArgs, toolContext, error }
|
|
202
|
+
),
|
|
203
|
+
"onToolErrorCallback"
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
export {
|
|
208
|
+
PluginManager
|
|
209
|
+
};
|