Context

The Context object is a shared state object that stores the state of the conversation across all Rounds within a Session. It is used to maintain the context of the conversation, as well as the overall status of the conversation.

Context Attributes

The attributes of the Context object are defined in the ContextNames class, which is an Enum. The ContextNames class specifies various context attributes used throughout the session. Below is the definition:

class ContextNames(Enum):
    """
    The context names.
    """

    ID = "ID"  # The ID of the session
    MODE = "MODE"  # The mode of the session
    LOG_PATH = "LOG_PATH"  # The folder path to store the logs
    REQUEST = "REQUEST"  # The current request
    SUBTASK = "SUBTASK"  # The current subtask processed by the AppAgent
    PREVIOUS_SUBTASKS = "PREVIOUS_SUBTASKS"  # The previous subtasks processed by the AppAgent
    HOST_MESSAGE = "HOST_MESSAGE"  # The message from the HostAgent sent to the AppAgent
    REQUEST_LOGGER = "REQUEST_LOGGER"  # The logger for the LLM request
    LOGGER = "LOGGER"  # The logger for the session
    EVALUATION_LOGGER = "EVALUATION_LOGGER"  # The logger for the evaluation
    ROUND_STEP = "ROUND_STEP"  # The step of all rounds
    SESSION_STEP = "SESSION_STEP"  # The step of the current session
    CURRENT_ROUND_ID = "CURRENT_ROUND_ID"  # The ID of the current round
    APPLICATION_WINDOW = "APPLICATION_WINDOW"  # The window of the application
    APPLICATION_PROCESS_NAME = "APPLICATION_PROCESS_NAME"  # The process name of the application
    APPLICATION_ROOT_NAME = "APPLICATION_ROOT_NAME"  # The root name of the application
    CONTROL_REANNOTATION = "CONTROL_REANNOTATION"  # The re-annotation of the control provided by the AppAgent
    SESSION_COST = "SESSION_COST"  # The cost of the session
    ROUND_COST = "ROUND_COST"  # The cost of all rounds
    ROUND_SUBTASK_AMOUNT = "ROUND_SUBTASK_AMOUNT"  # The amount of subtasks in all rounds
    CURRENT_ROUND_STEP = "CURRENT_ROUND_STEP"  # The step of the current round
    CURRENT_ROUND_COST = "CURRENT_ROUND_COST"  # The cost of the current round
    CURRENT_ROUND_SUBTASK_AMOUNT = "CURRENT_ROUND_SUBTASK_AMOUNT"  # The amount of subtasks in the current round
    STRUCTURAL_LOGS = "STRUCTURAL_LOGS"  # The structural logs of the session

Each attribute is a string that represents a specific aspect of the session context, ensuring that all necessary information is accessible and manageable within the application.

Attributes Description

Attribute Description
ID The ID of the session.
MODE The mode of the session.
LOG_PATH The folder path to store the logs.
REQUEST The current request.
SUBTASK The current subtask processed by the AppAgent.
PREVIOUS_SUBTASKS The previous subtasks processed by the AppAgent.
HOST_MESSAGE The message from the HostAgent sent to the AppAgent.
REQUEST_LOGGER The logger for the LLM request.
LOGGER The logger for the session.
EVALUATION_LOGGER The logger for the evaluation.
ROUND_STEP The step of all rounds.
SESSION_STEP The step of the current session.
CURRENT_ROUND_ID The ID of the current round.
APPLICATION_WINDOW The window of the application.
APPLICATION_PROCESS_NAME The process name of the application.
APPLICATION_ROOT_NAME The root name of the application.
CONTROL_REANNOTATION The re-annotation of the control provided by the AppAgent.
SESSION_COST The cost of the session.
ROUND_COST The cost of all rounds.
ROUND_SUBTASK_AMOUNT The amount of subtasks in all rounds.
CURRENT_ROUND_STEP The step of the current round.
CURRENT_ROUND_COST The cost of the current round.
CURRENT_ROUND_SUBTASK_AMOUNT The amount of subtasks in the current round.
STRUCTURAL_LOGS The structural logs of the session.

Reference for the Context object

The context class that maintains the context for the session and agent.

current_round_cost: Optional[float] property writable

Get the current round cost.

current_round_step: int property writable

Get the current round step.

current_round_subtask_amount: int property writable

