praval.core.agent๏ƒ

Core Agent class for the Praval framework.

The Agent class provides a simple, composable interface for LLM-based conversations with support for multiple providers, tools, and state persistence.

Classes

Agent(name[,ย provider,ย persist_state,ย ...])

A simple, composable LLM agent.

AgentConfig([provider,ย temperature,ย ...])

Configuration for Agent behavior and LLM parameters.

class praval.core.agent.AgentConfig(provider=None, temperature=0.7, max_tokens=1000, system_message=None)[source]๏ƒ

Bases: object

Configuration for Agent behavior and LLM parameters.

Parameters:
  • provider (str | None)

  • temperature (float)

  • max_tokens (int)

  • system_message (str | None)

provider: Optional[str] = None๏ƒ
temperature: float = 0.7๏ƒ
max_tokens: int = 1000๏ƒ
system_message: Optional[str] = None๏ƒ
__post_init__()[source]๏ƒ

Validate configuration parameters.

__init__(provider=None, temperature=0.7, max_tokens=1000, system_message=None)๏ƒ
Parameters:
  • provider (str | None)

  • temperature (float)

  • max_tokens (int)

  • system_message (str | None)

Return type:

None

class praval.core.agent.Agent(name, provider=None, persist_state=False, system_message=None, config=None, memory_enabled=False, memory_config=None, knowledge_base=None, max_history=100, hitl_enabled=False, hitl_db_path=None)[source]๏ƒ

Bases: object

A simple, composable LLM agent.

The Agent class provides the core functionality for LLM-based conversations with support for multiple providers, conversation history, tools, and state persistence.

Examples

Basic usage: >>> agent = Agent(โ€œassistantโ€) >>> response = agent.chat(โ€œHello!โ€)

With persistence: >>> agent = Agent(โ€œmy_agentโ€, persist_state=True) >>> agent.chat(โ€œRemember this conversationโ€)

With tools: >>> agent = Agent(โ€œcalculatorโ€) >>> @agent.tool >>> def add(x: int, y: int) -> int: โ€ฆ return x + y

Parameters:
  • name (str)

  • provider (str | None)

  • persist_state (bool)

  • system_message (str | None)

  • config (Dict[str, Any] | None)

  • memory_enabled (bool)

  • memory_config (Dict[str, Any] | None)

  • knowledge_base (str | None)

  • max_history (int | None)

  • hitl_enabled (bool)

  • hitl_db_path (str | None)

__init__(name, provider=None, persist_state=False, system_message=None, config=None, memory_enabled=False, memory_config=None, knowledge_base=None, max_history=100, hitl_enabled=False, hitl_db_path=None)[source]๏ƒ

Initialize a new Agent.

Parameters:
  • name (str) โ€“ Unique identifier for this agent

  • provider (Optional[str]) โ€“ LLM provider to use (openai, anthropic, cohere)

  • persist_state (bool) โ€“ Whether to persist conversation state

  • system_message (Optional[str]) โ€“ System message to set agent behavior

  • config (Optional[Dict[str, Any]]) โ€“ Additional configuration parameters

  • memory_enabled (bool) โ€“ Whether to enable vector memory capabilities

  • memory_config (Optional[Dict[str, Any]]) โ€“ Configuration for memory system

  • knowledge_base (Optional[str]) โ€“ Path to knowledge base files to auto-index

  • max_history (Optional[int]) โ€“ Max conversation turns to retain (None for unbounded)

  • hitl_enabled (bool)

  • hitl_db_path (str | None)

Raises:
chat(message)[source]๏ƒ

Send a message to the agent and get a response.

Parameters:

message (Optional[str]) โ€“ User message to send to the agent

Return type:

str

Returns:

Agentโ€™s response as a string

Raises:
configure_hitl(*, enabled=True, db_path=None)[source]๏ƒ

Configure HITL behavior for this agent.

Parameters:
  • enabled (bool) โ€“ Whether HITL is enabled for this agent

  • db_path (Optional[str]) โ€“ Optional SQLite path override for intervention storage

Return type:

None

get_pending_interventions(run_id=None, limit=100)[source]๏ƒ

Get pending interventions filtered to this agent.

Return type:

List[Any]

Parameters:
  • run_id (str | None)

  • limit (int)

approve_intervention(intervention_id, *, reviewer='human', edited_args=None)[source]๏ƒ

Approve or edit-approve an intervention for this agent.

Return type:

Any

Parameters:
reject_intervention(intervention_id, *, reason, reviewer='human')[source]๏ƒ

Reject an intervention for this agent.

Return type:

Any

Parameters:
  • intervention_id (str)

  • reason (str)

  • reviewer (str)

resume_run(run_id)[source]๏ƒ

Resume a previously suspended HITL run after a decision.

Parameters:

run_id (str) โ€“ Suspended run identifier

Return type:

str

Returns:

Final model response for the resumed run

tool(func)[source]๏ƒ

Decorator to register a function as a tool for the agent.

Parameters:

func (Callable) โ€“ Function to register as a tool

Return type:

Callable

Returns:

