Class ProjectContextFunctionTools
- All Implemented Interfaces:
FunctionTools
This class registers tools for setting, retrieving, pushing, and popping variables in a project context, enabling stateful data sharing across acts and episodes.
- Author:
- Viktor Tovstyi
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringgetProjectContextVariable(String name, File projectDir) Retrieves the value of a variable from the project-specific context.static ObjectpopProjectContextVariable(String name, String mode, File projectDir) Removes and returns a value from a project context variable.static ObjectpushProjectContextVariable(String name, String value, File projectDir) Pushes a value to a project context variable.static voidSets or updates a variable in the context map for the specified project directory.static StringputProjectContextVariable(String name, String value, File projectDir) Sets or updates a variable in the project-specific context.
-
Constructor Details
-
ProjectContextFunctionTools
public ProjectContextFunctionTools()
-
-
Method Details
-
putProjectContextVariable
Sets or updates a variable in the project-specific context.This method stores or updates a named variable associated with a particular project directory, making it available for act execution or prompt templates. It can be used to pass a variable to the next episode of an act or to share state between different steps in a workflow.
- Parameters:
name- The name of the context variable to set or update.value- The value to assign to the context variable.projectDir- The project directory with which the context variable is associated.- Returns:
- A message indicating whether the context variable was successfully set or if an error occurred.
-
put
public static void put(File projectDir, String name, Object value) throws com.fasterxml.jackson.core.JsonProcessingException Sets or updates a variable in the context map for the specified project directory.If the value is a
String, it is stored as-is. Otherwise, the value is serialized to a JSON string using Jackson'sObjectMapperbefore being stored. The variable is associated with the givennameand made available in the context for the specifiedprojectDir.- Parameters:
projectDir- the project directory with which the context variable is associatedname- the name of the context variable to set or updatevalue- the value to assign to the context variable; if not a string, it will be serialized to JSON- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if the value cannot be serialized to JSON
-
getProjectContextVariable
Retrieves the value of a variable from the project-specific context.This method accesses a named variable associated with a particular project directory, making it available for act execution or prompt templates. If the context or variable does not exist, an appropriate message is returned.
- Parameters:
name- The name of the context variable to retrieve.projectDir- The project directory with which the context variable is associated.- Returns:
- The value of the context variable if found, or a message indicating that the variable or context was not found, or an error message if retrieval fails.
-
pushProjectContextVariable
Pushes a value to a project context variable.If the variable does not exist, a new list is created and the value is added. If the variable exists and is a string, it is converted to a list containing the original string and the new value. If the variable exists and is already a list, the new value is appended to the list. If the variable exists and is of any other type, an error message is returned.
- Parameters:
name- The name of the context variable.value- The value to push to the context variable.projectDir- The project directory with which the context variable is associated.- Returns:
- A message indicating the result of the operation, or an error message if the operation fails or the variable type is unsupported.
-
popProjectContextVariable
Removes and returns a value from a project context variable.If the variable is a string, it is removed from the context and returned. If the variable is a list, a value is removed and returned according to the specified mode:
- LIFO (last-in, first-out, default): removes and returns the last element in the list.
- FIFO (first-in, first-out): removes and returns the first element in the list.
- Parameters:
name- The name of the context variable.mode- Pop mode, either "LIFO" (default) or "FIFO".projectDir- The project directory with which the context variable is associated.- Returns:
- The removed value, or a message if the variable does not exist, is empty, or is of an unsupported type, or if an error occurs.
-