/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { Content } from '@google/genai'; import { BaseAgent } from '../agents/base_agent.js'; import { Context } from '../agents/context.js'; import { InvocationContext } from '../agents/invocation_context.js'; import { Event } from '../events/event.js'; import { LlmRequest } from '../models/llm_request.js'; import { LlmResponse } from '../models/llm_response.js'; import { BaseTool } from '../tools/base_tool.js'; import type { BaseNode } from '../workflow/base_node.js'; import type { NodeContext } from '../workflow/node_context.js'; import { BasePlugin, ContextCompactionTrigger } from './base_plugin.js'; /** * Manages the registration and execution of plugins. * * The PluginManager is an internal class that orchestrates the invocation of * plugin callbacks at key points in the SDK's execution lifecycle. It maintains * a list of registered plugins and ensures they are called in the order they * were registered. * * The core execution logic implements an "early exit" strategy: if any plugin * callback returns a non-`undefined` value, the execution of subsequent plugins * for that specific event is halted, and the returned value is propagated up * the call stack. This allows plugins to short-circuit operations like agent * runs, tool calls, or model requests. */ export declare class PluginManager { private readonly plugins; /** Whether any plugin is registered. */ get hasPlugins(): boolean; /** * Initializes the plugin service. * * @param plugins An optional list of plugins to register upon * initialization. */ constructor(plugins?: BasePlugin[]); /** * Registers a new plugin. * * @param plugin The plugin instance to register. * @throws If the same exact plugin or a plugin with the same name is already * registered. */ registerPlugin(plugin: BasePlugin): void; /** * Retrieves a registered plugin by its name. * * @param pluginName The name of the plugin to retrieve. * @returns The plugin instance if found, otherwise `undefined`. */ getPlugin(pluginName: string): BasePlugin | undefined; /** * Runs the same callback for all plugins. This is a utility method to reduce * duplication below. * * @param plugins The set of plugins to run * @param callback A closure containing the callback method to run on each * plugin * @param callbackName The name of the function being called in the closure * above. Used for logging purposes. * @returns A promise containing the plugin method result. Must be casted to * the proper type for the plugin method. */ private runCallbacks; /** * Runs the `onUserMessageCallback` for all plugins. */ runOnUserMessageCallback({ userMessage, invocationContext, }: { userMessage: Content; invocationContext: InvocationContext; }): Promise; /** * Runs the `beforeRunCallback` for all plugins. */ runBeforeRunCallback({ invocationContext, }: { invocationContext: InvocationContext; }): Promise; /** * Runs the `afterRunCallback` for all plugins. */ runAfterRunCallback({ invocationContext, }: { invocationContext: InvocationContext; }): Promise; /** * Runs the `onEventCallback` for all plugins. */ runOnEventCallback({ invocationContext, event, }: { invocationContext: InvocationContext; event: Event; }): Promise; /** * Runs the `beforeAgentCallback` for all plugins. */ runBeforeAgentCallback({ agent, callbackContext, }: { agent: BaseAgent; callbackContext: Context; }): Promise; /** * Runs the `afterAgentCallback` for all plugins. */ runAfterAgentCallback({ agent, callbackContext, }: { agent: BaseAgent; callbackContext: Context; }): Promise; /** * Runs the `beforeNodeCallback` for all plugins. */ runBeforeNodeCallback({ node, nodeContext, input, }: { node: BaseNode; nodeContext: NodeContext; input: unknown; }): Promise; /** * Runs the `afterNodeCallback` for all plugins. */ runAfterNodeCallback({ node, nodeContext, output, }: { node: BaseNode; nodeContext: NodeContext; output: unknown; }): Promise; /** * Runs the `beforeToolSelection` for all plugins. */ runBeforeToolSelection({ callbackContext, tools, }: { callbackContext: Context; tools: Readonly>; }): Promise> | undefined>; /** * Runs the `beforeContextCompaction` for all plugins. */ runBeforeContextCompaction({ invocationContext, trigger, }: { invocationContext: InvocationContext; trigger: ContextCompactionTrigger; }): Promise; /** * Runs the `afterContextCompaction` for all plugins. */ runAfterContextCompaction({ invocationContext, trigger, }: { invocationContext: InvocationContext; trigger: ContextCompactionTrigger; }): Promise; /** * Runs the `beforeToolCallback` for all plugins. */ runBeforeToolCallback({ tool, toolArgs, toolContext, }: { tool: BaseTool; toolArgs: Record; toolContext: Context; }): Promise | undefined>; /** * Runs the `afterToolCallback` for all plugins. */ runAfterToolCallback({ tool, toolArgs, toolContext, result, }: { tool: BaseTool; toolArgs: Record; toolContext: Context; result: Record; }): Promise | undefined>; /** * Runs the `onModelErrorCallback` for all plugins. */ runOnModelErrorCallback({ callbackContext, llmRequest, error, }: { callbackContext: Context; llmRequest: LlmRequest; error: Error; }): Promise; /** * Runs the `beforeModelCallback` for all plugins. */ runBeforeModelCallback({ callbackContext, llmRequest, }: { callbackContext: Context; llmRequest: LlmRequest; }): Promise; /** * Runs the `afterModelCallback` for all plugins. */ runAfterModelCallback({ callbackContext, llmResponse, }: { callbackContext: Context; llmResponse: LlmResponse; }): Promise; /** * Runs the `onToolErrorCallback` for all plugins. */ runOnToolErrorCallback({ tool, toolArgs, toolContext, error, }: { tool: BaseTool; toolArgs: Record; toolContext: Context; error: Error; }): Promise | undefined>; }