<Action>
Define actions your Agent can perform.
The Action
component from react-agents
represents an action that a React AI agent can perform. It registers a new action with an agent registry, allowing the agent to trigger the action in response to certain events. This component is useful for defining discrete, reusable actions with associated schemas and event handlers, making it easier to define and handle actions within an AI-driven context.
Import
Usage
Here is an example of how the Action component might be used:
Example Breakdown:
name is set to "SendMessage" to identify the action. description provides a brief summary of the action's purpose. state is set to "ready" to indicate the current status of the action. schema defines the expected input parameters, validating that message and recipientId are strings. examples show sample data for the message and recipientId inputs. handler is an asynchronous function that handles the SendMessage action when it is triggered. Component Lifecycle When the Action component is mounted, it registers the action with the agent registry using the registerAction method, associating the action with a unique symbol. When unmounted, it unregisters the action, ensuring no memory leaks.
Props
Prop | Type | Default |
---|---|---|
name | string | N/A |
description | string | N/A |
state | string | undefined |
schema | ZodTypeAny | N/A |
examples | Array<object> | N/A |
handler | function | undefined |
Dependencies
The component recalculates and registers the action if any of the following dependencies change:
name
description
Serialized schema (printZodSchema)
Serialized examples
Serialized handler function