The Upstreet Agents SDK is now in public beta 🎉 Get started →
bg-pattern
📚 API ReferenceComponents

<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

import { Action } from 'react-agents';

Usage

Here is an example of how the Action component might be used:

import { Action } from 'react-agents';
import { z } from 'zod';
 
const sendMessageSchema = z.object({
  message: z.string(),
  recipientId: z.string(),
});
 
const handleSendMessage = async (e) => {
  console.log("Sending message:", e);
  // Code to send a message goes here
};
 
function MyComponent() {
  return (
    <Action
      name="SendMessage"
      description="Sends a message to a specific recipient."
      state="ready"
      schema={sendMessageSchema}
      examples={[
        { message: "Hello!", recipientId: "12345" },
        { message: "How are you?", recipientId: "67890" },
      ]}
      handler={handleSendMessage}
    />
  );
}

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

PropTypeDefault
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

On this page

Facing an issue? Add a ticket.