Context Manager

class app.utils.context_manager.ContextManager(max_context_length: int = 10)[source]

Bases: object

Manages conversation context using a fixed-size circular buffer.

This class provides methods to add, retrieve, and manipulate the conversation context.

add_to_context(message: str) None[source]

Add a message to the context.

Parameters:

message – The message to add to the context.

clear_context() None[source]

Clear all messages from the context.

get_context() List[str][source]

Retrieve the current context as a list of messages.

Returns:

The current context messages.

get_context_length() int[source]

Get the current number of messages in the context.

Returns:

The number of messages in the context.

get_context_string(separator: str = '\n') str[source]

Retrieve the current context as a single string.

Parameters:

separator – The string to use for joining context messages.

Returns:

The current context as a single string.

get_last_message() str | None[source]

Get the last message added to the context.

Returns:

The last message, or None if the context is empty.

get_max_context_length() int[source]

Get the maximum number of messages that can be stored in the context.

Returns:

The maximum context length.