Using Hooks to Update Custom Contexts

Introduction

Hooks in BrainSoup are a powerful mechanism that allows you to extend and customize the platform's functionality. This guide will show you how to use Hooks to update Custom Contexts, enabling your agents to stay informed with real-time data from various sources.

Possible Uses

Updating Custom Contexts with Hooks can serve multiple purposes, such as:

  • Update Custom Context with Local Data: System metrics, home automation status, etc.
  • Update Custom Context with Online Data: Weather updates, stock exchange information, etc.
  • Update Custom Context with Database: Business KPIs, sales data, etc.

Choosing the Right Trigger

Every Event or Custom Event in BrainSoup can be used as a trigger for a Hook. Depending on your needs, you can choose from:

  • Periodical triggers (e.g., Timer.EveryMinute )
  • Fixed Time triggers (e.g., Clock.8AM )
  • Specific BrainSoup Events (e.g., FileSystem.Created )
  • Custom Events (e.g., OrderReceived )

Writing the Hook Script

The script must be located in the appropriate subdirectory of the Hooks directory (e.g., Hooks\OnEvent\Timer.EveryMinute ). You can use any language executable by the host computer. Below is a simple example using a PowerShell script named GetSystemState.ps1 to fetch system metrics and update the SystemState Custom Context.

# Get the CPU load
$cpuLoad = Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select-Object -ExpandProperty Average

# Get the memory usage
$memory = Get-WmiObject Win32_OperatingSystem
$totalMemory = $memory.TotalVisibleMemorySize
$freeMemory = $memory.FreePhysicalMemory
$usedMemory = $totalMemory - $freeMemory
$usedMemoryPercent = [math]::Round(($usedMemory / $totalMemory) * 100)

# Get the number of processes
$processCount = (Get-Process).Count

# Create a directory named SystemState in the Context directory
$directory = "..\..\..\Context\SystemState"
New-Item -ItemType Directory -Path $directory -Force

# Save the system state data to text files
Set-Content -Path "$directory\CpuLoad.txt" -Value "$cpuLoad%" -Force
Set-Content -Path "$directory\MemoryUsage.txt" -Value "$usedMemoryPercent%" -Force
Set-Content -Path "$directory\ProcessCount.txt" -Value "$processCount" -Force

Tips:

  • You can quickly access the Hooks folder from the BrainSoup interface by going to Settings > App Folders > Hooks.
  • Instead of a text file tree, you can also define your Custom Context using a JSON-formatted file.
  • You can use BrainSoup's built-in expert agent, Doc-E, to generate the script for you. Simply describe the task you want to automate, and Doc-E will write the script in the language of your choice.

Subscribing to the Custom Context

Agents or chat rooms can subscribe to this context through their settings.

To subscribe an agent:

  1. Access the agent's settings.
  2. Navigate to the 'Context subscriptions' section.
  3. Add the subject of your context (e.g., SystemState ) to the list.

To subscribe a chat room:

  1. Open the chat room settings.
  2. Navigate to the 'Context subscriptions' section.
  3. Add the subject of your context to the list.

This process links the agent or chat room to your Custom Context, enabling it to access and utilize the provided data.

Considerations Regarding Context Size and Agent Working Memory

When updating Custom Contexts, it is crucial to keep context size small and relevant. Overloading agents with too much data can impact their performance and usage costs. Use only essential information that agents need for their tasks.

Conclusion

By using Hooks to update Custom Contexts in BrainSoup, you can ensure your agents have access to real-time data, making them more effective and responsive. Explore different triggers and scripts to automate updates seamlessly.