The Context API provides functionality for managing persistent storage contexts in the AgentBay cloud environment. Contexts allow you to persist data across sessions and reuse it in future sessions.
The Context class represents a persistent storage context in the AgentBay cloud environment.
id # The unique identifier of the context
name # The name of the context
state # The current state of the context (e.g., "available", "in-use")
created_at # Date and time when the Context was created
last_used_at # Date and time when the Context was last used
os_type # The operating system type this context is bound toThe ContextService class provides methods for managing persistent contexts in the AgentBay cloud environment.
Lists all available contexts.
list() -> ContextListResultReturns:
ContextListResult: A result object containing the list of Context objects and request ID.
Example:
from agentbay import AgentBay
# Initialize the SDK
agent_bay = AgentBay(api_key="your_api_key")
# List all contexts
result = agent_bay.context.list()
if result.success:
print(f"Found {len(result.contexts)} contexts:")
for context in result.contexts:
print(f"Context ID: {context.id}, Name: {context.name}, State: {context.state}")
else:
print("Failed to list contexts")Gets a context by name. Optionally creates it if it doesn't exist.
get(name: str, create: bool = False) -> ContextResultParameters:
name(str): The name of the context to get.create(bool, optional): Whether to create the context if it doesn't exist. Defaults to False.
Returns:
ContextResult: A result object containing the Context object and request ID.
Example:
from agentbay import AgentBay
# Initialize the SDK
agent_bay = AgentBay(api_key="your_api_key")
# Get a context, creating it if it doesn't exist
result = agent_bay.context.get("my-persistent-context", create=True)
if result.success:
context = result.context
print(f"Context ID: {context.id}, Name: {context.name}, State: {context.state}")
else:
print(f"Failed to get context: {result.error_message}")Creates a new context.
create(name: str) -> ContextResultParameters:
name(str): The name of the context to create.
Returns:
ContextResult: A result object containing the created Context object and request ID.
Example:
from agentbay import AgentBay
# Initialize the SDK
agent_bay = AgentBay(api_key="your_api_key")
# Create a new context
result = agent_bay.context.create("my-new-context")
if result.success:
context = result.context
print(f"Created context with ID: {context.id}, Name: {context.name}")
else:
print(f"Failed to create context: {result.error_message}")Deletes a context.
delete(context: Context) -> OperationResultParameters:
context(Context): The Context object to delete.
Returns:
OperationResult: A result object containing success status and request ID.
Example:
from agentbay import AgentBay
# Initialize the SDK
agent_bay = AgentBay(api_key="your_api_key")
# Get a context first
result = agent_bay.context.get("my-context")
if result.success and result.context:
# Delete the context
delete_result = agent_bay.context.delete(result.context)
if delete_result.success:
print("Context deleted successfully")
else:
print(f"Failed to delete context: {delete_result.error_message}")
else:
print(f"Failed to get context: {result.error_message}")Updates a context's properties.
update(context: Context) -> OperationResultParameters:
context(Context): The Context object with updated properties.
Returns:
OperationResult: A result object containing success status and request ID.
Example:
from agentbay import AgentBay
# Initialize the SDK
agent_bay = AgentBay(api_key="your_api_key")
# Get a context first
result = agent_bay.context.get("my-context")
if result.success and result.context:
# Update the context name
context = result.context
context.name = "my-renamed-context"
# Save the changes
update_result = agent_bay.context.update(context)
if update_result.success:
print("Context updated successfully")
else:
print(f"Failed to update context: {update_result.error_message}")
else:
print(f"Failed to get context: {result.error_message}")