The original function (unchanged)

Raises:

ValueError โ€“ If function lacks proper type hints

send_knowledge(to_agent, knowledge, channel='main')[source]๏ƒ

Send knowledge to another agent through the reef.

Parameters:
  • to_agent (str) โ€“ Name of the target agent

  • knowledge (Dict[str, Any]) โ€“ Knowledge data to send

  • channel (str) โ€“ Reef channel to use (default: โ€œmainโ€)

Return type:

str

Returns:

Spore ID of the sent message

broadcast_knowledge(knowledge, channel='main')[source]๏ƒ

Broadcast knowledge to all agents in the reef.

Parameters:
  • knowledge (Dict[str, Any]) โ€“ Knowledge data to broadcast

  • channel (str) โ€“ Reef channel to use (default: โ€œmainโ€)

Return type:

str

Returns:

Spore ID of the broadcast message

request_knowledge(from_agent, request, timeout=30)[source]๏ƒ

Request knowledge from another agent with timeout.

Parameters:
  • from_agent (str) โ€“ Name of the agent to request from

  • request (Dict[str, Any]) โ€“ Request data

  • timeout (int) โ€“ Timeout in seconds

Return type:

Optional[Dict[str, Any]]

Returns:

Response data or None if timeout

on_spore_received(spore)[source]๏ƒ

Handle received spores from the reef.

This is a default implementation that can be overridden by subclasses for custom spore handling.

Parameters:

spore โ€“ The received Spore object

Return type:

None

subscribe_to_channel(channel_name)[source]๏ƒ

Subscribe this agent to a reef channel.

Parameters:

channel_name (str) โ€“ Name of the channel to subscribe to

Return type:

None

unsubscribe_from_channel(channel_name)[source]๏ƒ

Unsubscribe this agent from a reef channel.

Parameters:

channel_name (str) โ€“ Name of the channel to unsubscribe from

Return type:

None

property spore_handler: Callable | None๏ƒ

Get the current spore handler for this agent.

Returns:

The custom spore handler function, or None if not set

set_spore_handler(handler)[source]๏ƒ

Set a custom spore handler for this agent.

Parameters:

handler (Callable) โ€“ Function that takes a Spore object and handles it

Return type:

None

close()[source]๏ƒ

Release all resources held by the agent.

This method: - Unsubscribes from all reef channels - Shuts down the memory system - Clears conversation history

Safe to call multiple times. After calling close(), the agent should not be used for further operations.

Example:

agent = Agent("assistant")
try:
    response = agent.chat("Hello")
finally:
    agent.close()

# Or use as context manager:
with Agent("assistant") as agent:
    response = agent.chat("Hello")
Return type:

None

__enter__()[source]๏ƒ

Context manager entry - returns the agent.

Return type:

Agent

__exit__(exc_type, exc_val, exc_tb)[source]๏ƒ

Context manager exit - ensures cleanup.

Return type:

None

__del__()[source]๏ƒ

Destructor - attempt cleanup if not already done.

property is_closed: bool๏ƒ

Check if the agent has been closed.

remember(content, importance=0.5, memory_type='short_term')[source]๏ƒ

Store a memory

Parameters:
  • content (str) โ€“ The content to remember

  • importance (float) โ€“ Importance score (0.0 to 1.0)

  • memory_type (str) โ€“ Type of memory (โ€œshort_termโ€, โ€œsemanticโ€, โ€œepisodicโ€)

Return type:

Optional[str]

Returns:

Memory ID if successful, None otherwise

recall(query, limit=5, similarity_threshold=0.1)[source]๏ƒ

Recall memories based on a query

Parameters:
  • query (str) โ€“ Search query

  • limit (int) โ€“ Maximum number of results

  • similarity_threshold (float) โ€“ Minimum similarity score

Return type:

List

Returns:

List of MemoryEntry objects

recall_by_id(memory_id)[source]๏ƒ

Recall a specific memory by ID (for resolving spore references)

Return type:

List

Parameters:

memory_id (str)

get_conversation_context(turns=10)[source]๏ƒ

Get recent conversation context

Return type:

List

Parameters:

turns (int)

create_knowledge_reference(content, importance=0.8)[source]๏ƒ

Create knowledge references for lightweight spores

Parameters:
  • content (str) โ€“ Knowledge content to store and reference

  • importance (float) โ€“ Importance threshold

Return type:

List[str]

Returns:

List of knowledge reference IDs

resolve_spore_knowledge(spore)[source]๏ƒ

Resolve knowledge references in a spore

Parameters:

spore โ€“ Spore object with potential knowledge references

Return type:

Dict[str, Any]

Returns:

Complete knowledge including resolved references

send_lightweight_knowledge(to_agent, large_content, summary, channel='main')[source]๏ƒ

Send large knowledge as lightweight spore with references

Parameters:
  • to_agent (str) โ€“ Target agent

  • large_content (str) โ€“ Large content to reference

  • summary (str) โ€“ Brief summary for the spore

  • channel (str) โ€“ Communication channel

Return type:

str

Returns:

Spore ID