Set Agent Data

The Set Agent Data component manages information flow within agent workflows. It enables the storage of key-value pairs in the agent's data cache, allowing information to persist throughout the agent's execution lifecycle. This component requires both a Data Key (identifier) and Data Value (content to be stored) and supports both static text values and dynamically evaluated JavaScript expressions. The component can intelligently parse and evaluate expressions that reference agent data, event arguments, or window objects, making it flexible for various data-capturing scenarios and information sharing between components.


Skill Level

Basic understanding of JavaScript expressions or ability to use simple key-value pairs.

Overview

The Set Agent Data component stores key-value pairs in the agent's data cache. This component serves as a mechanism to persist information throughout an agent's execution lifecycle, allowing data to be saved and accessed by other components within the same agent workflow.

The component requires a Data Key (the identifier for storing the value) and a Data Value (the content to be stored). You can store plain text values as well as dynamically evaluated JavaScript expressions, making it versatile for various data storage needs.

When the system stores the data successfully, it starts the onComplete event. When validation fails or another error occurs during storage, the system starts an onError event.

The component evaluates expressions that reference agent data, event arguments, or window objects. This allows dynamic data capture from various sources within the agent's execution context.


Input

Name

Code

Type

Description

Data Type

Name

message

text

Name of the component.

string

Resource ID

message

text

Unique ID of the component.

string

Data Key

dataKey

text

Key name to store in the agent data cache.

string

Data Value

dataValue

textarea

Value to store for the specified key.

string


Methods

ID

Name

Description

Input

Output

execute

Execute

Validates the inputs and stores the key-value pair in the agent data cache.

Event arguments (optional)

JSON object with operation status details


Events

ID

Name

Description

Event Data

Source Methods

onComplete

On Complete

Starts when the data is successfully stored in the agent's data cache.

JSON object that contains success status, key name, value type, evaluation status, and timestamp. Example:
{
   success: true,
   key: "userPreferences",
   valueType: "object",
   wasEvaluated: true,
   timestamp: "2025-05-11T10:15:30.456Z"
}

Execute

onError

On Error

Starts when an error occurs during the validation or storage process.

Possible Errors: Missing required fields, invalid JavaScript expression, evaluation errors

JSON object that contains an error message, component name, timestamp, and details about the key and value that caused the error. Example:
{
   message: "Validation failed: Data Key is required",
   component: "SetAgentDataComponent",
   timestamp: "2025-05-11T10:15:30.456Z",
   details: {
     key: "",
     value: "some value"
   }
}

Execute

---

Typical Chaining of Components

You typically use the Set Agent Data component in a workflow that captures user input, processes it, and stores the results. A common implementation flow is as follows:

  1. User Input Capture: The user's input is initially captured through components such as AI Chat.

  2. Data Preparation: The Custom Script component processes this input and prepares it for the pipeline.

  3. AI Processing: The Call GenAI Pipeline component uses the prepared data.

  4. Response Display: Clean & Display Response component shows the results to the user.

  5. Data Storage: Set Agent Data component stores the response or relevant data for future reference.

// Create an object to hold the input message from the eventvar result = {
input: args.$event.message, // Store the input message
};
// Return the result object containing the input messagereturn result;

Note:

For pipelines requiring only a single input, you can use the Get User Prompt component directly. This component lets you set variable names that match what your pipeline expects, which simplifies the process of capturing and passing user inputs or attachments.

Source Components

Source Component

Purpose

Description

AI Chat

Capture User Input

Captures the user's message or uploaded files that must be processed

Custom Script

Process Input

Transforms user input into the structure that the pipeline requires.

Call GenAI Pipeline

Generate Response

Processes the structured input and generates an AI response.

Target Components

Target Component

Purpose

Description

Clean & Display Response

Show Response

Formats and displays the pipeline response to the user.

Custom Script

Process Response

Further processes or transforms the response data if needed.

Display Message

Show Confirmation

Displays confirmation that the system has successfully stored the data.

Implementation Example

The following example demonstrates how to use the Set Agent Data component in a Quiz Builder agent workflow:

Follow these steps to implement the flow shown in the diagram above:

  1. Drag the Set Agent Data component to the canvas.

  2. Set up the Data Key field with a meaningful identifier (for example, "rating" as shown in the example).

  3. Set the Data Value to either a static value or a dynamic expression (for example, "args.$event.message" to capture event data).

  4. Connect appropriate source components to start the Set Agent Data component.

  5. Connect the onComplete and onError events to appropriate downstream components.

In this implementation example, the Quiz Builder agent has the following workflow:

  1. The flow begins with a Start component that initializes the agent.

  2. When the user enters the flow, an AI Chat widget captures the user's message.

  3. When the system receives a chat message (onChatMessage event), a Custom Script component prepares data for API processing.

  4. The Call GenAI Pipeline component processes the data.

  5. When the pipeline completes, a Clean & Display Response component formats the response for the user.

  6. Finally, the Set Agent Data component stores relevant information, which allows the agent to maintain context between interactions.

Example of using a static JSON value

// Data Key: userPreferences// Data Value:
{
"preferredLanguage": "English",
"secondaryLanguage": "Spanish",
"notifications": true
}

Example of using a dynamic expression to capture event data

// Data Key: lastUserMessage// Data Value: args.$event.message

Example of using JavaScript expression evaluation

// Data Key: currentTimestamp// Data Value:newDate().toISOString()

Enhanced Version

You can enhance your implementation by using more complex expressions and accessing various agent data elements:

// Data Key: conversationContext// Data Value:
{
userMessage: args.$event.message,
timestamp: newDate().toISOString(),
sessionId: this.agent.data.sessionId || Math.random().toString(36).substring(2),
messageCount: (this.agent.data.messageCount || 0) + 1
}

This example shows how you can create a complex data structure that combines event data, current time, and existing agent data values to build a rich conversation context.

Best Practices

  • Naming Convention: Use clear, descriptive key names that indicate the purpose and content of the stored data.

  • Data Structure: For complex data, use JSON structures with proper formatting to maintain readability and ease of access.

  • Validation: Include validation in your agent flow before storing data to ensure data integrity.

  • Error Handling: Always connect the onError event to appropriate error handling components or notifications.

  • Data Lifecycle: Consider the lifecycle of stored data and implement clearing mechanisms for sensitive or temporary data.

  • Expression Evaluation: When you use JavaScript expressions, ensure they are valid within the context of execution.

  • Documentation: Document the data structure and purpose in your agent's documentation for maintainability.

Common Use Cases

Use Case

Description

Example Key-Value

User Preferences Storage

Store user preferences such as language, theme, or notification settings for personalized experiences.

Key: "userPreferences"
Value: JSON with preference settings

Conversation Context

Maintain conversation context by storing previous interactions or user inputs.

Key: "chatHistory"
Value: Array of message objects

Session Management

Store session identifiers or authentication tokens for maintaining user sessions.

Key: "sessionToken"
Value: Authentication string

Workflow State

Track the current state of multi-step workflows or processes.

Key: "currentStep"
Value: Step identifier or number

Configuration Settings

Store configuration settings for components or integrations.

Key: "apiConfig"
Value: JSON with API settings

Cache API Results

Cache results from external API calls to reduce redundant requests

Key: "cachedResults"
Value: API response with timestamp

Form Data Collection

Collect and store form data across multiple interaction steps.

Key: "formData"
Value: JSON with form fields and values