# Class PluginManager

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.

- Defined in plugins/plugin_manager.ts:34

## Constructors

### constructor

- new PluginManager(plugins?: BasePlugin[]): PluginManager

  Initializes the plugin service.

  #### Parameters

  - `Optional` plugins: BasePlugin[]

    An optional list of plugins to register upon initialization.

  #### Returns PluginManager

  - Defined in plugins/plugin_manager.ts:42

## Methods

### getPlugin

- getPlugin(pluginName: string): BasePlugin | undefined

  Retrieves a registered plugin by its name.

  #### Parameters

  - pluginName: string

    The name of the plugin to retrieve.

  #### Returns BasePlugin | undefined

  The plugin instance if found, otherwise `undefined`.

  - Defined in plugins/plugin_manager.ts:77

### registerPlugin

- registerPlugin(plugin: BasePlugin): void

  Registers a new plugin.

  #### Parameters

  - plugin: BasePlugin

    The plugin instance to register.

  #### Returns void

  #### Throws

  If the same exact plugin or a plugin with the same name is already registered.

  - Defined in plugins/plugin_manager.ts:57

### runAfterAgentCallback

- runAfterAgentCallback(
  __namedParameters: { agent: BaseAgent; callbackContext: Context },
  ): Promise<Content | undefined>

  Runs the `afterAgentCallback` for all plugins.

  #### Parameters

  - __namedParameters: { agent: BaseAgent; callbackContext: Context }

  #### Returns Promise<Content | undefined>

  - Defined in plugins/plugin_manager.ts:204

### runAfterModelCallback

- runAfterModelCallback(
  __namedParameters: {
  callbackContext: Context;
  llmResponse: LlmResponse;
  },
  ): Promise<LlmResponse | undefined>

  Runs the `afterModelCallback` for all plugins.

  #### Parameters

  - __namedParameters: { callbackContext: Context; llmResponse: LlmResponse }

  #### Returns Promise<LlmResponse | undefined>

  - Defined in plugins/plugin_manager.ts:302

### runAfterRunCallback

- runAfterRunCallback(
  __namedParameters: { invocationContext: InvocationContext },
  ): Promise<void>

  Runs the `afterRunCallback` for all plugins.

  #### Parameters

  - __namedParameters: { invocationContext: InvocationContext }

  #### Returns Promise<void>

  - Defined in plugins/plugin_manager.ts:153

### runAfterToolCallback

- runAfterToolCallback(
  __namedParameters: {
  result: Record<string, unknown>;
  tool: BaseTool;
  toolArgs: Record<string, unknown>;
  toolContext: Context;
  },
  ): Promise<Record<string, unknown> | undefined>

  Runs the `afterToolCallback` for all plugins.

  #### Parameters

  - __namedParameters: {
    result: Record<string, unknown>;
    tool: BaseTool;
    toolArgs: Record<string, unknown>;
    toolContext: Context;
    }

  #### Returns Promise<Record<string, unknown> | undefined>

  - Defined in plugins/plugin_manager.ts:242

### runBeforeAgentCallback

- runBeforeAgentCallback(
  __namedParameters: { agent: BaseAgent; callbackContext: Context },
  ): Promise<Content | undefined>

  Runs the `beforeAgentCallback` for all plugins.

  #### Parameters

  - __namedParameters: { agent: BaseAgent; callbackContext: Context }

  #### Returns Promise<Content | undefined>

  - Defined in plugins/plugin_manager.ts:186

### runBeforeModelCallback

- runBeforeModelCallback(
  __namedParameters: { callbackContext: Context; llmRequest: LlmRequest },
  ): Promise<LlmResponse | undefined>

  Runs the `beforeModelCallback` for all plugins.

  #### Parameters

  - __namedParameters: { callbackContext: Context; llmRequest: LlmRequest }

  #### Returns Promise<LlmResponse | undefined>

  - Defined in plugins/plugin_manager.ts:284

### runBeforeRunCallback

- runBeforeRunCallback(
  __namedParameters: { invocationContext: InvocationContext },
  ): Promise<Content | undefined>

  Runs the `beforeRunCallback` for all plugins.

  #### Parameters

  - __namedParameters: { invocationContext: InvocationContext }

  #### Returns Promise<Content | undefined>

  - Defined in plugins/plugin_manager.ts:138

### runBeforeToolCallback

- runBeforeToolCallback(
  __namedParameters: {
  tool: BaseTool;
  toolArgs: Record<string, unknown>;
  toolContext: Context;
  },
  ): Promise<Record<string, unknown> | undefined>

  Runs the `beforeToolCallback` for all plugins.

  #### Parameters

  - __namedParameters: { tool: BaseTool; toolArgs: Record<string, unknown>; toolContext: Context }

  #### Returns Promise<Record<string, unknown> | undefined>

  - Defined in plugins/plugin_manager.ts:222

### runOnEventCallback

- runOnEventCallback(
  __namedParameters: {
  event: Event;
  invocationContext: InvocationContext;
  },
  ): Promise<Event | undefined>

  Runs the `onEventCallback` for all plugins.

  #### Parameters

  - __namedParameters: { event: Event; invocationContext: InvocationContext }

  #### Returns Promise<Event | undefined>

  - Defined in plugins/plugin_manager.ts:168

### runOnModelErrorCallback

- runOnModelErrorCallback(
  __namedParameters: {
  callbackContext: Context;
  error: Error;
  llmRequest: LlmRequest;
  },
  ): Promise<LlmResponse | undefined>

  Runs the `onModelErrorCallback` for all plugins.

  #### Parameters

  - __namedParameters: { callbackContext: Context; error: Error; llmRequest: LlmRequest }

  #### Returns Promise<LlmResponse | undefined>

  - Defined in plugins/plugin_manager.ts:264

### runOnToolErrorCallback

- runOnToolErrorCallback(
  __namedParameters: {
  error: Error;
  tool: BaseTool;
  toolArgs: Record<string, unknown>;
  toolContext: Context;
  },
  ): Promise<Record<string, unknown> | undefined>

  Runs the `onToolErrorCallback` for all plugins.

  #### Parameters

  - __namedParameters: {
    error: Error;
    tool: BaseTool;
    toolArgs: Record<string, unknown>;
    toolContext: Context;
    }

  #### Returns Promise<Record<string, unknown> | undefined>

  - Defined in plugins/plugin_manager.ts:320

### runOnUserMessageCallback

- runOnUserMessageCallback(
  __namedParameters: {
  invocationContext: InvocationContext;
  userMessage: Content;
  },
  ): Promise<Content | undefined>

  Runs the `onUserMessageCallback` for all plugins.

  #### Parameters

  - __namedParameters: { invocationContext: InvocationContext; userMessage: Content }

  #### Returns Promise<Content | undefined>

  - Defined in plugins/plugin_manager.ts:120