Get the current round subtask index.

add_to_structural_logs(data)

Add data to the structural logs.

Parameters:
  • data (Dict[str, Any]) –

    The data to add to the structural logs.

Source code in module/context.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
def add_to_structural_logs(self, data: Dict[str, Any]) -> None:
    """
    Add data to the structural logs.
    :param data: The data to add to the structural logs.
    """

    round_key = data.get("Round", None)
    subtask_key = data.get("SubtaskIndex", None)

    if round_key is None or subtask_key is None:
        return

    remaining_items = {key: data[key] for key in data if key not in ["a", "b"]}
    self._context[ContextNames.STRUCTURAL_LOGS.name][round_key][subtask_key].append(
        remaining_items
    )

filter_structural_logs(round_key, subtask_key, keys)

Filter the structural logs.

Parameters:
  • round_key (int) –

    The round key.

  • subtask_key (int) –

    The subtask key.

  • keys (Union[str, List[str]]) –

    The keys to filter.

Returns:
  • Union[List[Any], List[Dict[str, Any]]]

    The filtered structural logs.

Source code in module/context.py
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
def filter_structural_logs(
    self, round_key: int, subtask_key: int, keys: Union[str, List[str]]
) -> Union[List[Any], List[Dict[str, Any]]]:
    """
    Filter the structural logs.
    :param round_key: The round key.
    :param subtask_key: The subtask key.
    :param keys: The keys to filter.
    :return: The filtered structural logs.
    """

    structural_logs = self._context[ContextNames.STRUCTURAL_LOGS.name][round_key][
        subtask_key
    ]

    if isinstance(keys, str):
        return [log[keys] for log in structural_logs]
    elif isinstance(keys, list):
        return [{key: log[key] for key in keys} for log in structural_logs]
    else:
        raise TypeError(f"Keys should be a string or a list of strings.")

get(key)

Get the value from the context.

Parameters:
  • key (ContextNames) –

    The context name.

Returns:
  • Any

    The value from the context.

Source code in module/context.py
165
166
167
168
169
170
171
172
173
def get(self, key: ContextNames) -> Any:
    """
    Get the value from the context.
    :param key: The context name.
    :return: The value from the context.
    """
    # Sync the current round step and cost
    self._sync_round_values()
    return self._context.get(key.name)

set(key, value)

Set the value in the context.

Parameters:
  • key (ContextNames) –

    The context name.

  • value (Any) –

    The value to set in the context.

Source code in module/context.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def set(self, key: ContextNames, value: Any) -> None:
    """
    Set the value in the context.
    :param key: The context name.
    :param value: The value to set in the context.
    """
    if key.name in self._context:
        self._context[key.name] = value
        # Sync the current round step and cost
        if key == ContextNames.CURRENT_ROUND_STEP:
            self.current_round_step = value
        if key == ContextNames.CURRENT_ROUND_COST:
            self.current_round_cost = value
        if key == ContextNames.CURRENT_ROUND_SUBTASK_AMOUNT:
            self.current_round_subtask_amount = value
    else:
        raise KeyError(f"Key '{key}' is not a valid context name.")

to_dict()

Convert the context to a dictionary.

Returns:
  • Dict[str, Any]

    The dictionary of the context.

Source code in module/context.py
313
314
315
316
317
318
def to_dict(self) -> Dict[str, Any]:
    """
    Convert the context to a dictionary.
    :return: The dictionary of the context.
    """
    return self._context

update_dict(key, value)

Add a dictionary to a context key. The value and the context key should be dictionaries.

Parameters:
  • key (ContextNames) –

    The context key to update.

  • value (Dict[str, Any]) –

    The dictionary to add to the context key.

Source code in module/context.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
def update_dict(self, key: ContextNames, value: Dict[str, Any]) -> None:
    """
    Add a dictionary to a context key. The value and the context key should be dictionaries.
    :param key: The context key to update.
    :param value: The dictionary to add to the context key.
    """
    if key.name in self._context:
        context_value = self._context[key.name]
        if isinstance(value, dict) and isinstance(context_value, dict):
            self._context[key.name].update(value)
        else:
            raise TypeError(
                f"Value for key '{key.name}' is {key.value}, requires a dictionary."
            )
    else:
        raise KeyError(f"Key '{key.name}' is not a valid context name.")