Initializes the ReflectAndRetryToolPlugin.
Configuration options for the plugin.
ReadonlymaxReadonlynameReadonlyscopeReadonlythrowCallback executed after an agent's primary logic has completed.
This callback can be used to inspect, log, or modify the agent's final result before it is returned.
An optional Content object. If a value is returned, it will
replace the agent's original result. Returning undefined uses the
original, unmodified result.
Callback executed after context compaction.
This callback provides an opportunity to inspect the context after it has been compacted.
The context for the entire invocation.
The trigger for the context compaction.
Callback executed after a response is received from the model.
This is the ideal place to log model responses, collect metrics on token
usage, or perform post-processing on the raw LlmResponse.
The context for the current agent call.
The response object received from the model.
An optional value. A non-undefined return may be used by the
framework to modify or replace the response. Returning undefined
allows the original response to be used.
Callback executed after a workflow node has run.
Not called for a node whose body was skipped by beforeNodeCallback, and
not called when the node throws.
The node that has just run.
The node's execution context.
The output the node produced.
An optional value replacing the node's output. Returning
undefined keeps the original.
Callback executed after an ADK runner run has completed.
This is the final callback in the ADK lifecycle, suitable for cleanup, final logging, or reporting tasks.
The context for the entire invocation.
undefined
Handles successful tool calls or extracts and processes errors.
An optional record containing reflection guidance if an error is detected, or undefined if the tool call was successful or the response is already a reflection message.
Callback executed before an agent's primary logic is invoked.
This callback can be used for logging, setup, or to short-circuit the agent's execution by returning a value.
An optional Content object. If a value is returned, it will
bypass the agent's callbacks and its execution, and return this value
directly. Returning undefined allows the agent to proceed normally.
Callback executed before context compaction.
This callback provides an opportunity to inspect or modify the context before it is compacted.
The context for the entire invocation.
The trigger for the context compaction.
Callback executed before a request is sent to the model.
This provides an opportunity to inspect, log, or modify the LlmRequest
object. It can also be used to implement caching by returning a cached
LlmResponse, which would skip the actual model call.
The context for the current agent call.
The prepared request object to be sent to the model.
An optional value. The interpretation of a non-undefined
trigger an early exit and returns the response immediately. Returning
undefined allows the LLM request to proceed normally.
Callback executed before a workflow node runs.
The input the node is about to receive.
The node that is about to run.
The node's execution context.
An optional value. If anything other than undefined is returned,
the node's body is skipped and the returned value becomes its output,
which is how a plugin implements a node-level cache or a stub.
Returning undefined lets the node run normally.
Callback executed before the ADK runner runs.
This is the first callback to be called in the lifecycle, ideal for global setup or initialization tasks.
The context for the entire invocation, containing session information, the root agent, etc.
An optional Event to be returned to the ADK. Returning a value
to halt execution of the runner and ends the runner with that event.
Return undefined to proceed normally.
Callback executed before a tool is called.
This callback is useful for logging tool usage, input validation, or modifying the arguments before they are passed to the tool.
An optional dictionary. If a dictionary is returned, it will stop
the tool execution and return this response immediately. Returning
undefined uses the original, unmodified arguments.
Callback executed before a tool is selected.
This callback provides an opportunity to inspect, log, or modify the available tools before they are selected.
An optional value. A non-undefined return may be used by the
framework to modify or replace the available tools. Returning
undefined allows the original tools to be used.
ProtectedcreateGenerates structured reflection guidance for tool failures.
The tool that failed.
The arguments supplied to the tool.
The error encountered.
The current retry count.
The structured tool failure response record.
ProtectedensureEnsures the given error is an Error instance, wrapping if not.
The error object.
An Error instance.
Extracts an error from a successful tool result and triggers retry logic.
This is useful when a tool call finishes without throwing an exception but
the result contains an error payload like {status: 'error', message: '...'}
that should be handled by the plugin.
The extracted error if any, or undefined if no error was detected.
ProtectedformatFormats error details for inclusion in the reflection message.
The error object.
Formatted error string.
ProtectedgetReturns a unique key for the state tracker based on the configured scope.
This method can be overridden in a subclass to implement custom scoping logic, for example tracking failures on a per-user or per-session basis.
The tool context.
The resolved scope key.
ProtectedgetGenerates guidance when the maximum retry limit is exceeded.
The tool that failed.
The arguments supplied to the tool.
The error encountered.
The structured retry-exceeded failure response record.
ProtectedhandleCentral, thread-safe logic for processing tool errors.
An optional record containing reflection guidance for the error.
Callback executed after an event is yielded from runner.
This is the ideal place to make modification to the event before the event is handled by the underlying agent app.
The event raised by the runner.
The context for the entire invocation.
An optional value. A non-undefined return may be used by the
framework to modify or replace the response. Copy the incoming
event when constructing a replacement to preserve fields that are not
being modified, such as event actions. Returning undefined allows the
original response to be used.
Callback executed when a model call encounters an error.
This callback provides an opportunity to handle model errors gracefully, potentially providing alternative responses or recovery mechanisms.
The context for the current agent call.
The exception that was raised during model execution.
The request that was sent to the model when the error occurred.
An optional LlmResponse. If an LlmResponse is returned, it will be
used instead of propagating the error. Returning undefined allows
the original error to be raised.
Handles tool exceptions by providing reflection guidance.
An optional record containing reflection guidance for the error.
Callback executed when a user message is received before an invocation starts.
This callback helps logging and modifying the user message before the runner starts the invocation.
The context for the entire invocation.
The message content input by user.
An optional Content to be returned to the ADK. Returning a
value to replace the user message. Returning undefined to proceed
normally.
ProtectedresetAtomically resets the failure count for a tool and cleans up state.
The tool context.
The name of the tool.
Provides self-healing, concurrent-safe error recovery for tool failures.
This plugin intercepts tool failures, provides structured guidance to the LLM for reflection and correction, and retries the operation up to a configurable limit.
Key Features:
getScopeKeymethod can be overridden to implement custom tracking logic (e.g., per-user or per-session).extractErrorFromResult.Example